Skip to content

Commit 1422642

Browse files
committed
Check on message if source is the right one (fix #5)
1 parent 5b63459 commit 1422642

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

index.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,22 @@ class ICMP {
6868
});
6969

7070
this.socket.on('message', (buffer, source) => {
71-
const NS_PER_SEC = 1e9;
71+
if (source === this.ip) {
72+
const NS_PER_SEC = 1e9;
7273

73-
this.diff = process.hrtime(this.start);
74-
this.elapsed = (this.diff[0] + this.diff[1] / NS_PER_SEC) * 1000;
74+
this.diff = process.hrtime(this.start);
75+
this.elapsed = (this.diff[0] + this.diff[1] / NS_PER_SEC) * 1000;
7576

76-
const offset = 20;
77-
const type = buffer.readUInt8(offset);
78-
const code = buffer.readUInt8(offset + 1);
77+
const offset = 20;
78+
const type = buffer.readUInt8(offset);
79+
const code = buffer.readUInt8(offset + 1);
7980

80-
this.parse(type, code);
81+
this.parse(type, code);
8182

82-
this.close();
83+
this.close();
8384

84-
resolve();
85+
resolve();
86+
}
8587
});
8688

8789
this.socket.on('error', err => {
@@ -116,21 +118,16 @@ class ICMP {
116118
return raw.writeChecksum(header, 2, raw.createChecksum(header));
117119
}
118120

119-
send(data = "", timeout = 5000) {
120-
return new Promise((resolve, reject) => {
121-
const header = this.createHeader(data);
122-
123-
this._queue(header, timeout).then(resolve, reject);
124-
});
121+
async send(data = "", timeout = 5000) {
122+
const header = this.createHeader(data);
123+
return await this._queue(header, timeout);
125124
}
126125

127-
static send(host, data = "", timeout = 5000) {
128-
const obj = new this(host);
126+
static async send(host, data = "", timeout = 5000) {
127+
const obj = new ICMP(host);
129128

130-
return new Promise((resolve, reject) => obj.send(data, timeout)
131-
.then(() => resolve(obj))
132-
.catch(err => reject(err))
133-
);
129+
await obj.send(data, timeout);
130+
return obj;
134131
}
135132

136133
ping(timeout = 5000) {
@@ -141,12 +138,12 @@ class ICMP {
141138
return this.send(host, '', timeout);
142139
}
143140

144-
listen(cb = (buffer, source) => {}) {
141+
listen(cb = (buffer, source) => { }) {
145142
return this.socket.on('message', cb);
146143
}
147144

148-
static listen(cb = (buffer, source) => {}) {
149-
const obj = new this(null);
145+
static listen(cb = (buffer, source) => { }) {
146+
const obj = new ICMP(null);
150147
return obj.listen(cb);
151148
}
152149

0 commit comments

Comments
 (0)