diff options
-rw-r--r-- | auto/sources | 1 | ||||
-rw-r--r-- | src/nxt_job_resolve.c | 132 | ||||
-rw-r--r-- | src/nxt_job_resolve.h | 29 | ||||
-rw-r--r-- | src/nxt_main.h | 1 | ||||
-rw-r--r-- | src/nxt_sockaddr.c | 412 | ||||
-rw-r--r-- | src/nxt_sockaddr.h | 12 |
6 files changed, 0 insertions, 587 deletions
diff --git a/auto/sources b/auto/sources index 65a83f45..560f3bf3 100644 --- a/auto/sources +++ b/auto/sources @@ -69,7 +69,6 @@ NXT_LIB_SRCS=" \ src/nxt_event_conn_job_sendfile.c \ src/nxt_conn_proxy.c \ src/nxt_job.c \ - src/nxt_job_resolve.c \ src/nxt_sockaddr.c \ src/nxt_listen_socket.c \ src/nxt_upstream.c \ diff --git a/src/nxt_job_resolve.c b/src/nxt_job_resolve.c deleted file mode 100644 index 0f1fb9aa..00000000 --- a/src/nxt_job_resolve.c +++ /dev/null @@ -1,132 +0,0 @@ - -/* - * Copyright (C) Igor Sysoev - * Copyright (C) NGINX, Inc. - */ - -#include <nxt_main.h> - - -void -nxt_job_resolve(nxt_job_resolve_t *jbr) -{ - int err; - u_char *host; - size_t length; - nxt_mp_t *mp; - nxt_uint_t n; - nxt_sockaddr_t *sa; - struct addrinfo hint, *res, *r; - nxt_work_handler_t handler; - - #define NXT_BUFSIZE 64 - u_char buf[NXT_BUFSIZE]; - - handler = jbr->error_handler; - res = NULL; - - length = jbr->name.length + 1; - - if (nxt_fast_path(length <= NXT_BUFSIZE)) { - host = buf; - - } else { - host = nxt_mp_alloc(jbr->job.mem_pool, length); - if (nxt_slow_path(host == NULL)) { - goto fail; - } - } - - nxt_cpystrn(host, jbr->name.start, length); - - nxt_memzero(&hint, sizeof(struct addrinfo)); - hint.ai_socktype = SOCK_STREAM; - - err = getaddrinfo((char *) host, NULL, &hint, &res); - - if (err != 0) { - nxt_thread_log_error(jbr->log_level, - "getaddrinfo(\"%s\") failed (%d: %s)", - host, err, gai_strerror(err)); - goto fail; - } - - n = 0; - for (r = res; r != NULL; r = r->ai_next) { - - switch (r->ai_addr->sa_family) { -#if (NXT_INET6) - case AF_INET6: -#endif - case AF_INET: - n++; - break; - - default: - break; - } - } - - jbr->count = n; - mp = jbr->job.mem_pool; - - jbr->sockaddrs = nxt_mp_alloc(mp, n * sizeof(nxt_sockaddr_t *)); - if (nxt_slow_path(jbr->sockaddrs == NULL)) { - goto fail; - } - - n = 0; - for (r = res; r != NULL; r = r->ai_next) { - - switch (r->ai_addr->sa_family) { -#if (NXT_INET6) - case AF_INET6: - length = NXT_INET6_ADDR_STR_LEN; - break; -#endif - case AF_INET: - length = NXT_INET_ADDR_STR_LEN; - break; - - default: - continue; - } - - sa = nxt_sockaddr_create(mp, r->ai_addr, r->ai_addrlen, length); - if (nxt_slow_path(sa == NULL)) { - goto fail; - } - - jbr->sockaddrs[n++] = sa; - - if (jbr->port != 0) { - - switch (sa->u.sockaddr.sa_family) { - case AF_INET: - sa->u.sockaddr_in.sin_port = jbr->port; - break; -#if (NXT_INET6) - case AF_INET6: - sa->u.sockaddr_in6.sin6_port = jbr->port; - break; -#endif - default: - break; - } - } - } - - handler = jbr->ready_handler; - -fail: - - if (nxt_fast_path(res != NULL)) { - freeaddrinfo(res); - } - - if (host != buf) { - nxt_mp_free(jbr->job.mem_pool, host); - } - - nxt_job_return(jbr->job.task, &jbr->job, handler); -} diff --git a/src/nxt_job_resolve.h b/src/nxt_job_resolve.h deleted file mode 100644 index fef55b99..00000000 --- a/src/nxt_job_resolve.h +++ /dev/null @@ -1,29 +0,0 @@ - -/* - * Copyright (C) Igor Sysoev - * Copyright (C) NGINX, Inc. - */ - -#ifndef _NXT_JOB_RESOLVE_H_INCLUDED_ -#define _NXT_JOB_RESOLVE_H_INCLUDED_ - - -typedef struct { - nxt_job_t job; - nxt_str_t name; - - uint32_t log_level; /* 4 bits */ - in_port_t port; - uint16_t count; - - nxt_sockaddr_t **sockaddrs; - - nxt_work_handler_t ready_handler; - nxt_work_handler_t error_handler; -} nxt_job_resolve_t; - - -void nxt_job_resolve(nxt_job_resolve_t *jbr); - - -#endif /* _NXT_JOB_RESOLVE_H_INCLUDED_ */ diff --git a/src/nxt_main.h b/src/nxt_main.h index 7f812568..dca4b6dc 100644 --- a/src/nxt_main.h +++ b/src/nxt_main.h @@ -146,7 +146,6 @@ typedef void (*nxt_event_conn_handler_t)(nxt_thread_t *thr, nxt_conn_t *c); #include <nxt_job_file.h> #include <nxt_buf_filter.h> -#include <nxt_job_resolve.h> #include <nxt_sockaddr.h> #include <nxt_cache.h> diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c index e8dee83a..8220590e 100644 --- a/src/nxt_sockaddr.c +++ b/src/nxt_sockaddr.c @@ -15,10 +15,6 @@ static nxt_sockaddr_t *nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr); static nxt_sockaddr_t *nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr); static nxt_sockaddr_t *nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr); -static nxt_int_t nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs); -static nxt_int_t nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs); -static nxt_int_t nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs); - nxt_sockaddr_t * nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls) @@ -433,82 +429,6 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) } -size_t -nxt_sockaddr_ntop(nxt_sockaddr_t *sa, u_char *buf, u_char *end, nxt_bool_t port) -{ - u_char *p; - - switch (sa->u.sockaddr.sa_family) { - - case AF_INET: - p = (u_char *) &sa->u.sockaddr_in.sin_addr; - - if (port) { - p = nxt_sprintf(buf, end, "%ud.%ud.%ud.%ud:%d", - p[0], p[1], p[2], p[3], - ntohs(sa->u.sockaddr_in.sin_port)); - } else { - p = nxt_sprintf(buf, end, "%ud.%ud.%ud.%ud", - p[0], p[1], p[2], p[3]); - } - - return p - buf; - -#if (NXT_INET6) - - case AF_INET6: - p = buf; - - if (port) { - *p++ = '['; - } - - p = nxt_inet6_ntop(sa->u.sockaddr_in6.sin6_addr.s6_addr, p, end); - - if (port) { - p = nxt_sprintf(p, end, "]:%d", - ntohs(sa->u.sockaddr_in6.sin6_port)); - } - - return p - buf; -#endif - -#if (NXT_HAVE_UNIX_DOMAIN) - - case AF_UNIX: - -#if (NXT_LINUX) - - p = (u_char *) sa->u.sockaddr_un.sun_path; - - if (p[0] == '\0') { - size_t length; - - /* Linux abstract socket address has no trailing zero. */ - - length = sa->socklen - offsetof(struct sockaddr_un, sun_path) - 1; - p = nxt_sprintf(buf, end, "unix:\\0%*s", length, p + 1); - - } else { - p = nxt_sprintf(buf, end, "unix:%s", p); - } - -#else /* !(NXT_LINUX) */ - - p = nxt_sprintf(buf, end, "unix:%s", sa->u.sockaddr_un.sun_path); - -#endif - - return p - buf; - -#endif /* NXT_HAVE_UNIX_DOMAIN */ - - default: - return 0; - } -} - - #if (NXT_INET6) static u_char * @@ -849,338 +769,6 @@ nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr) } -void -nxt_job_sockaddr_parse(nxt_job_sockaddr_parse_t *jbs) -{ - u_char *p; - size_t length; - nxt_int_t ret; - nxt_work_handler_t handler; - - nxt_job_set_name(&jbs->resolve.job, "job sockaddr parse"); - - length = jbs->addr.length; - p = jbs->addr.start; - - if (length > 6 && nxt_memcmp(p, "unix:", 5) == 0) { - ret = nxt_job_sockaddr_unix_parse(jbs); - - } else if (length != 0 && *p == '[') { - ret = nxt_job_sockaddr_inet6_parse(jbs); - - } else { - ret = nxt_job_sockaddr_inet_parse(jbs); - } - - switch (ret) { - - case NXT_OK: - handler = jbs->resolve.ready_handler; - break; - - case NXT_ERROR: - handler = jbs->resolve.error_handler; - break; - - default: /* NXT_AGAIN */ - return; - } - - nxt_job_return(jbs->resolve.job.task, &jbs->resolve.job, handler); -} - - -static nxt_int_t -nxt_job_sockaddr_unix_parse(nxt_job_sockaddr_parse_t *jbs) -{ -#if (NXT_HAVE_UNIX_DOMAIN) - size_t length, socklen; - u_char *path; - nxt_mp_t *mp; - nxt_sockaddr_t *sa; - - /* - * Actual sockaddr_un length can be lesser or even larger than defined - * struct sockaddr_un length (see comment in nxt_socket.h). So - * limit maximum Unix domain socket address length by defined sun_path[] - * length because some OSes accept addresses twice larger than defined - * struct sockaddr_un. Also reserve space for a trailing zero to avoid - * ambiguity, since many OSes accept Unix domain socket addresses - * without a trailing zero. - */ - const size_t max_len = sizeof(struct sockaddr_un) - - offsetof(struct sockaddr_un, sun_path) - 1; - - /* cutting "unix:" */ - length = jbs->addr.length - 5; - path = jbs->addr.start + 5; - - if (length > max_len) { - nxt_thread_log_error(jbs->resolve.log_level, - "unix domain socket \"%V\" name is too long", - &jbs->addr); - return NXT_ERROR; - } - - socklen = offsetof(struct sockaddr_un, sun_path) + length + 1; - -#if (NXT_LINUX) - - /* - * Linux unix(7): - * - * abstract: an abstract socket address is distinguished by the fact - * that sun_path[0] is a null byte ('\0'). The socket's address in - * this namespace is given by the additional bytes in sun_path that - * are covered by the specified length of the address structure. - * (Null bytes in the name have no special significance.) - */ - if (path[0] == '\0') { - socklen--; - } - -#endif - - mp = jbs->resolve.job.mem_pool; - - jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *)); - - if (nxt_fast_path(jbs->resolve.sockaddrs != NULL)) { - sa = nxt_sockaddr_alloc(mp, socklen, jbs->addr.length); - - if (nxt_fast_path(sa != NULL)) { - jbs->resolve.count = 1; - jbs->resolve.sockaddrs[0] = sa; - - sa->u.sockaddr_un.sun_family = AF_UNIX; - nxt_memcpy(sa->u.sockaddr_un.sun_path, path, length); - - return NXT_OK; - } - } - - return NXT_ERROR; - -#else /* !(NXT_HAVE_UNIX_DOMAIN) */ - - nxt_thread_log_error(jbs->resolve.log_level, - "unix domain socket \"%V\" is not supported", - &jbs->addr); - return NXT_ERROR; - -#endif -} - - -static nxt_int_t -nxt_job_sockaddr_inet6_parse(nxt_job_sockaddr_parse_t *jbs) -{ -#if (NXT_INET6) - u_char *p, *addr, *addr_end; - size_t length; - nxt_mp_t *mp; - nxt_int_t port; - nxt_sockaddr_t *sa; - struct in6_addr *in6_addr; - - length = jbs->addr.length - 1; - addr = jbs->addr.start + 1; - - addr_end = nxt_memchr(addr, ']', length); - - if (addr_end == NULL) { - goto invalid_address; - } - - mp = jbs->resolve.job.mem_pool; - - jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *)); - - if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) { - return NXT_ERROR; - } - - sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in6), - NXT_INET6_ADDR_STR_LEN); - - if (nxt_slow_path(sa == NULL)) { - return NXT_ERROR; - } - - jbs->resolve.count = 1; - jbs->resolve.sockaddrs[0] = sa; - - in6_addr = &sa->u.sockaddr_in6.sin6_addr; - - if (nxt_inet6_addr(in6_addr, addr, addr_end - addr) != NXT_OK) { - goto invalid_address; - } - - p = addr_end + 1; - length = (addr + length) - p; - - if (length == 0) { - jbs->no_port = 1; - port = jbs->resolve.port; - goto found; - } - - if (*p == ':') { - port = nxt_int_parse(p + 1, length - 1); - - if (port >= 1 && port <= 65535) { - port = htons((in_port_t) port); - goto found; - } - } - - nxt_thread_log_error(jbs->resolve.log_level, - "invalid port in \"%V\"", &jbs->addr); - - return NXT_ERROR; - -found: - - sa->u.sockaddr_in6.sin6_family = AF_INET6; - sa->u.sockaddr_in6.sin6_port = (in_port_t) port; - - if (IN6_IS_ADDR_UNSPECIFIED(in6_addr)) { - jbs->wildcard = 1; - } - - return NXT_OK; - -invalid_address: - - nxt_thread_log_error(jbs->resolve.log_level, - "invalid IPv6 address in \"%V\"", &jbs->addr); - return NXT_ERROR; - -#else - - nxt_thread_log_error(jbs->resolve.log_level, - "IPv6 socket \"%V\" is not supported", &jbs->addr); - return NXT_ERROR; - -#endif -} - - -static nxt_int_t -nxt_job_sockaddr_inet_parse(nxt_job_sockaddr_parse_t *jbs) -{ - u_char *p, *host; - size_t length; - nxt_mp_t *mp; - nxt_int_t port; - in_addr_t addr; - nxt_sockaddr_t *sa; - - addr = INADDR_ANY; - - length = jbs->addr.length; - host = jbs->addr.start; - - p = nxt_memchr(host, ':', length); - - if (p == NULL) { - - /* single value port, address, or host name */ - - port = nxt_int_parse(host, length); - - if (port > 0) { - if (port > 65535) { - goto invalid_port; - } - - /* "*:XX" */ - port = htons((in_port_t) port); - jbs->resolve.port = (in_port_t) port; - - } else { - jbs->no_port = 1; - - addr = nxt_inet_addr(host, length); - - if (addr == INADDR_NONE) { - jbs->resolve.name.length = length; - jbs->resolve.name.start = host; - - nxt_job_resolve(&jbs->resolve); - return NXT_AGAIN; - } - - /* "x.x.x.x" */ - port = jbs->resolve.port; - } - - } else { - - /* x.x.x.x:XX or host:XX */ - - p++; - length = (host + length) - p; - port = nxt_int_parse(p, length); - - if (port < 1 || port > 65535) { - goto invalid_port; - } - - port = htons((in_port_t) port); - - length = (p - 1) - host; - - if (length != 1 || host[0] != '*') { - addr = nxt_inet_addr(host, length); - - if (addr == INADDR_NONE) { - jbs->resolve.name.length = length; - jbs->resolve.name.start = host; - jbs->resolve.port = (in_port_t) port; - - nxt_job_resolve(&jbs->resolve); - return NXT_AGAIN; - } - - /* "x.x.x.x:XX" */ - } - } - - mp = jbs->resolve.job.mem_pool; - - jbs->resolve.sockaddrs = nxt_mp_alloc(mp, sizeof(void *)); - if (nxt_slow_path(jbs->resolve.sockaddrs == NULL)) { - return NXT_ERROR; - } - - sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in), - NXT_INET_ADDR_STR_LEN); - - if (nxt_fast_path(sa != NULL)) { - jbs->resolve.count = 1; - jbs->resolve.sockaddrs[0] = sa; - - jbs->wildcard = (addr == INADDR_ANY); - - sa->u.sockaddr_in.sin_family = AF_INET; - sa->u.sockaddr_in.sin_port = (in_port_t) port; - sa->u.sockaddr_in.sin_addr.s_addr = addr; - - return NXT_OK; - } - - return NXT_ERROR; - -invalid_port: - - nxt_thread_log_error(jbs->resolve.log_level, - "invalid port in \"%V\"", &jbs->addr); - - return NXT_ERROR; -} - - in_addr_t nxt_inet_addr(u_char *buf, size_t length) { diff --git a/src/nxt_sockaddr.h b/src/nxt_sockaddr.h index a8f1b393..0f96f6dd 100644 --- a/src/nxt_sockaddr.h +++ b/src/nxt_sockaddr.h @@ -58,15 +58,6 @@ struct nxt_sockaddr_s { }; -typedef struct { - nxt_job_resolve_t resolve; - nxt_str_t addr; - - uint8_t wildcard; /* 1 bit */ - uint8_t no_port; /* 1 bit */ -} nxt_job_sockaddr_parse_t; - - nxt_sockaddr_t *nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls); void nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c); @@ -88,12 +79,9 @@ NXT_EXPORT void nxt_sockaddr_text(nxt_sockaddr_t *sa); NXT_EXPORT uint32_t nxt_sockaddr_port_number(nxt_sockaddr_t *sa); NXT_EXPORT nxt_bool_t nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2); -NXT_EXPORT size_t nxt_sockaddr_ntop(nxt_sockaddr_t *sa, u_char *buf, - u_char *end, nxt_bool_t port); NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr); NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr); -NXT_EXPORT void nxt_job_sockaddr_parse(nxt_job_sockaddr_parse_t *jbs); NXT_EXPORT in_addr_t nxt_inet_addr(u_char *buf, size_t len); #if (NXT_INET6) NXT_EXPORT nxt_int_t nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, |