Skip to content
This repository
Newer
Older
100644 403 lines (293 sloc) 11.262 kb
60818b95 »
2011-11-02 Add missing copyright headers
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
ff4a9d38 »
2012-03-10 core: use proper #include directives
22 #include "node.h"
23 #include "node_buffer.h"
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
24 #include "slab_allocator.h"
ff4a9d38 »
2012-03-10 core: use proper #include directives
25 #include "req_wrap.h"
26 #include "handle_wrap.h"
af98fc9d »
2012-05-16 child_process: new stdio API for .spawn() method
27 #include "udp_wrap.h"
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
28
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
29 #include <stdlib.h>
30
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
31 #define SLAB_SIZE (1024 * 1024)
32
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
33
34 using namespace v8;
35
36 namespace node {
37
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
38 typedef ReqWrap<uv_udp_send_t> SendWrap;
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
39
88021964 »
2012-05-02 tcp, udp: share sockaddr-to-object function
40 // see tcp_wrap.cc
7c02b5a5 »
2012-03-30 udp_wrap: simplify AddressToJS() function
41 Local<Object> AddressToJS(const sockaddr* addr);
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
42
43 static Persistent<String> buffer_sym;
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
44 static Persistent<String> oncomplete_sym;
45 static Persistent<String> onmessage_sym;
27061cc9 »
2012-06-05 udp_wrap, stream_wrap: lazy init slab allocator
46 static SlabAllocator* slab_allocator;
47
48
49 static void DeleteSlabAllocator(void*) {
50 delete slab_allocator;
51 slab_allocator = NULL;
52 }
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
53
bd9fa2e8 »
2012-01-11 dgram: use slab memory allocator
54
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
55 UDPWrap::UDPWrap(Handle<Object> object): HandleWrap(object,
56 (uv_handle_t*)&handle_) {
74a8215a »
2012-02-03 Revert support for isolates.
57 int r = uv_udp_init(uv_default_loop(), &handle_);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
58 assert(r == 0); // can't fail anyway
59 handle_.data = reinterpret_cast<void*>(this);
60 }
61
62
63 UDPWrap::~UDPWrap() {
64 }
65
66
67 void UDPWrap::Initialize(Handle<Object> target) {
68 HandleWrap::Initialize(target);
69
27061cc9 »
2012-06-05 udp_wrap, stream_wrap: lazy init slab allocator
70 slab_allocator = new SlabAllocator(SLAB_SIZE);
71 AtExit(DeleteSlabAllocator, NULL);
72
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
73 HandleScope scope;
74
75 buffer_sym = NODE_PSYMBOL("buffer");
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
76 oncomplete_sym = NODE_PSYMBOL("oncomplete");
77 onmessage_sym = NODE_PSYMBOL("onmessage");
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
78
79 Local<FunctionTemplate> t = FunctionTemplate::New(New);
80 t->InstanceTemplate()->SetInternalFieldCount(1);
81 t->SetClassName(String::NewSymbol("UDP"));
82
83 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
84 NODE_SET_PROTOTYPE_METHOD(t, "send", Send);
85 NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
86 NODE_SET_PROTOTYPE_METHOD(t, "send6", Send6);
87 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
88 NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
89 NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
90 NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
91 NODE_SET_PROTOTYPE_METHOD(t, "addMembership", AddMembership);
92 NODE_SET_PROTOTYPE_METHOD(t, "dropMembership", DropMembership);
f2b1f57f »
2011-11-22 dgram: reintroduce setMulticastTTL()
93 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastTTL", SetMulticastTTL);
46e86aa8 »
2012-01-23 dgram: bring back setMulticastLoopback()
94 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastLoopback", SetMulticastLoopback);
f749338e »
2011-10-27 dgram: reintroduce setBroadcast()
95 NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
2775c0e9 »
2012-01-23 dgram: bring back setTTL()
96 NODE_SET_PROTOTYPE_METHOD(t, "setTTL", SetTTL);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
97
19d43f85 »
2012-07-12 export HandleWrap Unref Ref in tcp/udp/timer/pipe
98 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
99 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
100
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
101 target->Set(String::NewSymbol("UDP"),
102 Persistent<FunctionTemplate>::New(t)->GetFunction());
103 }
104
105
106 Handle<Value> UDPWrap::New(const Arguments& args) {
107 HandleScope scope;
108
109 assert(args.IsConstructCall());
110 new UDPWrap(args.This());
111
112 return scope.Close(args.This());
113 }
114
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
115 Handle<Value> UDPWrap::DoBind(const Arguments& args, int family) {
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
116 HandleScope scope;
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
117 int r;
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
118
45de259b »
2012-05-21 Make UNWRAP macro generic.
119 UNWRAP(UDPWrap)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
120
121 // bind(ip, port, flags)
122 assert(args.Length() == 3);
123
249c3c16 »
2012-03-21 Avoiding unnecessary ToString() calls
124 String::Utf8Value address(args[0]);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
125 const int port = args[1]->Uint32Value();
126 const int flags = args[2]->Uint32Value();
127
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
128 switch (family) {
129 case AF_INET:
130 r = uv_udp_bind(&wrap->handle_, uv_ip4_addr(*address, port), flags);
131 break;
132 case AF_INET6:
133 r = uv_udp_bind6(&wrap->handle_, uv_ip6_addr(*address, port), flags);
134 break;
135 default:
136 assert(0 && "unexpected address family");
137 abort();
138 }
139
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
140 if (r)
74a8215a »
2012-02-03 Revert support for isolates.
141 SetErrno(uv_last_error(uv_default_loop()));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
142
143 return scope.Close(Integer::New(r));
144 }
145
146
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
147 Handle<Value> UDPWrap::Bind(const Arguments& args) {
148 return DoBind(args, AF_INET);
149 }
150
151
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
152 Handle<Value> UDPWrap::Bind6(const Arguments& args) {
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
153 return DoBind(args, AF_INET6);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
154 }
155
f749338e »
2011-10-27 dgram: reintroduce setBroadcast()
156
2775c0e9 »
2012-01-23 dgram: bring back setTTL()
157 #define X(name, fn) \
158 Handle<Value> UDPWrap::name(const Arguments& args) { \
159 HandleScope scope; \
45de259b »
2012-05-21 Make UNWRAP macro generic.
160 UNWRAP(UDPWrap) \
2775c0e9 »
2012-01-23 dgram: bring back setTTL()
161 assert(args.Length() == 1); \
162 int flag = args[0]->Int32Value(); \
163 int r = fn(&wrap->handle_, flag); \
164 if (r) SetErrno(uv_last_error(uv_default_loop())); \
165 return scope.Close(Integer::New(r)); \
166 }
f749338e »
2011-10-27 dgram: reintroduce setBroadcast()
167
2775c0e9 »
2012-01-23 dgram: bring back setTTL()
168 X(SetTTL, uv_udp_set_ttl)
169 X(SetBroadcast, uv_udp_set_broadcast)
170 X(SetMulticastTTL, uv_udp_set_multicast_ttl)
171 X(SetMulticastLoopback, uv_udp_set_multicast_loop)
f749338e »
2011-10-27 dgram: reintroduce setBroadcast()
172
2775c0e9 »
2012-01-23 dgram: bring back setTTL()
173 #undef X
f749338e »
2011-10-27 dgram: reintroduce setBroadcast()
174
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
175
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
176 Handle<Value> UDPWrap::SetMembership(const Arguments& args,
177 uv_membership membership) {
178 HandleScope scope;
45de259b »
2012-05-21 Make UNWRAP macro generic.
179 UNWRAP(UDPWrap)
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
180
181 assert(args.Length() == 2);
182
249c3c16 »
2012-03-21 Avoiding unnecessary ToString() calls
183 String::Utf8Value address(args[0]);
184 String::Utf8Value iface(args[1]);
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
185
b1b16d11 »
2012-01-23 udp: 'interface' is reserved word on windows.
186 const char* iface_cstr = *iface;
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
187 if (args[1]->IsUndefined() || args[1]->IsNull()) {
b1b16d11 »
2012-01-23 udp: 'interface' is reserved word on windows.
188 iface_cstr = NULL;
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
189 }
190
b1b16d11 »
2012-01-23 udp: 'interface' is reserved word on windows.
191 int r = uv_udp_set_membership(&wrap->handle_, *address, iface_cstr,
a38fd605 »
2011-10-12 dgram: reintroduce addMembership() and dropMembership()
192 membership);
193
194 if (r)
195 SetErrno(uv_last_error(uv_default_loop()));
196
197 return scope.Close(Integer::New(r));
198 }
199
200
201 Handle<Value> UDPWrap::AddMembership(const Arguments& args) {
202 return SetMembership(args, UV_JOIN_GROUP);
203 }
204
205
206 Handle<Value> UDPWrap::DropMembership(const Arguments& args) {
207 return SetMembership(args, UV_LEAVE_GROUP);
208 }
209
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
210
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
211 Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
212 HandleScope scope;
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
213 int r;
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
214
215 // send(buffer, offset, length, port, address)
216 assert(args.Length() == 5);
217
45de259b »
2012-05-21 Make UNWRAP macro generic.
218 UNWRAP(UDPWrap)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
219
220 assert(Buffer::HasInstance(args[0]));
221 Local<Object> buffer_obj = args[0]->ToObject();
222
223 size_t offset = args[1]->Uint32Value();
224 size_t length = args[2]->Uint32Value();
defa6373 »
2012-02-23 dgram: fix out-of-bound memory read
225 assert(offset < Buffer::Length(buffer_obj));
226 assert(length <= Buffer::Length(buffer_obj) - offset);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
227
228 SendWrap* req_wrap = new SendWrap();
229 req_wrap->object_->SetHiddenValue(buffer_sym, buffer_obj);
230
231 uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset,
232 length);
233
234 const unsigned short port = args[3]->Uint32Value();
249c3c16 »
2012-03-21 Avoiding unnecessary ToString() calls
235 String::Utf8Value address(args[4]);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
236
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
237 switch (family) {
238 case AF_INET:
239 r = uv_udp_send(&req_wrap->req_, &wrap->handle_, &buf, 1,
240 uv_ip4_addr(*address, port), OnSend);
241 break;
242 case AF_INET6:
243 r = uv_udp_send6(&req_wrap->req_, &wrap->handle_, &buf, 1,
244 uv_ip6_addr(*address, port), OnSend);
245 break;
246 default:
247 assert(0 && "unexpected address family");
248 abort();
249 }
250
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
251 req_wrap->Dispatched();
252
253 if (r) {
74a8215a »
2012-02-03 Revert support for isolates.
254 SetErrno(uv_last_error(uv_default_loop()));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
255 delete req_wrap;
256 return Null();
257 }
258 else {
259 return scope.Close(req_wrap->object_);
260 }
261 }
262
263
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
264 Handle<Value> UDPWrap::Send(const Arguments& args) {
265 return DoSend(args, AF_INET);
266 }
267
268
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
269 Handle<Value> UDPWrap::Send6(const Arguments& args) {
11e3cfce »
2011-08-24 dgram: integrate libuv support for UDP over IPv6
270 return DoSend(args, AF_INET6);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
271 }
272
273
274 Handle<Value> UDPWrap::RecvStart(const Arguments& args) {
275 HandleScope scope;
276
45de259b »
2012-05-21 Make UNWRAP macro generic.
277 UNWRAP(UDPWrap)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
278
279 // UV_EALREADY means that the socket is already bound but that's okay
280 int r = uv_udp_recv_start(&wrap->handle_, OnAlloc, OnRecv);
74a8215a »
2012-02-03 Revert support for isolates.
281 if (r && uv_last_error(uv_default_loop()).code != UV_EALREADY) {
282 SetErrno(uv_last_error(uv_default_loop()));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
283 return False();
284 }
285
286 return True();
287 }
288
289
290 Handle<Value> UDPWrap::RecvStop(const Arguments& args) {
291 HandleScope scope;
292
45de259b »
2012-05-21 Make UNWRAP macro generic.
293 UNWRAP(UDPWrap)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
294
295 int r = uv_udp_recv_stop(&wrap->handle_);
296
297 return scope.Close(Integer::New(r));
298 }
299
300
301 Handle<Value> UDPWrap::GetSockName(const Arguments& args) {
302 HandleScope scope;
303 struct sockaddr_storage address;
304
45de259b »
2012-05-21 Make UNWRAP macro generic.
305 UNWRAP(UDPWrap)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
306
307 int addrlen = sizeof(address);
c82ab45e »
2011-09-04 uv_getsockname got renamed
308 int r = uv_udp_getsockname(&wrap->handle_,
309 reinterpret_cast<sockaddr*>(&address),
310 &addrlen);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
311
7c02b5a5 »
2012-03-30 udp_wrap: simplify AddressToJS() function
312 if (r) {
74a8215a »
2012-02-03 Revert support for isolates.
313 SetErrno(uv_last_error(uv_default_loop()));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
314 return Null();
315 }
7c02b5a5 »
2012-03-30 udp_wrap: simplify AddressToJS() function
316
317 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
318 return scope.Close(AddressToJS(addr));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
319 }
320
321
322 // TODO share with StreamWrap::AfterWrite() in stream_wrap.cc
323 void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
324 HandleScope scope;
325
326 assert(req != NULL);
327
328 SendWrap* req_wrap = reinterpret_cast<SendWrap*>(req->data);
329 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(req->handle->data);
330
331 assert(req_wrap->object_.IsEmpty() == false);
332 assert(wrap->object_.IsEmpty() == false);
333
334 if (status) {
74a8215a »
2012-02-03 Revert support for isolates.
335 SetErrno(uv_last_error(uv_default_loop()));
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
336 }
337
338 Local<Value> argv[4] = {
339 Integer::New(status),
340 Local<Value>::New(wrap->object_),
341 Local<Value>::New(req_wrap->object_),
342 req_wrap->object_->GetHiddenValue(buffer_sym),
343 };
344
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
345 MakeCallback(req_wrap->object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
346 delete req_wrap;
347 }
348
349
350 uv_buf_t UDPWrap::OnAlloc(uv_handle_t* handle, size_t suggested_size) {
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
351 UDPWrap* wrap = static_cast<UDPWrap*>(handle->data);
27061cc9 »
2012-06-05 udp_wrap, stream_wrap: lazy init slab allocator
352 char* buf = slab_allocator->Allocate(wrap->object_, suggested_size);
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
353 return uv_buf_init(buf, suggested_size);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
354 }
355
356
357 void UDPWrap::OnRecv(uv_udp_t* handle,
358 ssize_t nread,
359 uv_buf_t buf,
360 struct sockaddr* addr,
361 unsigned flags) {
32b2964e »
2012-03-26 udp: remove slab allocator
362 HandleScope scope;
363
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
364 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(handle->data);
27061cc9 »
2012-06-05 udp_wrap, stream_wrap: lazy init slab allocator
365 Local<Object> slab = slab_allocator->Shrink(wrap->object_,
366 buf.base,
367 nread < 0 ? 0 : nread);
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
368 if (nread == 0) return;
369
370 if (nread < 0) {
371 Local<Value> argv[] = { Local<Object>::New(wrap->object_) };
372 SetErrno(uv_last_error(uv_default_loop()));
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
373 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
374 return;
375 }
376
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
377 Local<Value> argv[] = {
f86359cc »
2012-03-26 udp: root JS objects in HandleScope with Local<>
378 Local<Object>::New(wrap->object_),
1ab95a53 »
2012-03-30 udp_wrap: use new slab allocator
379 slab,
380 Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)),
381 Integer::NewFromUnsigned(nread),
7c02b5a5 »
2012-03-30 udp_wrap: simplify AddressToJS() function
382 AddressToJS(addr)
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
383 };
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
384 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
385 }
386
387
af98fc9d »
2012-05-16 child_process: new stdio API for .spawn() method
388 UDPWrap* UDPWrap::Unwrap(Local<Object> obj) {
389 assert(!obj.IsEmpty());
390 assert(obj->InternalFieldCount() > 0);
391 return static_cast<UDPWrap*>(obj->GetPointerFromInternalField(0));
392 }
393
394
395 uv_udp_t* UDPWrap::UVHandle() {
396 return &handle_;
397 }
398
399
cbd40336 »
2011-08-20 dgram: integrate libuv UDP support
400 } // namespace node
401
cdcb1118 »
2011-11-09 Remove stray NODE_MODULE() semi-colons.
402 NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize)
Something went wrong with that request. Please try again.