1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2/* geoiplookup.c
3 *
4 * Copyright (C) 2006 MaxMind LLC
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "GeoIP.h"
22#include "GeoIPCity.h"
23#include "GeoIP_internal.h"
24void geoiplookup(GeoIP* gi,char *hostname,int i);
25
26void usage() {
27	fprintf(stderr,"Usage: geoiplookup [-h] [-d custom_dir] [-f custom_file] [-v] <ipaddress|hostname>\n");
28}
29
30int main (int argc, char *argv[]) {
31	char * hostname = NULL;
32	char * db_info;
33	GeoIP * gi;
34	int i;
35	char *custom_directory = NULL;
36	char *custom_file = NULL;
37	int version_flag = 0;
38
39	if (argc < 2) {
40		usage();
41		exit(1);
42	}
43	i = 1;
44	while (i < argc) {
45		if (strcmp(argv[i],"-v") == 0) {
46			version_flag = 1;
47		} else if (strcmp(argv[i], "-h") == 0
48				|| strcmp(argv[i], "-?") == 0) {
49			usage();
50			exit(0);
51		} else if (strcmp(argv[i],"-f") == 0) {
52			if ((i+1) < argc){
53				i++;
54				custom_file = argv[i];
55			}
56		} else if (strcmp(argv[i],"-d") == 0) {
57			if ((i+1) < argc){
58				i++;
59				custom_directory = argv[i];
60			}
61		} else {
62			hostname = argv[i];
63		}
64		i++;
65	}
66	if (hostname == NULL) {
67		usage();
68		exit(1);
69	}
70
71	if (custom_directory != NULL) {
72		GeoIP_setup_custom_directory(custom_directory);
73	}
74	_GeoIP_setup_dbfilename();
75
76	if (custom_file != NULL) {
77		gi = GeoIP_open(custom_file, GEOIP_STANDARD);
78		if (NULL == gi) {
79			printf("%s not available, skipping...\n", custom_file);
80		} else {
81		  i = GeoIP_database_edition(gi);
82			if (version_flag == 1) {
83				db_info = GeoIP_database_info(gi);
84				printf("%s: %s\n",GeoIPDBDescription[i],db_info == NULL ? "": db_info );
85				free(db_info);
86			} else {
87				geoiplookup(gi,hostname,i);
88			}
89		}
90		GeoIP_delete(gi);
91	} else {
92		/* iterate through different database types */
93		for (i = 0; i < NUM_DB_TYPES; ++i) {
94			if (GeoIP_db_avail(i)) {
95				gi = GeoIP_open_type(i, GEOIP_STANDARD);
96				if (NULL == gi) {
97                                        /* Ignore these errors. It's possible
98                                         * to use the same database name for
99                                         * different databases.
100                                         */
101                                        ;
102				} else {
103					if (version_flag == 1) {
104						db_info = GeoIP_database_info(gi);
105						printf("%s: %s\n",GeoIPDBDescription[i],db_info);
106						free(db_info);
107					} else {
108						geoiplookup(gi,hostname,i);
109					}
110				}
111				GeoIP_delete(gi);
112			}
113		}
114	}
115	return 0;
116}
117
118static const char * _mk_NA( const char * p ){
119 return p ? p : "N/A";
120}
121
122void
123geoiplookup(GeoIP * gi, char *hostname, int i)
124{
125	const char     *country_code;
126	const char     *country_name;
127        const char     *asnum_name;
128	int             country_id;
129	GeoIPRecord    *gir;
130
131	geoipv6_t       ipnum;
132	ipnum = _GeoIP_lookupaddress_v6(hostname);
133	if (__GEOIP_V6_IS_NULL(ipnum)) {
134		printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname);
135
136	}
137	else {
138
139
140#if 0
141		if (GEOIP_DOMAIN_EDITION_V6 == i) {
142			domain_name = GeoIP_name_by_name_v6(gi, hostname);
143			if (domain_name == NULL) {
144				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
145			}
146			else {
147				printf("%s: %s\n", GeoIPDBDescription[i], domain_name);
148			}
149		}
150#endif
151
152
153
154		if (GEOIP_LOCATIONA_EDITION_V6 == i || GEOIP_ASNUM_EDITION_V6 == i || GEOIP_USERTYPE_EDITION_V6 == i || GEOIP_REGISTRAR_EDITION_V6 == i || GEOIP_DOMAIN_EDITION_V6 == i || GEOIP_ORG_EDITION_V6 == i || GEOIP_ISP_EDITION_V6 == i || GEOIP_NETSPEED_EDITION_REV1_V6 == i ) {
155			asnum_name = GeoIP_name_by_ipnum_v6(gi, ipnum);
156			if (asnum_name == NULL) {
157				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
158			}
159			else {
160				printf("%s: %s\n", GeoIPDBDescription[i], asnum_name);
161                              //  _say_range_by_ip(gi, ipnum);
162			}
163		}
164
165        	else if (GEOIP_CITY_EDITION_REV0_V6 == i) {
166		        gir = GeoIP_record_by_ipnum_v6(gi, ipnum);
167      		        if (NULL == gir) {
168			        printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
169		        }
170		        else {
171			        printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region),
172				       _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude);
173		        }
174	        }
175	        else if (GEOIP_CITY_EDITION_REV1_V6 == i) {
176		        gir = GeoIP_record_by_ipnum_v6(gi, ipnum);
177		        if (NULL == gir) {
178			        printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
179		        }
180		        else {
181		                printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code),
182				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
183                        }
184	        }
185
186		else if (GEOIP_COUNTRY_EDITION_V6 == i) {
187
188			country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
189			country_code = GeoIP_country_code[country_id];
190			country_name = GeoIP_country_name[country_id];
191			if (country_id == 0) {
192				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
193			}
194			else {
195				printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name);
196			}
197		}
198	}
199
200#if 0
201
202	else
203	if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) {
204		region = GeoIP_region_by_name_v6(gi, hostname);
205		if (NULL == region) {
206			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
207		}
208		else {
209			printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region);
210			GeoIPRegion_delete(region);
211		}
212	}
213	else if (GEOIP_CITY_EDITION_REV0 == i) {
214		gir = GeoIP_record_by_name(gi, hostname);
215		if (NULL == gir) {
216			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
217		}
218		else {
219			printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, gir->region,
220			       gir->city, gir->postal_code, gir->latitude, gir->longitude);
221		}
222	}
223	else if (GEOIP_CITY_EDITION_REV1 == i) {
224		gir = GeoIP_record_by_name(gi, hostname);
225		if (NULL == gir) {
226			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
227		}
228		else {
229			printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, gir->region, gir->city, gir->postal_code,
230			       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
231		}
232	}
233	else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) {
234		org = GeoIP_org_by_name_v6(gi, hostname);
235		if (org == NULL) {
236			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
237		}
238		else {
239			printf("%s: %s\n", GeoIPDBDescription[i], org);
240		}
241	}
242	else if (GEOIP_NETSPEED_EDITION == i) {
243		netspeed = GeoIP_id_by_name_v6(gi, hostname);
244		if (netspeed == GEOIP_UNKNOWN_SPEED) {
245			printf("%s: Unknown\n", GeoIPDBDescription[i]);
246		}
247		else if (netspeed == GEOIP_DIALUP_SPEED) {
248			printf("%s: Dialup\n", GeoIPDBDescription[i]);
249		}
250		else if (netspeed == GEOIP_CABLEDSL_SPEED) {
251			printf("%s: Cable/DSL\n", GeoIPDBDescription[i]);
252		}
253		else if (netspeed == GEOIP_CORPORATE_SPEED) {
254			printf("%s: Corporate\n", GeoIPDBDescription[i]);
255		}
256
257	}
258#endif
259
260}
261