Mercurial > hg > nginx
annotate src/http/modules/ngx_http_geoip_module.c @ 3736:257785918797
$geoip_dma_code and $geoip_area_code
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 03 Aug 2010 14:19:49 +0000 |
parents | 236634d2b603 |
children | 18ae5f09a53d |
rev | line source |
---|---|
2985 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 #include <GeoIP.h> | |
12 #include <GeoIPCity.h> | |
13 | |
14 | |
15 typedef struct { | |
16 GeoIP *country; | |
17 GeoIP *city; | |
18 } ngx_http_geoip_conf_t; | |
19 | |
20 | |
21 typedef struct { | |
22 ngx_str_t *name; | |
23 uintptr_t data; | |
24 } ngx_http_geoip_var_t; | |
25 | |
26 | |
27 typedef const char *(*ngx_http_geoip_variable_handler_pt)(GeoIP *, u_long addr); | |
28 | |
29 static ngx_int_t ngx_http_geoip_country_variable(ngx_http_request_t *r, | |
30 ngx_http_variable_value_t *v, uintptr_t data); | |
31 static ngx_int_t ngx_http_geoip_city_variable(ngx_http_request_t *r, | |
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 static GeoIPRecord *ngx_http_geoip_get_city_record(ngx_http_request_t *r); |
2985 | 38 |
39 static ngx_int_t ngx_http_geoip_add_variables(ngx_conf_t *cf); | |
40 static void *ngx_http_geoip_create_conf(ngx_conf_t *cf); | |
41 static char *ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, | |
42 void *conf); | |
43 static char *ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, | |
44 void *conf); | |
45 static void ngx_http_geoip_cleanup(void *data); | |
46 | |
47 | |
48 static ngx_command_t ngx_http_geoip_commands[] = { | |
49 | |
50 { ngx_string("geoip_country"), | |
51 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
52 ngx_http_geoip_country, | |
53 NGX_HTTP_MAIN_CONF_OFFSET, | |
54 0, | |
55 NULL }, | |
56 | |
57 { ngx_string("geoip_city"), | |
58 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, | |
59 ngx_http_geoip_city, | |
60 NGX_HTTP_MAIN_CONF_OFFSET, | |
61 0, | |
62 NULL }, | |
63 | |
64 ngx_null_command | |
65 }; | |
66 | |
67 | |
68 static ngx_http_module_t ngx_http_geoip_module_ctx = { | |
69 ngx_http_geoip_add_variables, /* preconfiguration */ | |
70 NULL, /* postconfiguration */ | |
71 | |
72 ngx_http_geoip_create_conf, /* create main configuration */ | |
73 NULL, /* init main configuration */ | |
74 | |
75 NULL, /* create server configuration */ | |
76 NULL, /* merge server configuration */ | |
77 | |
78 NULL, /* create location configuration */ | |
79 NULL /* merge location configuration */ | |
80 }; | |
81 | |
82 | |
83 ngx_module_t ngx_http_geoip_module = { | |
84 NGX_MODULE_V1, | |
85 &ngx_http_geoip_module_ctx, /* module context */ | |
86 ngx_http_geoip_commands, /* module directives */ | |
87 NGX_HTTP_MODULE, /* module type */ | |
88 NULL, /* init master */ | |
89 NULL, /* init module */ | |
90 NULL, /* init process */ | |
91 NULL, /* init thread */ | |
92 NULL, /* exit thread */ | |
93 NULL, /* exit process */ | |
94 NULL, /* exit master */ | |
95 NGX_MODULE_V1_PADDING | |
96 }; | |
97 | |
98 | |
99 static ngx_http_variable_t ngx_http_geoip_vars[] = { | |
100 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
101 { ngx_string("geoip_country_code"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
102 ngx_http_geoip_country_variable, |
2985 | 103 (uintptr_t) GeoIP_country_code_by_ipnum, 0, 0 }, |
104 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
105 { ngx_string("geoip_country_code3"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
106 ngx_http_geoip_country_variable, |
2985 | 107 (uintptr_t) GeoIP_country_code3_by_ipnum, 0, 0 }, |
108 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
109 { ngx_string("geoip_country_name"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
110 ngx_http_geoip_country_variable, |
2985 | 111 (uintptr_t) GeoIP_country_name_by_ipnum, 0, 0 }, |
112 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
113 { 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
|
114 ngx_http_geoip_city_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
115 offsetof(GeoIPRecord, continent_code), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
116 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
117 { 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
|
118 ngx_http_geoip_city_variable, |
2985 | 119 offsetof(GeoIPRecord, country_code), 0, 0 }, |
120 | |
121 { ngx_string("geoip_city_country_code3"), NULL, | |
122 ngx_http_geoip_city_variable, | |
123 offsetof(GeoIPRecord, country_code3), 0, 0 }, | |
124 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
125 { 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
|
126 ngx_http_geoip_city_variable, |
2985 | 127 offsetof(GeoIPRecord, country_name), 0, 0 }, |
128 | |
129 { ngx_string("geoip_region"), NULL, | |
130 ngx_http_geoip_city_variable, | |
131 offsetof(GeoIPRecord, region), 0, 0 }, | |
132 | |
133 { ngx_string("geoip_city"), NULL, | |
134 ngx_http_geoip_city_variable, | |
135 offsetof(GeoIPRecord, city), 0, 0 }, | |
136 | |
137 { ngx_string("geoip_postal_code"), NULL, | |
138 ngx_http_geoip_city_variable, | |
139 offsetof(GeoIPRecord, postal_code), 0, 0 }, | |
140 | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
141 { ngx_string("geoip_latitude"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
142 ngx_http_geoip_city_float_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
143 offsetof(GeoIPRecord, latitude), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
144 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
145 { ngx_string("geoip_longitude"), NULL, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
146 ngx_http_geoip_city_float_variable, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
147 offsetof(GeoIPRecord, longitude), 0, 0 }, |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
148 |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
149 { ngx_string("geoip_dma_code"), NULL, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
150 ngx_http_geoip_city_int_variable, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
151 offsetof(GeoIPRecord, dma_code), 0, 0 }, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
152 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
153 { ngx_string("geoip_area_code"), NULL, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
154 ngx_http_geoip_city_int_variable, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
155 offsetof(GeoIPRecord, area_code), 0, 0 }, |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
156 |
2985 | 157 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
158 }; | |
159 | |
160 | |
161 static ngx_int_t | |
162 ngx_http_geoip_country_variable(ngx_http_request_t *r, | |
163 ngx_http_variable_value_t *v, uintptr_t data) | |
164 { | |
165 ngx_http_geoip_variable_handler_pt handler = | |
166 (ngx_http_geoip_variable_handler_pt) data; | |
167 | |
168 u_long addr; | |
169 const char *val; | |
170 struct sockaddr_in *sin; | |
171 ngx_http_geoip_conf_t *gcf; | |
172 | |
173 gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module); | |
174 | |
175 if (gcf->country == NULL) { | |
176 goto not_found; | |
177 } | |
178 | |
179 if (r->connection->sockaddr->sa_family != AF_INET) { | |
180 goto not_found; | |
181 } | |
182 | |
183 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
184 addr = ntohl(sin->sin_addr.s_addr); | |
185 | |
186 val = handler(gcf->country, addr); | |
187 | |
188 if (val == NULL) { | |
189 goto not_found; | |
190 } | |
191 | |
192 v->len = ngx_strlen(val); | |
193 v->valid = 1; | |
194 v->no_cacheable = 0; | |
195 v->not_found = 0; | |
196 v->data = (u_char *) val; | |
197 | |
198 return NGX_OK; | |
199 | |
200 not_found: | |
201 | |
202 v->not_found = 1; | |
203 | |
204 return NGX_OK; | |
205 } | |
206 | |
207 | |
208 static ngx_int_t | |
209 ngx_http_geoip_city_variable(ngx_http_request_t *r, | |
210 ngx_http_variable_value_t *v, uintptr_t data) | |
211 { | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
212 char *val; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
213 size_t len; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
214 GeoIPRecord *gr; |
2985 | 215 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
216 gr = ngx_http_geoip_get_city_record(r); |
2985 | 217 if (gr == NULL) { |
218 goto not_found; | |
219 } | |
220 | |
221 val = *(char **) ((char *) gr + data); | |
222 if (val == NULL) { | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
223 goto no_value; |
2985 | 224 } |
225 | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
226 len = ngx_strlen(val); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
227 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
|
228 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
229 if (v->data == NULL) { |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
230 GeoIPRecord_delete(gr); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
231 return NGX_ERROR; |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
232 } |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
233 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
234 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
|
235 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
236 v->len = len; |
2985 | 237 v->valid = 1; |
238 v->no_cacheable = 0; | |
239 v->not_found = 0; | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
240 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
241 GeoIPRecord_delete(gr); |
2985 | 242 |
243 return NGX_OK; | |
244 | |
3031
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
245 no_value: |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
246 |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
247 GeoIPRecord_delete(gr); |
98a8336c5b7c
fix memory leak if GeoIP City database was used
Igor Sysoev <igor@sysoev.ru>
parents:
2985
diff
changeset
|
248 |
2985 | 249 not_found: |
250 | |
251 v->not_found = 1; | |
252 | |
253 return NGX_OK; | |
254 } | |
255 | |
256 | |
257 static ngx_int_t | |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
258 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
|
259 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
|
260 { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
261 float val; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
262 GeoIPRecord *gr; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
263 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
264 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
|
265 if (gr == NULL) { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
266 v->not_found = 1; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
267 return NGX_OK; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
268 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
269 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
270 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
|
271 if (v->data == NULL) { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
272 GeoIPRecord_delete(gr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
273 return NGX_ERROR; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
274 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
275 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
276 val = *(float *) ((char *) gr + data); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
277 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
278 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
|
279 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
280 GeoIPRecord_delete(gr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
281 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
282 return NGX_OK; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
283 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
284 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
285 |
3736
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
286 static ngx_int_t |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
287 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
|
288 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
|
289 { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
290 int val; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
291 GeoIPRecord *gr; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
292 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
293 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
|
294 if (gr == NULL) { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
295 v->not_found = 1; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
296 return NGX_OK; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
297 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
298 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
299 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
|
300 if (v->data == NULL) { |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
301 GeoIPRecord_delete(gr); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
302 return NGX_ERROR; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
303 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
304 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
305 val = *(int *) ((char *) gr + data); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
306 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
307 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
|
308 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
309 GeoIPRecord_delete(gr); |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
310 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
311 return NGX_OK; |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
312 } |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
313 |
257785918797
$geoip_dma_code and $geoip_area_code
Igor Sysoev <igor@sysoev.ru>
parents:
3398
diff
changeset
|
314 |
3398
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
315 static GeoIPRecord * |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
316 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
|
317 { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
318 u_long addr; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
319 struct sockaddr_in *sin; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
320 ngx_http_geoip_conf_t *gcf; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
321 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
322 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
|
323 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
324 if (gcf->city && r->connection->sockaddr->sa_family == AF_INET) { |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
325 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
326 sin = (struct sockaddr_in *) r->connection->sockaddr; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
327 addr = ntohl(sin->sin_addr.s_addr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
328 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
329 return GeoIP_record_by_ipnum(gcf->city, addr); |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
330 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
331 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
332 return NULL; |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
333 } |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
334 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
335 |
236634d2b603
$geoip_city_continent_code, $geoip_latitude, $geoip_longitude
Igor Sysoev <igor@sysoev.ru>
parents:
3031
diff
changeset
|
336 static ngx_int_t |
2985 | 337 ngx_http_geoip_add_variables(ngx_conf_t *cf) |
338 { | |
339 ngx_http_variable_t *var, *v; | |
340 | |
341 for (v = ngx_http_geoip_vars; v->name.len; v++) { | |
342 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
343 if (var == NULL) { | |
344 return NGX_ERROR; | |
345 } | |
346 | |
347 var->get_handler = v->get_handler; | |
348 var->data = v->data; | |
349 } | |
350 | |
351 return NGX_OK; | |
352 } | |
353 | |
354 | |
355 static void * | |
356 ngx_http_geoip_create_conf(ngx_conf_t *cf) | |
357 { | |
358 ngx_pool_cleanup_t *cln; | |
359 ngx_http_geoip_conf_t *conf; | |
360 | |
361 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_geoip_conf_t)); | |
362 if (conf == NULL) { | |
363 return NULL; | |
364 } | |
365 | |
366 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
367 if (cln == NULL) { | |
368 return NULL; | |
369 } | |
370 | |
371 cln->handler = ngx_http_geoip_cleanup; | |
372 cln->data = conf; | |
373 | |
374 return conf; | |
375 } | |
376 | |
377 | |
378 static char * | |
379 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
380 { | |
381 ngx_http_geoip_conf_t *gcf = conf; | |
382 | |
383 ngx_str_t *value; | |
384 | |
385 if (gcf->country) { | |
386 return "is duplicate"; | |
387 } | |
388 | |
389 value = cf->args->elts; | |
390 | |
391 gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); | |
392 | |
393 if (gcf->country == NULL) { | |
394 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
395 "GeoIO_open(\"%V\") failed", &value[1]); | |
396 | |
397 return NGX_CONF_ERROR; | |
398 } | |
399 | |
400 switch (gcf->country->databaseType) { | |
401 | |
402 case GEOIP_COUNTRY_EDITION: | |
403 case GEOIP_PROXY_EDITION: | |
404 case GEOIP_NETSPEED_EDITION: | |
405 | |
406 return NGX_CONF_OK; | |
407 | |
408 default: | |
409 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
410 "invalid GeoIP database \"%V\" type:%d", | |
411 &value[1], gcf->country->databaseType); | |
412 return NGX_CONF_ERROR; | |
413 } | |
414 } | |
415 | |
416 | |
417 static char * | |
418 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
419 { | |
420 ngx_http_geoip_conf_t *gcf = conf; | |
421 | |
422 ngx_str_t *value; | |
423 | |
424 if (gcf->city) { | |
425 return "is duplicate"; | |
426 } | |
427 | |
428 value = cf->args->elts; | |
429 | |
430 gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE); | |
431 | |
432 if (gcf->city == NULL) { | |
433 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
434 "GeoIO_open(\"%V\") failed", &value[1]); | |
435 | |
436 return NGX_CONF_ERROR; | |
437 } | |
438 | |
439 switch (gcf->city->databaseType) { | |
440 | |
441 case GEOIP_CITY_EDITION_REV0: | |
442 case GEOIP_CITY_EDITION_REV1: | |
443 | |
444 return NGX_CONF_OK; | |
445 | |
446 default: | |
447 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
448 "invalid GeoIP City database \"%V\" type:%d", | |
449 &value[1], gcf->city->databaseType); | |
450 return NGX_CONF_ERROR; | |
451 } | |
452 } | |
453 | |
454 | |
455 static void | |
456 ngx_http_geoip_cleanup(void *data) | |
457 { | |
458 ngx_http_geoip_conf_t *gcf = data; | |
459 | |
460 if (gcf->country) { | |
461 GeoIP_delete(gcf->country); | |
462 } | |
463 | |
464 if (gcf->city) { | |
465 GeoIP_delete(gcf->city); | |
466 } | |
467 } |