rbuv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +0 -1
  6. data/README.md +6 -1
  7. data/Rakefile +42 -0
  8. data/deps/libuv/.gitignore +34 -0
  9. data/deps/libuv/.mailmap +16 -0
  10. data/deps/libuv/AUTHORS +81 -0
  11. data/deps/libuv/ChangeLog +45 -0
  12. data/deps/libuv/LICENSE +41 -0
  13. data/deps/libuv/Makefile +53 -0
  14. data/deps/libuv/README.md +118 -0
  15. data/deps/libuv/build.mk +164 -0
  16. data/deps/libuv/checksparse.sh +230 -0
  17. data/deps/libuv/common.gypi +197 -0
  18. data/deps/libuv/config-mingw.mk +48 -0
  19. data/deps/libuv/config-unix.mk +167 -0
  20. data/deps/libuv/gyp_uv +98 -0
  21. data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
  22. data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
  23. data/deps/libuv/include/uv-private/tree.h +768 -0
  24. data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
  25. data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
  26. data/deps/libuv/include/uv-private/uv-linux.h +34 -0
  27. data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
  28. data/deps/libuv/include/uv-private/uv-unix.h +332 -0
  29. data/deps/libuv/include/uv-private/uv-win.h +585 -0
  30. data/deps/libuv/include/uv.h +1987 -0
  31. data/deps/libuv/src/fs-poll.c +248 -0
  32. data/deps/libuv/src/inet.c +298 -0
  33. data/deps/libuv/src/unix/aix.c +393 -0
  34. data/deps/libuv/src/unix/async.c +281 -0
  35. data/deps/libuv/src/unix/core.c +714 -0
  36. data/deps/libuv/src/unix/cygwin.c +93 -0
  37. data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
  38. data/deps/libuv/src/unix/darwin.c +431 -0
  39. data/deps/libuv/src/unix/dl.c +83 -0
  40. data/deps/libuv/src/unix/error.c +109 -0
  41. data/deps/libuv/src/unix/freebsd.c +343 -0
  42. data/deps/libuv/src/unix/fs.c +869 -0
  43. data/deps/libuv/src/unix/fsevents.c +299 -0
  44. data/deps/libuv/src/unix/getaddrinfo.c +159 -0
  45. data/deps/libuv/src/unix/internal.h +259 -0
  46. data/deps/libuv/src/unix/kqueue.c +347 -0
  47. data/deps/libuv/src/unix/linux-core.c +724 -0
  48. data/deps/libuv/src/unix/linux-inotify.c +236 -0
  49. data/deps/libuv/src/unix/linux-syscalls.c +388 -0
  50. data/deps/libuv/src/unix/linux-syscalls.h +150 -0
  51. data/deps/libuv/src/unix/loop-watcher.c +64 -0
  52. data/deps/libuv/src/unix/loop.c +114 -0
  53. data/deps/libuv/src/unix/netbsd.c +353 -0
  54. data/deps/libuv/src/unix/openbsd.c +304 -0
  55. data/deps/libuv/src/unix/pipe.c +261 -0
  56. data/deps/libuv/src/unix/poll.c +108 -0
  57. data/deps/libuv/src/unix/process.c +501 -0
  58. data/deps/libuv/src/unix/proctitle.c +103 -0
  59. data/deps/libuv/src/unix/signal.c +455 -0
  60. data/deps/libuv/src/unix/stream.c +1380 -0
  61. data/deps/libuv/src/unix/sunos.c +647 -0
  62. data/deps/libuv/src/unix/tcp.c +357 -0
  63. data/deps/libuv/src/unix/thread.c +431 -0
  64. data/deps/libuv/src/unix/threadpool.c +286 -0
  65. data/deps/libuv/src/unix/timer.c +153 -0
  66. data/deps/libuv/src/unix/tty.c +179 -0
  67. data/deps/libuv/src/unix/udp.c +715 -0
  68. data/deps/libuv/src/uv-common.c +431 -0
  69. data/deps/libuv/src/uv-common.h +204 -0
  70. data/deps/libuv/src/version.c +60 -0
  71. data/deps/libuv/src/win/async.c +99 -0
  72. data/deps/libuv/src/win/atomicops-inl.h +56 -0
  73. data/deps/libuv/src/win/core.c +310 -0
  74. data/deps/libuv/src/win/dl.c +86 -0
  75. data/deps/libuv/src/win/error.c +164 -0
  76. data/deps/libuv/src/win/fs-event.c +506 -0
  77. data/deps/libuv/src/win/fs.c +1951 -0
  78. data/deps/libuv/src/win/getaddrinfo.c +365 -0
  79. data/deps/libuv/src/win/handle-inl.h +164 -0
  80. data/deps/libuv/src/win/handle.c +153 -0
  81. data/deps/libuv/src/win/internal.h +346 -0
  82. data/deps/libuv/src/win/loop-watcher.c +124 -0
  83. data/deps/libuv/src/win/pipe.c +1656 -0
  84. data/deps/libuv/src/win/poll.c +615 -0
  85. data/deps/libuv/src/win/process-stdio.c +503 -0
  86. data/deps/libuv/src/win/process.c +1048 -0
  87. data/deps/libuv/src/win/req-inl.h +224 -0
  88. data/deps/libuv/src/win/req.c +25 -0
  89. data/deps/libuv/src/win/signal.c +354 -0
  90. data/deps/libuv/src/win/stream-inl.h +67 -0
  91. data/deps/libuv/src/win/stream.c +198 -0
  92. data/deps/libuv/src/win/tcp.c +1422 -0
  93. data/deps/libuv/src/win/thread.c +666 -0
  94. data/deps/libuv/src/win/threadpool.c +82 -0
  95. data/deps/libuv/src/win/timer.c +230 -0
  96. data/deps/libuv/src/win/tty.c +1857 -0
  97. data/deps/libuv/src/win/udp.c +744 -0
  98. data/deps/libuv/src/win/util.c +946 -0
  99. data/deps/libuv/src/win/winapi.c +152 -0
  100. data/deps/libuv/src/win/winapi.h +4476 -0
  101. data/deps/libuv/src/win/winsock.c +560 -0
  102. data/deps/libuv/src/win/winsock.h +171 -0
  103. data/deps/libuv/test/benchmark-async-pummel.c +119 -0
  104. data/deps/libuv/test/benchmark-async.c +139 -0
  105. data/deps/libuv/test/benchmark-fs-stat.c +136 -0
  106. data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
  107. data/deps/libuv/test/benchmark-list.h +163 -0
  108. data/deps/libuv/test/benchmark-loop-count.c +90 -0
  109. data/deps/libuv/test/benchmark-million-async.c +112 -0
  110. data/deps/libuv/test/benchmark-million-timers.c +77 -0
  111. data/deps/libuv/test/benchmark-multi-accept.c +432 -0
  112. data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
  113. data/deps/libuv/test/benchmark-pound.c +325 -0
  114. data/deps/libuv/test/benchmark-pump.c +459 -0
  115. data/deps/libuv/test/benchmark-sizes.c +45 -0
  116. data/deps/libuv/test/benchmark-spawn.c +163 -0
  117. data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
  118. data/deps/libuv/test/benchmark-thread.c +64 -0
  119. data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
  120. data/deps/libuv/test/blackhole-server.c +118 -0
  121. data/deps/libuv/test/dns-server.c +329 -0
  122. data/deps/libuv/test/echo-server.c +384 -0
  123. data/deps/libuv/test/fixtures/empty_file +0 -0
  124. data/deps/libuv/test/fixtures/load_error.node +1 -0
  125. data/deps/libuv/test/run-benchmarks.c +64 -0
  126. data/deps/libuv/test/run-tests.c +159 -0
  127. data/deps/libuv/test/runner-unix.c +328 -0
  128. data/deps/libuv/test/runner-unix.h +36 -0
  129. data/deps/libuv/test/runner-win.c +318 -0
  130. data/deps/libuv/test/runner-win.h +43 -0
  131. data/deps/libuv/test/runner.c +394 -0
  132. data/deps/libuv/test/runner.h +165 -0
  133. data/deps/libuv/test/task.h +122 -0
  134. data/deps/libuv/test/test-active.c +83 -0
  135. data/deps/libuv/test/test-async.c +136 -0
  136. data/deps/libuv/test/test-barrier.c +98 -0
  137. data/deps/libuv/test/test-callback-order.c +77 -0
  138. data/deps/libuv/test/test-callback-stack.c +204 -0
  139. data/deps/libuv/test/test-condvar.c +173 -0
  140. data/deps/libuv/test/test-connection-fail.c +150 -0
  141. data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
  142. data/deps/libuv/test/test-delayed-accept.c +189 -0
  143. data/deps/libuv/test/test-dlerror.c +58 -0
  144. data/deps/libuv/test/test-embed.c +136 -0
  145. data/deps/libuv/test/test-error.c +59 -0
  146. data/deps/libuv/test/test-fail-always.c +29 -0
  147. data/deps/libuv/test/test-fs-event.c +504 -0
  148. data/deps/libuv/test/test-fs-poll.c +148 -0
  149. data/deps/libuv/test/test-fs.c +1899 -0
  150. data/deps/libuv/test/test-get-currentexe.c +63 -0
  151. data/deps/libuv/test/test-get-loadavg.c +36 -0
  152. data/deps/libuv/test/test-get-memory.c +38 -0
  153. data/deps/libuv/test/test-getaddrinfo.c +120 -0
  154. data/deps/libuv/test/test-getsockname.c +344 -0
  155. data/deps/libuv/test/test-hrtime.c +54 -0
  156. data/deps/libuv/test/test-idle.c +82 -0
  157. data/deps/libuv/test/test-ipc-send-recv.c +218 -0
  158. data/deps/libuv/test/test-ipc.c +625 -0
  159. data/deps/libuv/test/test-list.h +492 -0
  160. data/deps/libuv/test/test-loop-handles.c +337 -0
  161. data/deps/libuv/test/test-loop-stop.c +73 -0
  162. data/deps/libuv/test/test-multiple-listen.c +103 -0
  163. data/deps/libuv/test/test-mutexes.c +63 -0
  164. data/deps/libuv/test/test-pass-always.c +28 -0
  165. data/deps/libuv/test/test-ping-pong.c +250 -0
  166. data/deps/libuv/test/test-pipe-bind-error.c +144 -0
  167. data/deps/libuv/test/test-pipe-connect-error.c +98 -0
  168. data/deps/libuv/test/test-platform-output.c +87 -0
  169. data/deps/libuv/test/test-poll-close.c +73 -0
  170. data/deps/libuv/test/test-poll.c +575 -0
  171. data/deps/libuv/test/test-process-title.c +49 -0
  172. data/deps/libuv/test/test-ref.c +415 -0
  173. data/deps/libuv/test/test-run-nowait.c +46 -0
  174. data/deps/libuv/test/test-run-once.c +49 -0
  175. data/deps/libuv/test/test-semaphore.c +111 -0
  176. data/deps/libuv/test/test-shutdown-close.c +105 -0
  177. data/deps/libuv/test/test-shutdown-eof.c +184 -0
  178. data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
  179. data/deps/libuv/test/test-signal.c +152 -0
  180. data/deps/libuv/test/test-spawn.c +938 -0
  181. data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
  182. data/deps/libuv/test/test-tcp-bind-error.c +198 -0
  183. data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
  184. data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
  185. data/deps/libuv/test/test-tcp-close.c +130 -0
  186. data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
  187. data/deps/libuv/test/test-tcp-connect-error.c +71 -0
  188. data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
  189. data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
  190. data/deps/libuv/test/test-tcp-flags.c +52 -0
  191. data/deps/libuv/test/test-tcp-open.c +175 -0
  192. data/deps/libuv/test/test-tcp-read-stop.c +73 -0
  193. data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
  194. data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
  195. data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
  196. data/deps/libuv/test/test-tcp-writealot.c +171 -0
  197. data/deps/libuv/test/test-thread.c +183 -0
  198. data/deps/libuv/test/test-threadpool-cancel.c +311 -0
  199. data/deps/libuv/test/test-threadpool.c +77 -0
  200. data/deps/libuv/test/test-timer-again.c +142 -0
  201. data/deps/libuv/test/test-timer.c +266 -0
  202. data/deps/libuv/test/test-tty.c +111 -0
  203. data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
  204. data/deps/libuv/test/test-udp-ipv6.c +158 -0
  205. data/deps/libuv/test/test-udp-multicast-join.c +140 -0
  206. data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
  207. data/deps/libuv/test/test-udp-open.c +154 -0
  208. data/deps/libuv/test/test-udp-options.c +87 -0
  209. data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
  210. data/deps/libuv/test/test-util.c +97 -0
  211. data/deps/libuv/test/test-walk-handles.c +78 -0
  212. data/deps/libuv/uv.gyp +431 -0
  213. data/deps/libuv/vcbuild.bat +128 -0
  214. data/ext/rbuv/debug.h +27 -0
  215. data/ext/rbuv/error.c +7 -0
  216. data/ext/rbuv/error.h +10 -0
  217. data/ext/rbuv/extconf.rb +35 -0
  218. data/ext/rbuv/handle.c +40 -0
  219. data/ext/rbuv/handle.h +14 -0
  220. data/ext/rbuv/libuv.mk +12 -0
  221. data/ext/rbuv/loop.c +50 -0
  222. data/ext/rbuv/loop.h +13 -0
  223. data/ext/rbuv/rbuv.c +15 -0
  224. data/ext/rbuv/rbuv.h +27 -0
  225. data/ext/rbuv/timer.c +133 -0
  226. data/ext/rbuv/timer.h +13 -0
  227. data/lib/rbuv/timer.rb +7 -0
  228. data/lib/rbuv/version.rb +1 -1
  229. data/lib/rbuv.rb +24 -2
  230. data/rbuv.gemspec +5 -1
  231. data/spec/spec_helper.rb +22 -0
  232. data/spec/timer_spec.rb +144 -0
  233. metadata +278 -9
