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;
40 using v8::FunctionTemplate;
42 using v8::HandleScope;
48 using v8::PropertyAttribute;
53 typedef ReqWrap<uv_udp_send_t> SendWrap;
56 Local<Object> AddressToJS(const sockaddr* addr);
58 static Persistent<Function> constructor;
59 static Persistent<String> buffer_sym;
60 static Persistent<String> oncomplete_sym;
61 static Persistent<String> onmessage_sym;
62 static SlabAllocator* slab_allocator;
65 static void DeleteSlabAllocator(void*) {
66 delete slab_allocator;
67 slab_allocator = NULL;
71 UDPWrap::UDPWrap(Handle<Object> object): HandleWrap(object,
72 (uv_handle_t*)&handle_) {
73 int r = uv_udp_init(uv_default_loop(), &handle_);
74 assert(r == 0); // can't fail anyway
75 handle_.data = reinterpret_cast<void*>(this);
83 void UDPWrap::Initialize(Handle<Object> target) {
84 HandleWrap::Initialize(target);
86 slab_allocator = new SlabAllocator(SLAB_SIZE);
87 AtExit(DeleteSlabAllocator, NULL);
91 buffer_sym = NODE_PSYMBOL("buffer");
92 oncomplete_sym = NODE_PSYMBOL("oncomplete");
93 onmessage_sym = NODE_PSYMBOL("onmessage");
95 Local<FunctionTemplate> t = FunctionTemplate::New(New);
96 t->InstanceTemplate()->SetInternalFieldCount(1);
97 t->SetClassName(String::NewSymbol("UDP"));
99 enum PropertyAttribute attributes =
100 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
101 t->InstanceTemplate()->SetAccessor(String::New("fd"),
108 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
109 NODE_SET_PROTOTYPE_METHOD(t, "send", Send);
110 NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
111 NODE_SET_PROTOTYPE_METHOD(t, "send6", Send6);
112 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
113 NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
114 NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
115 NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
116 NODE_SET_PROTOTYPE_METHOD(t, "addMembership", AddMembership);
117 NODE_SET_PROTOTYPE_METHOD(t, "dropMembership", DropMembership);
118 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastTTL", SetMulticastTTL);
119 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastLoopback", SetMulticastLoopback);
120 NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
121 NODE_SET_PROTOTYPE_METHOD(t, "setTTL", SetTTL);
123 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
124 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
126 constructor = Persistent<Function>::New(
127 Persistent<FunctionTemplate>::New(t)->GetFunction());
128 target->Set(String::NewSymbol("UDP"), constructor);
132 Handle<Value> UDPWrap::New(const Arguments& args) {
135 assert(args.IsConstructCall());
136 new UDPWrap(args.This());
138 return scope.Close(args.This());
142 Handle<Value> UDPWrap::GetFD(Local<String>, const AccessorInfo& args) {
148 int fd = (wrap == NULL) ? -1 : wrap->handle_.io_watcher.fd;
149 return scope.Close(Integer::New(fd));
154 Handle<Value> UDPWrap::DoBind(const Arguments& args, int family) {
160 // bind(ip, port, flags)
161 assert(args.Length() == 3);
163 String::Utf8Value address(args[0]);
164 const int port = args[1]->Uint32Value();
165 const int flags = args[2]->Uint32Value();
169 r = uv_udp_bind(&wrap->handle_, uv_ip4_addr(*address, port), flags);
172 r = uv_udp_bind6(&wrap->handle_, uv_ip6_addr(*address, port), flags);
175 assert(0 && "unexpected address family");
180 SetErrno(uv_last_error(uv_default_loop()));
182 return scope.Close(Integer::New(r));
186 Handle<Value> UDPWrap::Bind(const Arguments& args) {
187 return DoBind(args, AF_INET);
191 Handle<Value> UDPWrap::Bind6(const Arguments& args) {
192 return DoBind(args, AF_INET6);
196 #define X(name, fn) \
197 Handle<Value> UDPWrap::name(const Arguments& args) { \
200 assert(args.Length() == 1); \
201 int flag = args[0]->Int32Value(); \
202 int r = fn(&wrap->handle_, flag); \
203 if (r) SetErrno(uv_last_error(uv_default_loop())); \
204 return scope.Close(Integer::New(r)); \
207 X(SetTTL, uv_udp_set_ttl)
208 X(SetBroadcast, uv_udp_set_broadcast)
209 X(SetMulticastTTL, uv_udp_set_multicast_ttl)
210 X(SetMulticastLoopback, uv_udp_set_multicast_loop)
215 Handle<Value> UDPWrap::SetMembership(const Arguments& args,
216 uv_membership membership) {
220 assert(args.Length() == 2);
222 String::Utf8Value address(args[0]);
223 String::Utf8Value iface(args[1]);
225 const char* iface_cstr = *iface;
226 if (args[1]->IsUndefined() || args[1]->IsNull()) {
230 int r = uv_udp_set_membership(&wrap->handle_, *address, iface_cstr,
234 SetErrno(uv_last_error(uv_default_loop()));
236 return scope.Close(Integer::New(r));
240 Handle<Value> UDPWrap::AddMembership(const Arguments& args) {
241 return SetMembership(args, UV_JOIN_GROUP);
245 Handle<Value> UDPWrap::DropMembership(const Arguments& args) {
246 return SetMembership(args, UV_LEAVE_GROUP);
250 Handle<Value> UDPWrap::DoSend(const Arguments& args, int family) {
254 // send(buffer, offset, length, port, address)
255 assert(args.Length() == 5);
259 assert(Buffer::HasInstance(args[0]));
260 Local<Object> buffer_obj = args[0]->ToObject();
262 size_t offset = args[1]->Uint32Value();
263 size_t length = args[2]->Uint32Value();
264 assert(offset < Buffer::Length(buffer_obj));
265 assert(length <= Buffer::Length(buffer_obj) - offset);
267 SendWrap* req_wrap = new SendWrap();
268 req_wrap->object_->SetHiddenValue(buffer_sym, buffer_obj);
270 uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset,
273 const unsigned short port = args[3]->Uint32Value();
274 String::Utf8Value address(args[4]);
278 r = uv_udp_send(&req_wrap->req_, &wrap->handle_, &buf, 1,
279 uv_ip4_addr(*address, port), OnSend);
282 r = uv_udp_send6(&req_wrap->req_, &wrap->handle_, &buf, 1,
283 uv_ip6_addr(*address, port), OnSend);
286 assert(0 && "unexpected address family");
290 req_wrap->Dispatched();
293 SetErrno(uv_last_error(uv_default_loop()));
298 return scope.Close(req_wrap->object_);
303 Handle<Value> UDPWrap::Send(const Arguments& args) {
304 return DoSend(args, AF_INET);
308 Handle<Value> UDPWrap::Send6(const Arguments& args) {
309 return DoSend(args, AF_INET6);
313 Handle<Value> UDPWrap::RecvStart(const Arguments& args) {
318 // UV_EALREADY means that the socket is already bound but that's okay
319 int r = uv_udp_recv_start(&wrap->handle_, OnAlloc, OnRecv);
320 if (r && uv_last_error(uv_default_loop()).code != UV_EALREADY) {
321 SetErrno(uv_last_error(uv_default_loop()));
329 Handle<Value> UDPWrap::RecvStop(const Arguments& args) {
334 int r = uv_udp_recv_stop(&wrap->handle_);
336 return scope.Close(Integer::New(r));
340 Handle<Value> UDPWrap::GetSockName(const Arguments& args) {
342 struct sockaddr_storage address;
346 int addrlen = sizeof(address);
347 int r = uv_udp_getsockname(&wrap->handle_,
348 reinterpret_cast<sockaddr*>(&address),
352 SetErrno(uv_last_error(uv_default_loop()));
356 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
357 return scope.Close(AddressToJS(addr));
361 // TODO share with StreamWrap::AfterWrite() in stream_wrap.cc
362 void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
367 SendWrap* req_wrap = reinterpret_cast<SendWrap*>(req->data);
368 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(req->handle->data);
370 assert(req_wrap->object_.IsEmpty() == false);
371 assert(wrap->object_.IsEmpty() == false);
374 SetErrno(uv_last_error(uv_default_loop()));
377 Local<Value> argv[4] = {
378 Integer::New(status),
379 Local<Value>::New(wrap->object_),
380 Local<Value>::New(req_wrap->object_),
381 req_wrap->object_->GetHiddenValue(buffer_sym),
384 MakeCallback(req_wrap->object_, oncomplete_sym, ARRAY_SIZE(argv), argv);
389 uv_buf_t UDPWrap::OnAlloc(uv_handle_t* handle, size_t suggested_size) {
390 UDPWrap* wrap = static_cast<UDPWrap*>(handle->data);
391 char* buf = slab_allocator->Allocate(wrap->object_, suggested_size);
392 return uv_buf_init(buf, suggested_size);
396 void UDPWrap::OnRecv(uv_udp_t* handle,
399 struct sockaddr* addr,
403 UDPWrap* wrap = reinterpret_cast<UDPWrap*>(handle->data);
404 Local<Object> slab = slab_allocator->Shrink(wrap->object_,
406 nread < 0 ? 0 : nread);
407 if (nread == 0) return;
410 Local<Value> argv[] = { Local<Object>::New(wrap->object_) };
411 SetErrno(uv_last_error(uv_default_loop()));
412 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
416 Local<Value> argv[] = {
417 Local<Object>::New(wrap->object_),
419 Integer::NewFromUnsigned(buf.base - Buffer::Data(slab)),
420 Integer::NewFromUnsigned(nread),
423 MakeCallback(wrap->object_, onmessage_sym, ARRAY_SIZE(argv), argv);
427 UDPWrap* UDPWrap::Unwrap(Local<Object> obj) {
428 assert(!obj.IsEmpty());
429 assert(obj->InternalFieldCount() > 0);
430 return static_cast<UDPWrap*>(obj->GetPointerFromInternalField(0));
434 Local<Object> UDPWrap::Instantiate() {
435 // If this assert fires then Initialize hasn't been called yet.
436 assert(constructor.IsEmpty() == false);
439 Local<Object> obj = constructor->NewInstance();
441 return scope.Close(obj);
445 uv_udp_t* UDPWrap::UVHandle() {
452 NODE_MODULE(node_udp_wrap, node::UDPWrap::Initialize)