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.
25 #include "node_buffer.h"
26 #include "handle_wrap.h"
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
39 using v8::HandleScope;
43 using v8::PropertyAttribute;
44 using v8::PropertyCallbackInfo;
51 class SendWrap : public ReqWrap<uv_udp_send_t> {
53 SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
54 inline bool have_callback() const;
56 const bool have_callback_;
60 SendWrap::SendWrap(Environment* env,
61 Local<Object> req_wrap_obj,
63 : ReqWrap<uv_udp_send_t>(env, req_wrap_obj)
64 , have_callback_(have_callback) {
68 inline bool SendWrap::have_callback() const {
69 return have_callback_;
73 UDPWrap::UDPWrap(Environment* env, Handle<Object> object)
74 : HandleWrap(env, object, reinterpret_cast<uv_handle_t*>(&handle_)) {
75 int r = uv_udp_init(env->event_loop(), &handle_);
76 assert(r == 0); // can't fail anyway
84 void UDPWrap::Initialize(Handle<Object> target,
86 Handle<Context> context) {
87 Environment* env = Environment::GetCurrent(context);
89 Local<FunctionTemplate> t = FunctionTemplate::New(New);
90 t->InstanceTemplate()->SetInternalFieldCount(1);
91 t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "UDP"));
93 enum PropertyAttribute attributes =
94 static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
95 t->InstanceTemplate()->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "fd"),
102 NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
103 NODE_SET_PROTOTYPE_METHOD(t, "send", Send);
104 NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
105 NODE_SET_PROTOTYPE_METHOD(t, "send6", Send6);
106 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
107 NODE_SET_PROTOTYPE_METHOD(t, "recvStart", RecvStart);
108 NODE_SET_PROTOTYPE_METHOD(t, "recvStop", RecvStop);
109 NODE_SET_PROTOTYPE_METHOD(t, "getsockname", GetSockName);
110 NODE_SET_PROTOTYPE_METHOD(t, "addMembership", AddMembership);
111 NODE_SET_PROTOTYPE_METHOD(t, "dropMembership", DropMembership);
112 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastTTL", SetMulticastTTL);
113 NODE_SET_PROTOTYPE_METHOD(t, "setMulticastLoopback", SetMulticastLoopback);
114 NODE_SET_PROTOTYPE_METHOD(t, "setBroadcast", SetBroadcast);
115 NODE_SET_PROTOTYPE_METHOD(t, "setTTL", SetTTL);
117 NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
118 NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
120 target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "UDP"), t->GetFunction());
121 env->set_udp_constructor_function(t->GetFunction());
125 void UDPWrap::New(const FunctionCallbackInfo<Value>& args) {
126 assert(args.IsConstructCall());
127 Environment* env = Environment::GetCurrent(args.GetIsolate());
128 HandleScope handle_scope(args.GetIsolate());
129 new UDPWrap(env, args.This());
133 void UDPWrap::GetFD(Local<String>, const PropertyCallbackInfo<Value>& args) {
135 HandleScope scope(node_isolate);
137 NODE_UNWRAP(args.This(), UDPWrap, wrap);
138 int fd = (wrap == NULL) ? -1 : wrap->handle_.io_watcher.fd;
139 args.GetReturnValue().Set(fd);
144 void UDPWrap::DoBind(const FunctionCallbackInfo<Value>& args, int family) {
145 HandleScope scope(node_isolate);
148 NODE_UNWRAP(args.This(), UDPWrap, wrap);
150 // bind(ip, port, flags)
151 assert(args.Length() == 3);
153 String::Utf8Value address(args[0]);
154 const int port = args[1]->Uint32Value();
155 const int flags = args[2]->Uint32Value();
156 char addr[sizeof(sockaddr_in6)];
161 err = uv_ip4_addr(*address, port, reinterpret_cast<sockaddr_in*>(&addr));
164 err = uv_ip6_addr(*address, port, reinterpret_cast<sockaddr_in6*>(&addr));
167 assert(0 && "unexpected address family");
172 err = uv_udp_bind(&wrap->handle_,
173 reinterpret_cast<const sockaddr*>(&addr),
177 args.GetReturnValue().Set(err);
181 void UDPWrap::Bind(const FunctionCallbackInfo<Value>& args) {
182 DoBind(args, AF_INET);
186 void UDPWrap::Bind6(const FunctionCallbackInfo<Value>& args) {
187 DoBind(args, AF_INET6);
191 #define X(name, fn) \
192 void UDPWrap::name(const FunctionCallbackInfo<Value>& args) { \
193 HandleScope scope(node_isolate); \
195 NODE_UNWRAP(args.This(), UDPWrap, wrap); \
196 assert(args.Length() == 1); \
197 int flag = args[0]->Int32Value(); \
198 int err = fn(&wrap->handle_, flag); \
199 args.GetReturnValue().Set(err); \
202 X(SetTTL, uv_udp_set_ttl)
203 X(SetBroadcast, uv_udp_set_broadcast)
204 X(SetMulticastTTL, uv_udp_set_multicast_ttl)
205 X(SetMulticastLoopback, uv_udp_set_multicast_loop)
210 void UDPWrap::SetMembership(const FunctionCallbackInfo<Value>& args,
211 uv_membership membership) {
212 HandleScope scope(node_isolate);
214 NODE_UNWRAP(args.This(), UDPWrap, wrap);
216 assert(args.Length() == 2);
218 String::Utf8Value address(args[0]);
219 String::Utf8Value iface(args[1]);
221 const char* iface_cstr = *iface;
222 if (args[1]->IsUndefined() || args[1]->IsNull()) {
226 int err = uv_udp_set_membership(&wrap->handle_,
230 args.GetReturnValue().Set(err);
234 void UDPWrap::AddMembership(const FunctionCallbackInfo<Value>& args) {
235 SetMembership(args, UV_JOIN_GROUP);
239 void UDPWrap::DropMembership(const FunctionCallbackInfo<Value>& args) {
240 SetMembership(args, UV_LEAVE_GROUP);
244 void UDPWrap::DoSend(const FunctionCallbackInfo<Value>& args, int family) {
245 Environment* env = Environment::GetCurrent(args.GetIsolate());
246 HandleScope handle_scope(args.GetIsolate());
249 NODE_UNWRAP(args.This(), UDPWrap, wrap);
251 // send(req, buffer, offset, length, port, address)
252 assert(args[0]->IsObject());
253 assert(Buffer::HasInstance(args[1]));
254 assert(args[2]->IsUint32());
255 assert(args[3]->IsUint32());
256 assert(args[4]->IsUint32());
257 assert(args[5]->IsString());
258 assert(args[6]->IsBoolean());
260 Local<Object> req_wrap_obj = args[0].As<Object>();
261 Local<Object> buffer_obj = args[1].As<Object>();
262 size_t offset = args[2]->Uint32Value();
263 size_t length = args[3]->Uint32Value();
264 const unsigned short port = args[4]->Uint32Value();
265 String::Utf8Value address(args[5]);
266 const bool have_callback = args[6]->IsTrue();
268 assert(offset < Buffer::Length(buffer_obj));
269 assert(length <= Buffer::Length(buffer_obj) - offset);
271 SendWrap* req_wrap = new SendWrap(env, req_wrap_obj, have_callback);
273 uv_buf_t buf = uv_buf_init(Buffer::Data(buffer_obj) + offset,
275 char addr[sizeof(sockaddr_in6)];
280 err = uv_ip4_addr(*address, port, reinterpret_cast<sockaddr_in*>(&addr));
283 err = uv_ip6_addr(*address, port, reinterpret_cast<sockaddr_in6*>(&addr));
286 assert(0 && "unexpected address family");
291 err = uv_udp_send(&req_wrap->req_,
295 reinterpret_cast<const sockaddr*>(&addr),
299 req_wrap->Dispatched();
300 if (err) delete req_wrap;
302 args.GetReturnValue().Set(err);
306 void UDPWrap::Send(const FunctionCallbackInfo<Value>& args) {
307 DoSend(args, AF_INET);
311 void UDPWrap::Send6(const FunctionCallbackInfo<Value>& args) {
312 DoSend(args, AF_INET6);
316 void UDPWrap::RecvStart(const FunctionCallbackInfo<Value>& args) {
317 HandleScope scope(node_isolate);
319 NODE_UNWRAP(args.This(), UDPWrap, wrap);
321 int err = uv_udp_recv_start(&wrap->handle_, OnAlloc, OnRecv);
322 // UV_EALREADY means that the socket is already bound but that's okay
323 if (err == UV_EALREADY) err = 0;
324 args.GetReturnValue().Set(err);
328 void UDPWrap::RecvStop(const FunctionCallbackInfo<Value>& args) {
329 HandleScope scope(node_isolate);
331 NODE_UNWRAP(args.This(), UDPWrap, wrap);
333 int r = uv_udp_recv_stop(&wrap->handle_);
334 args.GetReturnValue().Set(r);
338 void UDPWrap::GetSockName(const FunctionCallbackInfo<Value>& args) {
339 Environment* env = Environment::GetCurrent(args.GetIsolate());
340 HandleScope handle_scope(args.GetIsolate());
342 struct sockaddr_storage address;
344 NODE_UNWRAP(args.This(), UDPWrap, wrap);
346 assert(args[0]->IsObject());
347 Local<Object> obj = args[0].As<Object>();
349 int addrlen = sizeof(address);
350 int err = uv_udp_getsockname(&wrap->handle_,
351 reinterpret_cast<sockaddr*>(&address),
355 const sockaddr* addr = reinterpret_cast<const sockaddr*>(&address);
356 AddressToJS(env, addr, obj);
359 args.GetReturnValue().Set(err);
363 // TODO(bnoordhuis) share with StreamWrap::AfterWrite() in stream_wrap.cc
364 void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
365 SendWrap* req_wrap = static_cast<SendWrap*>(req->data);
366 if (req_wrap->have_callback()) {
367 Environment* env = req_wrap->env();
368 Context::Scope context_scope(env->context());
369 HandleScope handle_scope(env->isolate());
370 Local<Object> req_wrap_obj = req_wrap->object();
371 Local<Value> arg = Integer::New(status, node_isolate);
372 MakeCallback(env, req_wrap_obj, env->oncomplete_string(), 1, &arg);
378 void UDPWrap::OnAlloc(uv_handle_t* handle,
379 size_t suggested_size,
381 buf->base = static_cast<char*>(malloc(suggested_size));
382 buf->len = suggested_size;
384 if (buf->base == NULL && suggested_size > 0) {
385 FatalError("node::UDPWrap::OnAlloc(uv_handle_t*, size_t, uv_buf_t*)",
391 void UDPWrap::OnRecv(uv_udp_t* handle,
394 const struct sockaddr* addr,
395 unsigned int flags) {
397 if (buf->base != NULL)
402 UDPWrap* wrap = static_cast<UDPWrap*>(handle->data);
403 Environment* env = wrap->env();
405 Context::Scope context_scope(env->context());
406 HandleScope handle_scope(env->isolate());
408 Local<Object> wrap_obj = wrap->object();
409 Local<Value> argv[] = {
410 Integer::New(nread, node_isolate),
417 if (buf->base != NULL)
421 env->onmessage_string(),
427 char* base = static_cast<char*>(realloc(buf->base, nread));
428 argv[2] = Buffer::Use(env, base, nread);
429 argv[3] = AddressToJS(env, addr);
430 MakeCallback(env, wrap_obj, env->onmessage_string(), ARRAY_SIZE(argv), argv);
434 UDPWrap* UDPWrap::Unwrap(Local<Object> obj) {
436 NODE_UNWRAP(obj, UDPWrap, wrap);
441 Local<Object> UDPWrap::Instantiate(Environment* env) {
442 // If this assert fires then Initialize hasn't been called yet.
443 assert(env->udp_constructor_function().IsEmpty() == false);
444 return env->udp_constructor_function()->NewInstance();
448 uv_udp_t* UDPWrap::UVHandle() {
455 NODE_MODULE_CONTEXT_AWARE(node_udp_wrap, node::UDPWrap::Initialize)