1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 static void uv__udp_run_completed(uv_udp_t* handle);
33 static void uv__udp_run_pending(uv_udp_t* handle);
34 static void uv__udp_io(uv_loop_t* loop, uv__io_t* w, unsigned int revents);
35 static void uv__udp_recvmsg(uv_loop_t* loop, uv__io_t* w, unsigned int revents);
36 static void uv__udp_sendmsg(uv_loop_t* loop, uv__io_t* w, unsigned int revents);
37 static int uv__udp_maybe_deferred_bind(uv_udp_t* handle, int domain);
38 static int uv__send(uv_udp_send_t* req,
42 struct sockaddr* addr,
44 uv_udp_send_cb send_cb);
47 void uv__udp_close(uv_udp_t* handle) {
48 uv__io_close(handle->loop, &handle->io_watcher);
49 uv__handle_stop(handle);
50 close(handle->io_watcher.fd);
51 handle->io_watcher.fd = -1;
55 void uv__udp_finish_close(uv_udp_t* handle) {
59 assert(!uv__io_active(&handle->io_watcher, UV__POLLIN | UV__POLLOUT));
60 assert(handle->io_watcher.fd == -1);
62 uv__udp_run_completed(handle);
64 while (!ngx_queue_empty(&handle->write_queue)) {
65 q = ngx_queue_head(&handle->write_queue);
68 req = ngx_queue_data(q, uv_udp_send_t, queue);
69 uv__req_unregister(handle->loop, req);
71 if (req->bufs != req->bufsml)
76 uv__set_artificial_error(handle->loop, UV_ECANCELED);
77 req->send_cb(req, -1);
81 /* Now tear down the handle. */
82 handle->recv_cb = NULL;
83 handle->alloc_cb = NULL;
84 /* but _do not_ touch close_cb */
88 static void uv__udp_run_pending(uv_udp_t* handle) {
94 while (!ngx_queue_empty(&handle->write_queue)) {
95 q = ngx_queue_head(&handle->write_queue);
98 req = ngx_queue_data(q, uv_udp_send_t, queue);
101 memset(&h, 0, sizeof h);
102 h.msg_name = &req->addr;
103 h.msg_namelen = (req->addr.sin6_family == AF_INET6 ?
104 sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
105 h.msg_iov = (struct iovec*)req->bufs;
106 h.msg_iovlen = req->bufcnt;
109 size = sendmsg(handle->io_watcher.fd, &h, 0);
111 while (size == -1 && errno == EINTR);
113 /* TODO try to write once or twice more in the
114 * hope that the socket becomes readable again?
116 if (size == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
119 req->status = (size == -1 ? -errno : size);
127 for (nbytes = i = 0; i < req->bufcnt; i++)
128 nbytes += req->bufs[i].len;
130 assert(size == nbytes);
134 /* Sending a datagram is an atomic operation: either all data
135 * is written or nothing is (and EMSGSIZE is raised). That is
136 * why we don't handle partial writes. Just pop the request
137 * off the write queue and onto the completed queue, done.
139 ngx_queue_remove(&req->queue);
140 ngx_queue_insert_tail(&handle->write_completed_queue, &req->queue);
145 static void uv__udp_run_completed(uv_udp_t* handle) {
149 while (!ngx_queue_empty(&handle->write_completed_queue)) {
150 q = ngx_queue_head(&handle->write_completed_queue);
153 req = ngx_queue_data(q, uv_udp_send_t, queue);
154 uv__req_unregister(handle->loop, req);
156 if (req->bufs != req->bufsml)
160 if (req->send_cb == NULL)
163 /* req->status >= 0 == bytes written
164 * req->status < 0 == errno
166 if (req->status >= 0) {
167 req->send_cb(req, 0);
170 uv__set_sys_error(handle->loop, -req->status);
171 req->send_cb(req, -1);
177 static void uv__udp_io(uv_loop_t* loop, uv__io_t* w, unsigned int revents) {
178 if (revents & UV__POLLIN)
179 uv__udp_recvmsg(loop, w, revents);
181 if (revents & UV__POLLOUT)
182 uv__udp_sendmsg(loop, w, revents);
186 static void uv__udp_recvmsg(uv_loop_t* loop,
188 unsigned int revents) {
189 struct sockaddr_storage peer;
197 handle = container_of(w, uv_udp_t, io_watcher);
198 assert(handle->type == UV_UDP);
199 assert(revents & UV__POLLIN);
201 assert(handle->recv_cb != NULL);
202 assert(handle->alloc_cb != NULL);
204 /* Prevent loop starvation when the data comes in as fast as (or faster than)
205 * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
209 memset(&h, 0, sizeof(h));
213 buf = handle->alloc_cb((uv_handle_t*)handle, 64 * 1024);
215 assert(buf.base != NULL);
217 h.msg_namelen = sizeof(peer);
218 h.msg_iov = (void*) &buf;
222 nread = recvmsg(handle->io_watcher.fd, &h, 0);
224 while (nread == -1 && errno == EINTR);
227 if (errno == EAGAIN || errno == EWOULDBLOCK) {
228 uv__set_sys_error(handle->loop, EAGAIN);
229 handle->recv_cb(handle, 0, buf, NULL, 0);
232 uv__set_sys_error(handle->loop, errno);
233 handle->recv_cb(handle, -1, buf, NULL, 0);
239 if (h.msg_flags & MSG_TRUNC)
240 flags |= UV_UDP_PARTIAL;
242 handle->recv_cb(handle,
245 (struct sockaddr*)&peer,
249 /* recv_cb callback may decide to pause or close the handle */
252 && handle->io_watcher.fd != -1
253 && handle->recv_cb != NULL);
257 static void uv__udp_sendmsg(uv_loop_t* loop,
259 unsigned int revents) {
262 handle = container_of(w, uv_udp_t, io_watcher);
263 assert(handle->type == UV_UDP);
264 assert(revents & UV__POLLOUT);
266 assert(!ngx_queue_empty(&handle->write_queue)
267 || !ngx_queue_empty(&handle->write_completed_queue));
269 /* Write out pending data first. */
270 uv__udp_run_pending(handle);
272 /* Drain 'request completed' queue. */
273 uv__udp_run_completed(handle);
275 if (!ngx_queue_empty(&handle->write_completed_queue)) {
276 /* Schedule completion callbacks. */
277 uv__io_feed(handle->loop, &handle->io_watcher);
279 else if (ngx_queue_empty(&handle->write_queue)) {
280 /* Pending queue and completion queue empty, stop watcher. */
281 uv__io_stop(loop, &handle->io_watcher, UV__POLLOUT);
283 if (!uv__io_active(&handle->io_watcher, UV__POLLIN))
284 uv__handle_stop(handle);
289 static int uv__bind(uv_udp_t* handle,
291 struct sockaddr* addr,
303 /* Check for bad flags. */
304 if (flags & ~UV_UDP_IPV6ONLY) {
305 uv__set_sys_error(handle->loop, EINVAL);
309 /* Cannot set IPv6-only mode on non-IPv6 socket. */
310 if ((flags & UV_UDP_IPV6ONLY) && domain != AF_INET6) {
311 uv__set_sys_error(handle->loop, EINVAL);
315 if (handle->io_watcher.fd == -1) {
316 if ((fd = uv__socket(domain, SOCK_DGRAM, 0)) == -1) {
317 uv__set_sys_error(handle->loop, errno);
320 handle->io_watcher.fd = fd;
323 fd = handle->io_watcher.fd;
325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
326 uv__set_sys_error(handle->loop, errno);
330 /* On the BSDs, SO_REUSEADDR lets you reuse an address that's in the TIME_WAIT
331 * state (i.e. was until recently tied to a socket) while SO_REUSEPORT lets
332 * multiple processes bind to the same address. Yes, it's something of a
333 * misnomer but then again, SO_REUSEADDR was already taken.
335 * None of the above applies to Linux: SO_REUSEADDR implies SO_REUSEPORT on
336 * Linux and hence it does not have SO_REUSEPORT at all.
340 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof yes) == -1) {
341 uv__set_sys_error(handle->loop, errno);
346 if (flags & UV_UDP_IPV6ONLY) {
349 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof yes) == -1) {
350 uv__set_sys_error(handle->loop, errno);
354 uv__set_sys_error(handle->loop, ENOTSUP);
359 if (bind(fd, addr, len) == -1) {
360 uv__set_sys_error(handle->loop, errno);
364 handle->io_watcher.fd = fd;
369 close(handle->io_watcher.fd);
370 handle->io_watcher.fd = -1;
378 static int uv__udp_maybe_deferred_bind(uv_udp_t* handle, int domain) {
379 unsigned char taddr[sizeof(struct sockaddr_in6)];
382 assert(domain == AF_INET || domain == AF_INET6);
384 if (handle->io_watcher.fd != -1)
390 struct sockaddr_in* addr = (void*)&taddr;
391 memset(addr, 0, sizeof *addr);
392 addr->sin_family = AF_INET;
393 addr->sin_addr.s_addr = INADDR_ANY;
394 addrlen = sizeof *addr;
399 struct sockaddr_in6* addr = (void*)&taddr;
400 memset(addr, 0, sizeof *addr);
401 addr->sin6_family = AF_INET6;
402 addr->sin6_addr = in6addr_any;
403 addrlen = sizeof *addr;
407 assert(0 && "unsupported address family");
411 return uv__bind(handle, domain, (struct sockaddr*)&taddr, addrlen, 0);
415 static int uv__send(uv_udp_send_t* req,
419 struct sockaddr* addr,
421 uv_udp_send_cb send_cb) {
424 if (uv__udp_maybe_deferred_bind(handle, addr->sa_family))
427 uv__req_init(handle->loop, req, UV_UDP_SEND);
429 assert(addrlen <= sizeof(req->addr));
430 memcpy(&req->addr, addr, addrlen);
431 req->send_cb = send_cb;
432 req->handle = handle;
433 req->bufcnt = bufcnt;
435 if (bufcnt <= (int) ARRAY_SIZE(req->bufsml)) {
436 req->bufs = req->bufsml;
438 else if ((req->bufs = malloc(bufcnt * sizeof(bufs[0]))) == NULL) {
439 uv__set_sys_error(handle->loop, ENOMEM);
443 memcpy(req->bufs, bufs, bufcnt * sizeof(bufs[0]));
444 ngx_queue_insert_tail(&handle->write_queue, &req->queue);
445 uv__io_start(handle->loop, &handle->io_watcher, UV__POLLOUT);
446 uv__handle_start(handle);
452 int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
453 uv__handle_init(loop, (uv_handle_t*)handle, UV_UDP);
454 handle->alloc_cb = NULL;
455 handle->recv_cb = NULL;
456 uv__io_init(&handle->io_watcher, uv__udp_io, -1);
457 ngx_queue_init(&handle->write_queue);
458 ngx_queue_init(&handle->write_completed_queue);
463 int uv__udp_bind(uv_udp_t* handle, struct sockaddr_in addr, unsigned flags) {
464 return uv__bind(handle,
466 (struct sockaddr*)&addr,
472 int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr, unsigned flags) {
473 return uv__bind(handle,
475 (struct sockaddr*)&addr,
481 int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) {
489 /* Check for already active socket. */
490 if (handle->io_watcher.fd != -1) {
491 uv__set_artificial_error(handle->loop, UV_EALREADY);
496 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
497 uv__set_sys_error(handle->loop, errno);
501 /* On the BSDs, SO_REUSEADDR lets you reuse an address that's in the TIME_WAIT
502 * state (i.e. was until recently tied to a socket) while SO_REUSEPORT lets
503 * multiple processes bind to the same address. Yes, it's something of a
504 * misnomer but then again, SO_REUSEADDR was already taken.
506 * None of the above applies to Linux: SO_REUSEADDR implies SO_REUSEPORT on
507 * Linux and hence it does not have SO_REUSEPORT at all.
511 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof yes) == -1) {
512 uv__set_sys_error(handle->loop, errno);
517 handle->io_watcher.fd = sock;
526 int uv_udp_set_membership(uv_udp_t* handle,
527 const char* multicast_addr,
528 const char* interface_addr,
529 uv_membership membership) {
533 memset(&mreq, 0, sizeof mreq);
535 if (interface_addr) {
536 mreq.imr_interface.s_addr = inet_addr(interface_addr);
538 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
541 mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
543 switch (membership) {
545 optname = IP_ADD_MEMBERSHIP;
548 optname = IP_DROP_MEMBERSHIP;
551 return uv__set_artificial_error(handle->loop, UV_EINVAL);
554 if (setsockopt(handle->io_watcher.fd,
559 return uv__set_sys_error(handle->loop, errno);
566 static int uv__setsockopt_maybe_char(uv_udp_t* handle, int option, int val) {
573 if (val < 0 || val > 255)
574 return uv__set_sys_error(handle->loop, EINVAL);
576 if (setsockopt(handle->io_watcher.fd, IPPROTO_IP, option, &arg, sizeof(arg)))
577 return uv__set_sys_error(handle->loop, errno);
583 int uv_udp_set_broadcast(uv_udp_t* handle, int on) {
584 if (setsockopt(handle->io_watcher.fd,
589 return uv__set_sys_error(handle->loop, errno);
596 int uv_udp_set_ttl(uv_udp_t* handle, int ttl) {
597 if (ttl < 1 || ttl > 255)
598 return uv__set_sys_error(handle->loop, EINVAL);
600 if (setsockopt(handle->io_watcher.fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)))
601 return uv__set_sys_error(handle->loop, errno);
607 int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) {
608 return uv__setsockopt_maybe_char(handle, IP_MULTICAST_TTL, ttl);
612 int uv_udp_set_multicast_loop(uv_udp_t* handle, int on) {
613 return uv__setsockopt_maybe_char(handle, IP_MULTICAST_LOOP, on);
617 int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) {
622 /* Don't clobber errno. */
625 if (handle->io_watcher.fd == -1) {
626 uv__set_sys_error(handle->loop, EINVAL);
631 /* sizeof(socklen_t) != sizeof(int) on some systems. */
632 socklen = (socklen_t)*namelen;
634 if (getsockname(handle->io_watcher.fd, name, &socklen) == -1) {
635 uv__set_sys_error(handle->loop, errno);
638 *namelen = (int)socklen;
647 int uv__udp_send(uv_udp_send_t* req,
651 struct sockaddr_in addr,
652 uv_udp_send_cb send_cb) {
657 (struct sockaddr*)&addr,
663 int uv__udp_send6(uv_udp_send_t* req,
667 struct sockaddr_in6 addr,
668 uv_udp_send_cb send_cb) {
673 (struct sockaddr*)&addr,
679 int uv__udp_recv_start(uv_udp_t* handle,
680 uv_alloc_cb alloc_cb,
681 uv_udp_recv_cb recv_cb) {
682 if (alloc_cb == NULL || recv_cb == NULL) {
683 uv__set_artificial_error(handle->loop, UV_EINVAL);
687 if (uv__io_active(&handle->io_watcher, UV__POLLIN)) {
688 uv__set_artificial_error(handle->loop, UV_EALREADY);
692 if (uv__udp_maybe_deferred_bind(handle, AF_INET))
695 handle->alloc_cb = alloc_cb;
696 handle->recv_cb = recv_cb;
698 uv__io_start(handle->loop, &handle->io_watcher, UV__POLLIN);
699 uv__handle_start(handle);
705 int uv__udp_recv_stop(uv_udp_t* handle) {
706 uv__io_stop(handle->loop, &handle->io_watcher, UV__POLLIN);
708 if (!uv__io_active(&handle->io_watcher, UV__POLLOUT))
709 uv__handle_stop(handle);
711 handle->alloc_cb = NULL;
712 handle->recv_cb = NULL;