Rizin
unix-like reverse engineering framework and cli tools
udp.c File Reference
#include <assert.h>
#include <stdlib.h>
#include "uv.h"
#include "internal.h"
#include "handle-inl.h"
#include "stream-inl.h"
#include "req-inl.h"

Go to the source code of this file.

Macros

#define SOCKOPT_SETTER(name, option4, option6, validate)
 
#define VALIDATE_TTL(value)   ((value) >= 1 && (value) <= 255)
 
#define VALIDATE_MULTICAST_TTL(value)   ((value) >= -1 && (value) <= 255)
 
#define VALIDATE_MULTICAST_LOOP(value)   (1)
 

Functions

int uv_udp_getpeername (const uv_udp_t *handle, struct sockaddr *name, int *namelen)
 
int uv_udp_getsockname (const uv_udp_t *handle, struct sockaddr *name, int *namelen)
 
static int uv_udp_set_socket (uv_loop_t *loop, uv_udp_t *handle, SOCKET socket, int family)
 
int uv__udp_init_ex (uv_loop_t *loop, uv_udp_t *handle, unsigned flags, int domain)
 
void uv_udp_close (uv_loop_t *loop, uv_udp_t *handle)
 
void uv_udp_endgame (uv_loop_t *loop, uv_udp_t *handle)
 
int uv_udp_using_recvmmsg (const uv_udp_t *handle)
 
static int uv_udp_maybe_bind (uv_udp_t *handle, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags)
 
static void uv_udp_queue_recv (uv_loop_t *loop, uv_udp_t *handle)
 
int uv__udp_recv_start (uv_udp_t *handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
 
int uv__udp_recv_stop (uv_udp_t *handle)
 
static int uv__send (uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen, uv_udp_send_cb cb)
 
void uv_process_udp_recv_req (uv_loop_t *loop, uv_udp_t *handle, uv_req_t *req)
 
void uv_process_udp_send_req (uv_loop_t *loop, uv_udp_t *handle, uv_udp_send_t *req)
 
static int uv__udp_set_membership4 (uv_udp_t *handle, const struct sockaddr_in *multicast_addr, const char *interface_addr, uv_membership membership)
 
int uv__udp_set_membership6 (uv_udp_t *handle, const struct sockaddr_in6 *multicast_addr, const char *interface_addr, uv_membership membership)
 
static int uv__udp_set_source_membership4 (uv_udp_t *handle, const struct sockaddr_in *multicast_addr, const char *interface_addr, const struct sockaddr_in *source_addr, uv_membership membership)
 
int uv__udp_set_source_membership6 (uv_udp_t *handle, const struct sockaddr_in6 *multicast_addr, const char *interface_addr, const struct sockaddr_in6 *source_addr, uv_membership membership)
 
int uv_udp_set_membership (uv_udp_t *handle, const char *multicast_addr, const char *interface_addr, uv_membership membership)
 
int uv_udp_set_source_membership (uv_udp_t *handle, const char *multicast_addr, const char *interface_addr, const char *source_addr, uv_membership membership)
 
int uv_udp_set_multicast_interface (uv_udp_t *handle, const char *interface_addr)
 
int uv_udp_set_broadcast (uv_udp_t *handle, int value)
 
int uv__udp_is_bound (uv_udp_t *handle)
 
int uv_udp_open (uv_udp_t *handle, uv_os_sock_t sock)
 
 SOCKOPT_SETTER (ttl, IP_TTL, IPV6_HOPLIMIT, VALIDATE_TTL)
 
int uv__udp_connect (uv_udp_t *handle, const struct sockaddr *addr, unsigned int addrlen)
 
int uv__udp_disconnect (uv_udp_t *handle)
 
int uv__udp_send (uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen, uv_udp_send_cb send_cb)
 
int uv__udp_try_send (uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen)
 

Variables

const unsigned int uv_active_udp_streams_threshold = 0
 
static char uv_zero_ [] = ""
 

Macro Definition Documentation

◆ SOCKOPT_SETTER

#define SOCKOPT_SETTER (   name,
  option4,
  option6,
  validate 
)

Definition at line 982 of file udp.c.

◆ VALIDATE_MULTICAST_LOOP

#define VALIDATE_MULTICAST_LOOP (   value)    (1)

Definition at line 1017 of file udp.c.

◆ VALIDATE_MULTICAST_TTL

#define VALIDATE_MULTICAST_TTL (   value)    ((value) >= -1 && (value) <= 255)

Definition at line 1016 of file udp.c.

◆ VALIDATE_TTL

#define VALIDATE_TTL (   value)    ((value) >= 1 && (value) <= 255)

Definition at line 1015 of file udp.c.

Function Documentation

◆ SOCKOPT_SETTER()

SOCKOPT_SETTER ( ttl  ,
IP_TTL  ,
IPV6_HOPLIMIT  ,
VALIDATE_TTL   
)

Definition at line 1019 of file udp.c.

1044  {
1045  int err;
1046 
1048  if (err)
1049  return uv_translate_sys_error(err);
1050 
1051  return 0;
1052 }
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset void static length char static len const struct iovec static count unsigned long static filedes static sched_yield static flags static oldfd static pause unsigned static seconds static protocol struct sockaddr addrlen
Definition: sflib.h:75
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
UV_EXTERN int uv_translate_sys_error(int sys_errno)
Definition: core.c:1249
static int uv_udp_maybe_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int addrlen, unsigned int flags)
Definition: udp.c:197
static int addr
Definition: z80asm.c:58

References addr, addrlen, err, flags, handle, uv_translate_sys_error(), and uv_udp_maybe_bind().

◆ uv__send()

static int uv__send ( uv_udp_send_t req,
uv_udp_t handle,
const uv_buf_t  bufs[],
unsigned int  nbufs,
const struct sockaddr addr,
unsigned int  addrlen,
uv_udp_send_cb  cb 
)
static

Definition at line 404 of file udp.c.

410  {
411  uv_loop_t* loop = handle->loop;
412  DWORD result, bytes;
413 
414  UV_REQ_INIT(req, UV_UDP_SEND);
415  req->handle = handle;
416  req->cb = cb;
417  memset(&req->u.io.overlapped, 0, sizeof(req->u.io.overlapped));
418 
419  result = WSASendTo(handle->socket,
420  (WSABUF*)bufs,
421  nbufs,
422  &bytes,
423  0,
424  addr,
425  addrlen,
426  &req->u.io.overlapped,
427  NULL);
428 
429  if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
430  /* Request completed immediately. */
431  req->u.io.queued_bytes = 0;
432  handle->reqs_pending++;
433  handle->send_queue_size += req->u.io.queued_bytes;
434  handle->send_queue_count++;
437  } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
438  /* Request queued by the kernel. */
439  req->u.io.queued_bytes = uv__count_bufs(bufs, nbufs);
440  handle->reqs_pending++;
441  handle->send_queue_size += req->u.io.queued_bytes;
442  handle->send_queue_count++;
444  } else {
445  /* Send failed due to an error. */
446  return WSAGetLastError();
447  }
448 
449  return 0;
450 }
static ut8 bytes[32]
Definition: asm_arc.c:23
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
return memset(p, 0, total)
static INLINE void uv_insert_pending_req(uv_loop_t *loop, uv_req_t *req)
Definition: req-inl.h:90
#define UV_SUCCEEDED_WITH_IOCP(result)
Definition: req-inl.h:72
#define REGISTER_HANDLE_REQ(loop, handle, req)
Definition: req-inl.h:56
#define UV_SUCCEEDED_WITHOUT_IOCP(result)
Definition: req-inl.h:69
Definition: uv.h:1780
Definition: uv.h:407
uv_loop_t * loop
Definition: main.c:7
static char bufs[4][128]
Buffers for uint64_to_str() and uint64_to_nicestr()
Definition: util.c:18
size_t uv__count_bufs(const uv_buf_t bufs[], unsigned int nbufs)
Definition: uv-common.c:573
#define UV_REQ_INIT(req, typ)
Definition: uv-common.h:322
DWORD
static const char * cb[]
Definition: z80_tab.h:176

