File Makefile.in changed (mode: 100644) (index 3bc3144..a1a71bb) |
1 |
1 |
export CC := gcc |
export CC := gcc |
2 |
|
export INCS += -I. |
|
3 |
|
export LIBS += |
|
4 |
|
export CFLAGS += -ggdb3 -Wall -Wextra -pedantic -Wno-long-long -pipe $(INCS) |
|
|
2 |
|
export INCS += |
|
3 |
|
export LIBS += -L/usr/lib -L/usr/lib64 |
|
4 |
|
|
|
5 |
|
export CFLAGS += -O2 -ggdb -Wall -Wextra -pipe |
|
6 |
|
export CFLAGS += -D _FORTIFY_SOURCES=2 |
|
7 |
|
export CFLAGS += @CC_SWITCHES@ |
|
8 |
|
|
5 |
9 |
export OBJS += i_util.o parser_text.o parser_ip2location.o parser.o i_conf.o \ |
export OBJS += i_util.o parser_text.o parser_ip2location.o parser.o i_conf.o \ |
6 |
10 |
parser_core.o |
parser_core.o |
7 |
11 |
|
|
8 |
12 |
.PHONY: all |
.PHONY: all |
9 |
|
all: ip2clued ip2clue |
|
|
13 |
|
all: ip2clued ip2clue ip2clue_stress |
10 |
14 |
|
|
11 |
15 |
ip2clued: $(OBJS) ip2clued.c |
ip2clued: $(OBJS) ip2clued.c |
12 |
|
$(CC) $(CFLAGS) ip2clued.c -o ip2clued $(OBJS) -lpthread -lConn |
|
|
16 |
|
$(CC) $(CFLAGS) $(INCS) ip2clued.c -o ip2clued $(OBJS) $(LIBS) -lpthread -lConn |
13 |
17 |
|
|
14 |
18 |
ip2clue: $(OBJS) ip2clue.c |
ip2clue: $(OBJS) ip2clue.c |
15 |
|
$(CC) $(CFLAGS) ip2clue.c -o ip2clue $(OBJS) -lConn |
|
|
19 |
|
$(CC) $(CFLAGS) $(INCS) ip2clue.c -o ip2clue $(OBJS) $(LIBS) -lConn |
|
20 |
|
|
|
21 |
|
ip2clue_stress: $(OBJS) ip2clue_stress.c |
|
22 |
|
$(CC) $(CFLAGS) $(INCS) ip2clue_stress.c -o ip2clue_stress $(OBJS) $(LIBS) -lConn |
16 |
23 |
|
|
17 |
24 |
i_util.o: i_util.c i_util.h i_types.h i_config.h |
i_util.o: i_util.c i_util.h i_types.h i_config.h |
18 |
|
$(CC) $(CFLAGS) -c $< |
|
|
25 |
|
$(CC) $(CFLAGS) $(INCS) -c $< |
19 |
26 |
|
|
20 |
27 |
parser_text.o: parser_text.c parser_text.h i_util.o parser_core.o |
parser_text.o: parser_text.c parser_text.h i_util.o parser_core.o |
21 |
|
$(CC) $(CFLAGS) -c $< |
|
|
28 |
|
$(CC) $(CFLAGS) $(INCS) -c $< |
22 |
29 |
|
|
23 |
30 |
parser_ip2location.o: parser_ip2location.c i_util.o |
parser_ip2location.o: parser_ip2location.c i_util.o |
24 |
|
$(CC) $(CFLAGS) -c $< |
|
|
31 |
|
$(CC) $(CFLAGS) $(INCS) -c $< |
25 |
32 |
|
|
26 |
33 |
parser.o: parser.c parser.h parser_text.o parser_ip2location.o parser_core.o |
parser.o: parser.c parser.h parser_text.o parser_ip2location.o parser_core.o |
27 |
|
$(CC) $(CFLAGS) -c $< |
|
|
34 |
|
$(CC) $(CFLAGS) $(INCS) -c $< |
28 |
35 |
|
|
29 |
36 |
i_conf.o: i_conf.c i_conf.h i_config.h |
i_conf.o: i_conf.c i_conf.h i_config.h |
30 |
|
$(CC) $(CFLAGS) -c $< |
|
|
37 |
|
$(CC) $(CFLAGS) $(INCS) -c $< |
31 |
38 |
|
|
32 |
39 |
.PHONY: clean |
.PHONY: clean |
33 |
40 |
clean: |
clean: |
34 |
41 |
rm -f $(OBJS) ip2clued ip2clue |
rm -f $(OBJS) ip2clued ip2clue |
35 |
42 |
|
|
36 |
43 |
install: all |
install: all |
37 |
|
mkdir -p "${I_VAR}/cache/ip2clue" |
|
|
44 |
|
mkdir -p "${I_VAR}/cache/${PRJ}" |
38 |
45 |
mkdir -p "${I_ETC}/${PRJ}" |
mkdir -p "${I_ETC}/${PRJ}" |
39 |
46 |
cp -vd etc/*.conf "${I_ETC}/${PRJ}/" |
cp -vd etc/*.conf "${I_ETC}/${PRJ}/" |
40 |
47 |
mkdir -p "${I_ETC}/rc.d/init.d/" |
mkdir -p "${I_ETC}/rc.d/init.d/" |
|
... |
... |
install: all |
45 |
52 |
cp -vd ip2clued "${I_USR_SBIN}" |
cp -vd ip2clued "${I_USR_SBIN}" |
46 |
53 |
mkdir -p "${I_ETC}/cron.daily/" |
mkdir -p "${I_ETC}/cron.daily/" |
47 |
54 |
cp -vd crons/* "${I_ETC}/cron.daily/" |
cp -vd crons/* "${I_ETC}/cron.daily/" |
|
55 |
|
mkdir -p "${I_USR_SHARE}/${PRJ}" |
|
56 |
|
cp -vd data/* "${I_USR_SHARE}/${PRJ}/" |
File README changed (mode: 100644) (index e2c35e1..89d0824) |
1 |
1 |
Name: ip2clue |
Name: ip2clue |
2 |
|
Description: High-performance IPv4 and IPv6 daemon to retrieve IP information |
|
|
2 |
|
Description: High-performance IPv4 and IPv6 daemon to retrieve IPv4/6 |
|
3 |
|
country information |
3 |
4 |
Author: Catalin(ux) M. BOIE |
Author: Catalin(ux) M. BOIE |
4 |
|
Web: http://kernel.embedromix.ro/us/ |
|
|
5 |
|
Web: https://rocketgit.com/user/catalinux/ip2clue |
5 |
6 |
|
|
6 |
7 |
|
|
7 |
8 |
. Paid support |
. Paid support |
|
... |
... |
Software 77, Webhosting.info and ip2location) to retrieve IP information |
14 |
15 |
(country short, country long etc.). |
(country short, country long etc.). |
15 |
16 |
It has a daemon that supports lots of concurrent connections thanks to Conn |
It has a daemon that supports lots of concurrent connections thanks to Conn |
16 |
17 |
library (also at http://kernel.embedromix.ro/us/), and a client binary. |
library (also at http://kernel.embedromix.ro/us/), and a client binary. |
17 |
|
Also, a PHP function and an example are provided. |
|
|
18 |
|
Also, some client examples are provided (PHP, C etc.). |
18 |
19 |
|
|
19 |
|
Database supported: |
|
20 |
|
- http://ip-to-country.webhosting.info/ |
|
21 |
|
- http://software77.net/ |
|
22 |
|
- http://www.maxmind.com/app/geolitecountry - both IPv4 and IPv6 |
|
|
20 |
|
Databases supported: |
|
21 |
|
- http://ip-to-country.webhosting.info/ (format 'webhosting') |
|
22 |
|
- http://software77.net/ (format 'software77') |
|
23 |
|
- http://www.maxmind.com/app/geolitecountry - both IPv4 and IPv6 (formats |
|
24 |
|
'maxmind' and 'maxmind-v6') |
|
25 |
|
- http://www.ip2location.com/ (format 'ip2location') |
23 |
26 |
|
|
24 |
27 |
|
|
25 |
28 |
. Configuration |
. Configuration |
26 |
29 |
- Edit /etc/ip2clue/download.conf to start automatically download the data. |
- Edit /etc/ip2clue/download.conf to start automatically download the data. |
27 |
30 |
- Edit /etc/ip2clue/ip2clued.conf to configure IPv4/IPv6 support, port etc. |
- Edit /etc/ip2clue/ip2clued.conf to configure IPv4/IPv6 support, port etc. |
|
31 |
|
|
|
32 |
|
|
|
33 |
|
. Running & operations |
28 |
34 |
- Start the ip2clued daemon (service ip2clued start, for example). |
- Start the ip2clued daemon (service ip2clued start, for example). |
29 |
35 |
- Connect to it with "telnet localhost 9999" and type "S" and Enter: |
- Connect to it with "telnet localhost 9999" and type "S" and Enter: |
30 |
36 |
statistics will be displayed. Depending on databases sizes, will take some time |
statistics will be displayed. Depending on databases sizes, will take some time |
|
... |
... |
then press Enter: Something like "OK ip=193.193.193.193 cs=UA cl= tz= isp=" |
34 |
40 |
will appear. |
will appear. |
35 |
41 |
|
|
36 |
42 |
|
|
|
43 |
|
. Performance |
|
44 |
|
- 2010-07-07, Athlon X2 5400+, running ip2clue_stress.php on the same machine |
|
45 |
|
(loopback network, IPv4), it achived more than 10.000 requests per second. |
|
46 |
|
ip2clue_stress (C version) achieved more than 12.500 requests per second. |
|
47 |
|
|
|
48 |
|
|
37 |
49 |
. License |
. License |
38 |
50 |
GPLv3 or later |
GPLv3 or later |
File clients/ip2clue.inc.php changed (mode: 100644) (index 9a3df49..9f4f5dd) |
... |
... |
function ip2clue_lookup($ip2clue_host, $ip2clue_port, $timeout, $ip) |
30 |
30 |
$ip2clue_fd = $fd; |
$ip2clue_fd = $fd; |
31 |
31 |
} |
} |
32 |
32 |
|
|
33 |
|
$h = fwrite($ip2clue_fd, "R$ip\n"); |
|
|
33 |
|
stream_set_timeout($ip2clue_fd, $timeout); |
|
34 |
|
|
|
35 |
|
$h = @fwrite($ip2clue_fd, "R$ip\n"); |
34 |
36 |
if ($h === FALSE) { |
if ($h === FALSE) { |
35 |
37 |
$ret['errmsg'] = "Cannot write to $ip2clue_host" |
$ret['errmsg'] = "Cannot write to $ip2clue_host" |
36 |
38 |
. ":$ip2clue_port"; |
. ":$ip2clue_port"; |
|
... |
... |
function ip2clue_lookup($ip2clue_host, $ip2clue_port, $timeout, $ip) |
39 |
41 |
return $ret; |
return $ret; |
40 |
42 |
} |
} |
41 |
43 |
|
|
42 |
|
$buf = fgets($ip2clue_fd, 8192); |
|
|
44 |
|
$buf = @fgets($ip2clue_fd, 8192); |
43 |
45 |
if ($buf === FALSE) { |
if ($buf === FALSE) { |
44 |
46 |
$ret['errmsg'] = "Cannot read from $ip2clue_host" |
$ret['errmsg'] = "Cannot read from $ip2clue_host" |
45 |
47 |
. ":$ip2clue_port"; |
. ":$ip2clue_port"; |
|
... |
... |
function ip2clue_lookup($ip2clue_host, $ip2clue_port, $timeout, $ip) |
48 |
50 |
return $ret; |
return $ret; |
49 |
51 |
} |
} |
50 |
52 |
|
|
|
53 |
|
$info = stream_get_meta_data($ip2clue_fd); |
|
54 |
|
if ($info['timed_out']) { |
|
55 |
|
$ret['errmsg'] = "Timout reading from $ip2clue_host" |
|
56 |
|
. ":$ip2clue_port"; |
|
57 |
|
close($ip2clue_fd); |
|
58 |
|
$ip2clue_fd = -1; |
|
59 |
|
return $ret; |
|
60 |
|
} |
|
61 |
|
|
51 |
62 |
if (strncmp($buf, "OK", 2) != 0) { |
if (strncmp($buf, "OK", 2) != 0) { |
52 |
63 |
$ret['errmsg'] = $buf; |
$ret['errmsg'] = $buf; |
53 |
64 |
return $ret; |
return $ret; |
File etc/ip2clued.conf changed (mode: 100644) (index 763c614..bf31a19) |
... |
... |
datadir = /var/cache/ip2clue |
4 |
4 |
|
|
5 |
5 |
# Now, follows the input files, in preference order (if the IP is not found in |
# Now, follows the input files, in preference order (if the IP is not found in |
6 |
6 |
# first file, it will be searched in the second and so on). |
# first file, it will be searched in the second and so on). |
|
7 |
|
# Separate them by ','. |
7 |
8 |
# The format is: type1:file1, type2:file2... |
# The format is: type1:file1, type2:file2... |
8 |
9 |
# Allowed formats: webhosting, maxmind, maxmind-v6, software77, ip2location. |
# Allowed formats: webhosting, maxmind, maxmind-v6, software77, ip2location. |
9 |
10 |
# You can mix IPv4 and IPv6 files. |
# You can mix IPv4 and IPv6 files. |
|
... |
... |
datadir = /var/cache/ip2clue |
14 |
15 |
#files = webhosting:ip-to-country.csv |
#files = webhosting:ip-to-country.csv |
15 |
16 |
# only software77 |
# only software77 |
16 |
17 |
#files = software77:IpToCountry.csv |
#files = software77:IpToCountry.csv |
|
18 |
|
# only ip2location IPv6 sample |
|
19 |
|
files = ip2location:IPV6-COUNTRY.SAMPLE.BIN |
17 |
20 |
# all |
# all |
18 |
|
#files = maxmind:GeoIPCountryWhois.csv, maxmind-v6:maxmind-ipv6.csv webhosting:ip-to-country.csv software77:IpToCountry.csv |
|
|
21 |
|
#files = maxmind:GeoIPCountryWhois.csv, maxmind-v6:maxmind-ipv6.csv, webhosting:ip-to-country.csv, software77:IpToCountry.csv, ip2location:IPV6-COUNTRY.SAMPLE.BIN |
19 |
22 |
|
|
20 |
23 |
# Format for answer |
# Format for answer |
21 |
24 |
# %a - country short |
# %a - country short |
File i_util.c changed (mode: 100644) (index 7288a53..c9658a4) |
3 |
3 |
* Description: parser for IP files |
* Description: parser for IP files |
4 |
4 |
*/ |
*/ |
5 |
5 |
|
|
6 |
|
#include <i_config.h> |
|
|
6 |
|
#include "i_config.h" |
7 |
7 |
|
|
8 |
8 |
#include <stdio.h> |
#include <stdio.h> |
9 |
9 |
#include <string.h> |
#include <string.h> |
|
14 |
14 |
#include <netinet/in.h> |
#include <netinet/in.h> |
15 |
15 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
16 |
16 |
#include <errno.h> |
#include <errno.h> |
|
17 |
|
#include <stdarg.h> |
17 |
18 |
|
|
18 |
|
#include <i_util.h> |
|
|
19 |
|
#include "i_util.h" |
19 |
20 |
|
|
20 |
|
char ip2clue_error[256]; |
|
|
21 |
|
__thread char ip2clue_error[256]; |
21 |
22 |
|
|
22 |
23 |
/* |
/* |
23 |
24 |
* Returns ip2clue_error content |
* Returns ip2clue_error content |
|
... |
... |
char *ip2clue_strerror(void) |
27 |
28 |
return ip2clue_error; |
return ip2clue_error; |
28 |
29 |
} |
} |
29 |
30 |
|
|
|
31 |
|
/* |
|
32 |
|
* Set error message |
|
33 |
|
*/ |
|
34 |
|
void ip2clue_set_error(const char *format, ...) |
|
35 |
|
{ |
|
36 |
|
va_list ap; |
|
37 |
|
|
|
38 |
|
va_start(ap, format); |
|
39 |
|
vsnprintf(ip2clue_error, sizeof(ip2clue_error), format, ap); |
|
40 |
|
va_end(ap); |
|
41 |
|
} |
|
42 |
|
|
30 |
43 |
/* |
/* |
31 |
44 |
* Show a nice IPv6 address |
* Show a nice IPv6 address |
32 |
45 |
* TODO: Replace with inet_ntop! |
* TODO: Replace with inet_ntop! |
|
... |
... |
int ip2clue_addr_v6(char *out, size_t out_size, const unsigned int *a) |
45 |
58 |
/* |
/* |
46 |
59 |
* Compare two IPv6 addresses |
* Compare two IPv6 addresses |
47 |
60 |
*/ |
*/ |
48 |
|
int ip2clue_compare_v6(const unsigned int *a, const unsigned int *b) |
|
|
61 |
|
static int ip2clue_compare_v6(const unsigned int *a, const unsigned int *b) |
49 |
62 |
{ |
{ |
50 |
63 |
unsigned int i; |
unsigned int i; |
51 |
64 |
|
|
|
... |
... |
long ip2clue_file_lines(const char *file) |
180 |
193 |
*/ |
*/ |
181 |
194 |
void ip2clue_destroy(struct ip2clue_db *db) |
void ip2clue_destroy(struct ip2clue_db *db) |
182 |
195 |
{ |
{ |
183 |
|
unsigned int i; |
|
|
196 |
|
int i; |
184 |
197 |
struct ip2clue_cell_v4 *p_cell4, *cell4; |
struct ip2clue_cell_v4 *p_cell4, *cell4; |
185 |
198 |
struct ip2clue_cell_v6 *p_cell6, *cell6; |
struct ip2clue_cell_v6 *p_cell6, *cell6; |
186 |
199 |
|
|
File i_util.h changed (mode: 100644) (index 479062a..619bd9e) |
1 |
1 |
#ifndef IP2CLUE_I_UTIL_H |
#ifndef IP2CLUE_I_UTIL_H |
2 |
2 |
#define IP2CLUE_I_UTIL_H 1 |
#define IP2CLUE_I_UTIL_H 1 |
3 |
3 |
|
|
4 |
|
#include <i_config.h> |
|
|
4 |
|
#include "i_config.h" |
5 |
5 |
|
|
6 |
6 |
#include <stdlib.h> |
#include <stdlib.h> |
7 |
7 |
|
|
8 |
|
#include <i_types.h> |
|
|
8 |
|
#include "i_types.h" |
9 |
9 |
|
|
10 |
|
extern char ip2clue_error[256]; |
|
|
10 |
|
char *ip2clue_strerror(void); |
|
11 |
|
void ip2clue_set_error(const char *format, ...); |
11 |
12 |
|
|
12 |
|
extern char *ip2clue_strerror(void); |
|
13 |
|
|
|
14 |
|
extern int ip2clue_addr_v6(char *out, size_t out_size, |
|
|
13 |
|
int ip2clue_addr_v6(char *out, size_t out_size, |
15 |
14 |
const unsigned int *a); |
const unsigned int *a); |
16 |
15 |
|
|
17 |
|
extern int ip2clue_split(struct ip2clue_split *s, const char *line, |
|
|
16 |
|
int ip2clue_split(struct ip2clue_split *s, const char *line, |
18 |
17 |
const char *sep); |
const char *sep); |
19 |
18 |
|
|
20 |
|
extern long ip2clue_file_lines(const char *file); |
|
|
19 |
|
long ip2clue_file_lines(const char *file); |
21 |
20 |
|
|
22 |
|
extern void ip2clue_destroy(struct ip2clue_db *db); |
|
|
21 |
|
void ip2clue_destroy(struct ip2clue_db *db); |
23 |
22 |
|
|
24 |
|
extern void ip2clue_print_extra(char *out, size_t out_size, |
|
|
23 |
|
void ip2clue_print_extra(char *out, size_t out_size, |
25 |
24 |
const struct ip2clue_extra *e); |
const struct ip2clue_extra *e); |
26 |
25 |
|
|
27 |
26 |
/* v4 */ |
/* v4 */ |
28 |
|
extern struct ip2clue_cell_v4 *ip2clue_search_v4(struct ip2clue_db *db, |
|
|
27 |
|
struct ip2clue_cell_v4 *ip2clue_search_v4(struct ip2clue_db *db, |
29 |
28 |
const char *ip); |
const char *ip); |
30 |
|
extern struct ip2clue_cell_v4 *ip2clue_list_search_v4(struct ip2clue_list *list, |
|
|
29 |
|
struct ip2clue_cell_v4 *ip2clue_list_search_v4(struct ip2clue_list *list, |
31 |
30 |
const char *ip); |
const char *ip); |
32 |
|
extern void ip2clue_dump_cell_v4(char *out, size_t out_size, |
|
|
31 |
|
void ip2clue_dump_cell_v4(char *out, size_t out_size, |
33 |
32 |
struct ip2clue_cell_v4 *cell); /* TODO */ |
struct ip2clue_cell_v4 *cell); /* TODO */ |
34 |
33 |
|
|
35 |
34 |
/* v6 */ |
/* v6 */ |
36 |
|
extern struct ip2clue_cell_v6 *ip2clue_search_v6(struct ip2clue_db *db, |
|
|
35 |
|
struct ip2clue_cell_v6 *ip2clue_search_v6(struct ip2clue_db *db, |
37 |
36 |
const char *ip); |
const char *ip); |
38 |
|
extern struct ip2clue_cell_v6 *ip2clue_list_search_v6(struct ip2clue_list *list, |
|
|
37 |
|
struct ip2clue_cell_v6 *ip2clue_list_search_v6(struct ip2clue_list *list, |
39 |
38 |
const char *ip); |
const char *ip); |
40 |
|
extern void ip2clue_dump_cell_v6(char *out, size_t out_size, |
|
|
39 |
|
void ip2clue_dump_cell_v6(char *out, size_t out_size, |
41 |
40 |
struct ip2clue_cell_v6 *cell); |
struct ip2clue_cell_v6 *cell); |
42 |
41 |
|
|
43 |
42 |
/* common */ |
/* common */ |
44 |
|
extern int ip2clue_list_search(struct ip2clue_list *list, |
|
|
43 |
|
int ip2clue_list_search(struct ip2clue_list *list, |
45 |
44 |
char *out, const unsigned int out_size, |
char *out, const unsigned int out_size, |
46 |
45 |
const char *format, const char *ip); |
const char *format, const char *ip); |
47 |
46 |
#endif |
#endif |
File ip2clue.c changed (mode: 100644) (index 6b4dd0c..e830c97) |
1 |
|
#include <i_config.h> |
|
|
1 |
|
#include "i_config.h" |
2 |
2 |
|
|
3 |
3 |
#include <stdio.h> |
#include <stdio.h> |
4 |
4 |
#include <string.h> |
#include <string.h> |
5 |
5 |
#include <sys/time.h> |
#include <sys/time.h> |
|
6 |
|
#include <sys/socket.h> |
6 |
7 |
|
|
7 |
8 |
#include <Conn.h> |
#include <Conn.h> |
8 |
9 |
|
|
9 |
|
#include <i_types.h> |
|
10 |
|
#include <i_util.h> |
|
11 |
|
#include <i_conf.h> |
|
12 |
|
#include <parser.h> |
|
|
10 |
|
#include "i_types.h" |
|
11 |
|
#include "i_util.h" |
|
12 |
|
#include "i_conf.h" |
|
13 |
|
#include "parser.h" |
13 |
14 |
|
|
14 |
|
static FILE *Logf = NULL; |
|
|
15 |
|
static int log_fd = 1; |
15 |
16 |
static int conf_port = 9999; |
static int conf_port = 9999; |
16 |
17 |
static char cmd[128]; |
static char cmd[128]; |
17 |
18 |
|
|
|
... |
... |
static void connected_cb(struct Conn *C) |
26 |
27 |
} |
} |
27 |
28 |
} |
} |
28 |
29 |
|
|
29 |
|
static int data_cb(struct Conn *C, char *line) |
|
|
30 |
|
static void data_cb(struct Conn *C, char *line) |
30 |
31 |
{ |
{ |
31 |
32 |
printf("%s\n", line); |
printf("%s\n", line); |
32 |
33 |
Conn_close(C); |
Conn_close(C); |
33 |
|
|
|
34 |
|
return 0; |
|
35 |
34 |
} |
} |
36 |
35 |
|
|
37 |
36 |
static void data(struct Conn *C) |
static void data(struct Conn *C) |
|
... |
... |
int main(int argc, char *argv[]) |
59 |
58 |
/* prepare sending */ |
/* prepare sending */ |
60 |
59 |
snprintf(cmd, sizeof(cmd), "R%s\n", argv[1]); |
snprintf(cmd, sizeof(cmd), "R%s\n", argv[1]); |
61 |
60 |
|
|
62 |
|
Conn_debug(Logf, 0); |
|
|
61 |
|
Conn_debug(log_fd, 0); |
63 |
62 |
|
|
64 |
63 |
if (argc >= 3) |
if (argc >= 3) |
65 |
64 |
conf_port = strtol(argv[2], NULL, 10); |
conf_port = strtol(argv[2], NULL, 10); |
|
... |
... |
int main(int argc, char *argv[]) |
71 |
70 |
return 1; |
return 1; |
72 |
71 |
} |
} |
73 |
72 |
|
|
74 |
|
/* set callbacks */ |
|
75 |
|
Conn_data_cb = data; |
|
76 |
|
Conn_error_cb = error; |
|
77 |
|
Conn_connected_cb = connected_cb; |
|
78 |
|
|
|
79 |
73 |
/* prefer IPv6 */ |
/* prefer IPv6 */ |
80 |
74 |
Log(1, "IPv6...\n"); |
Log(1, "IPv6...\n"); |
81 |
75 |
C = Conn_alloc(); |
C = Conn_alloc(); |
|
... |
... |
int main(int argc, char *argv[]) |
89 |
83 |
Conn_set_socket_type(C, SOCK_STREAM); |
Conn_set_socket_type(C, SOCK_STREAM); |
90 |
84 |
Conn_set_socket_addr(C, "::1"); |
Conn_set_socket_addr(C, "::1"); |
91 |
85 |
Conn_set_socket_port(C, conf_port); |
Conn_set_socket_port(C, conf_port); |
|
86 |
|
|
92 |
87 |
ret = Conn_commit(C); |
ret = Conn_commit(C); |
93 |
88 |
if (ret != 0) { |
if (ret != 0) { |
94 |
89 |
Log(0, "Cannot commit v6 (%s)!\n", |
Log(0, "Cannot commit v6 (%s)!\n", |
|
... |
... |
int main(int argc, char *argv[]) |
107 |
102 |
} |
} |
108 |
103 |
} |
} |
109 |
104 |
|
|
|
105 |
|
Conn_set_cb(C, CONN_CB_DATA, data); |
|
106 |
|
Conn_set_cb(C, CONN_CB_ERROR, error); |
|
107 |
|
Conn_set_cb(C, CONN_CB_CONNECTED, connected_cb); |
|
108 |
|
|
|
109 |
|
|
110 |
110 |
Log(1, "Start polling...\n"); |
Log(1, "Start polling...\n"); |
111 |
111 |
while (1) { |
while (1) { |
112 |
112 |
ret = Conn_poll(-1); |
ret = Conn_poll(-1); |
File ip2clued.c changed (mode: 100644) (index 6068fa3..d4d5cbc) |
1 |
|
#include <i_config.h> |
|
|
1 |
|
#include "i_config.h" |
2 |
2 |
|
|
3 |
3 |
#include <stdio.h> |
#include <stdio.h> |
4 |
4 |
#include <string.h> |
#include <string.h> |
5 |
5 |
#include <sys/time.h> |
#include <sys/time.h> |
6 |
6 |
#include <pthread.h> |
#include <pthread.h> |
|
7 |
|
#include <unistd.h> |
|
8 |
|
#include <errno.h> |
|
9 |
|
#include <sys/socket.h> |
7 |
10 |
|
|
8 |
11 |
#include <Conn.h> |
#include <Conn.h> |
9 |
12 |
|
|
10 |
|
#include <i_types.h> |
|
11 |
|
#include <i_util.h> |
|
12 |
|
#include <i_conf.h> |
|
13 |
|
#include <parser.h> |
|
|
13 |
|
#include "i_types.h" |
|
14 |
|
#include "i_util.h" |
|
15 |
|
#include "i_conf.h" |
|
16 |
|
#include "parser.h" |
14 |
17 |
|
|
15 |
|
static FILE *Logf = NULL; |
|
|
18 |
|
static int log_fd = 1; |
16 |
19 |
static char *log_file = "/var/log/ip2clued.log"; |
static char *log_file = "/var/log/ip2clued.log"; |
17 |
20 |
|
|
18 |
21 |
/* conf stuff */ |
/* conf stuff */ |
|
... |
... |
static pthread_rwlock_t list_rwlock; |
33 |
36 |
static struct ip2clue_list list; |
static struct ip2clue_list list; |
34 |
37 |
|
|
35 |
38 |
|
|
36 |
|
static int data_cb(struct Conn *C, char *line) |
|
|
39 |
|
static void data_cb(struct Conn *C, char *line) |
37 |
40 |
{ |
{ |
38 |
|
int err; |
|
|
41 |
|
int err, close = 0; |
39 |
42 |
char out[2048], *p; |
char out[2048], *p; |
40 |
43 |
|
|
41 |
44 |
switch (line[0]) { |
switch (line[0]) { |
|
... |
... |
static int data_cb(struct Conn *C, char *line) |
71 |
74 |
break; |
break; |
72 |
75 |
*/ |
*/ |
73 |
76 |
|
|
|
77 |
|
case 'q': |
|
78 |
|
case 'Q': |
|
79 |
|
strcpy(out, "Bye!"); |
|
80 |
|
close = 1; |
|
81 |
|
break; |
|
82 |
|
|
74 |
83 |
default: |
default: |
75 |
84 |
strcpy(out, "errmsg=\"Invalid command\""); |
strcpy(out, "errmsg=\"Invalid command\""); |
76 |
85 |
break; |
break; |
|
... |
... |
static int data_cb(struct Conn *C, char *line) |
85 |
94 |
Conn_close(C); |
Conn_close(C); |
86 |
95 |
} |
} |
87 |
96 |
|
|
88 |
|
return 0; |
|
|
97 |
|
if (close == 1) |
|
98 |
|
Conn_close(C); |
89 |
99 |
} |
} |
90 |
100 |
|
|
91 |
101 |
static void data(struct Conn *C) |
static void data(struct Conn *C) |
|
... |
... |
int main(int argc, char *argv[]) |
144 |
154 |
void *res; |
void *res; |
145 |
155 |
struct ip2clue_conf *conf; |
struct ip2clue_conf *conf; |
146 |
156 |
|
|
147 |
|
Logf = fopen(log_file, "w"); |
|
148 |
|
if (!Logf) { |
|
|
157 |
|
log_fd = open(log_file, O_WRONLY | O_TRUNC | O_CREAT, 0600); |
|
158 |
|
if (log_fd == -1) { |
149 |
159 |
fprintf(stderr, "Cannot open log file [%s] (%s)!\n", |
fprintf(stderr, "Cannot open log file [%s] (%s)!\n", |
150 |
160 |
log_file, strerror(errno)); |
log_file, strerror(errno)); |
151 |
161 |
return 1; |
return 1; |
152 |
162 |
} |
} |
153 |
|
setlinebuf(Logf); |
|
154 |
163 |
|
|
155 |
164 |
|
|
156 |
165 |
/* Load config file */ |
/* Load config file */ |
|
... |
... |
int main(int argc, char *argv[]) |
207 |
216 |
ip2clue_list_init(&list); |
ip2clue_list_init(&list); |
208 |
217 |
|
|
209 |
218 |
|
|
210 |
|
Conn_debug(Logf, conf_debug); |
|
|
219 |
|
Conn_debug(log_fd, conf_debug); |
211 |
220 |
|
|
212 |
221 |
Log(0, "Starting 'files reload' thread...\n"); |
Log(0, "Starting 'files reload' thread...\n"); |
213 |
222 |
ret = pthread_create(&workers[0], NULL, worker_loader, NULL); |
ret = pthread_create(&workers[0], NULL, worker_loader, NULL); |
|
... |
... |
int main(int argc, char *argv[]) |
225 |
234 |
return 1; |
return 1; |
226 |
235 |
} |
} |
227 |
236 |
|
|
228 |
|
/* set callbacks */ |
|
229 |
|
Conn_data_cb = data; |
|
230 |
|
Conn_error_cb = error; |
|
231 |
|
|
|
232 |
237 |
if (conf_ipv4 == 1) { |
if (conf_ipv4 == 1) { |
233 |
238 |
Log(0, "IPv4...\n"); |
Log(0, "IPv4...\n"); |
234 |
239 |
C4 = Conn_alloc(); |
C4 = Conn_alloc(); |
|
... |
... |
int main(int argc, char *argv[]) |
241 |
246 |
Conn_set_socket_type(C4, SOCK_STREAM); |
Conn_set_socket_type(C4, SOCK_STREAM); |
242 |
247 |
Conn_set_socket_bind_addr(C4, "0.0.0.0"); |
Conn_set_socket_bind_addr(C4, "0.0.0.0"); |
243 |
248 |
Conn_set_socket_bind_port(C4, conf_port); |
Conn_set_socket_bind_port(C4, conf_port); |
|
249 |
|
|
|
250 |
|
/* set callbacks */ |
|
251 |
|
Conn_set_cb(C4, CONN_CB_DATA, data); |
|
252 |
|
Conn_set_cb(C4, CONN_CB_ERROR, error); |
|
253 |
|
|
244 |
254 |
ret = Conn_commit(C4); |
ret = Conn_commit(C4); |
245 |
255 |
if (ret != 0) { |
if (ret != 0) { |
246 |
256 |
Log(0, "Cannot commit (%s)!\n", |
Log(0, "Cannot commit (%s)!\n", |
|
... |
... |
int main(int argc, char *argv[]) |
261 |
271 |
Conn_set_socket_type(C6, SOCK_STREAM); |
Conn_set_socket_type(C6, SOCK_STREAM); |
262 |
272 |
Conn_set_socket_bind_addr(C6, "::"); |
Conn_set_socket_bind_addr(C6, "::"); |
263 |
273 |
Conn_set_socket_bind_port(C6, conf_port); |
Conn_set_socket_bind_port(C6, conf_port); |
|
274 |
|
|
|
275 |
|
/* set callbacks */ |
|
276 |
|
Conn_set_cb(C4, CONN_CB_DATA, data); |
|
277 |
|
Conn_set_cb(C4, CONN_CB_ERROR, error); |
|
278 |
|
|
264 |
279 |
ret = Conn_commit(C6); |
ret = Conn_commit(C6); |
265 |
280 |
if (ret != 0) { |
if (ret != 0) { |
266 |
281 |
Log(0, "Cannot commit (%s)!\n", |
Log(0, "Cannot commit (%s)!\n", |
|
... |
... |
int main(int argc, char *argv[]) |
299 |
314 |
|
|
300 |
315 |
ip2clue_conf_free(conf); |
ip2clue_conf_free(conf); |
301 |
316 |
|
|
302 |
|
fclose(Logf); |
|
|
317 |
|
close(log_fd); |
303 |
318 |
|
|
304 |
319 |
return 0; |
return 0; |
305 |
320 |
} |
} |
File parser.c changed (mode: 100644) (index e6d8fb9..88d5eb5) |
3 |
3 |
* Description: parser for IP files |
* Description: parser for IP files |
4 |
4 |
*/ |
*/ |
5 |
5 |
|
|
6 |
|
#include <i_config.h> |
|
|
6 |
|
#include "i_config.h" |
7 |
7 |
|
|
8 |
8 |
#include <sys/time.h> |
#include <sys/time.h> |
9 |
9 |
#include <sys/types.h> |
#include <sys/types.h> |
|
12 |
12 |
#include <string.h> |
#include <string.h> |
13 |
13 |
#include <unistd.h> |
#include <unistd.h> |
14 |
14 |
|
|
15 |
|
#include <i_util.h> |
|
16 |
|
#include <parser_core.h> |
|
17 |
|
#include <parser_text.h> |
|
18 |
|
#include <parser_ip2location.h> |
|
|
15 |
|
#include "i_util.h" |
|
16 |
|
#include "parser_core.h" |
|
17 |
|
#include "parser_text.h" |
|
18 |
|
#include "parser_ip2location.h" |
|
19 |
|
#include "parser.h" |
19 |
20 |
|
|
20 |
21 |
/* |
/* |
21 |
22 |
* Parse file and store data in @db database |
* Parse file and store data in @db database |
|
... |
... |
int ip2clue_parse_file(struct ip2clue_db *db, const char *file_name, |
51 |
52 |
db->format = IP2CLUE_FORMAT_IP2LOCATION; |
db->format = IP2CLUE_FORMAT_IP2LOCATION; |
52 |
53 |
err = ip2clue_parse_ip2location(db); |
err = ip2clue_parse_ip2location(db); |
53 |
54 |
} else { |
} else { |
54 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
55 |
|
"invalid file format [%s]", format); |
|
|
55 |
|
ip2clue_set_error("invalid file format [%s]", format); |
56 |
56 |
return -1; |
return -1; |
57 |
57 |
} |
} |
58 |
58 |
|
|
|
... |
... |
void ip2clue_list_destroy(struct ip2clue_list *list) |
103 |
103 |
* Output statistics about the databases |
* Output statistics about the databases |
104 |
104 |
* TODO: Add number of lookups (successful or not) and number of reloads. |
* TODO: Add number of lookups (successful or not) and number of reloads. |
105 |
105 |
*/ |
*/ |
106 |
|
void ip2clue_list_stats(char *out, const size_t out_size, struct ip2clue_list *list) |
|
|
106 |
|
void ip2clue_list_stats(char *out, const size_t out_size, |
|
107 |
|
struct ip2clue_list *list) |
107 |
108 |
{ |
{ |
108 |
109 |
size_t rest, line_size; |
size_t rest, line_size; |
109 |
110 |
char line[512]; |
char line[512]; |
|
... |
... |
void ip2clue_list_stats(char *out, const size_t out_size, struct ip2clue_list *l |
126 |
127 |
db = list->entries[i]; |
db = list->entries[i]; |
127 |
128 |
line_size = snprintf(line, sizeof(line), |
line_size = snprintf(line, sizeof(line), |
128 |
129 |
"\n" |
"\n" |
129 |
|
"db %u: format [%s], %s, entries=%llu" |
|
|
130 |
|
"db %u: format [%s], %s, entries=%d" |
130 |
131 |
", build_ts=%ld, load_ts=%ld, load=%ums" |
", build_ts=%ld, load_ts=%ld, load=%ums" |
131 |
132 |
", file=[%s], mem=%lluB" |
", file=[%s], mem=%lluB" |
132 |
133 |
" ok/notfound/malformed=%llu/%llu/%llu", |
" ok/notfound/malformed=%llu/%llu/%llu", |
|
... |
... |
void ip2clue_list_stats(char *out, const size_t out_size, struct ip2clue_list *l |
147 |
148 |
/* |
/* |
148 |
149 |
* Allocates a list structure |
* Allocates a list structure |
149 |
150 |
*/ |
*/ |
150 |
|
int ip2clue_list_alloc(struct ip2clue_list *a, const unsigned int number) |
|
|
151 |
|
static int ip2clue_list_alloc(struct ip2clue_list *a, const unsigned int number) |
151 |
152 |
{ |
{ |
152 |
153 |
unsigned int mem; |
unsigned int mem; |
153 |
154 |
|
|
154 |
155 |
mem = number * sizeof(void *); |
mem = number * sizeof(void *); |
155 |
156 |
a->entries = (struct ip2clue_db **) malloc(mem); |
a->entries = (struct ip2clue_db **) malloc(mem); |
156 |
157 |
if (a->entries == NULL) { |
if (a->entries == NULL) { |
157 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
158 |
|
"cannot alloc %u bytes", mem); |
|
|
158 |
|
ip2clue_set_error("cannot alloc %u bytes", mem); |
159 |
159 |
return -1; |
return -1; |
160 |
160 |
} |
} |
161 |
161 |
|
|
|
... |
... |
int ip2clue_list_alloc(struct ip2clue_list *a, const unsigned int number) |
169 |
169 |
/* |
/* |
170 |
170 |
* Clone a list |
* Clone a list |
171 |
171 |
*/ |
*/ |
172 |
|
int ip2clue_list_clone(struct ip2clue_list *dst, struct ip2clue_list *src) |
|
|
172 |
|
int ip2clue_list_clone(struct ip2clue_list *dst, |
|
173 |
|
struct ip2clue_list *src) |
173 |
174 |
{ |
{ |
174 |
175 |
unsigned int i; |
unsigned int i; |
175 |
176 |
|
|
|
... |
... |
int ip2clue_list_clone(struct ip2clue_list *dst, struct ip2clue_list *src) |
189 |
190 |
/* |
/* |
190 |
191 |
* Replaces a list with other |
* Replaces a list with other |
191 |
192 |
*/ |
*/ |
192 |
|
int ip2clue_list_replace(struct ip2clue_list *dst, struct ip2clue_list *src) |
|
|
193 |
|
int ip2clue_list_replace(struct ip2clue_list *dst, |
|
194 |
|
struct ip2clue_list *src) |
193 |
195 |
{ |
{ |
194 |
196 |
int ret; |
int ret; |
195 |
197 |
|
|
|
... |
... |
static int ip2clue_list_load_one(struct ip2clue_db *db, const char *dir, |
215 |
217 |
|
|
216 |
218 |
file = strchr(option, ':'); |
file = strchr(option, ':'); |
217 |
219 |
if (file == NULL) { |
if (file == NULL) { |
218 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
219 |
|
"invalid format [%s]", option); |
|
|
220 |
|
ip2clue_set_error("invalid format [%s]", option); |
220 |
221 |
return -1; |
return -1; |
221 |
222 |
} |
} |
222 |
223 |
file++; |
file++; |
|
... |
... |
static struct ip2clue_db *ip2clue_db_search(const struct ip2clue_list *l, |
259 |
260 |
* Refreshes a db list if files changed |
* Refreshes a db list if files changed |
260 |
261 |
* @options can be something like 'maxmind:file1, ip2location:file2'. |
* @options can be something like 'maxmind:file1, ip2location:file2'. |
261 |
262 |
*/ |
*/ |
262 |
|
int ip2clue_list_refresh(struct ip2clue_list *dst, struct ip2clue_list *src, |
|
263 |
|
const char *dir, const char *options) |
|
|
263 |
|
int ip2clue_list_refresh(struct ip2clue_list *dst, |
|
264 |
|
struct ip2clue_list *src, const char *dir, |
|
265 |
|
const char *options) |
264 |
266 |
{ |
{ |
265 |
267 |
struct ip2clue_db *db; |
struct ip2clue_db *db; |
266 |
268 |
struct ip2clue_split s; |
struct ip2clue_split s; |
|
... |
... |
int ip2clue_list_refresh(struct ip2clue_list *dst, struct ip2clue_list *src, |
283 |
285 |
/* find the last change of the file */ |
/* find the last change of the file */ |
284 |
286 |
file = strchr(s.fields[i], ':'); |
file = strchr(s.fields[i], ':'); |
285 |
287 |
if (file == NULL) { |
if (file == NULL) { |
286 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
287 |
|
"invalid option [%s] (no ':')", s.fields[i]); |
|
|
288 |
|
ip2clue_set_error("invalid option [%s] (no ':')", |
|
289 |
|
s.fields[i]); |
288 |
290 |
goto out_free_dst; /* Invalid entry */ |
goto out_free_dst; /* Invalid entry */ |
289 |
291 |
} |
} |
290 |
292 |
file++; |
file++; |
|
... |
... |
int ip2clue_list_refresh(struct ip2clue_list *dst, struct ip2clue_list *src, |
310 |
312 |
mem = sizeof(struct ip2clue_db); |
mem = sizeof(struct ip2clue_db); |
311 |
313 |
db = (struct ip2clue_db *) malloc(mem); |
db = (struct ip2clue_db *) malloc(mem); |
312 |
314 |
if (db == NULL) { |
if (db == NULL) { |
313 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
314 |
|
"cannot alloc %u bytes for db", mem); |
|
|
315 |
|
ip2clue_set_error("cannot alloc %u bytes for db", |
|
316 |
|
mem); |
315 |
317 |
goto out_free_dst; |
goto out_free_dst; |
316 |
318 |
} |
} |
317 |
319 |
|
|
File parser.h changed (mode: 100644) (index bb3bd11..af2e94d) |
1 |
1 |
#ifndef IP2CLUE_PARSER_H |
#ifndef IP2CLUE_PARSER_H |
2 |
2 |
#define IP2CLUE_PARSER_H 1 |
#define IP2CLUE_PARSER_H 1 |
3 |
3 |
|
|
4 |
|
#include <i_config.h> |
|
|
4 |
|
#include "i_config.h" |
5 |
5 |
|
|
6 |
|
#include <i_types.h> |
|
|
6 |
|
#include "i_types.h" |
7 |
7 |
|
|
8 |
|
extern int ip2clue_parse_file(struct ip2clue_db *db, |
|
|
8 |
|
int ip2clue_parse_file(struct ip2clue_db *db, |
9 |
9 |
const char *file_name, |
const char *file_name, |
10 |
10 |
const char *format); |
const char *format); |
11 |
11 |
|
|
12 |
|
extern void ip2clue_list_init(struct ip2clue_list *list); |
|
13 |
|
extern void ip2clue_list_destroy(struct ip2clue_list *db); |
|
|
12 |
|
void ip2clue_list_init(struct ip2clue_list *list); |
|
13 |
|
void ip2clue_list_destroy(struct ip2clue_list *db); |
14 |
14 |
|
|
15 |
|
extern void ip2clue_list_stats(char *out, |
|
|
15 |
|
void ip2clue_list_stats(char *out, |
16 |
16 |
const size_t out_size, struct ip2clue_list *list); |
const size_t out_size, struct ip2clue_list *list); |
17 |
17 |
|
|
18 |
|
extern int ip2clue_list_clone(struct ip2clue_list *dst, |
|
|
18 |
|
int ip2clue_list_clone(struct ip2clue_list *dst, |
19 |
19 |
struct ip2clue_list *src); |
struct ip2clue_list *src); |
20 |
20 |
|
|
21 |
|
extern int ip2clue_list_replace(struct ip2clue_list *dst, |
|
|
21 |
|
int ip2clue_list_replace(struct ip2clue_list *dst, |
22 |
22 |
struct ip2clue_list *src); |
struct ip2clue_list *src); |
23 |
23 |
|
|
24 |
|
extern int ip2clue_list_refresh(struct ip2clue_list *dst, |
|
|
24 |
|
int ip2clue_list_refresh(struct ip2clue_list *dst, |
25 |
25 |
struct ip2clue_list *src, const char *dir, |
struct ip2clue_list *src, const char *dir, |
26 |
26 |
const char *options); |
const char *options); |
27 |
27 |
|
|
28 |
|
extern int ip2clue_list_load(struct ip2clue_list *list, |
|
|
28 |
|
int ip2clue_list_load(struct ip2clue_list *list, |
29 |
29 |
const char *dir, const char *options); |
const char *dir, const char *options); |
30 |
30 |
|
|
31 |
31 |
#endif |
#endif |
File parser_ip2location.c changed (mode: 100644) (index fbf48f0..d4620ff) |
4 |
4 |
* ip2location stuff |
* ip2location stuff |
5 |
5 |
*/ |
*/ |
6 |
6 |
|
|
7 |
|
#include <i_config.h> |
|
|
7 |
|
#include "i_config.h" |
8 |
8 |
|
|
9 |
9 |
#include <errno.h> |
#include <errno.h> |
10 |
10 |
#include <time.h> |
#include <time.h> |
|
14 |
14 |
#include <error.h> |
#include <error.h> |
15 |
15 |
#include <string.h> |
#include <string.h> |
16 |
16 |
|
|
17 |
|
#include <i_types.h> |
|
18 |
|
#include <i_util.h> |
|
19 |
|
#include <parser_ip2location.h> |
|
|
17 |
|
#include "i_types.h" |
|
18 |
|
#include "i_util.h" |
|
19 |
|
#include "parser_ip2location.h" |
20 |
20 |
|
|
21 |
21 |
enum ip2clue_ip2location_item |
enum ip2clue_ip2location_item |
22 |
22 |
{ |
{ |
|
... |
... |
static int xread8(unsigned char *ret, const int fd, off_t off) |
74 |
74 |
|
|
75 |
75 |
x = lseek(fd, off, SEEK_SET); |
x = lseek(fd, off, SEEK_SET); |
76 |
76 |
if (x != off) { |
if (x != off) { |
77 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
78 |
|
"cannot search to position %lld (%s)", |
|
79 |
|
(long long) off, strerror(errno)); |
|
|
77 |
|
ip2clue_set_error("cannot search to position %jd (%s)", |
|
78 |
|
off, strerror(errno)); |
80 |
79 |
return -1; |
return -1; |
81 |
80 |
} |
} |
82 |
81 |
|
|
83 |
82 |
n = read(fd, ret, 1); |
n = read(fd, ret, 1); |
84 |
83 |
if (n != 1) { |
if (n != 1) { |
85 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
86 |
|
"cannot read (%s) n=%d at off %lld", |
|
87 |
|
strerror(errno), n, (long long) off); |
|
|
84 |
|
ip2clue_set_error("cannot read (%s) n=%zd at off %jd", |
|
85 |
|
strerror(errno), n, off); |
88 |
86 |
return -1; |
return -1; |
89 |
87 |
} |
} |
90 |
88 |
|
|
91 |
89 |
/* |
/* |
92 |
|
printf("%s: readed value %u at offset %lld!\n", |
|
93 |
|
__FUNCTION__, *ret, (long long) off); |
|
|
90 |
|
printf("%s: readed value %u at offset %jd!\n", |
|
91 |
|
__FUNCTION__, *ret, off); |
94 |
92 |
*/ |
*/ |
95 |
93 |
|
|
96 |
94 |
return 0; |
return 0; |
|
... |
... |
static char *xread_str(char *out, const size_t out_len, const int fd, |
198 |
196 |
|
|
199 |
197 |
n = read(fd, out, len); |
n = read(fd, out, len); |
200 |
198 |
if (n != len) { |
if (n != len) { |
201 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
202 |
|
"cannot read at offset %lld, len=%d, n=%d (%s)", |
|
203 |
|
(long long) off, len, n, strerror(errno)); |
|
|
199 |
|
ip2clue_set_error("cannot read at offset %jd, len=%d, n=%d (%s)", |
|
200 |
|
off, len, n, strerror(errno)); |
204 |
201 |
return NULL; |
return NULL; |
205 |
202 |
} |
} |
206 |
203 |
out[n] = '\0'; |
out[n] = '\0'; |
207 |
204 |
|
|
208 |
205 |
/* |
/* |
209 |
|
printf("String at offset %lld, with len=%d is [%s]!\n", |
|
210 |
|
(long long) off, len, out); |
|
|
206 |
|
printf("String at offset %jd, with len=%d is [%s]!\n", |
|
207 |
|
off, len, out); |
211 |
208 |
*/ |
*/ |
212 |
209 |
|
|
213 |
210 |
return 0; |
return 0; |
|
... |
... |
static char *xread_str(char *out, const size_t out_len, const int fd, |
216 |
213 |
/* |
/* |
217 |
214 |
* Substract one from IPv6 address |
* Substract one from IPv6 address |
218 |
215 |
*/ |
*/ |
219 |
|
void substract(unsigned int *a) |
|
|
216 |
|
static void substract(unsigned int *a) |
220 |
217 |
{ |
{ |
221 |
218 |
int i; |
int i; |
222 |
219 |
|
|
|
... |
... |
int ip2clue_parse_ip2location(struct ip2clue_db *db) |
253 |
250 |
|
|
254 |
251 |
fd = open(db->file, O_RDONLY); |
fd = open(db->file, O_RDONLY); |
255 |
252 |
if (fd == -1) { |
if (fd == -1) { |
256 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
257 |
|
"cannot open [%s] (%s)", |
|
|
253 |
|
ip2clue_set_error("cannot open [%s] (%s)", |
258 |
254 |
db->file, strerror(errno)); |
db->file, strerror(errno)); |
259 |
255 |
return -1; |
return -1; |
260 |
256 |
} |
} |
|
... |
... |
int ip2clue_parse_ip2location(struct ip2clue_db *db) |
489 |
485 |
break; |
break; |
490 |
486 |
|
|
491 |
487 |
default: |
default: |
492 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
493 |
|
"unknown item type (%u)", i); |
|
|
488 |
|
ip2clue_set_error("unknown item type (%u)", i); |
494 |
489 |
goto out_parse_error; |
goto out_parse_error; |
495 |
490 |
} |
} |
496 |
491 |
} |
} |
|
... |
... |
int ip2clue_parse_ip2location(struct ip2clue_db *db) |
498 |
493 |
if (x_set == 1) { |
if (x_set == 1) { |
499 |
494 |
e = (struct ip2clue_extra *) malloc(sizeof(struct ip2clue_extra)); |
e = (struct ip2clue_extra *) malloc(sizeof(struct ip2clue_extra)); |
500 |
495 |
if (e == NULL) { |
if (e == NULL) { |
501 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
502 |
|
"cannot alloc memory"); |
|
|
496 |
|
ip2clue_set_error("cannot alloc memory"); |
503 |
497 |
goto out_parse_error; |
goto out_parse_error; |
504 |
498 |
} |
} |
505 |
499 |
memcpy(e, &x, sizeof(struct ip2clue_extra)); |
memcpy(e, &x, sizeof(struct ip2clue_extra)); |
File parser_text.c changed (mode: 100644) (index 89e1c4f..7e96815) |
3 |
3 |
* Description: parser for IP files |
* Description: parser for IP files |
4 |
4 |
*/ |
*/ |
5 |
5 |
|
|
6 |
|
#include <i_config.h> |
|
|
6 |
|
#include "i_config.h" |
7 |
7 |
|
|
8 |
8 |
#include <errno.h> |
#include <errno.h> |
9 |
9 |
#include <string.h> |
#include <string.h> |
10 |
10 |
#include <stdio.h> |
#include <stdio.h> |
11 |
11 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
12 |
12 |
|
|
13 |
|
#include <i_types.h> |
|
14 |
|
#include <i_util.h> |
|
15 |
|
#include <parser_core.h> |
|
16 |
|
#include <parser_text.h> |
|
17 |
|
#include <parser_ip2location.h> |
|
|
13 |
|
#include "i_types.h" |
|
14 |
|
#include "i_util.h" |
|
15 |
|
#include "parser_core.h" |
|
16 |
|
#include "parser_text.h" |
|
17 |
|
#include "parser_ip2location.h" |
18 |
18 |
|
|
19 |
19 |
static int ip2clue_set_fields(struct ip2clue_fields *f, |
static int ip2clue_set_fields(struct ip2clue_fields *f, |
20 |
|
const enum ip2clue_type type) |
|
|
20 |
|
const enum ip2clue_format format) |
21 |
21 |
{ |
{ |
22 |
22 |
/* defaults */ |
/* defaults */ |
23 |
23 |
strcpy(f->comment_chars, ""); |
strcpy(f->comment_chars, ""); |
24 |
24 |
f->v4_or_v6 = IP2CLUE_TYPE_V4; |
f->v4_or_v6 = IP2CLUE_TYPE_V4; |
25 |
25 |
|
|
26 |
|
switch (type) { |
|
|
26 |
|
switch (format) { |
27 |
27 |
case IP2CLUE_FORMAT_WEBHOSTING: |
case IP2CLUE_FORMAT_WEBHOSTING: |
28 |
28 |
f->ip_bin_start = 0; |
f->ip_bin_start = 0; |
29 |
29 |
f->ip_bin_end = 1; |
f->ip_bin_end = 1; |
|
... |
... |
static int ip2clue_add_cell(struct ip2clue_db *db, const struct ip2clue_split *s |
98 |
98 |
cell6->extra = NULL; |
cell6->extra = NULL; |
99 |
99 |
|
|
100 |
100 |
if (inet_pton(AF_INET6, s->fields[f->ip_start], (void *) &addr) != 1) { |
if (inet_pton(AF_INET6, s->fields[f->ip_start], (void *) &addr) != 1) { |
101 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
102 |
|
"malformed address [%s]", |
|
|
101 |
|
ip2clue_set_error("malformed address [%s]", |
103 |
102 |
s->fields[f->ip_start]); |
s->fields[f->ip_start]); |
104 |
103 |
return -1; |
return -1; |
105 |
104 |
} |
} |
|
... |
... |
static int ip2clue_add_cell(struct ip2clue_db *db, const struct ip2clue_split *s |
107 |
106 |
cell6->ip_start[i] = ntohl(addr.s6_addr32[i]); |
cell6->ip_start[i] = ntohl(addr.s6_addr32[i]); |
108 |
107 |
|
|
109 |
108 |
if (inet_pton(AF_INET6, s->fields[f->ip_end], (void *) &addr) != 1) { |
if (inet_pton(AF_INET6, s->fields[f->ip_end], (void *) &addr) != 1) { |
110 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
111 |
|
"malformed address [%s]", |
|
|
109 |
|
ip2clue_set_error("malformed address [%s]", |
112 |
110 |
s->fields[f->ip_end]); |
s->fields[f->ip_end]); |
113 |
111 |
return -1; |
return -1; |
114 |
112 |
} |
} |
|
... |
... |
static int ip2clue_add_cell(struct ip2clue_db *db, const struct ip2clue_split *s |
122 |
120 |
else |
else |
123 |
121 |
strcpy(cell6->country_short, "ZZ"); |
strcpy(cell6->country_short, "ZZ"); |
124 |
122 |
} else { |
} else { |
125 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
126 |
|
"address is nor ipv4 neither ipv6 [%u]", db->v4_or_v6); |
|
|
123 |
|
ip2clue_set_error("address is nor ipv4 neither ipv6 [%u]", |
|
124 |
|
db->v4_or_v6); |
127 |
125 |
return -1; |
return -1; |
128 |
126 |
} |
} |
129 |
127 |
|
|
|
... |
... |
int ip2clue_parse_text(struct ip2clue_db *db) |
139 |
137 |
FILE *f; |
FILE *f; |
140 |
138 |
char line[1024], *r; |
char line[1024], *r; |
141 |
139 |
struct ip2clue_split s; |
struct ip2clue_split s; |
142 |
|
unsigned long line_no, final_lines; |
|
|
140 |
|
int line_no, final_lines; |
143 |
141 |
struct ip2clue_fields fields; |
struct ip2clue_fields fields; |
144 |
142 |
unsigned int pos; |
unsigned int pos; |
145 |
143 |
|
|
|
... |
... |
int ip2clue_parse_text(struct ip2clue_db *db) |
163 |
161 |
return -1; |
return -1; |
164 |
162 |
db->cells = malloc(db->mem); |
db->cells = malloc(db->mem); |
165 |
163 |
if (db->cells == NULL) { |
if (db->cells == NULL) { |
166 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
167 |
|
"cannot alloc %llu bytes", |
|
|
164 |
|
ip2clue_set_error("cannot alloc %llu bytes", |
168 |
165 |
db->mem); |
db->mem); |
169 |
166 |
return -1; |
return -1; |
170 |
167 |
} |
} |
171 |
168 |
|
|
172 |
169 |
f = fopen(db->file, "r"); |
f = fopen(db->file, "r"); |
173 |
170 |
if (f == NULL) { |
if (f == NULL) { |
174 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
175 |
|
"cannot open [%s] (%s)", |
|
|
171 |
|
ip2clue_set_error("cannot open [%s] (%s)", |
176 |
172 |
db->file, strerror(errno)); |
db->file, strerror(errno)); |
177 |
173 |
goto out_free_db_cells; |
goto out_free_db_cells; |
178 |
174 |
} |
} |
|
... |
... |
int ip2clue_parse_text(struct ip2clue_db *db) |
201 |
197 |
goto out_parse_error; |
goto out_parse_error; |
202 |
198 |
|
|
203 |
199 |
if (err != fields.total) { |
if (err != fields.total) { |
204 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
205 |
|
"error splitting file [%s], format [%s]" |
|
206 |
|
", line %lu [%s]:" |
|
207 |
|
" too less fields; found %d needed %d", |
|
|
200 |
|
ip2clue_set_error("error splitting file [%s]" |
|
201 |
|
", format [%s], line %d [%s] too less fields;" |
|
202 |
|
" found %d needed %d", |
208 |
203 |
db->file, ip2clue_format(db->format), |
db->file, ip2clue_format(db->format), |
209 |
204 |
line_no, line, err, fields.total); |
line_no, line, err, fields.total); |
210 |
205 |
goto out_parse_error; |
goto out_parse_error; |
211 |
206 |
} |
} |
212 |
207 |
|
|
213 |
208 |
if (final_lines == db->no_of_cells) { |
if (final_lines == db->no_of_cells) { |
214 |
|
snprintf(ip2clue_error, sizeof(ip2clue_error), |
|
215 |
|
"seems that the file changed behind our back"); |
|
|
209 |
|
ip2clue_set_error("seems that the file changed behind" |
|
210 |
|
" our back"); |
216 |
211 |
goto out_parse_error; |
goto out_parse_error; |
217 |
212 |
} |
} |
218 |
213 |
|
|