udp_io.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 Denis 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. #ifdef UNUSED
  25. ssize_t
  26. send_to_from(int fd, void *buf, size_t len, int flags,
  27. const struct sockaddr *from, const struct sockaddr *to,
  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. char cbuf[sizeof(struct in_pktinfo)
  36. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  37. | sizeof(struct in6_pktinfo) /* (a|b) is poor man's max(a,b) */
  38. #endif
  39. ];
  40. struct cmsghdr* cmsgptr;
  41. if (from->sa_family != AF_INET
  42. #if ENABLE_FEATURE_IPV6
  43. && from->sa_family != AF_INET6
  44. #endif
  45. ) {
  46. /* ANY local address */
  47. return sendto(fd, buf, len, flags, to, tolen);
  48. }
  49. /* man recvmsg and man cmsg is needed to make sense of code below */
  50. iov[0].iov_base = buf;
  51. iov[0].iov_len = len;
  52. memset(cbuf, 0, sizeof(cbuf));
  53. memset(&msg, 0, sizeof(msg));
  54. msg.msg_name = (void *)(struct sockaddr *)to; /* or compiler will annoy us */
  55. msg.msg_namelen = tolen;
  56. msg.msg_iov = iov;
  57. msg.msg_iovlen = 1;
  58. msg.msg_control = cbuf;
  59. msg.msg_controllen = sizeof(cbuf);
  60. msg.msg_flags = flags;
  61. cmsgptr = CMSG_FIRSTHDR(&msg);
  62. if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
  63. struct in_pktinfo *pktptr;
  64. cmsgptr->cmsg_level = IPPROTO_IP;
  65. cmsgptr->cmsg_type = IP_PKTINFO;
  66. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  67. pktptr = (struct in_pktinfo *)(CMSG_DATA(cmsgptr));
  68. /* pktptr->ipi_ifindex = 0; -- already done by memset(cbuf...) */
  69. pktptr->ipi_spec_dst = ((struct sockaddr_in*)from)->sin_addr;
  70. }
  71. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  72. else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
  73. struct in6_pktinfo *pktptr;
  74. cmsgptr->cmsg_level = IPPROTO_IPV6;
  75. cmsgptr->cmsg_type = IPV6_PKTINFO;
  76. cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
  77. pktptr = (struct in6_pktinfo *)(CMSG_DATA(cmsgptr));
  78. /* pktptr->ipi6_ifindex = 0; -- already done by memset(cbuf...) */
  79. pktptr->ipi6_addr = ((struct sockaddr_in6*)from)->sin6_addr;
  80. }
  81. #endif
  82. return sendmsg(fd, &msg, flags);
  83. #endif
  84. }
  85. #endif /* UNUSED */
  86. /* NB: this will never set port# in 'to'!
  87. * _Only_ IP/IPv6 address part of 'to' is _maybe_ modified.
  88. * Typical usage is to preinit 'to' with "default" value
  89. * before calling recv_from_to(). */
  90. ssize_t
  91. recv_from_to(int fd, void *buf, size_t len, int flags,
  92. struct sockaddr *from, struct sockaddr *to,
  93. socklen_t sa_size)
  94. {
  95. #ifndef IP_PKTINFO
  96. return recvfrom(fd, buf, len, flags, from, &sa_size);
  97. #else
  98. /* man recvmsg and man cmsg is needed to make sense of code below */
  99. struct iovec iov[1];
  100. union {
  101. char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
  102. char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
  103. } u;
  104. struct cmsghdr *cmsgptr;
  105. struct msghdr msg;
  106. socklen_t recv_length;
  107. iov[0].iov_base = buf;
  108. iov[0].iov_len = len;
  109. memset(&msg, 0, sizeof(msg));
  110. msg.msg_name = (struct sockaddr *)from;
  111. msg.msg_namelen = sa_size;
  112. msg.msg_iov = iov;
  113. msg.msg_iovlen = 1;
  114. msg.msg_control = &u;
  115. msg.msg_controllen = sizeof(u);
  116. recv_length = recvmsg(fd, &msg, flags);
  117. if (recv_length < 0)
  118. return recv_length;
  119. /* Here we try to retrieve destination IP and memorize it */
  120. for (cmsgptr = CMSG_FIRSTHDR(&msg);
  121. cmsgptr != NULL;
  122. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)
  123. ) {
  124. if (cmsgptr->cmsg_level == IPPROTO_IP
  125. && cmsgptr->cmsg_type == IP_PKTINFO
  126. ) {
  127. #define pktinfo(cmsgptr) ( (struct in_pktinfo*)(CMSG_DATA(cmsgptr)) )
  128. to->sa_family = AF_INET;
  129. ((struct sockaddr_in*)to)->sin_addr = pktinfo(cmsgptr)->ipi_addr;
  130. /* ((struct sockaddr_in*)to)->sin_port = 123; */
  131. #undef pktinfo
  132. break;
  133. }
  134. #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO)
  135. if (cmsgptr->cmsg_level == IPPROTO_IPV6
  136. && cmsgptr->cmsg_type == IPV6_PKTINFO
  137. ) {
  138. #define pktinfo(cmsgptr) ( (struct in6_pktinfo*)(CMSG_DATA(cmsgptr)) )
  139. to->sa_family = AF_INET6;
  140. ((struct sockaddr_in6*)to)->sin6_addr = pktinfo(cmsgptr)->ipi6_addr;
  141. /* ((struct sockaddr_in6*)to)->sin6_port = 123; */
  142. #undef pktinfo
  143. break;
  144. }
  145. #endif
  146. }
  147. return recv_length;
  148. #endif
  149. }