References addr, addrlen, bufs, bytes, cb, DWORD, handle, loop, memset(), NULL, REGISTER_HANDLE_REQ, req, uv__count_bufs(), uv_insert_pending_req(), UV_REQ_INIT, UV_SUCCEEDED_WITH_IOCP, and UV_SUCCEEDED_WITHOUT_IOCP.

Referenced by uv__udp_send().

◆ uv__udp_connect()

int uv__udp_connect ( uv_udp_t handle,
const struct sockaddr addr,
unsigned int  addrlen 
)

Definition at line 1055 of file udp.c.

1057  {
1058  const struct sockaddr* bind_addr;
1059  int err;
1060 
1061  if (!(handle->flags & UV_HANDLE_BOUND)) {
1062  if (addrlen == sizeof(uv_addr_ip4_any_))
1063  bind_addr = (const struct sockaddr*) &uv_addr_ip4_any_;
1064  else if (addrlen == sizeof(uv_addr_ip6_any_))
1065  bind_addr = (const struct sockaddr*) &uv_addr_ip6_any_;
1066  else
1067  return UV_EINVAL;
1068 
1069  err = uv_udp_maybe_bind(handle, bind_addr, addrlen, 0);
1070  if (err)
1071  return uv_translate_sys_error(err);
1072  }
1073 
1074  err = connect(handle->socket, addr, addrlen);
1075  if (err)
1076  return uv_translate_sys_error(WSAGetLastError());
1077 
1078  handle->flags |= UV_HANDLE_UDP_CONNECTED;
1079 
1080  return 0;
1081 }
struct sockaddr_in uv_addr_ip4_any_
Definition: winsock.c:34
struct sockaddr_in6 uv_addr_ip6_any_
Definition: winsock.c:35
@ UV_HANDLE_UDP_CONNECTED
Definition: uv-common.h:114
@ UV_HANDLE_BOUND
Definition: uv-common.h:91

References addr, addrlen, err, handle, uv_addr_ip4_any_, uv_addr_ip6_any_, UV_HANDLE_BOUND, UV_HANDLE_UDP_CONNECTED, uv_translate_sys_error(), and uv_udp_maybe_bind().

◆ uv__udp_disconnect()

int uv__udp_disconnect ( uv_udp_t handle)

Definition at line 1084 of file udp.c.

1084  {
1085  int err;
1086  struct sockaddr addr;
1087 
1088  memset(&addr, 0, sizeof(addr));
1089 
1090  err = connect(handle->socket, &addr, sizeof(addr));
1091  if (err)
1092  return uv_translate_sys_error(WSAGetLastError());
1093 
1094  handle->flags &= ~UV_HANDLE_UDP_CONNECTED;
1095  return 0;
1096 }

References addr, err, handle, memset(), UV_HANDLE_UDP_CONNECTED, and uv_translate_sys_error().

◆ uv__udp_init_ex()

int uv__udp_init_ex ( uv_loop_t loop,
uv_udp_t handle,
unsigned  flags,
int  domain 
)

Definition at line 128 of file udp.c.

131  {
132  uv__handle_init(loop, (uv_handle_t*) handle, UV_UDP);
133  handle->socket = INVALID_SOCKET;
134  handle->reqs_pending = 0;
135  handle->activecnt = 0;
136  handle->func_wsarecv = WSARecv;
137  handle->func_wsarecvfrom = WSARecvFrom;
138  handle->send_queue_size = 0;
139  handle->send_queue_count = 0;
140  UV_REQ_INIT(&handle->recv_req, UV_UDP_RECV);
141  handle->recv_req.data = handle;
142 
143  /* If anything fails beyond this point we need to remove the handle from
144  * the handle queue, since it was added by uv__handle_init.
145  */
146 
147  if (domain != AF_UNSPEC) {
148  SOCKET sock;
149  DWORD err;
150 
151  sock = socket(domain, SOCK_DGRAM, 0);
152  if (sock == INVALID_SOCKET) {
153  err = WSAGetLastError();
154  QUEUE_REMOVE(&handle->handle_queue);
155  return uv_translate_sys_error(err);
156  }
157 
158  err = uv_udp_set_socket(handle->loop, handle, sock, domain);
159  if (err) {
160  closesocket(sock);
161  QUEUE_REMOVE(&handle->handle_queue);
162  return uv_translate_sys_error(err);
163  }
164  }
165 
166  return 0;
167 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd domain
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd socket
Definition: sflib.h:79
#define QUEUE_REMOVE(q)
Definition: queue.h:101
@ SOCK_DGRAM
Definition: sftypes.h:227
#define AF_UNSPEC
Definition: sftypes.h:283
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:301
static int uv_udp_set_socket(uv_loop_t *loop, uv_udp_t *handle, SOCKET socket, int family)
Definition: udp.c:63

References AF_UNSPEC, domain, DWORD, err, handle, loop, QUEUE_REMOVE, SOCK_DGRAM, socket, uv__handle_init, UV_REQ_INIT, uv_translate_sys_error(), and uv_udp_set_socket().

◆ uv__udp_is_bound()

int uv__udp_is_bound ( uv_udp_t handle)

Definition at line 938 of file udp.c.

938  {
939  struct sockaddr_storage addr;
940  int addrlen;
941 
942  addrlen = sizeof(addr);
943  if (uv_udp_getsockname(handle, (struct sockaddr*) &addr, &addrlen) != 0)
944  return 0;
945 
946  return addrlen > 0;
947 }
int uv_udp_getsockname(const uv_udp_t *handle, struct sockaddr *name, int *namelen)
Definition: udp.c:1286

References addr, addrlen, handle, and uv_udp_getsockname().

Referenced by uv_udp_open().

◆ uv__udp_recv_start()

int uv__udp_recv_start ( uv_udp_t handle,
uv_alloc_cb  alloc_cb,
uv_udp_recv_cb  recv_cb 
)

Definition at line 361 of file udp.c.

362  {
363  uv_loop_t* loop = handle->loop;
364  int err;
365 
366  if (handle->flags & UV_HANDLE_READING) {
367  return UV_EALREADY;
368  }
369 
371  (const struct sockaddr*) &uv_addr_ip4_any_,
372  sizeof(uv_addr_ip4_any_),
373  0);
374  if (err)
375  return uv_translate_sys_error(err);
376 
377  handle->flags |= UV_HANDLE_READING;
379  loop->active_udp_streams++;
380 
381  handle->recv_cb = recv_cb;
382  handle->alloc_cb = alloc_cb;
383 
384  /* If reading was stopped and then started again, there could still be a recv
385  * request pending. */
386  if (!(handle->flags & UV_HANDLE_READ_PENDING))
388 
389  return 0;
390 }
#define INCREASE_ACTIVE_COUNT(loop, handle)
Definition: handle-inl.h:42
@ UV_HANDLE_READING
Definition: uv-common.h:90
@ UV_HANDLE_READ_PENDING
Definition: uv-common.h:94
static void uv_udp_queue_recv(uv_loop_t *loop, uv_udp_t *handle)
Definition: udp.c:267

