dgechoaddr.c 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "unp.h"
  2. #undef MAXLINE
  3. #define MAXLINE 20 /* to see datagram truncation */
  4. void
  5. dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
  6. {
  7. int flags;
  8. const int on = 1;
  9. socklen_t len;
  10. ssize_t n;
  11. char mesg[MAXLINE], str[INET6_ADDRSTRLEN];
  12. struct in_addr dstaddr;
  13. #ifdef IP_RECVDSTADDR
  14. Setsockopt(sockfd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on));
  15. #endif
  16. for ( ; ; ) {
  17. len = clilen;
  18. flags = 0;
  19. n = Recvfrom_flags(sockfd, mesg, MAXLINE, &flags,
  20. pcliaddr, &len, &dstaddr);
  21. printf("%d-byte datagram from %s", n, Sock_ntop(pcliaddr, len));
  22. if ((flags & MSG_CTRUNC) == 0)
  23. printf(", to %s", Inet_ntop(AF_INET, &dstaddr, str, sizeof(str)));
  24. #ifdef MSG_TRUNC
  25. if (flags & MSG_TRUNC) printf(" (datagram truncated)");
  26. #endif
  27. #ifdef MSG_BCAST
  28. if (flags & MSG_BCAST) printf(" (broadcast)");
  29. #endif
  30. #ifdef MSG_MCAST
  31. if (flags & MSG_MCAST) printf(" (multicast)");
  32. #endif
  33. printf("\n");
  34. Sendto(sockfd, mesg, n, 0, pcliaddr, clilen);
  35. }
  36. }