2
* Copyright (C) 2003 Luca Deri <deri@ntop.org>
3
* Andreas Pfaller <apfaller@yahoo.com.au>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39
char *OptTableFilename="./p2c.opt.table";
41
/* ************************************************************************* */
43
void addNodeInternal(u_int32_t ip, int prefix, char *country)
49
for (i=0; i<prefix; i++) {
52
if (!(p2=malloc(sizeof(IPNode))))
54
memset(p2, 0, sizeof(IPNode));
63
strncpy(p2->cc, country, sizeof(p2->cc));
64
p2->cc[sizeof(p2->cc)-1]=0;
68
/* ************************************************************************* */
70
void initIPCountryTable(void)
74
if ((Head=malloc(sizeof(IPNode)))==NULL)
76
strcpy(Head->cc, "***");
80
fd = fopen(OptTableFilename, "r");
82
perror(OptTableFilename);
88
char *strtokState, *cc, *ip, *prefix;
90
if (fgets(buff, sizeof(buff), fd)==NULL)
92
if ((cc=strtok_r(buff, ":", &strtokState))==NULL)
94
if ((ip=strtok_r(NULL, "/", &strtokState))==NULL)
96
if ((prefix=strtok_r(NULL, "\n", &strtokState))==NULL)
99
addNodeInternal(xaton(ip), atoi(prefix), cc);
104
/* ************************************************************************* */
106
char *ip2CountryCode(u_int32_t ip)
117
printf("%s/%d ", cc, i);
119
b=(ip>>(31-i)) & 0x1;
126
/* ************************************************************************* */
130
fprintf(stderr, "lookuptest %s\n", VERSION);
131
fprintf(stderr, "Usage: lookuptest [OPTION]... [IP]...\n");
132
fprintf(stderr, "Lookup country codes for specified IPs.\n");
133
fprintf(stderr, "If no IPs are specified on the commandline a list of IPs is read from stdin.\n\n");
134
fprintf(stderr, " -t fname Read mapping table from fname\n\n");
135
fprintf(stderr, "Example: ./lookuptest 19.203.239.24, returns:\n\n");
136
fprintf(stderr, "19.203.239.24: ***/0 US/6 IL/29\n\n");
137
fprintf(stderr, " Which means the address, 19.203.239.24 is contained in the root block ***/0 (always).\n");
138
fprintf(stderr, " More specifically it is contained in a /6 listed as in the US and\n");
139
fprintf(stderr, " most specifically it is in a /29 which is listed as in IL (Israel).\n\n");
143
/* ************************************************************************* */
145
void parseOptions(int argc, char *argv[])
149
while ((c=getopt(argc, argv, "?ht:"))!=-1) {
152
OptTableFilename=optarg;
163
/* ************************************************************************* */
165
int main(int argc, char *argv[])
169
parseOptions(argc, argv);
171
initIPCountryTable();
174
while (!feof(stdin)) {
177
fgets(buff, sizeof(buff)-1, stdin);
179
ip2CountryCode(xaton(buff));
185
for (i=optind; i<argc; i++) {
186
printf("%s: ", argv[i]);
187
ip2CountryCode(xaton(argv[i]));