References err, handle, INCREASE_ACTIVE_COUNT, loop, uv_addr_ip4_any_, UV_HANDLE_READ_PENDING, UV_HANDLE_READING, uv_translate_sys_error(), uv_udp_maybe_bind(), and uv_udp_queue_recv().

◆ uv__udp_recv_stop()

int uv__udp_recv_stop ( uv_udp_t handle)

Definition at line 393 of file udp.c.

393  {
394  if (handle->flags & UV_HANDLE_READING) {
395  handle->flags &= ~UV_HANDLE_READING;
396  handle->loop->active_udp_streams--;
398  }
399 
400  return 0;
401 }
#define DECREASE_ACTIVE_COUNT(loop, handle)
Definition: handle-inl.h:32

References DECREASE_ACTIVE_COUNT, handle, loop, and UV_HANDLE_READING.

◆ uv__udp_send()

int uv__udp_send ( uv_udp_send_t req,
uv_udp_t handle,
const uv_buf_t  bufs[],
unsigned int  nbufs,
const struct sockaddr addr,
unsigned int  addrlen,
uv_udp_send_cb  send_cb 
)

Definition at line 1102 of file udp.c.

1108  {
1109  const struct sockaddr* bind_addr;
1110  int err;
1111 
1112  if (!(handle->flags & UV_HANDLE_BOUND)) {
1113  if (addrlen == sizeof(uv_addr_ip4_any_))
1114  bind_addr = (const struct sockaddr*) &uv_addr_ip4_any_;
1115  else if (addrlen == sizeof(uv_addr_ip6_any_))
1116  bind_addr = (const struct sockaddr*) &uv_addr_ip6_any_;
1117  else
1118  return UV_EINVAL;
1119 
1120  err = uv_udp_maybe_bind(handle, bind_addr, addrlen, 0);
1121  if (err)
1122  return uv_translate_sys_error(err);
1123  }
1124 
1125  err = uv__send(req, handle, bufs, nbufs, addr, addrlen, send_cb);
1126  if (err)
1127  return uv_translate_sys_error(err);
1128 
1129  return 0;
1130 }
static int uv__send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, unsigned int addrlen, uv_udp_send_cb cb)
Definition: udp.c:404

References addr, addrlen, bufs, err, handle, req, uv__send(), uv_addr_ip4_any_, uv_addr_ip6_any_, UV_HANDLE_BOUND, uv_translate_sys_error(), and uv_udp_maybe_bind().

◆ uv__udp_set_membership4()

static int uv__udp_set_membership4 ( uv_udp_t handle,
const struct sockaddr_in multicast_addr,
const char *  interface_addr,
uv_membership  membership 
)
static

Definition at line 589 of file udp.c.

592  {
593  int err;
594  int optname;
595  struct ip_mreq mreq;
596 
597  if (handle->flags & UV_HANDLE_IPV6)
598  return UV_EINVAL;
599 
600  /* If the socket is unbound, bind to inaddr_any. */
602  (const struct sockaddr*) &uv_addr_ip4_any_,
603  sizeof(uv_addr_ip4_any_),
605  if (err)
606  return uv_translate_sys_error(err);
607 
608  memset(&mreq, 0, sizeof mreq);
609 
610  if (interface_addr) {
611  err = uv_inet_pton(AF_INET, interface_addr, &mreq.imr_interface.s_addr);
612  if (err)
613  return err;
614  } else {
615  mreq.imr_interface.s_addr = htonl(INADDR_ANY);
616  }
617 
618  mreq.imr_multiaddr.s_addr = multicast_addr->sin_addr.s_addr;
619 
620  switch (membership) {
621  case UV_JOIN_GROUP:
622  optname = IP_ADD_MEMBERSHIP;
623  break;
624  case UV_LEAVE_GROUP:
625  optname = IP_DROP_MEMBERSHIP;
626  break;
627  default:
628  return UV_EINVAL;
629  }
630 
631  if (setsockopt(handle->socket,
632  IPPROTO_IP,
633  optname,
634  (char*) &mreq,
635  sizeof mreq) == SOCKET_ERROR) {
636  return uv_translate_sys_error(WSAGetLastError());
637  }
638 
639  return 0;
640 }
#define AF_INET
Definition: sftypes.h:287
in_addr_t s_addr
Definition: sftypes.h:337
struct in_addr sin_addr
Definition: sftypes.h:344
@ UV_HANDLE_IPV6
Definition: uv-common.h:102
@ UV_UDP_REUSEADDR
Definition: uv.h:611
@ UV_JOIN_GROUP
Definition: uv.h:384
@ UV_LEAVE_GROUP
Definition: uv.h:383
UV_EXTERN int uv_inet_pton(int af, const char *src, void *dst)
Definition: inet.c:150

References AF_INET, err, handle, memset(), in_addr::s_addr, sockaddr_in::sin_addr, uv_addr_ip4_any_, UV_HANDLE_IPV6, uv_inet_pton(), UV_JOIN_GROUP, UV_LEAVE_GROUP, uv_translate_sys_error(), uv_udp_maybe_bind(), and UV_UDP_REUSEADDR.

Referenced by uv_udp_set_membership().

◆ uv__udp_set_membership6()

int uv__udp_set_membership6 ( uv_udp_t handle,
const struct sockaddr_in6 multicast_addr,
const char *  interface_addr,
uv_membership  membership 
)

Definition at line 643 of file udp.c.

646  {
647  int optname;
648  int err;
649  struct ipv6_mreq mreq;
650  struct sockaddr_in6 addr6;
651 
652  if ((handle->flags & UV_HANDLE_BOUND) && !(handle->flags & UV_HANDLE_IPV6))
653  return UV_EINVAL;
654 
656  (const struct sockaddr*) &uv_addr_ip6_any_,
657  sizeof(uv_addr_ip6_any_),
659 
660  if (err)
661  return uv_translate_sys_error(err);
662 
663  memset(&mreq, 0, sizeof(mreq));
664 
665  if (interface_addr) {
666  if (uv_ip6_addr(interface_addr, 0, &addr6))
667  return UV_EINVAL;
668  mreq.ipv6mr_interface = addr6.sin6_scope_id;
669  } else {
670  mreq.ipv6mr_interface = 0;
671  }
672 
673  mreq.ipv6mr_multiaddr = multicast_addr->sin6_addr;
674 
675  switch (membership) {
676  case UV_JOIN_GROUP:
677  optname = IPV6_ADD_MEMBERSHIP;
678  break;
679  case UV_LEAVE_GROUP:
680  optname = IPV6_DROP_MEMBERSHIP;
681  break;
682  default:
683  return UV_EINVAL;
684  }
685 
686  if (setsockopt(handle->socket,
687  IPPROTO_IPV6,
688  optname,
689  (char*) &mreq,
690  sizeof mreq) == SOCKET_ERROR) {
691  return uv_translate_sys_error(WSAGetLastError());
692  }
693 
694  return 0;
695 }
struct in6_addr sin6_addr
Definition: sftypes.h:375
UV_EXTERN int uv_ip6_addr(const char *ip, int port, struct sockaddr_in6 *addr)
Definition: uv-common.c:232

