1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
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_strings.h"
21
#include "apr_strings.h"
22
#include "apr_private.h"
28
#define APR_WANT_STRFUNC
31
struct apr_ipsubnet_t {
34
apr_uint32_t sub[4]; /* big enough for IPv4 and IPv6 addresses */
42
#if !defined(NETWARE) && !defined(WIN32)
43
#ifdef HAVE_SET_H_ERRNO
44
#define SET_H_ERRNO(newval) set_h_errno(newval)
46
#define SET_H_ERRNO(newval) h_errno = (newval)
49
#define SET_H_ERRNO(newval)
52
#if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
53
defined(HAVE_GETHOSTBYNAME_R)
54
/* This is the maximum size that may be returned from the reentrant
55
* gethostbyname_r function. If the system tries to use more, it
56
* should return ERANGE.
58
#define GETHOSTBYNAME_BUFLEN 512
62
/* XXX: BS solution. Need an HAVE_GETSERVBYNAME and actually
63
* do something here, to provide the obvious proto mappings.
65
static void *getservbyname(const char *name, const char *proto)
71
static apr_status_t get_local_addr(apr_socket_t *sock)
73
sock->local_addr->salen = sizeof(sock->local_addr->sa);
74
if (getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa,
75
&sock->local_addr->salen) < 0) {
76
return apr_get_netos_error();
79
sock->local_port_unknown = sock->local_interface_unknown = 0;
80
/* XXX assumes sin_port and sin6_port at same offset */
81
sock->local_addr->port = ntohs(sock->local_addr->sa.sin.sin_port);
86
static apr_status_t get_remote_addr(apr_socket_t *sock)
88
sock->remote_addr->salen = sizeof(sock->remote_addr->sa);
89
if (getpeername(sock->socketdes, (struct sockaddr *)&sock->remote_addr->sa,
90
&sock->remote_addr->salen) < 0) {
91
return apr_get_netos_error();
94
sock->remote_addr_unknown = 0;
95
/* XXX assumes sin_port and sin6_port at same offset */
96
sock->remote_addr->port = ntohs(sock->remote_addr->sa.sin.sin_port);
101
APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
102
apr_sockaddr_t *sockaddr)
104
*addr = apr_palloc(sockaddr->pool, sockaddr->addr_str_len);
105
apr_inet_ntop(sockaddr->family,
106
sockaddr->ipaddr_ptr,
108
sockaddr->addr_str_len);
110
if (sockaddr->family == AF_INET6 &&
111
IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sockaddr->ipaddr_ptr)) {
112
/* This is an IPv4-mapped IPv6 address; drop the leading
113
* part of the address string so we're left with the familiar
116
*addr += strlen("::ffff:");
122
void apr_sockaddr_vars_set(apr_sockaddr_t *addr, int family, apr_port_t port)
124
addr->family = family;
125
addr->sa.sin.sin_family = family;
127
/* XXX IPv6: assumes sin_port and sin6_port at same offset */
128
addr->sa.sin.sin_port = htons(port);
132
if (family == APR_INET) {
133
addr->salen = sizeof(struct sockaddr_in);
134
addr->addr_str_len = 16;
135
addr->ipaddr_ptr = &(addr->sa.sin.sin_addr);
136
addr->ipaddr_len = sizeof(struct in_addr);
139
else if (family == APR_INET6) {
140
addr->salen = sizeof(struct sockaddr_in6);
141
addr->addr_str_len = 46;
142
addr->ipaddr_ptr = &(addr->sa.sin6.sin6_addr);
143
addr->ipaddr_len = sizeof(struct in6_addr);
148
APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
149
apr_interface_e which,
152
if (which == APR_LOCAL) {
153
if (sock->local_interface_unknown || sock->local_port_unknown) {
154
apr_status_t rv = get_local_addr(sock);
156
if (rv != APR_SUCCESS) {
160
*sa = sock->local_addr;
162
else if (which == APR_REMOTE) {
163
if (sock->remote_addr_unknown) {
164
apr_status_t rv = get_remote_addr(sock);
166
if (rv != APR_SUCCESS) {
170
*sa = sock->remote_addr;
179
APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
185
const char *ch, *lastchar;
189
*addr = NULL; /* assume not specified */
190
*scope_id = NULL; /* assume not specified */
191
*port = 0; /* assume not specified */
193
/* First handle the optional port number. That may be all that
194
* is specified in the string.
196
ch = lastchar = str + strlen(str) - 1;
197
while (ch >= str && apr_isdigit(*ch)) {
201
if (ch < str) { /* Entire string is the port. */
202
big_port = atoi(str);
203
if (big_port < 1 || big_port > 65535) {
210
if (*ch == ':' && ch < lastchar) { /* host and port number specified */
211
if (ch == str) { /* string starts with ':' -- bad */
214
big_port = atoi(ch + 1);
215
if (big_port < 1 || big_port > 65535) {
222
/* now handle the hostname */
223
addrlen = lastchar - str + 1;
225
/* XXX we don't really have to require APR_HAVE_IPV6 for this;
226
* just pass char[] for ipaddr (so we don't depend on struct in6_addr)
227
* and always define APR_INET6
231
const char *end_bracket = memchr(str, ']', addrlen);
232
struct in6_addr ipaddr;
233
const char *scope_delim;
235
if (!end_bracket || end_bracket != lastchar) {
240
/* handle scope id; this is the only context where it is allowed */
241
scope_delim = memchr(str, '%', addrlen);
243
if (scope_delim == end_bracket - 1) { /* '%' without scope id */
247
addrlen = scope_delim - str - 1;
248
*scope_id = apr_palloc(p, end_bracket - scope_delim);
249
memcpy(*scope_id, scope_delim + 1, end_bracket - scope_delim - 1);
250
(*scope_id)[end_bracket - scope_delim - 1] = '\0';
253
addrlen = addrlen - 2; /* minus 2 for '[' and ']' */
256
*addr = apr_palloc(p, addrlen + 1);
260
(*addr)[addrlen] = '\0';
261
if (apr_inet_pton(AF_INET6, *addr, &ipaddr) != 1) {
271
/* XXX If '%' is not a valid char in a DNS name, we *could* check
272
* for bogus scope ids first.
274
*addr = apr_palloc(p, addrlen + 1);
275
memcpy(*addr, str, addrlen);
276
(*addr)[addrlen] = '\0';
281
#if defined(HAVE_GETADDRINFO)
283
static apr_status_t call_resolver(apr_sockaddr_t **sa,
284
const char *hostname, apr_int32_t family,
285
apr_port_t port, apr_int32_t flags,
288
struct addrinfo hints, *ai, *ai_list;
289
apr_sockaddr_t *prev_sa;
291
char *servname = NULL;
293
memset(&hints, 0, sizeof(hints));
294
hints.ai_family = family;
295
hints.ai_socktype = SOCK_STREAM;
296
#ifdef HAVE_GAI_ADDRCONFIG
297
if (family == APR_UNSPEC) {
298
/* By default, only look up addresses using address types for
299
* which a local interface is configured, i.e. no IPv6 if no
300
* IPv6 interfaces configured. */
301
hints.ai_flags = AI_ADDRCONFIG;
304
if(hostname == NULL) {
306
/* If hostname is NULL, assume we are trying to bind to all
308
hints.ai_flags |= AI_PASSIVE;
310
/* getaddrinfo according to RFC 2553 must have either hostname
311
* or servname non-NULL.
314
/* The Tru64 5.0 getaddrinfo() can only resolve services given
315
* by the name listed in /etc/services; a numeric or unknown
316
* servname gets an EAI_SERVICE error. So just resolve the
317
* appropriate anyaddr and fill in the port later. */
318
hostname = family == AF_INET6 ? "::" : "0.0.0.0";
320
#ifdef AI_NUMERICHOST
321
hints.ai_flags |= AI_NUMERICHOST;
325
/* But current AIX getaddrinfo() doesn't like servname = "0";
326
* the "1" won't hurt since we use the port parameter to fill
327
* in the returned socket addresses later
334
servname = apr_itoa(p, port);
337
error = getaddrinfo(hostname, servname, &hints, &ai_list);
338
#ifdef HAVE_GAI_ADDRCONFIG
339
if (error == EAI_BADFLAGS && family == APR_UNSPEC) {
340
/* Retry with no flags if AI_ADDRCONFIG was rejected. */
342
error = getaddrinfo(hostname, servname, &hints, &ai_list);
347
if (error == EAI_SYSTEM) {
353
/* issues with representing this with APR's error scheme:
354
* glibc uses negative values for these numbers, perhaps so
355
* they don't conflict with h_errno values... Tru64 uses
356
* positive values which conflict with h_errno values
358
#if defined(NEGATIVE_EAI)
361
return error + APR_OS_START_EAIERR;
367
while (ai) { /* while more addresses to report */
368
apr_sockaddr_t *new_sa;
370
/* Ignore anything bogus: getaddrinfo in some old versions of
371
* glibc will return AF_UNIX entries for APR_UNSPEC+AI_PASSIVE
373
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
378
new_sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
381
memcpy(&new_sa->sa, ai->ai_addr, ai->ai_addrlen);
382
apr_sockaddr_vars_set(new_sa, ai->ai_family, port);
384
if (!prev_sa) { /* first element in new list */
386
new_sa->hostname = apr_pstrdup(p, hostname);
391
new_sa->hostname = prev_sa->hostname;
392
prev_sa->next = new_sa;
398
freeaddrinfo(ai_list);
402
static apr_status_t find_addresses(apr_sockaddr_t **sa,
403
const char *hostname, apr_int32_t family,
404
apr_port_t port, apr_int32_t flags,
407
if (flags & APR_IPV4_ADDR_OK) {
408
apr_status_t error = call_resolver(sa, hostname, AF_INET, port, flags, p);
412
family = AF_INET6; /* try again */
419
else if (flags & APR_IPV6_ADDR_OK) {
420
apr_status_t error = call_resolver(sa, hostname, AF_INET6, port, flags, p);
423
family = AF_INET; /* try again */
431
return call_resolver(sa, hostname, family, port, flags, p);
434
#else /* end of HAVE_GETADDRINFO code */
436
static apr_status_t find_addresses(apr_sockaddr_t **sa,
437
const char *hostname, apr_int32_t family,
438
apr_port_t port, apr_int32_t flags,
442
apr_sockaddr_t *prev_sa;
444
#if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
445
defined(HAVE_GETHOSTBYNAME_R) && !defined(BEOS)
446
#ifdef GETHOSTBYNAME_R_HOSTENT_DATA
447
struct hostent_data hd;
449
/* If you see ERANGE, that means GETHOSBYNAME_BUFLEN needs to be
451
char tmp[GETHOSTBYNAME_BUFLEN];
456
struct in_addr ipaddr;
458
const char *orig_hostname = hostname;
460
if (hostname == NULL) {
461
/* if we are given a NULL hostname, assume '0.0.0.0' */
462
hostname = "0.0.0.0";
465
if (*hostname >= '0' && *hostname <= '9' &&
466
strspn(hostname, "0123456789.") == strlen(hostname)) {
468
ipaddr.s_addr = inet_addr(hostname);
469
addr_list[0] = (char *)&ipaddr;
470
addr_list[1] = NULL; /* just one IP in list */
471
hs.h_addr_list = (char **)addr_list;
475
#if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
476
defined(HAVE_GETHOSTBYNAME_R) && !defined(BEOS)
477
#if defined(GETHOSTBYNAME_R_HOSTENT_DATA)
478
/* AIX, HP/UX, D/UX et alia */
479
gethostbyname_r(hostname, &hs, &hd);
482
#if defined(GETHOSTBYNAME_R_GLIBC2)
484
gethostbyname_r(hostname, &hs, tmp, GETHOSTBYNAME_BUFLEN - 1,
487
/* Solaris, Irix et alia */
488
hp = gethostbyname_r(hostname, &hs, tmp, GETHOSTBYNAME_BUFLEN - 1,
490
#endif /* !defined(GETHOSTBYNAME_R_GLIBC2) */
492
return (hosterror + APR_OS_START_SYSERR);
494
#endif /* !defined(GETHOSTBYNAME_R_HOSTENT_DATA) */
496
hp = gethostbyname(hostname);
501
return apr_get_netos_error();
503
return (h_errno + APR_OS_START_SYSERR);
510
while (hp->h_addr_list[curaddr]) {
511
apr_sockaddr_t *new_sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
514
new_sa->sa.sin.sin_addr = *(struct in_addr *)hp->h_addr_list[curaddr];
515
apr_sockaddr_vars_set(new_sa, AF_INET, port);
517
if (!prev_sa) { /* first element in new list */
519
new_sa->hostname = apr_pstrdup(p, orig_hostname);
524
new_sa->hostname = prev_sa->hostname;
525
prev_sa->next = new_sa;
535
#endif /* end of !HAVE_GETADDRINFO code */
537
APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
538
const char *hostname,
539
apr_int32_t family, apr_port_t port,
540
apr_int32_t flags, apr_pool_t *p)
545
if ((masked = flags & (APR_IPV4_ADDR_OK | APR_IPV6_ADDR_OK))) {
547
family != APR_UNSPEC ||
548
masked == (APR_IPV4_ADDR_OK | APR_IPV6_ADDR_OK)) {
552
if (flags & APR_IPV6_ADDR_OK) {
558
/* What may happen is that APR is not IPv6-enabled, but we're still
559
* going to call getaddrinfo(), so we have to tell the OS we only
560
* want IPv4 addresses back since we won't know what to do with
563
if (family == APR_UNSPEC) {
568
return find_addresses(sa, hostname, family, port, flags, p);
571
APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
572
apr_sockaddr_t *sockaddr,
575
#if defined(HAVE_GETNAMEINFO)
577
#if defined(NI_MAXHOST)
578
char tmphostname[NI_MAXHOST];
580
char tmphostname[256];
583
/* don't know if it is portable for getnameinfo() to set h_errno;
584
* clear it then see if it was set */
587
/* default flags are NI_NAMREQD; otherwise, getnameinfo() will return
588
* a numeric address string if it fails to resolve the host name;
589
* that is *not* what we want here
591
* For IPv4-mapped IPv6 addresses, drop down to IPv4 before calling
592
* getnameinfo() to avoid getnameinfo bugs (MacOS X, glibc).
595
if (sockaddr->family == AF_INET6 &&
596
IN6_IS_ADDR_V4MAPPED(&sockaddr->sa.sin6.sin6_addr)) {
597
struct sockaddr_in tmpsa;
598
tmpsa.sin_family = AF_INET;
600
tmpsa.sin_addr.s_addr = ((apr_uint32_t *)sockaddr->ipaddr_ptr)[3];
602
tmpsa.sin_len = sizeof(tmpsa);
605
rc = getnameinfo((const struct sockaddr *)&tmpsa, sizeof(tmpsa),
606
tmphostname, sizeof(tmphostname), NULL, 0,
607
flags != 0 ? flags : NI_NAMEREQD);
611
rc = getnameinfo((const struct sockaddr *)&sockaddr->sa, sockaddr->salen,
612
tmphostname, sizeof(tmphostname), NULL, 0,
613
flags != 0 ? flags : NI_NAMEREQD);
618
/* something went wrong. Look at the EAI_ error code */
619
if (rc == EAI_SYSTEM) {
620
/* EAI_SYSTEM System error returned in errno. */
621
/* IMHO, Implementations that set h_errno a simply broken. */
622
if (h_errno) { /* for broken implementations which set h_errno */
623
return h_errno + APR_OS_START_SYSERR;
625
else { /* "normal" case */
626
return errno + APR_OS_START_SYSERR;
632
#if defined(NEGATIVE_EAI)
633
if (rc < 0) rc = -rc;
635
return rc + APR_OS_START_EAIERR; /* return the EAI_ error */
638
*hostname = sockaddr->hostname = apr_pstrdup(sockaddr->pool,
642
#if APR_HAS_THREADS && !defined(GETHOSTBYADDR_IS_THREAD_SAFE) && \
643
defined(HAVE_GETHOSTBYADDR_R) && !defined(BEOS)
644
#ifdef GETHOSTBYNAME_R_HOSTENT_DATA
645
struct hostent_data hd;
647
char tmp[GETHOSTBYNAME_BUFLEN];
650
struct hostent hs, *hptr;
652
#if defined(GETHOSTBYNAME_R_HOSTENT_DATA)
653
/* AIX, HP/UX, D/UX et alia */
654
gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
655
sizeof(struct in_addr), AF_INET, &hs, &hd);
658
#if defined(GETHOSTBYNAME_R_GLIBC2)
660
gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
661
sizeof(struct in_addr), AF_INET,
662
&hs, tmp, GETHOSTBYNAME_BUFLEN - 1, &hptr, &hosterror);
664
/* Solaris, Irix et alia */
665
hptr = gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
666
sizeof(struct in_addr), AF_INET,
667
&hs, tmp, GETHOSTBYNAME_BUFLEN, &hosterror);
668
#endif /* !defined(GETHOSTBYNAME_R_GLIBC2) */
671
return hosterror + APR_OS_START_SYSERR;
673
#endif /* !defined(GETHOSTBYNAME_R_HOSTENT_DATA) */
675
struct hostent *hptr;
676
hptr = gethostbyaddr((char *)&sockaddr->sa.sin.sin_addr,
677
sizeof(struct in_addr), AF_INET);
681
*hostname = sockaddr->hostname = apr_pstrdup(sockaddr->pool, hptr->h_name);
686
return apr_get_netos_error();
690
return h_errno + APR_OS_START_SYSERR;
695
APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
696
const char *servname)
700
if (servname == NULL)
703
if ((se = getservbyname(servname, NULL)) != NULL){
704
sockaddr->port = htons(se->s_port);
705
sockaddr->servname = apr_pstrdup(sockaddr->pool, servname);
706
sockaddr->sa.sin.sin_port = se->s_port;
712
#define V4MAPPED_EQUAL(a,b) \
713
((a)->sa.sin.sin_family == AF_INET && \
714
(b)->sa.sin.sin_family == AF_INET6 && \
715
IN6_IS_ADDR_V4MAPPED((struct in6_addr *)(b)->ipaddr_ptr) && \
716
!memcmp((a)->ipaddr_ptr, \
717
&((struct in6_addr *)(b)->ipaddr_ptr)->s6_addr[12], \
720
APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
721
const apr_sockaddr_t *addr2)
723
if (addr1->ipaddr_len == addr2->ipaddr_len &&
724
!memcmp(addr1->ipaddr_ptr, addr2->ipaddr_ptr, addr1->ipaddr_len)) {
728
if (V4MAPPED_EQUAL(addr1, addr2)) {
731
if (V4MAPPED_EQUAL(addr2, addr1)) {
735
return 0; /* not equal */
738
static apr_status_t parse_network(apr_ipsubnet_t *ipsub, const char *network)
740
/* legacy syntax for ip addrs: a.b.c. ==> a.b.c.0/24 for example */
744
char buf[sizeof "255.255.255.255"];
746
if (strlen(network) < sizeof buf) {
747
strcpy(buf, network);
753
/* parse components */
760
if (!apr_isdigit(*t)) {
763
while (apr_isdigit(*t)) {
776
if (octet < 0 || octet > 255) {
779
ipsub->sub[0] |= octet << shift;
780
ipsub->mask[0] |= 0xFFUL << shift;
784
ipsub->sub[0] = ntohl(ipsub->sub[0]);
785
ipsub->mask[0] = ntohl(ipsub->mask[0]);
786
ipsub->family = AF_INET;
791
* APR_EINVAL not an IP address; caller should see if it is something else
792
* APR_BADIP IP address portion is is not valid
793
* APR_BADMASK mask portion is not valid
796
static apr_status_t parse_ip(apr_ipsubnet_t *ipsub, const char *ipstr, int network_allowed)
798
/* supported flavors of IP:
800
* . IPv6 numeric address string (e.g., "fe80::1")
802
* IMPORTANT: Don't store IPv4-mapped IPv6 address as an IPv6 address.
804
* . IPv4 numeric address string (e.g., "127.0.0.1")
806
* . IPv4 network string (e.g., "9.67")
808
* IMPORTANT: This network form is only allowed if network_allowed is on.
813
rc = apr_inet_pton(AF_INET6, ipstr, ipsub->sub);
815
if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ipsub->sub)) {
816
/* apr_ipsubnet_test() assumes that we don't create IPv4-mapped IPv6
817
* addresses; this of course forces the user to specify IPv4 addresses
818
* in a.b.c.d style instead of ::ffff:a.b.c.d style.
822
ipsub->family = AF_INET6;
827
rc = apr_inet_pton(AF_INET, ipstr, ipsub->sub);
829
ipsub->family = AF_INET;
833
if (network_allowed) {
834
return parse_network(ipsub, ipstr);
843
static int looks_like_ip(const char *ipstr)
845
if (strchr(ipstr, ':')) {
846
/* definitely not a hostname; assume it is intended to be an IPv6 address */
850
/* simple IPv4 address string check */
851
while ((*ipstr == '.') || apr_isdigit(*ipstr))
853
return (*ipstr == '\0');
856
static void fix_subnet(apr_ipsubnet_t *ipsub)
858
/* in case caller specified more bits in network address than are
859
* valid according to the mask, turn off the extra bits
863
for (i = 0; i < sizeof ipsub->mask / sizeof(apr_int32_t); i++) {
864
ipsub->sub[i] &= ipsub->mask[i];
868
/* be sure not to store any IPv4 address as a v4-mapped IPv6 address */
869
APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub, const char *ipstr,
870
const char *mask_or_numbits, apr_pool_t *p)
874
long bits, maxbits = 32;
876
/* filter out stuff which doesn't look remotely like an IP address; this helps
877
* callers like mod_access which have a syntax allowing hostname or IP address;
878
* APR_EINVAL tells the caller that it was probably not intended to be an IP
881
if (!looks_like_ip(ipstr)) {
885
*ipsub = apr_pcalloc(p, sizeof(apr_ipsubnet_t));
887
/* assume ipstr is an individual IP address, not a subnet */
888
memset((*ipsub)->mask, 0xFF, sizeof (*ipsub)->mask);
890
rv = parse_ip(*ipsub, ipstr, mask_or_numbits == NULL);
891
if (rv != APR_SUCCESS) {
895
if (mask_or_numbits) {
897
if ((*ipsub)->family == AF_INET6) {
901
bits = strtol(mask_or_numbits, &endptr, 10);
902
if (*endptr == '\0' && bits > 0 && bits <= maxbits) {
903
/* valid num-bits string; fill in mask appropriately */
905
apr_int32_t cur_bit_value;
907
memset((*ipsub)->mask, 0, sizeof (*ipsub)->mask);
909
(*ipsub)->mask[cur_entry] = 0xFFFFFFFF; /* all 32 bits */
913
cur_bit_value = 0x80000000;
915
(*ipsub)->mask[cur_entry] |= cur_bit_value;
919
(*ipsub)->mask[cur_entry] = htonl((*ipsub)->mask[cur_entry]);
921
else if (apr_inet_pton(AF_INET, mask_or_numbits, (*ipsub)->mask) == 1 &&
922
(*ipsub)->family == AF_INET) {
923
/* valid IPv4 netmask */
935
APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa)
938
/* XXX This line will segv on Win32 build with APR_HAVE_IPV6,
939
* but without the IPV6 drivers installed.
941
if (sa->sa.sin.sin_family == AF_INET) {
942
if (ipsub->family == AF_INET &&
943
((sa->sa.sin.sin_addr.s_addr & ipsub->mask[0]) == ipsub->sub[0])) {
947
else if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sa->ipaddr_ptr)) {
948
if (ipsub->family == AF_INET &&
949
(((apr_uint32_t *)sa->ipaddr_ptr)[3] & ipsub->mask[0]) == ipsub->sub[0]) {
954
apr_uint32_t *addr = (apr_uint32_t *)sa->ipaddr_ptr;
956
if ((addr[0] & ipsub->mask[0]) == ipsub->sub[0] &&
957
(addr[1] & ipsub->mask[1]) == ipsub->sub[1] &&
958
(addr[2] & ipsub->mask[2]) == ipsub->sub[2] &&
959
(addr[3] & ipsub->mask[3]) == ipsub->sub[3]) {
964
if ((sa->sa.sin.sin_addr.s_addr & ipsub->mask[0]) == ipsub->sub[0]) {
967
#endif /* APR_HAVE_IPV6 */
968
return 0; /* no match */