mirror of
https://github.com/nginx/nginx.git
synced 2025-06-07 01:12:40 +08:00
*) refactor ngx_ptocidr()
*) allow address without bitmask *) thus now ngx_http_geo_module accepts addresses without bitmask
This commit is contained in:
parent
caa4a45c0f
commit
9c388c0a7f
@ -102,43 +102,48 @@ ngx_inet_ntop(int family, void *addr, u_char *text, size_t len)
|
|||||||
ngx_int_t
|
ngx_int_t
|
||||||
ngx_ptocidr(ngx_str_t *text, void *cidr)
|
ngx_ptocidr(ngx_str_t *text, void *cidr)
|
||||||
{
|
{
|
||||||
ngx_int_t m;
|
u_char *addr, *mask, *last;
|
||||||
ngx_uint_t i;
|
ngx_int_t shift;
|
||||||
ngx_inet_cidr_t *in_cidr;
|
ngx_inet_cidr_t *in_cidr;
|
||||||
|
|
||||||
in_cidr = cidr;
|
in_cidr = cidr;
|
||||||
|
addr = text->data;
|
||||||
|
last = addr + text->len;
|
||||||
|
|
||||||
for (i = 0; i < text->len; i++) {
|
mask = ngx_strlchr(addr, last, '/');
|
||||||
if (text->data[i] == '/') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i == text->len) {
|
in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
text->data[i] = '\0';
|
|
||||||
in_cidr->addr = inet_addr((char *) text->data);
|
|
||||||
text->data[i] = '/';
|
|
||||||
if (in_cidr->addr == INADDR_NONE) {
|
if (in_cidr->addr == INADDR_NONE) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
m = ngx_atoi(&text->data[i + 1], text->len - (i + 1));
|
if (mask == NULL) {
|
||||||
if (m == NGX_ERROR) {
|
in_cidr->mask = 0xffffffff;
|
||||||
|
return NGX_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
mask++;
|
||||||
|
|
||||||
|
shift = ngx_atoi(mask, last - mask);
|
||||||
|
if (shift == NGX_ERROR) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m == 0) {
|
if (shift == 0) {
|
||||||
|
|
||||||
/* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
/* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
||||||
|
|
||||||
in_cidr->mask = 0;
|
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)) {
|
if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) {
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
|
@ -1051,13 +1051,6 @@ ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
return NGX_CONF_ERROR;
|
return NGX_CONF_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
dc->addr = inet_addr((char *) value[1].data);
|
|
||||||
|
|
||||||
if (dc->addr != INADDR_NONE) {
|
|
||||||
dc->mask = 0xffffffff;
|
|
||||||
return NGX_CONF_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ngx_ptocidr(&value[1], &in_cidr);
|
rc = ngx_ptocidr(&value[1], &in_cidr);
|
||||||
|
|
||||||
if (rc == NGX_DONE) {
|
if (rc == NGX_DONE) {
|
||||||
|
@ -166,14 +166,6 @@ ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
return NGX_CONF_OK;
|
return NGX_CONF_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
rule->addr = inet_addr((char *) value[1].data);
|
|
||||||
|
|
||||||
if (rule->addr != INADDR_NONE) {
|
|
||||||
rule->mask = 0xffffffff;
|
|
||||||
|
|
||||||
return NGX_CONF_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ngx_ptocidr(&value[1], &in_cidr);
|
rc = ngx_ptocidr(&value[1], &in_cidr);
|
||||||
|
|
||||||
if (rc == NGX_ERROR) {
|
if (rc == NGX_ERROR) {
|
||||||
|
@ -251,14 +251,6 @@ ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
|
|
||||||
value = cf->args->elts;
|
value = cf->args->elts;
|
||||||
|
|
||||||
from->addr = inet_addr((char *) value[1].data);
|
|
||||||
|
|
||||||
if (from->addr != INADDR_NONE) {
|
|
||||||
from->mask = 0xffffffff;
|
|
||||||
|
|
||||||
return NGX_CONF_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ngx_ptocidr(&value[1], &in_cidr);
|
rc = ngx_ptocidr(&value[1], &in_cidr);
|
||||||
|
|
||||||
if (rc == NGX_ERROR) {
|
if (rc == NGX_ERROR) {
|
||||||
|
Loading…
Reference in New Issue
Block a user