test1.c 4.8 KB

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