File: geoiplookup6.c

package info (click to toggle)
geoip 1.4.8+dfsg-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,272 kB
  • sloc: ansic: 19,471; sh: 10,137; cpp: 526; perl: 177; makefile: 133; awk: 13
file content (255 lines) | stat: -rw-r--r-- 7,511 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* geoiplookup.c
 *
 * Copyright (C) 2006 MaxMind LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <GeoIP.h>
#include <GeoIPCity.h>
#include <GeoIP_internal.h>
void geoiplookup(GeoIP* gi,char *hostname,int i);

void usage() {
	fprintf(stderr,"Usage: geoiplookup [-d custom_dir] [-f custom_file] [-v] <ipaddress|hostname>\n");
}

int main (int argc, char *argv[]) {
	char * hostname = NULL;
	char * db_info;
	GeoIP * gi;
	int i;
	char *custom_directory = NULL;
	char *custom_file = NULL;
	int version_flag = 0;

	if (argc < 2) {
		usage();
		exit(1);
	} 
	i = 1;
	while (i < argc) {
		if (strcmp(argv[i],"-v") == 0) {
			version_flag = 1;
		} else if (strcmp(argv[i],"-f") == 0) {
			if ((i+1) < argc){
				i++;
				custom_file = argv[i];
			}
		} else if (strcmp(argv[i],"-d") == 0) {
			if ((i+1) < argc){
				i++;
				custom_directory = argv[i];
			}
		} else {
			hostname = argv[i];
		}
		i++;
	}
	if (hostname == NULL) {
		usage();
		exit(1);
	}

	if (custom_directory != NULL) {
		GeoIP_setup_custom_directory(custom_directory);
	}
	_GeoIP_setup_dbfilename();

	if (custom_file != NULL) {
		gi = GeoIP_open(custom_file, GEOIP_STANDARD);
		if (NULL == gi) {
			printf("%s not available, skipping...\n", custom_file);
		} else {
		  i = GeoIP_database_edition(gi);
			if (version_flag == 1) {
				db_info = GeoIP_database_info(gi);
				printf("%s: %s\n",GeoIPDBDescription[i],db_info == NULL ? "": db_info );
				free(db_info);
			} else {
				geoiplookup(gi,hostname,i);
			}
		}
		GeoIP_delete(gi);
	} else {
		/* iterate through different database types */
		for (i = 0; i < NUM_DB_TYPES; ++i) {
			if (GeoIP_db_avail(i)) {
				gi = GeoIP_open_type(i, GEOIP_STANDARD);
				if (NULL == gi) {
					printf("%s not available, skipping...\n", GeoIPDBDescription[i]);
				} else {
					if (version_flag == 1) {
						db_info = GeoIP_database_info(gi);
						printf("%s: %s\n",GeoIPDBDescription[i],db_info);
						free(db_info);
					} else {
						geoiplookup(gi,hostname,i);
					}
				}
				GeoIP_delete(gi);
			}
		}
	}
	return 0;
}

static const char * _mk_NA( const char * p ){
 return p ? p : "N/A";
}

void
geoiplookup(GeoIP * gi, char *hostname, int i)
{
	const char     *country_code;
	const char     *country_name;
	const char     *domain_name;
        const char     *asnum_name;        
	int             netspeed;
	int             country_id;
	GeoIPRegion    *region;
	GeoIPRecord    *gir;
	const char     *org;

	geoipv6_t       ipnum;
	ipnum = _GeoIP_lookupaddress_v6(hostname);
	if (__GEOIP_V6_IS_NULL(ipnum)) {
		printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname);

	}
	else {


#if 0
		if (GEOIP_DOMAIN_EDITION_V6 == i) {
			domain_name = GeoIP_name_by_name_v6(gi, hostname);
			if (domain_name == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], domain_name);
			}
		}
#endif



		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 ) {
			asnum_name = GeoIP_name_by_ipnum_v6(gi, ipnum);
			if (asnum_name == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], asnum_name);
                              //  _say_range_by_ip(gi, ipnum);
			}
		}
	
        	else if (GEOIP_CITY_EDITION_REV0_V6 == i) {
		        gir = GeoIP_record_by_name_v6(gi, hostname);
      		        if (NULL == gir) {
			        printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		        }
		        else {
			        printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region),
				       _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude);
		        }
	        }
	        else if (GEOIP_CITY_EDITION_REV1_V6 == i) {
		        gir = GeoIP_record_by_name_v6(gi, hostname);
		        if (NULL == gir) {
			        printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		        }
		        else {
		                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),
				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
                        }
	        }
	
		else if (GEOIP_COUNTRY_EDITION_V6 == i) {

			country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
			country_code = GeoIP_country_code[country_id];
			country_name = GeoIP_country_name[country_id];
			if (country_id == 0) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name);
			}
		}
	}

#if 0

	else
	if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) {
		region = GeoIP_region_by_name_v6(gi, hostname);
		if (NULL == region) {
			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		}
		else {
			printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region);
		}
	}
	else if (GEOIP_CITY_EDITION_REV0 == i) {
		gir = GeoIP_record_by_name(gi, hostname);
		if (NULL == gir) {
			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		}
		else {
			printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, gir->region,
			       gir->city, gir->postal_code, gir->latitude, gir->longitude);
		}
	}
	else if (GEOIP_CITY_EDITION_REV1 == i) {
		gir = GeoIP_record_by_name(gi, hostname);
		if (NULL == gir) {
			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		}
		else {
			printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, gir->region, gir->city, gir->postal_code,
			       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
		}
	}
	else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) {
		org = GeoIP_org_by_name_v6(gi, hostname);
		if (org == NULL) {
			printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		}
		else {
			printf("%s: %s\n", GeoIPDBDescription[i], org);
		}
	}
	else if (GEOIP_NETSPEED_EDITION == i) {
		netspeed = GeoIP_id_by_name_v6(gi, hostname);
		if (netspeed == GEOIP_UNKNOWN_SPEED) {
			printf("%s: Unknown\n", GeoIPDBDescription[i]);
		}
		else if (netspeed == GEOIP_DIALUP_SPEED) {
			printf("%s: Dialup\n", GeoIPDBDescription[i]);
		}
		else if (netspeed == GEOIP_CABLEDSL_SPEED) {
			printf("%s: Cable/DSL\n", GeoIPDBDescription[i]);
		}
		else if (netspeed == GEOIP_CORPORATE_SPEED) {
			printf("%s: Corporate\n", GeoIPDBDescription[i]);
		}

	}
#endif

}