annotate src/http/modules/ngx_http_geoip_module.c @ 5029:795722a74254

GeoIP: removed pseudo-support of "proxy" and "netspeed" databases.
author Ruslan Ermilov <ru@nginx.com>
date Mon, 04 Feb 2013 16:44:22 +0000
parents a74d211f034d
children f7fe817c92a2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 /*
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
4412
d620f497c50f Copyright updated.
Maxim Konovalov <maxim@nginx.com>
parents: 3919
diff changeset
4 * Copyright (C) Nginx, Inc.
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5 */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 #include <ngx_config.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 #include <ngx_core.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10 #include <ngx_http.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12 #include <GeoIP.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
13 #include <GeoIPCity.h>
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
14
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
15
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
16 #define NGX_GEOIP_COUNTRY_CODE 0
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
17 #define NGX_GEOIP_COUNTRY_CODE3 1
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
18 #define NGX_GEOIP_COUNTRY_NAME 2
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
19
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
20
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
21 typedef struct {
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
22 GeoIP *country;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
23 GeoIP *org;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
24 GeoIP *city;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
25 ngx_array_t *proxies; /* array of ngx_cidr_t */
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
26 ngx_flag_t proxy_recursive;
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
27 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
28 unsigned country_v6:1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
29 unsigned org_v6:1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
30 unsigned city_v6:1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
31 #endif
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
32 } ngx_http_geoip_conf_t;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
33
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
34
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
35 typedef struct {
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
36 ngx_str_t *name;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
37 uintptr_t data;
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
38 } ngx_http_geoip_var_t;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
39
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
40
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
41 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
42 u_long addr);
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
43
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
44
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
45 ngx_http_geoip_variable_handler_pt ngx_http_geoip_country_functions[] = {
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
46 GeoIP_country_code_by_ipnum,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
47 GeoIP_country_code3_by_ipnum,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
48 GeoIP_country_name_by_ipnum,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
49 };
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
50
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
51
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
52 #if (NGX_HAVE_GEOIP_V6)
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
53
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
54 typedef const char *(*ngx_http_geoip_variable_handler_v6_pt)(GeoIP *,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
55 geoipv6_t addr);
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
56
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
57
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
58 ngx_http_geoip_variable_handler_v6_pt ngx_http_geoip_country_v6_functions[] = {
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
59 GeoIP_country_code_by_ipnum_v6,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
60 GeoIP_country_code3_by_ipnum_v6,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
61 GeoIP_country_name_by_ipnum_v6,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
62 };
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
63
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
64 #endif
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
65
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
66
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
67 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
68 ngx_http_variable_value_t *v, uintptr_t data);
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
69 static ngx_int_t ngx_http_geoip_org_variable(ngx_http_request_t *r,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
70 ngx_http_variable_value_t *v, uintptr_t data);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
71 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
72 ngx_http_variable_value_t *v, uintptr_t data);
3737
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
73 static ngx_int_t ngx_http_geoip_region_name_variable(ngx_http_request_t *r,
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
74 ngx_http_variable_value_t *v, uintptr_t data);
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
75 static ngx_int_t ngx_http_geoip_city_float_variable(ngx_http_request_t *r,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
76 ngx_http_variable_value_t *v, uintptr_t data);
3736
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
77 static ngx_int_t ngx_http_geoip_city_int_variable(ngx_http_request_t *r,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
78 ngx_http_variable_value_t *v, uintptr_t data);
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
79 static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
80
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
81 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
82 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf);
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
83 static char *ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
84 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
85 void *conf);
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
86 static char *ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
87 void *conf);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
88 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
89 void *conf);
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
90 static char *ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
91 void *conf);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
92 static ngx_int_t ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
93 ngx_cidr_t *cidr);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
94 static void ngx_http_geoip_cleanup(void *data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
95
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
96
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
97 static ngx_command_t ngx_http_geoip_commands[] = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
98
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
99 { ngx_string("geoip_country"),
3859
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
100 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
101 ngx_http_geoip_country,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
102 NGX_HTTP_MAIN_CONF_OFFSET,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
103 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
104 NULL },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
105
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
106 { ngx_string("geoip_org"),
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
107 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
108 ngx_http_geoip_org,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
109 NGX_HTTP_MAIN_CONF_OFFSET,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
110 0,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
111 NULL },
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
112
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
113 { ngx_string("geoip_city"),
3859
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
114 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
115 ngx_http_geoip_city,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
116 NGX_HTTP_MAIN_CONF_OFFSET,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
117 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
118 NULL },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
119
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
120 { ngx_string("geoip_proxy"),
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
121 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
122 ngx_http_geoip_proxy,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
123 NGX_HTTP_MAIN_CONF_OFFSET,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
124 0,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
125 NULL },
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
126
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
127 { ngx_string("geoip_proxy_recursive"),
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
128 NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
129 ngx_conf_set_flag_slot,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
130 NGX_HTTP_MAIN_CONF_OFFSET,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
131 offsetof(ngx_http_geoip_conf_t, proxy_recursive),
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
132 NULL },
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
133
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
134 ngx_null_command
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
135 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
136
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
137
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
138 static ngx_http_module_t ngx_http_geoip_module_ctx = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
139 ngx_http_geoip_add_variables, /* preconfiguration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
140 NULL, /* postconfiguration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
141
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
142 ngx_http_geoip_create_conf, /* create main configuration */
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
143 ngx_http_geoip_init_conf, /* init main configuration */
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
144
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
145 NULL, /* create server configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
146 NULL, /* merge server configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
147
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
148 NULL, /* create location configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
149 NULL /* merge location configuration */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
150 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
151
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
152
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
153 ngx_module_t ngx_http_geoip_module = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
154 NGX_MODULE_V1,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
155 &ngx_http_geoip_module_ctx, /* module context */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
156 ngx_http_geoip_commands, /* module directives */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
157 NGX_HTTP_MODULE, /* module type */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
158 NULL, /* init master */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
159 NULL, /* init module */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
160 NULL, /* init process */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
161 NULL, /* init thread */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
162 NULL, /* exit thread */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
163 NULL, /* exit process */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
164 NULL, /* exit master */
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
165 NGX_MODULE_V1_PADDING
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
166 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
167
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
168
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
169 static ngx_http_variable_t ngx_http_geoip_vars[] = {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
170
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
171 { ngx_string("geoip_country_code"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
172 ngx_http_geoip_country_variable,
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
173 NGX_GEOIP_COUNTRY_CODE, 0, 0 },
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
174
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
175 { ngx_string("geoip_country_code3"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
176 ngx_http_geoip_country_variable,
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
177 NGX_GEOIP_COUNTRY_CODE3, 0, 0 },
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
178
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
179 { ngx_string("geoip_country_name"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
180 ngx_http_geoip_country_variable,
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
181 NGX_GEOIP_COUNTRY_NAME, 0, 0 },
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
182
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
183 { ngx_string("geoip_org"), NULL,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
184 ngx_http_geoip_org_variable,
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
185 0, 0, 0 },
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
186
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
187 { ngx_string("geoip_city_continent_code"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
188 ngx_http_geoip_city_variable,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
189 offsetof(GeoIPRecord, continent_code), 0, 0 },
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
190
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
191 { ngx_string("geoip_city_country_code"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
192 ngx_http_geoip_city_variable,
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
193 offsetof(GeoIPRecord, country_code), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
194
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
195 { ngx_string("geoip_city_country_code3"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
196 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
197 offsetof(GeoIPRecord, country_code3), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
198
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
199 { ngx_string("geoip_city_country_name"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
200 ngx_http_geoip_city_variable,
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
201 offsetof(GeoIPRecord, country_name), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
202
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
203 { ngx_string("geoip_region"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
204 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
205 offsetof(GeoIPRecord, region), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
206
3737
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
207 { ngx_string("geoip_region_name"), NULL,
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
208 ngx_http_geoip_region_name_variable,
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
209 0, 0, 0 },
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
210
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
211 { ngx_string("geoip_city"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
212 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
213 offsetof(GeoIPRecord, city), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
214
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
215 { ngx_string("geoip_postal_code"), NULL,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
216 ngx_http_geoip_city_variable,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
217 offsetof(GeoIPRecord, postal_code), 0, 0 },
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
218
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
219 { ngx_string("geoip_latitude"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
220 ngx_http_geoip_city_float_variable,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
221 offsetof(GeoIPRecord, latitude), 0, 0 },
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
222
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
223 { ngx_string("geoip_longitude"), NULL,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
224 ngx_http_geoip_city_float_variable,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
225 offsetof(GeoIPRecord, longitude), 0, 0 },
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
226
3736
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
227 { ngx_string("geoip_dma_code"), NULL,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
228 ngx_http_geoip_city_int_variable,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
229 offsetof(GeoIPRecord, dma_code), 0, 0 },
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
230
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
231 { ngx_string("geoip_area_code"), NULL,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
232 ngx_http_geoip_city_int_variable,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
233 offsetof(GeoIPRecord, area_code), 0, 0 },
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
234
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
235 { ngx_null_string, NULL, NULL, 0, 0, 0 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
236 };
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
237
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
238
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
239 static u_long
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
240 ngx_http_geoip_addr(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf)
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
241 {
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
242 ngx_addr_t addr;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
243 ngx_table_elt_t *xfwd;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
244 struct sockaddr_in *sin;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
245
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
246 addr.sockaddr = r->connection->sockaddr;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
247 addr.socklen = r->connection->socklen;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
248 /* addr.name = r->connection->addr_text; */
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
249
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
250 xfwd = r->headers_in.x_forwarded_for;
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
251
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
252 if (xfwd != NULL && gcf->proxies != NULL) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
253 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
254 xfwd->value.len, gcf->proxies,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
255 gcf->proxy_recursive);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
256 }
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
257
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
258 #if (NGX_HAVE_INET6)
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
259
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
260 if (addr.sockaddr->sa_family == AF_INET6) {
4828
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
261 u_char *p;
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
262 in_addr_t inaddr;
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
263 struct in6_addr *inaddr6;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
264
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
265 inaddr6 = &((struct sockaddr_in6 *) addr.sockaddr)->sin6_addr;
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
266
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
267 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
4828
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
268 p = inaddr6->s6_addr;
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
269
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
270 inaddr = p[12] << 24;
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
271 inaddr += p[13] << 16;
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
272 inaddr += p[14] << 8;
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
273 inaddr += p[15];
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
274
f57154322e0e Fixed strict aliasing bugs when dealing with IPv4-mapped IPv6 addresses
Ruslan Ermilov <ru@nginx.com>
parents: 4649
diff changeset
275 return inaddr;
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
276 }
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
277 }
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
278
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
279 #endif
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
280
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
281 if (addr.sockaddr->sa_family != AF_INET) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
282 return INADDR_NONE;
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
283 }
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
284
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
285 sin = (struct sockaddr_in *) addr.sockaddr;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
286 return ntohl(sin->sin_addr.s_addr);
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
287 }
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
288
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
289
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
290 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
291
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
292 static geoipv6_t
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
293 ngx_http_geoip_addr_v6(ngx_http_request_t *r, ngx_http_geoip_conf_t *gcf)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
294 {
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
295 ngx_addr_t addr;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
296 ngx_table_elt_t *xfwd;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
297 in_addr_t addr4;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
298 struct in6_addr addr6;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
299 struct sockaddr_in *sin;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
300 struct sockaddr_in6 *sin6;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
301
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
302 addr.sockaddr = r->connection->sockaddr;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
303 addr.socklen = r->connection->socklen;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
304 /* addr.name = r->connection->addr_text; */
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
305
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
306 xfwd = r->headers_in.x_forwarded_for;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
307
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
308 if (xfwd != NULL && gcf->proxies != NULL) {
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
309 (void) ngx_http_get_forwarded_addr(r, &addr, xfwd->value.data,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
310 xfwd->value.len, gcf->proxies,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
311 gcf->proxy_recursive);
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
312 }
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
313
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
314 switch (addr.sockaddr->sa_family) {
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
315
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
316 case AF_INET:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
317 /* Produce IPv4-mapped IPv6 address. */
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
318 sin = (struct sockaddr_in *) addr.sockaddr;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
319 addr4 = ntohl(sin->sin_addr.s_addr);
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
320
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
321 ngx_memzero(&addr6, sizeof(struct in6_addr));
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
322 addr6.s6_addr[10] = 0xff;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
323 addr6.s6_addr[11] = 0xff;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
324 addr6.s6_addr[12] = addr4 >> 24;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
325 addr6.s6_addr[13] = addr4 >> 16;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
326 addr6.s6_addr[14] = addr4 >> 8;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
327 addr6.s6_addr[15] = addr4;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
328 return addr6;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
329
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
330 case AF_INET6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
331 sin6 = (struct sockaddr_in6 *) addr.sockaddr;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
332 return sin6->sin6_addr;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
333
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
334 default:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
335 return in6addr_any;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
336 }
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
337 }
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
338
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
339 #endif
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
340
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
341
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
342 static ngx_int_t
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
343 ngx_http_geoip_country_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
344 ngx_http_variable_value_t *v, uintptr_t data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
345 {
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
346 ngx_http_geoip_variable_handler_pt handler =
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
347 ngx_http_geoip_country_functions[data];
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
348 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
349 ngx_http_geoip_variable_handler_v6_pt handler_v6 =
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
350 ngx_http_geoip_country_v6_functions[data];
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
351 #endif
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
352
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
353 const char *val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
354 ngx_http_geoip_conf_t *gcf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
355
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
356 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
357
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
358 if (gcf->country == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
359 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
360 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
361
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
362 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
363 val = gcf->country_v6
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
364 ? handler_v6(gcf->country, ngx_http_geoip_addr_v6(r, gcf))
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
365 : handler(gcf->country, ngx_http_geoip_addr(r, gcf));
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
366 #else
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
367 val = handler(gcf->country, ngx_http_geoip_addr(r, gcf));
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
368 #endif
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
369
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
370 if (val == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
371 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
372 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
373
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
374 v->len = ngx_strlen(val);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
375 v->valid = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
376 v->no_cacheable = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
377 v->not_found = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
378 v->data = (u_char *) val;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
379
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
380 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
381
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
382 not_found:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
383
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
384 v->not_found = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
385
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
386 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
387 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
388
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
389
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
390 static ngx_int_t
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
391 ngx_http_geoip_org_variable(ngx_http_request_t *r,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
392 ngx_http_variable_value_t *v, uintptr_t data)
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
393 {
4647
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
394 size_t len;
4649
95d93f7e6fa2 geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents: 4648
diff changeset
395 char *val;
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
396 ngx_http_geoip_conf_t *gcf;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
397
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
398 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
399
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
400 if (gcf->org == NULL) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
401 goto not_found;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
402 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
403
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
404 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
405 val = gcf->org_v6
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
406 ? GeoIP_name_by_ipnum_v6(gcf->org,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
407 ngx_http_geoip_addr_v6(r, gcf))
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
408 : GeoIP_name_by_ipnum(gcf->org,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
409 ngx_http_geoip_addr(r, gcf));
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
410 #else
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
411 val = GeoIP_name_by_ipnum(gcf->org, ngx_http_geoip_addr(r, gcf));
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
412 #endif
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
413
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
414 if (val == NULL) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
415 goto not_found;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
416 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
417
4647
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
418 len = ngx_strlen(val);
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
419 v->data = ngx_pnalloc(r->pool, len);
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
420 if (v->data == NULL) {
4649
95d93f7e6fa2 geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents: 4648
diff changeset
421 ngx_free(val);
4647
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
422 return NGX_ERROR;
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
423 }
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
424
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
425 ngx_memcpy(v->data, val, len);
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
426
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
427 v->len = len;
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
428 v->valid = 1;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
429 v->no_cacheable = 0;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
430 v->not_found = 0;
4647
a321eadcb16c Fixed memory leak if $geoip_org variable was used.
Ruslan Ermilov <ru@nginx.com>
parents: 4627
diff changeset
431
4649
95d93f7e6fa2 geoip: got rid of ugly casts when calling ngx_free().
Ruslan Ermilov <ru@nginx.com>
parents: 4648
diff changeset
432 ngx_free(val);
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
433
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
434 return NGX_OK;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
435
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
436 not_found:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
437
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
438 v->not_found = 1;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
439
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
440 return NGX_OK;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
441 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
442
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
443
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
444 static ngx_int_t
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
445 ngx_http_geoip_city_variable(ngx_http_request_t *r,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
446 ngx_http_variable_value_t *v, uintptr_t data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
447 {
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
448 char *val;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
449 size_t len;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
450 GeoIPRecord *gr;
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
451
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
452 gr = ngx_http_geoip_get_city_record(r);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
453 if (gr == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
454 goto not_found;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
455 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
456
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
457 val = *(char **) ((char *) gr + data);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
458 if (val == NULL) {
3031
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
459 goto no_value;
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
460 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
461
3031
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
462 len = ngx_strlen(val);
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
463 v->data = ngx_pnalloc(r->pool, len);
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
464 if (v->data == NULL) {
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
465 GeoIPRecord_delete(gr);
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
466 return NGX_ERROR;
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
467 }
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
468
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
469 ngx_memcpy(v->data, val, len);
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
470
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
471 v->len = len;
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
472 v->valid = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
473 v->no_cacheable = 0;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
474 v->not_found = 0;
3031
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
475
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
476 GeoIPRecord_delete(gr);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
477
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
478 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
479
3031
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
480 no_value:
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
481
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
482 GeoIPRecord_delete(gr);
98a8336c5b7c fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents: 2985
diff changeset
483
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
484 not_found:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
485
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
486 v->not_found = 1;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
487
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
488 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
489 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
490
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
491
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
492 static ngx_int_t
3737
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
493 ngx_http_geoip_region_name_variable(ngx_http_request_t *r,
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
494 ngx_http_variable_value_t *v, uintptr_t data)
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
495 {
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
496 size_t len;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
497 const char *val;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
498 GeoIPRecord *gr;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
499
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
500 gr = ngx_http_geoip_get_city_record(r);
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
501 if (gr == NULL) {
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
502 goto not_found;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
503 }
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
504
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
505 val = GeoIP_region_name_by_code(gr->country_code, gr->region);
3742
01691af60f94 we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents: 3741
diff changeset
506
01691af60f94 we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents: 3741
diff changeset
507 GeoIPRecord_delete(gr);
01691af60f94 we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents: 3741
diff changeset
508
3741
0f9b2d285bfc fix segfault, the bug has been introduced in r3738
Igor Sysoev <igor@sysoev.ru>
parents: 3737
diff changeset
509 if (val == NULL) {
3742
01691af60f94 we can free GeoIPRecord just after GeoIP_region_name_by_code(),
Igor Sysoev <igor@sysoev.ru>
parents: 3741
diff changeset
510 goto not_found;
3741
0f9b2d285bfc fix segfault, the bug has been introduced in r3738
Igor Sysoev <igor@sysoev.ru>
parents: 3737
diff changeset
511 }
3737
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
512
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
513 len = ngx_strlen(val);
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
514 v->data = ngx_pnalloc(r->pool, len);
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
515 if (v->data == NULL) {
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
516 return NGX_ERROR;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
517 }
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
518
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
519 ngx_memcpy(v->data, val, len);
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
520
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
521 v->len = len;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
522 v->valid = 1;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
523 v->no_cacheable = 0;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
524 v->not_found = 0;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
525
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
526 return NGX_OK;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
527
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
528 not_found:
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
529
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
530 v->not_found = 1;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
531
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
532 return NGX_OK;
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
533 }
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
534
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
535
18ae5f09a53d $geoip_region_name
Igor Sysoev <igor@sysoev.ru>
parents: 3736
diff changeset
536 static ngx_int_t
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
537 ngx_http_geoip_city_float_variable(ngx_http_request_t *r,
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
538 ngx_http_variable_value_t *v, uintptr_t data)
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
539 {
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
540 float val;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
541 GeoIPRecord *gr;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
542
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
543 gr = ngx_http_geoip_get_city_record(r);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
544 if (gr == NULL) {
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
545 v->not_found = 1;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
546 return NGX_OK;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
547 }
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
548
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
549 v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN + 5);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
550 if (v->data == NULL) {
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
551 GeoIPRecord_delete(gr);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
552 return NGX_ERROR;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
553 }
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
554
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
555 val = *(float *) ((char *) gr + data);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
556
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
557 v->len = ngx_sprintf(v->data, "%.4f", val) - v->data;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
558
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
559 GeoIPRecord_delete(gr);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
560
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
561 return NGX_OK;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
562 }
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
563
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
564
3736
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
565 static ngx_int_t
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
566 ngx_http_geoip_city_int_variable(ngx_http_request_t *r,
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
567 ngx_http_variable_value_t *v, uintptr_t data)
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
568 {
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
569 int val;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
570 GeoIPRecord *gr;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
571
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
572 gr = ngx_http_geoip_get_city_record(r);
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
573 if (gr == NULL) {
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
574 v->not_found = 1;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
575 return NGX_OK;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
576 }
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
577
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
578 v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN);
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
579 if (v->data == NULL) {
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
580 GeoIPRecord_delete(gr);
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
581 return NGX_ERROR;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
582 }
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
583
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
584 val = *(int *) ((char *) gr + data);
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
585
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
586 v->len = ngx_sprintf(v->data, "%d", val) - v->data;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
587
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
588 GeoIPRecord_delete(gr);
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
589
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
590 return NGX_OK;
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
591 }
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
592
257785918797 $geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents: 3398
diff changeset
593
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
594 static GeoIPRecord *
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
595 ngx_http_geoip_get_city_record(ngx_http_request_t *r)
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
596 {
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
597 ngx_http_geoip_conf_t *gcf;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
598
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
599 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
600
3919
0dceaa117e0d support IPv4 mapped to IPv6 in geoip module
Igor Sysoev <igor@sysoev.ru>
parents: 3915
diff changeset
601 if (gcf->city) {
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
602 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
603 return gcf->city_v6
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
604 ? GeoIP_record_by_ipnum_v6(gcf->city,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
605 ngx_http_geoip_addr_v6(r, gcf))
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
606 : GeoIP_record_by_ipnum(gcf->city,
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
607 ngx_http_geoip_addr(r, gcf));
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
608 #else
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
609 return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf));
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
610 #endif
3398
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
611 }
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
612
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
613 return NULL;
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
614 }
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
615
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
616
236634d2b603 $geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents: 3031
diff changeset
617 static ngx_int_t
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
618 ngx_http_geoip_add_variables(ngx_conf_t *cf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
619 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
620 ngx_http_variable_t *var, *v;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
621
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
622 for (v = ngx_http_geoip_vars; v->name.len; v++) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
623 var = ngx_http_add_variable(cf, &v->name, v->flags);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
624 if (var == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
625 return NGX_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
626 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
627
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
628 var->get_handler = v->get_handler;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
629 var->data = v->data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
630 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
631
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
632 return NGX_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
633 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
634
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
635
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
636 static void *
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
637 ngx_http_geoip_create_conf(ngx_conf_t *cf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
638 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
639 ngx_pool_cleanup_t *cln;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
640 ngx_http_geoip_conf_t *conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
641
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
642 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t));
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
643 if (conf == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
644 return NULL;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
645 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
646
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
647 conf->proxy_recursive = NGX_CONF_UNSET;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
648
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
649 cln = ngx_pool_cleanup_add(cf->pool, 0);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
650 if (cln == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
651 return NULL;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
652 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
653
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
654 cln->handler = ngx_http_geoip_cleanup;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
655 cln->data = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
656
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
657 return conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
658 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
659
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
660
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
661 static char *
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
662 ngx_http_geoip_init_conf(ngx_conf_t *cf, void *conf)
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
663 {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
664 ngx_http_geoip_conf_t *gcf = conf;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
665
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
666 ngx_conf_init_value(gcf->proxy_recursive, 0);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
667
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
668 return NGX_CONF_OK;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
669 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
670
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
671
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
672 static char *
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
673 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
674 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
675 ngx_http_geoip_conf_t *gcf = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
676
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
677 ngx_str_t *value;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
678
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
679 if (gcf->country) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
680 return "is duplicate";
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
681 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
682
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
683 value = cf->args->elts;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
684
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
685 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
686
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
687 if (gcf->country == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
688 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3914
2b92090d8a43 fix typo
Igor Sysoev <igor@sysoev.ru>
parents: 3859
diff changeset
689 "GeoIP_open(\"%V\") failed", &value[1]);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
690
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
691 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
692 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
693
3859
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
694 if (cf->args->nelts == 3) {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
695 if (ngx_strcmp(value[2].data, "utf8") == 0) {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
696 GeoIP_set_charset (gcf->country, GEOIP_CHARSET_UTF8);
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
697
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
698 } else {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
699 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
700 "invalid parameter \"%V\"", &value[2]);
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
701 return NGX_CONF_ERROR;
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
702 }
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
703 }
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
704
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
705 switch (gcf->country->databaseType) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
706
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
707 case GEOIP_COUNTRY_EDITION:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
708
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
709 return NGX_CONF_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
710
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
711 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
712 case GEOIP_COUNTRY_EDITION_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
713
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
714 gcf->country_v6 = 1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
715 return NGX_CONF_OK;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
716 #endif
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
717
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
718 default:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
719 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
720 "invalid GeoIP database \"%V\" type:%d",
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
721 &value[1], gcf->country->databaseType);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
722 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
723 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
724 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
725
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
726
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
727 static char *
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
728 ngx_http_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
729 {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
730 ngx_http_geoip_conf_t *gcf = conf;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
731
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
732 ngx_str_t *value;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
733
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
734 if (gcf->org) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
735 return "is duplicate";
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
736 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
737
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
738 value = cf->args->elts;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
739
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
740 gcf->org = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
741
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
742 if (gcf->org == NULL) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
743 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
744 "GeoIP_open(\"%V\") failed", &value[1]);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
745
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
746 return NGX_CONF_ERROR;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
747 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
748
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
749 if (cf->args->nelts == 3) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
750 if (ngx_strcmp(value[2].data, "utf8") == 0) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
751 GeoIP_set_charset (gcf->org, GEOIP_CHARSET_UTF8);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
752
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
753 } else {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
754 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
755 "invalid parameter \"%V\"", &value[2]);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
756 return NGX_CONF_ERROR;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
757 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
758 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
759
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
760 switch (gcf->org->databaseType) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
761
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
762 case GEOIP_ISP_EDITION:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
763 case GEOIP_ORG_EDITION:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
764 case GEOIP_DOMAIN_EDITION:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
765 case GEOIP_ASNUM_EDITION:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
766
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
767 return NGX_CONF_OK;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
768
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
769 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
770 case GEOIP_ISP_EDITION_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
771 case GEOIP_ORG_EDITION_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
772 case GEOIP_DOMAIN_EDITION_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
773 case GEOIP_ASNUM_EDITION_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
774
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
775 gcf->org_v6 = 1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
776 return NGX_CONF_OK;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
777 #endif
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
778
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
779 default:
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
780 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
781 "invalid GeoIP database \"%V\" type:%d",
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
782 &value[1], gcf->org->databaseType);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
783 return NGX_CONF_ERROR;
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
784 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
785 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
786
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
787
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
788 static char *
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
789 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
790 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
791 ngx_http_geoip_conf_t *gcf = conf;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
792
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
793 ngx_str_t *value;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
794
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
795 if (gcf->city) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
796 return "is duplicate";
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
797 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
798
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
799 value = cf->args->elts;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
800
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
801 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
802
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
803 if (gcf->city == NULL) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
804 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3914
2b92090d8a43 fix typo
Igor Sysoev <igor@sysoev.ru>
parents: 3859
diff changeset
805 "GeoIP_open(\"%V\") failed", &value[1]);
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
806
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
807 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
808 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
809
3859
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
810 if (cf->args->nelts == 3) {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
811 if (ngx_strcmp(value[2].data, "utf8") == 0) {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
812 GeoIP_set_charset (gcf->city, GEOIP_CHARSET_UTF8);
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
813
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
814 } else {
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
815 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
816 "invalid parameter \"%V\"", &value[2]);
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
817 return NGX_CONF_ERROR;
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
818 }
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
819 }
30d4d6187316 utf8 parameter of geoip_country and geoip_city
Igor Sysoev <igor@sysoev.ru>
parents: 3742
diff changeset
820
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
821 switch (gcf->city->databaseType) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
822
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
823 case GEOIP_CITY_EDITION_REV0:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
824 case GEOIP_CITY_EDITION_REV1:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
825
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
826 return NGX_CONF_OK;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
827
5015
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
828 #if (NGX_HAVE_GEOIP_V6)
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
829 case GEOIP_CITY_EDITION_REV0_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
830 case GEOIP_CITY_EDITION_REV1_V6:
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
831
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
832 gcf->city_v6 = 1;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
833 return NGX_CONF_OK;
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
834 #endif
a74d211f034d GeoIP: IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4828
diff changeset
835
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
836 default:
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
837 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
838 "invalid GeoIP City database \"%V\" type:%d",
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
839 &value[1], gcf->city->databaseType);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
840 return NGX_CONF_ERROR;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
841 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
842 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
843
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
844
4627
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
845 static char *
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
846 ngx_http_geoip_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
847 {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
848 ngx_http_geoip_conf_t *gcf = conf;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
849
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
850 ngx_str_t *value;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
851 ngx_cidr_t cidr, *c;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
852
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
853 value = cf->args->elts;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
854
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
855 if (ngx_http_geoip_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
856 return NGX_CONF_ERROR;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
857 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
858
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
859 if (gcf->proxies == NULL) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
860 gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t));
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
861 if (gcf->proxies == NULL) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
862 return NGX_CONF_ERROR;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
863 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
864 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
865
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
866 c = ngx_array_push(gcf->proxies);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
867 if (c == NULL) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
868 return NGX_CONF_ERROR;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
869 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
870
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
871 *c = cidr;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
872
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
873 return NGX_CONF_OK;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
874 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
875
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
876 static ngx_int_t
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
877 ngx_http_geoip_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
878 {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
879 ngx_int_t rc;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
880
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
881 if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
882 cidr->family = AF_INET;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
883 cidr->u.in.addr = 0xffffffff;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
884 cidr->u.in.mask = 0xffffffff;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
885
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
886 return NGX_OK;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
887 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
888
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
889 rc = ngx_ptocidr(net, cidr);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
890
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
891 if (rc == NGX_ERROR) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
892 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid network \"%V\"", net);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
893 return NGX_ERROR;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
894 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
895
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
896 if (rc == NGX_DONE) {
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
897 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
898 "low address bits of %V are meaningless", net);
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
899 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
900
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
901 return NGX_OK;
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
902 }
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
903
3152e4c371d7 geoip: trusted proxies support and partial IPv6 support.
Ruslan Ermilov <ru@nginx.com>
parents: 4412
diff changeset
904
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
905 static void
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
906 ngx_http_geoip_cleanup(void *data)
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
907 {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
908 ngx_http_geoip_conf_t *gcf = data;
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
909
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
910 if (gcf->country) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
911 GeoIP_delete(gcf->country);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
912 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
913
3915
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
914 if (gcf->org) {
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
915 GeoIP_delete(gcf->org);
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
916 }
e3645e338937 geoip_org
Igor Sysoev <igor@sysoev.ru>
parents: 3914
diff changeset
917
2985
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
918 if (gcf->city) {
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
919 GeoIP_delete(gcf->city);
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
920 }
31af2d1a742e ngx_http_geoip_module
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
921 }