forked from devDomotz/node-raw-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (179 loc) · 4.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var events = require ("events");
var net = require ("net");
var raw = require ("./build/Release/raw.node");
var util = require ("util");
function _expandConstantObject (object) {
var keys = [];
for (key in object)
keys.push (key);
for (var i = 0; i < keys.length; i++)
object[object[keys[i]]] = parseInt (keys[i]);
}
var AddressFamily = {
1: "IPv4",
2: "IPv6"
};
_expandConstantObject (AddressFamily);
var Protocol = {
0: "None",
1: "ICMP",
6: "TCP",
17: "UDP",
58: "ICMPv6"
};
_expandConstantObject (Protocol);
for (var key in events.EventEmitter.prototype) {
raw.SocketWrap.prototype[key] = events.EventEmitter.prototype[key];
}
function Socket (options) {
Socket.super_.call (this);
this.requests = [];
this.buffer = Buffer.alloc((options && options.bufferSize)
? options.bufferSize
: 4096);
this.recvPaused = false;
this.sendPaused = true;
this.wrap = new raw.SocketWrap (
((options && options.protocol)
? options.protocol
: 0),
((options && options.addressFamily)
? options.addressFamily
: AddressFamily.IPv4)
);
var me = this;
this.wrap.on ("sendReady", this.onSendReady.bind (me));
this.wrap.on ("recvReady", this.onRecvReady.bind (me));
this.wrap.on ("error", this.onError.bind (me));
this.wrap.on ("close", this.onClose.bind (me));
};
util.inherits (Socket, events.EventEmitter);
Socket.prototype.close = function () {
this.wrap.close ();
return this;
}
Socket.prototype.getOption = function (level, option, value, length) {
return this.wrap.getOption (level, option, value, length);
}
Socket.prototype.onClose = function () {
this.emit ("close");
}
Socket.prototype.onError = function (error) {
this.emit ("error", error);
this.close ();
}
Socket.prototype.onRecvReady = function () {
var me = this;
try {
this.wrap.recv (this.buffer, function (buffer, bytes, source) {
var newBuffer = buffer.slice (0, bytes);
me.emit ("message", newBuffer, source);
});
} catch (error) {
me.emit ("error", error);
}
}
Socket.prototype.onSendReady = function () {
if (this.requests.length > 0) {
var me = this;
var req = this.requests.shift ();
try {
if (req.beforeCallback)
req.beforeCallback ();
this.wrap.send (req.buffer, req.offset, req.length,
req.address, function (bytes) {
req.afterCallback.call (me, null, bytes);
});
} catch (error) {
req.afterCallback.call (me, error, 0);
}
} else {
if (! this.sendPaused)
this.pauseSend ();
}
}
Socket.prototype.pauseRecv = function () {
this.recvPaused = true;
this.wrap.pause (this.recvPaused, this.sendPaused);
return this;
}
Socket.prototype.pauseSend = function () {
this.sendPaused = true;
this.wrap.pause (this.recvPaused, this.sendPaused);
return this;
}
Socket.prototype.resumeRecv = function () {
this.recvPaused = false;
this.wrap.pause (this.recvPaused, this.sendPaused);
return this;
}
Socket.prototype.resumeSend = function () {
this.sendPaused = false;
this.wrap.pause (this.recvPaused, this.sendPaused);
return this;
}
Socket.prototype.send = function (buffer, offset, length, address,
beforeCallback, afterCallback) {
if (! afterCallback) {
afterCallback = beforeCallback;
beforeCallback = null;
}
if (length + offset > buffer.length) {
afterCallback.call (this, new Error ("Buffer length '" + buffer.length
+ "' is not large enough for the specified offset '" + offset
+ "' plus length '" + length + "'"));
return this;
}
if (! net.isIP (address)) {
afterCallback.call (this, new Error ("Invalid IP address '" + address + "'"));
return this;
}
var req = {
buffer: buffer,
offset: offset,
length: length,
address: address,
afterCallback: afterCallback,
beforeCallback: beforeCallback
};
this.requests.push (req);
if (this.sendPaused)
this.resumeSend ();
return this;
}
Socket.prototype.setOption = function (level, option, value, length) {
if (arguments.length > 3)
this.wrap.setOption (level, option, value, length);
else
this.wrap.setOption (level, option, value);
}
exports.createChecksum = function () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
var object = arguments[i];
if (object instanceof Buffer) {
sum = raw.createChecksum (sum, object, 0, object.length);
} else {
sum = raw.createChecksum (sum, object.buffer, object.offset,
object.length);
}
}
return sum;
}
exports.writeChecksum = function (buffer, offset, checksum) {
buffer.writeUInt8 ((checksum & 0xff00) >> 8, offset);
buffer.writeUInt8 (checksum & 0xff, offset + 1);
return buffer;
}
exports.createSocket = function (options) {
return new Socket (options || {});
};
exports.AddressFamily = AddressFamily;
exports.Protocol = Protocol;
exports.Socket = Socket;
exports.SocketLevel = raw.SocketLevel;
exports.SocketOption = raw.SocketOption;
exports.htonl = raw.htonl;
exports.htons = raw.htons;
exports.ntohl = raw.ntohl;
exports.ntohs = raw.ntohs;