ngx_parse_addr()

This commit is contained in:
Igor Sysoev 2009-11-02 15:20:42 +00:00
parent 0c189c5159
commit 1d52beba73
2 changed files with 64 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);