FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/udp_wrap.cc at master · CombatCode/node · GitHub
CombatCode
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
udp_wrap.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
421 lines (319 loc) · 12 KB
Breadcrumbs
node
/
src
/
udp_wrap.cc
Copy path
File metadata and controls
421 lines (319 loc) · 12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
Copyright Joyent, Inc. and other Node contributors.
//
//
Permission is hereby granted, free of charge, to any person obtaining a
//
copy of this software and associated documentation files (the
//
"Software"), to deal in the Software without restriction, including
//
without limitation the rights to use, copy, modify, merge, publish,
//
distribute, sublicense, and/or sell copies of the Software, and to permit
//
persons to whom the Software is furnished to do so, subject to the
//
following conditions:
//
//
The above copyright notice and this permission notice shall be included
//
in all copies or substantial portions of the Software.
//
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
//
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
//
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
//
USE OR OTHER DEALINGS IN THE SOFTWARE.
#
include
"
node.h
"
#
include
"
node_buffer.h
"
#
include
"
req_wrap.h
"
#
include
"
handle_wrap.h
"
#
include
"
udp_wrap.h
"
#
include
<
stdlib.h
>
namespace
node
{
using
v8::Function;
using
v8::FunctionCallbackInfo;
using
v8::FunctionTemplate;
using
v8::Handle;
using
v8::HandleScope;
using
v8::Integer;
using
v8::Local;
using
v8::Object;
using
v8::Persistent;
using
v8::PropertyAttribute;
using
v8::PropertyCallbackInfo;
using
v8::String;
using
v8::Uint32;
using
v8::Value;
typedef
ReqWrap<
uv_udp_send_t
> SendWrap;
//
see tcp_wrap.cc
Local<Object>
AddressToJS
(
const
sockaddr* addr);
static
Persistent<Function> constructor;
static
Cached<String> buffer_sym;
static
Cached<String> oncomplete_sym;
static
Cached<String> onmessage_sym;
UDPWrap::UDPWrap
(Handle<Object> object)
: HandleWrap(object,
reinterpret_cast
<
uv_handle_t
*>(&handle_)) {
int
r =
uv_udp_init
(
uv_default_loop
(), &handle_);
assert
(r ==
0
);
//
can't fail anyway
}
UDPWrap::~UDPWrap
() {
}
void
UDPWrap::Initialize
(Handle<Object> target) {
HandleWrap::Initialize
(target);
HandleScope
scope
(node_isolate);
buffer_sym =
String::New
(
"
buffer
"
);
oncomplete_sym =
String::New
(
"
oncomplete
"
);
onmessage_sym =
String::New
(
"
onmessage
"
);
Local<FunctionTemplate> t =
FunctionTemplate::New
(New);
t->
InstanceTemplate
()->
SetInternalFieldCount
(
1
);
t->
SetClassName
(
String::NewSymbol
(
"
UDP
"
));
enum
PropertyAttribute attributes =
static_cast
<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
t->
InstanceTemplate
()->
SetAccessor
(
String::New
(
"
fd
"
),
UDPWrap::GetFD,
NULL
,
Handle<Value>(),
v8::
DEFAULT
,
attributes);
NODE_SET_PROTOTYPE_METHOD
(t,
"
bind
"
, Bind);
NODE_SET_PROTOTYPE_METHOD
(t,
"
send
"
, Send);
NODE_SET_PROTOTYPE_METHOD
(t,
"
bind6
"
, Bind6);
NODE_SET_PROTOTYPE_METHOD
(t,
"
send6
"
, Send6);
NODE_SET_PROTOTYPE_METHOD
(t,
"
close
"
, Close);
NODE_SET_PROTOTYPE_METHOD
(t,
"
recvStart
"
, RecvStart);
NODE_SET_PROTOTYPE_METHOD
(t,
"
recvStop
"
, RecvStop);
NODE_SET_PROTOTYPE_METHOD
(t,
"
getsockname
"
, GetSockName);
NODE_SET_PROTOTYPE_METHOD
(t,
"
addMembership
"
, AddMembership);
NODE_SET_PROTOTYPE_METHOD
(t,
"
dropMembership
"
, DropMembership);
NODE_SET_PROTOTYPE_METHOD
(t,
"
setMulticastTTL
"
, SetMulticastTTL);
NODE_SET_PROTOTYPE_METHOD
(t,
"
setMulticastLoopback
"
, SetMulticastLoopback);
NODE_SET_PROTOTYPE_METHOD
(t,
"
setBroadcast
"
, SetBroadcast);
NODE_SET_PROTOTYPE_METHOD
(t,
"
setTTL
"
, SetTTL);
NODE_SET_PROTOTYPE_METHOD
(t,
"
ref
"
, HandleWrap::Ref);
NODE_SET_PROTOTYPE_METHOD
(t,
"
unref
"
, HandleWrap::Unref);
constructor.
Reset
(node_isolate, t->
GetFunction
());
target->
Set
(
String::NewSymbol
(
"
UDP
"
), t->
GetFunction
());
}
void
UDPWrap::New
(
const
FunctionCallbackInfo<Value>& args) {
HandleScope
scope
(node_isolate);
assert
(args.
IsConstructCall
());
new
UDPWrap
(args.
This
());
}
void
UDPWrap::GetFD
(Local<String>,
const
PropertyCallbackInfo<Value>& args) {
#
if
!defined(_WIN32)
HandleScope
scope
(node_isolate);
UNWRAP
(UDPWrap)
int
fd = (wrap ==
NULL
) ? -
1
: wrap->
handle_
.
io_watcher
.
fd
;
args.
GetReturnValue
().
Set
(fd);
#
endif
}
void
UDPWrap::DoBind
(
const
FunctionCallbackInfo<Value>& args,
int
family) {
HandleScope
scope
(node_isolate);
int
r;
UNWRAP
(UDPWrap)
//
bind(ip, port, flags)
assert
(args.
Length
() ==
3
);
String::Utf8Value
address
(args[
0
]);
const
int
port = args[
1
]->
Uint32Value
();
const
int
flags = args[
2
]->
Uint32Value
();
switch
(family) {
case
AF_INET
:
r =
uv_udp_bind
(&wrap->
handle_
,
uv_ip4_addr
(*address, port), flags);
break
;
case
AF_INET6
:
r =
uv_udp_bind6
(&wrap->
handle_
,
uv_ip6_addr
(*address, port), flags);
break
;
default
:
assert
(
0
&&
"
unexpected address family
"
);
abort
();
}
if
(r)
SetErrno
(
uv_last_error
(
uv_default_loop
()));
args.
GetReturnValue
().
Set
(r);
}
void
UDPWrap::Bind
(
const
FunctionCallbackInfo<Value>& args) {
DoBind
(args,
AF_INET
);
}
void
UDPWrap::Bind6
(
const
FunctionCallbackInfo<Value>& args) {
DoBind
(args,
AF_INET6
);
}
#
define
X
(
name, fn
) \
void
UDPWrap::name
(
const
FunctionCallbackInfo<Value>& args) { \
HandleScope
scope
(node_isolate); \
UNWRAP
(UDPWrap) \
assert
(args.
Length
() ==
1
); \
int
flag = args[
0
]->
Int32Value
(); \
int
r =
fn
(&wrap->
handle_
, flag); \
if
(r)
SetErrno
(
uv_last_error
(
uv_default_loop
())); \
args.
GetReturnValue
().
Set
(r); \
}
X
(SetTTL, uv_udp_set_ttl)
X
(SetBroadcast, uv_udp_set_broadcast)
X
(SetMulticastTTL, uv_udp_set_multicast_ttl)
X
(SetMulticastLoopback, uv_udp_set_multicast_loop)
#
undef
X
void
UDPWrap::SetMembership
(
const
FunctionCallbackInfo<Value>& args,
uv_membership membership) {
HandleScope
scope
(node_isolate);
UNWRAP
(UDPWrap)
assert
(args.
Length
() ==
2
);
String::Utf8Value
address
(args[
0
]);
String::Utf8Value
iface
(args[
1
]);
const
char
* iface_cstr = *iface;
if
(args[
1
]->
IsUndefined
() || args[
1
]->
IsNull
()) {
iface_cstr =
NULL
;
}
int
r =
uv_udp_set_membership
(&wrap->
handle_
,
*address,
iface_cstr,
membership);
if
(r)
SetErrno
(
uv_last_error
(
uv_default_loop
()));
args.
GetReturnValue
().
Set
(r);
}
void
UDPWrap::AddMembership
(
const
FunctionCallbackInfo<Value>& args) {
SetMembership
(args,
UV_JOIN_GROUP
);
}
void
UDPWrap::DropMembership
(
const
FunctionCallbackInfo<Value>& args) {
SetMembership
(args,
UV_LEAVE_GROUP
);
}
void
UDPWrap::DoSend
(
const
FunctionCallbackInfo<Value>& args,
int
family) {
HandleScope
scope
(node_isolate);
int
r;
//
send(buffer, offset, length, port, address)
assert
(args.
Length
() ==
5
);
UNWRAP
(UDPWrap)
assert
(
Buffer::HasInstance
(args[
0
]));
Local<Object> buffer_obj = args[
0
]->
ToObject
();
size_t
offset = args[
1
]->
Uint32Value
();
size_t
length = args[
2
]->
Uint32Value
();
assert
(offset <
Buffer::Length
(buffer_obj));
assert
(length <=
Buffer::Length
(buffer_obj) - offset);
SendWrap* req_wrap =
new
SendWrap
();
req_wrap->
object
()->
SetHiddenValue
(buffer_sym, buffer_obj);
uv_buf_t
buf =
uv_buf_init
(
Buffer::Data
(buffer_obj) + offset,
length);
const
unsigned
short
port = args[
3
]->
Uint32Value
();
String::Utf8Value
address
(args[
4
]);
switch
(family) {
case
AF_INET
:
r =
uv_udp_send
(&req_wrap->
req_
, &wrap->
handle_
, &buf,
1
,
uv_ip4_addr
(*address, port), OnSend);
break
;
case
AF_INET6
:
r =
uv_udp_send6
(&req_wrap->
req_
, &wrap->
handle_
, &buf,
1
,
uv_ip6_addr
(*address, port), OnSend);
break
;
default
:
assert
(
0
&&
"
unexpected address family
"
);
abort
();
}
req_wrap->
Dispatched
();
if
(r) {
SetErrno
(
uv_last_error
(
uv_default_loop
()));
delete
req_wrap;
}
else
{
args.
GetReturnValue
().
Set
(req_wrap->
persistent
());
}
}
void
UDPWrap::Send
(
const
FunctionCallbackInfo<Value>& args) {
DoSend
(args,
AF_INET
);
}
void
UDPWrap::Send6
(
const
FunctionCallbackInfo<Value>& args) {
DoSend
(args,
AF_INET6
);
}
void
UDPWrap::RecvStart
(
const
FunctionCallbackInfo<Value>& args) {
HandleScope
scope
(node_isolate);
UNWRAP
(UDPWrap)
//
UV_EALREADY means that the socket is already bound but that's okay
int
r =
uv_udp_recv_start
(&wrap->
handle_
, OnAlloc, OnRecv);
bool
ok = r ==
0
||
uv_last_error
(
uv_default_loop
()).
code
==
UV_EALREADY
;
if
(ok ==
false
)
SetErrno
(
uv_last_error
(
uv_default_loop
()));
args.
GetReturnValue
().
Set
(ok);
}
void
UDPWrap::RecvStop
(
const
FunctionCallbackInfo<Value>& args) {
HandleScope
scope
(node_isolate);
UNWRAP
(UDPWrap)
int
r =
uv_udp_recv_stop
(&wrap->
handle_
);
args.
GetReturnValue
().
Set
(r);
}
void
UDPWrap::GetSockName
(
const
FunctionCallbackInfo<Value>& args) {
HandleScope
scope
(node_isolate);
struct
sockaddr_storage
address;
UNWRAP
(UDPWrap)
int
addrlen =
sizeof
(address);
int
r =
uv_udp_getsockname
(&wrap->
handle_
,
reinterpret_cast
<sockaddr*>(&address),
&addrlen);
if
(r)
return
SetErrno
(
uv_last_error
(
uv_default_loop
()));
const
sockaddr* addr =
reinterpret_cast
<
const
sockaddr*>(&address);
args.
GetReturnValue
().
Set
(
AddressToJS
(addr));
}
//
TODO share with StreamWrap::AfterWrite() in stream_wrap.cc
void
UDPWrap::OnSend
(
uv_udp_send_t
* req,
int
status) {
HandleScope
scope
(node_isolate);
assert
(req !=
NULL
);
SendWrap* req_wrap =
reinterpret_cast
<SendWrap*>(req->
data
);
UDPWrap* wrap =
reinterpret_cast
<UDPWrap*>(req->
handle
->
data
);
assert
(req_wrap->
persistent
().
IsEmpty
() ==
false
);
assert
(wrap->
persistent
().
IsEmpty
() ==
false
);
if
(status) {
SetErrno
(
uv_last_error
(
uv_default_loop
()));
}
Local<Object> req_wrap_obj = req_wrap->
object
();
Local<Value> argv[
4
] = {
Integer::New
(status, node_isolate),
wrap->
object
(),
req_wrap_obj,
req_wrap_obj->
GetHiddenValue
(buffer_sym),
};
MakeCallback
(req_wrap_obj, oncomplete_sym,
ARRAY_SIZE
(argv), argv);
delete
req_wrap;
}
uv_buf_t
UDPWrap::OnAlloc
(
uv_handle_t
* handle,
size_t
suggested_size) {
char
* data =
static_cast
<
char
*>(
malloc
(suggested_size));
if
(data ==
NULL
&& suggested_size >
0
) {
FatalError
(
"
node::UDPWrap::OnAlloc(uv_handle_t*, size_t)
"
,
"
Out Of Memory
"
);
}
return
uv_buf_init
(data, suggested_size);
}
void
UDPWrap::OnRecv
(
uv_udp_t
* handle,
ssize_t
nread,
uv_buf_t
buf,
struct
sockaddr
* addr,
unsigned
flags) {
HandleScope
scope
(node_isolate);
UDPWrap* wrap =
reinterpret_cast
<UDPWrap*>(handle->
data
);
if
(nread <
0
) {
if
(buf.
base
!=
NULL
)
free
(buf.
base
);
Local<Object> wrap_obj = wrap->
object
();
Local<Value> argv[] = { wrap_obj };
SetErrno
(
uv_last_error
(
uv_default_loop
()));
MakeCallback
(wrap_obj, onmessage_sym,
ARRAY_SIZE
(argv), argv);
return
;
}
if
(nread ==
0
) {
if
(buf.
base
!=
NULL
)
free
(buf.
base
);
return
;
}
buf.
base
=
static_cast
<
char
*>(
realloc
(buf.
base
, nread));
Local<Object> wrap_obj = wrap->
object
();
Local<Value> argv[] = {
wrap_obj,
Buffer::Use
(buf.
base
, nread),
AddressToJS
(addr)
};
MakeCallback
(wrap_obj, onmessage_sym,
ARRAY_SIZE
(argv), argv);
}
UDPWrap*
UDPWrap::Unwrap
(Local<Object> obj) {
assert
(!obj.
IsEmpty
());
assert
(obj->
InternalFieldCount
() >
0
);
return
static_cast
<UDPWrap*>(obj->
GetAlignedPointerFromInternalField
(
0
));
}
Local<Object>
UDPWrap::Instantiate
() {
//
If this assert fires then Initialize hasn't been called yet.
assert
(constructor.
IsEmpty
() ==
false
);
return
NewInstance
(constructor);
}
uv_udp_t
*
UDPWrap::UVHandle
() {
return
&handle_;
}
}
//
namespace node
NODE_MODULE
(node_udp_wrap, node::UDPWrap::Initialize)
Back
|
FazBrowse Home
|
New Git URL