References err, handle, memset(), sockaddr_in6::sin6_addr, sockaddr_in6::sin6_scope_id, uv_addr_ip6_any_, UV_HANDLE_BOUND, UV_HANDLE_IPV6, uv_ip6_addr(), UV_JOIN_GROUP, UV_LEAVE_GROUP, uv_translate_sys_error(), uv_udp_maybe_bind(), and UV_UDP_REUSEADDR.

Referenced by uv_udp_set_membership().

◆ uv__udp_set_source_membership4()

static int uv__udp_set_source_membership4 ( uv_udp_t handle,
const struct sockaddr_in multicast_addr,
const char *  interface_addr,
const struct sockaddr_in source_addr,
uv_membership  membership 
)
static

Definition at line 698 of file udp.c.

702  {
703  struct ip_mreq_source mreq;
704  int optname;
705  int err;
706 
707  if (handle->flags & UV_HANDLE_IPV6)
708  return UV_EINVAL;
709 
710  /* If the socket is unbound, bind to inaddr_any. */
712  (const struct sockaddr*) &uv_addr_ip4_any_,
713  sizeof(uv_addr_ip4_any_),
715  if (err)
716  return uv_translate_sys_error(err);
717 
718  memset(&mreq, 0, sizeof(mreq));
719 
720  if (interface_addr != NULL) {
721  err = uv_inet_pton(AF_INET, interface_addr, &mreq.imr_interface.s_addr);
722  if (err)
723  return err;
724  } else {
725  mreq.imr_interface.s_addr = htonl(INADDR_ANY);
726  }
727 
728  mreq.imr_multiaddr.s_addr = multicast_addr->sin_addr.s_addr;
729  mreq.imr_sourceaddr.s_addr = source_addr->sin_addr.s_addr;
730 
731  if (membership == UV_JOIN_GROUP)
732  optname = IP_ADD_SOURCE_MEMBERSHIP;
733  else if (membership == UV_LEAVE_GROUP)
734  optname = IP_DROP_SOURCE_MEMBERSHIP;
735  else
736  return UV_EINVAL;
737 
738  if (setsockopt(handle->socket,
739  IPPROTO_IP,
740  optname,
741  (char*) &mreq,
742  sizeof(mreq)) == SOCKET_ERROR) {
743  return uv_translate_sys_error(WSAGetLastError());
744  }
745 
746  return 0;
747 }

References AF_INET, err, handle, memset(), NULL, in_addr::s_addr, sockaddr_in::sin_addr, uv_addr_ip4_any_, UV_HANDLE_IPV6, uv_inet_pton(), UV_JOIN_GROUP, UV_LEAVE_GROUP, uv_translate_sys_error(), uv_udp_maybe_bind(), and UV_UDP_REUSEADDR.

Referenced by uv_udp_set_source_membership().

◆ uv__udp_set_source_membership6()

int uv__udp_set_source_membership6 ( uv_udp_t handle,
const struct sockaddr_in6 multicast_addr,
const char *  interface_addr,
const struct sockaddr_in6 source_addr,
uv_membership  membership 
)

Definition at line 750 of file udp.c.

