From 1d52beba7339a6edf235a2160bc20fc466e1823a Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Mon, 2 Nov 2009 15:20:42 +0000 Subject: [PATCH] ngx_parse_addr() --- src/core/ngx_inet.c | 63 +++++++++++++++++++++++++++++++++++++++++++++ src/core/ngx_inet.h | 1 + 2 files changed, 64 insertions(+) diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c index 5d2f86ecf..e051f7f0f 100644 --- a/src/core/ngx_inet.c +++ b/src/core/ngx_inet.c @@ -416,6 +416,69 @@ ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr) } +ngx_addr_t * +ngx_parse_addr(ngx_pool_t *pool, ngx_str_t *addr) +{ + size_t len; + in_addr_t inaddr; + ngx_uint_t family; + ngx_addr_t *a; + struct sockaddr_in *sin; +#if (NGX_HAVE_INET6) + struct in6_addr inaddr6; + struct sockaddr_in6 *sin6; +#endif + + inaddr = ngx_inet_addr(addr->data, addr->len); + + if (inaddr != INADDR_NONE) { + family = AF_INET; + len = sizeof(struct sockaddr_in); + +#if (NGX_HAVE_INET6) + } else if (ngx_inet6_addr(addr->data, addr->len, inaddr6.s6_addr) == NGX_OK) + { + family = AF_INET6; + len = sizeof(struct sockaddr_in6); + +#endif + } else { + return NULL; + } + + a = ngx_palloc(pool, sizeof(ngx_addr_t)); + if (a == NULL) { + return NULL; + } + + a->sockaddr = ngx_pcalloc(pool, len); + if (a->sockaddr == NULL) { + return NULL; + } + + a->sockaddr->sa_family = family; + a->socklen = len; + a->name = *addr; + + switch (family) { + +#if (NGX_HAVE_INET6) + case AF_INET6: + sin6 = (struct sockaddr_in6 *) a->sockaddr; + ngx_memcpy(sin6->sin6_addr.s6_addr, inaddr6.s6_addr, 16); + break; +#endif + + default: /* AF_INET */ + sin = (struct sockaddr_in *) a->sockaddr; + sin->sin_addr.s_addr = inaddr; + break; + } + + return a; +} + + ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u) { diff --git a/src/core/ngx_inet.h b/src/core/ngx_inet.h index 7e8cce68d..3ac2e372e 100644 --- a/src/core/ngx_inet.h +++ b/src/core/ngx_inet.h @@ -109,6 +109,7 @@ size_t ngx_sock_ntop(struct sockaddr *sa, u_char *text, size_t len, ngx_uint_t port); size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len); ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr); +ngx_addr_t *ngx_parse_addr(ngx_pool_t *pool, ngx_str_t *addr); ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u); ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);