1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
23 #include "node_buffer.h"
24 #include "slab_allocator.h"
26 #include "handle_wrap.h"
31 #define SLAB_SIZE (1024 * 1024)
36 using v8::AccessorInfo;
39 using v8::FunctionTemplate;
41 using v8::HandleScope;
46 using v8::PropertyAttribute;
50 typedef ReqWrap<uv_udp_send_t> SendWrap;
53 Local<Object> AddressToJS(const sockaddr* addr);
55 static Persistent<Function> constructor;
56 static Persistent<String> buffer_sym;
57 static Persistent<String> oncomplete_sym;
58 static Persistent<String> onmessage_sym;
59 static SlabAllocator* slab_allocator;
62 static void DeleteSlabAllocator(void*) {
63 delete slab_allocator;
64 slab_allocator = NULL;
68 UDPWrap::UDPWrap(Handle<Object> object): HandleWrap(object,
69 (uv_handle_t*)&handle_) {
70 int r = uv_udp_init(uv_default_loop(), &handle_);
71 assert(r == 0); // can't fail anyway
72 handle_.data = reinterpret_cast<void*>(this);
80 void UDPWrap::Initialize(Handle<Object> target) {
81 HandleWrap::Initialize(target);
83 slab_allocator = new SlabAllocator(SLAB_SIZE);
84 AtExit(DeleteSlabAllocator, NULL);
86 HandleScope scope(node_isolate);
88 buffer_sym = NODE_PSYMBOL("buffer");
89 oncomplete_sym = NODE_PSYMBOL("oncomplete");
90 onmessage_sym = NODE_PSYMBOL("onmessage");
92 Local<FunctionTemplate> t = FunctionTemplate::New(New);
93 t->InstanceTemplate()->SetInternalFieldCount(1);
94 t->SetClassName(String::NewSymbol("UDP"));
96 enum PropertyAttribute attributes =
97 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
98 t->InstanceTemplate()->SetAccessor(String::New("fd"),
105 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
106 NODE_SET_PROTOTYPE_METHOD(t, "send", Send);
107 NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
108 NODE_SET_PROTOTYPE_METHOD(t, "send6", Send6);
109 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
110 NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
111 NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
112 NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
113 NODE_SET_PROTOTYPE_METHOD(t, "addMembership", AddMembership);
114 NODE_SET_PROTOTYPE_METHOD(t, "dropMembership", DropMembership);
115 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastTTL", SetMulticastTTL);
116 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastLoopback", SetMulticastLoopback);
117 NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
118 NODE_SET_PROTOTYPE_METHOD(t, "setTTL", SetTTL);
120 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
121 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
123 constructor = Persistent<Function>::New(node_isolate,
124 Persistent<FunctionTemplate>::New(node_isolate, t)->GetFunction());
125 target->Set(String::NewSymbol("UDP"), constructor);
129 Handle<Value> UDPWrap::New(const Arguments& args) {
130 HandleScope scope(node_isolate);
132 assert(args.IsConstructCall());
133 new UDPWrap(args.This());
135 return scope.Close(args.This());
139 Handle<Value> UDPWrap::GetFD(Local<String>, const AccessorInfo& args) {
141 return v8::Null(node_isolate);
143 HandleScope scope(node_isolate);
145 int fd = (wrap == NULL) ? -1 : wrap->handle_.io_watcher.fd;
146 return scope.Close(Integer::New(fd, node_isolate));
151 Handle<Value> UDPWrap::DoBind(const Arguments& args, int family) {
152 HandleScope scope(node_isolate);
157 // bind(ip, port, flags)
158 assert(args.Length() == 3);
160 String::Utf8Value address(args[0]);
161 const int port = args[1]->Uint32Value();
162 const int flags = args[2]->Uint32Value();
166 r = uv_udp_bind(&wrap->handle_, uv_ip4_addr(*address, port), flags);
169 r = uv_udp_bind6(&wrap->handle_, uv_ip6_addr(*address, port), flags);
172 assert(0 && "unexpected address family");
177 SetErrno(uv_last_error(uv_default_loop()));
179 return scope.Close(Integer::New(r, node_isolate));
183 Handle<Value> UDPWrap::Bind(const Arguments& args) {
184 return DoBind(args, AF_INET);
188 Handle<Value> UDPWrap::Bind6(const Arguments& args) {
189 return DoBind(args, AF_INET6);
193 #define X(name, fn) \
194 Handle<Value> UDPWrap::name(const Arguments& args) { \
195 HandleScope scope(node_isolate); \
197 assert(args.Length() == 1); \
198 int flag = args[0]->Int32Value(); \
199 int r = fn(&wrap->handle_, flag); \
200 if (r) SetErrno(uv_last_error(uv_default_loop())); \
201 return scope.Close(Integer::New(r, node_isolate)); \
204 X(SetTTL, uv_udp_set_ttl)
205 X(SetBroadcast, uv_udp_set_broadcast)
206 X(SetMulticastTTL, uv_udp_set_multicast_ttl)
207 X(SetMulticastLoopback, uv_udp_set_multicast_loop)
212 Handle<Value> UDPWrap::SetMembership(const Arguments& args,
213 uv_membership membership) {
214 HandleScope scope(node_isolate);
217 assert(args.Length() == 2);
219 String::Utf8Value address(args[0]);
220 String::Utf8Value iface(args[1]);
222 const char* iface_cstr = *iface;
223 if (args[1]->IsUndefined() || args[1]->IsNull()) {
227 int r = uv_udp_set_membership(&wrap->handle_, *address, iface_cstr,
231 SetErrno(uv_last_error(uv_default_loop()));
233 return scope.Close(Integer::New(r, node_isolate));
237 Handle<Value> UDPWrap::AddMembership(const Arguments& args) {
238 return SetMembership(args, UV_JOIN_GROUP);
242 Handle<Value> UDPWrap::DropMembership(const Arguments& args) {
243 return SetMembership(args, UV_LEAVE_GROUP);
247 Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
248 HandleScope scope(node_isolate);
251 // send(buffer, offset, length, port, address)
252 assert(args.Length() == 5);
256 assert(Buffer::HasInstance(args[0]));
257 Local<Object> buffer_obj = args[0]->ToObject();
259 size_t offset = args[1]->Uint32Value();
260 size_t length = args[2]->Uint32Value();
261 assert(offset < Buffer::Length(buffer_obj));
262 assert(length <= Buffer::Length(buffer_obj) - offset);
264 SendWrap* req_wrap = new SendWrap();
265 req_wrap->object_->SetHiddenValue(buffer_sym, buffer_obj);
267 uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset,
270 const unsigned short port = args[3]->Uint32Value();
271 String::Utf8Value address(args[4]);
275 r = uv_udp_send(&req_wrap->req_, &wrap->handle_, &buf, 1,
276 uv_ip4_addr(*address, port), OnSend);
279 r = uv_udp_send6(&req_wrap->req_, &wrap->handle_, &buf, 1,
280 uv_ip6_addr(*address, port), OnSend);
283 assert(0 && "unexpected address family");
287 req_wrap->Dispatched();
290 SetErrno(uv_last_error(uv_default_loop()));
292 return Null(node_isolate);
295 return scope.Close(req_wrap->object_);
300 Handle<Value> UDPWrap::Send(const Arguments& args) {
301 return DoSend(args, AF_INET);
305 Handle<Value> UDPWrap::Send6(const Arguments& args) {
306 return DoSend(args, AF_INET6);
310 Handle<Value> UDPWrap::RecvStart(const Arguments& args) {
311 HandleScope scope(node_isolate);
315 // UV_EALREADY means that the socket is already bound but that's okay
316 int r = uv_udp_recv_start(&wrap->handle_, OnAlloc, OnRecv);
317 if (r && uv_last_error(uv_default_loop()).code != UV_EALREADY) {
318 SetErrno(uv_last_error(uv_default_loop()));
319 return False(node_isolate);
322 return True(node_isolate);
326 Handle<Value> UDPWrap::RecvStop(const Arguments& args) {
327 HandleScope scope(node_isolate);
331 int r = uv_udp_recv_stop(&wrap->handle_);
333 return scope.Close(Integer::New(r, node_isolate));
337 Handle<Value> UDPWrap::GetSockName(const Arguments& args) {
338 HandleScope scope(node_isolate);
339 struct sockaddr_storage address;
343 int addrlen = sizeof(address);
344 int r = uv_udp_getsockname(&wrap->handle_,
345 reinterpret_cast<sockaddr*>(&address),
349 SetErrno(uv_last_error(uv_default_loop()));
350 return Null(node_isolate);
353 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
354 return scope.Close(AddressToJS(addr));
358 // TODO share with StreamWrap::AfterWrite() in stream_wrap.cc
359 void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
360 HandleScope scope(node_isolate);
364 SendWrap* req_wrap = reinterpret_cast<SendWrap*>(req->data);
365 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(req->handle->data);
367 assert(req_wrap->object_.IsEmpty() == false);
368 assert(wrap->object_.IsEmpty() == false);
371 SetErrno(uv_last_error(uv_default_loop()));
374 Local<Value> argv[4] = {
375 Integer::New(status, node_isolate),
376 Local<Value>::New(node_isolate, wrap->object_),
377 Local<Value>::New(node_isolate, req_wrap->object_),
378 req_wrap->object_->GetHiddenValue(buffer_sym),
381 MakeCallback(req_wrap->object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
386 uv_buf_t UDPWrap::OnAlloc(uv_handle_t* handle, size_t suggested_size) {
387 UDPWrap* wrap = static_cast<UDPWrap*>(handle->data);
388 char* buf = slab_allocator->Allocate(wrap->object_, suggested_size);
389 return uv_buf_init(buf, suggested_size);
393 void UDPWrap::OnRecv(uv_udp_t* handle,
396 struct sockaddr* addr,
398 HandleScope scope(node_isolate);
400 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(handle->data);
401 Local<Object> slab = slab_allocator->Shrink(wrap->object_,
403 nread < 0 ? 0 : nread);
404 if (nread == 0) return;
407 Local<Value> argv[] = { Local<Object>::New(node_isolate, wrap->object_) };
408 SetErrno(uv_last_error(uv_default_loop()));
409 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
413 Local<Value> argv[] = {
414 Local<Object>::New(node_isolate, wrap->object_),
416 Integer::NewFromUnsigned(buf.base - Buffer::Data(slab), node_isolate),
417 Integer::NewFromUnsigned(nread, node_isolate),
420 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
424 UDPWrap* UDPWrap::Unwrap(Local<Object> obj) {
425 assert(!obj.IsEmpty());
426 assert(obj->InternalFieldCount() > 0);
427 return static_cast<UDPWrap*>(obj->GetAlignedPointerFromInternalField(0));
431 Local<Object> UDPWrap::Instantiate() {
432 // If this assert fires then Initialize hasn't been called yet.
433 assert(constructor.IsEmpty() == false);
435 HandleScope scope(node_isolate);
436 Local<Object> obj = constructor->NewInstance();
438 return scope.Close(obj);
442 uv_udp_t* UDPWrap::UVHandle() {
449 NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize)