~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/network_io/unix/multicast.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2004-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
 
 
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"
 
22
 
 
23
#ifdef HAVE_GETIFADDRS
 
24
#include <net/if.h>
 
25
#include <ifaddrs.h>
 
26
#endif
 
27
 
 
28
#ifdef HAVE_STRUCT_IPMREQ
 
29
/* Only UDP and Raw Sockets can be used for Multicast */
 
30
static apr_status_t mcast_check_type(apr_socket_t *sock)
 
31
{
 
32
    int type;
 
33
    apr_status_t rv;
 
34
 
 
35
    rv = apr_socket_type_get(sock, &type);
 
36
 
 
37
    if (rv != APR_SUCCESS) {
 
38
        return rv;
 
39
    }
 
40
    else if (type == SOCK_DGRAM || type == SOCK_RAW) {
 
41
        return APR_SUCCESS;
 
42
    }
 
43
    else {
 
44
        return APR_ENOTIMPL;
 
45
    }
 
46
}
 
47
 
 
48
static void fill_mip_v4(struct ip_mreq *mip, apr_sockaddr_t *mcast,
 
49
                        apr_sockaddr_t *iface)
 
50
{
 
51
    mip->imr_multiaddr = mcast->sa.sin.sin_addr;
 
52
    if (iface == NULL) {
 
53
        mip->imr_interface.s_addr = INADDR_ANY;
 
54
    }
 
55
    else {
 
56
        mip->imr_interface = iface->sa.sin.sin_addr;
 
57
    }
 
58
}
 
59
 
 
60
#if APR_HAVE_IPV6
 
61
static unsigned int find_if_index(const apr_sockaddr_t *iface)
 
62
{
 
63
    unsigned int index = 0;
 
64
#ifdef HAVE_GETIFADDRS
 
65
    struct ifaddrs *ifp, *ifs;
 
66
 
 
67
    /**
 
68
     * TODO: getifaddrs is only portable to *BSD and OS X. Using ioctl 
 
69
     *       and SIOCGIFCONF is needed for Linux/Solaris support.
 
70
     *       
 
71
     *       There is a wrapper that takes the messy ioctl interface into 
 
72
     *       getifaddrs. The license is acceptable, but, It is a fairly large 
 
73
     *       chunk of code.
 
74
     */
 
75
    if (getifaddrs(&ifs) != 0) {
 
76
        return 0;
 
77
    }
 
78
 
 
79
    for (ifp = ifs; ifp; ifp = ifp->ifa_next) {
 
80
        if (ifp->ifa_addr != NULL && ifp->ifa_addr->sa_family == AF_INET6) {
 
81
            if (memcmp(&iface->sa.sin6.sin6_addr,
 
82
                       &ifp->ifa_addr->sa_data[0],
 
83
                       sizeof(iface->sa.sin6.sin6_addr)) == 0) {
 
84
                index = if_nametoindex(ifp->ifa_name);
 
85
                break;
 
86
            }
 
87
        }
 
88
    }
 
89
 
 
90
    freeifaddrs(ifs);
 
91
#endif
 
92
    return index;
 
93
}
 
94
 
 
95
static void fill_mip_v6(struct ipv6_mreq *mip, const apr_sockaddr_t *mcast,
 
96
                        const apr_sockaddr_t *iface)
 
97
{
 
98
    memcpy(&mip->ipv6mr_multiaddr, mcast->ipaddr_ptr,
 
99
           sizeof(mip->ipv6mr_multiaddr));
 
100
 
 
101
    if (iface == NULL) {
 
102
        mip->ipv6mr_interface = 0;
 
103
    }
 
104
    else {
 
105
        mip->ipv6mr_interface = find_if_index(iface);
 
106
    }
 
107
}
 
108
 
 
109
#endif
 
110
 
 
111
static int sock_is_ipv4(apr_socket_t *sock)
 
112
{
 
113
    if (sock->local_addr->family == APR_INET)
 
114
        return 1;
 
115
    return 0;
 
116
}
 
117
 
 
118
#if APR_HAVE_IPV6
 
119
static int sock_is_ipv6(apr_socket_t *sock)
 
120
{
 
121
    if (sock->local_addr->family == APR_INET6)
 
122
        return 1;
 
123
    return 0;
 
124
}
 
125
#endif
 