754  {
755  struct group_source_req mreq;
756  struct sockaddr_in6 addr6;
757  int optname;
758  int err;
759 
760  STATIC_ASSERT(sizeof(mreq.gsr_group) >= sizeof(*multicast_addr));
761  STATIC_ASSERT(sizeof(mreq.gsr_source) >= sizeof(*source_addr));
762 
763  if ((handle->flags & UV_HANDLE_BOUND) && !(handle->flags & UV_HANDLE_IPV6))
764  return UV_EINVAL;
765 
767  (const struct sockaddr*) &uv_addr_ip6_any_,
768  sizeof(uv_addr_ip6_any_),
770 
771  if (err)
772  return uv_translate_sys_error(err);
773 
774  memset(&mreq, 0, sizeof(mreq));
775 
776  if (interface_addr != NULL) {
777  err = uv_ip6_addr(interface_addr, 0, &addr6);
778  if (err)
779  return err;
780  mreq.gsr_interface = addr6.sin6_scope_id;
781  } else {
782  mreq.gsr_interface = 0;
783  }
784 
785  memcpy(&mreq.gsr_group, multicast_addr, sizeof(*multicast_addr));
786  memcpy(&mreq.gsr_source, source_addr, sizeof(*source_addr));
787 
788  if (membership == UV_JOIN_GROUP)
789  optname = MCAST_JOIN_SOURCE_GROUP;
790  else if (membership == UV_LEAVE_GROUP)
791  optname = MCAST_LEAVE_SOURCE_GROUP;
792  else
793  return UV_EINVAL;
794 
795  if (setsockopt(handle->socket,
796  IPPROTO_IPV6,
797  optname,
798  (char*) &mreq,
799  sizeof(mreq)) == SOCKET_ERROR) {
800  return uv_translate_sys_error(WSAGetLastError());
801  }
802 
803  return 0;
804 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define STATIC_ASSERT(expr)
Definition: uv-common.h:60
#define MCAST_LEAVE_SOURCE_GROUP
Definition: winsock.h:62
#define MCAST_JOIN_SOURCE_GROUP
Definition: winsock.h:58

References err, handle, MCAST_JOIN_SOURCE_GROUP, MCAST_LEAVE_SOURCE_GROUP, memcpy(), memset(), NULL, sockaddr_in6::sin6_scope_id, STATIC_ASSERT, uv_addr_ip6_any_, UV_HANDLE_BOUND, UV_HANDLE_IPV6, uv_ip6_addr(), UV_JOIN_GROUP, UV_LEAVE_GROUP, uv_translate_sys_error(), uv_udp_maybe_bind(), and UV_UDP_REUSEADDR.

Referenced by uv_udp_set_source_membership().

◆ uv__udp_try_send()

int uv__udp_try_send ( uv_udp_t handle,
const uv_buf_t  bufs[],
unsigned int  nbufs,
const struct sockaddr addr,
unsigned int  addrlen 
)

Definition at line 1133 of file udp.c.

1137  {
1138  DWORD bytes;
1139  const struct sockaddr* bind_addr;
1140  struct sockaddr_storage converted;
1141  int err;
1142 
1143  assert(nbufs > 0);
1144 
1145  if (addr != NULL) {
1147  if (err)
1148  return err;
1149  }
1150 
1151  /* Already sending a message.*/
1152  if (handle->send_queue_count != 0)
1153  return UV_EAGAIN;
1154 
1155  if (!(handle->flags & UV_HANDLE_BOUND)) {
1156  if (addrlen == sizeof(uv_addr_ip4_any_))
1157  bind_addr = (const struct sockaddr*) &uv_addr_ip4_any_;
1158  else if (addrlen == sizeof(uv_addr_ip6_any_))
1159  bind_addr = (const struct sockaddr*) &uv_addr_ip6_any_;
1160  else
1161  return UV_EINVAL;
1162  err = uv_udp_maybe_bind(handle, bind_addr, addrlen, 0);
1163  if (err)
1164  return uv_translate_sys_error(err);
1165  }
1166 
1167  err = WSASendTo(handle->socket,
1168  (WSABUF*)bufs,
1169  nbufs,
1170  &bytes,
1171  0,
1172  (const struct sockaddr*) &converted,
1173  addrlen,
1174  NULL,
1175  NULL);
1176 
1177  if (err)
1178  return uv_translate_sys_error(WSAGetLastError());
1179 
1180  return bytes;
1181 }
assert(limit<=UINT32_MAX/2)
int uv__convert_to_localhost_if_unspecified(const struct sockaddr *addr, struct sockaddr_storage *storage)
Definition: winsock.c:547

References addr, addrlen, assert(), bufs, bytes, DWORD, err, handle, NULL, uv__convert_to_localhost_if_unspecified(), uv_addr_ip4_any_, uv_addr_ip6_any_, UV_HANDLE_BOUND, uv_translate_sys_error(), and uv_udp_maybe_bind().

◆ uv_process_udp_recv_req()

void uv_process_udp_recv_req ( uv_loop_t loop,
uv_udp_t handle,
uv_req_t req 
)

Definition at line 453 of file udp.c.

454  {
455  uv_buf_t buf;
456  int partial;
457 
458  assert(handle->type == UV_UDP);
459 
460  handle->flags &= ~UV_HANDLE_READ_PENDING;
461 
462  if (!REQ_SUCCESS(req)) {
464  if (err == WSAEMSGSIZE) {
465  /* Not a real error, it just indicates that the received packet was
466  * bigger than the receive buffer. */
467  } else if (err == WSAECONNRESET || err == WSAENETRESET) {
468  /* A previous sendto operation failed; ignore this error. If zero-reading
469  * we need to call WSARecv/WSARecvFrom _without_ the. MSG_PEEK flag to
470  * clear out the error queue. For nonzero reads, immediately queue a new
471  * receive. */
472  if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
473  goto done;
474  }
475  } else {
476  /* A real error occurred. Report the error to the user only if we're
477  * currently reading. */
478  if (handle->flags & UV_HANDLE_READING) {
480  buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
481  uv_buf_init(NULL, 0) : handle->recv_buffer;
482  handle->recv_cb(handle, uv_translate_sys_error(err), &buf, NULL, 0);
483  }
484  goto done;
485  }
486  }
487 
488  if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
489  /* Successful read */
490  partial = !REQ_SUCCESS(req);
491  handle->recv_cb(handle,
492  req->u.io.overlapped.InternalHigh,
493  &handle->recv_buffer,
494  (const struct sockaddr*) &handle->recv_from,
495  partial ? UV_UDP_PARTIAL : 0);
496  } else if (handle->flags & UV_HANDLE_READING) {
497  DWORD bytes, err, flags;
498  struct sockaddr_storage from;
499  int from_len;
500 
501  /* Do a nonblocking receive.
502  * TODO: try to read multiple datagrams at once. FIONREAD maybe? */
503  buf = uv_buf_init(NULL, 0);
504  handle->alloc_cb((uv_handle_t*) handle, 65536, &buf);
505  if (buf.base == NULL || buf.len == 0) {
506  handle->recv_cb(handle, UV_ENOBUFS, &buf, NULL, 0);
507  goto done;
508  }
509  assert(buf.base != NULL);
510 
511  memset(&from, 0, sizeof from);
512  from_len = sizeof from;
513 
514  flags = 0;
515 
516  if (WSARecvFrom(handle->socket,
517  (WSABUF*)&buf,
518  1,
519  &bytes,
520  &flags,
521  (struct sockaddr*) &from,
522  &from_len,
523  NULL,
524  NULL) != SOCKET_ERROR) {
525 
526  /* Message received */
527  handle->recv_cb(handle, bytes, &buf, (const struct sockaddr*) &from, 0);
528  } else {
529  err = WSAGetLastError();
530  if (err == WSAEMSGSIZE) {
531  /* Message truncated */
532  handle->recv_cb(handle,
533  bytes,
534  &buf,
535  (const struct sockaddr*) &from,
537  } else if (err == WSAEWOULDBLOCK) {
538  /* Kernel buffer empty */
539  handle->recv_cb(handle, 0, &buf, NULL, 0);
540  } else if (err == WSAECONNRESET || err == WSAENETRESET) {
541  /* WSAECONNRESET/WSANETRESET is ignored because this just indicates
542  * that a previous sendto operation failed.
543  */
544  handle->recv_cb(handle, 0, &buf, NULL, 0);
545  } else {
546  /* Any other error that we want to report back to the user. */
548  handle->recv_cb(handle, uv_translate_sys_error(err), &buf, NULL, 0);
549  }
550  }
551  }
552 
553 done:
554  /* Post another read if still reading and not closing. */
555  if ((handle->flags & UV_HANDLE_READING) &&
556  !(handle->flags & UV_HANDLE_READ_PENDING)) {
558  }
559 
561 }
struct tab * done
Definition: enough.c:233
#define DECREASE_PENDING_REQ_COUNT(handle)
Definition: handle-inl.h:51
voidpf void * buf
Definition: ioapi.h:138
#define REQ_SUCCESS(req)
Definition: req-inl.h:46
#define GET_REQ_SOCK_ERROR(req)
Definition: req-inl.h:52
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
Definition: unix.h:123
@ UV_HANDLE_ZERO_READ
Definition: uv-common.h:96
@ UV_UDP_PARTIAL
Definition: uv.h:602
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
UV_EXTERN int uv_udp_recv_stop(uv_udp_t *handle)
Definition: uv-common.c:478

References assert(), bytes, DECREASE_PENDING_REQ_COUNT, done, DWORD, err, flags, from, GET_REQ_SOCK_ERROR, handle, loop, memset(), NULL, req, REQ_SUCCESS, uv_buf_init(), UV_HANDLE_READ_PENDING, UV_HANDLE_READING, UV_HANDLE_ZERO_READ, uv_translate_sys_error(), UV_UDP_PARTIAL, uv_udp_queue_recv(), and uv_udp_recv_stop().

Referenced by uv_process_reqs().

◆ uv_process_udp_send_req()

void uv_process_udp_send_req ( uv_loop_t loop,
uv_udp_t handle,
uv_udp_send_t req 
)

Definition at line 564 of file udp.c.

565  {
566  int err;
567 
568  assert(handle->type == UV_UDP);
569 
570  assert(handle->send_queue_size >= req->u.io.queued_bytes);
571  assert(handle->send_queue_count >= 1);
572  handle->send_queue_size -= req->u.io.queued_bytes;
573  handle->send_queue_count--;
574 
576 
577  if (req->cb) {
578  err = 0;
579  if (!REQ_SUCCESS(req)) {
581  }
583  }
584 
586 }
#define UNREGISTER_HANDLE_REQ(loop, handle, req)
Definition: req-inl.h:62

References assert(), DECREASE_PENDING_REQ_COUNT, err, GET_REQ_SOCK_ERROR, handle, loop, req, REQ_SUCCESS, UNREGISTER_HANDLE_REQ, and uv_translate_sys_error().

Referenced by uv_process_reqs().

◆ uv_udp_close()

void uv_udp_close ( uv_loop_t loop,
uv_udp_t handle 
)

Definition at line 170 of file udp.c.

170  {
172  closesocket(handle->socket);
173  handle->socket = INVALID_SOCKET;
174 
176 
177  if (handle->reqs_pending == 0) {
179  }
180 }
static INLINE void uv_want_endgame(uv_loop_t *loop, uv_handle_t *handle)
Definition: handle-inl.h:88
#define uv__handle_closing(handle)
Definition: handle-inl.h:63

