racer 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (336) hide show
  1. data/LICENSE +22 -0
  2. data/bin/racer +33 -0
  3. data/ext/libuv/AUTHORS +45 -0
  4. data/ext/libuv/LICENSE +42 -0
  5. data/ext/libuv/Makefile +119 -0
  6. data/ext/libuv/README.md +88 -0
  7. data/ext/libuv/build/gcc_version.py +20 -0
  8. data/ext/libuv/common.gypi +176 -0
  9. data/ext/libuv/config-mingw.mk +61 -0
  10. data/ext/libuv/config-unix.mk +171 -0
  11. data/ext/libuv/gyp_uv +64 -0
  12. data/ext/libuv/include/ares.h +591 -0
  13. data/ext/libuv/include/ares_version.h +24 -0
  14. data/ext/libuv/include/uv-private/eio.h +403 -0
  15. data/ext/libuv/include/uv-private/ev.h +838 -0
  16. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  17. data/ext/libuv/include/uv-private/tree.h +768 -0
  18. data/ext/libuv/include/uv-private/uv-unix.h +310 -0
  19. data/ext/libuv/include/uv-private/uv-win.h +510 -0
  20. data/ext/libuv/include/uv.h +1796 -0
  21. data/ext/libuv/src/ares/AUTHORS +37 -0
  22. data/ext/libuv/src/ares/CHANGES +1218 -0
  23. data/ext/libuv/src/ares/CMakeLists.txt +22 -0
  24. data/ext/libuv/src/ares/NEWS +21 -0
  25. data/ext/libuv/src/ares/README +60 -0
  26. data/ext/libuv/src/ares/README.cares +13 -0
  27. data/ext/libuv/src/ares/README.msvc +142 -0
  28. data/ext/libuv/src/ares/README.node +21 -0
  29. data/ext/libuv/src/ares/RELEASE-NOTES +26 -0
  30. data/ext/libuv/src/ares/TODO +23 -0
  31. data/ext/libuv/src/ares/ares__close_sockets.c +66 -0
  32. data/ext/libuv/src/ares/ares__get_hostent.c +263 -0
  33. data/ext/libuv/src/ares/ares__read_line.c +71 -0
  34. data/ext/libuv/src/ares/ares__timeval.c +111 -0
  35. data/ext/libuv/src/ares/ares_cancel.c +63 -0
  36. data/ext/libuv/src/ares/ares_data.c +190 -0
  37. data/ext/libuv/src/ares/ares_data.h +65 -0
  38. data/ext/libuv/src/ares/ares_destroy.c +105 -0
  39. data/ext/libuv/src/ares/ares_dns.h +90 -0
  40. data/ext/libuv/src/ares/ares_expand_name.c +200 -0
  41. data/ext/libuv/src/ares/ares_expand_string.c +75 -0
  42. data/ext/libuv/src/ares/ares_fds.c +63 -0
  43. data/ext/libuv/src/ares/ares_free_hostent.c +42 -0
  44. data/ext/libuv/src/ares/ares_free_string.c +25 -0
  45. data/ext/libuv/src/ares/ares_getenv.c +30 -0
  46. data/ext/libuv/src/ares/ares_getenv.h +26 -0
  47. data/ext/libuv/src/ares/ares_gethostbyaddr.c +301 -0
  48. data/ext/libuv/src/ares/ares_gethostbyname.c +523 -0
  49. data/ext/libuv/src/ares/ares_getnameinfo.c +427 -0
  50. data/ext/libuv/src/ares/ares_getopt.c +122 -0
  51. data/ext/libuv/src/ares/ares_getopt.h +53 -0
  52. data/ext/libuv/src/ares/ares_getsock.c +72 -0
  53. data/ext/libuv/src/ares/ares_init.c +1809 -0
  54. data/ext/libuv/src/ares/ares_iphlpapi.h +221 -0
  55. data/ext/libuv/src/ares/ares_ipv6.h +78 -0
  56. data/ext/libuv/src/ares/ares_library_init.c +142 -0
  57. data/ext/libuv/src/ares/ares_library_init.h +42 -0
  58. data/ext/libuv/src/ares/ares_llist.c +86 -0
  59. data/ext/libuv/src/ares/ares_llist.h +42 -0
  60. data/ext/libuv/src/ares/ares_mkquery.c +195 -0
  61. data/ext/libuv/src/ares/ares_nowarn.c +181 -0
  62. data/ext/libuv/src/ares/ares_nowarn.h +55 -0
  63. data/ext/libuv/src/ares/ares_options.c +248 -0
  64. data/ext/libuv/src/ares/ares_parse_a_reply.c +263 -0
  65. data/ext/libuv/src/ares/ares_parse_aaaa_reply.c +259 -0
  66. data/ext/libuv/src/ares/ares_parse_mx_reply.c +170 -0
  67. data/ext/libuv/src/ares/ares_parse_ns_reply.c +182 -0
  68. data/ext/libuv/src/ares/ares_parse_ptr_reply.c +217 -0
  69. data/ext/libuv/src/ares/ares_parse_srv_reply.c +179 -0
  70. data/ext/libuv/src/ares/ares_parse_txt_reply.c +201 -0
  71. data/ext/libuv/src/ares/ares_platform.c +11035 -0
  72. data/ext/libuv/src/ares/ares_platform.h +43 -0
  73. data/ext/libuv/src/ares/ares_private.h +355 -0
  74. data/ext/libuv/src/ares/ares_process.c +1295 -0
  75. data/ext/libuv/src/ares/ares_query.c +183 -0
  76. data/ext/libuv/src/ares/ares_rules.h +144 -0
  77. data/ext/libuv/src/ares/ares_search.c +321 -0
  78. data/ext/libuv/src/ares/ares_send.c +134 -0
  79. data/ext/libuv/src/ares/ares_setup.h +199 -0
  80. data/ext/libuv/src/ares/ares_strcasecmp.c +66 -0
  81. data/ext/libuv/src/ares/ares_strcasecmp.h +30 -0
  82. data/ext/libuv/src/ares/ares_strdup.c +42 -0
  83. data/ext/libuv/src/ares/ares_strdup.h +26 -0
  84. data/ext/libuv/src/ares/ares_strerror.c +56 -0
  85. data/ext/libuv/src/ares/ares_timeout.c +80 -0
  86. data/ext/libuv/src/ares/ares_version.c +11 -0
  87. data/ext/libuv/src/ares/ares_writev.c +79 -0
  88. data/ext/libuv/src/ares/ares_writev.h +36 -0
  89. data/ext/libuv/src/ares/bitncmp.c +59 -0
  90. data/ext/libuv/src/ares/bitncmp.h +26 -0
  91. data/ext/libuv/src/ares/config_cygwin/ares_config.h +512 -0
  92. data/ext/libuv/src/ares/config_darwin/ares_config.h +512 -0
  93. data/ext/libuv/src/ares/config_freebsd/ares_config.h +512 -0
  94. data/ext/libuv/src/ares/config_linux/ares_config.h +512 -0
  95. data/ext/libuv/src/ares/config_netbsd/ares_config.h +512 -0
  96. data/ext/libuv/src/ares/config_openbsd/ares_config.h +512 -0
  97. data/ext/libuv/src/ares/config_sunos/ares_config.h +512 -0
  98. data/ext/libuv/src/ares/config_win32/ares_config.h +369 -0
  99. data/ext/libuv/src/ares/get_ver.awk +35 -0
  100. data/ext/libuv/src/ares/inet_net_pton.c +451 -0
  101. data/ext/libuv/src/ares/inet_net_pton.h +31 -0
  102. data/ext/libuv/src/ares/inet_ntop.c +208 -0
  103. data/ext/libuv/src/ares/inet_ntop.h +26 -0
  104. data/ext/libuv/src/ares/nameser.h +203 -0
  105. data/ext/libuv/src/ares/setup_once.h +504 -0
  106. data/ext/libuv/src/ares/windows_port.c +22 -0
  107. data/ext/libuv/src/cares.c +225 -0
  108. data/ext/libuv/src/fs-poll.c +237 -0
  109. data/ext/libuv/src/unix/async.c +119 -0
  110. data/ext/libuv/src/unix/core.c +647 -0
  111. data/ext/libuv/src/unix/cygwin.c +84 -0
  112. data/ext/libuv/src/unix/darwin.c +341 -0
  113. data/ext/libuv/src/unix/dl.c +83 -0
  114. data/ext/libuv/src/unix/eio/Changes +63 -0
  115. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  116. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  117. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  118. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  119. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  120. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  121. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  122. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  123. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  124. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  125. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  126. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  127. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  128. data/ext/libuv/src/unix/eio/demo.c +194 -0
  129. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  130. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  131. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  132. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  133. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  134. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  135. data/ext/libuv/src/unix/error.c +104 -0
  136. data/ext/libuv/src/unix/ev/Changes +388 -0
  137. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  138. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  139. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  140. data/ext/libuv/src/unix/ev/README +58 -0
  141. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  142. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  143. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  144. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  145. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  146. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  147. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  148. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  149. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  150. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  151. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  152. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  153. data/ext/libuv/src/unix/ev/configure +13037 -0
  154. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  155. data/ext/libuv/src/unix/ev/depcomp +630 -0
  156. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  157. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  158. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  159. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  160. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  161. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  162. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  163. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  164. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  165. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  166. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  167. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  168. data/ext/libuv/src/unix/ev/event.c +402 -0
  169. data/ext/libuv/src/unix/ev/event.h +170 -0
  170. data/ext/libuv/src/unix/ev/install-sh +294 -0
  171. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  172. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  173. data/ext/libuv/src/unix/ev/missing +336 -0
  174. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  175. data/ext/libuv/src/unix/freebsd.c +312 -0
  176. data/ext/libuv/src/unix/fs.c +699 -0
  177. data/ext/libuv/src/unix/internal.h +180 -0
  178. data/ext/libuv/src/unix/kqueue.c +122 -0
  179. data/ext/libuv/src/unix/linux/core.c +474 -0
  180. data/ext/libuv/src/unix/linux/inotify.c +209 -0
  181. data/ext/libuv/src/unix/linux/syscalls.c +268 -0
  182. data/ext/libuv/src/unix/linux/syscalls.h +93 -0
  183. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  184. data/ext/libuv/src/unix/loop.c +81 -0
  185. data/ext/libuv/src/unix/netbsd.c +108 -0
  186. data/ext/libuv/src/unix/openbsd.c +295 -0
  187. data/ext/libuv/src/unix/pipe.c +261 -0
  188. data/ext/libuv/src/unix/poll.c +116 -0
  189. data/ext/libuv/src/unix/process.c +447 -0
  190. data/ext/libuv/src/unix/stream.c +1026 -0
  191. data/ext/libuv/src/unix/sunos.c +483 -0
  192. data/ext/libuv/src/unix/tcp.c +328 -0
  193. data/ext/libuv/src/unix/thread.c +211 -0
  194. data/ext/libuv/src/unix/timer.c +138 -0
  195. data/ext/libuv/src/unix/tty.c +146 -0
  196. data/ext/libuv/src/unix/udp.c +661 -0
  197. data/ext/libuv/src/unix/uv-eio.c +107 -0
  198. data/ext/libuv/src/unix/uv-eio.h +13 -0
  199. data/ext/libuv/src/uv-common.c +384 -0
  200. data/ext/libuv/src/uv-common.h +171 -0
  201. data/ext/libuv/src/win/async.c +130 -0
  202. data/ext/libuv/src/win/core.c +283 -0
  203. data/ext/libuv/src/win/dl.c +86 -0
  204. data/ext/libuv/src/win/error.c +150 -0
  205. data/ext/libuv/src/win/fs-event.c +512 -0
  206. data/ext/libuv/src/win/fs.c +1856 -0
  207. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  208. data/ext/libuv/src/win/handle-inl.h +145 -0
  209. data/ext/libuv/src/win/handle.c +150 -0
  210. data/ext/libuv/src/win/internal.h +337 -0
  211. data/ext/libuv/src/win/loop-watcher.c +123 -0
  212. data/ext/libuv/src/win/pipe.c +1675 -0
  213. data/ext/libuv/src/win/poll.c +618 -0
  214. data/ext/libuv/src/win/process-stdio.c +479 -0
  215. data/ext/libuv/src/win/process.c +1013 -0
  216. data/ext/libuv/src/win/req-inl.h +225 -0
  217. data/ext/libuv/src/win/req.c +25 -0
  218. data/ext/libuv/src/win/stream-inl.h +69 -0
  219. data/ext/libuv/src/win/stream.c +167 -0
  220. data/ext/libuv/src/win/tcp.c +1400 -0
  221. data/ext/libuv/src/win/thread.c +372 -0
  222. data/ext/libuv/src/win/threadpool.c +74 -0
  223. data/ext/libuv/src/win/timer.c +226 -0
  224. data/ext/libuv/src/win/tty.c +1801 -0
  225. data/ext/libuv/src/win/udp.c +718 -0
  226. data/ext/libuv/src/win/util.c +864 -0
  227. data/ext/libuv/src/win/winapi.c +132 -0
  228. data/ext/libuv/src/win/winapi.h +4443 -0
  229. data/ext/libuv/src/win/winsock.c +557 -0
  230. data/ext/libuv/src/win/winsock.h +171 -0
  231. data/ext/libuv/test/benchmark-ares.c +117 -0
  232. data/ext/libuv/test/benchmark-fs-stat.c +164 -0
  233. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  234. data/ext/libuv/test/benchmark-list.h +114 -0
  235. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  236. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  237. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  238. data/ext/libuv/test/benchmark-pound.c +324 -0
  239. data/ext/libuv/test/benchmark-pump.c +462 -0
  240. data/ext/libuv/test/benchmark-sizes.c +42 -0
  241. data/ext/libuv/test/benchmark-spawn.c +162 -0
  242. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  243. data/ext/libuv/test/benchmark-thread.c +64 -0
  244. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  245. data/ext/libuv/test/blackhole-server.c +118 -0
  246. data/ext/libuv/test/dns-server.c +321 -0
  247. data/ext/libuv/test/echo-server.c +378 -0
  248. data/ext/libuv/test/fixtures/empty_file +0 -0
  249. data/ext/libuv/test/fixtures/load_error.node +1 -0
  250. data/ext/libuv/test/run-benchmarks.c +64 -0
  251. data/ext/libuv/test/run-tests.c +138 -0
  252. data/ext/libuv/test/runner-unix.c +290 -0
  253. data/ext/libuv/test/runner-unix.h +36 -0
  254. data/ext/libuv/test/runner-win.c +285 -0
  255. data/ext/libuv/test/runner-win.h +42 -0
  256. data/ext/libuv/test/runner.c +326 -0
  257. data/ext/libuv/test/runner.h +159 -0
  258. data/ext/libuv/test/task.h +109 -0
  259. data/ext/libuv/test/test-async.c +118 -0
  260. data/ext/libuv/test/test-callback-order.c +76 -0
  261. data/ext/libuv/test/test-callback-stack.c +203 -0
  262. data/ext/libuv/test/test-connection-fail.c +148 -0
  263. data/ext/libuv/test/test-counters-init.c +215 -0
  264. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  265. data/ext/libuv/test/test-delayed-accept.c +197 -0
  266. data/ext/libuv/test/test-dlerror.c +58 -0
  267. data/ext/libuv/test/test-error.c +59 -0
  268. data/ext/libuv/test/test-fail-always.c +29 -0
  269. data/ext/libuv/test/test-fs-event.c +442 -0
  270. data/ext/libuv/test/test-fs-poll.c +146 -0
  271. data/ext/libuv/test/test-fs.c +1843 -0
  272. data/ext/libuv/test/test-get-currentexe.c +63 -0
  273. data/ext/libuv/test/test-get-loadavg.c +36 -0
  274. data/ext/libuv/test/test-get-memory.c +38 -0
  275. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  276. data/ext/libuv/test/test-gethostbyname.c +189 -0
  277. data/ext/libuv/test/test-getsockname.c +342 -0
  278. data/ext/libuv/test/test-hrtime.c +54 -0
  279. data/ext/libuv/test/test-idle.c +81 -0
  280. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  281. data/ext/libuv/test/test-ipc.c +620 -0
  282. data/ext/libuv/test/test-list.h +421 -0
  283. data/ext/libuv/test/test-loop-handles.c +336 -0
  284. data/ext/libuv/test/test-multiple-listen.c +102 -0
  285. data/ext/libuv/test/test-mutexes.c +63 -0
  286. data/ext/libuv/test/test-pass-always.c +28 -0
  287. data/ext/libuv/test/test-ping-pong.c +253 -0
  288. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  289. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  290. data/ext/libuv/test/test-platform-output.c +87 -0
  291. data/ext/libuv/test/test-poll-close.c +77 -0
  292. data/ext/libuv/test/test-poll.c +573 -0
  293. data/ext/libuv/test/test-process-title.c +42 -0
  294. data/ext/libuv/test/test-ref.c +338 -0
  295. data/ext/libuv/test/test-run-once.c +48 -0
  296. data/ext/libuv/test/test-semaphore.c +111 -0
  297. data/ext/libuv/test/test-shutdown-close.c +103 -0
  298. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  299. data/ext/libuv/test/test-spawn.c +847 -0
  300. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  301. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  302. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  303. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  304. data/ext/libuv/test/test-tcp-close.c +129 -0
  305. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  306. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  307. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  308. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  309. data/ext/libuv/test/test-tcp-flags.c +51 -0
  310. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  311. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  312. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  313. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  314. data/ext/libuv/test/test-thread.c +183 -0
  315. data/ext/libuv/test/test-threadpool.c +57 -0
  316. data/ext/libuv/test/test-timer-again.c +141 -0
  317. data/ext/libuv/test/test-timer.c +152 -0
  318. data/ext/libuv/test/test-tty.c +110 -0
  319. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  320. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  321. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  322. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  323. data/ext/libuv/test/test-udp-options.c +86 -0
  324. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  325. data/ext/libuv/test/test-util.c +97 -0
  326. data/ext/libuv/test/test-walk-handles.c +77 -0
  327. data/ext/libuv/uv.gyp +452 -0
  328. data/ext/libuv/vcbuild.bat +105 -0
  329. data/ext/racer/extconf.rb +20 -0
  330. data/ext/racer/http_parser.c +2059 -0
  331. data/ext/racer/http_parser.h +318 -0
  332. data/ext/racer/mkmf.log +22 -0
  333. data/ext/racer/racer.c +551 -0
  334. data/lib/racer.rb +2 -0
  335. data/lib/racer/version.rb +3 -0
  336. metadata +382 -0
