socktest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. socktest.c
  5. Abstract:
  6. This module implements an application that tests out the system's socket
  7. functionality.
  8. Author:
  9. Evan Green 6-May-2013
  10. Environment:
  11. User Mode
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <arpa/inet.h>
  18. #include <errno.h>
  19. #include <netinet/in.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/socket.h>
  23. #include <unistd.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. ULONG
  34. TestTransmitThroughput (
  35. ULONG ChunkSize,
  36. ULONG ChunkCount
  37. );
  38. //
  39. // -------------------------------------------------------------------- Globals
  40. //
  41. //
  42. // ------------------------------------------------------------------ Functions
  43. //
  44. int
  45. main (
  46. int ArgumentCount,
  47. char **Arguments
  48. )
  49. /*++
  50. Routine Description:
  51. This routine implements the socket test program.
  52. Arguments:
  53. ArgumentCount - Supplies the number of elements in the arguments array.
  54. Arguments - Supplies an array of strings. The array count is bounded by the
  55. previous parameter, and the strings are null-terminated.
  56. Return Value:
  57. 0 on success.
  58. Non-zero on failure.
  59. --*/
  60. {
  61. return TestTransmitThroughput(64 * 1024, 16);
  62. }
  63. //
  64. // --------------------------------------------------------- Internal Functions
  65. //
  66. ULONG
  67. TestTransmitThroughput (
  68. ULONG ChunkSize,
  69. ULONG ChunkCount
  70. )
  71. /*++
  72. Routine Description:
  73. This routine tests transmitting a large amount of data out of a socket.
  74. Arguments:
  75. ChunkSize - Supplies the size of each buffer passed to the send() function.
  76. ChunkCount - Supplies the number of chunks that will be sent.
  77. Return Value:
  78. Returns the number of failures that occurred in the test.
  79. --*/
  80. {
  81. ULONG ByteIndex;
  82. int BytesSent;
  83. struct sockaddr_in DestinationHost;
  84. ULONG Errors;
  85. ULONG LoopIndex;
  86. int Result;
  87. PCHAR TestSendBuffer;
  88. int TestSocket;
  89. Errors = 0;
  90. TestSendBuffer = NULL;
  91. TestSocket = socket(AF_INET, SOCK_STREAM, 0);
  92. if (TestSocket == -1) {
  93. printf("socket() failed. Errno = %d.\n", errno);
  94. Errors += 1;
  95. goto TestTransmitThroughputEnd;
  96. }
  97. //
  98. // TODO: Replace this with a real function (and not just a hard-coded
  99. // network order IP address.
  100. //
  101. DestinationHost.sin_family = AF_INET;
  102. DestinationHost.sin_port = htons(7653);
  103. DestinationHost.sin_addr.s_addr = (192 << 0) | (168 << 8) | (1 << 16) |
  104. (19 << 24);
  105. //
  106. // Connect to the remote host.
  107. //
  108. printf("Connecting to host...");
  109. Result = connect(TestSocket,
  110. (struct sockaddr *)&DestinationHost,
  111. sizeof(struct sockaddr_in));
  112. if (Result == 0) {
  113. printf("Connected.\n");
  114. } else {
  115. printf("Failed: Return value %d, errno = %d.\n", Result, errno);
  116. Errors += 1;
  117. goto TestTransmitThroughputEnd;
  118. }
  119. //
  120. // Allocate and initialize a big old test buffer.
  121. //
  122. TestSendBuffer = malloc(ChunkSize);
  123. if (TestSendBuffer == NULL) {
  124. printf("Failed to allocate %d bytes.\n", ChunkSize);
  125. Errors += 1;
  126. goto TestTransmitThroughputEnd;
  127. }
  128. for (ByteIndex = 0; ByteIndex < ChunkSize; ByteIndex += 1) {
  129. if ((ByteIndex & 0x1) == 0) {
  130. TestSendBuffer[ByteIndex] = (UCHAR)ByteIndex;
  131. } else {
  132. TestSendBuffer[ByteIndex] = (UCHAR)(ByteIndex >> 8);
  133. }
  134. }
  135. //
  136. // Loop sending data hardcore.
  137. //
  138. for (LoopIndex = 0; LoopIndex < ChunkCount; LoopIndex += 1) {
  139. BytesSent = send(TestSocket, TestSendBuffer, ChunkSize, 0);
  140. if (BytesSent == -1) {
  141. printf("Error: Failed to send chunk. errno = %d.\n", errno);
  142. Errors += 1;
  143. }
  144. if (BytesSent != ChunkSize) {
  145. printf("Error: send() sent only %d of %d bytes.\n",
  146. BytesSent,
  147. ChunkSize);
  148. Errors += 1;
  149. }
  150. //
  151. // At some point stop just stupidly printing out failures and give
  152. // up.
  153. //
  154. if (Errors > 10) {
  155. goto TestTransmitThroughputEnd;
  156. }
  157. }
  158. TestTransmitThroughputEnd:
  159. if (TestSendBuffer != NULL) {
  160. free(TestSendBuffer);
  161. }
  162. close(TestSocket);
  163. printf("TestTransmitThroughput done. %d errors found.\n", Errors);
  164. return Errors;
  165. }