@@ -0,0 +1,744 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
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:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
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
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ #include <assert.h>
23
+
24
+ #include "uv.h"
25
+ #include "internal.h"
26
+ #include "handle-inl.h"
27
+ #include "stream-inl.h"
28
+ #include "req-inl.h"
29
+
30
+
31
+ /*
32
+ * Threshold of active udp streams for which to preallocate udp read buffers.
33
+ */
34
+ const unsigned int uv_active_udp_streams_threshold = 0;
35
+
36
+ /* A zero-size buffer for use by uv_udp_read */
37
+ static char uv_zero_[] = "";
38
+
39
+ int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
40
+ int* namelen) {
41
+ uv_loop_t* loop = handle->loop;
42
+ int result;
43
+
44
+ if (!(handle->flags & UV_HANDLE_BOUND)) {
45
+ uv__set_sys_error(loop, WSAEINVAL);
46
+ return -1;
47
+ }
48
+
49
+ result = getsockname(handle->socket, name, namelen);
50
+ if (result != 0) {
51
+ uv__set_sys_error(loop, WSAGetLastError());
52
+ return -1;
53
+ }
54
+
55
+ return 0;
56
+ }
57
+
58
+
59
+ static int uv_udp_set_socket(uv_loop_t* loop, uv_udp_t* handle, SOCKET socket,
60
+ int family) {
61
+ DWORD yes = 1;
62
+ WSAPROTOCOL_INFOW info;
63
+ int opt_len;
64
+
65
+ assert(handle->socket == INVALID_SOCKET);
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
+
77
+ /* Set the socket to nonblocking mode */
78
+ if (ioctlsocket(socket, FIONBIO, &yes) == SOCKET_ERROR) {
79
+ uv__set_sys_error(loop, WSAGetLastError());
80
+ return -1;
81
+ }
82
+
83
+ /* Make the socket non-inheritable */
84
+ if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) {
85
+ uv__set_sys_error(loop, GetLastError());
86
+ return -1;
87
+ }
88
+
89
+ /* Associate it with the I/O completion port. */
90
+ /* Use uv_handle_t pointer as completion key. */
91
+ if (CreateIoCompletionPort((HANDLE)socket,
92
+ loop->iocp,
93
+ (ULONG_PTR)socket,
94
+ 0) == NULL) {
95
+ uv__set_sys_error(loop, GetLastError());
96
+ return -1;
97
+ }
98
+
99
+ if (pSetFileCompletionNotificationModes) {
100
+ /* All know windowses that support SetFileCompletionNotificationModes */
101
+ /* have a bug that makes it impossible to use this function in */
102
+ /* conjunction with datagram sockets. We can work around that but only */
103
+ /* if the user is using the default UDP driver (AFD) and has no other */
104
+ /* LSPs stacked on top. Here we check whether that is the case. */
105
+ opt_len = (int) sizeof info;
106
+ if (getsockopt(socket,
107
+ SOL_SOCKET,
108
+ SO_PROTOCOL_INFOW,
109
+ (char*) &info,
110
+ &opt_len) == SOCKET_ERROR) {
111
+ uv__set_sys_error(loop, GetLastError());
112
+ return -1;
113
+ }
114
+
115
+ if (info.ProtocolChain.ChainLen == 1) {
116
+ if (pSetFileCompletionNotificationModes((HANDLE)socket,
117
+ FILE_SKIP_SET_EVENT_ON_HANDLE |
118
+ FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)) {
119
+ handle->flags |= UV_HANDLE_SYNC_BYPASS_IOCP;
120
+ handle->func_wsarecv = uv_wsarecv_workaround;
121
+ handle->func_wsarecvfrom = uv_wsarecvfrom_workaround;
122
+ } else if (GetLastError() != ERROR_INVALID_FUNCTION) {
123
+ uv__set_sys_error(loop, GetLastError());
124
+ return -1;
125
+ }
126
+ }
127
+ }
128
+
129
+ handle->socket = socket;
130
+
131
+ if (family == AF_INET6) {
132
+ handle->flags |= UV_HANDLE_IPV6;
133
+ } else {
134
+ assert(!(handle->flags & UV_HANDLE_IPV6));
135
+ }
136
+
137
+ return 0;
138
+ }
139
+
140
+
141
+ int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
142
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_UDP);
143
+
144
+ handle->socket = INVALID_SOCKET;
145
+ handle->reqs_pending = 0;
146
+ handle->activecnt = 0;
147
+ handle->func_wsarecv = WSARecv;
148
+ handle->func_wsarecvfrom = WSARecvFrom;
149
+
150
+ uv_req_init(loop, (uv_req_t*) &(handle->recv_req));
151
+ handle->recv_req.type = UV_UDP_RECV;
152
+ handle->recv_req.data = handle;
153
+
154
+ return 0;
155
+ }
156
+
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
+
170
+ void uv_udp_endgame(uv_loop_t* loop, uv_udp_t* handle) {
171
+ if (handle->flags & UV__HANDLE_CLOSING &&
172
+ handle->reqs_pending == 0) {
173
+ assert(!(handle->flags & UV_HANDLE_CLOSED));
174
+ uv__handle_close(handle);
175
+ }
176
+ }
177
+
178
+
179
+ static int uv__bind(uv_udp_t* handle,
180
+ int family,
181
+ struct sockaddr* addr,
182
+ int addrsize,
183
+ unsigned int flags) {
184
+ int r;
185
+ DWORD no = 0, yes = 1;
186
+
187
+ if ((flags & UV_UDP_IPV6ONLY) && family != AF_INET6) {
188
+ /* UV_UDP_IPV6ONLY is supported only for IPV6 sockets */
189
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
190
+ return -1;
191
+ }
192
+
193
+ if (handle->socket == INVALID_SOCKET) {
194
+ SOCKET sock = socket(family, SOCK_DGRAM, 0);
195
+ if (sock == INVALID_SOCKET) {
196
+ uv__set_sys_error(handle->loop, WSAGetLastError());
197
+ return -1;
198
+ }
199
+
200
+ if (uv_udp_set_socket(handle->loop, handle, sock, family) < 0) {
201
+ closesocket(sock);
202
+ return -1;
203
+ }
204
+
205
+ if (family == AF_INET6)
206
+ handle->flags |= UV_HANDLE_IPV6;
207
+ }
208
+
209
+ if (family == AF_INET6 && !(flags & UV_UDP_IPV6ONLY)) {
210
+ /* On windows IPV6ONLY is on by default. */
211
+ /* If the user doesn't specify it libuv turns it off. */
212
+
213
+ /* TODO: how to handle errors? This may fail if there is no ipv4 stack */
214
+ /* available, or when run on XP/2003 which have no support for dualstack */
215
+ /* sockets. For now we're silently ignoring the error. */
216
+ setsockopt(handle->socket,
217
+ IPPROTO_IPV6,
218
+ IPV6_V6ONLY,
219
+ (char*) &no,
220
+ sizeof no);
221
+ }
222
+
223
+ r = bind(handle->socket, addr, addrsize);
224
+ if (r == SOCKET_ERROR) {
225
+ uv__set_sys_error(handle->loop, WSAGetLastError());
226
+ return -1;
227
+ }
228
+
229
+ handle->flags |= UV_HANDLE_BOUND;
230
+
231
+ return 0;
232
+ }
233
+
234
+
235
+ int uv__udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
236
+ unsigned int flags) {
237
+ return uv__bind(handle,
238
+ AF_INET,
239
+ (struct sockaddr*) &addr,
240
+ sizeof(struct sockaddr_in),
241
+ flags);
242
+ }
243
+
244
+
245
+ int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
246
+ unsigned int flags) {
247
+ return uv__bind(handle,
248
+ AF_INET6,
249
+ (struct sockaddr*) &addr,
250
+ sizeof(struct sockaddr_in6),
251
+ flags);
252
+ }
253
+
254
+
255
+ static void uv_udp_queue_recv(uv_loop_t* loop, uv_udp_t* handle) {
256
+ uv_req_t* req;
257
+ uv_buf_t buf;
258
+ DWORD bytes, flags;
259
+ int result;
260
+
261
+ assert(handle->flags & UV_HANDLE_READING);
262
+ assert(!(handle->flags & UV_HANDLE_READ_PENDING));
263
+
264
+ req = &handle->recv_req;
265
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
266
+
267
+ /*
268
+ * Preallocate a read buffer if the number of active streams is below
269
+ * the threshold.
270
+ */
271
+ if (loop->active_udp_streams < uv_active_udp_streams_threshold) {
272
+ handle->flags &= ~UV_HANDLE_ZERO_READ;
273
+
274
+ handle->recv_buffer = handle->alloc_cb((uv_handle_t*) handle, 65536);
275
+ assert(handle->recv_buffer.len > 0);
276
+
277
+ buf = handle->recv_buffer;
278
+ memset(&handle->recv_from, 0, sizeof handle->recv_from);
279
+ handle->recv_from_len = sizeof handle->recv_from;
280
+ flags = 0;
281
+
282
+ result = handle->func_wsarecvfrom(handle->socket,
283
+ (WSABUF*) &buf,
284
+ 1,
285
+ &bytes,
286
+ &flags,
287
+ (struct sockaddr*) &handle->recv_from,
288
+ &handle->recv_from_len,
289
+ &req->overlapped,
290
+ NULL);
291
+
292
+ if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
293
+ /* Process the req without IOCP. */
294
+ handle->flags |= UV_HANDLE_READ_PENDING;
295
+ req->overlapped.InternalHigh = bytes;
296
+ handle->reqs_pending++;
297
+ uv_insert_pending_req(loop, req);
298
+ } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
299
+ /* The req will be processed with IOCP. */
300
+ handle->flags |= UV_HANDLE_READ_PENDING;
301
+ handle->reqs_pending++;
302
+ } else {
303
+ /* Make this req pending reporting an error. */
304
+ SET_REQ_ERROR(req, WSAGetLastError());
305
+ uv_insert_pending_req(loop, req);
306
+ handle->reqs_pending++;
307
+ }
308
+
309
+ } else {
310
+ handle->flags |= UV_HANDLE_ZERO_READ;
311
+
312
+ buf.base = (char*) uv_zero_;
313
+ buf.len = 0;
314
+ flags = MSG_PEEK;
315
+
316
+ result = handle->func_wsarecv(handle->socket,
317
+ (WSABUF*) &buf,
318
+ 1,
319
+ &bytes,
320
+ &flags,
321
+ &req->overlapped,
322
+ NULL);
323
+
324
+ if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
325
+ /* Process the req without IOCP. */
326
+ handle->flags |= UV_HANDLE_READ_PENDING;
327
+ req->overlapped.InternalHigh = bytes;
328
+ handle->reqs_pending++;
329
+ uv_insert_pending_req(loop, req);
330
+ } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
331
+ /* The req will be processed with IOCP. */
332
+ handle->flags |= UV_HANDLE_READ_PENDING;
333
+ handle->reqs_pending++;
334
+ } else {
335
+ /* Make this req pending reporting an error. */
336
+ SET_REQ_ERROR(req, WSAGetLastError());
337
+ uv_insert_pending_req(loop, req);
338
+ handle->reqs_pending++;
339
+ }
340
+ }
341
+ }
342
+
343
+
344
+ int uv__udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
345
+ uv_udp_recv_cb recv_cb) {
346
+ uv_loop_t* loop = handle->loop;
347
+
348
+ if (handle->flags & UV_HANDLE_READING) {
349
+ uv__set_sys_error(loop, WSAEALREADY);
350
+ return -1;
351
+ }
352
+
353
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
354
+ uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) {
355
+ return -1;
356
+ }
357
+
358
+ handle->flags |= UV_HANDLE_READING;
359
+ INCREASE_ACTIVE_COUNT(loop, handle);
360
+ loop->active_udp_streams++;
361
+
362
+ handle->recv_cb = recv_cb;
363
+ handle->alloc_cb = alloc_cb;
364
+
365
+ /* If reading was stopped and then started again, there could still be a */
366
+ /* recv request pending. */
367
+ if (!(handle->flags & UV_HANDLE_READ_PENDING))
368
+ uv_udp_queue_recv(loop, handle);
369
+
370
+ return 0;
371
+ }
372
+
373
+
374
+ int uv__udp_recv_stop(uv_udp_t* handle) {
375
+ if (handle->flags & UV_HANDLE_READING) {
376
+ handle->flags &= ~UV_HANDLE_READING;
377
+ handle->loop->active_udp_streams--;
378
+ DECREASE_ACTIVE_COUNT(loop, handle);
379
+ }
380
+
381
+ return 0;
382
+ }
383
+
384
+
385
+ static int uv__send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
386
+ int bufcnt, struct sockaddr* addr, int addr_len, uv_udp_send_cb cb) {
387
+ uv_loop_t* loop = handle->loop;
388
+ DWORD result, bytes;
389
+
390
+ uv_req_init(loop, (uv_req_t*) req);
391
+ req->type = UV_UDP_SEND;
392
+ req->handle = handle;
393
+ req->cb = cb;
394
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
395
+
396
+ result = WSASendTo(handle->socket,
397
+ (WSABUF*)bufs,
398
+ bufcnt,
399
+ &bytes,
400
+ 0,
401
+ addr,
402
+ addr_len,
403
+ &req->overlapped,
404
+ NULL);
405
+
406
+ if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
407
+ /* Request completed immediately. */
408
+ req->queued_bytes = 0;
409
+ handle->reqs_pending++;
410
+ REGISTER_HANDLE_REQ(loop, handle, req);
411
+ uv_insert_pending_req(loop, (uv_req_t*)req);
412
+ } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
413
+ /* Request queued by the kernel. */
414
+ req->queued_bytes = uv_count_bufs(bufs, bufcnt);
415
+ handle->reqs_pending++;
416
+ REGISTER_HANDLE_REQ(loop, handle, req);
417
+ } else {
418
+ /* Send failed due to an error. */
419
+ uv__set_sys_error(loop, WSAGetLastError());
420
+ return -1;
421
+ }
422
+
423
+ return 0;
424
+ }
425
+
426
+
427
+ int uv__udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
428
+ int bufcnt, struct sockaddr_in addr, uv_udp_send_cb cb) {
429
+
430
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
431
+ uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) {
432
+ return -1;
433
+ }
434
+
435
+ return uv__send(req,
436
+ handle,
437
+ bufs,
438
+ bufcnt,
439
+ (struct sockaddr*) &addr,
440
+ sizeof addr,
441
+ cb);
442
+ }
443
+
444
+
445
+ int uv__udp_send6(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
446
+ int bufcnt, struct sockaddr_in6 addr, uv_udp_send_cb cb) {
447
+
448
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
449
+ uv_udp_bind6(handle, uv_addr_ip6_any_, 0) < 0) {
450
+ return -1;
451
+ }
452
+
453
+ return uv__send(req,
454
+ handle,
455
+ bufs,
456
+ bufcnt,
457
+ (struct sockaddr*) &addr,
458
+ sizeof addr,
459
+ cb);
460
+ }
461
+
462
+
463
+ void uv_process_udp_recv_req(uv_loop_t* loop, uv_udp_t* handle,
464
+ uv_req_t* req) {
465
+ uv_buf_t buf;
466
+ int partial;
467
+
468
+ assert(handle->type == UV_UDP);
469
+
470
+ handle->flags &= ~UV_HANDLE_READ_PENDING;
471
+
472
+ if (!REQ_SUCCESS(req)) {
473
+ DWORD err = GET_REQ_SOCK_ERROR(req);
474
+ if (err == WSAEMSGSIZE) {
475
+ /* Not a real error, it just indicates that the received packet */
476
+ /* was bigger than the receive buffer. */
477
+ } else if (err == WSAECONNRESET || err == WSAENETRESET) {
478
+ /* A previous sendto operation failed; ignore this error. If */
479
+ /* zero-reading we need to call WSARecv/WSARecvFrom _without_ the */
480
+ /* MSG_PEEK flag to clear out the error queue. For nonzero reads, */
481
+ /* immediately queue a new receive. */
482
+ if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
483
+ goto done;
484
+ }
485
+ } else {
486
+ /* A real error occurred. Report the error to the user only if we're */
487
+ /* currently reading. */
488
+ if (handle->flags & UV_HANDLE_READING) {
489
+ uv__set_sys_error(loop, err);
490
+ uv_udp_recv_stop(handle);
491
+ buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
492
+ uv_buf_init(NULL, 0) : handle->recv_buffer;
493
+ handle->recv_cb(handle, -1, buf, NULL, 0);
494
+ }
495
+ goto done;
496
+ }
497
+ }
498
+
499
+ if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
500
+ /* Successful read */
501
+ partial = !REQ_SUCCESS(req);
502
+ handle->recv_cb(handle,
503
+ req->overlapped.InternalHigh,
504
+ handle->recv_buffer,
505
+ (struct sockaddr*) &handle->recv_from,
506
+ partial ? UV_UDP_PARTIAL : 0);
507
+ } else if (handle->flags & UV_HANDLE_READING) {
508
+ DWORD bytes, err, flags;
509
+ struct sockaddr_storage from;
510
+ int from_len;
511
+
512
+ /* Do a nonblocking receive */
513
+ /* TODO: try to read multiple datagrams at once. FIONREAD maybe? */
514
+ buf = handle->alloc_cb((uv_handle_t*) handle, 65536);
515
+ assert(buf.len > 0);
516
+
517
+ memset(&from, 0, sizeof from);
518
+ from_len = sizeof from;
519
+
520
+ flags = 0;
521
+
522
+ if (WSARecvFrom(handle->socket,
523
+ (WSABUF*)&buf,
524
+ 1,
525
+ &bytes,
526
+ &flags,
527
+ (struct sockaddr*) &from,
528
+ &from_len,
529
+ NULL,
530
+ NULL) != SOCKET_ERROR) {
531
+
532
+ /* Message received */
533
+ handle->recv_cb(handle, bytes, buf, (struct sockaddr*) &from, 0);
534
+ } else {
535
+ err = WSAGetLastError();
536
+ if (err == WSAEMSGSIZE) {
537
+ /* Message truncated */
538
+ handle->recv_cb(handle,
539
+ bytes,
540
+ buf,
541
+ (struct sockaddr*) &from,
542
+ UV_UDP_PARTIAL);
543
+ } if (err == WSAEWOULDBLOCK) {
544
+ /* Kernel buffer empty */
545
+ uv__set_sys_error(loop, WSAEWOULDBLOCK);
546
+ handle->recv_cb(handle, 0, buf, NULL, 0);
547
+ } else if (err != WSAECONNRESET && err != WSAENETRESET) {
548
+ /* Serious error. WSAECONNRESET/WSANETRESET is ignored because this */
549
+ /* just indicates that a previous sendto operation failed. */
550
+ uv_udp_recv_stop(handle);
551
+ uv__set_sys_error(loop, err);
552
+ handle->recv_cb(handle, -1, buf, NULL, 0);
553
+ }
554
+ }
555
+ }
556
+
557
+ done:
558
+ /* Post another read if still reading and not closing. */
559
+ if ((handle->flags & UV_HANDLE_READING) &&
560
+ !(handle->flags & UV_HANDLE_READ_PENDING)) {
561
+ uv_udp_queue_recv(loop, handle);
562
+ }
563
+
564
+ DECREASE_PENDING_REQ_COUNT(handle);
565
+ }
566
+
567
+
568
+ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle,
569
+ uv_udp_send_t* req) {
570
+ assert(handle->type == UV_UDP);
571
+
572
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
573
+
574
+ if (req->cb) {
575
+ if (REQ_SUCCESS(req)) {
576
+ req->cb(req, 0);
577
+ } else {
578
+ uv__set_sys_error(loop, GET_REQ_SOCK_ERROR(req));
579
+ req->cb(req, -1);
580
+ }
581
+ }
582
+
583
+ DECREASE_PENDING_REQ_COUNT(handle);
584
+ }
585
+
586
+
587
+ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
588
+ const char* interface_addr, uv_membership membership) {
589
+ int optname;
590
+ struct ip_mreq mreq;
591
+
592
+ /* If the socket is unbound, bind to inaddr_any. */
593
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
594
+ uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) {
595
+ return -1;
596
+ }
597
+
598
+ if (handle->flags & UV_HANDLE_IPV6) {
599
+ uv__set_artificial_error(handle->loop, UV_ENOSYS);
600
+ return -1;
601
+ }
602
+
603
+ memset(&mreq, 0, sizeof mreq);
604
+
605
+ if (interface_addr) {
606
+ mreq.imr_interface.s_addr = inet_addr(interface_addr);
607
+ } else {
608
+ mreq.imr_interface.s_addr = htonl(INADDR_ANY);
609
+ }
610
+
611
+ mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
612
+
613
+ switch (membership) {
614
+ case UV_JOIN_GROUP:
615
+ optname = IP_ADD_MEMBERSHIP;
616
+ break;
617
+ case UV_LEAVE_GROUP:
618
+ optname = IP_DROP_MEMBERSHIP;
619
+ break;
620
+ default:
621
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
622
+ }
623
+
624
+ if (setsockopt(handle->socket,
625
+ IPPROTO_IP,
626
+ optname,
627
+ (char*) &mreq,
628
+ sizeof mreq) == SOCKET_ERROR) {
629
+ uv__set_sys_error(handle->loop, WSAGetLastError());
630
+ return -1;
631
+ }
632
+
633
+ return 0;
634
+ }
635
+
636
+
637
+ int uv_udp_set_broadcast(uv_udp_t* handle, int value) {
638
+ BOOL optval = (BOOL) value;
639
+
640
+ /* If the socket is unbound, bind to inaddr_any. */
641
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
642
+ uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) {
643
+ return -1;
644
+ }
645
+
646
+ if (setsockopt(handle->socket,
647
+ SOL_SOCKET,
648
+ SO_BROADCAST,
649
+ (char*) &optval,
650
+ sizeof optval)) {
651
+ uv__set_sys_error(handle->loop, WSAGetLastError());
652
+ return -1;
653
+ }
654
+ return 0;
655
+ }
656
+
657
+
658
+ int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock) {
659
+ WSAPROTOCOL_INFOW protocol_info;
660
+ int opt_len;
661
+ DWORD yes = 1;
662
+
663
+ /* Detect the address family of the socket. */
664
+ opt_len = (int) sizeof protocol_info;
665
+ if (getsockopt(sock,
666
+ SOL_SOCKET,
667
+ SO_PROTOCOL_INFOW,
668
+ (char*) &protocol_info,
669
+ &opt_len) == SOCKET_ERROR) {
670
+ uv__set_sys_error(handle->loop, GetLastError());
671
+ return -1;
672
+ }
673
+
674
+ if (uv_udp_set_socket(handle->loop,
675
+ handle,
676
+ sock,
677
+ protocol_info.iAddressFamily) < 0) {
678
+ return -1;
679
+ }
680
+
681
+ return 0;
682
+ }
683
+
684
+
685
+ #define SOCKOPT_SETTER(name, option4, option6, validate) \
686
+ int uv_udp_set_##name(uv_udp_t* handle, int value) { \
687
+ DWORD optval = (DWORD) value; \
688
+ \
689
+ if (!(validate(value))) { \
690
+ uv__set_artificial_error(handle->loop, UV_EINVAL); \
691
+ return -1; \
692
+ } \
693
+ \
694
+ /* If the socket is unbound, bind to inaddr_any. */ \
695
+ if (!(handle->flags & UV_HANDLE_BOUND) && \
696
+ uv_udp_bind(handle, uv_addr_ip4_any_, 0) < 0) { \
697
+ return -1; \
698
+ } \
699
+ \
700
+ if (!(handle->flags & UV_HANDLE_IPV6)) { \
701
+ /* Set IPv4 socket option */ \
702
+ if (setsockopt(handle->socket, \
703
+ IPPROTO_IP, \
704
+ option4, \
705
+ (char*) &optval, \
706
+ sizeof optval)) { \
707
+ uv__set_sys_error(handle->loop, WSAGetLastError()); \
708
+ return -1; \
709
+ } \
710
+ } else { \
711
+ /* Set IPv6 socket option */ \
712
+ if (setsockopt(handle->socket, \
713
+ IPPROTO_IPV6, \
714
+ option6, \
715
+ (char*) &optval, \
716
+ sizeof optval)) { \
717
+ uv__set_sys_error(handle->loop, WSAGetLastError()); \
718
+ return -1; \
719
+ } \
720
+ } \
721
+ return 0; \
722
+ }
723
+
724
+ #define VALIDATE_TTL(value) ((value) >= 1 && (value) <= 255)
725
+ #define VALIDATE_MULTICAST_TTL(value) ((value) >= -1 && (value) <= 255)
726
+ #define VALIDATE_MULTICAST_LOOP(value) (1)
727
+
728
+ SOCKOPT_SETTER(ttl,
729
+ IP_TTL,
730
+ IPV6_HOPLIMIT,
731
+ VALIDATE_TTL)
732
+ SOCKOPT_SETTER(multicast_ttl,
733
+ IP_MULTICAST_TTL,
734
+ IPV6_MULTICAST_HOPS,
735
+ VALIDATE_MULTICAST_TTL)
736
+ SOCKOPT_SETTER(multicast_loop,
737
+ IP_MULTICAST_LOOP,
738
+ IPV6_MULTICAST_LOOP,
739
+ VALIDATE_MULTICAST_LOOP)
740
+
741
+ #undef SOCKOPT_SETTER
742
+ #undef VALIDATE_TTL
743
+ #undef VALIDATE_MULTICAST_TTL
744
+ #undef VALIDATE_MULTICAST_LOOP