~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to deps/uv/src/win/udp.c

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <assert.h>
23
23
 
24
24
#include "uv.h"
25
 
#include "../uv-common.h"
26
25
#include "internal.h"
 
26
#include "handle-inl.h"
 
27
#include "stream-inl.h"
 
28
#include "req-inl.h"
27
29
 
28
30
 
29
31
/*
34
36
/* A zero-size buffer for use by uv_udp_read */
35
37
static char uv_zero_[] = "";
36
38
 
37
 
/* Counter to keep track of active udp streams */
38
 
static unsigned int active_udp_streams = 0;
39
 
 
40
 
 
41
39
int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
42
40
    int* namelen) {
43
41
  uv_loop_t* loop = handle->loop;
58
56
}
59
57
 
60
58
 
61
 
static int uv_udp_set_socket(uv_loop_t* loop, uv_udp_t* handle,
62
 
    SOCKET socket) {
 
59
static int uv_udp_set_socket(uv_loop_t* loop, uv_udp_t* handle, SOCKET socket,
 
60
    int family) {
63
61
  DWORD yes = 1;
64
62
  WSAPROTOCOL_INFOW info;
65
63
  int opt_len;
66
64
 
67
65
  assert(handle->socket == INVALID_SOCKET);
68
66
 
 
67
  /* Set SO_REUSEADDR on the socket. */
 
68
  if (setsockopt(socket,
 
69
                 SOL_SOCKET,
 
70
                 SO_REUSEADDR,
 
71
                 (char*) &yes,
 
72
                 sizeof yes) == SOCKET_ERROR) {
 
73
    uv__set_sys_error(loop, WSAGetLastError());
 
74
    return -1;
 
75
  }
 
76
 
69
77
  /* Set the socket to nonblocking mode */
70
78
  if (ioctlsocket(socket, FIONBIO, &yes) == SOCKET_ERROR) {
71
79
    uv__set_sys_error(loop, WSAGetLastError());
95
103
    /* if the user is using the default UDP driver (AFD) and has no other */
96
104
    /* LSPs stacked on top. Here we check whether that is the case. */
97
105
    opt_len = (int) sizeof info;
98
 
    if (!getsockopt(socket,
 
106
    if (getsockopt(socket,
99
107
                   SOL_SOCKET,
100
108
                   SO_PROTOCOL_INFOW,
101
109
                   (char*) &info,
120
128
 
121
129
  handle->socket = socket;
122
130
 
 
131
  if (family == AF_INET6) {
 
132
    handle->flags |= UV_HANDLE_IPV6;
 
133
  } else {
 
134
    assert(!(handle->flags & UV_HANDLE_IPV6));
 
135
  }
 
136
 
123
137
  return 0;
124
138
}
125
139
 
126
140
 
127
141
int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
128
 
  handle->type = UV_UDP;
 
142
  uv__handle_init(loop, (uv_handle_t*) handle, UV_UDP);
 
143
 
129
144
  handle->socket = INVALID_SOCKET;
130
145
  handle->reqs_pending = 0;
131
 
  handle->loop = loop;
132
 
  handle->flags = 0;
 
146
  handle->activecnt = 0;
133
147
  handle->func_wsarecv = WSARecv;
134
148
  handle->func_wsarecvfrom = WSARecvFrom;
135
149
 
137
151
  handle->recv_req.type = UV_UDP_RECV;
138
152
  handle->recv_req.data = handle;
139
153
 
140
 
  uv_ref(loop);
141
 
 
142
 
  loop->counters.handle_init++;
143
 
  loop->counters.udp_init++;
144
 
 
145
154
  return 0;
146
155
}
147
156
 
148
157
 
 
158
void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle) {
 
159
  uv_udp_recv_stop(handle);
 
160
  closesocket(handle->socket);
 
161
 
 
162
  uv__handle_closing(handle);
 
163
 
 
164
  if (handle->reqs_pending == 0) {
 
165
    uv_want_endgame(loop, (uv_handle_t*) handle);
 
166
  }
 
167
}
 
168
 
 
169
 
149
170
void uv_udp_endgame(uv_loop_t* loop, uv_udp_t* handle) {
150
 
  if (handle->flags & UV_HANDLE_CLOSING &&
 
171
  if (handle->flags & UV__HANDLE_CLOSING &&
151
172
      handle->reqs_pending == 0) {
152
173
    assert(!(handle->flags & UV_HANDLE_CLOSED));
153
 
    handle->flags |= UV_HANDLE_CLOSED;
154
 
 
155
 
    if (handle->close_cb) {
156
 
      handle->close_cb((uv_handle_t*)handle);
157
 
    }
158
 
 
159
 
    uv_unref(loop);
 
174
    uv__handle_close(handle);
160
175
  }
161
176
}
162
177
 
163
178
 
164
179
static int uv__bind(uv_udp_t* handle,
165
 
                    int domain,
 
180
                    int family,
166
181
                    struct sockaddr* addr,
167
182
                    int addrsize,
168
183
                    unsigned int flags) {
169
184
  int r;
170
 
  DWORD no = 0, yes = 1;
 
185
  DWORD no = 0;
171
186
 
172
 
  if ((flags & UV_UDP_IPV6ONLY) && domain != AF_INET6) {
 
187
  if ((flags & UV_UDP_IPV6ONLY) && family != AF_INET6) {
173
188
    /* UV_UDP_IPV6ONLY is supported only for IPV6 sockets */
174
189
    uv__set_artificial_error(handle->loop, UV_EINVAL);
175
190
    return -1;
176
191
  }
177
192
 
178
193
  if (handle->socket == INVALID_SOCKET) {
179
 
    SOCKET sock = socket(domain, SOCK_DGRAM, 0);
 
194
    SOCKET sock = socket(family, SOCK_DGRAM, 0);
180
195
    if (sock == INVALID_SOCKET) {
181
196
      uv__set_sys_error(handle->loop, WSAGetLastError());
182
197
      return -1;
183
198
    }
184
199
 
185
 
    if (uv_udp_set_socket(handle->loop, handle, sock) == -1) {
 
200
    if (uv_udp_set_socket(handle->loop, handle, sock, family) < 0) {
186
201
      closesocket(sock);
187
202
      return -1;
188
203
    }
 
204
 
 
205
    if (family == AF_INET6)
 
206
      handle->flags |= UV_HANDLE_IPV6;
189
207
  }
190
208
 
191
 
  if (domain == AF_INET6 && !(flags & UV_UDP_IPV6ONLY)) {
 
209
  if (family == AF_INET6 && !(flags & UV_UDP_IPV6ONLY)) {
192
210
    /* On windows IPV6ONLY is on by default. */
193
211
    /* If the user doesn't specify it libuv turns it off. */
194
212
 
202
220
               sizeof no);
203
221
  }
204
222
 
205
 
  r = setsockopt(handle->socket,
206
 
                 SOL_SOCKET,
207
 
                 SO_REUSEADDR,
208
 
                 (char*) &yes,
209
 
                 sizeof yes);
210
 
  if (r == SOCKET_ERROR) {
211
 
    uv__set_sys_error(handle->loop, WSAGetLastError());
212
 
    return -1;
213
 
  }
214
 
 
215
223
  r = bind(handle->socket, addr, addrsize);
216
224
  if (r == SOCKET_ERROR) {
217
225
    uv__set_sys_error(handle->loop, WSAGetLastError());
236
244
 
237
245
int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
238
246
    unsigned int flags) {
239
 
  if (uv_allow_ipv6) {
240
 
    handle->flags |= UV_HANDLE_IPV6;
241
 
    return uv__bind(handle,
242
 
                    AF_INET6,
243
 
                    (struct sockaddr*) &addr,
244
 
                    sizeof(struct sockaddr_in6),
245
 
                    flags);
246
 
  } else {
247
 
    uv__set_sys_error(handle->loop, WSAEAFNOSUPPORT);
248
 
    return -1;
249
 
  }
 
247
  return uv__bind(handle,
 
248
                  AF_INET6,
 
249
                  (struct sockaddr*) &addr,
 
250
                  sizeof(struct sockaddr_in6),
 
251
                  flags);
250
252
}
251
253
 
252
254
 
266
268
   * Preallocate a read buffer if the number of active streams is below
267
269
   * the threshold.
268
270
  */
269
 
  if (active_udp_streams < uv_active_udp_streams_threshold) {
 
271
  if (loop->active_udp_streams < uv_active_udp_streams_threshold) {
270
272
    handle->flags &= ~UV_HANDLE_ZERO_READ;
271
273
 
272
274
    handle->recv_buffer = handle->alloc_cb((uv_handle_t*) handle, 65536);
339
341
}
340
342
 
341
343
 
342
 
int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
 
344
int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
343
345
    uv_udp_recv_cb recv_cb) {
344
346
  uv_loop_t* loop = handle->loop;
345
347
 
354
356
  }
355
357
 
356
358
  handle->flags |= UV_HANDLE_READING;
357
 
  active_udp_streams++;
 
359
  INCREASE_ACTIVE_COUNT(loop, handle);
 
360
  loop->active_udp_streams++;
358
361
 
359
362
  handle->recv_cb = recv_cb;
360
363
  handle->alloc_cb = alloc_cb;
368
371
}
369
372
 
370
373
 
371
 
int uv_udp_recv_stop(uv_udp_t* handle) {
 
374
int uv__udp_recv_stop(uv_udp_t* handle) {
372
375
  if (handle->flags & UV_HANDLE_READING) {
373
376
    handle->flags &= ~UV_HANDLE_READING;
374
 
    active_udp_streams--;
 
377
    handle->loop->active_udp_streams--;
 
378
    DECREASE_ACTIVE_COUNT(loop, handle);
375
379
  }
376
380
 
377
381
  return 0;
378
382
}
379
383
 
380
384
 
381
 
static int uv__udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
 
385
static int uv__send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
382
386
    int bufcnt, struct sockaddr* addr, int addr_len, uv_udp_send_cb cb) {
383
387
  uv_loop_t* loop = handle->loop;
384
388
  DWORD result, bytes;
403
407
    /* Request completed immediately. */
404
408
    req->queued_bytes = 0;
405
409
    handle->reqs_pending++;
406
 
    uv_ref(loop);
 
410
    REGISTER_HANDLE_REQ(loop, handle, req);
407
411
    uv_insert_pending_req(loop, (uv_req_t*)req);
408
412
  } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
409
413
    /* Request queued by the kernel. */
410
414
    req->queued_bytes = uv_count_bufs(bufs, bufcnt);
411
415
    handle->reqs_pending++;
412
 
    uv_ref(loop);
 
416
    REGISTER_HANDLE_REQ(loop, handle, req);
413
417
  } else {
414
418
    /* Send failed due to an error. */
415
419
    uv__set_sys_error(loop, WSAGetLastError());
420
424
}
421
425
 
422
426
 
423
 
int uv_udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
 
427
int uv__udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
424
428
    int bufcnt, struct sockaddr_in addr, uv_udp_send_cb cb) {
425
429
 
426
430
  if (!(handle->flags & UV_HANDLE_BOUND) &&
428
432
    return -1;
429
433
  }
430
434
 
431
 
  return uv__udp_send(req,
432
 
                      handle,
433
 
                      bufs,
434
 
                      bufcnt,
435
 
                      (struct sockaddr*) &addr,
436
 
                      sizeof addr,
437
 
                      cb);
 
435
  return uv__send(req,
 
436
                  handle,
 
437
                  bufs,
 
438
                  bufcnt,
 
439
                  (struct sockaddr*) &addr,
 
440
                  sizeof addr,
 
441
                  cb);
438
442
}
439
443
 
440
444
 
441
 
int uv_udp_send6(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
 
445
int uv__udp_send6(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
442
446
    int bufcnt, struct sockaddr_in6 addr, uv_udp_send_cb cb) {
443
447
 
444
448
  if (!(handle->flags & UV_HANDLE_BOUND) &&
446
450
    return -1;
447
451
  }
448
452
 
449
 
  return uv__udp_send(req,
450
 
                      handle,
451
 
                      bufs,
452
 
                      bufcnt,
453
 
                      (struct sockaddr*) &addr,
454
 
                      sizeof addr,
455
 
                      cb);
 
453
  return uv__send(req,
 
454
                  handle,
 
455
                  bufs,
 
456
                  bufcnt,
 
457
                  (struct sockaddr*) &addr,
 
458
                  sizeof addr,
 
459
                  cb);
456
460
}
457
461
 
458
462
 
565
569
    uv_udp_send_t* req) {
566
570
  assert(handle->type == UV_UDP);
567
571
 
 
572
  UNREGISTER_HANDLE_REQ(loop, handle, req);
 
573
 
568
574
  if (req->cb) {
569
575
    if (REQ_SUCCESS(req)) {
570
576
      req->cb(req, 0);
574
580
    }
575
581
  }
576
582
 
577
 
  uv_unref(loop);
578
583
  DECREASE_PENDING_REQ_COUNT(handle);
579
584
}
580
585
 
613
618
      optname = IP_DROP_MEMBERSHIP;
614
619
      break;
615
620
    default:
616
 
      uv__set_artificial_error(handle->loop, UV_EFAULT);
617
 
      return -1;
 
621
      return uv__set_artificial_error(handle->loop, UV_EINVAL);
618
622
  }
619
623
 
620
624
  if (setsockopt(handle->socket,
640
644
  }
641
645
 
642
646
  if (setsockopt(handle->socket,
643
 
                  SOL_SOCKET,
644
 
                  SO_BROADCAST,
645
 
                  (char*) &optval,
646
 
                  sizeof optval)) {
 
647
                 SOL_SOCKET,
 
648
                 SO_BROADCAST,
 
649
                 (char*) &optval,
 
650
                 sizeof optval)) {
647
651
    uv__set_sys_error(handle->loop, WSAGetLastError());
648
652
    return -1;
649
653
  }
651
655
}
652
656
 
653
657
 
654
 
#define SOCKOPT_SETTER(name, option4, option6)                                \
 
658
int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) {
 
659
  WSAPROTOCOL_INFOW protocol_info;
 
660
  int opt_len;
 
661
 
 
662
  /* Detect the address family of the socket. */
 
663
  opt_len = (int) sizeof protocol_info;
 
664
  if (getsockopt(sock,
 
665
                 SOL_SOCKET,
 
666
                 SO_PROTOCOL_INFOW,
 
667
                 (char*) &protocol_info,
 
668
                 &opt_len) == SOCKET_ERROR) {
 
669
    uv__set_sys_error(handle->loop, GetLastError());
 
670
    return -1;
 
671
  }
 
672
 
 
673
  if (uv_udp_set_socket(handle->loop,
 
674
                        handle,
 
675
                        sock,
 
676
                        protocol_info.iAddressFamily) < 0) {
 
677
    return -1;
 
678
  }
 
679
 
 
680
  return 0;
 
681
}
 
682
 
 
683
 
 
684
#define SOCKOPT_SETTER(name, option4, option6, validate)                      \
655
685
  int uv_udp_set_##name(uv_udp_t* handle, int value) {                        \
656
686
    DWORD optval = (DWORD) value;                                             \
657
687
                                                                              \
 
688
    if (!(validate(value))) {                                                 \
 
689
      uv__set_artificial_error(handle->loop, UV_EINVAL);                      \
 
690
      return -1;                                                              \
 
691
    }                                                                         \
 
692
                                                                              \
658
693
    /* If the socket is unbound, bind to inaddr_any. */                       \
659
694
    if (!(handle->flags & UV_HANDLE_BOUND) &&                                 \
660
695
        uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) {                       \
685
720
    return 0;                                                                 \
686
721
  }
687
722
 
688
 
SOCKOPT_SETTER(multicast_loop, IP_MULTICAST_LOOP, IPV6_MULTICAST_LOOP)
689
 
SOCKOPT_SETTER(multicast_ttl, IP_MULTICAST_TTL, IPV6_MULTICAST_HOPS)
690
 
SOCKOPT_SETTER(ttl, IP_TTL, IPV6_HOPLIMIT)
 
723
#define VALIDATE_TTL(value) ((value) >= 1 && (value) <= 255)
 
724
#define VALIDATE_MULTICAST_TTL(value) ((value) >= -1 && (value) <= 255)
 
725
#define VALIDATE_MULTICAST_LOOP(value) (1)
 
726
 
 
727
SOCKOPT_SETTER(ttl,
 
728
               IP_TTL,
 
729
               IPV6_HOPLIMIT,
 
730
               VALIDATE_TTL)
 
731
SOCKOPT_SETTER(multicast_ttl,
 
732
               IP_MULTICAST_TTL,
 
733
               IPV6_MULTICAST_HOPS,
 
734
               VALIDATE_MULTICAST_TTL)
 
735
SOCKOPT_SETTER(multicast_loop,
 
736
               IP_MULTICAST_LOOP,
 
737
               IPV6_MULTICAST_LOOP,
 
738
               VALIDATE_MULTICAST_LOOP)
691
739
 
692
740
#undef SOCKOPT_SETTER
 
741
#undef VALIDATE_TTL
 
742
#undef VALIDATE_MULTICAST_TTL
 
743
#undef VALIDATE_MULTICAST_LOOP