Mercurial > hg > nginx
comparison src/http/modules/ngx_http_realip_module.c @ 3273:fe71be4a02f1
support IPv6 addresses in Real IP headers
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 02 Nov 2009 16:24:27 +0000 |
parents | fb4f7605505f |
children | 95b0b0d7843f |
comparison
equal
deleted
inserted
replaced
3272:98f49b2bcae8 | 3273:fe71be4a02f1 |
---|---|
27 ngx_str_t header; | 27 ngx_str_t header; |
28 } ngx_http_realip_loc_conf_t; | 28 } ngx_http_realip_loc_conf_t; |
29 | 29 |
30 | 30 |
31 typedef struct { | 31 typedef struct { |
32 ngx_connection_t *connection; | 32 ngx_connection_t *connection; |
33 in_addr_t addr; | 33 struct sockaddr *sockaddr; |
34 ngx_str_t addr_text; | 34 socklen_t socklen; |
35 ngx_str_t addr_text; | |
35 } ngx_http_realip_ctx_t; | 36 } ngx_http_realip_ctx_t; |
36 | 37 |
37 | 38 |
38 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); | 39 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); |
39 static void ngx_http_realip_cleanup(void *data); | 40 static void ngx_http_realip_cleanup(void *data); |
101 static ngx_int_t | 102 static ngx_int_t |
102 ngx_http_realip_handler(ngx_http_request_t *r) | 103 ngx_http_realip_handler(ngx_http_request_t *r) |
103 { | 104 { |
104 u_char *ip, *p; | 105 u_char *ip, *p; |
105 size_t len; | 106 size_t len; |
106 in_addr_t addr; | 107 ngx_int_t rc; |
107 ngx_uint_t i, hash; | 108 ngx_uint_t i, hash; |
109 ngx_addr_t addr; | |
108 ngx_list_part_t *part; | 110 ngx_list_part_t *part; |
109 ngx_table_elt_t *header; | 111 ngx_table_elt_t *header; |
110 struct sockaddr_in *sin; | 112 struct sockaddr_in *sin; |
111 ngx_connection_t *c; | 113 ngx_connection_t *c; |
112 ngx_pool_cleanup_t *cln; | 114 ngx_pool_cleanup_t *cln; |
224 | 226 |
225 ctx = cln->data; | 227 ctx = cln->data; |
226 | 228 |
227 ngx_http_set_ctx(r, ctx, ngx_http_realip_module); | 229 ngx_http_set_ctx(r, ctx, ngx_http_realip_module); |
228 | 230 |
229 addr = ngx_inet_addr(ip, len); | 231 rc = ngx_parse_addr(c->pool, &addr, ip, len); |
230 | 232 |
231 if (addr == INADDR_NONE) { | 233 switch (rc) { |
234 case NGX_DECLINED: | |
232 return NGX_DECLINED; | 235 return NGX_DECLINED; |
236 case NGX_ERROR: | |
237 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
238 default: /* NGX_OK */ | |
239 break; | |
233 } | 240 } |
234 | 241 |
235 p = ngx_pnalloc(c->pool, len); | 242 p = ngx_pnalloc(c->pool, len); |
236 if (p == NULL) { | 243 if (p == NULL) { |
237 return NGX_HTTP_INTERNAL_SERVER_ERROR; | 244 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
240 ngx_memcpy(p, ip, len); | 247 ngx_memcpy(p, ip, len); |
241 | 248 |
242 cln->handler = ngx_http_realip_cleanup; | 249 cln->handler = ngx_http_realip_cleanup; |
243 | 250 |
244 ctx->connection = c; | 251 ctx->connection = c; |
245 ctx->addr = sin->sin_addr.s_addr; | 252 ctx->sockaddr = c->sockaddr; |
253 ctx->socklen = c->socklen; | |
246 ctx->addr_text = c->addr_text; | 254 ctx->addr_text = c->addr_text; |
247 | 255 |
248 sin->sin_addr.s_addr = addr; | 256 c->sockaddr = addr.sockaddr; |
249 | 257 c->socklen = addr.socklen; |
250 c->addr_text.len = len; | 258 c->addr_text.len = len; |
251 c->addr_text.data = p; | 259 c->addr_text.data = p; |
252 | 260 |
253 return NGX_DECLINED; | 261 return NGX_DECLINED; |
254 } | 262 } |
261 static void | 269 static void |
262 ngx_http_realip_cleanup(void *data) | 270 ngx_http_realip_cleanup(void *data) |
263 { | 271 { |
264 ngx_http_realip_ctx_t *ctx = data; | 272 ngx_http_realip_ctx_t *ctx = data; |
265 | 273 |
266 ngx_connection_t *c; | 274 ngx_connection_t *c; |
267 struct sockaddr_in *sin; | |
268 | 275 |
269 c = ctx->connection; | 276 c = ctx->connection; |
270 | 277 |
271 sin = (struct sockaddr_in *) c->sockaddr; | 278 c->sockaddr = ctx->sockaddr; |
272 sin->sin_addr.s_addr = ctx->addr; | 279 c->socklen = ctx->socklen; |
273 | |
274 c->addr_text = ctx->addr_text; | 280 c->addr_text = ctx->addr_text; |
275 } | 281 } |
276 | 282 |
277 | 283 |
278 static char * | 284 static char * |