1
0

test-getsockname.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. static const int server_port = TEST_PORT;
  27. /* Will be updated right after making the uv_connect_call */
  28. static int connect_port = -1;
  29. static int getsocknamecount = 0;
  30. static int getpeernamecount = 0;
  31. static uv_loop_t* loop;
  32. static uv_tcp_t tcp;
  33. static uv_udp_t udp;
  34. static uv_connect_t connect_req;
  35. static uv_tcp_t tcpServer;
  36. static uv_udp_t udpServer;
  37. static uv_udp_send_t send_req;
  38. static void alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
  39. buf->base = malloc(suggested_size);
  40. buf->len = suggested_size;
  41. }
  42. static void on_close(uv_handle_t* peer) {
  43. free(peer);
  44. uv_close((uv_handle_t*)&tcpServer, NULL);
  45. }
  46. static void after_shutdown(uv_shutdown_t* req, int status) {
  47. uv_close((uv_handle_t*) req->handle, on_close);
  48. free(req);
  49. }
  50. static void after_read(uv_stream_t* handle,
  51. ssize_t nread,
  52. const uv_buf_t* buf) {
  53. uv_shutdown_t* req;
  54. int r;
  55. if (buf->base) {
  56. free(buf->base);
  57. }
  58. req = (uv_shutdown_t*) malloc(sizeof *req);
  59. r = uv_shutdown(req, handle, after_shutdown);
  60. ASSERT(r == 0);
  61. }
  62. static void check_sockname(struct sockaddr* addr, const char* compare_ip,
  63. int compare_port, const char* context) {
  64. struct sockaddr_in check_addr = *(struct sockaddr_in*) addr;
  65. struct sockaddr_in compare_addr;
  66. char check_ip[17];
  67. int r;
  68. ASSERT(0 == uv_ip4_addr(compare_ip, compare_port, &compare_addr));
  69. /* Both addresses should be ipv4 */
  70. ASSERT(check_addr.sin_family == AF_INET);
  71. ASSERT(compare_addr.sin_family == AF_INET);
  72. /* Check if the ip matches */
  73. ASSERT(memcmp(&check_addr.sin_addr,
  74. &compare_addr.sin_addr,
  75. sizeof compare_addr.sin_addr) == 0);
  76. /* Check if the port matches. If port == 0 anything goes. */
  77. ASSERT(compare_port == 0 || check_addr.sin_port == compare_addr.sin_port);
  78. r = uv_ip4_name(&check_addr, (char*) check_ip, sizeof check_ip);
  79. ASSERT(r == 0);
  80. printf("%s: %s:%d\n", context, check_ip, ntohs(check_addr.sin_port));
  81. }
  82. static void on_connection(uv_stream_t* server, int status) {
  83. struct sockaddr sockname, peername;
  84. int namelen;
  85. uv_tcp_t* handle;
  86. int r;
  87. if (status != 0) {
  88. fprintf(stderr, "Connect error %s\n", uv_err_name(status));
  89. }
  90. ASSERT(status == 0);
  91. handle = malloc(sizeof(*handle));
  92. ASSERT(handle != NULL);
  93. r = uv_tcp_init(loop, handle);
  94. ASSERT(r == 0);
  95. /* associate server with stream */
  96. handle->data = server;
  97. r = uv_accept(server, (uv_stream_t*)handle);
  98. ASSERT(r == 0);
  99. namelen = sizeof sockname;
  100. r = uv_tcp_getsockname(handle, &sockname, &namelen);
  101. ASSERT(r == 0);
  102. check_sockname(&sockname, "127.0.0.1", server_port, "accepted socket");
  103. getsocknamecount++;
  104. namelen = sizeof peername;
  105. r = uv_tcp_getpeername(handle, &peername, &namelen);
  106. ASSERT(r == 0);
  107. check_sockname(&peername, "127.0.0.1", connect_port, "accepted socket peer");
  108. getpeernamecount++;
  109. r = uv_read_start((uv_stream_t*)handle, alloc, after_read);
  110. ASSERT(r == 0);
  111. }
  112. static void on_connect(uv_connect_t* req, int status) {
  113. struct sockaddr sockname, peername;
  114. int r, namelen;
  115. ASSERT(status == 0);
  116. namelen = sizeof sockname;
  117. r = uv_tcp_getsockname((uv_tcp_t*) req->handle, &sockname, &namelen);
  118. ASSERT(r == 0);
  119. check_sockname(&sockname, "127.0.0.1", 0, "connected socket");
  120. getsocknamecount++;
  121. namelen = sizeof peername;
  122. r = uv_tcp_getpeername((uv_tcp_t*) req->handle, &peername, &namelen);
  123. ASSERT(r == 0);
  124. check_sockname(&peername, "127.0.0.1", server_port, "connected socket peer");
  125. getpeernamecount++;
  126. uv_close((uv_handle_t*)&tcp, NULL);
  127. }
  128. static int tcp_listener(void) {
  129. struct sockaddr_in addr;
  130. struct sockaddr sockname, peername;
  131. int namelen;
  132. int r;
  133. ASSERT(0 == uv_ip4_addr("0.0.0.0", server_port, &addr));
  134. r = uv_tcp_init(loop, &tcpServer);
  135. if (r) {
  136. fprintf(stderr, "Socket creation error\n");
  137. return 1;
  138. }
  139. r = uv_tcp_bind(&tcpServer, (const struct sockaddr*) &addr, 0);
  140. if (r) {
  141. fprintf(stderr, "Bind error\n");
  142. return 1;
  143. }
  144. r = uv_listen((uv_stream_t*)&tcpServer, 128, on_connection);
  145. if (r) {
  146. fprintf(stderr, "Listen error\n");
  147. return 1;
  148. }
  149. memset(&sockname, -1, sizeof sockname);
  150. namelen = sizeof sockname;
  151. r = uv_tcp_getsockname(&tcpServer, &sockname, &namelen);
  152. ASSERT(r == 0);
  153. check_sockname(&sockname, "0.0.0.0", server_port, "server socket");
  154. getsocknamecount++;
  155. namelen = sizeof sockname;
  156. r = uv_tcp_getpeername(&tcpServer, &peername, &namelen);
  157. ASSERT(r == UV_ENOTCONN);
  158. getpeernamecount++;
  159. return 0;
  160. }
  161. static void tcp_connector(void) {
  162. struct sockaddr_in server_addr;
  163. struct sockaddr sockname;
  164. int r, namelen;
  165. ASSERT(0 == uv_ip4_addr("127.0.0.1", server_port, &server_addr));
  166. r = uv_tcp_init(loop, &tcp);
  167. tcp.data = &connect_req;
  168. ASSERT(!r);
  169. r = uv_tcp_connect(&connect_req,
  170. &tcp,
  171. (const struct sockaddr*) &server_addr,
  172. on_connect);
  173. ASSERT(!r);
  174. /* Fetch the actual port used by the connecting socket. */
  175. namelen = sizeof sockname;
  176. r = uv_tcp_getsockname(&tcp, &sockname, &namelen);
  177. ASSERT(!r);
  178. ASSERT(sockname.sa_family == AF_INET);
  179. connect_port = ntohs(((struct sockaddr_in*) &sockname)->sin_port);
  180. ASSERT(connect_port > 0);
  181. }
  182. static void udp_recv(uv_udp_t* handle,
  183. ssize_t nread,
  184. const uv_buf_t* buf,
  185. const struct sockaddr* addr,
  186. unsigned flags) {
  187. struct sockaddr sockname;
  188. int namelen;
  189. int r;
  190. ASSERT(nread >= 0);
  191. free(buf->base);
  192. if (nread == 0) {
  193. return;
  194. }
  195. memset(&sockname, -1, sizeof sockname);
  196. namelen = sizeof(sockname);
  197. r = uv_udp_getsockname(&udp, &sockname, &namelen);
  198. ASSERT(r == 0);
  199. check_sockname(&sockname, "0.0.0.0", 0, "udp receiving socket");
  200. getsocknamecount++;
  201. uv_close((uv_handle_t*) &udp, NULL);
  202. uv_close((uv_handle_t*) handle, NULL);
  203. }
  204. static void udp_send(uv_udp_send_t* req, int status) {
  205. }
  206. static int udp_listener(void) {
  207. struct sockaddr_in addr;
  208. struct sockaddr sockname;
  209. int namelen;
  210. int r;
  211. ASSERT(0 == uv_ip4_addr("0.0.0.0", server_port, &addr));
  212. r = uv_udp_init(loop, &udpServer);
  213. if (r) {
  214. fprintf(stderr, "Socket creation error\n");
  215. return 1;
  216. }
  217. r = uv_udp_bind(&udpServer, (const struct sockaddr*) &addr, 0);
  218. if (r) {
  219. fprintf(stderr, "Bind error\n");
  220. return 1;
  221. }
  222. memset(&sockname, -1, sizeof sockname);
  223. namelen = sizeof sockname;
  224. r = uv_udp_getsockname(&udpServer, &sockname, &namelen);
  225. ASSERT(r == 0);
  226. check_sockname(&sockname, "0.0.0.0", server_port, "udp listener socket");
  227. getsocknamecount++;
  228. r = uv_udp_recv_start(&udpServer, alloc, udp_recv);
  229. ASSERT(r == 0);
  230. return 0;
  231. }
  232. static void udp_sender(void) {
  233. struct sockaddr_in server_addr;
  234. uv_buf_t buf;
  235. int r;
  236. r = uv_udp_init(loop, &udp);
  237. ASSERT(!r);
  238. buf = uv_buf_init("PING", 4);
  239. ASSERT(0 == uv_ip4_addr("127.0.0.1", server_port, &server_addr));
  240. r = uv_udp_send(&send_req,
  241. &udp,
  242. &buf,
  243. 1,
  244. (const struct sockaddr*) &server_addr,
  245. udp_send);
  246. ASSERT(!r);
  247. }
  248. TEST_IMPL(getsockname_tcp) {
  249. loop = uv_default_loop();
  250. if (tcp_listener())
  251. return 1;
  252. tcp_connector();
  253. uv_run(loop, UV_RUN_DEFAULT);
  254. ASSERT(getsocknamecount == 3);
  255. ASSERT(getpeernamecount == 3);
  256. MAKE_VALGRIND_HAPPY();
  257. return 0;
  258. }
  259. TEST_IMPL(getsockname_udp) {
  260. loop = uv_default_loop();
  261. if (udp_listener())
  262. return 1;
  263. udp_sender();
  264. uv_run(loop, UV_RUN_DEFAULT);
  265. ASSERT(getsocknamecount == 2);
  266. MAKE_VALGRIND_HAPPY();
  267. return 0;
  268. }