diff options
author | Peter Wemm <peter@FreeBSD.org> | 2018-10-08 08:24:14 +0000 |
---|---|---|
committer | Peter Wemm <peter@FreeBSD.org> | 2018-10-08 08:24:14 +0000 |
commit | f7eb533f85d0941dbf6edb3081f065e4c010b8cc (patch) | |
tree | a9a3ba945deee0800d3818a48c45323608935019 /network_io/unix | |
parent | df84d2567179e9d8867957c089683d753016bd75 (diff) |
Diffstat (limited to 'network_io/unix')
-rw-r--r-- | network_io/unix/multicast.c | 2 | ||||
-rw-r--r-- | network_io/unix/sockaddr.c | 95 | ||||
-rw-r--r-- | network_io/unix/sockets.c | 61 | ||||
-rw-r--r-- | network_io/unix/sockopt.c | 35 |
4 files changed, 176 insertions, 17 deletions
diff --git a/network_io/unix/multicast.c b/network_io/unix/multicast.c index 3767bfdd15c14..a604b06006d7b 100644 --- a/network_io/unix/multicast.c +++ b/network_io/unix/multicast.c @@ -62,7 +62,7 @@ static unsigned int find_if_index(const apr_sockaddr_t *iface) for (ifp = ifs; ifp; ifp = ifp->ifa_next) { if (ifp->ifa_addr != NULL && ifp->ifa_addr->sa_family == AF_INET6) { if (memcmp(&iface->sa.sin6.sin6_addr, - &ifp->ifa_addr->sa_data[0], + &((struct sockaddr_in6*)ifp->ifa_addr)->sin6_addr, sizeof(iface->sa.sin6.sin6_addr)) == 0) { index = if_nametoindex(ifp->ifa_name); break; diff --git a/network_io/unix/sockaddr.c b/network_io/unix/sockaddr.c index e6d7e0be93d81..a375f72ece3b0 100644 --- a/network_io/unix/sockaddr.c +++ b/network_io/unix/sockaddr.c @@ -167,6 +167,14 @@ void apr_sockaddr_vars_set(apr_sockaddr_t *addr, int family, apr_port_t port) addr->ipaddr_len = sizeof(struct in6_addr); } #endif +#if APR_HAVE_SOCKADDR_UN + else if (family == APR_UNIX) { + addr->salen = sizeof(struct sockaddr_un); + addr->addr_str_len = sizeof(addr->sa.unx.sun_path);; + addr->ipaddr_ptr = &(addr->sa.unx.sun_path); + addr->ipaddr_len = addr->addr_str_len; + } +#endif } APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa, @@ -269,19 +277,13 @@ APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr, return APR_EINVAL; } addrlen = scope_delim - str - 1; - *scope_id = apr_palloc(p, end_bracket - scope_delim); - memcpy(*scope_id, scope_delim + 1, end_bracket - scope_delim - 1); - (*scope_id)[end_bracket - scope_delim - 1] = '\0'; + *scope_id = apr_pstrmemdup(p, scope_delim + 1, end_bracket - scope_delim - 1); } else { addrlen = addrlen - 2; /* minus 2 for '[' and ']' */ } - *addr = apr_palloc(p, addrlen + 1); - memcpy(*addr, - str + 1, - addrlen); - (*addr)[addrlen] = '\0'; + *addr = apr_pstrmemdup(p, str + 1, addrlen); if (apr_inet_pton(AF_INET6, *addr, &ipaddr) != 1) { *addr = NULL; *scope_id = NULL; @@ -295,9 +297,7 @@ APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr, /* XXX If '%' is not a valid char in a DNS name, we *could* check * for bogus scope ids first. */ - *addr = apr_palloc(p, addrlen + 1); - memcpy(*addr, str, addrlen); - (*addr)[addrlen] = '\0'; + *addr = apr_pstrmemdup(p, str, addrlen); } return APR_SUCCESS; } @@ -622,6 +622,33 @@ APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa, } #endif } + if (family == APR_UNSPEC && hostname && *hostname == '/') { + family = APR_UNIX; + } + if (family == APR_UNIX) { +#if APR_HAVE_SOCKADDR_UN + if (hostname && *hostname == '/') { + *sa = apr_pcalloc(p, sizeof(apr_sockaddr_t)); + (*sa)->pool = p; + apr_cpystrn((*sa)->sa.unx.sun_path, hostname, + sizeof((*sa)->sa.unx.sun_path)); + (*sa)->hostname = apr_pstrdup(p, hostname); + (*sa)->family = APR_UNIX; + (*sa)->sa.unx.sun_family = APR_UNIX; + (*sa)->salen = sizeof(struct sockaddr_un); + (*sa)->addr_str_len = sizeof((*sa)->sa.unx.sun_path); + (*sa)->ipaddr_ptr = &((*sa)->sa.unx.sun_path); + (*sa)->ipaddr_len = (*sa)->addr_str_len; + + return APR_SUCCESS; + } + else +#endif + { + *sa = NULL; + return APR_ENOTIMPL; + } + } #if !APR_HAVE_IPV6 /* What may happen is that APR is not IPv6-enabled, but we're still * going to call getaddrinfo(), so we have to tell the OS we only @@ -636,6 +663,42 @@ APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa, return find_addresses(sa, hostname, family, port, flags, p); } +APR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst, + const apr_sockaddr_t *src, + apr_pool_t *p) +{ + apr_sockaddr_t *d; + const apr_sockaddr_t *s; + + for (*dst = d = NULL, s = src; s; s = s->next) { + if (!d) { + *dst = d = apr_pmemdup(p, s, sizeof *s); + } + else { + d = d->next = apr_pmemdup(p, s, sizeof *s); + } + if (s->hostname) { + if (s == src || s->hostname != src->hostname) { + d->hostname = apr_pstrdup(p, s->hostname); + } + else { + d->hostname = (*dst)->hostname; + } + } + if (s->servname) { + if (s == src || s->servname != src->servname) { + d->servname = apr_pstrdup(p, s->servname); + } + else { + d->servname = (*dst)->servname; + } + } + d->pool = p; + apr_sockaddr_vars_set(d, s->family, s->port); + } + return APR_SUCCESS; +} + APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname, apr_sockaddr_t *sockaddr, apr_int32_t flags) @@ -674,6 +737,12 @@ APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname, tmphostname, sizeof(tmphostname), NULL, 0, flags != 0 ? flags : NI_NAMEREQD); } +#if APR_HAVE_SOCKADDR_UN + else if (sockaddr->family == APR_UNIX) { + *hostname = sockaddr->hostname; + return APR_SUCCESS; + } +#endif else #endif rc = getnameinfo((const struct sockaddr *)&sockaddr->sa, sockaddr->salen, @@ -985,6 +1054,10 @@ static apr_status_t parse_ip(apr_ipsubnet_t *ipsub, const char *ipstr, int netwo static int looks_like_ip(const char *ipstr) { + if (strlen(ipstr) == 0) { + return 0; + } + if (strchr(ipstr, ':')) { /* definitely not a hostname; assume it is intended to be an IPv6 address */ return 1; diff --git a/network_io/unix/sockets.c b/network_io/unix/sockets.c index b95794f183add..206c654471583 100644 --- a/network_io/unix/sockets.c +++ b/network_io/unix/sockets.c @@ -26,13 +26,26 @@ #define close closesocket #endif /* BEOS_R5 */ -static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */ +#if APR_HAVE_SOCKADDR_UN +#define GENERIC_INADDR_ANY_LEN sizeof(struct sockaddr_un) +#else +#define GENERIC_INADDR_ANY_LEN 16 +#endif + +/* big enough for IPv4, IPv6 and optionaly sun_path */ +static char generic_inaddr_any[GENERIC_INADDR_ANY_LEN] = {0}; static apr_status_t socket_cleanup(void *sock) { apr_socket_t *thesocket = sock; int sd = thesocket->socketdes; +#if APR_HAVE_SOCKADDR_UN + if (thesocket->bound && thesocket->local_addr->family == APR_UNIX) { + /* XXX: Check for return values ? */ + unlink(thesocket->local_addr->hostname); + } +#endif /* Set socket descriptor to -1 before close(), so that there is no * chance of returning an already closed FD from apr_os_sock_get(). */ @@ -49,6 +62,18 @@ static apr_status_t socket_cleanup(void *sock) } } +static apr_status_t socket_child_cleanup(void *sock) +{ + apr_socket_t *thesocket = sock; + if (close(thesocket->socketdes) == 0) { + thesocket->socketdes = -1; + return APR_SUCCESS; + } + else { + return errno; + } +} + static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol) { sock->type = type; @@ -92,6 +117,7 @@ apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, int protocol, apr_pool_t *cont) { int family = ofamily, flags = 0; + int oprotocol = protocol; #ifdef HAVE_SOCK_CLOEXEC flags |= SOCK_CLOEXEC; @@ -104,7 +130,11 @@ apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, family = APR_INET; #endif } - +#if APR_HAVE_SOCKADDR_UN + if (family == APR_UNIX) { + protocol = 0; + } +#endif alloc_socket(new, cont); #ifndef BEOS_R5 @@ -140,7 +170,7 @@ apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, if ((*new)->socketdes < 0) { return errno; } - set_socket_vars(*new, family, type, protocol); + set_socket_vars(*new, family, type, oprotocol); #ifndef HAVE_SOCK_CLOEXEC { @@ -167,7 +197,7 @@ apr_status_t apr_socket_create(apr_socket_t **new, int ofamily, int type, (*new)->timeout = -1; (*new)->inherit = 0; apr_pool_cleanup_register((*new)->pool, (void *)(*new), socket_cleanup, - socket_cleanup); + socket_child_cleanup); return APR_SUCCESS; } @@ -192,6 +222,13 @@ apr_status_t apr_socket_bind(apr_socket_t *sock, apr_sockaddr_t *sa) else { sock->local_addr = sa; /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ +#if APR_HAVE_SOCKADDR_UN + if (sock->local_addr->family == APR_UNIX) { + sock->bound = 1; + sock->local_port_unknown = 1; + } + else +#endif if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */ sock->local_port_unknown = 1; /* kernel got us an ephemeral port */ } @@ -282,6 +319,14 @@ apr_status_t apr_socket_accept(apr_socket_t **new, apr_socket_t *sock, (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr; } #endif +#if APR_HAVE_SOCKADDR_UN + else if (sock->local_addr->sa.sin.sin_family == AF_UNIX) { + *(*new)->remote_addr = *sock->local_addr; + (*new)->local_addr->ipaddr_ptr = &((*new)->local_addr->sa.unx.sun_path); + (*new)->remote_addr->ipaddr_ptr = &((*new)->remote_addr->sa.unx.sun_path); + } + if (sock->local_addr->sa.sin.sin_family != AF_UNIX) +#endif (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port); if (sock->local_port_unknown) { /* not likely for a listening socket, but theoretically possible :) */ @@ -375,7 +420,6 @@ apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa) #endif /* SO_ERROR */ } - if (memcmp(sa->ipaddr_ptr, generic_inaddr_any, sa->ipaddr_len)) { /* A real remote address was passed in. If the unspecified * address was used, the actual remote addr will have to be @@ -393,6 +437,13 @@ apr_status_t apr_socket_connect(apr_socket_t *sock, apr_sockaddr_t *sa) /* connect() got us an ephemeral port */ sock->local_port_unknown = 1; } +#if APR_HAVE_SOCKADDR_UN + if (sock->local_addr->sa.sin.sin_family == AF_UNIX) { + /* Assign connect address as local. */ + sock->local_addr = sa; + } + else +#endif if (!memcmp(sock->local_addr->ipaddr_ptr, generic_inaddr_any, sock->local_addr->ipaddr_len)) { diff --git a/network_io/unix/sockopt.c b/network_io/unix/sockopt.c index 6ce4b911efc04..6194e9b02c112 100644 --- a/network_io/unix/sockopt.c +++ b/network_io/unix/sockopt.c @@ -330,6 +330,20 @@ apr_status_t apr_socket_opt_set(apr_socket_t *sock, return APR_ENOTIMPL; #endif break; + case APR_SO_FREEBIND: +#if defined(IP_FREEBIND) + if (setsockopt(sock->socketdes, SOL_IP, IP_FREEBIND, + (void *)&one, sizeof(int)) == -1) { + return errno; + } + apr_set_option(sock, APR_SO_FREEBIND, on); +#elif 0 /* defined(IP_BINDANY) ... */ + /* TODO: insert FreeBSD support here, note family specific + * options, IP_BINDANY vs IPV6_BINDANY */ +#else + return APR_ENOTIMPL; +#endif + break; default: return APR_EINVAL; } @@ -428,3 +442,24 @@ apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *nonconst_name, return APR_SUCCESS; } #endif + +APR_PERMS_SET_IMPLEMENT(socket) +{ +#if APR_HAVE_SOCKADDR_UN + apr_status_t rv = APR_SUCCESS; + apr_socket_t *socket = (apr_socket_t *)thesocket; + + if (socket->local_addr->family == APR_UNIX) { + if (!(perms & APR_FPROT_GSETID)) + gid = -1; + if (fchown(socket->socketdes, uid, gid) < 0) { + rv = errno; + } + } + else + rv = APR_EINVAL; + return rv; +#else + return APR_ENOTIMPL; +#endif +} |