Skip to content

Commit 97c2102

Browse files
authored
Merge pull request #1 from gquintard/spring_cleaning
Spring cleaning
2 parents b3a3828 + 3b83d74 commit 97c2102

File tree

1 file changed

+62
-234
lines changed

1 file changed

+62
-234
lines changed

src/vmod_ip2proxy.c

Lines changed: 62 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -18,256 +18,84 @@
1818
*/
1919
#include <stdlib.h>
2020
#include <string.h>
21-
#include <IP2Proxy.h>
2221

23-
#include <sys/types.h>
24-
#include <sys/stat.h>
25-
#include <unistd.h>
26-
#include <pthread.h>
22+
#include <IP2Proxy.h>
2723

2824
#include "cache/cache.h"
2925

30-
/* Varnish < 6.2 compat */
31-
#ifndef VPFX
32-
# define VPFX(a) vmod_ ## a
33-
# define VARGS(a) vmod_ ## a ## _arg
34-
# define VENUM(a) vmod_enum_ ## a
35-
# define VEVENT(a) a
36-
#else
37-
# define VEVENT(a) VPFX(a)
38-
#endif
39-
40-
#ifndef VRT_H_INCLUDED
41-
#include "vrt.h"
42-
#endif
43-
44-
/* Defined options for querying IP2Location data */
45-
#define query_COUNTRY_SHORT 1
46-
#define query_COUNTRY_LONG 2
47-
#define query_REGION 3
48-
#define query_CITY 4
49-
#define query_ISP 5
50-
#define query_DOMAIN 6
51-
#define query_USAGETYPE 7
52-
#define query_PROXYTYPE 8
53-
#define query_ASN 9
54-
#define query_AS 10
55-
#define query_LASTSEEN 11
56-
#define query_ISPROXY 12
57-
58-
typedef struct vmod_ip2proxy_data {
59-
time_t ip2proxy_db_ts; /* timestamp of the database file */
60-
IP2Proxy *ip2proxy_handle;
61-
pthread_mutex_t lock;
62-
} ip2proxy_data_t;
63-
64-
void
65-
ip2proxy_free(void *d)
26+
static void
27+
ip2proxy_free(void *ptr)
6628
{
67-
ip2proxy_data_t *data = d;
68-
69-
if (data->ip2proxy_handle != NULL) {
70-
IP2Proxy_close(data->ip2proxy_handle);
71-
}
29+
IP2Proxy_close((IP2Proxy *)ptr);
7230
}
7331

