3 * IP-address/hostname to country converter.
5 * Problem; you want to know where IP a.b.c.d is located.
7 * Use ares_gethostbyname ("d.c.b.a.zz.countries.nerd.dk")
8 * and get the CNAME (host->h_name). Result will be:
9 * CNAME = zz<CC>.countries.nerd.dk with address 127.0.x.y (ver 1) or
10 * CNAME = <a.b.c.d>.zz.countries.nerd.dk with address 127.0.x.y (ver 2)
12 * The 2 letter country code is in <CC> and the ISO-3166 country
13 * number is in x.y (number = x*256 + y). Version 2 of the protocol is missing
16 * Ref: http://countries.nerd.dk/more.html
18 * Written by G. Vanem <gvanem@broadpark.no> 2006, 2007
20 * NB! This program may not be big-endian aware.
22 * Permission to use, copy, modify, and distribute this
23 * software and its documentation for any purpose and without
24 * fee is hereby granted, provided that the above copyright
25 * notice appear in all copies and that both that copyright
26 * notice and this permission notice appear in supporting
27 * documentation, and that the name of M.I.T. not be used in
28 * advertising or publicity pertaining to distribution of the
29 * software without specific, written prior permission.
30 * M.I.T. makes no representations about the suitability of
31 * this software for any purpose. It is provided "as is"
32 * without express or implied warranty.
35 #include "ares_setup.h"
41 #if defined(WIN32) && !defined(WATT32)
44 #include <arpa/inet.h>
45 #include <netinet/in.h>
50 #include "ares_getopt.h"
51 #include "ares_nowarn.h"
54 # include "ares_strdup.h"
55 # define strdup(ptr) ares_strdup(ptr)
58 #ifndef HAVE_STRCASECMP
59 # include "ares_strcasecmp.h"
60 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
63 #ifndef HAVE_STRNCASECMP
64 # include "ares_strcasecmp.h"
65 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
69 #define INADDR_NONE 0xffffffff
72 static const char *usage = "acountry [-vh?] {host|addr} ...\n";
73 static const char nerd_fmt[] = "%u.%u.%u.%u.zz.countries.nerd.dk";
74 static const char *nerd_ver1 = nerd_fmt + 14;
75 static const char *nerd_ver2 = nerd_fmt + 11;
76 static int verbose = 0;
78 #define TRACE(fmt) do { \
83 static void wait_ares(ares_channel channel);
84 static void callback(void *arg, int status, int timeouts, struct hostent *host);
85 static void callback2(void *arg, int status, int timeouts, struct hostent *host);
86 static void find_country_from_cname(const char *cname, struct in_addr addr);
88 static void Abort(const char *fmt, ...)
92 vfprintf(stderr, fmt, args);
97 int main(int argc, char **argv)
102 #if defined(WIN32) && !defined(WATT32)
103 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
105 WSAStartup(wVersionRequested, &wsaData);
108 status = ares_library_init(ARES_LIB_INIT_ALL);
109 if (status != ARES_SUCCESS)
111 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
115 while ((ch = ares_getopt(argc, argv, "dvh?")) != -1)
137 status = ares_init(&channel);
138 if (status != ARES_SUCCESS)
140 fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
144 /* Initiate the queries, one per command-line argument. */
145 for ( ; *argv; argv++)
150 /* If this fails, assume '*argv' is a host-name that
151 * must be resolved first
153 if (ares_inet_pton(AF_INET, *argv, &addr) != 1)
155 ares_gethostbyname(channel, *argv, AF_INET, callback2, &addr);
157 if (addr.s_addr == INADDR_NONE)
159 printf("Failed to lookup %s\n", *argv);
164 sprintf(buf, nerd_fmt,
165 (unsigned int)(addr.s_addr >> 24),
166 (unsigned int)((addr.s_addr >> 16) & 255),
167 (unsigned int)((addr.s_addr >> 8) & 255),
168 (unsigned int)(addr.s_addr & 255));
169 TRACE(("Looking up %s...", buf));
171 ares_gethostbyname(channel, buf, AF_INET, callback, buf);
175 ares_destroy(channel);
177 ares_library_cleanup();
179 #if defined(WIN32) && !defined(WATT32)
187 * Wait for the queries to complete.
189 static void wait_ares(ares_channel channel)
193 struct timeval *tvp, tv;
194 fd_set read_fds, write_fds;
199 nfds = ares_fds(channel, &read_fds, &write_fds);
202 tvp = ares_timeout(channel, NULL, &tv);
203 select(nfds, &read_fds, &write_fds, NULL, tvp);
204 ares_process(channel, &read_fds, &write_fds);
209 * This is the callback used when we have the IP-address of interest.
210 * Extract the CNAME and figure out the country-code from it.
212 static void callback(void *arg, int status, int timeouts, struct hostent *host)
214 const char *name = (const char*)arg;
220 if (!host || status != ARES_SUCCESS)
222 printf("Failed to lookup %s: %s\n", name, ares_strerror(status));
226 TRACE(("\nFound address %s, name %s\n",
227 ares_inet_ntop(AF_INET,(const char*)host->h_addr,buf,sizeof(buf)),
230 cname = host->h_name; /* CNAME gets put here */
232 printf("Failed to get CNAME for %s\n", name);
234 find_country_from_cname(cname, *(struct in_addr*)host->h_addr);
238 * This is the callback used to obtain the IP-address of the host of interest.
240 static void callback2(void *arg, int status, int timeouts, struct hostent *host)
242 struct in_addr *addr = (struct in_addr*) arg;
245 if (!host || status != ARES_SUCCESS)
246 memset(addr, INADDR_NONE, sizeof(*addr));
248 memcpy(addr, host->h_addr, sizeof(*addr));
252 int country_number; /* ISO-3166 country number */
253 char short_name[3]; /* A2 short country code */
254 const char *long_name; /* normal country name */
257 static const struct search_list *list_lookup(int number, const struct search_list *list, int num)
259 while (num > 0 && list->long_name)
261 if (list->country_number == number)
270 * Ref: ftp://ftp.ripe.net/iso3166-countrycodes.txt
272 static const struct search_list country_list[] = {
273 { 4, "af", "Afghanistan" },
274 { 248, "ax", "Ã…land Island" },
275 { 8, "al", "Albania" },
276 { 12, "dz", "Algeria" },
277 { 16, "as", "American Samoa" },
278 { 20, "ad", "Andorra" },
279 { 24, "ao", "Angola" },
280 { 660, "ai", "Anguilla" },
281 { 10, "aq", "Antarctica" },
282 { 28, "ag", "Antigua & Barbuda" },
283 { 32, "ar", "Argentina" },
284 { 51, "am", "Armenia" },
285 { 533, "aw", "Aruba" },
286 { 36, "au", "Australia" },
287 { 40, "at", "Austria" },
288 { 31, "az", "Azerbaijan" },
289 { 44, "bs", "Bahamas" },
290 { 48, "bh", "Bahrain" },
291 { 50, "bd", "Bangladesh" },
292 { 52, "bb", "Barbados" },
293 { 112, "by", "Belarus" },
294 { 56, "be", "Belgium" },
295 { 84, "bz", "Belize" },
296 { 204, "bj", "Benin" },
297 { 60, "bm", "Bermuda" },
298 { 64, "bt", "Bhutan" },
299 { 68, "bo", "Bolivia" },
300 { 70, "ba", "Bosnia & Herzegowina" },
301 { 72, "bw", "Botswana" },
302 { 74, "bv", "Bouvet Island" },
303 { 76, "br", "Brazil" },
304 { 86, "io", "British Indian Ocean Territory" },
305 { 96, "bn", "Brunei Darussalam" },
306 { 100, "bg", "Bulgaria" },
307 { 854, "bf", "Burkina Faso" },
308 { 108, "bi", "Burundi" },
309 { 116, "kh", "Cambodia" },
310 { 120, "cm", "Cameroon" },
311 { 124, "ca", "Canada" },
312 { 132, "cv", "Cape Verde" },
313 { 136, "ky", "Cayman Islands" },
314 { 140, "cf", "Central African Republic" },
315 { 148, "td", "Chad" },
316 { 152, "cl", "Chile" },
317 { 156, "cn", "China" },
318 { 162, "cx", "Christmas Island" },
319 { 166, "cc", "Cocos Islands" },
320 { 170, "co", "Colombia" },
321 { 174, "km", "Comoros" },
322 { 178, "cg", "Congo" },
323 { 180, "cd", "Congo" },
324 { 184, "ck", "Cook Islands" },
325 { 188, "cr", "Costa Rica" },
326 { 384, "ci", "Cote d'Ivoire" },
327 { 191, "hr", "Croatia" },
328 { 192, "cu", "Cuba" },
329 { 196, "cy", "Cyprus" },
330 { 203, "cz", "Czech Republic" },
331 { 208, "dk", "Denmark" },
332 { 262, "dj", "Djibouti" },
333 { 212, "dm", "Dominica" },
334 { 214, "do", "Dominican Republic" },
335 { 218, "ec", "Ecuador" },
336 { 818, "eg", "Egypt" },
337 { 222, "sv", "El Salvador" },
338 { 226, "gq", "Equatorial Guinea" },
339 { 232, "er", "Eritrea" },
340 { 233, "ee", "Estonia" },
341 { 231, "et", "Ethiopia" },
342 { 238, "fk", "Falkland Islands" },
343 { 234, "fo", "Faroe Islands" },
344 { 242, "fj", "Fiji" },
345 { 246, "fi", "Finland" },
346 { 250, "fr", "France" },
347 { 249, "fx", "France, Metropolitan" },
348 { 254, "gf", "French Guiana" },
349 { 258, "pf", "French Polynesia" },
350 { 260, "tf", "French Southern Territories" },
351 { 266, "ga", "Gabon" },
352 { 270, "gm", "Gambia" },
353 { 268, "ge", "Georgia" },
354 { 276, "de", "Germany" },
355 { 288, "gh", "Ghana" },
356 { 292, "gi", "Gibraltar" },
357 { 300, "gr", "Greece" },
358 { 304, "gl", "Greenland" },
359 { 308, "gd", "Grenada" },
360 { 312, "gp", "Guadeloupe" },
361 { 316, "gu", "Guam" },
362 { 320, "gt", "Guatemala" },
363 { 324, "gn", "Guinea" },
364 { 624, "gw", "Guinea-Bissau" },
365 { 328, "gy", "Guyana" },
366 { 332, "ht", "Haiti" },
367 { 334, "hm", "Heard & Mc Donald Islands" },
368 { 336, "va", "Vatican City" },
369 { 340, "hn", "Honduras" },
370 { 344, "hk", "Hong kong" },
371 { 348, "hu", "Hungary" },
372 { 352, "is", "Iceland" },
373 { 356, "in", "India" },
374 { 360, "id", "Indonesia" },
375 { 364, "ir", "Iran" },
376 { 368, "iq", "Iraq" },
377 { 372, "ie", "Ireland" },
378 { 376, "il", "Israel" },
379 { 380, "it", "Italy" },
380 { 388, "jm", "Jamaica" },
381 { 392, "jp", "Japan" },
382 { 400, "jo", "Jordan" },
383 { 398, "kz", "Kazakhstan" },
384 { 404, "ke", "Kenya" },
385 { 296, "ki", "Kiribati" },
386 { 408, "kp", "Korea (north)" },
387 { 410, "kr", "Korea (south)" },
388 { 414, "kw", "Kuwait" },
389 { 417, "kg", "Kyrgyzstan" },
390 { 418, "la", "Laos" },
391 { 428, "lv", "Latvia" },
392 { 422, "lb", "Lebanon" },
393 { 426, "ls", "Lesotho" },
394 { 430, "lr", "Liberia" },
395 { 434, "ly", "Libya" },
396 { 438, "li", "Liechtenstein" },
397 { 440, "lt", "Lithuania" },
398 { 442, "lu", "Luxembourg" },
399 { 446, "mo", "Macao" },
400 { 807, "mk", "Macedonia" },
401 { 450, "mg", "Madagascar" },
402 { 454, "mw", "Malawi" },
403 { 458, "my", "Malaysia" },
404 { 462, "mv", "Maldives" },
405 { 466, "ml", "Mali" },
406 { 470, "mt", "Malta" },
407 { 584, "mh", "Marshall Islands" },
408 { 474, "mq", "Martinique" },
409 { 478, "mr", "Mauritania" },
410 { 480, "mu", "Mauritius" },
411 { 175, "yt", "Mayotte" },
412 { 484, "mx", "Mexico" },
413 { 583, "fm", "Micronesia" },
414 { 498, "md", "Moldova" },
415 { 492, "mc", "Monaco" },
416 { 496, "mn", "Mongolia" },
417 { 500, "ms", "Montserrat" },
418 { 504, "ma", "Morocco" },
419 { 508, "mz", "Mozambique" },
420 { 104, "mm", "Myanmar" },
421 { 516, "na", "Namibia" },
422 { 520, "nr", "Nauru" },
423 { 524, "np", "Nepal" },
424 { 528, "nl", "Netherlands" },
425 { 530, "an", "Netherlands Antilles" },
426 { 540, "nc", "New Caledonia" },
427 { 554, "nz", "New Zealand" },
428 { 558, "ni", "Nicaragua" },
429 { 562, "ne", "Niger" },
430 { 566, "ng", "Nigeria" },
431 { 570, "nu", "Niue" },
432 { 574, "nf", "Norfolk Island" },
433 { 580, "mp", "Northern Mariana Islands" },
434 { 578, "no", "Norway" },
435 { 512, "om", "Oman" },
436 { 586, "pk", "Pakistan" },
437 { 585, "pw", "Palau" },
438 { 275, "ps", "Palestinian Territory" },
439 { 591, "pa", "Panama" },
440 { 598, "pg", "Papua New Guinea" },
441 { 600, "py", "Paraguay" },
442 { 604, "pe", "Peru" },
443 { 608, "ph", "Philippines" },
444 { 612, "pn", "Pitcairn" },
445 { 616, "pl", "Poland" },
446 { 620, "pt", "Portugal" },
447 { 630, "pr", "Puerto Rico" },
448 { 634, "qa", "Qatar" },
449 { 638, "re", "Reunion" },
450 { 642, "ro", "Romania" },
451 { 643, "ru", "Russia" },
452 { 646, "rw", "Rwanda" },
453 { 659, "kn", "Saint Kitts & Nevis" },
454 { 662, "lc", "Saint Lucia" },
455 { 670, "vc", "Saint Vincent" },
456 { 882, "ws", "Samoa" },
457 { 674, "sm", "San Marino" },
458 { 678, "st", "Sao Tome & Principe" },
459 { 682, "sa", "Saudi Arabia" },
460 { 686, "sn", "Senegal" },
461 { 891, "cs", "Serbia and Montenegro" },
462 { 690, "sc", "Seychelles" },
463 { 694, "sl", "Sierra Leone" },
464 { 702, "sg", "Singapore" },
465 { 703, "sk", "Slovakia" },
466 { 705, "si", "Slovenia" },
467 { 90, "sb", "Solomon Islands" },
468 { 706, "so", "Somalia" },
469 { 710, "za", "South Africa" },
470 { 239, "gs", "South Georgia" },
471 { 724, "es", "Spain" },
472 { 144, "lk", "Sri Lanka" },
473 { 654, "sh", "St. Helena" },
474 { 666, "pm", "St. Pierre & Miquelon" },
475 { 736, "sd", "Sudan" },
476 { 740, "sr", "Suriname" },
477 { 744, "sj", "Svalbard & Jan Mayen Islands" },
478 { 748, "sz", "Swaziland" },
479 { 752, "se", "Sweden" },
480 { 756, "ch", "Switzerland" },
481 { 760, "sy", "Syrian Arab Republic" },
482 { 626, "tl", "Timor-Leste" },
483 { 158, "tw", "Taiwan" },
484 { 762, "tj", "Tajikistan" },
485 { 834, "tz", "Tanzania" },
486 { 764, "th", "Thailand" },
487 { 768, "tg", "Togo" },
488 { 772, "tk", "Tokelau" },
489 { 776, "to", "Tonga" },
490 { 780, "tt", "Trinidad & Tobago" },
491 { 788, "tn", "Tunisia" },
492 { 792, "tr", "Turkey" },
493 { 795, "tm", "Turkmenistan" },
494 { 796, "tc", "Turks & Caicos Islands" },
495 { 798, "tv", "Tuvalu" },
496 { 800, "ug", "Uganda" },
497 { 804, "ua", "Ukraine" },
498 { 784, "ae", "United Arab Emirates" },
499 { 826, "gb", "United Kingdom" },
500 { 840, "us", "United States" },
501 { 581, "um", "United States Minor Outlying Islands" },
502 { 858, "uy", "Uruguay" },
503 { 860, "uz", "Uzbekistan" },
504 { 548, "vu", "Vanuatu" },
505 { 862, "ve", "Venezuela" },
506 { 704, "vn", "Vietnam" },
507 { 92, "vg", "Virgin Islands (British)" },
508 { 850, "vi", "Virgin Islands (US)" },
509 { 876, "wf", "Wallis & Futuna Islands" },
510 { 732, "eh", "Western Sahara" },
511 { 887, "ye", "Yemen" },
512 { 894, "zm", "Zambia" },
513 { 716, "zw", "Zimbabwe" }
517 * Check if start of 'str' is simply an IPv4 address.
519 #define BYTE_OK(x) ((x) >= 0 && (x) <= 255)
521 static int is_addr(char *str, char **end)
523 int a0, a1, a2, a3, num, rc = 0, length = 0;
525 num = sscanf(str,"%3d.%3d.%3d.%3d%n",&a0,&a1,&a2,&a3,&length);
527 BYTE_OK(a0) && BYTE_OK(a1) && BYTE_OK(a2) && BYTE_OK(a3) &&
537 * Find the country-code and name from the CNAME. E.g.:
538 * version 1: CNAME = zzno.countries.nerd.dk with address 127.0.2.66
539 * yields ccode_A" = "no" and cnumber 578 (2.66).
540 * version 2: CNAME = <a.b.c.d>.zz.countries.nerd.dk with address 127.0.2.66
541 * yields cnumber 578 (2.66). ccode_A is "";
543 static void find_country_from_cname(const char *cname, struct in_addr addr)
545 const struct search_list *country;
546 char ccode_A2[3], *ccopy, *dot_4;
547 int cnumber, z0, z1, ver_1, ver_2;
550 ip = ntohl(addr.s_addr);
551 z0 = TOLOWER(cname[0]);
552 z1 = TOLOWER(cname[1]);
553 ccopy = strdup(cname);
556 ver_1 = (z0 == 'z' && z1 == 'z' && !strcasecmp(cname+4,nerd_ver1));
557 ver_2 = (is_addr(ccopy,&dot_4) && !strcasecmp(dot_4,nerd_ver2));
561 const char *dot = strchr(cname, '.');
562 if ((z0 != 'z' && z1 != 'z') || dot != cname+4)
564 printf("Unexpected CNAME %s (ver_1)\n", cname);
570 z0 = TOLOWER(dot_4[1]);
571 z1 = TOLOWER(dot_4[2]);
572 if (z0 != 'z' && z1 != 'z')
574 printf("Unexpected CNAME %s (ver_2)\n", cname);
580 printf("Unexpected CNAME %s (ver?)\n", cname);
586 ccode_A2[0] = (char)TOLOWER(cname[2]);
587 ccode_A2[1] = (char)TOLOWER(cname[3]);
593 cnumber = ip & 0xFFFF;
595 TRACE(("Found country-code `%s', number %d\n",
596 ver_1 ? ccode_A2 : "<n/a>", cnumber));
598 country = list_lookup(cnumber, country_list,
599 sizeof(country_list) / sizeof(country_list[0]));
601 printf("Name for country-number %d not found.\n", cnumber);
606 if ((country->short_name[0] != ccode_A2[0]) ||
607 (country->short_name[1] != ccode_A2[1]) ||
608 (country->short_name[2] != ccode_A2[2]))
609 printf("short-name mismatch; %s vs %s\n",
610 country->short_name, ccode_A2);
612 printf("%s (%s), number %d.\n",
613 country->long_name, country->short_name, cnumber);