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_strings.h"
21 #include "apr_strings.h"
22 #include "apr_private.h"
32 #if defined(HAVE_IF_INDEXTONAME) && defined(_MSC_VER)
33 #include "arch/win32/apr_arch_misc.h"
36 #define APR_WANT_STRFUNC
39 struct apr_ipsubnet_t {
42 apr_uint32_t sub[4]; /* big enough for IPv4 and IPv6 addresses */
50 #if !defined(NETWARE) && !defined(WIN32)
51 #ifdef HAVE_SET_H_ERRNO
52 #define SET_H_ERRNO(newval) set_h_errno(newval)
54 #define SET_H_ERRNO(newval) h_errno = (newval)
57 #define SET_H_ERRNO(newval)
60 #if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
61 defined(HAVE_GETHOSTBYNAME_R)
62 /* This is the maximum size that may be returned from the reentrant
63 * gethostbyname_r function. If the system tries to use more, it
64 * should return ERANGE.
66 #define GETHOSTBYNAME_BUFLEN 512
70 /* Some levels of AIX getaddrinfo() don't like servname = "0", so
71 * set servname to "1" when port is 0 and fix it up later.
73 #define AIX_SERVNAME_HACK 1
75 #define AIX_SERVNAME_HACK 0
79 /* XXX: BS solution. Need an HAVE_GETSERVBYNAME and actually
80 * do something here, to provide the obvious proto mappings.
82 static void *getservbyname(const char *name, const char *proto)
88 static apr_status_t get_local_addr(apr_socket_t *sock)
90 sock->local_addr->salen = sizeof(sock->local_addr->sa);
91 if (getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa,
92 &sock->local_addr->salen) < 0) {
93 return apr_get_netos_error();
96 sock->local_port_unknown = sock->local_interface_unknown = 0;
97 /* XXX assumes sin_port and sin6_port at same offset */
98 sock->local_addr->port = ntohs(sock->local_addr->sa.sin.sin_port);
103 static apr_status_t get_remote_addr(apr_socket_t *sock)
105 sock->remote_addr->salen = sizeof(sock->remote_addr->sa);
106 if (getpeername(sock->socketdes, (struct sockaddr *)&sock->remote_addr->sa,
107 &sock->remote_addr->salen) < 0) {
108 return apr_get_netos_error();
111 sock->remote_addr_unknown = 0;
112 /* XXX assumes sin_port and sin6_port at same offset */
113 sock->remote_addr->port = ntohs(sock->remote_addr->sa.sin.sin_port);
118 APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
119 apr_sockaddr_t *sockaddr)
121 if (!apr_inet_ntop(sockaddr->family, sockaddr->ipaddr_ptr, buf, buflen)) {
126 if (sockaddr->family == AF_INET6
127 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sockaddr->ipaddr_ptr)
128 && buflen > strlen("::ffff:")) {
129 /* This is an IPv4-mapped IPv6 address; drop the leading
130 * part of the address string so we're left with the familiar
133 memmove(buf, buf + strlen("::ffff:"),
134 strlen(buf + strlen("::ffff:"))+1);
137 /* ensure NUL termination if the buffer is too short */
138 buf[buflen-1] = '\0';
140 #ifdef HAVE_IF_INDEXTONAME
141 /* Append scope name for link-local addresses. */
142 if (sockaddr->family == AF_INET6
143 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)sockaddr->ipaddr_ptr)) {
144 char scbuf[IF_NAMESIZE], *p = buf + strlen(buf);
146 if (if_indextoname(sockaddr->sa.sin6.sin6_scope_id, scbuf) == scbuf) {
147 /* Space check, need room for buf + '%' + scope + '\0'.
148 * Assert: buflen >= strlen(buf) + strlen(scbuf) + 2
149 * Equiv: buflen >= (p-buf) + strlen(buf) + 2
150 * Thus, fail in inverse condition: */
151 if (buflen < strlen(scbuf) + (p - buf) + 2) {
155 memcpy(p, scbuf, strlen(scbuf) + 1);
158 #endif /* HAVE_IF_INDEXTONAME */
159 #endif /* APR_HAVE_IPV6 */
164 APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
165 apr_sockaddr_t *sockaddr)
167 *addr = apr_palloc(sockaddr->pool, sockaddr->addr_str_len);
168 return apr_sockaddr_ip_getbuf(*addr, sockaddr->addr_str_len, sockaddr);
171 void apr_sockaddr_vars_set(apr_sockaddr_t *addr, int family, apr_port_t port)
173 addr->family = family;
174 addr->sa.sin.sin_family = family;
176 /* XXX IPv6: assumes sin_port and sin6_port at same offset */
177 addr->sa.sin.sin_port = htons(port);
180 #if AIX_SERVNAME_HACK
182 addr->sa.sin.sin_port = htons(port);
186 if (family == APR_INET) {
187 addr->salen = sizeof(struct sockaddr_in);
188 addr->addr_str_len = 16;
189 addr->ipaddr_ptr = &(addr->sa.sin.sin_addr);
190 addr->ipaddr_len = sizeof(struct in_addr);
193 else if (family == APR_INET6) {
194 addr->salen = sizeof(struct sockaddr_in6);
195 addr->addr_str_len = 46;
196 addr->ipaddr_ptr = &(addr->sa.sin6.sin6_addr);
197 addr->ipaddr_len = sizeof(struct in6_addr);
200 #if APR_HAVE_SOCKADDR_UN
201 else if (family == APR_UNIX) {
202 addr->salen = sizeof(struct sockaddr_un);
203 addr->addr_str_len = sizeof(addr->sa.unx.sun_path);;
204 addr->ipaddr_ptr = &(addr->sa.unx.sun_path);
205 addr->ipaddr_len = addr->addr_str_len;
210 APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
211 apr_interface_e which,
214 if (which == APR_LOCAL) {
215 if (sock->local_interface_unknown || sock->local_port_unknown) {
216 apr_status_t rv = get_local_addr(sock);
218 if (rv != APR_SUCCESS) {
222 *sa = sock->local_addr;
224 else if (which == APR_REMOTE) {
225 if (sock->remote_addr_unknown) {
226 apr_status_t rv = get_remote_addr(sock);
228 if (rv != APR_SUCCESS) {
232 *sa = sock->remote_addr;
241 APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
247 const char *ch, *lastchar;
251 *addr = NULL; /* assume not specified */
252 *scope_id = NULL; /* assume not specified */
253 *port = 0; /* assume not specified */
255 /* First handle the optional port number. That may be all that
256 * is specified in the string.
258 ch = lastchar = str + strlen(str) - 1;
259 while (ch >= str && apr_isdigit(*ch)) {
263 if (ch < str) { /* Entire string is the port. */
264 big_port = atoi(str);
265 if (big_port < 1 || big_port > 65535) {
272 if (*ch == ':' && ch < lastchar) { /* host and port number specified */
273 if (ch == str) { /* string starts with ':' -- bad */
276 big_port = atoi(ch + 1);
277 if (big_port < 1 || big_port > 65535) {
284 /* now handle the hostname */
285 addrlen = lastchar - str + 1;
287 /* XXX we don't really have to require APR_HAVE_IPV6 for this;
288 * just pass char[] for ipaddr (so we don't depend on struct in6_addr)
289 * and always define APR_INET6
293 const char *end_bracket = memchr(str, ']', addrlen);
294 struct in6_addr ipaddr;
295 const char *scope_delim;
297 if (!end_bracket || end_bracket != lastchar) {
302 /* handle scope id; this is the only context where it is allowed */
303 scope_delim = memchr(str, '%', addrlen);
305 if (scope_delim == end_bracket - 1) { /* '%' without scope id */
309 addrlen = scope_delim - str - 1;
310 *scope_id = apr_pstrmemdup(p, scope_delim + 1, end_bracket - scope_delim - 1);
313 addrlen = addrlen - 2; /* minus 2 for '[' and ']' */
316 *addr = apr_pstrmemdup(p, str + 1, addrlen);
317 if (apr_inet_pton(AF_INET6, *addr, &ipaddr) != 1) {
327 /* XXX If '%' is not a valid char in a DNS name, we *could* check
328 * for bogus scope ids first.
330 *addr = apr_pstrmemdup(p, str, addrlen);
335 #if defined(HAVE_GETADDRINFO)
337 static apr_status_t call_resolver(apr_sockaddr_t **sa,
338 const char *hostname, apr_int32_t family,
339 apr_port_t port, apr_int32_t flags,
342 struct addrinfo hints, *ai, *ai_list;
343 apr_sockaddr_t *prev_sa;
345 char *servname = NULL;
347 memset(&hints, 0, sizeof(hints));
348 hints.ai_family = family;
349 hints.ai_socktype = SOCK_STREAM;
350 #ifdef HAVE_GAI_ADDRCONFIG
351 if (family == APR_UNSPEC) {
352 /* By default, only look up addresses using address types for
353 * which a local interface is configured, i.e. no IPv6 if no
354 * IPv6 interfaces configured. */
355 hints.ai_flags = AI_ADDRCONFIG;
360 /* z/OS will not return IPv4 address under AF_UNSPEC if any IPv6 results
361 * are returned, w/o AI_ALL.
363 if (family == APR_UNSPEC) {
364 hints.ai_flags |= AI_ALL;
368 if(hostname == NULL) {
370 /* If hostname is NULL, assume we are trying to bind to all
372 hints.ai_flags |= AI_PASSIVE;
374 /* getaddrinfo according to RFC 2553 must have either hostname
375 * or servname non-NULL.
378 /* The Tru64 5.0 getaddrinfo() can only resolve services given
379 * by the name listed in /etc/services; a numeric or unknown
380 * servname gets an EAI_SERVICE error. So just resolve the
381 * appropriate anyaddr and fill in the port later. */
382 hostname = family == AF_INET6 ? "::" : "0.0.0.0";
384 #ifdef AI_NUMERICHOST
385 hints.ai_flags |= AI_NUMERICHOST;
388 #if AIX_SERVNAME_HACK
393 #endif /* AIX_SERVNAME_HACK */
394 servname = apr_itoa(p, port);
397 error = getaddrinfo(hostname, servname, &hints, &ai_list);
398 #ifdef HAVE_GAI_ADDRCONFIG
400 * Using AI_ADDRCONFIG involves some unfortunate guesswork because it
401 * does not consider loopback addresses when trying to determine if
402 * IPv4 or IPv6 is configured on a system (see RFC 3493).
403 * This is a problem if one actually wants to listen on or connect to
404 * the loopback address of a protocol family that is not otherwise
405 * configured on the system. See PR 52709.
406 * To work around some of the problems, retry without AI_ADDRCONFIG
407 * in case of EAI_ADDRFAMILY.
408 * XXX: apr_sockaddr_info_get() should really accept a flag to determine
409 * XXX: if AI_ADDRCONFIG's guesswork is wanted and if the address is
410 * XXX: to be used for listen() or connect().
412 * In case of EAI_BADFLAGS, AI_ADDRCONFIG is not supported.
414 if ((family == APR_UNSPEC) && (error == EAI_BADFLAGS
415 #ifdef EAI_ADDRFAMILY
416 || error == EAI_ADDRFAMILY
419 hints.ai_flags &= ~AI_ADDRCONFIG;
420 error = getaddrinfo(hostname, servname, &hints, &ai_list);
425 return apr_get_netos_error();
427 if (error == EAI_SYSTEM) {
428 return errno ? errno : APR_EGENERAL;
432 /* issues with representing this with APR's error scheme:
433 * glibc uses negative values for these numbers, perhaps so
434 * they don't conflict with h_errno values... Tru64 uses
435 * positive values which conflict with h_errno values
437 #if defined(NEGATIVE_EAI)
440 return error + APR_OS_START_EAIERR;
447 while (ai) { /* while more addresses to report */
448 apr_sockaddr_t *new_sa;
450 /* Ignore anything bogus: getaddrinfo in some old versions of
451 * glibc will return AF_UNIX entries for APR_UNSPEC+AI_PASSIVE
454 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
456 if (ai->ai_family != AF_INET) {
462 new_sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
465 memcpy(&new_sa->sa, ai->ai_addr, ai->ai_addrlen);
466 apr_sockaddr_vars_set(new_sa, ai->ai_family, port);
468 if (!prev_sa) { /* first element in new list */
470 new_sa->hostname = apr_pstrdup(p, hostname);
475 new_sa->hostname = prev_sa->hostname;
476 prev_sa->next = new_sa;
482 freeaddrinfo(ai_list);
484 if (prev_sa == NULL) {
486 * getaddrinfo returned only useless entries and *sa is still empty.
487 * This should be treated as an error.
495 static apr_status_t find_addresses(apr_sockaddr_t **sa,
496 const char *hostname, apr_int32_t family,
497 apr_port_t port, apr_int32_t flags,
500 if (flags & APR_IPV4_ADDR_OK) {
501 apr_status_t error = call_resolver(sa, hostname, AF_INET, port, flags, p);
505 family = AF_INET6; /* try again */
512 else if (flags & APR_IPV6_ADDR_OK) {
513 apr_status_t error = call_resolver(sa, hostname, AF_INET6, port, flags, p);
516 family = AF_INET; /* try again */
524 return call_resolver(sa, hostname, family, port, flags, p);
527 #else /* end of HAVE_GETADDRINFO code */
529 static apr_status_t find_addresses(apr_sockaddr_t **sa,
530 const char *hostname, apr_int32_t family,
531 apr_port_t port, apr_int32_t flags,
535 apr_sockaddr_t *prev_sa;
537 #if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
538 defined(HAVE_GETHOSTBYNAME_R) && !defined(BEOS)
539 #ifdef GETHOSTBYNAME_R_HOSTENT_DATA
540 struct hostent_data hd;
542 /* If you see ERANGE, that means GETHOSBYNAME_BUFLEN needs to be
544 char tmp[GETHOSTBYNAME_BUFLEN];
549 struct in_addr ipaddr;
551 const char *orig_hostname = hostname;
553 if (hostname == NULL) {
554 /* if we are given a NULL hostname, assume '0.0.0.0' */
555 hostname = "0.0.0.0";
558 if (*hostname >= '0' && *hostname <= '9' &&
559 strspn(hostname, "0123456789.") == strlen(hostname)) {
561 ipaddr.s_addr = inet_addr(hostname);
562 addr_list[0] = (char *)&ipaddr;
563 addr_list[1] = NULL; /* just one IP in list */
564 hs.h_addr_list = (char **)addr_list;
568 #if APR_HAS_THREADS && !defined(GETHOSTBYNAME_IS_THREAD_SAFE) && \
569 defined(HAVE_GETHOSTBYNAME_R) && !defined(BEOS)
570 #if defined(GETHOSTBYNAME_R_HOSTENT_DATA)
571 /* AIX, HP/UX, D/UX et alia */
572 gethostbyname_r(hostname, &hs, &hd);
575 #if defined(GETHOSTBYNAME_R_GLIBC2)
577 gethostbyname_r(hostname, &hs, tmp, GETHOSTBYNAME_BUFLEN - 1,
580 /* Solaris, Irix et alia */
581 hp = gethostbyname_r(hostname, &hs, tmp, GETHOSTBYNAME_BUFLEN - 1,
583 #endif /* !defined(GETHOSTBYNAME_R_GLIBC2) */
585 return (hosterror + APR_OS_START_SYSERR);
587 #endif /* !defined(GETHOSTBYNAME_R_HOSTENT_DATA) */
589 hp = gethostbyname(hostname);
594 return apr_get_netos_error();
596 return (h_errno + APR_OS_START_SYSERR);
603 while (hp->h_addr_list[curaddr]) {
604 apr_sockaddr_t *new_sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
607 new_sa->sa.sin.sin_addr = *(struct in_addr *)hp->h_addr_list[curaddr];
608 apr_sockaddr_vars_set(new_sa, AF_INET, port);
610 if (!prev_sa) { /* first element in new list */
612 new_sa->hostname = apr_pstrdup(p, orig_hostname);
617 new_sa->hostname = prev_sa->hostname;
618 prev_sa->next = new_sa;
625 if (prev_sa == NULL) {
626 /* this should not happen but no result should be treated as error */
633 #endif /* end of !HAVE_GETADDRINFO code */
635 APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
636 const char *hostname,
637 apr_int32_t family, apr_port_t port,
638 apr_int32_t flags, apr_pool_t *p)
643 if ((masked = flags & (APR_IPV4_ADDR_OK | APR_IPV6_ADDR_OK))) {
645 family != APR_UNSPEC ||
646 masked == (APR_IPV4_ADDR_OK | APR_IPV6_ADDR_OK)) {
650 if (flags & APR_IPV6_ADDR_OK) {
655 if (family == APR_UNSPEC && hostname && *hostname == '/') {
658 if (family == APR_UNIX) {
659 #if APR_HAVE_SOCKADDR_UN
660 if (hostname && *hostname == '/') {
661 *sa = apr_pcalloc(p, sizeof(apr_sockaddr_t));
663 apr_cpystrn((*sa)->sa.unx.sun_path, hostname,
664 sizeof((*sa)->sa.unx.sun_path));
665 (*sa)->hostname = apr_pstrdup(p, hostname);
666 (*sa)->family = APR_UNIX;
667 (*sa)->sa.unx.sun_family = APR_UNIX;
668 (*sa)->salen = sizeof(struct sockaddr_un);
669 (*sa)->addr_str_len = sizeof((*sa)->sa.unx.sun_path);
670 (*sa)->ipaddr_ptr = &((*sa)->sa.unx.sun_path);
671 (*sa)->ipaddr_len = (*sa)->addr_str_len;
683 /* What may happen is that APR is not IPv6-enabled, but we're still
684 * going to call getaddrinfo(), so we have to tell the OS we only
685 * want IPv4 addresses back since we won't know what to do with
688 if (family == APR_UNSPEC) {
693 return find_addresses(sa, hostname, family, port, flags, p);
696 APR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst,
697 const apr_sockaddr_t *src,
701 const apr_sockaddr_t *s;
703 for (*dst = d = NULL, s = src; s; s = s->next) {
705 *dst = d = apr_pmemdup(p, s, sizeof *s);
708 d = d->next = apr_pmemdup(p, s, sizeof *s);
711 if (s == src || s->hostname != src->hostname) {
712 d->hostname = apr_pstrdup(p, s->hostname);
715 d->hostname = (*dst)->hostname;
719 if (s == src || s->servname != src->servname) {
720 d->servname = apr_pstrdup(p, s->servname);
723 d->servname = (*dst)->servname;
727 apr_sockaddr_vars_set(d, s->family, s->port);
732 APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
733 apr_sockaddr_t *sockaddr,
736 #if defined(HAVE_GETNAMEINFO)
738 #if defined(NI_MAXHOST)
739 char tmphostname[NI_MAXHOST];
741 char tmphostname[256];
744 /* don't know if it is portable for getnameinfo() to set h_errno;
745 * clear it then see if it was set */
748 /* default flags are NI_NAMREQD; otherwise, getnameinfo() will return
749 * a numeric address string if it fails to resolve the host name;
750 * that is *not* what we want here
752 * For IPv4-mapped IPv6 addresses, drop down to IPv4 before calling
753 * getnameinfo() to avoid getnameinfo bugs (MacOS X, glibc).
756 if (sockaddr->family == AF_INET6 &&
757 IN6_IS_ADDR_V4MAPPED(&sockaddr->sa.sin6.sin6_addr)) {
758 struct sockaddr_in tmpsa;
759 tmpsa.sin_family = AF_INET;
761 tmpsa.sin_addr.s_addr = ((apr_uint32_t *)sockaddr->ipaddr_ptr)[3];
763 tmpsa.sin_len = sizeof(tmpsa);
766 rc = getnameinfo((const struct sockaddr *)&tmpsa, sizeof(tmpsa),
767 tmphostname, sizeof(tmphostname), NULL, 0,
768 flags != 0 ? flags : NI_NAMEREQD);
770 #if APR_HAVE_SOCKADDR_UN
771 else if (sockaddr->family == APR_UNIX) {
772 *hostname = sockaddr->hostname;
778 rc = getnameinfo((const struct sockaddr *)&sockaddr->sa, sockaddr->salen,
779 tmphostname, sizeof(tmphostname), NULL, 0,
780 flags != 0 ? flags : NI_NAMEREQD);
785 /* something went wrong. Look at the EAI_ error code */
786 if (rc == EAI_SYSTEM) {
787 /* EAI_SYSTEM System error returned in errno. */
788 /* IMHO, Implementations that set h_errno a simply broken. */
789 if (h_errno) { /* for broken implementations which set h_errno */
790 return h_errno + APR_OS_START_SYSERR;
792 else { /* "normal" case */
793 return errno + APR_OS_START_SYSERR;
799 #if defined(NEGATIVE_EAI)
800 if (rc < 0) rc = -rc;
802 return rc + APR_OS_START_EAIERR; /* return the EAI_ error */
805 *hostname = sockaddr->hostname = apr_pstrdup(sockaddr->pool,
809 #if APR_HAS_THREADS && !defined(GETHOSTBYADDR_IS_THREAD_SAFE) && \
810 defined(HAVE_GETHOSTBYADDR_R) && !defined(BEOS)
811 #ifdef GETHOSTBYNAME_R_HOSTENT_DATA
812 struct hostent_data hd;
814 char tmp[GETHOSTBYNAME_BUFLEN];
817 struct hostent hs, *hptr;
819 #if defined(GETHOSTBYNAME_R_HOSTENT_DATA)
820 /* AIX, HP/UX, D/UX et alia */
821 gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
822 sizeof(struct in_addr), AF_INET, &hs, &hd);
825 #if defined(GETHOSTBYNAME_R_GLIBC2)
827 gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
828 sizeof(struct in_addr), AF_INET,
829 &hs, tmp, GETHOSTBYNAME_BUFLEN - 1, &hptr, &hosterror);
831 /* Solaris, Irix et alia */
832 hptr = gethostbyaddr_r((char *)&sockaddr->sa.sin.sin_addr,
833 sizeof(struct in_addr), AF_INET,
834 &hs, tmp, GETHOSTBYNAME_BUFLEN, &hosterror);
835 #endif /* !defined(GETHOSTBYNAME_R_GLIBC2) */
838 return hosterror + APR_OS_START_SYSERR;
840 #endif /* !defined(GETHOSTBYNAME_R_HOSTENT_DATA) */
842 struct hostent *hptr;
843 hptr = gethostbyaddr((char *)&sockaddr->sa.sin.sin_addr,
844 sizeof(struct in_addr), AF_INET);
848 *hostname = sockaddr->hostname = apr_pstrdup(sockaddr->pool, hptr->h_name);
853 return apr_get_netos_error();
857 return h_errno + APR_OS_START_SYSERR;
862 APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
863 const char *servname)
865 #if APR_HAS_THREADS && !defined(GETSERVBYNAME_IS_THREAD_SAFE) && \
866 defined(HAVE_GETSERVBYNAME_R) && \
867 (defined(GETSERVBYNAME_R_GLIBC2) || defined(GETSERVBYNAME_R_SOLARIS) || \
868 defined(GETSERVBYNAME_R_OSF1))
870 #if defined(GETSERVBYNAME_R_OSF1)
871 struct servent_data sed;
873 memset(&sed, 0, sizeof(sed)); /* must zero fill before use */
875 #if defined(GETSERVBYNAME_R_GLIBC2)
884 if (servname == NULL)
887 #if APR_HAS_THREADS && !defined(GETSERVBYNAME_IS_THREAD_SAFE) && \
888 defined(HAVE_GETSERVBYNAME_R) && \
889 (defined(GETSERVBYNAME_R_GLIBC2) || defined(GETSERVBYNAME_R_SOLARIS) || \
890 defined(GETSERVBYNAME_R_OSF1))
891 #if defined(GETSERVBYNAME_R_GLIBC2)
892 if (getservbyname_r(servname, NULL,
893 &se, buf, sizeof(buf), &res) == 0 && res != NULL) {
894 sockaddr->port = ntohs(res->s_port);
895 sockaddr->servname = apr_pstrdup(sockaddr->pool, servname);
896 sockaddr->sa.sin.sin_port = res->s_port;
899 #elif defined(GETSERVBYNAME_R_SOLARIS)
900 if (getservbyname_r(servname, NULL, &se, buf, sizeof(buf)) != NULL) {
901 sockaddr->port = ntohs(se.s_port);
902 sockaddr->servname = apr_pstrdup(sockaddr->pool, servname);
903 sockaddr->sa.sin.sin_port = se.s_port;
906 #elif defined(GETSERVBYNAME_R_OSF1)
907 if (getservbyname_r(servname, NULL, &se, &sed) == 0) {
908 sockaddr->port = ntohs(se.s_port);
909 sockaddr->servname = apr_pstrdup(sockaddr->pool, servname);
910 sockaddr->sa.sin.sin_port = se.s_port;
915 if ((se = getservbyname(servname, NULL)) != NULL){
916 sockaddr->port = ntohs(se->s_port);
917 sockaddr->servname = apr_pstrdup(sockaddr->pool, servname);
918 sockaddr->sa.sin.sin_port = se->s_port;
925 #define V4MAPPED_EQUAL(a,b) \
926 ((a)->sa.sin.sin_family == AF_INET && \
927 (b)->sa.sin.sin_family == AF_INET6 && \
928 IN6_IS_ADDR_V4MAPPED((struct in6_addr *)(b)->ipaddr_ptr) && \
929 !memcmp((a)->ipaddr_ptr, \
930 &((struct in6_addr *)(b)->ipaddr_ptr)->s6_addr[12], \
934 #define SCOPE_OR_ZERO(sa_) ((sa_)->family != AF_INET6 ? 0 : \
935 ((sa_)->sa.sin6.sin6_scope_id))
937 #define SCOPE_OR_ZERO(sa_) (0)
940 APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
941 const apr_sockaddr_t *addr2)
943 if (addr1->ipaddr_len == addr2->ipaddr_len
944 && !memcmp(addr1->ipaddr_ptr, addr2->ipaddr_ptr, addr1->ipaddr_len)
945 && SCOPE_OR_ZERO(addr1) == SCOPE_OR_ZERO(addr2)) {
949 if (V4MAPPED_EQUAL(addr1, addr2)) {
952 if (V4MAPPED_EQUAL(addr2, addr1)) {
956 return 0; /* not equal */
959 APR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr)
961 static const char inaddr_any[
963 sizeof(struct in6_addr)
965 sizeof(struct in_addr)
969 if (addr->ipaddr_ptr /* IP address initialized */
970 && addr->ipaddr_len <= sizeof inaddr_any) { /* else bug elsewhere? */
971 if (!memcmp(inaddr_any, addr->ipaddr_ptr, addr->ipaddr_len)) {
975 if (addr->family == AF_INET6
976 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr->ipaddr_ptr)) {
977 struct in_addr *v4 = (struct in_addr *)&((apr_uint32_t *)addr->ipaddr_ptr)[3];
979 if (!memcmp(inaddr_any, v4, sizeof *v4)) {
988 static apr_status_t parse_network(apr_ipsubnet_t *ipsub, const char *network)
990 /* legacy syntax for ip addrs: a.b.c. ==> a.b.c.0/24 for example */
994 char buf[sizeof "255.255.255.255"];
996 if (strlen(network) < sizeof buf) {
997 strcpy(buf, network);
1003 /* parse components */
1010 if (!apr_isdigit(*t)) {
1013 while (apr_isdigit(*t)) {
1026 if (octet < 0 || octet > 255) {
1029 ipsub->sub[0] |= octet << shift;
1030 ipsub->mask[0] |= 0xFFUL << shift;
1034 ipsub->sub[0] = ntohl(ipsub->sub[0]);
1035 ipsub->mask[0] = ntohl(ipsub->mask[0]);
1036 ipsub->family = AF_INET;
1041 * APR_EINVAL not an IP address; caller should see if it is something else
1042 * APR_BADIP IP address portion is is not valid
1043 * APR_BADMASK mask portion is not valid
1046 static apr_status_t parse_ip(apr_ipsubnet_t *ipsub, const char *ipstr, int network_allowed)
1048 /* supported flavors of IP:
1050 * . IPv6 numeric address string (e.g., "fe80::1")
1052 * IMPORTANT: Don't store IPv4-mapped IPv6 address as an IPv6 address.
1054 * . IPv4 numeric address string (e.g., "127.0.0.1")
1056 * . IPv4 network string (e.g., "9.67")
1058 * IMPORTANT: This network form is only allowed if network_allowed is on.
1063 rc = apr_inet_pton(AF_INET6, ipstr, ipsub->sub);
1065 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ipsub->sub)) {
1066 /* apr_ipsubnet_test() assumes that we don't create IPv4-mapped IPv6
1067 * addresses; this of course forces the user to specify IPv4 addresses
1068 * in a.b.c.d style instead of ::ffff:a.b.c.d style.
1072 ipsub->family = AF_INET6;
1077 rc = apr_inet_pton(AF_INET, ipstr, ipsub->sub);
1079 ipsub->family = AF_INET;
1083 if (network_allowed) {
1084 return parse_network(ipsub, ipstr);
1093 static int looks_like_ip(const char *ipstr)
1095 if (strlen(ipstr) == 0) {
1099 if (strchr(ipstr, ':')) {
1100 /* definitely not a hostname; assume it is intended to be an IPv6 address */
1104 /* simple IPv4 address string check */
1105 while ((*ipstr == '.') || apr_isdigit(*ipstr))
1107 return (*ipstr == '\0');
1110 static void fix_subnet(apr_ipsubnet_t *ipsub)
1112 /* in case caller specified more bits in network address than are
1113 * valid according to the mask, turn off the extra bits
1117 for (i = 0; i < sizeof ipsub->mask / sizeof(apr_int32_t); i++) {
1118 ipsub->sub[i] &= ipsub->mask[i];
1122 /* be sure not to store any IPv4 address as a v4-mapped IPv6 address */
1123 APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub, const char *ipstr,
1124 const char *mask_or_numbits, apr_pool_t *p)
1128 long bits, maxbits = 32;
1130 /* filter out stuff which doesn't look remotely like an IP address; this helps
1131 * callers like mod_access which have a syntax allowing hostname or IP address;
1132 * APR_EINVAL tells the caller that it was probably not intended to be an IP
1135 if (!looks_like_ip(ipstr)) {
1139 *ipsub = apr_pcalloc(p, sizeof(apr_ipsubnet_t));
1141 /* assume ipstr is an individual IP address, not a subnet */
1142 memset((*ipsub)->mask, 0xFF, sizeof (*ipsub)->mask);
1144 rv = parse_ip(*ipsub, ipstr, mask_or_numbits == NULL);
1145 if (rv != APR_SUCCESS) {
1149 if (mask_or_numbits) {
1151 if ((*ipsub)->family == AF_INET6) {
1155 bits = strtol(mask_or_numbits, &endptr, 10);
1156 if (*endptr == '\0' && bits > 0 && bits <= maxbits) {
1157 /* valid num-bits string; fill in mask appropriately */
1159 apr_int32_t cur_bit_value;
1161 memset((*ipsub)->mask, 0, sizeof (*ipsub)->mask);
1163 (*ipsub)->mask[cur_entry] = 0xFFFFFFFF; /* all 32 bits */
1167 cur_bit_value = 0x80000000;
1169 (*ipsub)->mask[cur_entry] |= cur_bit_value;
1173 (*ipsub)->mask[cur_entry] = htonl((*ipsub)->mask[cur_entry]);
1175 else if (apr_inet_pton(AF_INET, mask_or_numbits, (*ipsub)->mask) == 1 &&
1176 (*ipsub)->family == AF_INET) {
1177 /* valid IPv4 netmask */
1180 return APR_EBADMASK;
1189 APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa)
1192 /* XXX This line will segv on Win32 build with APR_HAVE_IPV6,
1193 * but without the IPV6 drivers installed.
1195 if (sa->family == AF_INET) {
1196 if (ipsub->family == AF_INET &&
1197 ((sa->sa.sin.sin_addr.s_addr & ipsub->mask[0]) == ipsub->sub[0])) {
1201 else if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)sa->ipaddr_ptr)) {
1202 if (ipsub->family == AF_INET &&
1203 (((apr_uint32_t *)sa->ipaddr_ptr)[3] & ipsub->mask[0]) == ipsub->sub[0]) {
1207 else if (sa->family == AF_INET6 && ipsub->family == AF_INET6) {
1208 apr_uint32_t *addr = (apr_uint32_t *)sa->ipaddr_ptr;
1210 if ((addr[0] & ipsub->mask[0]) == ipsub->sub[0] &&
1211 (addr[1] & ipsub->mask[1]) == ipsub->sub[1] &&
1212 (addr[2] & ipsub->mask[2]) == ipsub->sub[2] &&
1213 (addr[3] & ipsub->mask[3]) == ipsub->sub[3]) {
1218 if ((sa->sa.sin.sin_addr.s_addr & ipsub->mask[0]) == ipsub->sub[0]) {
1221 #endif /* APR_HAVE_IPV6 */
1222 return 0; /* no match */
1225 APR_DECLARE(apr_status_t) apr_sockaddr_zone_set(apr_sockaddr_t *sa,
1226 const char *zone_id)
1228 #if !APR_HAVE_IPV6 || !defined(HAVE_IF_NAMETOINDEX)
1229 return APR_ENOTIMPL;
1233 if (sa->family != APR_INET6
1234 || !IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)sa->ipaddr_ptr)) {
1238 idx = if_nametoindex(zone_id);
1240 sa->sa.sin6.sin6_scope_id = idx;
1244 if (errno != ENODEV) {
1249 apr_int64_t i = apr_strtoi64(zone_id, &endptr, 10);
1251 if (*endptr != '\0' || errno || i < 1 || i > APR_INT16_MAX) {
1252 return APR_EGENERAL;
1255 sa->sa.sin6.sin6_scope_id = (unsigned int) i;
1261 APR_DECLARE(apr_status_t) apr_sockaddr_zone_get(const apr_sockaddr_t *sa,
1266 #if !APR_HAVE_IPV6 || !defined(HAVE_IF_INDEXTONAME)
1267 return APR_ENOTIMPL;
1269 if (sa->family != APR_INET6 || !sa->sa.sin6.sin6_scope_id) {
1274 char *buf = apr_palloc(p, IF_NAMESIZE);
1275 if (if_indextoname(sa->sa.sin6.sin6_scope_id, buf) == NULL)
1280 if (id) *id = sa->sa.sin6.sin6_scope_id;