@@ -0,0 +1,138 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
3
+ * of this software and associated documentation files (the "Software"), to
4
+ * deal in the Software without restriction, including without limitation the
5
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
+ * sell copies of the Software, and to permit persons to whom the Software is
7
+ * furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in
10
+ * all copies or substantial portions of the Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18
+ * IN THE SOFTWARE.
19
+ */
20
+
21
+ #include "uv.h"
22
+ #include "internal.h"
23
+ #include <assert.h>
24
+
25
+ static int uv__timer_cmp(const uv_timer_t* a, const uv_timer_t* b) {
26
+ if (a->timeout < b->timeout)
27
+ return -1;
28
+ if (a->timeout > b->timeout)
29
+ return 1;
30
+ if (a < b)
31
+ return -1;
32
+ if (a > b)
33
+ return 1;
34
+ return 0;
35
+ }
36
+
37
+
38
+ RB_GENERATE_STATIC(uv__timers, uv_timer_s, tree_entry, uv__timer_cmp)
39
+
40
+
41
+ int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
42
+ loop->counters.timer_init++;
43
+
44
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
45
+ handle->timer_cb = NULL;
46
+
47
+ return 0;
48
+ }
49
+
50
+
51
+ int uv_timer_start(uv_timer_t* handle,
52
+ uv_timer_cb cb,
53
+ int64_t timeout,
54
+ int64_t repeat) {
55
+ assert(timeout >= 0);
56
+ assert(repeat >= 0);
57
+
58
+ if (uv__is_active(handle))
59
+ uv_timer_stop(handle);
60
+
61
+ handle->timer_cb = cb;
62
+ handle->timeout = handle->loop->time + timeout;
63
+ handle->repeat = repeat;
64
+
65
+ RB_INSERT(uv__timers, &handle->loop->timer_handles, handle);
66
+ uv__handle_start(handle);
67
+
68
+ return 0;
69
+ }
70
+
71
+
72
+ int uv_timer_stop(uv_timer_t* handle) {
73
+ if (!uv__is_active(handle))
74
+ return 0;
75
+
76
+ RB_REMOVE(uv__timers, &handle->loop->timer_handles, handle);
77
+ uv__handle_stop(handle);
78
+
79
+ return 0;
80
+ }
81
+
82
+
83
+ int uv_timer_again(uv_timer_t* handle) {
84
+ if (handle->timer_cb == NULL)
85
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
86
+
87
+ if (handle->repeat) {
88
+ uv_timer_stop(handle);
89
+ uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
90
+ }
91
+
92
+ return 0;
93
+ }
94
+
95
+
96
+ void uv_timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
97
+ assert(repeat >= 0);
98
+ handle->repeat = repeat;
99
+ }
100
+
101
+
102
+ int64_t uv_timer_get_repeat(uv_timer_t* handle) {
103
+ return handle->repeat;
104
+ }
105
+
106
+
107
+ unsigned int uv__next_timeout(uv_loop_t* loop) {
108
+ uv_timer_t* handle;
109
+
110
+ handle = RB_MIN(uv__timers, &loop->timer_handles);
111
+
112
+ if (handle == NULL)
113
+ return (unsigned int) -1; /* block indefinitely */
114
+
115
+ if (handle->timeout <= loop->time)
116
+ return 0;
117
+
118
+ return handle->timeout - loop->time;
119
+ }
120
+
121
+
122
+ void uv__run_timers(uv_loop_t* loop) {
123
+ uv_timer_t* handle;
124
+
125
+ while ((handle = RB_MIN(uv__timers, &loop->timer_handles))) {
126
+ if (handle->timeout > loop->time)
127
+ break;
128
+
129
+ uv_timer_stop(handle);
130
+ uv_timer_again(handle);
131
+ handle->timer_cb(handle, 0);
132
+ }
133
+ }
134
+
135
+
136
+ void uv__timer_close(uv_timer_t* handle) {
137
+ uv_timer_stop(handle);
138
+ }
@@ -0,0 +1,146 @@
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 "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <assert.h>
26
+ #include <unistd.h>
27
+ #include <termios.h>
28
+ #include <errno.h>
29
+ #include <sys/ioctl.h>
30
+
31
+
32
+ static int orig_termios_fd = -1;
33
+ static struct termios orig_termios;
34
+
35
+
36
+ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, int fd, int readable) {
37
+ uv__stream_init(loop, (uv_stream_t*)tty, UV_TTY);
38
+
39
+ if (readable) {
40
+ uv__nonblock(fd, 1);
41
+ uv__stream_open((uv_stream_t*)tty, fd, UV_STREAM_READABLE);
42
+ } else {
43
+ /* Note: writable tty we set to blocking mode. */
44
+ uv__stream_open((uv_stream_t*)tty, fd, UV_STREAM_WRITABLE);
45
+ tty->flags |= UV_STREAM_BLOCKING;
46
+ }
47
+
48
+ loop->counters.tty_init++;
49
+ tty->mode = 0;
50
+ return 0;
51
+ }
52
+
53
+
54
+ int uv_tty_set_mode(uv_tty_t* tty, int mode) {
55
+ int fd = tty->fd;
56
+ struct termios raw;
57
+
58
+ if (mode && tty->mode == 0) {
59
+ /* on */
60
+
61
+ if (tcgetattr(fd, &tty->orig_termios)) {
62
+ goto fatal;
63
+ }
64
+
65
+ /* This is used for uv_tty_reset_mode() */
66
+ if (orig_termios_fd == -1) {
67
+ orig_termios = tty->orig_termios;
68
+ orig_termios_fd = fd;
69
+ }
70
+
71
+ raw = tty->orig_termios;
72
+ raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
73
+ raw.c_oflag |= (ONLCR);
74
+ raw.c_cflag |= (CS8);
75
+ raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
76
+ raw.c_cc[VMIN] = 1;
77
+ raw.c_cc[VTIME] = 0;
78
+
79
+ /* Put terminal in raw mode after draining */
80
+ if (tcsetattr(fd, TCSADRAIN, &raw)) {
81
+ goto fatal;
82
+ }
83
+
84
+ tty->mode = 1;
85
+ return 0;
86
+ } else if (mode == 0 && tty->mode) {
87
+ /* off */
88
+
89
+ /* Put terminal in original mode after flushing */
90
+ if (tcsetattr(fd, TCSAFLUSH, &tty->orig_termios)) {
91
+ goto fatal;
92
+ }
93
+
94
+ tty->mode = 0;
95
+ return 0;
96
+ }
97
+
98
+ fatal:
99
+ uv__set_sys_error(tty->loop, errno);
100
+ return -1;
101
+ }
102
+
103
+
104
+ int uv_tty_get_winsize(uv_tty_t* tty, int* width, int* height) {
105
+ struct winsize ws;
106
+
107
+ if (ioctl(tty->fd, TIOCGWINSZ, &ws) < 0) {
108
+ uv__set_sys_error(tty->loop, errno);
109
+ return -1;
110
+ }
111
+
112
+ *width = ws.ws_col;
113
+ *height = ws.ws_row;
114
+
115
+ return 0;
116
+ }
117
+
118
+
119
+ uv_handle_type uv_guess_handle(uv_file file) {
120
+ struct stat s;
121
+
122
+ if (file < 0) {
123
+ return UV_UNKNOWN_HANDLE;
124
+ }
125
+
126
+ if (isatty(file)) {
127
+ return UV_TTY;
128
+ }
129
+
130
+ if (fstat(file, &s)) {
131
+ return UV_UNKNOWN_HANDLE;
132
+ }
133
+
134
+ if (!S_ISSOCK(s.st_mode) && !S_ISFIFO(s.st_mode)) {
135
+ return UV_FILE;
136
+ }
137
+
138
+ return UV_NAMED_PIPE;
139
+ }
140
+
141
+
142
+ void uv_tty_reset_mode() {
143
+ if (orig_termios_fd >= 0) {
144
+ tcsetattr(orig_termios_fd, TCSANOW, &orig_termios);
145
+ }
146
+ }
@@ -0,0 +1,661 @@
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 "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <assert.h>
26
+ #include <string.h>
27
+ #include <errno.h>
28
+ #include <stdlib.h>
29
+ #include <unistd.h>
30
+
31
+
32
+ static void uv__udp_run_completed(uv_udp_t* handle);
33
+ static void uv__udp_run_pending(uv_udp_t* handle);
34
+ static void uv__udp_recvmsg(uv_loop_t* loop, uv__io_t* w, int revents);
35
+ static void uv__udp_sendmsg(uv_loop_t* loop, uv__io_t* w, int revents);
36
+ static int uv__udp_maybe_deferred_bind(uv_udp_t* handle, int domain);
37
+ static int uv__udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
38
+ int bufcnt, struct sockaddr* addr, socklen_t addrlen, uv_udp_send_cb send_cb);
39
+
40
+
41
+ static void uv__udp_start_watcher(uv_udp_t* handle,
42
+ uv__io_t* w,
43
+ uv__io_cb cb,
44
+ int events) {
45
+ if (uv__io_active(w)) return;
46
+ uv__io_init(w, cb, handle->fd, events);
47
+ uv__io_start(handle->loop, w);
48
+ uv__handle_start(handle);
49
+ }
50
+
51
+
52
+ static void uv__udp_stop_watcher(uv_udp_t* handle, uv__io_t* w) {
53
+ if (!uv__io_active(w)) return;
54
+ uv__io_stop(handle->loop, w);
55
+
56
+ if (!uv__io_active(&handle->read_watcher) &&
57
+ !uv__io_active(&handle->write_watcher))
58
+ {
59
+ uv__handle_stop(handle);
60
+ }
61
+ }
62
+
63
+
64
+ void uv__udp_close(uv_udp_t* handle) {
65
+ uv__udp_stop_watcher(handle, &handle->write_watcher);
66
+ uv__udp_stop_watcher(handle, &handle->read_watcher);
67
+ close(handle->fd);
68
+ handle->fd = -1;
69
+ }
70
+
71
+
72
+ void uv__udp_finish_close(uv_udp_t* handle) {
73
+ uv_udp_send_t* req;
74
+ ngx_queue_t* q;
75
+
76
+ assert(!uv__io_active(&handle->write_watcher));
77
+ assert(!uv__io_active(&handle->read_watcher));
78
+ assert(handle->fd == -1);
79
+
80
+ uv__udp_run_completed(handle);
81
+
82
+ while (!ngx_queue_empty(&handle->write_queue)) {
83
+ q = ngx_queue_head(&handle->write_queue);
84
+ ngx_queue_remove(q);
85
+
86
+ req = ngx_queue_data(q, uv_udp_send_t, queue);
87
+ uv__req_unregister(handle->loop, req);
88
+
89
+ if (req->send_cb) {
90
+ /* FIXME proper error code like UV_EABORTED */
91
+ uv__set_artificial_error(handle->loop, UV_EINTR);
92
+ req->send_cb(req, -1);
93
+ }
94
+ }
95
+
96
+ /* Now tear down the handle. */
97
+ handle->flags = 0;
98
+ handle->recv_cb = NULL;
99
+ handle->alloc_cb = NULL;
100
+ /* but _do not_ touch close_cb */
101
+ }
102
+
103
+
104
+ static void uv__udp_run_pending(uv_udp_t* handle) {
105
+ uv_udp_send_t* req;
106
+ ngx_queue_t* q;
107
+ struct msghdr h;
108
+ ssize_t size;
109
+
110
+ while (!ngx_queue_empty(&handle->write_queue)) {
111
+ q = ngx_queue_head(&handle->write_queue);
112
+ assert(q != NULL);
113
+
114
+ req = ngx_queue_data(q, uv_udp_send_t, queue);
115
+ assert(req != NULL);
116
+
117
+ memset(&h, 0, sizeof h);
118
+ h.msg_name = &req->addr;
119
+ h.msg_namelen = (req->addr.sin6_family == AF_INET6 ?
120
+ sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
121
+ h.msg_iov = (struct iovec*)req->bufs;
122
+ h.msg_iovlen = req->bufcnt;
123
+
124
+ do {
125
+ size = sendmsg(handle->fd, &h, 0);
126
+ }
127
+ while (size == -1 && errno == EINTR);
128
+
129
+ /* TODO try to write once or twice more in the
130
+ * hope that the socket becomes readable again?
131
+ */
132
+ if (size == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
133
+ break;
134
+
135
+ req->status = (size == -1 ? -errno : size);
136
+
137
+ #ifndef NDEBUG
138
+ /* Sanity check. */
139
+ if (size != -1) {
140
+ ssize_t nbytes;
141
+ int i;
142
+
143
+ for (nbytes = i = 0; i < req->bufcnt; i++)
144
+ nbytes += req->bufs[i].len;
145
+
146
+ assert(size == nbytes);
147
+ }
148
+ #endif
149
+
150
+ /* Sending a datagram is an atomic operation: either all data
151
+ * is written or nothing is (and EMSGSIZE is raised). That is
152
+ * why we don't handle partial writes. Just pop the request
153
+ * off the write queue and onto the completed queue, done.
154
+ */
155
+ ngx_queue_remove(&req->queue);
156
+ ngx_queue_insert_tail(&handle->write_completed_queue, &req->queue);
157
+ }
158
+ }
159
+
160
+
161
+ static void uv__udp_run_completed(uv_udp_t* handle) {
162
+ uv_udp_send_t* req;
163
+ ngx_queue_t* q;
164
+
165
+ while (!ngx_queue_empty(&handle->write_completed_queue)) {
166
+ q = ngx_queue_head(&handle->write_completed_queue);
167
+ ngx_queue_remove(q);
168
+
169
+ req = ngx_queue_data(q, uv_udp_send_t, queue);
170
+ uv__req_unregister(handle->loop, req);
171
+
172
+ if (req->bufs != req->bufsml)
173
+ free(req->bufs);
174
+
175
+ if (req->send_cb == NULL)
176
+ continue;
177
+
178
+ /* req->status >= 0 == bytes written
179
+ * req->status < 0 == errno
180
+ */
181
+ if (req->status >= 0) {
182
+ req->send_cb(req, 0);
183
+ }
184
+ else {
185
+ uv__set_sys_error(handle->loop, -req->status);
186
+ req->send_cb(req, -1);
187
+ }
188
+ }
189
+ }
190
+
191
+
192
+ static void uv__udp_recvmsg(uv_loop_t* loop, uv__io_t* w, int revents) {
193
+ struct sockaddr_storage peer;
194
+ struct msghdr h;
195
+ uv_udp_t* handle;
196
+ ssize_t nread;
197
+ uv_buf_t buf;
198
+ int flags;
199
+ int count;
200
+
201
+ handle = container_of(w, uv_udp_t, read_watcher);
202
+ assert(handle->type == UV_UDP);
203
+ assert(revents & UV__IO_READ);
204
+
205
+ assert(handle->recv_cb != NULL);
206
+ assert(handle->alloc_cb != NULL);
207
+
208
+ /* Prevent loop starvation when the data comes in as fast as (or faster than)
209
+ * we can read it. XXX Need to rearm fd if we switch to edge-triggered I/O.
210
+ */
211
+ count = 32;
212
+
213
+ memset(&h, 0, sizeof(h));
214
+ h.msg_name = &peer;
215
+
216
+ do {
217
+ buf = handle->alloc_cb((uv_handle_t*)handle, 64 * 1024);
218
+ assert(buf.len > 0);
219
+ assert(buf.base != NULL);
220
+
221
+ h.msg_namelen = sizeof(peer);
222
+ h.msg_iov = (struct iovec*)&buf;
223
+ h.msg_iovlen = 1;
224
+
225
+ do {
226
+ nread = recvmsg(handle->fd, &h, 0);
227
+ }
228
+ while (nread == -1 && errno == EINTR);
229
+
230
+ if (nread == -1) {
231
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
232
+ uv__set_sys_error(handle->loop, EAGAIN);
233
+ handle->recv_cb(handle, 0, buf, NULL, 0);
234
+ }
235
+ else {
236
+ uv__set_sys_error(handle->loop, errno);
237
+ handle->recv_cb(handle, -1, buf, NULL, 0);
238
+ }
239
+ }
240
+ else {
241
+ flags = 0;
242
+
243
+ if (h.msg_flags & MSG_TRUNC)
244
+ flags |= UV_UDP_PARTIAL;
245
+
246
+ handle->recv_cb(handle,
247
+ nread,
248
+ buf,
249
+ (struct sockaddr*)&peer,
250
+ flags);
251
+ }
252
+ }
253
+ /* recv_cb callback may decide to pause or close the handle */
254
+ while (nread != -1
255
+ && count-- > 0
256
+ && handle->fd != -1
257
+ && handle->recv_cb != NULL);
258
+ }
259
+
260
+
261
+ static void uv__udp_sendmsg(uv_loop_t* loop, uv__io_t* w, int revents) {
262
+ uv_udp_t* handle;
263
+
264
+ handle = container_of(w, uv_udp_t, write_watcher);
265
+ assert(handle->type == UV_UDP);
266
+ assert(revents & UV__IO_WRITE);
267
+
268
+ assert(!ngx_queue_empty(&handle->write_queue)
269
+ || !ngx_queue_empty(&handle->write_completed_queue));
270
+
271
+ /* Write out pending data first. */
272
+ uv__udp_run_pending(handle);
273
+
274
+ /* Drain 'request completed' queue. */
275
+ uv__udp_run_completed(handle);
276
+
277
+ if (!ngx_queue_empty(&handle->write_completed_queue)) {
278
+ /* Schedule completion callbacks. */
279
+ uv__io_feed(handle->loop, &handle->write_watcher, UV__IO_WRITE);
280
+ }
281
+ else if (ngx_queue_empty(&handle->write_queue)) {
282
+ /* Pending queue and completion queue empty, stop watcher. */
283
+ uv__udp_stop_watcher(handle, &handle->write_watcher);
284
+ }
285
+ }
286
+
287
+
288
+ static int uv__bind(uv_udp_t* handle,
289
+ int domain,
290
+ struct sockaddr* addr,
291
+ socklen_t len,
292
+ unsigned flags) {
293
+ int saved_errno;
294
+ int status;
295
+ int yes;
296
+ int fd;
297
+
298
+ saved_errno = errno;
299
+ status = -1;
300
+ fd = -1;
301
+
302
+ /* Check for bad flags. */
303
+ if (flags & ~UV_UDP_IPV6ONLY) {
304
+ uv__set_sys_error(handle->loop, EINVAL);
305
+ goto out;
306
+ }
307
+
308
+ /* Cannot set IPv6-only mode on non-IPv6 socket. */
309
+ if ((flags & UV_UDP_IPV6ONLY) && domain != AF_INET6) {
310
+ uv__set_sys_error(handle->loop, EINVAL);
311
+ goto out;
312
+ }
313
+
314
+ /* Check for already active socket. */
315
+ if (handle->fd != -1) {
316
+ uv__set_artificial_error(handle->loop, UV_EALREADY);
317
+ goto out;
318
+ }
319
+
320
+ if ((fd = uv__socket(domain, SOCK_DGRAM, 0)) == -1) {
321
+ uv__set_sys_error(handle->loop, errno);
322
+ goto out;
323
+ }
324
+
325
+ yes = 1;
326
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
327
+ uv__set_sys_error(handle->loop, errno);
328
+ goto out;
329
+ }
330
+
331
+ /* On the BSDs, SO_REUSEADDR lets you reuse an address that's in the TIME_WAIT
332
+ * state (i.e. was until recently tied to a socket) while SO_REUSEPORT lets
333
+ * multiple processes bind to the same address. Yes, it's something of a
334
+ * misnomer but then again, SO_REUSEADDR was already taken.
335
+ *
336
+ * None of the above applies to Linux: SO_REUSEADDR implies SO_REUSEPORT on
337
+ * Linux and hence it does not have SO_REUSEPORT at all.
338
+ */
339
+ #ifdef SO_REUSEPORT
340
+ yes = 1;
341
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof yes) == -1) {
342
+ uv__set_sys_error(handle->loop, errno);
343
+ goto out;
344
+ }
345
+ #endif
346
+
347
+ if (flags & UV_UDP_IPV6ONLY) {
348
+ #ifdef IPV6_V6ONLY
349
+ yes = 1;
350
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof yes) == -1) {
351
+ uv__set_sys_error(handle->loop, errno);
352
+ goto out;
353
+ }
354
+ #else
355
+ uv__set_sys_error(handle->loop, ENOTSUP);
356
+ goto out;
357
+ #endif
358
+ }
359
+
360
+ if (bind(fd, addr, len) == -1) {
361
+ uv__set_sys_error(handle->loop, errno);
362
+ goto out;
363
+ }
364
+
365
+ handle->fd = fd;
366
+ status = 0;
367
+
368
+ out:
369
+ if (status)
370
+ close(fd);
371
+
372
+ errno = saved_errno;
373
+ return status;
374
+ }
375
+
376
+
377
+ static int uv__udp_maybe_deferred_bind(uv_udp_t* handle, int domain) {
378
+ struct sockaddr_storage taddr;
379
+ socklen_t addrlen;
380
+
381
+ assert(domain == AF_INET || domain == AF_INET6);
382
+
383
+ if (handle->fd != -1)
384
+ return 0;
385
+
386
+ switch (domain) {
387
+ case AF_INET:
388
+ {
389
+ struct sockaddr_in* addr = (void*)&taddr;
390
+ memset(addr, 0, sizeof *addr);
391
+ addr->sin_family = AF_INET;
392
+ addr->sin_addr.s_addr = INADDR_ANY;
393
+ addrlen = sizeof *addr;
394
+ break;
395
+ }
396
+ case AF_INET6:
397
+ {
398
+ struct sockaddr_in6* addr = (void*)&taddr;
399
+ memset(addr, 0, sizeof *addr);
400
+ addr->sin6_family = AF_INET6;
401
+ addr->sin6_addr = in6addr_any;
402
+ addrlen = sizeof *addr;
403
+ break;
404
+ }
405
+ default:
406
+ assert(0 && "unsupported address family");
407
+ abort();
408
+ }
409
+
410
+ return uv__bind(handle, domain, (struct sockaddr*)&taddr, addrlen, 0);
411
+ }
412
+
413
+
414
+ static int uv__udp_send(uv_udp_send_t* req,
415
+ uv_udp_t* handle,
416
+ uv_buf_t bufs[],
417
+ int bufcnt,
418
+ struct sockaddr* addr,
419
+ socklen_t addrlen,
420
+ uv_udp_send_cb send_cb) {
421
+ if (uv__udp_maybe_deferred_bind(handle, addr->sa_family))
422
+ return -1;
423
+
424
+ uv__req_init(handle->loop, req, UV_UDP_SEND);
425
+
426
+ assert(addrlen <= sizeof(req->addr));
427
+ memcpy(&req->addr, addr, addrlen);
428
+ req->send_cb = send_cb;
429
+ req->handle = handle;
430
+ req->bufcnt = bufcnt;
431
+
432
+ if (bufcnt <= UV_REQ_BUFSML_SIZE) {
433
+ req->bufs = req->bufsml;
434
+ }
435
+ else if ((req->bufs = malloc(bufcnt * sizeof(bufs[0]))) == NULL) {
436
+ uv__set_sys_error(handle->loop, ENOMEM);
437
+ return -1;
438
+ }
439
+ memcpy(req->bufs, bufs, bufcnt * sizeof(bufs[0]));
440
+
441
+ ngx_queue_insert_tail(&handle->write_queue, &req->queue);
442
+
443
+ uv__udp_start_watcher(handle,
444
+ &handle->write_watcher,
445
+ uv__udp_sendmsg,
446
+ UV__IO_WRITE);
447
+
448
+ return 0;
449
+ }
450
+
451
+
452
+ int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
453
+ memset(handle, 0, sizeof *handle);
454
+
455
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_UDP);
456
+ loop->counters.udp_init++;
457
+
458
+ handle->fd = -1;
459
+ ngx_queue_init(&handle->write_queue);
460
+ ngx_queue_init(&handle->write_completed_queue);
461
+
462
+ return 0;
463
+ }
464
+
465
+
466
+ int uv__udp_bind(uv_udp_t* handle, struct sockaddr_in addr, unsigned flags) {
467
+ return uv__bind(handle,
468
+ AF_INET,
469
+ (struct sockaddr*)&addr,
470
+ sizeof addr,
471
+ flags);
472
+ }
473
+
474
+
475
+ int uv__udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr, unsigned flags) {
476
+ return uv__bind(handle,
477
+ AF_INET6,
478
+ (struct sockaddr*)&addr,
479
+ sizeof addr,
480
+ flags);
481
+ }
482
+
483
+
484
+ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
485
+ const char* interface_addr, uv_membership membership) {
486
+
487
+ int optname;
488
+ struct ip_mreq mreq;
489
+ memset(&mreq, 0, sizeof mreq);
490
+
491
+ if (interface_addr) {
492
+ mreq.imr_interface.s_addr = inet_addr(interface_addr);
493
+ } else {
494
+ mreq.imr_interface.s_addr = htonl(INADDR_ANY);
495
+ }
496
+
497
+ mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
498
+
499
+ switch (membership) {
500
+ case UV_JOIN_GROUP:
501
+ optname = IP_ADD_MEMBERSHIP;
502
+ break;
503
+ case UV_LEAVE_GROUP:
504
+ optname = IP_DROP_MEMBERSHIP;
505
+ break;
506
+ default:
507
+ uv__set_sys_error(handle->loop, EFAULT);
508
+ return -1;
509
+ }
510
+
511
+ if (setsockopt(handle->fd, IPPROTO_IP, optname, (void*) &mreq, sizeof mreq) == -1) {
512
+ uv__set_sys_error(handle->loop, errno);
513
+ return -1;
514
+ }
515
+
516
+ return 0;
517
+ }
518
+
519
+
520
+ static int uv__setsockopt_maybe_char(uv_udp_t* handle, int option, int val) {
521
+ #if __sun
522
+ char arg = val;
523
+ #else
524
+ int arg = val;
525
+ #endif
526
+
527
+ if (val < 0 || val > 255)
528
+ return uv__set_sys_error(handle->loop, EINVAL);
529
+
530
+ if (setsockopt(handle->fd, IPPROTO_IP, option, &arg, sizeof(arg)))
531
+ return uv__set_sys_error(handle->loop, errno);
532
+
533
+ return 0;
534
+ }
535
+
536
+
537
+ int uv_udp_set_broadcast(uv_udp_t* handle, int on) {
538
+ if (setsockopt(handle->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)))
539
+ return uv__set_sys_error(handle->loop, errno);
540
+
541
+ return 0;
542
+ }
543
+
544
+
545
+ int uv_udp_set_ttl(uv_udp_t* handle, int ttl) {
546
+ if (ttl < 1 || ttl > 255)
547
+ return uv__set_sys_error(handle->loop, EINVAL);
548
+
549
+ if (setsockopt(handle->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)))
550
+ return uv__set_sys_error(handle->loop, errno);
551
+
552
+ return 0;
553
+ }
554
+
555
+
556
+ int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) {
557
+ return uv__setsockopt_maybe_char(handle, IP_MULTICAST_TTL, ttl);
558
+ }
559
+
560
+
561
+ int uv_udp_set_multicast_loop(uv_udp_t* handle, int on) {
562
+ return uv__setsockopt_maybe_char(handle, IP_MULTICAST_LOOP, on);
563
+ }
564
+
565
+
566
+ int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) {
567
+ socklen_t socklen;
568
+ int saved_errno;
569
+ int rv = 0;
570
+
571
+ /* Don't clobber errno. */
572
+ saved_errno = errno;
573
+
574
+ if (handle->fd < 0) {
575
+ uv__set_sys_error(handle->loop, EINVAL);
576
+ rv = -1;
577
+ goto out;
578
+ }
579
+
580
+ /* sizeof(socklen_t) != sizeof(int) on some systems. */
581
+ socklen = (socklen_t)*namelen;
582
+
583
+ if (getsockname(handle->fd, name, &socklen) == -1) {
584
+ uv__set_sys_error(handle->loop, errno);
585
+ rv = -1;
586
+ } else {
587
+ *namelen = (int)socklen;
588
+ }
589
+
590
+ out:
591
+ errno = saved_errno;
592
+ return rv;
593
+ }
594
+
595
+
596
+ int uv_udp_send(uv_udp_send_t* req,
597
+ uv_udp_t* handle,
598
+ uv_buf_t bufs[],
599
+ int bufcnt,
600
+ struct sockaddr_in addr,
601
+ uv_udp_send_cb send_cb) {
602
+ return uv__udp_send(req,
603
+ handle,
604
+ bufs,
605
+ bufcnt,
606
+ (struct sockaddr*)&addr,
607
+ sizeof addr,
608
+ send_cb);
609
+ }
610
+
611
+
612
+ int uv_udp_send6(uv_udp_send_t* req,
613
+ uv_udp_t* handle,
614
+ uv_buf_t bufs[],
615
+ int bufcnt,
616
+ struct sockaddr_in6 addr,
617
+ uv_udp_send_cb send_cb) {
618
+ return uv__udp_send(req,
619
+ handle,
620
+ bufs,
621
+ bufcnt,
622
+ (struct sockaddr*)&addr,
623
+ sizeof addr,
624
+ send_cb);
625
+ }
626
+
627
+
628
+ int uv_udp_recv_start(uv_udp_t* handle,
629
+ uv_alloc_cb alloc_cb,
630
+ uv_udp_recv_cb recv_cb) {
631
+ if (alloc_cb == NULL || recv_cb == NULL) {
632
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
633
+ return -1;
634
+ }
635
+
636
+ if (uv__io_active(&handle->read_watcher)) {
637
+ uv__set_artificial_error(handle->loop, UV_EALREADY);
638
+ return -1;
639
+ }
640
+
641
+ if (uv__udp_maybe_deferred_bind(handle, AF_INET))
642
+ return -1;
643
+
644
+ handle->alloc_cb = alloc_cb;
645
+ handle->recv_cb = recv_cb;
646
+
647
+ uv__udp_start_watcher(handle,
648
+ &handle->read_watcher,
649
+ uv__udp_recvmsg,
650
+ UV__IO_READ);
651
+
652
+ return 0;
653
+ }
654
+
655
+
656
+ int uv_udp_recv_stop(uv_udp_t* handle) {
657
+ uv__udp_stop_watcher(handle, &handle->read_watcher);
658
+ handle->alloc_cb = NULL;
659
+ handle->recv_cb = NULL;
660
+ return 0;
661
+ }