PageRenderTime 35ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/UNIX/unp/test/test1.c

https://gitlab.com/o1s2/selflrrn
C | 208 lines | 116 code | 44 blank | 48 comment | 41 complexity | c5d95e8b716003709e5dc36158d6b248 MD5 | raw file
  1. /* Authors: W. R. Stevens, B. Fenner, A. M. Rudoff */
  2. #include "test.h"
  3. /*
  4. * Socket test program.
  5. * Try and figure out everything that we can automatically.
  6. * Lines preceded by a + are deviations from 4.4BSD.
  7. * Lines preceded by a ! are fatal errors.
  8. * Lines without a + or ! are just informational.
  9. */
  10. /* allocate globals */
  11. struct sockaddr_in servaddr, cliaddr;
  12. char buff[8192];
  13. int verbose;
  14. /*
  15. * Check whether various header flags are defined.
  16. */
  17. void
  18. header_flags()
  19. {
  20. /* these are all "if not defined" */
  21. #ifndef MSG_DONTROUTE
  22. printf("+ MSG_DONTROUTE not defined\n");
  23. #endif
  24. #ifndef MSG_OOB
  25. printf("+ MSG_OOB not defined\n");
  26. #endif
  27. #ifndef MSG_PEEK
  28. printf("+ MSG_PEEK not defined\n");
  29. #endif
  30. #ifndef MSG_WAITALL
  31. printf("+ MSG_WAITALL not defined\n");
  32. #endif
  33. }
  34. /*
  35. * Check whether we can use sendto() and recvfrom() with a TCP socket.
  36. * Use a different length for each output function, so if it does work,
  37. * we can see it with tcpdump and separate it from the other outputs.
  38. */
  39. void
  40. sendto_01()
  41. {
  42. int sockfd, n;
  43. socklen_t len;
  44. sockfd = TcpSockByAddr("140.252.13.34", 7); /* echo server */
  45. /*
  46. * This also verifies that we can call sendto() on a TCP socket
  47. * if we don't specify a destination address.
  48. */
  49. Sendto(sockfd, "hello", 5, 0, NULL, NULL);
  50. if ( (n = Recvfrom(sockfd, buff, sizeof(buff), 0, NULL, NULL)) != 5)
  51. err_quit("! Recvfrom expected 5");
  52. /*
  53. * Now see what happens when we ask for the server's address.
  54. * Berkeley-derived implementations do not return this (p. 517, tcpipiv2)
  55. * while Solaris does.
  56. */
  57. Sendto(sockfd, "world", 5, 0, NULL, NULL);
  58. len = sizeof(servaddr) * 2; /* that's a lie */
  59. if ( (n = Recvfrom(sockfd, buff, sizeof(buff), 0,
  60. (SA *) &servaddr, &len)) != 5)
  61. err_quit("! Recvfrom expected 5");
  62. if (len != 0) {
  63. err_msg("+ recvfrom on TCP socket returns len = %d for sender's addr",
  64. len);
  65. if (len == sizeof(servaddr))
  66. printf(" recvfrom from %s, port %d\n",
  67. inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
  68. }
  69. Close(sockfd);
  70. /*
  71. * Now try and specify a destination address for sendto() on
  72. * a TCP socket.
  73. */
  74. sockfd = TcpSockByAddr("140.252.13.34", 7); /* echo server */
  75. /* should not work with destination address specified */
  76. n = sendto(sockfd, "hello1", 6, 0, (SA *) &servaddr, sizeof(servaddr));
  77. if (n < 0)
  78. err_ret("sendto on TCP socket specifying dest addr returns error");
  79. else if (n == 6)
  80. #ifdef MSG_EOF /* defined only if T/TCP supported */
  81. err_msg("+ sendto on TCP socket specifying dest addr OK (T/TCP supported)");
  82. #else
  83. err_msg("+ sendto on TCP socket specifying dest addr OK");
  84. #endif
  85. else
  86. err_quit("! sendto on TCP socket specifying dest addr, n = %d", n);
  87. Close(sockfd);
  88. /*
  89. * Now an unconnected UDP socket.
  90. */
  91. sockfd = UdpSockByAddr("140.252.13.34", 7); /* echo server */
  92. /* should not work */
  93. if ( (n = sendto(sockfd, "hello12", 7, 0, (SA *) 0, 0)) >= 0)
  94. err_msg("+ sendto on unconnected UDP without dest addr OK, n = %d", n);
  95. else if (errno != EDESTADDRREQ)
  96. err_ret("+ sendto on unconnected UDP without dest addr, unexpected errno");
  97. /* should not work */
  98. if ( (n = write(sockfd, "hello", 7)) >= 0)
  99. err_msg("+ write on unconnected UDP OK, n = %d", n);
  100. else if (errno != EDESTADDRREQ)
  101. err_ret("+ write on unconnected UDP, unexpected errno");
  102. Close(sockfd);
  103. /*
  104. * Now a connected UDP socket.
  105. */
  106. sockfd = UdpConnSockByAddr("140.252.13.34", 7); /* echo server */
  107. /* should work */
  108. if ( (n = write(sockfd, "hello123", 8)) < 0)
  109. err_sys("! write on connected UDP, n = %d", n);
  110. else if (n != 8)
  111. err_quit("! write on connected UDP, n = %d", n);
  112. /* should work */
  113. if ( (n = sendto(sockfd, "hello1234", 9, 0, (SA *) 0, 0)) < 0)
  114. err_sys("! sendto on connected UDP without dest addr, n = %d", n);
  115. else if (n != 9)
  116. err_quit("! sendto on connected UDP without dest addr, n = %d", n);
  117. /* should not work */
  118. n = sendto(sockfd, "hello12345", 10, 0, (SA *) &servaddr, sizeof(servaddr));
  119. if (n < 0 && errno != EISCONN)
  120. err_ret("+ sendto on connected UDP with dest addr, unexpected errno");
  121. else if (n >= 0)
  122. err_msg("+ sendto on connected UDP with dest addr OK, n = %d", n);
  123. Close(sockfd);
  124. }
  125. /*
  126. * Send a UDP datagram to a server at IP address 1, and look at
  127. * the return address in the response. If the server is multihomed,
  128. * the return address can differ from our original destination address.
  129. */
  130. void
  131. udp_01()
  132. {
  133. }
  134. static void
  135. usage(const char *msg)
  136. {
  137. err_msg(
  138. "options: -v verbose\n"
  139. );
  140. if (msg[0] != 0)
  141. err_quit("%s", msg);
  142. exit(1);
  143. }
  144. int
  145. main(int argc, char **argv)
  146. {
  147. int c;
  148. opterr = 0; /* don't want getopt() writing to stderr */
  149. while ( (c = getopt(argc, argv, "v")) != -1) {
  150. switch (c) {
  151. case 'v':
  152. verbose = 1;
  153. break;
  154. case '?':
  155. usage("unrecognized option");
  156. }
  157. }
  158. if (verbose) printf("header_flags\n");
  159. header_flags();
  160. if (verbose) printf("udp_01\n");
  161. udp_01();
  162. if (verbose) printf("sendto_01\n");
  163. sendto_01();
  164. }