References handle, loop, uv__handle_closing, uv_udp_recv_stop(), and uv_want_endgame().

Referenced by uv_close().

◆ uv_udp_endgame()

void uv_udp_endgame ( uv_loop_t loop,
uv_udp_t handle 
)

Definition at line 183 of file udp.c.

183  {
184  if (handle->flags & UV_HANDLE_CLOSING &&
185  handle->reqs_pending == 0) {
186  assert(!(handle->flags & UV_HANDLE_CLOSED));
188  }
189 }
#define uv__handle_close(handle)
Definition: handle-inl.h:76
@ UV_HANDLE_CLOSING
Definition: uv-common.h:74
@ UV_HANDLE_CLOSED
Definition: uv-common.h:75

References assert(), handle, uv__handle_close, UV_HANDLE_CLOSED, and UV_HANDLE_CLOSING.

Referenced by uv_process_endgames().

◆ uv_udp_getpeername()

int uv_udp_getpeername ( const uv_udp_t handle,
struct sockaddr name,
int namelen 
)

Definition at line 39 of file udp.c.

41  {
42 
43  return uv__getsockpeername((const uv_handle_t*) handle,
44  getpeername,
45  name,
46  namelen,
47  0);
48 }
Definition: z80asm.h:102
int uv__getsockpeername(const uv_handle_t *handle, uv__peersockfunc func, struct sockaddr *name, int *namelen)
Definition: core.c:1490

References handle, and uv__getsockpeername().

Referenced by uv__udp_is_connected().

◆ uv_udp_getsockname()

int uv_udp_getsockname ( const uv_udp_t handle,
struct sockaddr name,
int namelen 
)

Definition at line 51 of file udp.c.

53  {
54 
55  return uv__getsockpeername((const uv_handle_t*) handle,
56  getsockname,
57  name,
58  namelen,
59  0);
60 }

References handle, and uv__getsockpeername().

◆ uv_udp_maybe_bind()

static int uv_udp_maybe_bind ( uv_udp_t handle,
const struct sockaddr addr,
unsigned int  addrlen,
unsigned int  flags 
)
static

Definition at line 197 of file udp.c.

200  {
201  int r;
202  int err;
203  DWORD no = 0;
204 
205  if (handle->flags & UV_HANDLE_BOUND)
206  return 0;
207 
208  if ((flags & UV_UDP_IPV6ONLY) && addr->sa_family != AF_INET6) {
209  /* UV_UDP_IPV6ONLY is supported only for IPV6 sockets */
210  return ERROR_INVALID_PARAMETER;
211  }
212 
213  if (handle->socket == INVALID_SOCKET) {
214  SOCKET sock = socket(addr->sa_family, SOCK_DGRAM, 0);
215  if (sock == INVALID_SOCKET) {
216  return WSAGetLastError();
217  }
218 
219  err = uv_udp_set_socket(handle->loop, handle, sock, addr->sa_family);
220  if (err) {
221  closesocket(sock);
222  return err;
223  }
224  }
225 
226  if (flags & UV_UDP_REUSEADDR) {
227  DWORD yes = 1;
228  /* Set SO_REUSEADDR on the socket. */
229  if (setsockopt(handle->socket,
230  SOL_SOCKET,
231  SO_REUSEADDR,
232  (char*) &yes,
233  sizeof yes) == SOCKET_ERROR) {
234  err = WSAGetLastError();
235  return err;
236  }
237  }
238 
239  if (addr->sa_family == AF_INET6)
240  handle->flags |= UV_HANDLE_IPV6;
241 
242  if (addr->sa_family == AF_INET6 && !(flags & UV_UDP_IPV6ONLY)) {
243  /* On windows IPV6ONLY is on by default. If the user doesn't specify it
244  * libuv turns it off. */
245 
246  /* TODO: how to handle errors? This may fail if there is no ipv4 stack
247  * available, or when run on XP/2003 which have no support for dualstack
248  * sockets. For now we're silently ignoring the error. */
249  setsockopt(handle->socket,
250  IPPROTO_IPV6,
251  IPV6_V6ONLY,
252  (char*) &no,
253  sizeof no);
254  }
255 
256  r = bind(handle->socket, addr, addrlen);
257  if (r == SOCKET_ERROR) {
258  return WSAGetLastError();
259  }
260 
261  handle->flags |= UV_HANDLE_BOUND;
262 
263  return 0;
264 }
#define r
Definition: crypto_rc6.c:12
static bind
Definition: sfsocketcall.h:114
#define SO_REUSEADDR
Definition: sftypes.h:430
#define SOL_SOCKET
Definition: sftypes.h:427
#define AF_INET6
Definition: sftypes.h:295
@ UV_UDP_IPV6ONLY
Definition: uv.h:597
#define IPV6_V6ONLY
Definition: winsock.h:46

References addr, addrlen, AF_INET6, bind, DWORD, err, flags, handle, IPV6_V6ONLY, r, SO_REUSEADDR, SOCK_DGRAM, socket, SOL_SOCKET, UV_HANDLE_BOUND, UV_HANDLE_IPV6, UV_UDP_IPV6ONLY, UV_UDP_REUSEADDR, and uv_udp_set_socket().

Referenced by SOCKOPT_SETTER(), uv__udp_connect(), uv__udp_recv_start(), uv__udp_send(), uv__udp_set_membership4(), uv__udp_set_membership6(), uv__udp_set_source_membership4(), uv__udp_set_source_membership6(), and uv__udp_try_send().

◆ uv_udp_open()

int uv_udp_open ( uv_udp_t handle,
uv_os_sock_t  sock 
)

Definition at line 950 of file udp.c.

950  {
951  WSAPROTOCOL_INFOW protocol_info;
952  int opt_len;
953  int err;
954 
955  /* Detect the address family of the socket. */
956  opt_len = (int) sizeof protocol_info;
957  if (getsockopt(sock,
958  SOL_SOCKET,
959  SO_PROTOCOL_INFOW,
960  (char*) &protocol_info,
961  &opt_len) == SOCKET_ERROR) {
962  return uv_translate_sys_error(GetLastError());
963  }
964 
965  err = uv_udp_set_socket(handle->loop,
966  handle,
967  sock,
968  protocol_info.iAddressFamily);
969  if (err)
970  return uv_translate_sys_error(err);
971 
973  handle->flags |= UV_HANDLE_BOUND;
974 
977 
978  return 0;
979 }
static int
Definition: sfsocketcall.h:114
int uv__udp_is_connected(uv_udp_t *handle)
Definition: uv-common.c:393
int uv__udp_is_bound(uv_udp_t *handle)
Definition: udp.c:938

References err, handle, int, SOL_SOCKET, uv__fd_exists(), uv__nonblock, uv__set_reuse(), uv__udp_is_bound(), uv__udp_is_connected(), UV_HANDLE_BOUND, UV_HANDLE_UDP_CONNECTED, uv_translate_sys_error(), and uv_udp_set_socket().

Referenced by uv_accept().

◆ uv_udp_queue_recv()

static void uv_udp_queue_recv ( uv_loop_t loop,
uv_udp_t handle 
)
static

Definition at line 267 of file udp.c.