7432
VCL_VOID
75-
VPFX(init_db)(VRT_CTX, struct VPFX(priv) *priv, char *filename, char *memtype)
33+
vmod_init_db(VRT_CTX, struct vmod_priv *priv, char *filename, char *memtype)
7634
{
35+
IP2Proxy *IP2ProxyObj;
36+
enum IP2Proxy_mem_type mtype;
7737

7838
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
79-
printf("The filename accepted is %s.\n", (char *) filename);
80-
// printf("The memtype accepted is %s.\n", (char *) memtype);
81-
if (priv->priv != NULL) {
82-
IP2Proxy_close(priv->priv);
39+
AN(priv);
40+
AN(memtype);
41+
42+
if (strcmp(memtype, "IP2PROXY_FILE_IO") == 0)
43+
mtype = IP2PROXY_FILE_IO;
44+
else if (strcmp(memtype, "IP2PROXY_SHARED_MEMORY") == 0)
45+
mtype = IP2PROXY_SHARED_MEMORY;
46+
else if (strcmp(memtype, "IP2PROXY_CACHE_MEMORY") == 0)
47+
mtype = IP2PROXY_CACHE_MEMORY;
48+
else {
49+
VRT_fail(ctx, "IP2Proxy: invalid memtype (%s)", memtype);
50+
return;
8351
}
84-
IP2Proxy *IP2ProxyObj = IP2Proxy_open( (char *) filename);
85-
// if (IP2ProxyObj != NULL) {
86-
// printf ("Can open the database.");
87-
// } else {
88-
// printf ("Cannot open the database. NULL value detected.");
89-
// }
90-
if (strcmp(memtype, "IP2PROXY_FILE_IO") == 0) {
91-
IP2Proxy_open_mem(priv->priv, IP2PROXY_FILE_IO);
92-
} else if (strcmp(memtype, "IP2PROXY_SHARED_MEMORY") == 0) {
93-
IP2Proxy_open_mem(priv->priv, IP2PROXY_SHARED_MEMORY);
94-
} else if (strcmp(memtype, "IP2PROXY_CACHE_MEMORY") == 0) {
95-
IP2Proxy_open_mem(priv->priv, IP2PROXY_CACHE_MEMORY);
96-
}
97-
priv->priv = IP2ProxyObj;
98-
AN(priv->priv);
99-
priv->free = ip2proxy_free;
100-
}
101-
102-
// Use this function to query result, and then extract the field based on user selection
103-
void *
104-
query_all(VRT_CTX, struct VPFX(priv) *priv, char * ip, int option)
105-
{
106-
IP2ProxyRecord *r;
107-
IP2Proxy *handle;
108-
char *result = NULL;
109-
110-
char *ip1 = (char *) ip;
111-
112-
printf("The IP address accepted is %s.\n", ip1);
113-
// printf("The option accepted is %i.\n", option);
114-
115-
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
11652

117-
if (priv->priv != NULL) {
118-
handle = priv->priv;
119-
r = IP2Proxy_get_all(handle, ip1);
53+
if (priv->priv != NULL)
54+
IP2Proxy_close((IP2Proxy *)priv->priv);
12055

121-
if (r != NULL) {
122-
// printf ("Result is not null.");
123-
switch (option) {
124-
case query_COUNTRY_SHORT:
125-
result = WS_Copy(ctx->ws, r->country_short, -1);
126-
// printf ("Calling Country Short.");
127-
break;
128-
case query_COUNTRY_LONG:
129-
result = WS_Copy(ctx->ws, r->country_long, -1);
130-
break;
131-
case query_REGION:
132-
result = WS_Copy(ctx->ws, r->region, -1);
133-
break;
134-
case query_CITY:
135-
result = WS_Copy(ctx->ws, r->city, -1);
136-
break;
137-
case query_ISP:
138-
result = WS_Copy(ctx->ws, r->isp, -1);
139-
break;
140-
case query_DOMAIN:
141-
result = WS_Copy(ctx->ws, r->domain, -1);
142-
break;
143-
case query_USAGETYPE:
144-
result = WS_Copy(ctx->ws, r->usage_type, -1);
145-
break;
146-
case query_PROXYTYPE:
147-
result = WS_Copy(ctx->ws, r->proxy_type, -1);
148-
break;
149-
case query_ASN:
150-
result = WS_Copy(ctx->ws, r->asn, -1);
151-
break;
152-
case query_AS:
153-
result = WS_Copy(ctx->ws, r->as_, -1);
154-
break;
155-
case query_LASTSEEN:
156-
result = WS_Copy(ctx->ws, r->last_seen, -1);
157-
break;
158-
case query_ISPROXY:
159-
result = WS_Copy(ctx->ws, r->is_proxy, -1);
160-
break;
161-
default:
162-
result = WS_Copy(ctx->ws, "-", -1);
163-
break;
164-
}
165-
IP2Proxy_free_record(r);
166-
167-
return (result);
168-
} else {
169-
// printf ("Result is null.");
170-
}
171-
}
172-
173-
// VMOD_LOG("ERROR: IP2Location database failed to load");
174-
175-
return WS_Copy(ctx->ws, "-", -1);
176-
}
177-
178-
VCL_STRING
179-
VPFX(country_short)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
180-
{
181-
const char *result = NULL;
182-
result = query_all(ctx, priv, ip, query_COUNTRY_SHORT);
183-
return (result);
184-
}
185-
186-
VCL_STRING
187-
VPFX(country_long)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
188-
{
189-
const char *result = NULL;
190-
result = query_all(ctx, priv, ip, query_COUNTRY_LONG);
191-
return (result);
192-
}
193-
194-
VCL_STRING
195-
VPFX(region)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
196-
{
197-
const char *result = NULL;
198-
result = query_all(ctx, priv, ip, query_REGION);
199-
return (result);
200-
}
201-
202-
VCL_STRING
203-
VPFX(city)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
204-
{
205-
const char *result = NULL;
206-
result = query_all(ctx, priv, ip, query_CITY);
207-
return (result);
208-
}
209-
210-
VCL_STRING
211-
VPFX(isp)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
212-
{
213-
const char *result = NULL;
214-
result = query_all(ctx, priv, ip, query_ISP);
215-
return (result);
216-
}
217-
218-
VCL_STRING
219-
VPFX(domain)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
220-
{
221-
const char *result = NULL;
222-
result = query_all(ctx, priv, ip, query_DOMAIN);
223-
return (result);
224-
}
225-
226-
VCL_STRING
227-
VPFX(usage_type)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
228-
{
229-
const char *result = NULL;
230-
result = query_all(ctx, priv, ip, query_USAGETYPE);
231-
return (result);
232-
}
233-
234-
VCL_STRING
235-
VPFX(proxy_type)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
236-
{
237-
const char *result = NULL;
238-
result = query_all(ctx, priv, ip, query_PROXYTYPE);
239-
return (result);
240-
}
241-
242-
VCL_STRING
243-
VPFX(asn)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
244-
{
245-
const char *result = NULL;
246-
result = query_all(ctx, priv, ip, query_ASN);
247-
return (result);
248-
}
249-
250-
VCL_STRING
251-
VPFX(as)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
252-
{
253-
const char *result = NULL;
254-
result = query_all(ctx, priv, ip, query_AS);
255-
return (result);
256-
}
56+
IP2ProxyObj = IP2Proxy_open(filename);
57+
if (!IP2ProxyObj) {
58+
VRT_fail(ctx, "IP2Proxy: can't open database (%s)", filename);
59+
return;
60+
}
61+
IP2Proxy_open_mem(IP2ProxyObj, mtype);
25762

258-
VCL_STRING
259-
VPFX(last_seen)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
260-
{
261-
const char *result = NULL;
262-
result = query_all(ctx, priv, ip, query_LASTSEEN);
263-
return (result);
63+
priv->priv = IP2ProxyObj;
64+
priv->free = ip2proxy_free;
26465
}
26566

266-
VCL_STRING
267-
VPFX(is_proxy)(VRT_CTX, struct VPFX(priv) *priv, char * ip)
268-
{
269-
const char *result = NULL;
270-
result = query_all(ctx, priv, ip, query_ISPROXY);
271-
return (result);
67+
#define FUNC(lower, field) \
68+
VCL_STRING \
69+
vmod_ ## lower(VRT_CTX, struct vmod_priv *priv, char * ip) \
70+
{ \
71+
char *result; \
72+
IP2ProxyRecord *r; \
73+
\
74+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); \
75+
AN(priv); \
76+
\
77+
if (!ip || !priv->priv) \
78+
return ("-"); \
79+
\
80+
r = IP2Proxy_get_all((IP2Proxy *)priv->priv, ip); \
81+
if (!r) \
82+
return ("-"); \
83+
\
84+
result = WS_Copy(ctx->ws, r->field, -1); \
85+
IP2Proxy_free_record(r); \
86+
\
87+
return (result); \
27288
}
27389

90+
FUNC(country_short, country_short)
91+
FUNC(country_long, country_long)
92+
FUNC(region, region)
93+
FUNC(city, city)
94+
FUNC(isp, isp)
95+
FUNC(domain, domain)
96+
FUNC(usage_type, usage_type)
97+
FUNC(proxy_type, proxy_type)
98+
FUNC(asn, asn)
99+
FUNC(as, as_)
100+
FUNC(last_seen, last_seen)
101+
FUNC(is_proxy, is_proxy)

0 commit comments

Comments
 (0)