diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /src/nxt_job_resolve.c | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_job_resolve.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/nxt_job_resolve.c b/src/nxt_job_resolve.c new file mode 100644 index 00000000..600f1aae --- /dev/null +++ b/src/nxt_job_resolve.c @@ -0,0 +1,125 @@ + +/* + * 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 len; + nxt_uint_t n; + nxt_mem_pool_t *mp; + 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; + + len = jbr->name.len + 1; + + if (nxt_fast_path(len <= NXT_BUFSIZE)) { + host = buf; + + } else { + host = nxt_mem_alloc(jbr->job.mem_pool, len); + if (nxt_slow_path(host == NULL)) { + goto fail; + } + } + + nxt_cpystrn(host, jbr->name.data, len); + + 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_mem_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: +#endif + case AF_INET: + break; + + default: + continue; + } + + sa = nxt_sockaddr_create(mp, r->ai_addr, r->ai_addrlen); + 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); + } + + nxt_job_return(nxt_thread(), &jbr->job, handler); +} |