126
 
 
127
static apr_status_t do_mcast(int type, apr_socket_t *sock,
 
128
                             apr_sockaddr_t *mcast, apr_sockaddr_t *iface,
 
129
                             apr_sockaddr_t *source)
 
130
{
 
131
    struct ip_mreq mip4;
 
132
    apr_status_t rv = APR_SUCCESS;
 
133
#if APR_HAVE_IPV6
 
134
    struct ipv6_mreq mip6;
 
135
#endif
 
136
#if MCAST_JOIN_SOURCE_GROUP
 
137
    struct group_source_req mip;
 
138
    int ip_proto;
 
139
#endif
 
140
 
 
141
    rv = mcast_check_type(sock);
 
142
 
 
143
    if (rv != APR_SUCCESS) {
 
144
        return rv;
 
145
    }
 
146
 
 
147
    if (source != NULL) {
 
148
#if MCAST_JOIN_SOURCE_GROUP
 
149
        if (sock_is_ipv4(sock)) {
 
150
            ip_proto = IPPROTO_IP;
 
151
        } 
 
152
#if APR_HAVE_IPV6
 
153
        else if (sock_is_ipv6(sock)) {
 
154
            ip_proto = IPPROTO_IPV6;
 
155
        }
 
156
#endif
 
157
        else {
 
158
            return APR_ENOTIMPL;
 
159
        }
 
160
 
 
161
        if (type == IP_ADD_MEMBERSHIP)
 
162
            type = MCAST_JOIN_SOURCE_GROUP;
 
163
        else if (type == IP_DROP_MEMBERSHIP)
 
164
            type = MCAST_LEAVE_SOURCE_GROUP;
 
165
        else
 
166
            return APR_ENOTIMPL;
 
167
 
 
168
        mip.gsr_interface = find_if_index(iface);
 
169
        memcpy(&mip.gsr_group, mcast->ipaddr_ptr, sizeof(mip.gsr_group));
 
170
        memcpy(&mip.gsr_source, source->ipaddr_ptr, sizeof(mip.gsr_source));
 
171
 
 
172
        if (setsockopt(sock->socketdes, ip_proto, type, (const void *) &mip,
 
173
                       sizeof(mip)) == -1) {
 
174
            rv = errno;
 
175
        }
 
176
#else
 
177
        /* We do not support Source-Specific Multicast. */
 
178
        return APR_ENOTIMPL;
 
179
#endif
 
180
    }
 
181
    else {
 
182
        if (sock_is_ipv4(sock)) {
 
183
 
 
184
            fill_mip_v4(&mip4, mcast, iface);
 
185
 
 
186
            if (setsockopt(sock->socketdes, IPPROTO_IP, type,
 
187
                           (const void *) &mip4, sizeof(mip4)) == -1) {
 
188
                rv = errno;
 
189
            }
 
190
        }
 
191
#if APR_HAVE_IPV6 && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
 
192
        else if (sock_is_ipv6(sock)) {
 
193
            if (type == IP_ADD_MEMBERSHIP) {
 
194
                type = IPV6_JOIN_GROUP;
 
195
            }
 
196
            else if (type == IP_DROP_MEMBERSHIP) {
 
197
                type = IPV6_LEAVE_GROUP;
 
198
            }
 
199
            else {
 
200
                return APR_ENOTIMPL;
 
201
            }
 
202
 
 
203
            fill_mip_v6(&mip6, mcast, iface);
 
204
 
 
205
            if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
 
206
                           &mip6, sizeof(mip6)) == -1) {
 
207
                rv = errno;
 
208
            }
 
209
        }
 
210
#endif
 
211
        else {
 
212
            rv = APR_ENOTIMPL;
 
213
        }
 
214
    }
 
215
    return rv;
 
216
}
 
217
 
 
218
static apr_status_t do_mcast_opt(int type, apr_socket_t *sock,
 
219
                                 apr_byte_t value)
 
