acceptreademu.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * This test is the same as acceptread.c except that it uses the
  7. * emulated acceptread method instead of the regular acceptread.
  8. */
  9. #include <prio.h>
  10. #include <prprf.h>
  11. #include <prinit.h>
  12. #include <prnetdb.h>
  13. #include <prinrval.h>
  14. #include <prthread.h>
  15. #include <pprio.h>
  16. #include <plerror.h>
  17. #include <stdlib.h>
  18. #ifdef DEBUG
  19. #define PORT_INC_DO +100
  20. #else
  21. #define PORT_INC_DO
  22. #endif
  23. #ifdef IS_64
  24. #define PORT_INC_3264 +200
  25. #else
  26. #define PORT_INC_3264
  27. #endif
  28. #define DEFAULT_PORT 12273 PORT_INC_DO PORT_INC_3264
  29. #define GET "GET / HTTP/1.0\n\n"
  30. static PRFileDesc *std_out, *err_out;
  31. static PRIntervalTime write_dally, accept_timeout;
  32. static PRDescIdentity emu_layer_ident;
  33. static PRIOMethods emu_layer_methods;
  34. /* the acceptread method in emu_layer_methods */
  35. static PRInt32 PR_CALLBACK emu_AcceptRead(PRFileDesc *sd, PRFileDesc **nd,
  36. PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout)
  37. {
  38. return PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout);
  39. }
  40. static PRStatus PrintAddress(const PRNetAddr* address)
  41. {
  42. char buffer[100];
  43. PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
  44. if (PR_FAILURE == rv) {
  45. PL_FPrintError(err_out, "PR_NetAddrToString");
  46. }
  47. else PR_fprintf(
  48. std_out, "Accepted connection from (0x%p)%s:%d\n",
  49. address, buffer, address->inet.port);
  50. return rv;
  51. } /* PrintAddress */
  52. static void ConnectingThread(void *arg)
  53. {
  54. PRInt32 nbytes;
  55. char buf[1024];
  56. PRFileDesc *sock;
  57. PRNetAddr peer_addr, *addr;
  58. addr = (PRNetAddr*)arg;
  59. sock = PR_NewTCPSocket();
  60. if (sock == NULL)
  61. {
  62. PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
  63. PR_ProcessExit(1);
  64. }
  65. if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
  66. {
  67. PL_FPrintError(err_out, "PR_Connect (client) failed");
  68. PR_ProcessExit(1);
  69. }
  70. if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
  71. {
  72. PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
  73. PR_ProcessExit(1);
  74. }
  75. /*
  76. ** Then wait between the connection coming up and sending the expected
  77. ** data. At some point in time, the server should fail due to a timeou
  78. ** on the AcceptRead() operation, which according to the document is
  79. ** only due to the read() portion.
  80. */
  81. PR_Sleep(write_dally);
  82. nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
  83. if (nbytes == -1) {
  84. PL_FPrintError(err_out, "PR_Send (client) failed");
  85. }
  86. nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
  87. if (nbytes == -1) {
  88. PL_FPrintError(err_out, "PR_Recv (client) failed");
  89. }
  90. else
  91. {
  92. PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
  93. buf[sizeof(buf) - 1] = '\0';
  94. PR_fprintf(std_out, "%s\n", buf);
  95. }
  96. if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH)) {
  97. PL_FPrintError(err_out, "PR_Shutdown (client) failed");
  98. }
  99. if (PR_FAILURE == PR_Close(sock)) {
  100. PL_FPrintError(err_out, "PR_Close (client) failed");
  101. }
  102. return;
  103. } /* ConnectingThread */
  104. #define BUF_SIZE 117
  105. static void AcceptingThread(void *arg)
  106. {
  107. PRStatus rv;
  108. PRInt32 bytes;
  109. PRSize buf_size = BUF_SIZE;
  110. PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
  111. PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
  112. PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
  113. PRFileDesc *layer;
  114. PRSocketOptionData sock_opt;
  115. if (NULL == listen_sock)
  116. {
  117. PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
  118. PR_ProcessExit(1);
  119. }
  120. layer = PR_CreateIOLayerStub(emu_layer_ident, &emu_layer_methods);
  121. if (NULL == layer)
  122. {
  123. PL_FPrintError(err_out, "PR_CreateIOLayerStub (server) failed");
  124. PR_ProcessExit(1);
  125. }
  126. if (PR_PushIOLayer(listen_sock, PR_TOP_IO_LAYER, layer) == PR_FAILURE)
  127. {
  128. PL_FPrintError(err_out, "PR_PushIOLayer (server) failed");
  129. PR_ProcessExit(1);
  130. }
  131. sock_opt.option = PR_SockOpt_Reuseaddr;
  132. sock_opt.value.reuse_addr = PR_TRUE;
  133. rv = PR_SetSocketOption(listen_sock, &sock_opt);
  134. if (PR_FAILURE == rv)
  135. {
  136. PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
  137. PR_ProcessExit(1);
  138. }
  139. rv = PR_Bind(listen_sock, listen_addr);
  140. if (PR_FAILURE == rv)
  141. {
  142. PL_FPrintError(err_out, "PR_Bind (server) failed");
  143. PR_ProcessExit(1);
  144. }
  145. rv = PR_Listen(listen_sock, 10);
  146. if (PR_FAILURE == rv)
  147. {
  148. PL_FPrintError(err_out, "PR_Listen (server) failed");
  149. PR_ProcessExit(1);
  150. }
  151. bytes = PR_AcceptRead(
  152. listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
  153. if (-1 == bytes) {
  154. PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
  155. }
  156. else
  157. {
  158. PrintAddress(accept_addr);
  159. PR_fprintf(
  160. std_out, "(Server) read [0x%p..0x%p) %s\n",
  161. buf, &buf[BUF_SIZE], buf);
  162. bytes = PR_Write(accept_sock, buf, bytes);
  163. rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
  164. if (PR_FAILURE == rv) {
  165. PL_FPrintError(err_out, "PR_Shutdown (server) failed");
  166. }
  167. }
  168. if (-1 != bytes)
  169. {
  170. rv = PR_Close(accept_sock);
  171. if (PR_FAILURE == rv) {
  172. PL_FPrintError(err_out, "PR_Close (server) failed");
  173. }
  174. }
  175. rv = PR_Close(listen_sock);
  176. if (PR_FAILURE == rv) {
  177. PL_FPrintError(err_out, "PR_Close (server) failed");
  178. }
  179. } /* AcceptingThread */
  180. int main(int argc, char **argv)
  181. {
  182. PRHostEnt he;
  183. PRStatus status;
  184. PRIntn next_index;
  185. PRUint16 port_number;
  186. char netdb_buf[PR_NETDB_BUF_SIZE];
  187. PRNetAddr client_addr, server_addr;
  188. PRThread *client_thread, *server_thread;
  189. PRIntervalTime delta = PR_MillisecondsToInterval(500);
  190. err_out = PR_STDERR;
  191. std_out = PR_STDOUT;
  192. accept_timeout = PR_SecondsToInterval(2);
  193. emu_layer_ident = PR_GetUniqueIdentity("Emulated AcceptRead");
  194. emu_layer_methods = *PR_GetDefaultIOMethods();
  195. emu_layer_methods.acceptread = emu_AcceptRead;
  196. if (argc != 2 && argc != 3) {
  197. port_number = DEFAULT_PORT;
  198. }
  199. else {
  200. port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
  201. }
  202. status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
  203. if (PR_SUCCESS != status)
  204. {
  205. PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
  206. PR_ProcessExit(1);
  207. }
  208. if (argc < 3)
  209. {
  210. status = PR_InitializeNetAddr(
  211. PR_IpAddrLoopback, port_number, &client_addr);
  212. if (PR_SUCCESS != status)
  213. {
  214. PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
  215. PR_ProcessExit(1);
  216. }
  217. }
  218. else
  219. {
  220. status = PR_GetHostByName(
  221. argv[1], netdb_buf, sizeof(netdb_buf), &he);
  222. if (status == PR_FAILURE)
  223. {
  224. PL_FPrintError(err_out, "PR_GetHostByName failed");
  225. PR_ProcessExit(1);
  226. }
  227. next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
  228. if (next_index == -1)
  229. {
  230. PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
  231. PR_ProcessExit(1);
  232. }
  233. }
  234. for (
  235. write_dally = 0;
  236. write_dally < accept_timeout + (2 * delta);
  237. write_dally += delta)
  238. {
  239. PR_fprintf(
  240. std_out, "Testing w/ write_dally = %d msec\n",
  241. PR_IntervalToMilliseconds(write_dally));
  242. server_thread = PR_CreateThread(
  243. PR_USER_THREAD, AcceptingThread, &server_addr,
  244. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  245. if (server_thread == NULL)
  246. {
  247. PL_FPrintError(err_out, "PR_CreateThread (server) failed");
  248. PR_ProcessExit(1);
  249. }
  250. PR_Sleep(delta); /* let the server pot thicken */
  251. client_thread = PR_CreateThread(
  252. PR_USER_THREAD, ConnectingThread, &client_addr,
  253. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  254. if (client_thread == NULL)
  255. {
  256. PL_FPrintError(err_out, "PR_CreateThread (client) failed");
  257. PR_ProcessExit(1);
  258. }
  259. if (PR_JoinThread(client_thread) == PR_FAILURE) {
  260. PL_FPrintError(err_out, "PR_JoinThread (client) failed");
  261. }
  262. if (PR_JoinThread(server_thread) == PR_FAILURE) {
  263. PL_FPrintError(err_out, "PR_JoinThread (server) failed");
  264. }
  265. }
  266. return 0;
  267. }