udpserv03.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* include udpserv1 */
  2. #include "unpifi.h"
  3. void mydg_echo(int, SA *, socklen_t, SA *);
  4. int
  5. main(int argc, char **argv)
  6. {
  7. int sockfd;
  8. const int on = 1;
  9. pid_t pid;
  10. struct ifi_info *ifi, *ifihead;
  11. struct sockaddr_in *sa, cliaddr, wildaddr;
  12. for (ifihead = ifi = Get_ifi_info(AF_INET, 1);
  13. ifi != NULL; ifi = ifi->ifi_next) {
  14. /*4bind unicast address */
  15. sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  16. Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  17. sa = (struct sockaddr_in *) ifi->ifi_addr;
  18. sa->sin_family = AF_INET;
  19. sa->sin_port = htons(SERV_PORT);
  20. Bind(sockfd, (SA *) sa, sizeof(*sa));
  21. printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
  22. if ( (pid = Fork()) == 0) { /* child */
  23. mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
  24. exit(0); /* never executed */
  25. }
  26. /* end udpserv1 */
  27. /* include udpserv2 */
  28. if (ifi->ifi_flags & IFF_BROADCAST) {
  29. /* 4try to bind broadcast address */
  30. sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  31. Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  32. sa = (struct sockaddr_in *) ifi->ifi_brdaddr;
  33. sa->sin_family = AF_INET;
  34. sa->sin_port = htons(SERV_PORT);
  35. if (bind(sockfd, (SA *) sa, sizeof(*sa)) < 0) {
  36. if (errno == EADDRINUSE) {
  37. printf("EADDRINUSE: %s\n",
  38. Sock_ntop((SA *) sa, sizeof(*sa)));
  39. Close(sockfd);
  40. continue;
  41. } else
  42. err_sys("bind error for %s",
  43. Sock_ntop((SA *) sa, sizeof(*sa)));
  44. }
  45. printf("bound %s\n", Sock_ntop((SA *) sa, sizeof(*sa)));
  46. if ( (pid = Fork()) == 0) { /* child */
  47. mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr),
  48. (SA *) sa);
  49. exit(0); /* never executed */
  50. }
  51. }
  52. }
  53. /* end udpserv2 */
  54. /* include udpserv3 */
  55. /* 4bind wildcard address */
  56. sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  57. Setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  58. bzero(&wildaddr, sizeof(wildaddr));
  59. wildaddr.sin_family = AF_INET;
  60. wildaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  61. wildaddr.sin_port = htons(SERV_PORT);
  62. Bind(sockfd, (SA *) &wildaddr, sizeof(wildaddr));
  63. printf("bound %s\n", Sock_ntop((SA *) &wildaddr, sizeof(wildaddr)));
  64. if ( (pid = Fork()) == 0) { /* child */
  65. mydg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr), (SA *) sa);
  66. exit(0); /* never executed */
  67. }
  68. exit(0);
  69. }
  70. /* end udpserv3 */
  71. /* include mydg_echo */
  72. void
  73. mydg_echo(int sockfd, SA *pcliaddr, socklen_t clilen, SA *myaddr)
  74. {
  75. int n;
  76. char mesg[MAXLINE];
  77. socklen_t len;
  78. for ( ; ; ) {
  79. len = clilen;
  80. n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
  81. printf("child %d, datagram from %s", getpid(),
  82. Sock_ntop(pcliaddr, len));
  83. printf(", to %s\n", Sock_ntop(myaddr, clilen));
  84. Sendto(sockfd, mesg, n, 0, pcliaddr, len);
  85. }
  86. }
  87. /* end mydg_echo */