Mercurial > hg > nginx
comparison 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 |
comparison
equal
deleted
inserted
replaced
2201:49a269b67213 | 2202:2300ab9b069e |
---|---|
100 /* AF_INET only */ | 100 /* AF_INET only */ |
101 | 101 |
102 ngx_int_t | 102 ngx_int_t |
103 ngx_ptocidr(ngx_str_t *text, void *cidr) | 103 ngx_ptocidr(ngx_str_t *text, void *cidr) |
104 { | 104 { |
105 ngx_int_t m; | 105 u_char *addr, *mask, *last; |
106 ngx_uint_t i; | 106 ngx_int_t shift; |
107 ngx_inet_cidr_t *in_cidr; | 107 ngx_inet_cidr_t *in_cidr; |
108 | 108 |
109 in_cidr = cidr; | 109 in_cidr = cidr; |
110 | 110 addr = text->data; |
111 for (i = 0; i < text->len; i++) { | 111 last = addr + text->len; |
112 if (text->data[i] == '/') { | 112 |
113 break; | 113 mask = ngx_strlchr(addr, last, '/'); |
114 } | 114 |
115 } | 115 in_cidr->addr = ngx_inet_addr(addr, (mask ? mask : last) - addr); |
116 | 116 |
117 if (i == text->len) { | |
118 return NGX_ERROR; | |
119 } | |
120 | |
121 text->data[i] = '\0'; | |
122 in_cidr->addr = inet_addr((char *) text->data); | |
123 text->data[i] = '/'; | |
124 if (in_cidr->addr == INADDR_NONE) { | 117 if (in_cidr->addr == INADDR_NONE) { |
125 return NGX_ERROR; | 118 return NGX_ERROR; |
126 } | 119 } |
127 | 120 |
128 m = ngx_atoi(&text->data[i + 1], text->len - (i + 1)); | 121 if (mask == NULL) { |
129 if (m == NGX_ERROR) { | 122 in_cidr->mask = 0xffffffff; |
130 return NGX_ERROR; | 123 return NGX_OK; |
131 } | 124 } |
132 | 125 |
133 if (m == 0) { | 126 mask++; |
127 | |
128 shift = ngx_atoi(mask, last - mask); | |
129 if (shift == NGX_ERROR) { | |
130 return NGX_ERROR; | |
131 } | |
132 | |
133 if (shift == 0) { | |
134 | 134 |
135 /* the x86 compilers use the shl instruction that shifts by modulo 32 */ | 135 /* the x86 compilers use the shl instruction that shifts by modulo 32 */ |
136 | 136 |
137 in_cidr->mask = 0; | 137 in_cidr->mask = 0; |
138 return NGX_OK; | 138 |
139 } | 139 if (in_cidr->addr == 0) { |
140 | 140 return NGX_OK; |
141 in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - m)))); | 141 } |
142 | |
143 return NGX_DONE; | |
144 } | |
145 | |
146 in_cidr->mask = htonl((ngx_uint_t) (0 - (1 << (32 - shift)))); | |
142 | 147 |
143 if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) { | 148 if (in_cidr->addr == (in_cidr->addr & in_cidr->mask)) { |
144 return NGX_OK; | 149 return NGX_OK; |
145 } | 150 } |
146 | 151 |