tmoacc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include "nspr.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "plerror.h"
  9. #include "plgetopt.h"
  10. #ifdef DEBUG
  11. #define PORT_INC_DO +100
  12. #else
  13. #define PORT_INC_DO
  14. #endif
  15. #ifdef IS_64
  16. #define PORT_INC_3264 +200
  17. #else
  18. #define PORT_INC_3264
  19. #endif
  20. #define BASE_PORT 9867 PORT_INC_DO PORT_INC_3264
  21. #define DEFAULT_THREADS 1
  22. #define DEFAULT_BACKLOG 10
  23. #define DEFAULT_TIMEOUT 10
  24. #define RANDOM_RANGE 100 /* should be significantly smaller than RAND_MAX */
  25. typedef enum {running, stopped} Status;
  26. typedef struct Shared
  27. {
  28. PRLock *ml;
  29. PRCondVar *cv;
  30. PRBool passed;
  31. PRBool random;
  32. PRFileDesc *debug;
  33. PRIntervalTime timeout;
  34. PRFileDesc *listenSock;
  35. Status status;
  36. } Shared;
  37. static PRIntervalTime Timeout(const Shared *shared)
  38. {
  39. PRIntervalTime timeout = shared->timeout;
  40. if (shared->random)
  41. {
  42. PRIntervalTime half = timeout >> 1; /* one half of the interval */
  43. PRIntervalTime quarter = half >> 1; /* one quarter of the interval */
  44. /* something in [0..timeout / 2) */
  45. PRUint32 random = (rand() % RANDOM_RANGE) * half / RANDOM_RANGE;
  46. timeout = (3 * quarter) + random; /* [75..125)% */
  47. }
  48. return timeout;
  49. } /* Timeout */
  50. static void Accept(void *arg)
  51. {
  52. PRStatus rv;
  53. char *buffer = NULL;
  54. PRNetAddr clientAddr;
  55. Shared *shared = (Shared*)arg;
  56. PRInt32 recv_length = 0, flags = 0;
  57. PRFileDesc *clientSock;
  58. PRIntn toread, byte, bytes, loop = 0;
  59. struct Descriptor {
  60. PRInt32 length;
  61. PRUint32 checksum;
  62. } descriptor;
  63. do
  64. {
  65. PRUint32 checksum = 0;
  66. if (NULL != shared->debug) {
  67. PR_fprintf(shared->debug, "[%d]accepting ... ", loop++);
  68. }
  69. clientSock = PR_Accept(
  70. shared->listenSock, &clientAddr, Timeout(shared));
  71. if (clientSock != NULL)
  72. {
  73. if (NULL != shared->debug) {
  74. PR_fprintf(shared->debug, "reading length ... ");
  75. }
  76. bytes = PR_Recv(
  77. clientSock, &descriptor, sizeof(descriptor),
  78. flags, Timeout(shared));
  79. if (sizeof(descriptor) == bytes)
  80. {
  81. /* and, before doing something stupid ... */
  82. descriptor.length = PR_ntohl(descriptor.length);
  83. descriptor.checksum = PR_ntohl(descriptor.checksum);
  84. if (NULL != shared->debug) {
  85. PR_fprintf(shared->debug, "%d bytes ... ", descriptor.length);
  86. }
  87. toread = descriptor.length;
  88. if (recv_length < descriptor.length)
  89. {
  90. if (NULL != buffer) {
  91. PR_DELETE(buffer);
  92. }
  93. buffer = (char*)PR_MALLOC(descriptor.length);
  94. recv_length = descriptor.length;
  95. }
  96. for (toread = descriptor.length; toread > 0; toread -= bytes)
  97. {
  98. bytes = PR_Recv(
  99. clientSock, &buffer[descriptor.length - toread],
  100. toread, flags, Timeout(shared));
  101. if (-1 == bytes)
  102. {
  103. if (NULL != shared->debug) {
  104. PR_fprintf(shared->debug, "read data failed...");
  105. }
  106. bytes = 0;
  107. }
  108. }
  109. }
  110. else if (NULL != shared->debug)
  111. {
  112. PR_fprintf(shared->debug, "read desciptor failed...");
  113. descriptor.length = -1;
  114. }
  115. if (NULL != shared->debug) {
  116. PR_fprintf(shared->debug, "closing");
  117. }
  118. rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH);
  119. if ((PR_FAILURE == rv) && (NULL != shared->debug))
  120. {
  121. PR_fprintf(shared->debug, " failed");
  122. shared->passed = PR_FALSE;
  123. }
  124. rv = PR_Close(clientSock);
  125. if (PR_FAILURE == rv) if (NULL != shared->debug)
  126. {
  127. PR_fprintf(shared->debug, " failed");
  128. shared->passed = PR_FALSE;
  129. }
  130. if (descriptor.length > 0)
  131. {
  132. for (byte = 0; byte < descriptor.length; ++byte)
  133. {
  134. PRUint32 overflow = checksum & 0x80000000;
  135. checksum = (checksum << 1);
  136. if (0x00000000 != overflow) {
  137. checksum += 1;
  138. }
  139. checksum += buffer[byte];
  140. }
  141. if ((descriptor.checksum != checksum) && (NULL != shared->debug))
  142. {
  143. PR_fprintf(shared->debug, " ... data mismatch");
  144. shared->passed = PR_FALSE;
  145. }
  146. }
  147. else if (0 == descriptor.length)
  148. {
  149. PR_Lock(shared->ml);
  150. shared->status = stopped;
  151. PR_NotifyCondVar(shared->cv);
  152. PR_Unlock(shared->ml);
  153. }
  154. if (NULL != shared->debug) {
  155. PR_fprintf(shared->debug, "\n");
  156. }
  157. }
  158. else
  159. {
  160. if (PR_PENDING_INTERRUPT_ERROR != PR_GetError())
  161. {
  162. if (NULL != shared->debug) {
  163. PL_PrintError("Accept");
  164. }
  165. shared->passed = PR_FALSE;
  166. }
  167. }
  168. } while (running == shared->status);
  169. if (NULL != buffer) {
  170. PR_DELETE(buffer);
  171. }
  172. } /* Accept */
  173. PRIntn Tmoacc(PRIntn argc, char **argv)
  174. {
  175. PRStatus rv;
  176. PRIntn exitStatus;
  177. PRIntn index;
  178. Shared *shared;
  179. PLOptStatus os;
  180. PRThread **thread;
  181. PRNetAddr listenAddr;
  182. PRSocketOptionData sockOpt;
  183. PRIntn timeout = DEFAULT_TIMEOUT;
  184. PRIntn threads = DEFAULT_THREADS;
  185. PRIntn backlog = DEFAULT_BACKLOG;
  186. PRThreadScope thread_scope = PR_LOCAL_THREAD;
  187. PLOptState *opt = PL_CreateOptState(argc, argv, "dGb:t:T:R");
  188. shared = PR_NEWZAP(Shared);
  189. shared->debug = NULL;
  190. shared->passed = PR_TRUE;
  191. shared->random = PR_TRUE;
  192. shared->status = running;
  193. shared->ml = PR_NewLock();
  194. shared->cv = PR_NewCondVar(shared->ml);
  195. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  196. {
  197. if (PL_OPT_BAD == os) {
  198. continue;
  199. }
  200. switch (opt->option)
  201. {
  202. case 'd': /* debug mode */
  203. shared->debug = PR_GetSpecialFD(PR_StandardError);
  204. break;
  205. case 'G': /* use global threads */
  206. thread_scope = PR_GLOBAL_THREAD;
  207. break;
  208. case 'b': /* size of listen backlog */
  209. backlog = atoi(opt->value);
  210. break;
  211. case 't': /* number of threads doing accept */
  212. threads = atoi(opt->value);
  213. break;
  214. case 'T': /* timeout used for network operations */
  215. timeout = atoi(opt->value);
  216. break;
  217. case 'R': /* randomize the timeout values */
  218. shared->random = PR_TRUE;
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. PL_DestroyOptState(opt);
  225. if (0 == threads) {
  226. threads = DEFAULT_THREADS;
  227. }
  228. if (0 == backlog) {
  229. backlog = DEFAULT_BACKLOG;
  230. }
  231. if (0 == timeout) {
  232. timeout = DEFAULT_TIMEOUT;
  233. }
  234. PR_STDIO_INIT();
  235. memset(&listenAddr, 0, sizeof(listenAddr));
  236. rv = PR_InitializeNetAddr(PR_IpAddrAny, BASE_PORT, &listenAddr);
  237. PR_ASSERT(PR_SUCCESS == rv);
  238. shared->timeout = PR_SecondsToInterval(timeout);
  239. /* First bind to the socket */
  240. shared->listenSock = PR_NewTCPSocket();
  241. if (shared->listenSock)
  242. {
  243. sockOpt.option = PR_SockOpt_Reuseaddr;
  244. sockOpt.value.reuse_addr = PR_TRUE;
  245. rv = PR_SetSocketOption(shared->listenSock, &sockOpt);
  246. PR_ASSERT(PR_SUCCESS == rv);
  247. rv = PR_Bind(shared->listenSock, &listenAddr);
  248. if (rv != PR_FAILURE)
  249. {
  250. rv = PR_Listen(shared->listenSock, threads + backlog);
  251. if (PR_SUCCESS == rv)
  252. {
  253. thread = (PRThread**)PR_CALLOC(threads * sizeof(PRThread*));
  254. for (index = 0; index < threads; ++index)
  255. {
  256. thread[index] = PR_CreateThread(
  257. PR_USER_THREAD, Accept, shared,
  258. PR_PRIORITY_NORMAL, thread_scope,
  259. PR_JOINABLE_THREAD, 0);
  260. PR_ASSERT(NULL != thread[index]);
  261. }
  262. PR_Lock(shared->ml);
  263. while (shared->status == running) {
  264. PR_WaitCondVar(shared->cv, PR_INTERVAL_NO_TIMEOUT);
  265. }
  266. PR_Unlock(shared->ml);
  267. for (index = 0; index < threads; ++index)
  268. {
  269. rv = PR_Interrupt(thread[index]);
  270. PR_ASSERT(PR_SUCCESS== rv);
  271. rv = PR_JoinThread(thread[index]);
  272. PR_ASSERT(PR_SUCCESS== rv);
  273. }
  274. PR_DELETE(thread);
  275. }
  276. else
  277. {
  278. if (shared->debug) {
  279. PL_PrintError("Listen");
  280. }
  281. shared->passed = PR_FALSE;
  282. }
  283. }
  284. else
  285. {
  286. if (shared->debug) {
  287. PL_PrintError("Bind");
  288. }
  289. shared->passed = PR_FALSE;
  290. }
  291. PR_Close(shared->listenSock);
  292. }
  293. else
  294. {
  295. if (shared->debug) {
  296. PL_PrintError("Create");
  297. }
  298. shared->passed = PR_FALSE;
  299. }
  300. PR_DestroyCondVar(shared->cv);
  301. PR_DestroyLock(shared->ml);
  302. PR_fprintf(
  303. PR_GetSpecialFD(PR_StandardError), "%s\n",
  304. ((shared->passed) ? "PASSED" : "FAILED"));
  305. exitStatus = (shared->passed) ? 0 : 1;
  306. PR_DELETE(shared);
  307. return exitStatus;
  308. }
  309. int main(int argc, char **argv)
  310. {
  311. return (PR_VersionCheck(PR_VERSION)) ?
  312. PR_Initialize(Tmoacc, argc, argv, 4) : -1;
  313. } /* main */
  314. /* tmoacc */