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