udp_io.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  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 FAST_FUNC
  15. socket_want_pktinfo(int fd UNUSED_PARAM)
  16. {
  17. #ifdef IP_PKTINFO
  18. setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO);
  19. #endif
  20. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  21. setsockopt_1(fd, IPPROTO_IPV6, IPV6_PKTINFO);
  22. #endif
  23. }
  24. ssize_t FAST_FUNC
  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. (void)from; /* suppress "unused from" warning */
  32. return sendto(fd, buf, len, flags, to, tolen);
  33. #else
  34. struct iovec iov[1];
  35. struct msghdr msg;
  36. union {
  37. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  38. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  39. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  40. # endif
  41. } u;
  42. struct cmsghdr* cmsgptr;
  43. if (from->sa_family != AF_INET
  44. # if ENABLE_FEATURE_IPV6
  45. && from->sa_family != AF_INET6
  46. # endif
  47. ) {
  48. /* ANY local address */
  49. return sendto(fd, buf, len, flags, to, tolen);
  50. }
  51. /* man recvmsg and man cmsg is needed to make sense of code below */
  52. iov[0].iov_base = buf;
  53. iov[0].iov_len = len;
  54. memset(&u, 0, sizeof(u));
  55. memset(&msg, 0, sizeof(msg));
  56. msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */
  57. msg.msg_namelen = tolen;
  58. msg.msg_iov = iov;
  59. msg.msg_iovlen = 1;
  60. msg.msg_control = &u;
  61. msg.msg_controllen = sizeof(u);
  62. msg.msg_flags = flags;
  63. cmsgptr = CMSG_FIRSTHDR(&msg);
  64. /*
  65. * Users report that to->sa_family can be AF_INET6 too,
  66. * if "to" was acquired by recv_from_to(). IOW: recv_from_to()
  67. * was seen showing IPv6 "from" even when the destination
  68. * of received packet (our local address) was IPv4.
  69. */
  70. if (/* to->sa_family == AF_INET && */ from->sa_family == AF_INET) {
  71. struct in_pktinfo *pktptr;
  72. cmsgptr->cmsg_level = IPPROTO_IP;
  73. cmsgptr->cmsg_type = IP_PKTINFO;
  74. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  75. pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
  76. /*pktptr->ipi_ifindex = 0; -- already done by memset(u...) */
  77. /* In general, CMSG_DATA() can be unaligned, but in this case
  78. * we know for sure it is sufficiently aligned:
  79. * CMSG_FIRSTHDR simply returns &u above,
  80. * and CMSG_DATA returns &u + size_t + int + int.
  81. * Thus direct assignment is ok:
  82. */
  83. pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
  84. }
  85. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  86. else if (/* to->sa_family == AF_INET6 && */ from->sa_family == AF_INET6) {
  87. struct in6_pktinfo *pktptr;
  88. cmsgptr->cmsg_level = IPPROTO_IPV6;
  89. cmsgptr->cmsg_type = IPV6_PKTINFO;
  90. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
  91. pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
  92. /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */
  93. pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
  94. }
  95. # endif
  96. msg.msg_controllen = cmsgptr->cmsg_len;
  97. return sendmsg(fd, &msg, flags);
  98. #endif
  99. }
  100. /* NB: this will never set port# in 'to'!
  101. * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
  102. * Typical usage is to preinit 'to' with "default" value
  103. * before calling recv_from_to(). */
  104. ssize_t FAST_FUNC
  105. recv_from_to(int fd, void *buf, size_t len, int flags,
  106. struct sockaddr *from, struct sockaddr *to,
  107. socklen_t sa_size)
  108. {
  109. #ifndef IP_PKTINFO
  110. (void)to; /* suppress "unused to" warning */
  111. return recvfrom(fd, buf, len, flags, from, &sa_size);
  112. #else
  113. /* man recvmsg and man cmsg is needed to make sense of code below */
  114. struct iovec iov[1];
  115. union {
  116. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  117. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  118. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  119. # endif
  120. } u;
  121. struct cmsghdr *cmsgptr;
  122. struct msghdr msg;
  123. ssize_t recv_length;
  124. iov[0].iov_base = buf;
  125. iov[0].iov_len = len;
  126. memset(&msg, 0, sizeof(msg));
  127. msg.msg_name = (struct sockaddr *)from;
  128. msg.msg_namelen = sa_size;
  129. msg.msg_iov = iov;
  130. msg.msg_iovlen = 1;
  131. msg.msg_control = &u;
  132. msg.msg_controllen = sizeof(u);
  133. recv_length = recvmsg(fd, &msg, flags);
  134. if (recv_length < 0)
  135. return recv_length;
  136. # define to4 ((struct sockaddr_in*)to)
  137. # define to6 ((struct sockaddr_in6*)to)
  138. /* Here we try to retrieve destination IP and memorize it */
  139. for (cmsgptr = CMSG_FIRSTHDR(&msg);
  140. cmsgptr != NULL;
  141. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)
  142. ) {
  143. if (cmsgptr->cmsg_level == IPPROTO_IP
  144. && cmsgptr->cmsg_type == IP_PKTINFO
  145. ) {
  146. const int IPI_ADDR_OFF = offsetof(struct in_pktinfo, ipi_addr);
  147. to->sa_family = AF_INET;
  148. /*# define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
  149. /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */
  150. memcpy(&to4->sin_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI_ADDR_OFF, sizeof(to4->sin_addr));
  151. /*to4->sin_port = 123; - this data is not supplied by kernel */
  152. break;
  153. }
  154. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  155. if (cmsgptr->cmsg_level == IPPROTO_IPV6
  156. && cmsgptr->cmsg_type == IPV6_PKTINFO
  157. ) {
  158. const int IPI6_ADDR_OFF = offsetof(struct in6_pktinfo, ipi6_addr);
  159. to->sa_family = AF_INET6;
  160. /*# define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )*/
  161. /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */
  162. memcpy(&to6->sin6_addr, (char*)(CMSG_DATA(cmsgptr)) + IPI6_ADDR_OFF, sizeof(to6->sin6_addr));
  163. /*to6->sin6_port = 123; */
  164. break;
  165. }
  166. # endif
  167. }
  168. return recv_length;
  169. #endif
  170. }