267  {
268  uv_req_t* req;
269  uv_buf_t buf;
270  DWORD bytes, flags;
271  int result;
272 
273  assert(handle->flags & UV_HANDLE_READING);
274  assert(!(handle->flags & UV_HANDLE_READ_PENDING));
275 
276  req = &handle->recv_req;
277  memset(&req->u.io.overlapped, 0, sizeof(req->u.io.overlapped));
278 
279  /*
280  * Preallocate a read buffer if the number of active streams is below
281  * the threshold.
282  */
283  if (loop->active_udp_streams < uv_active_udp_streams_threshold) {
284  handle->flags &= ~UV_HANDLE_ZERO_READ;
285 
286  handle->recv_buffer = uv_buf_init(NULL, 0);
287  handle->alloc_cb((uv_handle_t*) handle, 65536, &handle->recv_buffer);
288  if (handle->recv_buffer.base == NULL || handle->recv_buffer.len == 0) {
289  handle->recv_cb(handle, UV_ENOBUFS, &handle->recv_buffer, NULL, 0);
290  return;
291  }
292  assert(handle->recv_buffer.base != NULL);
293 
294  buf = handle->recv_buffer;
295  memset(&handle->recv_from, 0, sizeof handle->recv_from);
296  handle->recv_from_len = sizeof handle->recv_from;
297  flags = 0;
298 
299  result = handle->func_wsarecvfrom(handle->socket,
300  (WSABUF*) &buf,
301  1,
302  &bytes,
303  &flags,
304  (struct sockaddr*) &handle->recv_from,
305  &handle->recv_from_len,
306  &req->u.io.overlapped,
307  NULL);
308 
309  if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
310  /* Process the req without IOCP. */
311  handle->flags |= UV_HANDLE_READ_PENDING;
312  req->u.io.overlapped.InternalHigh = bytes;
313  handle->reqs_pending++;
315  } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
316  /* The req will be processed with IOCP. */
317  handle->flags |= UV_HANDLE_READ_PENDING;
318  handle->reqs_pending++;
319  } else {
320  /* Make this req pending reporting an error. */
321  SET_REQ_ERROR(req, WSAGetLastError());
323  handle->reqs_pending++;
324  }
325 
326  } else {
327  handle->flags |= UV_HANDLE_ZERO_READ;
328 
329  buf.base = (char*) uv_zero_;
330  buf.len = 0;
331  flags = MSG_PEEK;
332 
333  result = handle->func_wsarecv(handle->socket,
334  (WSABUF*) &buf,
335  1,
336  &bytes,
337  &flags,
338  &req->u.io.overlapped,
339  NULL);
340 
341  if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
342  /* Process the req without IOCP. */
343  handle->flags |= UV_HANDLE_READ_PENDING;
344  req->u.io.overlapped.InternalHigh = bytes;
345  handle->reqs_pending++;
347  } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
348  /* The req will be processed with IOCP. */
349  handle->flags |= UV_HANDLE_READ_PENDING;
350  handle->reqs_pending++;
351  } else {
352  /* Make this req pending reporting an error. */
353  SET_REQ_ERROR(req, WSAGetLastError());
355  handle->reqs_pending++;
356  }
357  }
358 }
#define SET_REQ_ERROR(req, error)
Definition: req-inl.h:34
const unsigned int uv_active_udp_streams_threshold
Definition: udp.c:35
static char uv_zero_[]
Definition: udp.c:38

References assert(), bytes, DWORD, flags, handle, loop, memset(), NULL, req, SET_REQ_ERROR, uv_active_udp_streams_threshold, uv_buf_init(), UV_HANDLE_READ_PENDING, UV_HANDLE_READING, UV_HANDLE_ZERO_READ, uv_insert_pending_req(), UV_SUCCEEDED_WITH_IOCP, UV_SUCCEEDED_WITHOUT_IOCP, and uv_zero_.

Referenced by uv__udp_recv_start(), and uv_process_udp_recv_req().

◆ uv_udp_set_broadcast()

int uv_udp_set_broadcast ( uv_udp_t handle,
int  value 
)

Definition at line 920 of file udp.c.

920  {
921  BOOL optval = (BOOL) value;
922 
923  if (handle->socket == INVALID_SOCKET)
924  return UV_EBADF;
925 
926  if (setsockopt(handle->socket,
927  SOL_SOCKET,
928  SO_BROADCAST,
929  (char*) &optval,
930  sizeof optval)) {
931  return uv_translate_sys_error(WSAGetLastError());
932  }
933 
934  return 0;
935 }
static int value
Definition: cmd_api.c:93
#define SO_BROADCAST
Definition: sftypes.h:434

References handle, SO_BROADCAST, SOL_SOCKET, UV__ERR, uv_translate_sys_error(), and value.

Referenced by main().

◆ uv_udp_set_membership()

int uv_udp_set_membership ( uv_udp_t handle,
const char *  multicast_addr,
const char *  interface_addr,
uv_membership  membership 
)

Definition at line 807 of file udp.c.

810  {
811  struct sockaddr_in addr4;
812  struct sockaddr_in6 addr6;
813 
814  if (uv_ip4_addr(multicast_addr, 0, &addr4) == 0)
815  return uv__udp_set_membership4(handle, &addr4, interface_addr, membership);
816  else if (uv_ip6_addr(multicast_addr, 0, &addr6) == 0)
817  return uv__udp_set_membership6(handle, &addr6, interface_addr, membership);
818  else
819  return UV_EINVAL;
820 }
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
static int uv__udp_set_membership4(uv_udp_t *handle, const struct sockaddr_in *multicast_addr, const char *interface_addr, uv_membership membership)
Definition: udp.c:589
int uv__udp_set_membership6(uv_udp_t *handle, const struct sockaddr_in6 *multicast_addr, const char *interface_addr, uv_membership membership)
Definition: udp.c:643

References AF_INET, AF_INET6, err, handle, uv__udp_maybe_deferred_bind(), uv__udp_set_membership4(), uv__udp_set_membership6(), uv_ip4_addr(), uv_ip6_addr(), and UV_UDP_REUSEADDR.

◆ uv_udp_set_multicast_interface()

int uv_udp_set_multicast_interface ( uv_udp_t handle,
const char *  interface_addr 
)

Definition at line 867 of file udp.c.

867  {
868  struct sockaddr_storage addr_st;
869  struct sockaddr_in* addr4;
870  struct sockaddr_in6* addr6;
871 
872  addr4 = (struct sockaddr_in*) &addr_st;
873  addr6 = (struct sockaddr_in6*) &addr_st;
874 
875  if (!interface_addr) {
876  memset(&addr_st, 0, sizeof addr_st);
877  if (handle->flags & UV_HANDLE_IPV6) {
878  addr_st.ss_family = AF_INET6;
879  addr6->sin6_scope_id = 0;
880  } else {
881  addr_st.ss_family = AF_INET;
882  addr4->sin_addr.s_addr = htonl(INADDR_ANY);
883  }
884  } else if (uv_ip4_addr(interface_addr, 0, addr4) == 0) {
885  /* nothing, address was parsed */
886  } else if (uv_ip6_addr(interface_addr, 0, addr6) == 0) {
887  /* nothing, address was parsed */
888  } else {
889  return UV_EINVAL;
890  }
891 
892  if (handle->socket == INVALID_SOCKET)
893  return UV_EBADF;
894 
895  if (addr_st.ss_family == AF_INET) {
896  if (setsockopt(handle->socket,
897  IPPROTO_IP,
898  IP_MULTICAST_IF,
899  (char*) &addr4->sin_addr,
900  sizeof(addr4->sin_addr)) == SOCKET_ERROR) {
901  return uv_translate_sys_error(WSAGetLastError());
902  }
903  } else if (addr_st.ss_family == AF_INET6) {
904  if (setsockopt(handle->socket,
905  IPPROTO_IPV6,
906  IPV6_MULTICAST_IF,
907  (char*) &addr6->sin6_scope_id,
908  sizeof(addr6->sin6_scope_id)) == SOCKET_ERROR) {
909  return uv_translate_sys_error(WSAGetLastError());
910  }
911  } else {
912  assert(0 && "unexpected address family");
913  abort();
914  }
915 
916  return 0;
917 }
uint32_t sin6_scope_id
Definition: sftypes.h:376

