select.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "test.h"
  20. /**
  21. * \page page_pjlib_select_test Test: Socket Select()
  22. *
  23. * This file provides implementation of \b select_test(). It tests the
  24. * functionality of the pj_sock_select() API.
  25. *
  26. *
  27. * This file is <b>pjlib-test/select.c</b>
  28. *
  29. * \include pjlib-test/select.c
  30. */
  31. #if INCLUDE_SELECT_TEST
  32. #include <pj/sock.h>
  33. #include <pj/sock_select.h>
  34. #include <pj/log.h>
  35. #include <pj/string.h>
  36. #include <pj/assert.h>
  37. #include <pj/os.h>
  38. #include <pj/errno.h>
  39. enum
  40. {
  41. READ_FDS,
  42. WRITE_FDS,
  43. EXCEPT_FDS
  44. };
  45. #define UDP_PORT 51232
  46. #define THIS_FILE "select_test"
  47. /*
  48. * do_select()
  49. *
  50. * Perform pj_sock_select() and find out which sockets
  51. * are signalled.
  52. */
  53. static int do_select( pj_sock_t sock1, pj_sock_t sock2,
  54. int setcount[])
  55. {
  56. pj_fd_set_t fds[3];
  57. pj_time_val timeout;
  58. int i, n;
  59. for (i=0; i<3; ++i) {
  60. PJ_FD_ZERO(&fds[i]);
  61. PJ_FD_SET(sock1, &fds[i]);
  62. PJ_FD_SET(sock2, &fds[i]);
  63. setcount[i] = 0;
  64. }
  65. timeout.sec = 1;
  66. timeout.msec = 0;
  67. n = pj_sock_select(PJ_IOQUEUE_MAX_HANDLES, &fds[0], &fds[1], &fds[2],
  68. &timeout);
  69. if (n < 0)
  70. return n;
  71. if (n == 0)
  72. return 0;
  73. for (i=0; i<3; ++i) {
  74. if (PJ_FD_ISSET(sock1, &fds[i]))
  75. setcount[i]++;
  76. if (PJ_FD_ISSET(sock2, &fds[i]))
  77. setcount[i]++;
  78. }
  79. return n;
  80. }
  81. /*
  82. * select_test()
  83. *
  84. * Test main entry.
  85. */
  86. int select_test()
  87. {
  88. pj_sock_t udp1=PJ_INVALID_SOCKET, udp2=PJ_INVALID_SOCKET;
  89. pj_sockaddr_in udp_addr;
  90. int status;
  91. int setcount[3];
  92. pj_str_t s;
  93. const char data[] = "hello";
  94. const int datalen = 5;
  95. pj_ssize_t sent, received;
  96. char buf[10];
  97. pj_status_t rc;
  98. PJ_LOG(3, (THIS_FILE, "...Testing simple UDP select()"));
  99. // Create two UDP sockets.
  100. rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp1);
  101. if (rc != PJ_SUCCESS) {
  102. app_perror("...error: unable to create socket", rc);
  103. status=-10; goto on_return;
  104. }
  105. rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp2);
  106. if (udp2 == PJ_INVALID_SOCKET) {
  107. app_perror("...error: unable to create socket", rc);
  108. status=-20; goto on_return;
  109. }
  110. // Bind one of the UDP socket.
  111. pj_bzero(&udp_addr, sizeof(udp_addr));
  112. udp_addr.sin_family = pj_AF_INET();
  113. udp_addr.sin_port = UDP_PORT;
  114. udp_addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
  115. if (pj_sock_bind(udp2, &udp_addr, sizeof(udp_addr))) {
  116. status=-30; goto on_return;
  117. }
  118. // Send data.
  119. sent = datalen;
  120. rc = pj_sock_sendto(udp1, data, &sent, 0, &udp_addr, sizeof(udp_addr));
  121. if (rc != PJ_SUCCESS || sent != datalen) {
  122. app_perror("...error: sendto() error", rc);
  123. status=-40; goto on_return;
  124. }
  125. // Sleep a bit. See https://github.com/pjsip/pjproject/issues/890
  126. pj_thread_sleep(10);
  127. // Check that socket is marked as reable.
  128. // Note that select() may also report that sockets are writable.
  129. status = do_select(udp1, udp2, setcount);
  130. if (status < 0) {
  131. char errbuf[128];
  132. pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf));
  133. PJ_LOG(1,(THIS_FILE, "...error: %s", errbuf));
  134. status=-50; goto on_return;
  135. }
  136. if (status == 0) {
  137. status=-60; goto on_return;
  138. }
  139. if (setcount[READ_FDS] != 1) {
  140. status=-70; goto on_return;
  141. }
  142. if (setcount[WRITE_FDS] != 0) {
  143. if (setcount[WRITE_FDS] == 2) {
  144. PJ_LOG(3,(THIS_FILE, "...info: system reports writable sockets"));
  145. } else {
  146. status=-80; goto on_return;
  147. }
  148. } else {
  149. PJ_LOG(3,(THIS_FILE,
  150. "...info: system doesn't report writable sockets"));
  151. }
  152. if (setcount[EXCEPT_FDS] != 0) {
  153. status=-90; goto on_return;
  154. }
  155. // Read the socket to clear readable sockets.
  156. received = sizeof(buf);
  157. rc = pj_sock_recv(udp2, buf, &received, 0);
  158. if (rc != PJ_SUCCESS || received != 5) {
  159. status=-100; goto on_return;
  160. }
  161. status = 0;
  162. // Test timeout on the read part.
  163. // This won't necessarily return zero, as select() may report that
  164. // sockets are writable.
  165. setcount[0] = setcount[1] = setcount[2] = 0;
  166. status = do_select(udp1, udp2, setcount);
  167. if (status != 0 && status != setcount[WRITE_FDS]) {
  168. PJ_LOG(3,(THIS_FILE, "...error: expecting timeout but got %d sks set",
  169. status));
  170. PJ_LOG(3,(THIS_FILE, " rdset: %d, wrset: %d, exset: %d",
  171. setcount[0], setcount[1], setcount[2]));
  172. status = -110; goto on_return;
  173. }
  174. if (setcount[READ_FDS] != 0) {
  175. PJ_LOG(3,(THIS_FILE, "...error: readable socket not expected"));
  176. status = -120; goto on_return;
  177. }
  178. status = 0;
  179. on_return:
  180. if (udp1 != PJ_INVALID_SOCKET)
  181. pj_sock_close(udp1);
  182. if (udp2 != PJ_INVALID_SOCKET)
  183. pj_sock_close(udp2);
  184. return status;
  185. }
  186. #else
  187. /* To prevent warning about "translation unit is empty"
  188. * when this test is disabled.
  189. */
  190. int dummy_select_test;
  191. #endif /* INCLUDE_SELECT_TEST */