mirror of
https://github.com/nginx/nginx.git
synced 2025-06-19 02:40:49 +08:00
ngx_ptocidr() supports IPv6
This commit is contained in:
parent
fd078fd6d5
commit
4ac5ca8ae0
@ -354,31 +354,47 @@ ngx_inet6_ntop(u_char *p, u_char *text, size_t len)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* AF_INET only */
|
|
||||||
|
|
||||||
ngx_int_t
|
ngx_int_t
|
||||||
ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)
|
ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)
|
||||||
{
|
{
|
||||||
u_char *addr, *mask, *last;
|
u_char *addr, *mask, *last;
|
||||||
|
size_t len;
|
||||||
ngx_int_t shift;
|
ngx_int_t shift;
|
||||||
|
#if (NGX_HAVE_INET6)
|
||||||
|
ngx_int_t rc;
|
||||||
|
ngx_uint_t s, i;
|
||||||
|
#endif
|
||||||
|
|
||||||
addr = text->data;
|
addr = text->data;
|
||||||
last = addr + text->len;
|
last = addr + text->len;
|
||||||
|
|
||||||
mask = ngx_strlchr(addr, last, '/');
|
mask = ngx_strlchr(addr, last, '/');
|
||||||
|
len = (mask ? mask : last) - addr;
|
||||||
|
|
||||||
cidr->u.in.addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
|
cidr->u.in.addr = ngx_inet_addr(addr, len);
|
||||||
|
|
||||||
if (cidr->u.in.addr == INADDR_NONE) {
|
if (cidr->u.in.addr != INADDR_NONE) {
|
||||||
return NGX_ERROR;
|
cidr->family = AF_INET;
|
||||||
}
|
|
||||||
|
|
||||||
if (mask == NULL) {
|
if (mask == NULL) {
|
||||||
cidr->family = AF_INET;
|
|
||||||
cidr->u.in.mask = 0xffffffff;
|
cidr->u.in.mask = 0xffffffff;
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (NGX_HAVE_INET6)
|
||||||
|
} else if (ngx_inet6_addr(addr, len, cidr->u.in6.addr.s6_addr) == NGX_OK) {
|
||||||
|
cidr->family = AF_INET6;
|
||||||
|
|
||||||
|
if (mask == NULL) {
|
||||||
|
ngx_memset(cidr->u.in6.mask.s6_addr, 0xff, 16);
|
||||||
|
return NGX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
mask++;
|
mask++;
|
||||||
|
|
||||||
shift = ngx_atoi(mask, last - mask);
|
shift = ngx_atoi(mask, last - mask);
|
||||||
@ -386,23 +402,40 @@ ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)
|
|||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
cidr->family = AF_INET;
|
switch (cidr->family) {
|
||||||
|
|
||||||
if (shift == 0) {
|
#if (NGX_HAVE_INET6)
|
||||||
|
case AF_INET6:
|
||||||
|
addr = cidr->u.in6.addr.s6_addr;
|
||||||
|
mask = cidr->u.in6.mask.s6_addr;
|
||||||
|
rc = NGX_OK;
|
||||||
|
|
||||||
/* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
for (i = 0; i < 16; i++) {
|
||||||
|
|
||||||
cidr->u.in.mask = 0;
|
s = (shift > 8) ? 8 : shift;
|
||||||
|
shift -= s;
|
||||||
|
|
||||||
if (cidr->u.in.addr == 0) {
|
mask[i] = (u_char) (0 - (1 << (8 - s)));
|
||||||
return NGX_OK;
|
|
||||||
|
if (addr[i] != (addr[i] & mask[i])) {
|
||||||
|
rc = NGX_DONE;
|
||||||
|
addr[i] &= mask[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NGX_DONE;
|
return rc;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
|
default: /* AF_INET */
|
||||||
|
|
||||||
|
if (shift) {
|
||||||
cidr->u.in.mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
|
cidr->u.in.mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
/* x86 compilers use a shl instruction that shifts by modulo 32 */
|
||||||
|
cidr->u.in.mask = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (cidr->u.in.addr == (cidr->u.in.addr & cidr->u.in.mask)) {
|
if (cidr->u.in.addr == (cidr->u.in.addr & cidr->u.in.mask)) {
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
@ -410,6 +443,7 @@ ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr)
|
|||||||
cidr->u.in.addr &= cidr->u.in.mask;
|
cidr->u.in.addr &= cidr->u.in.mask;
|
||||||
|
|
||||||
return NGX_DONE;
|
return NGX_DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -422,6 +456,12 @@ ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text, size_t len)
|
|||||||
#if (NGX_HAVE_INET6)
|
#if (NGX_HAVE_INET6)
|
||||||
struct in6_addr inaddr6;
|
struct in6_addr inaddr6;
|
||||||
struct sockaddr_in6 *sin6;
|
struct sockaddr_in6 *sin6;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* prevent MSVC8 waring:
|
||||||
|
* potentially uninitialized local variable 'inaddr6' used
|
||||||
|
*/
|
||||||
|
ngx_memzero(inaddr6.s6_addr, sizeof(struct in6_addr));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inaddr = ngx_inet_addr(text, len);
|
inaddr = ngx_inet_addr(text, len);
|
||||||
|
Loading…
Reference in New Issue
Block a user