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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_networkio.h"
18 #include "apr_network_io.h"
19 #include "apr_support.h"
20 #include "apr_portable.h"
21 #include "apr_arch_inherit.h"
23 #ifdef HAVE_GETIFADDRS
28 #ifdef HAVE_STRUCT_IPMREQ
29 static void fill_mip_v4(struct ip_mreq *mip, apr_sockaddr_t *mcast,
30 apr_sockaddr_t *iface)
32 mip->imr_multiaddr = mcast->sa.sin.sin_addr;
34 mip->imr_interface.s_addr = INADDR_ANY;
37 mip->imr_interface = iface->sa.sin.sin_addr;
41 /* This function is only interested in AF_INET6 sockets, so a noop
42 * "return 0" implementation for the !APR_HAVE_IPV6 build is
44 static unsigned int find_if_index(const apr_sockaddr_t *iface)
46 unsigned int index = 0;
47 #if defined(HAVE_GETIFADDRS) && APR_HAVE_IPV6
48 struct ifaddrs *ifp, *ifs;
51 * TODO: getifaddrs is only portable to *BSD and OS X. Using ioctl
52 * and SIOCGIFCONF is needed for Linux/Solaris support.
54 * There is a wrapper that takes the messy ioctl interface into
55 * getifaddrs. The license is acceptable, but, It is a fairly large
58 if (getifaddrs(&ifs) != 0) {
62 for (ifp = ifs; ifp; ifp = ifp->ifa_next) {
63 if (ifp->ifa_addr != NULL && ifp->ifa_addr->sa_family == AF_INET6) {
64 if (memcmp(&iface->sa.sin6.sin6_addr,
65 &ifp->ifa_addr->sa_data[0],
66 sizeof(iface->sa.sin6.sin6_addr)) == 0) {
67 index = if_nametoindex(ifp->ifa_name);
79 static void fill_mip_v6(struct ipv6_mreq *mip, const apr_sockaddr_t *mcast,
80 const apr_sockaddr_t *iface)
82 memcpy(&mip->ipv6mr_multiaddr, mcast->ipaddr_ptr,
83 sizeof(mip->ipv6mr_multiaddr));
86 mip->ipv6mr_interface = 0;
89 mip->ipv6mr_interface = find_if_index(iface);
95 static int sock_is_ipv4(apr_socket_t *sock)
97 if (sock->local_addr->family == APR_INET)
103 static int sock_is_ipv6(apr_socket_t *sock)
105 if (sock->local_addr->family == APR_INET6)
111 static apr_status_t do_mcast(int type, apr_socket_t *sock,
112 apr_sockaddr_t *mcast, apr_sockaddr_t *iface,
113 apr_sockaddr_t *source)
116 apr_status_t rv = APR_SUCCESS;
118 struct ipv6_mreq mip6;
120 #ifdef GROUP_FILTER_SIZE
121 struct group_source_req mip;
125 if (source != NULL) {
126 #ifdef GROUP_FILTER_SIZE
127 if (sock_is_ipv4(sock)) {
128 ip_proto = IPPROTO_IP;
131 else if (sock_is_ipv6(sock)) {
132 ip_proto = IPPROTO_IPV6;
139 if (type == IP_ADD_MEMBERSHIP)
140 type = MCAST_JOIN_SOURCE_GROUP;
141 else if (type == IP_DROP_MEMBERSHIP)
142 type = MCAST_LEAVE_SOURCE_GROUP;
146 mip.gsr_interface = find_if_index(iface);
147 memcpy(&mip.gsr_group, mcast->ipaddr_ptr, sizeof(mip.gsr_group));
148 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) {
155 /* We do not support Source-Specific Multicast. */
160 if (sock_is_ipv4(sock)) {
162 fill_mip_v4(&mip4, mcast, iface);
164 if (setsockopt(sock->socketdes, IPPROTO_IP, type,
165 (const void *) &mip4, sizeof(mip4)) == -1) {
169 #if APR_HAVE_IPV6 && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
170 else if (sock_is_ipv6(sock)) {
171 if (type == IP_ADD_MEMBERSHIP) {
172 type = IPV6_JOIN_GROUP;
174 else if (type == IP_DROP_MEMBERSHIP) {
175 type = IPV6_LEAVE_GROUP;
181 fill_mip_v6(&mip6, mcast, iface);
183 if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
184 (const void *) &mip6, sizeof(mip6)) == -1) {
196 /* Set the IP_MULTICAST_TTL or IP_MULTICAST_LOOP option, or IPv6
197 * equivalents, for the socket, to the given value. Note that this
198 * function *only works* for those particular option types. */
199 static apr_status_t do_mcast_opt(int type, apr_socket_t *sock,
202 apr_status_t rv = APR_SUCCESS;
204 if (sock_is_ipv4(sock)) {
205 /* For the IP_MULTICAST_* options, this must be a (char *)
207 if (setsockopt(sock->socketdes, IPPROTO_IP, type,
208 (const void *) &value, sizeof(value)) == -1) {
213 else if (sock_is_ipv6(sock)) {
214 /* For the IPV6_* options, an (int *) pointer must be used. */
217 if (type == IP_MULTICAST_TTL) {
218 type = IPV6_MULTICAST_HOPS;
220 else if (type == IP_MULTICAST_LOOP) {
221 type = IPV6_MULTICAST_LOOP;
227 if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
228 (const void *) &ivalue, sizeof(ivalue)) == -1) {
241 APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
242 apr_sockaddr_t *join,
243 apr_sockaddr_t *iface,
244 apr_sockaddr_t *source)
246 #if defined(IP_ADD_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
247 return do_mcast(IP_ADD_MEMBERSHIP, sock, join, iface, source);
253 APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
254 apr_sockaddr_t *addr,
255 apr_sockaddr_t *iface,
256 apr_sockaddr_t *source)
258 #if defined(IP_DROP_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
259 return do_mcast(IP_DROP_MEMBERSHIP, sock, addr, iface, source);
265 APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock, apr_byte_t ttl)
267 #if defined(IP_MULTICAST_TTL) && defined(HAVE_STRUCT_IPMREQ)
268 return do_mcast_opt(IP_MULTICAST_TTL, sock, ttl);
274 APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
277 #if defined(IP_MULTICAST_LOOP) && defined(HAVE_STRUCT_IPMREQ)
278 return do_mcast_opt(IP_MULTICAST_LOOP, sock, opt);
284 APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
285 apr_sockaddr_t *iface)
287 #if defined(IP_MULTICAST_IF) && defined(HAVE_STRUCT_IPMREQ)
288 apr_status_t rv = APR_SUCCESS;
290 if (sock_is_ipv4(sock)) {
291 if (setsockopt(sock->socketdes, IPPROTO_IP, IP_MULTICAST_IF,
292 (const void *) &iface->sa.sin.sin_addr,
293 sizeof(iface->sa.sin.sin_addr)) == -1) {
298 else if (sock_is_ipv6(sock)) {
299 unsigned int idx = find_if_index(iface);
300 if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_MULTICAST_IF,
301 (const void *) &idx, sizeof(idx)) == -1) {