*) refactor ngx_ptocidr()
*) allow address without bitmask
*) thus now ngx_http_geo_module accepts addresses without bitmask
Igor Sysoev
13 years ago
101 | 101 |
ngx_int_t
|
102 | 102 |
ngx_ptocidr(ngx_str_t *text, void *cidr)
|
103 | 103 |
{
|
104 | |
ngx_int_t m;
|
105 | |
ngx_uint_t i;
|
|
104 |
u_char *addr, *mask, *last;
|
|
105 |
ngx_int_t shift;
|
106 | 106 |
ngx_inet_cidr_t *in_cidr;
|
107 | 107 |
|
108 | 108 |
in_cidr = cidr;
|
109 | |
|
110 | |
for (i = 0; i < text->len; i++) {
|
111 | |
if (text->data[i] == '/') {
|
112 | |
break;
|
113 | |
}
|
114 | |
}
|
115 | |
|
116 | |
if (i == text->len) {
|
117 | |
return NGX_ERROR;
|
118 | |
}
|
119 | |
|
120 | |
text->data[i] = '\0';
|
121 | |
in_cidr->addr = inet_addr((char *) text->data);
|
122 | |
text->data[i] = '/';
|
|
109 |
addr = text->data;
|
|
110 |
last = addr + text->len;
|
|
111 |
|
|
112 |
mask = ngx_strlchr(addr, last, '/');
|
|
113 |
|
|
114 |
in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr);
|
|
115 |
|
123 | 116 |
if (in_cidr->addr == INADDR_NONE) {
|
124 | 117 |
return NGX_ERROR;
|
125 | 118 |
}
|
126 | 119 |
|
127 | |
m = ngx_atoi(&text->data[i + 1], text->len - (i + 1));
|
128 | |
if (m == NGX_ERROR) {
|
129 | |
return NGX_ERROR;
|
130 | |
}
|
131 | |
|
132 | |
if (m == 0) {
|
|
120 |
if (mask == NULL) {
|
|
121 |
in_cidr->mask = 0xffffffff;
|
|
122 |
return NGX_OK;
|
|
123 |
}
|
|
124 |
|
|
125 |
mask++;
|
|
126 |
|
|
127 |
shift = ngx_atoi(mask, last - mask);
|
|
128 |
if (shift == NGX_ERROR) {
|
|
129 |
return NGX_ERROR;
|
|
130 |
}
|
|
131 |
|
|
132 |
if (shift == 0) {
|
133 | 133 |
|
134 | 134 |
/* the x86 compilers use the shl instruction that shifts by modulo 32 */
|
135 | 135 |
|
136 | 136 |
in_cidr->mask = 0;
|
137 | |
return NGX_OK;
|
138 | |
}
|
139 | |
|
140 | |
in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m))));
|
|
137 |
|
|
138 |
if (in_cidr->addr == 0) {
|
|
139 |
return NGX_OK;
|
|
140 |
}
|
|
141 |
|
|
142 |
return NGX_DONE;
|
|
143 |
}
|
|
144 |
|
|
145 |
in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift))));
|
141 | 146 |
|
142 | 147 |
if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) {
|
143 | 148 |
return NGX_OK;
|