diff options
author | Valentin Bartenev <vbart@nginx.com> | 2018-04-18 17:28:22 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2018-04-18 17:28:22 +0300 |
commit | b06a0c95d08d3a3752353f5098e98ed3014170e6 (patch) | |
tree | 1a4150cb3f821710fbd78ac22a946429f626fc08 | |
parent | 54ccb32333782dd7f31771919af3fa73dce52eee (diff) | |
download | unit-b06a0c95d08d3a3752353f5098e98ed3014170e6.tar.gz unit-b06a0c95d08d3a3752353f5098e98ed3014170e6.tar.bz2 |
Removed duplicating socket address parsing function.
This also fixes #101 issue on GitHub. The function previously used to
parse IPv6 address of control socket was broken. Now the working function
is used instead.
-rw-r--r-- | src/nxt_runtime.c | 282 |
1 files changed, 3 insertions, 279 deletions
diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index b581209d..5907b14e 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -28,14 +28,6 @@ static nxt_int_t nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt); static nxt_int_t nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt); static nxt_int_t nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt); -static nxt_sockaddr_t *nxt_runtime_sockaddr_parse(nxt_task_t *task, - nxt_mp_t *mp, nxt_str_t *addr); -static nxt_sockaddr_t *nxt_runtime_sockaddr_unix_parse(nxt_task_t *task, - nxt_mp_t *mp, nxt_str_t *addr); -static nxt_sockaddr_t *nxt_runtime_sockaddr_inet6_parse(nxt_task_t *task, - nxt_mp_t *mp, nxt_str_t *addr); -static nxt_sockaddr_t *nxt_runtime_sockaddr_inet_parse(nxt_task_t *task, - nxt_mp_t *mp, nxt_str_t *addr); static nxt_int_t nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt); static nxt_int_t nxt_runtime_log_files_init(nxt_runtime_t *rt); static nxt_int_t nxt_runtime_log_files_create(nxt_task_t *task, @@ -800,11 +792,13 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) control.length = nxt_strlen(rt->control); control.start = (u_char *) rt->control; - sa = nxt_runtime_sockaddr_parse(task, rt->mem_pool, &control); + sa = nxt_sockaddr_parse(rt->mem_pool, &control); if (nxt_slow_path(sa == NULL)) { return NXT_ERROR; } + sa->type = SOCK_STREAM; + rt->controller_listen = sa; if (nxt_runtime_controller_socket(task, rt) != NXT_OK) { @@ -1016,276 +1010,6 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) } -static nxt_sockaddr_t * -nxt_runtime_sockaddr_parse(nxt_task_t *task, nxt_mp_t *mp, nxt_str_t *addr) -{ - u_char *p; - size_t length; - - length = addr->length; - p = addr->start; - - if (length >= 5 && nxt_memcmp(p, "unix:", 5) == 0) { - return nxt_runtime_sockaddr_unix_parse(task, mp, addr); - } - - if (length != 0 && *p == '[') { - return nxt_runtime_sockaddr_inet6_parse(task, mp, addr); - } - - return nxt_runtime_sockaddr_inet_parse(task, mp, addr); -} - - -static nxt_sockaddr_t * -nxt_runtime_sockaddr_unix_parse(nxt_task_t *task, nxt_mp_t *mp, nxt_str_t *addr) -{ -#if (NXT_HAVE_UNIX_DOMAIN) - u_char *p; - size_t length, socklen; - 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 = addr->length - 5; - p = addr->start + 5; - - if (length == 0) { - nxt_alert(task, "unix domain socket \"%V\" name is invalid", addr); - return NULL; - } - - if (length > max_len) { - nxt_alert(task, "unix domain socket \"%V\" name is too long", addr); - return NULL; - } - - 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 (p[0] == '@') { - p[0] = '\0'; - socklen--; - } - -#endif - - sa = nxt_sockaddr_alloc(mp, socklen, addr->length); - - if (nxt_slow_path(sa == NULL)) { - return NULL; - } - - sa->type = SOCK_STREAM; - - sa->u.sockaddr_un.sun_family = AF_UNIX; - nxt_memcpy(sa->u.sockaddr_un.sun_path, p, length); - - return sa; - -#else /* !(NXT_HAVE_UNIX_DOMAIN) */ - - nxt_alert(task, "unix domain socket \"%V\" is not supported", addr); - - return NULL; - -#endif -} - - -static nxt_sockaddr_t * -nxt_runtime_sockaddr_inet6_parse(nxt_task_t *task, nxt_mp_t *mp, - nxt_str_t *addr) -{ -#if (NXT_INET6) - u_char *p, *addr_end; - size_t length; - nxt_int_t port; - nxt_sockaddr_t *sa; - struct in6_addr *in6_addr; - - length = addr->length - 1; - p = addr->start + 1; - - addr_end = nxt_memchr(p, ']', length); - - if (addr_end == NULL) { - goto invalid_address; - } - - sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in6), - NXT_INET6_ADDR_STR_LEN); - if (nxt_slow_path(sa == NULL)) { - return NULL; - } - - in6_addr = &sa->u.sockaddr_in6.sin6_addr; - - if (nxt_inet6_addr(in6_addr, p, addr_end - p) != NXT_OK) { - goto invalid_address; - } - - port = 0; - p = addr_end + 1; - length = (p + length) - p; - - if (length == 0) { - goto found; - } - - if (*p == ':') { - port = nxt_int_parse(p + 1, length - 1); - - if (port >= 1 && port <= 65535) { - goto found; - } - } - - nxt_alert(task, "invalid port in \"%V\"", addr); - - return NULL; - -found: - - sa->type = SOCK_STREAM; - - sa->u.sockaddr_in6.sin6_family = AF_INET6; - sa->u.sockaddr_in6.sin6_port = htons((in_port_t) port); - - return sa; - -invalid_address: - - nxt_alert(task, "invalid IPv6 address in \"%V\"", addr); - - return NULL; - -#else - - nxt_alert(task, "IPv6 socket \"%V\" is not supported", addr); - - return NULL; - -#endif -} - - -static nxt_sockaddr_t * -nxt_runtime_sockaddr_inet_parse(nxt_task_t *task, nxt_mp_t *mp, - nxt_str_t *string) -{ - u_char *p, *ip; - size_t length; - in_addr_t addr; - nxt_int_t port; - nxt_sockaddr_t *sa; - - addr = INADDR_ANY; - - length = string->length; - ip = string->start; - - p = nxt_memchr(ip, ':', length); - - if (p == NULL) { - - /* single value port, or address */ - - port = nxt_int_parse(ip, length); - - if (port > 0) { - /* "*:XX" */ - - if (port < 1 || port > 65535) { - goto invalid_port; - } - - } else { - /* "x.x.x.x" */ - - addr = nxt_inet_addr(ip, length); - - if (addr == INADDR_NONE) { - goto invalid_port; - } - - port = 8080; - } - - } else { - - /* x.x.x.x:XX */ - - p++; - length = (ip + length) - p; - port = nxt_int_parse(p, length); - - if (port < 1 || port > 65535) { - goto invalid_port; - } - - length = (p - 1) - ip; - - if (length != 1 || ip[0] != '*') { - addr = nxt_inet_addr(ip, length); - - if (addr == INADDR_NONE) { - goto invalid_addr; - } - - /* "x.x.x.x:XX" */ - } - } - - sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in), - NXT_INET_ADDR_STR_LEN); - if (nxt_slow_path(sa == NULL)) { - return NULL; - } - - sa->type = SOCK_STREAM; - - sa->u.sockaddr_in.sin_family = AF_INET; - sa->u.sockaddr_in.sin_port = htons((in_port_t) port); - sa->u.sockaddr_in.sin_addr.s_addr = addr; - - return sa; - -invalid_port: - - nxt_alert(task, "invalid port in \"%V\"", string); - - return NULL; - -invalid_addr: - - nxt_alert(task, "invalid address in \"%V\"", string); - - return NULL; -} - - nxt_listen_socket_t * nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) { |