multicast.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_arch_networkio.h"
  17. #include "fspr_network_io.h"
  18. #include "fspr_support.h"
  19. #include "fspr_portable.h"
  20. #include "fspr_arch_inherit.h"
  21. #ifdef HAVE_GETIFADDRS
  22. #include <net/if.h>
  23. #include <ifaddrs.h>
  24. #endif
  25. #ifdef HAVE_STRUCT_IPMREQ
  26. /* Only UDP and Raw Sockets can be used for Multicast */
  27. static fspr_status_t mcast_check_type(fspr_socket_t *sock)
  28. {
  29. int type;
  30. fspr_status_t rv;
  31. rv = fspr_socket_type_get(sock, &type);
  32. if (rv != APR_SUCCESS) {
  33. return rv;
  34. }
  35. else if (type == SOCK_DGRAM || type == SOCK_RAW) {
  36. return APR_SUCCESS;
  37. }
  38. else {
  39. return APR_ENOTIMPL;
  40. }
  41. }
  42. static void fill_mip_v4(struct ip_mreq *mip, fspr_sockaddr_t *mcast,
  43. fspr_sockaddr_t *iface)
  44. {
  45. mip->imr_multiaddr = mcast->sa.sin.sin_addr;
  46. if (iface == NULL) {
  47. mip->imr_interface.s_addr = INADDR_ANY;
  48. }
  49. else {
  50. mip->imr_interface = iface->sa.sin.sin_addr;
  51. }
  52. }
  53. static unsigned int find_if_index(const fspr_sockaddr_t *iface)
  54. {
  55. unsigned int index = 0;
  56. #if defined(HAVE_GETIFADDRS) && APR_HAVE_IPV6
  57. struct ifaddrs *ifp, *ifs;
  58. /**
  59. * TODO: getifaddrs is only portable to *BSD and OS X. Using ioctl
  60. * and SIOCGIFCONF is needed for Linux/Solaris support.
  61. *
  62. * There is a wrapper that takes the messy ioctl interface into
  63. * getifaddrs. The license is acceptable, but, It is a fairly large
  64. * chunk of code.
  65. */
  66. if (getifaddrs(&ifs) != 0) {
  67. return 0;
  68. }
  69. for (ifp = ifs; ifp; ifp = ifp->ifa_next) {
  70. if (ifp->ifa_addr != NULL && ifp->ifa_addr->sa_family == AF_INET6) {
  71. if (memcmp(&iface->sa.sin6.sin6_addr,
  72. &ifp->ifa_addr->sa_data[0],
  73. sizeof(iface->sa.sin6.sin6_addr)) == 0) {
  74. index = if_nametoindex(ifp->ifa_name);
  75. break;
  76. }
  77. }
  78. }
  79. freeifaddrs(ifs);
  80. #endif
  81. return index;
  82. }
  83. #if APR_HAVE_IPV6
  84. static void fill_mip_v6(struct ipv6_mreq *mip, const fspr_sockaddr_t *mcast,
  85. const fspr_sockaddr_t *iface)
  86. {
  87. memcpy(&mip->ipv6mr_multiaddr, mcast->ipaddr_ptr,
  88. sizeof(mip->ipv6mr_multiaddr));
  89. if (iface == NULL) {
  90. mip->ipv6mr_interface = 0;
  91. }
  92. else {
  93. mip->ipv6mr_interface = find_if_index(iface);
  94. }
  95. }
  96. #endif
  97. static int sock_is_ipv4(fspr_socket_t *sock)
  98. {
  99. if (sock->local_addr->family == APR_INET)
  100. return 1;
  101. return 0;
  102. }
  103. #if APR_HAVE_IPV6
  104. static int sock_is_ipv6(fspr_socket_t *sock)
  105. {
  106. if (sock->local_addr->family == APR_INET6)
  107. return 1;
  108. return 0;
  109. }
  110. #endif
  111. static fspr_status_t do_mcast(int type, fspr_socket_t *sock,
  112. fspr_sockaddr_t *mcast, fspr_sockaddr_t *iface,
  113. fspr_sockaddr_t *source)
  114. {
  115. struct ip_mreq mip4;
  116. fspr_status_t rv = APR_SUCCESS;
  117. #if APR_HAVE_IPV6
  118. struct ipv6_mreq mip6;
  119. #endif
  120. #if MCAST_JOIN_SOURCE_GROUP
  121. struct group_source_req mip;
  122. int ip_proto;
  123. #endif
  124. rv = mcast_check_type(sock);
  125. if (rv != APR_SUCCESS) {
  126. return rv;
  127. }
  128. if (source != NULL) {
  129. #if MCAST_JOIN_SOURCE_GROUP
  130. if (sock_is_ipv4(sock)) {
  131. ip_proto = IPPROTO_IP;
  132. }
  133. #if APR_HAVE_IPV6
  134. else if (sock_is_ipv6(sock)) {
  135. ip_proto = IPPROTO_IPV6;
  136. }
  137. #endif
  138. else {
  139. return APR_ENOTIMPL;
  140. }
  141. if (type == IP_ADD_MEMBERSHIP)
  142. type = MCAST_JOIN_SOURCE_GROUP;
  143. else if (type == IP_DROP_MEMBERSHIP)
  144. type = MCAST_LEAVE_SOURCE_GROUP;
  145. else
  146. return APR_ENOTIMPL;
  147. mip.gsr_interface = find_if_index(iface);
  148. memcpy(&mip.gsr_group, mcast->ipaddr_ptr, sizeof(mip.gsr_group));
  149. memcpy(&mip.gsr_source, source->ipaddr_ptr, sizeof(mip.gsr_source));
  150. if (setsockopt(sock->socketdes, ip_proto, type, (const void *) &mip,
  151. sizeof(mip)) == -1) {
  152. rv = errno;
  153. }
  154. #else
  155. /* We do not support Source-Specific Multicast. */
  156. return APR_ENOTIMPL;
  157. #endif
  158. }
  159. else {
  160. if (sock_is_ipv4(sock)) {
  161. fill_mip_v4(&mip4, mcast, iface);
  162. if (setsockopt(sock->socketdes, IPPROTO_IP, type,
  163. (const void *) &mip4, sizeof(mip4)) == -1) {
  164. rv = errno;
  165. }
  166. }
  167. #if APR_HAVE_IPV6 && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
  168. else if (sock_is_ipv6(sock)) {
  169. if (type == IP_ADD_MEMBERSHIP) {
  170. type = IPV6_JOIN_GROUP;
  171. }
  172. else if (type == IP_DROP_MEMBERSHIP) {
  173. type = IPV6_LEAVE_GROUP;
  174. }
  175. else {
  176. return APR_ENOTIMPL;
  177. }
  178. fill_mip_v6(&mip6, mcast, iface);
  179. if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
  180. &mip6, sizeof(mip6)) == -1) {
  181. rv = errno;
  182. }
  183. }
  184. #endif
  185. else {
  186. rv = APR_ENOTIMPL;
  187. }
  188. }
  189. return rv;
  190. }
  191. static fspr_status_t do_mcast_opt(int type, fspr_socket_t *sock,
  192. fspr_byte_t value)
  193. {
  194. fspr_status_t rv = APR_SUCCESS;
  195. rv = mcast_check_type(sock);
  196. if (rv != APR_SUCCESS) {
  197. return rv;
  198. }
  199. if (sock_is_ipv4(sock)) {
  200. if (setsockopt(sock->socketdes, IPPROTO_IP, type,
  201. (const void *) &value, sizeof(value)) == -1) {
  202. rv = errno;
  203. }
  204. }
  205. #if APR_HAVE_IPV6
  206. else if (sock_is_ipv6(sock) && type == IP_MULTICAST_LOOP) {
  207. unsigned int loopopt = value;
  208. type = IPV6_MULTICAST_LOOP;
  209. if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
  210. &loopopt, sizeof(loopopt)) == -1) {
  211. rv = errno;
  212. }
  213. }
  214. else if (sock_is_ipv6(sock)) {
  215. if (type == IP_MULTICAST_TTL) {
  216. type = IPV6_MULTICAST_HOPS;
  217. }
  218. else {
  219. return APR_ENOTIMPL;
  220. }
  221. if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
  222. &value, sizeof(value)) == -1) {
  223. rv = errno;
  224. }
  225. }
  226. #endif
  227. else {
  228. rv = APR_ENOTIMPL;
  229. }
  230. return rv;
  231. }
  232. #endif
  233. APR_DECLARE(fspr_status_t) fspr_mcast_join(fspr_socket_t *sock,
  234. fspr_sockaddr_t *join,
  235. fspr_sockaddr_t *iface,
  236. fspr_sockaddr_t *source)
  237. {
  238. #if defined(IP_ADD_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
  239. return do_mcast(IP_ADD_MEMBERSHIP, sock, join, iface, source);
  240. #else
  241. return APR_ENOTIMPL;
  242. #endif
  243. }
  244. APR_DECLARE(fspr_status_t) fspr_mcast_leave(fspr_socket_t *sock,
  245. fspr_sockaddr_t *addr,
  246. fspr_sockaddr_t *iface,
  247. fspr_sockaddr_t *source)
  248. {
  249. #if defined(IP_DROP_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
  250. return do_mcast(IP_DROP_MEMBERSHIP, sock, addr, iface, source);
  251. #else
  252. return APR_ENOTIMPL;
  253. #endif
  254. }
  255. APR_DECLARE(fspr_status_t) fspr_mcast_hops(fspr_socket_t *sock, fspr_byte_t ttl)
  256. {
  257. #if defined(IP_MULTICAST_TTL) && defined(HAVE_STRUCT_IPMREQ)
  258. return do_mcast_opt(IP_MULTICAST_TTL, sock, ttl);
  259. #else
  260. return APR_ENOTIMPL;
  261. #endif
  262. }
  263. APR_DECLARE(fspr_status_t) fspr_mcast_loopback(fspr_socket_t *sock,
  264. fspr_byte_t opt)
  265. {
  266. #if defined(IP_MULTICAST_LOOP) && defined(HAVE_STRUCT_IPMREQ)
  267. return do_mcast_opt(IP_MULTICAST_LOOP, sock, opt);
  268. #else
  269. return APR_ENOTIMPL;
  270. #endif
  271. }
  272. APR_DECLARE(fspr_status_t) fspr_mcast_interface(fspr_socket_t *sock,
  273. fspr_sockaddr_t *iface)
  274. {
  275. #if defined(IP_MULTICAST_IF) && defined(HAVE_STRUCT_IPMREQ)
  276. fspr_status_t rv = APR_SUCCESS;
  277. if (sock_is_ipv4(sock)) {
  278. if (setsockopt(sock->socketdes, IPPROTO_IP, IP_MULTICAST_IF,
  279. (const void *) &iface->sa.sin.sin_addr,
  280. sizeof(iface->sa.sin.sin_addr)) == -1) {
  281. rv = errno;
  282. }
  283. }
  284. #if APR_HAVE_IPV6
  285. else if (sock_is_ipv6(sock)) {
  286. unsigned int idx = find_if_index(iface);
  287. if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  288. &idx, sizeof(idx)) == -1) {
  289. rv = errno;
  290. }
  291. }
  292. #endif
  293. else {
  294. rv = APR_ENOTIMPL;
  295. }
  296. return rv;
  297. #else
  298. return APR_ENOTIMPL;
  299. #endif
  300. }