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