References AF_INET, AF_INET6, assert(), handle, memset(), in_addr::s_addr, sockaddr_in6::sin6_scope_id, sockaddr_in::sin_addr, UV__ERR, UV_HANDLE_IPV6, uv_ip4_addr(), uv_ip6_addr(), and uv_translate_sys_error().

◆ uv_udp_set_socket()

static int uv_udp_set_socket ( uv_loop_t loop,
uv_udp_t handle,
SOCKET  socket,
int  family 
)
static

Definition at line 63 of file udp.c.

64  {
65  DWORD yes = 1;
66  WSAPROTOCOL_INFOW info;
67  int opt_len;
68 
69  if (handle->socket != INVALID_SOCKET)
70  return UV_EBUSY;
71 
72  /* Set the socket to nonblocking mode */
73  if (ioctlsocket(socket, FIONBIO, &yes) == SOCKET_ERROR) {
74  return WSAGetLastError();
75  }
76 
77  /* Make the socket non-inheritable */
78  if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) {
79  return GetLastError();
80  }
81 
82  /* Associate it with the I/O completion port. Use uv_handle_t pointer as
83  * completion key. */
84  if (CreateIoCompletionPort((HANDLE)socket,
85  loop->iocp,
86  (ULONG_PTR)socket,
87  0) == NULL) {
88  return GetLastError();
89  }
90 
91  /* All known Windows that support SetFileCompletionNotificationModes have a
92  * bug that makes it impossible to use this function in conjunction with
93  * datagram sockets. We can work around that but only if the user is using
94  * the default UDP driver (AFD) and has no other. LSPs stacked on top. Here
95  * we check whether that is the case. */
96  opt_len = (int) sizeof info;
97  if (getsockopt(
98  socket, SOL_SOCKET, SO_PROTOCOL_INFOW, (char*) &info, &opt_len) ==
99  SOCKET_ERROR) {
100  return GetLastError();
101  }
102 
103  if (info.ProtocolChain.ChainLen == 1) {
104  if (SetFileCompletionNotificationModes(
105  (HANDLE) socket,
109  handle->func_wsarecv = uv_wsarecv_workaround;
110  handle->func_wsarecvfrom = uv_wsarecvfrom_workaround;
111  } else if (GetLastError() != ERROR_INVALID_FUNCTION) {
112  return GetLastError();
113  }
114  }
115 
116  handle->socket = socket;
117 
118  if (family == AF_INET6) {
119  handle->flags |= UV_HANDLE_IPV6;
120  } else {
121  assert(!(handle->flags & UV_HANDLE_IPV6));
122  }
123 
124  return 0;
125 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define FIONBIO
Definition: sftypes.h:736
int WSAAPI uv_wsarecv_workaround(SOCKET socket, WSABUF *buffers, DWORD buffer_count, DWORD *bytes, DWORD *flags, WSAOVERLAPPED *overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine)
Definition: winsock.c:270
int WSAAPI uv_wsarecvfrom_workaround(SOCKET socket, WSABUF *buffers, DWORD buffer_count, DWORD *bytes, DWORD *flags, struct sockaddr *addr, int *addr_len, WSAOVERLAPPED *overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine)
Definition: winsock.c:364
@ UV_HANDLE_SYNC_BYPASS_IOCP
Definition: uv-common.h:95
#define FILE_SKIP_SET_EVENT_ON_HANDLE
Definition: winapi.h:4607
#define FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
Definition: winapi.h:4603
DWORD * HANDLE

References AF_INET6, assert(), DWORD, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS, FILE_SKIP_SET_EVENT_ON_HANDLE, FIONBIO, handle, HANDLE, info(), int, loop, NULL, socket, SOL_SOCKET, UV_HANDLE_IPV6, UV_HANDLE_SYNC_BYPASS_IOCP, uv_wsarecv_workaround(), and uv_wsarecvfrom_workaround().

Referenced by uv__udp_init_ex(), uv_udp_maybe_bind(), and uv_udp_open().

◆ uv_udp_set_source_membership()

int uv_udp_set_source_membership ( uv_udp_t handle,
const char *  multicast_addr,
const char *  interface_addr,
const char *  source_addr,
uv_membership  membership 
)

Definition at line 823 of file udp.c.

827  {
828  int err;
829  struct sockaddr_storage mcast_addr;
830  struct sockaddr_in* mcast_addr4;
831  struct sockaddr_in6* mcast_addr6;
832  struct sockaddr_storage src_addr;
833  struct sockaddr_in* src_addr4;
834  struct sockaddr_in6* src_addr6;
835 
836  mcast_addr4 = (struct sockaddr_in*)&mcast_addr;
837  mcast_addr6 = (struct sockaddr_in6*)&mcast_addr;
838  src_addr4 = (struct sockaddr_in*)&src_addr;
839  src_addr6 = (struct sockaddr_in6*)&src_addr;
840 
841  err = uv_ip4_addr(multicast_addr, 0, mcast_addr4);
842  if (err) {
843  err = uv_ip6_addr(multicast_addr, 0, mcast_addr6);
844  if (err)
845  return err;
846  err = uv_ip6_addr(source_addr, 0, src_addr6);
847  if (err)
848  return err;
850  mcast_addr6,
851  interface_addr,
852  src_addr6,
853  membership);
854  }
855 
856  err = uv_ip4_addr(source_addr, 0, src_addr4);
857  if (err)
858  return err;
860  mcast_addr4,
861  interface_addr,
862  src_addr4,
863  membership);
864 }
int uv__udp_set_source_membership6(uv_udp_t *handle, const struct sockaddr_in6 *multicast_addr, const char *interface_addr, const struct sockaddr_in6 *source_addr, uv_membership membership)
Definition: udp.c:750
static int uv__udp_set_source_membership4(uv_udp_t *handle, const struct sockaddr_in *multicast_addr, const char *interface_addr, const struct sockaddr_in *source_addr, uv_membership membership)
Definition: udp.c:698

References err, handle, uv__sockaddr::in, uv__sockaddr::in6, uv__udp_set_source_membership4(), uv__udp_set_source_membership6(), uv_ip4_addr(), and uv_ip6_addr().

◆ uv_udp_using_recvmmsg()

int uv_udp_using_recvmmsg ( const uv_udp_t handle)

Definition at line 192 of file udp.c.

192  {
193  return 0;
194 }

References handle, once, UV_HANDLE_UDP_RECVMMSG, and uv_once().

Variable Documentation

◆ uv_active_udp_streams_threshold

const unsigned int uv_active_udp_streams_threshold = 0

Definition at line 35 of file udp.c.

Referenced by uv_udp_queue_recv().

◆ uv_zero_

char uv_zero_[] = ""
static

Definition at line 38 of file udp.c.

Referenced by uv_udp_queue_recv().