Mercurial > hg > nginx
diff src/core/ngx_inet.c @ 2202:2300ab9b069e
*) refactor ngx_ptocidr()
*) allow address without bitmask
*) thus now ngx_http_geo_module accepts addresses without bitmask
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 26 Aug 2008 14:19:37 +0000 |
parents | fd15c193572d |
children | 8e5bf1bc87e2 |
line wrap: on
line diff
--- a/src/core/ngx_inet.c +++ b/src/core/ngx_inet.c @@ -102,43 +102,48 @@ ngx_inet_ntop(int family, void *addr, u_ ngx_int_t ngx_ptocidr(ngx_str_t *text, void *cidr) { - ngx_int_t m; - ngx_uint_t i; + u_char *addr, *mask, *last; + ngx_int_t shift; ngx_inet_cidr_t *in_cidr; in_cidr = cidr; - - for (i = 0; i < text->len; i++) { - if (text->data[i] == '/') { - break; - } - } + addr = text->data; + last = addr + text->len; - if (i == text->len) { - return NGX_ERROR; - } + mask = ngx_strlchr(addr, last, '/'); - text->data[i] = '\0'; - in_cidr->addr = inet_addr((char *) text->data); - text->data[i] = '/'; + in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr); + if (in_cidr->addr == INADDR_NONE) { return NGX_ERROR; } - m = ngx_atoi(&text->data[i + 1], text->len - (i + 1)); - if (m == NGX_ERROR) { + if (mask == NULL) { + in_cidr->mask = 0xffffffff; + return NGX_OK; + } + + mask++; + + shift = ngx_atoi(mask, last - mask); + if (shift == NGX_ERROR) { return NGX_ERROR; } - if (m == 0) { + if (shift == 0) { /* the x86 compilers use the shl instruction that shifts by modulo 32 */ in_cidr->mask = 0; - return NGX_OK; + + if (in_cidr->addr == 0) { + return NGX_OK; + } + + return NGX_DONE; } - in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m)))); + in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift)))); if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) { return NGX_OK;