Klaus Demo nginx / 9c388c0
*) refactor ngx_ptocidr() *) allow address without bitmask *) thus now ngx_http_geo_module accepts addresses without bitmask Igor Sysoev 12 years ago
4 changed file(s) with 31 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
101101 ngx_int_t
102102 ngx_ptocidr(ngx_str_t *text, void *cidr)
103103 {
104 ngx_int_t m;
105 ngx_uint_t i;
104 u_char *addr, *mask, *last;
105 ngx_int_t shift;
106106 ngx_inet_cidr_t *in_cidr;
107107
108108 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
123116 if (in_cidr->addr == INADDR_NONE) {
124117 return NGX_ERROR;
125118 }
126119
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) {
133133
134134 /* the x86 compilers use the shl instruction that shifts by modulo 32 */
135135
136136 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))));
141146
142147 if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) {
143148 return NGX_OK;
10501050 return NGX_CONF_ERROR;
10511051 }
10521052
1053 dc->addr = inet_addr((char *) value[1].data);
1054
1055 if (dc->addr != INADDR_NONE) {
1056 dc->mask = 0xffffffff;
1057 return NGX_CONF_OK;
1058 }
1059
10601053 rc = ngx_ptocidr(&value[1], &in_cidr);
10611054
10621055 if (rc == NGX_DONE) {
165165 return NGX_CONF_OK;
166166 }
167167
168 rule->addr = inet_addr((char *) value[1].data);
169
170 if (rule->addr != INADDR_NONE) {
171 rule->mask = 0xffffffff;
172
173 return NGX_CONF_OK;
174 }
175
176168 rc = ngx_ptocidr(&value[1], &in_cidr);
177169
178170 if (rc == NGX_ERROR) {
250250
251251 value = cf->args->elts;
252252
253 from->addr = inet_addr((char *) value[1].data);
254
255 if (from->addr != INADDR_NONE) {
256 from->mask = 0xffffffff;
257
258 return NGX_CONF_OK;
259 }
260
261253 rc = ngx_ptocidr(&value[1], &in_cidr);
262254
263255 if (rc == NGX_ERROR) {