-
Notifications
You must be signed in to change notification settings - Fork 31
/
geoip_handler.c
181 lines (169 loc) · 3.76 KB
/
geoip_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <memory.h>
//#include <ncapi.h>
#include "common.h"
#include "reqpool.h"
//#include "trace.h"
#include "vstring.h"
#include "site.h"
#include "httpd.h"
#include <GeoIP.h>
#include <GeoIPCity.h>
/*
* GEOIP 연동 모듈
* pre-requisite : libgeoip
*/
#define GEOIP_COUNTRY 0
#define GEOIP_CITY 1
#define GEOIP_ISP 2
#define MAX_GEOIP_FILES 3
static GeoIP *__geoip[MAX_GEOIP_FILES] = {NULL, NULL, NULL};
int
gip_load()
{
int i = 0;
vstring_t *country;
vstring_t *city;
vstring_t *isp;
country = scx_get_vstring(gscx__default_site, SV_GEOIP_COUNTRY, NULL);
if (country) {
__geoip[GEOIP_COUNTRY] = GeoIP_open(vs_data(country), GEOIP_STANDARD|GEOIP_CHECK_CACHE);
TRACE((T_INFO, "GEOIP country data loading %s\n", __geoip[GEOIP_COUNTRY]?"ok":"failed"));
}
city = scx_get_vstring(gscx__default_site, SV_GEOIP_CITY, NULL);
if (city) {
__geoip[GEOIP_CITY] = GeoIP_open(vs_data(city), GEOIP_INDEX_CACHE);
TRACE((T_INFO, "GEOIP city data loading %s\n", __geoip[GEOIP_CITY]?"ok":"failed"));
}
isp = scx_get_vstring(gscx__default_site, SV_GEOIP_ISP, NULL);
if (isp) {
__geoip[GEOIP_ISP] = GeoIP_open(vs_data(isp), GEOIP_STANDARD|GEOIP_CHECK_CACHE);
TRACE((T_INFO, "GEOIP isp data loading %s\n", __geoip[GEOIP_ISP]?"ok":"failed"));
}
return 0;
}
/* two-letter country code, for example, "RU", "US".*/
char *
gip_lookup_country2(const char *ip, char *buf)
{
const char *code = NULL;
if (!__geoip[GEOIP_COUNTRY])
return NULL;
code = GeoIP_country_code_by_addr(__geoip[GEOIP_COUNTRY], ip);
if (code) {
#if 1
sprintf(buf, "%s", code);
#else
strcpy(buf, code);
#endif
return buf;
}
return NULL;
}
/* three-letter country code, for example, "RUS", "USA". */
char *
gip_lookup_country3(const char *ip, char *buf)
{
const char *code = NULL;
if (!__geoip[GEOIP_COUNTRY])
return NULL;
code = GeoIP_country_code3_by_addr(__geoip[GEOIP_COUNTRY], ip);
if (code) {
#if 1
sprintf(buf, "%s", code);
#else
strcpy(buf, code);
#endif
return buf;
}
return NULL;
}
/* country name, for example, "Russian Federation", "United States". */
char *
gip_lookup_country_name(const char *ip, char *buf)
{
const char *code = NULL;
if (!__geoip[GEOIP_COUNTRY])
return NULL;
code = GeoIP_country_name_by_addr(__geoip[GEOIP_COUNTRY], ip);
if (code) {
#if 1
sprintf(buf, "%s", code);
#else
strcpy(buf, code);
#endif
return buf;
}
return NULL;
}
/* two-symbol country region code (region, territory, state, province, federal land and the like), for example, "48", "DC". */
char *
gip_lookup_region(const char *ip, char *buf)
{
const char *code = NULL;
if (!__geoip[GEOIP_COUNTRY])
return NULL;
code = GeoIP_region_by_addr(__geoip[GEOIP_COUNTRY], ip);
if (code) {
#if 1
sprintf(buf, "%s", code);
#else
strcpy(buf, code);
#endif
return buf;
}
return NULL;
}
char *
gip_lookup_isp(const char *ip, char *buf)
{
const char *code = NULL;
if (!__geoip[GEOIP_ISP])
return NULL;
code = GeoIP_org_by_addr(__geoip[GEOIP_ISP], ip);
if (code) {
#if 1
sprintf(buf, "%s", code);
#else
strcpy(buf, code);
#endif
return buf;
}
return NULL;
}
/* city name, for example, "Moscow", "Washington". */
char *
gip_lookup_city(const char *ip, char *city)
{
GeoIPRecord *gir = NULL;
if (!__geoip[GEOIP_CITY])
return NULL;
city[0] = '\0';
gir = GeoIP_record_by_addr(__geoip[GEOIP_CITY], ip);
if (gir) {
if(!gir->city){
return NULL;
}
#if 1
sprintf(city, "%s", gir->city);
#else
strcpy(city, gir->city);
#endif
GeoIPRecord_delete(gir);
return city;
}
return NULL;
}
void
gip_close()
{
if (__geoip[GEOIP_COUNTRY])
GeoIP_delete(__geoip[GEOIP_COUNTRY]);
if (__geoip[GEOIP_CITY])
GeoIP_delete(__geoip[GEOIP_CITY]);
if (__geoip[GEOIP_ISP])
GeoIP_delete(__geoip[GEOIP_ISP]);
}