udp_io.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 Denys Vlasenko
  6. *
  7. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. /*
  11. * This asks kernel to let us know dst addr/port of incoming packets
  12. * We don't check for errors here. Not supported == won't be used
  13. */
  14. void
  15. socket_want_pktinfo(int fd)
  16. {
  17. #ifdef IP_PKTINFO
  18. setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &const_int_1, sizeof(int));
  19. #endif
  20. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  21. setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &const_int_1, sizeof(int));
  22. #endif
  23. }
  24. ssize_t
  25. send_to_from(int fd, void *buf, size_t len, int flags,
  26. const struct sockaddr *to,
  27. const struct sockaddr *from,
  28. socklen_t tolen)
  29. {
  30. #ifndef IP_PKTINFO
  31. return sendto(fd, buf, len, flags, to, tolen);
  32. #else
  33. struct iovec iov[1];
  34. struct msghdr msg;
  35. union {
  36. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  37. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  38. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  39. #endif
  40. } u;
  41. struct cmsghdr* cmsgptr;
  42. if (from->sa_family != AF_INET
  43. #if ENABLE_FEATURE_IPV6
  44. && from->sa_family != AF_INET6
  45. #endif
  46. ) {
  47. /* ANY local address */
  48. return sendto(fd, buf, len, flags, to, tolen);
  49. }
  50. /* man recvmsg and man cmsg is needed to make sense of code below */
  51. iov[0].iov_base = buf;
  52. iov[0].iov_len = len;
  53. memset(&u, 0, sizeof(u));
  54. memset(&msg, 0, sizeof(msg));
  55. msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */
  56. msg.msg_namelen = tolen;
  57. msg.msg_iov = iov;
  58. msg.msg_iovlen = 1;
  59. msg.msg_control = &u;
  60. msg.msg_controllen = sizeof(u);
  61. msg.msg_flags = flags;
  62. cmsgptr = CMSG_FIRSTHDR(&msg);
  63. if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
  64. struct in_pktinfo *pktptr;
  65. cmsgptr->cmsg_level = IPPROTO_IP;
  66. cmsgptr->cmsg_type = IP_PKTINFO;
  67. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  68. pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
  69. /* pktptr->ipi_ifindex = 0; -- already done by memset(cbuf...) */
  70. pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
  71. }
  72. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  73. else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
  74. struct in6_pktinfo *pktptr;
  75. cmsgptr->cmsg_level = IPPROTO_IPV6;
  76. cmsgptr->cmsg_type = IPV6_PKTINFO;
  77. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
  78. pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
  79. /* pktptr->ipi6_ifindex = 0; -- already done by memset(cbuf...) */
  80. pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
  81. }
  82. #endif
  83. msg.msg_controllen = cmsgptr->cmsg_len;
  84. return sendmsg(fd, &msg, flags);
  85. #endif
  86. }
  87. /* NB: this will never set port# in 'to'!
  88. * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
  89. * Typical usage is to preinit 'to' with "default" value
  90. * before calling recv_from_to(). */
  91. ssize_t
  92. recv_from_to(int fd, void *buf, size_t len, int flags,
  93. struct sockaddr *from, struct sockaddr *to,
  94. socklen_t sa_size)
  95. {
  96. #ifndef IP_PKTINFO
  97. return recvfrom(fd, buf, len, flags, from, &sa_size);
  98. #else
  99. /* man recvmsg and man cmsg is needed to make sense of code below */
  100. struct iovec iov[1];
  101. union {
  102. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  103. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  104. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  105. #endif
  106. } u;
  107. struct cmsghdr *cmsgptr;
  108. struct msghdr msg;
  109. ssize_t recv_length;
  110. iov[0].iov_base = buf;
  111. iov[0].iov_len = len;
  112. memset(&msg, 0, sizeof(msg));
  113. msg.msg_name = (struct sockaddr *)from;
  114. msg.msg_namelen = sa_size;
  115. msg.msg_iov = iov;
  116. msg.msg_iovlen = 1;
  117. msg.msg_control = &u;
  118. msg.msg_controllen = sizeof(u);
  119. recv_length = recvmsg(fd, &msg, flags);
  120. if (recv_length < 0)
  121. return recv_length;
  122. /* Here we try to retrieve destination IP and memorize it */
  123. for (cmsgptr = CMSG_FIRSTHDR(&msg);
  124. cmsgptr != NULL;
  125. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)
  126. ) {
  127. if (cmsgptr->cmsg_level == IPPROTO_IP
  128. && cmsgptr->cmsg_type == IP_PKTINFO
  129. ) {
  130. #define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )
  131. to->sa_family = AF_INET;
  132. ((struct sockaddr_in*)to)->sin_addr = pktinfo(cmsgptr)->ipi_addr;
  133. /* ((struct sockaddr_in*)to)->sin_port = 123; */
  134. #undef pktinfo
  135. break;
  136. }
  137. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  138. if (cmsgptr->cmsg_level == IPPROTO_IPV6
  139. && cmsgptr->cmsg_type == IPV6_PKTINFO
  140. ) {
  141. #define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )
  142. to->sa_family = AF_INET6;
  143. ((struct sockaddr_in6*)to)->sin6_addr = pktinfo(cmsgptr)->ipi6_addr;
  144. /* ((struct sockaddr_in6*)to)->sin6_port = 123; */
  145. #undef pktinfo
  146. break;
  147. }
  148. #endif
  149. }
  150. return recv_length;
  151. #endif
  152. }