udp_io.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 FAST_FUNC
  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 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. if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
  65. struct in_pktinfo *pktptr;
  66. cmsgptr->cmsg_level = IPPROTO_IP;
  67. cmsgptr->cmsg_type = IP_PKTINFO;
  68. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  69. pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
  70. /*pktptr->ipi_ifindex = 0; -- already done by memset(u...) */
  71. /* In general, CMSG_DATA() can be unaligned, but in this case
  72. * we know for sure it is sufficiently aligned:
  73. * CMSG_FIRSTHDR simply returns &u above,
  74. * and CMSG_DATA returns &u + size_t + int + int.
  75. * Thus direct assignment is ok:
  76. */
  77. pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
  78. }
  79. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  80. else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
  81. struct in6_pktinfo *pktptr;
  82. cmsgptr->cmsg_level = IPPROTO_IPV6;
  83. cmsgptr->cmsg_type = IPV6_PKTINFO;
  84. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
  85. pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
  86. /* pktptr->ipi6_ifindex = 0; -- already done by memset(u...) */
  87. pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
  88. }
  89. # endif
  90. msg.msg_controllen = cmsgptr->cmsg_len;
  91. return sendmsg(fd, &msg, flags);
  92. #endif
  93. }
  94. /* NB: this will never set port# in 'to'!
  95. * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
  96. * Typical usage is to preinit 'to' with "default" value
  97. * before calling recv_from_to(). */
  98. ssize_t FAST_FUNC
  99. recv_from_to(int fd, void *buf, size_t len, int flags,
  100. struct sockaddr *from, struct sockaddr *to,
  101. socklen_t sa_size)
  102. {
  103. #ifndef IP_PKTINFO
  104. (void)to; /* suppress "unused to" warning */
  105. return recvfrom(fd, buf, len, flags, from, &sa_size);
  106. #else
  107. /* man recvmsg and man cmsg is needed to make sense of code below */
  108. struct iovec iov[1];
  109. union {
  110. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  111. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  112. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  113. # endif
  114. } u;
  115. struct cmsghdr *cmsgptr;
  116. struct msghdr msg;
  117. ssize_t recv_length;
  118. iov[0].iov_base = buf;
  119. iov[0].iov_len = len;
  120. memset(&msg, 0, sizeof(msg));
  121. msg.msg_name = (struct sockaddr *)from;
  122. msg.msg_namelen = sa_size;
  123. msg.msg_iov = iov;
  124. msg.msg_iovlen = 1;
  125. msg.msg_control = &u;
  126. msg.msg_controllen = sizeof(u);
  127. recv_length = recvmsg(fd, &msg, flags);
  128. if (recv_length < 0)
  129. return recv_length;
  130. # define to4 ((struct sockaddr_in*)to)
  131. # define to6 ((struct sockaddr_in6*)to)
  132. /* Here we try to retrieve destination IP and memorize it */
  133. for (cmsgptr = CMSG_FIRSTHDR(&msg);
  134. cmsgptr != NULL;
  135. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)
  136. ) {
  137. if (cmsgptr->cmsg_level == IPPROTO_IP
  138. && cmsgptr->cmsg_type == IP_PKTINFO
  139. ) {
  140. # define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )
  141. to->sa_family = AF_INET;
  142. /*to4->sin_addr = pktinfo(cmsgptr)->ipi_addr; - may be unaligned */
  143. memcpy(&to4->sin_addr, &pktinfo(cmsgptr)->ipi_addr, sizeof(to4->sin_addr));
  144. /*to4->sin_port = 123; - this data is not supplied by kernel */
  145. # undef pktinfo
  146. break;
  147. }
  148. # if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  149. if (cmsgptr->cmsg_level == IPPROTO_IPV6
  150. && cmsgptr->cmsg_type == IPV6_PKTINFO
  151. ) {
  152. # define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )
  153. to->sa_family = AF_INET6;
  154. /*to6->sin6_addr = pktinfo(cmsgptr)->ipi6_addr; - may be unaligned */
  155. memcpy(&to6->sin6_addr, &pktinfo(cmsgptr)->ipi6_addr, sizeof(to6->sin6_addr));
  156. /*to6->sin6_port = 123; */
  157. # undef pktinfo
  158. break;
  159. }
  160. # endif
  161. }
  162. return recv_length;
  163. #endif
  164. }