220
{
 
221
    apr_status_t rv = APR_SUCCESS;
 
222
 
 
223
    rv = mcast_check_type(sock);
 
224
 
 
225
    if (rv != APR_SUCCESS) {
 
226
        return rv;
 
227
    }
 
228
 
 
229
    if (sock_is_ipv4(sock)) {
 
230
        if (setsockopt(sock->socketdes, IPPROTO_IP, type,
 
231
                       (const void *) &value, sizeof(value)) == -1) {
 
232
            rv = errno;
 
233
        }
 
234
    }
 
235
#if APR_HAVE_IPV6
 
236
    else if (sock_is_ipv6(sock) && type == IP_MULTICAST_LOOP) {
 
237
        unsigned int loopopt = value;
 
238
        type = IPV6_MULTICAST_LOOP;
 
239
        if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
 
240
                       &loopopt, sizeof(loopopt)) == -1) {
 
241
            rv = errno;
 
242
        }
 
243
    }
 
244
    else if (sock_is_ipv6(sock)) {
 
245
        if (type == IP_MULTICAST_TTL) {
 
246
            type = IPV6_MULTICAST_HOPS;
 
247
        }
 
248
        else {
 
249
            return APR_ENOTIMPL;
 
250
        }
 
251
 
 
252
        if (setsockopt(sock->socketdes, IPPROTO_IPV6, type,
 
253
                       &value, sizeof(value)) == -1) {
 
254
            rv = errno;
 
255
        }
 
256
    }
 
257
#endif
 
258
    else {
 
259
        rv = APR_ENOTIMPL;
 
260
    }
 
261
 
 
262
    return rv;
 
263
}
 
264
#endif
 
265
 
 
266
APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
 
267
                                         apr_sockaddr_t *join,
 
268
                                         apr_sockaddr_t *iface,
 
269
                                         apr_sockaddr_t *source)
 
270
{
 
271
#if defined(IP_ADD_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
 
272
    return do_mcast(IP_ADD_MEMBERSHIP, sock, join, iface, source);
 
273
#else
 
274
    return APR_ENOTIMPL;
 
275
#endif
 
276
}
 
277
 
 
278
APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
 
279
                                          apr_sockaddr_t *addr,
 
280
                                          apr_sockaddr_t *iface,
 
281
                                          apr_sockaddr_t *source)
 
282
{
 
283
#if defined(IP_DROP_MEMBERSHIP) && defined(HAVE_STRUCT_IPMREQ)
 
284
    return do_mcast(IP_DROP_MEMBERSHIP, sock, addr, iface, source);
 
285
#else
 
286
    return APR_ENOTIMPL;
 
287
#endif
 
288
}
 
289
 
 
290
APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock, apr_byte_t ttl)
 
291
{
 
292
#if defined(IP_MULTICAST_TTL) && defined(HAVE_STRUCT_IPMREQ)
 
293
    return do_mcast_opt(IP_MULTICAST_TTL, sock, ttl);
 
294
#else
 
295
    return APR_ENOTIMPL;
 
296
#endif
 
297
}
 
298
 
 
299
APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
 
300
                                             apr_byte_t opt)
 
301
{
 
302
#if defined(IP_MULTICAST_LOOP) && defined(HAVE_STRUCT_IPMREQ)
 
303
    return do_mcast_opt(IP_MULTICAST_LOOP, sock, opt);
 
304
#else
 
305
    return APR_ENOTIMPL;
 
306
#endif
 
307
}
 
308
 
 
309
APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
 
310
                                              apr_sockaddr_t *iface)
 
311
{
 
312
#if defined(IP_MULTICAST_IF) && defined(HAVE_STRUCT_IPMREQ)
 
313
    apr_status_t rv = APR_SUCCESS;
 
314
 
 
315
    if (sock_is_ipv4(sock)) {
 
316
        if (setsockopt(sock->socketdes, IPPROTO_IP, IP_MULTICAST_IF,
 
317
                       (const void *) &iface->sa.sin.sin_addr,
 
318
                       sizeof(iface->sa.sin.sin_addr)) == -1) {
 
319
            rv = errno;
 
320
        }
 
321
    }
 
322
#if APR_HAVE_IPV6
 
323
    else if (sock_is_ipv6(sock)) {
 
324
        unsigned int idx = find_if_index(iface);
 
325
        if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_MULTICAST_IF,
 
326
                       &idx, sizeof(idx)) == -1) {
 
327
            rv = errno;
 
328
        }
 
329
    }
 
330
#endif
 
331
    else {
 
332
        rv = APR_ENOTIMPL;
 
333
    }
 
334
    return rv;
 
335
#else
 
336
    return APR_ENOTIMPL;
 
337
#endif
 
338
}