• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * IP-address/hostname to country converter.
4  *
5  * Problem; you want to know where IP a.b.c.d is located.
6  *
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)
11  *
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
14  * the <CC> number.
15  *
16  * Ref: http://countries.nerd.dk/more.html
17  *
18  * Written by G. Vanem <gvanem@yahoo.no> 2006, 2007
19  *
20  * NB! This program may not be big-endian aware.
21  *
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.
33  */
34 
35 #include "ares_setup.h"
36 
37 #ifdef HAVE_STRINGS_H
38 #include <strings.h>
39 #endif
40 
41 #if defined(WIN32) && !defined(WATT32)
42   #include <winsock.h>
43 #else
44   #include <arpa/inet.h>
45   #include <netinet/in.h>
46   #include <netdb.h>
47 #endif
48 
49 #include "ares.h"
50 #include "ares_getopt.h"
51 #include "ares_nowarn.h"
52 
53 #ifndef HAVE_STRDUP
54 #  include "ares_strdup.h"
55 #  define strdup(ptr) ares_strdup(ptr)
56 #endif
57 
58 #ifndef HAVE_STRCASECMP
59 #  include "ares_strcasecmp.h"
60 #  define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
61 #endif
62 
63 #ifndef HAVE_STRNCASECMP
64 #  include "ares_strcasecmp.h"
65 #  define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
66 #endif
67 
68 #ifndef INADDR_NONE
69 #define INADDR_NONE 0xffffffff
70 #endif
71 
72 /* By using a double cast, we can get rid of the bogus warning of
73  * warning: cast from 'const struct sockaddr *' to 'const struct sockaddr_in6 *' increases required alignment from 1 to 4 [-Wcast-align]
74  */
75 #define CARES_INADDR_CAST(type, var) ((type)((void *)var))
76 
77 static const char *usage      = "acountry [-?hdv] {host|addr} ...\n";
78 static const char  nerd_fmt[] = "%u.%u.%u.%u.zz.countries.nerd.dk";
79 static const char *nerd_ver1  = nerd_fmt + 14;  /* .countries.nerd.dk */
80 static const char *nerd_ver2  = nerd_fmt + 11;  /* .zz.countries.nerd.dk */
81 static int         verbose    = 0;
82 
83 #define TRACE(fmt) do {               \
84                      if (verbose > 0) \
85                        printf fmt ;   \
86                    } WHILE_FALSE
87 
88 static void wait_ares(ares_channel channel);
89 static void callback(void *arg, int status, int timeouts, struct hostent *host);
90 static void callback2(void *arg, int status, int timeouts, struct hostent *host);
91 static void find_country_from_cname(const char *cname, struct in_addr addr);
92 static void print_help_info_acountry(void);
93 
Abort(const char * fmt,...)94 static void Abort(const char *fmt, ...)
95 {
96   va_list args;
97   va_start(args, fmt);
98   vfprintf(stderr, fmt, args);
99   va_end(args);
100   exit(1);
101 }
102 
main(int argc,char ** argv)103 int main(int argc, char **argv)
104 {
105   ares_channel channel;
106   int    ch, status;
107 
108 #if defined(WIN32) && !defined(WATT32)
109   WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
110   WSADATA wsaData;
111   WSAStartup(wVersionRequested, &wsaData);
112 #endif
113 
114   status = ares_library_init(ARES_LIB_INIT_ALL);
115   if (status != ARES_SUCCESS)
116     {
117       fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
118       return 1;
119     }
120 
121   while ((ch = ares_getopt(argc, argv, "dvh?")) != -1)
122     switch (ch)
123       {
124       case 'd':
125 #ifdef WATT32
126         dbug_init();
127 #endif
128         break;
129       case 'v':
130         verbose++;
131         break;
132       case 'h':
133         print_help_info_acountry();
134         break;
135       case '?':
136         print_help_info_acountry();
137         break;
138       default:
139         Abort(usage);
140       }
141 
142   argc -= optind;
143   argv += optind;
144   if (argc < 1)
145      Abort(usage);
146 
147   status = ares_init(&channel);
148   if (status != ARES_SUCCESS)
149     {
150       fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
151       return 1;
152     }
153 
154   /* Initiate the queries, one per command-line argument. */
155   for ( ; *argv; argv++)
156     {
157       struct in_addr addr;
158       char buf[100];
159 
160       /* If this fails, assume '*argv' is a host-name that
161        * must be resolved first
162        */
163       if (ares_inet_pton(AF_INET, *argv, &addr) != 1)
164         {
165           ares_gethostbyname(channel, *argv, AF_INET, callback2, &addr);
166           wait_ares(channel);
167           if (addr.s_addr == INADDR_NONE)
168             {
169               printf("Failed to lookup %s\n", *argv);
170               continue;
171             }
172         }
173 
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));
180       fflush(stdout);
181       ares_gethostbyname(channel, buf, AF_INET, callback, buf);
182     }
183 
184   wait_ares(channel);
185   ares_destroy(channel);
186 
187   ares_library_cleanup();
188 
189 #if defined(WIN32) && !defined(WATT32)
190   WSACleanup();
191 #endif
192 
193   return 0;
194 }
195 
196 /*
197  * Wait for the queries to complete.
198  */
wait_ares(ares_channel channel)199 static void wait_ares(ares_channel channel)
200 {
201   for (;;)
202     {
203       struct timeval *tvp, tv;
204       fd_set read_fds, write_fds;
205       int nfds;
206 
207       FD_ZERO(&read_fds);
208       FD_ZERO(&write_fds);
209       nfds = ares_fds(channel, &read_fds, &write_fds);
210       if (nfds == 0)
211         break;
212       tvp = ares_timeout(channel, NULL, &tv);
213       nfds = select(nfds, &read_fds, &write_fds, NULL, tvp);
214       if (nfds < 0)
215         continue;
216       ares_process(channel, &read_fds, &write_fds);
217     }
218 }
219 
220 /*
221  * This is the callback used when we have the IP-address of interest.
222  * Extract the CNAME and figure out the country-code from it.
223  */
callback(void * arg,int status,int timeouts,struct hostent * host)224 static void callback(void *arg, int status, int timeouts, struct hostent *host)
225 {
226   const char *name = (const char*)arg;
227   const char *cname;
228   char buf[20];
229 
230   (void)timeouts;
231 
232   if (!host || status != ARES_SUCCESS)
233     {
234       printf("Failed to lookup %s: %s\n", name, ares_strerror(status));
235       return;
236     }
237 
238   TRACE(("\nFound address %s, name %s\n",
239          ares_inet_ntop(AF_INET,(const char*)host->h_addr,buf,sizeof(buf)),
240          host->h_name));
241 
242   cname = host->h_name;  /* CNAME gets put here */
243   if (!cname)
244     printf("Failed to get CNAME for %s\n", name);
245   else
246     find_country_from_cname(cname, *(CARES_INADDR_CAST(struct in_addr *, host->h_addr)));
247 }
248 
249 /*
250  * This is the callback used to obtain the IP-address of the host of interest.
251  */
callback2(void * arg,int status,int timeouts,struct hostent * host)252 static void callback2(void *arg, int status, int timeouts, struct hostent *host)
253 {
254   struct in_addr *addr = (struct in_addr*) arg;
255 
256   (void)timeouts;
257   if (!host || status != ARES_SUCCESS)
258     memset(addr, INADDR_NONE, sizeof(*addr));
259   else
260     memcpy(addr, host->h_addr, sizeof(*addr));
261 }
262 
263 struct search_list {
264        int         country_number; /* ISO-3166 country number */
265        char        short_name[3];  /* A2 short country code */
266        const char *long_name;      /* normal country name */
267      };
268 
list_lookup(int number,const struct search_list * list,int num)269 static const struct search_list *list_lookup(int number, const struct search_list *list, int num)
270 {
271   while (num > 0 && list->long_name)
272     {
273       if (list->country_number == number)
274         return (list);
275       num--;
276       list++;
277     }
278   return (NULL);
279 }
280 
281 /*
282  * Ref: https://en.wikipedia.org/wiki/ISO_3166-1
283  */
284 static const struct search_list country_list[] = {
285        {   4, "af", "Afghanistan"                          },
286        { 248, "ax", "Åland Island"                         },
287        {   8, "al", "Albania"                              },
288        {  12, "dz", "Algeria"                              },
289        {  16, "as", "American Samoa"                       },
290        {  20, "ad", "Andorra"                              },
291        {  24, "ao", "Angola"                               },
292        { 660, "ai", "Anguilla"                             },
293        {  10, "aq", "Antarctica"                           },
294        {  28, "ag", "Antigua & Barbuda"                    },
295        {  32, "ar", "Argentina"                            },
296        {  51, "am", "Armenia"                              },
297        { 533, "aw", "Aruba"                                },
298        {  36, "au", "Australia"                            },
299        {  40, "at", "Austria"                              },
300        {  31, "az", "Azerbaijan"                           },
301        {  44, "bs", "Bahamas"                              },
302        {  48, "bh", "Bahrain"                              },
303        {  50, "bd", "Bangladesh"                           },
304        {  52, "bb", "Barbados"                             },
305        { 112, "by", "Belarus"                              },
306        {  56, "be", "Belgium"                              },
307        {  84, "bz", "Belize"                               },
308        { 204, "bj", "Benin"                                },
309        {  60, "bm", "Bermuda"                              },
310        {  64, "bt", "Bhutan"                               },
311        {  68, "bo", "Bolivia"                              },
312        { 535, "bq", "Bonaire, Sint Eustatius and Saba"     }, /* Formerly 'Bonaire' / 'Netherlands Antilles' */
313        {  70, "ba", "Bosnia & Herzegovina"                 },
314        {  72, "bw", "Botswana"                             },
315        {  74, "bv", "Bouvet Island"                        },
316        {  76, "br", "Brazil"                               },
317        {  86, "io", "British Indian Ocean Territory"       },
318        {  96, "bn", "Brunei Darussalam"                    },
319        { 100, "bg", "Bulgaria"                             },
320        { 854, "bf", "Burkina Faso"                         },
321        { 108, "bi", "Burundi"                              },
322        { 116, "kh", "Cambodia"                             },
323        { 120, "cm", "Cameroon"                             },
324        { 124, "ca", "Canada"                               },
325        { 132, "cv", "Cape Verde"                           },
326        { 136, "ky", "Cayman Islands"                       },
327        { 140, "cf", "Central African Republic"             },
328        { 148, "td", "Chad"                                 },
329        { 152, "cl", "Chile"                                },
330        { 156, "cn", "China"                                },
331        { 162, "cx", "Christmas Island"                     },
332        { 166, "cc", "Cocos Islands"                        },
333        { 170, "co", "Colombia"                             },
334        { 174, "km", "Comoros"                              },
335        { 178, "cg", "Congo"                                },
336        { 180, "cd", "Congo"                                },
337        { 184, "ck", "Cook Islands"                         },
338        { 188, "cr", "Costa Rica"                           },
339        { 384, "ci", "Cote d'Ivoire"                        },
340        { 191, "hr", "Croatia"                              },
341        { 192, "cu", "Cuba"                                 },
342        { 531, "cw", "Curaçao"                              },
343        { 196, "cy", "Cyprus"                               },
344        { 203, "cz", "Czech Republic"                       },
345        { 208, "dk", "Denmark"                              },
346        { 262, "dj", "Djibouti"                             },
347        { 212, "dm", "Dominica"                             },
348        { 214, "do", "Dominican Republic"                   },
349        { 218, "ec", "Ecuador"                              },
350        { 818, "eg", "Egypt"                                },
351        { 222, "sv", "El Salvador"                          },
352        { 226, "gq", "Equatorial Guinea"                    },
353        { 232, "er", "Eritrea"                              },
354        { 233, "ee", "Estonia"                              },
355        { 748, "sz", "Eswatini"                             }, /* Formerly Swaziland */
356        { 231, "et", "Ethiopia"                             },
357      { 65281, "eu", "European Union"                       }, /* 127.0.255.1 */
358        { 238, "fk", "Falkland Islands"                     },
359        { 234, "fo", "Faroe Islands"                        },
360        { 242, "fj", "Fiji"                                 },
361        { 246, "fi", "Finland"                              },
362        { 250, "fr", "France"                               },
363        { 249, "fx", "France, Metropolitan"                 },
364        { 254, "gf", "French Guiana"                        },
365        { 258, "pf", "French Polynesia"                     },
366        { 260, "tf", "French Southern Territories"          },
367        { 266, "ga", "Gabon"                                },
368        { 270, "gm", "Gambia"                               },
369        { 268, "ge", "Georgia"                              },
370        { 276, "de", "Germany"                              },
371        { 288, "gh", "Ghana"                                },
372        { 292, "gi", "Gibraltar"                            },
373        { 300, "gr", "Greece"                               },
374        { 304, "gl", "Greenland"                            },
375        { 308, "gd", "Grenada"                              },
376        { 312, "gp", "Guadeloupe"                           },
377        { 316, "gu", "Guam"                                 },
378        { 320, "gt", "Guatemala"                            },
379        { 831, "gg", "Guernsey"                             },
380        { 324, "gn", "Guinea"                               },
381        { 624, "gw", "Guinea-Bissau"                        },
382        { 328, "gy", "Guyana"                               },
383        { 332, "ht", "Haiti"                                },
384        { 334, "hm", "Heard & Mc Donald Islands"            },
385        { 336, "va", "Holy See"                             }, /* Vatican City */
386        { 340, "hn", "Honduras"                             },
387        { 344, "hk", "Hong kong"                            },
388        { 348, "hu", "Hungary"                              },
389        { 352, "is", "Iceland"                              },
390        { 356, "in", "India"                                },
391        { 360, "id", "Indonesia"                            },
392        { 364, "ir", "Iran"                                 },
393        { 368, "iq", "Iraq"                                 },
394        { 372, "ie", "Ireland"                              },
395        { 833, "im", "Isle of Man"                          },
396        { 376, "il", "Israel"                               },
397        { 380, "it", "Italy"                                },
398        { 388, "jm", "Jamaica"                              },
399        { 392, "jp", "Japan"                                },
400        { 832, "je", "Jersey"                               },
401        { 400, "jo", "Jordan"                               },
402        { 398, "kz", "Kazakhstan"                           },
403        { 404, "ke", "Kenya"                                },
404        { 296, "ki", "Kiribati"                             },
405        { 408, "kp", "Korea (north)"                        },
406        { 410, "kr", "Korea (south)"                        },
407        {   0, "xk", "Kosovo"                               }, /* https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 */
408        { 414, "kw", "Kuwait"                               },
409        { 417, "kg", "Kyrgyzstan"                           },
410        { 418, "la", "Laos"                                 },
411        { 428, "lv", "Latvia"                               },
412        { 422, "lb", "Lebanon"                              },
413        { 426, "ls", "Lesotho"                              },
414        { 430, "lr", "Liberia"                              },
415        { 434, "ly", "Libya"                                },
416        { 438, "li", "Liechtenstein"                        },
417        { 440, "lt", "Lithuania"                            },
418        { 442, "lu", "Luxembourg"                           },
419        { 446, "mo", "Macao"                                },
420        { 450, "mg", "Madagascar"                           },
421        { 454, "mw", "Malawi"                               },
422        { 458, "my", "Malaysia"                             },
423        { 462, "mv", "Maldives"                             },
424        { 466, "ml", "Mali"                                 },
425        { 470, "mt", "Malta"                                },
426        { 584, "mh", "Marshall Islands"                     },
427        { 474, "mq", "Martinique"                           },
428        { 478, "mr", "Mauritania"                           },
429        { 480, "mu", "Mauritius"                            },
430        { 175, "yt", "Mayotte"                              },
431        { 484, "mx", "Mexico"                               },
432        { 583, "fm", "Micronesia"                           },
433        { 498, "md", "Moldova"                              },
434        { 492, "mc", "Monaco"                               },
435        { 496, "mn", "Mongolia"                             },
436        { 499, "me", "Montenegro"                           },
437        { 500, "ms", "Montserrat"                           },
438        { 504, "ma", "Morocco"                              },
439        { 508, "mz", "Mozambique"                           },
440        { 104, "mm", "Myanmar"                              },
441        { 516, "na", "Namibia"                              },
442        { 520, "nr", "Nauru"                                },
443        { 524, "np", "Nepal"                                },
444        { 528, "nl", "Netherlands"                          },
445        { 540, "nc", "New Caledonia"                        },
446        { 554, "nz", "New Zealand"                          },
447        { 558, "ni", "Nicaragua"                            },
448        { 562, "ne", "Niger"                                },
449        { 566, "ng", "Nigeria"                              },
450        { 570, "nu", "Niue"                                 },
451        { 574, "nf", "Norfolk Island"                       },
452        { 807, "mk", "North Macedonia"                      }, /* 'Macedonia' until February 2019 */
453        { 580, "mp", "Northern Mariana Islands"             },
454        { 578, "no", "Norway"                               },
455        { 512, "om", "Oman"                                 },
456        { 586, "pk", "Pakistan"                             },
457        { 585, "pw", "Palau"                                },
458        { 275, "ps", "Palestinian Territory"                },
459        { 591, "pa", "Panama"                               },
460        { 598, "pg", "Papua New Guinea"                     },
461        { 600, "py", "Paraguay"                             },
462        { 604, "pe", "Peru"                                 },
463        { 608, "ph", "Philippines"                          },
464        { 612, "pn", "Pitcairn"                             },
465        { 616, "pl", "Poland"                               },
466        { 620, "pt", "Portugal"                             },
467        { 630, "pr", "Puerto Rico"                          },
468        { 634, "qa", "Qatar"                                },
469        { 638, "re", "Reunion"                              },
470        { 642, "ro", "Romania"                              },
471        { 643, "ru", "Russian Federation"                   },
472        { 646, "rw", "Rwanda"                               },
473        { 0,   "bl", "Saint Barthélemy"                     }, /* https://en.wikipedia.org/wiki/ISO_3166-2:BL */
474        { 659, "kn", "Saint Kitts & Nevis"                  },
475        { 662, "lc", "Saint Lucia"                          },
476        { 663, "mf", "Saint Martin"                         },
477        { 670, "vc", "Saint Vincent"                        },
478        { 882, "ws", "Samoa"                                },
479        { 674, "sm", "San Marino"                           },
480        { 678, "st", "Sao Tome & Principe"                  },
481        { 682, "sa", "Saudi Arabia"                         },
482        { 686, "sn", "Senegal"                              },
483        { 688, "rs", "Serbia"                               },
484        { 690, "sc", "Seychelles"                           },
485        { 694, "sl", "Sierra Leone"                         },
486        { 702, "sg", "Singapore"                            },
487        { 534, "sx", "Sint Maarten"                         },
488        { 703, "sk", "Slovakia"                             },
489        { 705, "si", "Slovenia"                             },
490        {  90, "sb", "Solomon Islands"                      },
491        { 706, "so", "Somalia"                              },
492        { 710, "za", "South Africa"                         },
493        { 239, "gs", "South Georgia & South Sandwich Is."   },
494        { 728, "ss", "South Sudan"                          },
495        { 724, "es", "Spain"                                },
496        { 144, "lk", "Sri Lanka"                            },
497        { 654, "sh", "St. Helena"                           },
498        { 666, "pm", "St. Pierre & Miquelon"                },
499        { 736, "sd", "Sudan"                                },
500        { 740, "sr", "Suriname"                             },
501        { 744, "sj", "Svalbard & Jan Mayen Islands"         },
502        { 752, "se", "Sweden"                               },
503        { 756, "ch", "Switzerland"                          },
504        { 760, "sy", "Syrian Arab Republic"                 },
505        { 158, "tw", "Taiwan"                               },
506        { 762, "tj", "Tajikistan"                           },
507        { 834, "tz", "Tanzania"                             },
508        { 764, "th", "Thailand"                             },
509        { 626, "tl", "Timor-Leste"                          },
510        { 768, "tg", "Togo"                                 },
511        { 772, "tk", "Tokelau"                              },
512        { 776, "to", "Tonga"                                },
513        { 780, "tt", "Trinidad & Tobago"                    },
514        { 788, "tn", "Tunisia"                              },
515        { 792, "tr", "Turkey"                               },
516        { 795, "tm", "Turkmenistan"                         },
517        { 796, "tc", "Turks & Caicos Islands"               },
518        { 798, "tv", "Tuvalu"                               },
519        { 800, "ug", "Uganda"                               },
520        { 804, "ua", "Ukraine"                              },
521        { 784, "ae", "United Arab Emirates"                 },
522        { 826, "gb", "United Kingdom"                       },
523        { 840, "us", "United States"                        },
524        { 581, "um", "United States Minor Outlying Islands" },
525        { 858, "uy", "Uruguay"                              },
526        { 860, "uz", "Uzbekistan"                           },
527        { 548, "vu", "Vanuatu"                              },
528        { 862, "ve", "Venezuela"                            },
529        { 704, "vn", "Vietnam"                              },
530        {  92, "vg", "Virgin Islands (British)"             },
531        { 850, "vi", "Virgin Islands (US)"                  },
532        { 876, "wf", "Wallis & Futuna Islands"              },
533        { 732, "eh", "Western Sahara"                       },
534        { 887, "ye", "Yemen"                                },
535        { 894, "zm", "Zambia"                               },
536        { 716, "zw", "Zimbabwe"                             }
537      };
538 
539 /*
540  * Check if start of 'str' is simply an IPv4 address.
541  */
542 #define BYTE_OK(x) ((x) >= 0 && (x) <= 255)
543 
is_addr(char * str,char ** end)544 static int is_addr(char *str, char **end)
545 {
546   int a0, a1, a2, a3, num, rc = 0, length = 0;
547 
548   num = sscanf(str,"%3d.%3d.%3d.%3d%n",&a0,&a1,&a2,&a3,&length);
549   if( (num == 4) &&
550       BYTE_OK(a0) && BYTE_OK(a1) && BYTE_OK(a2) && BYTE_OK(a3) &&
551       length >= (3+4))
552     {
553       rc = 1;
554       *end = str + length;
555     }
556   return rc;
557 }
558 
559 /*
560  * Find the country-code and name from the CNAME. E.g.:
561  *   version 1: CNAME = zzno.countries.nerd.dk with address 127.0.2.66
562  *              yields ccode_A" = "no" and cnumber 578 (2.66).
563  *   version 2: CNAME = <a.b.c.d>.zz.countries.nerd.dk with address 127.0.2.66
564  *              yields cnumber 578 (2.66). ccode_A is "";
565  */
find_country_from_cname(const char * cname,struct in_addr addr)566 static void find_country_from_cname(const char *cname, struct in_addr addr)
567 {
568   const struct search_list *country;
569   char  ccode_A2[3], *ccopy, *dot_4;
570   int   cnumber, z0, z1, ver_1, ver_2;
571   unsigned long ip;
572 
573   ip = ntohl(addr.s_addr);
574   z0 = TOLOWER(cname[0]);
575   z1 = TOLOWER(cname[1]);
576   ccopy = strdup(cname);
577   dot_4 = NULL;
578 
579   ver_1 = (z0 == 'z' && z1 == 'z' && !strcasecmp(cname+4,nerd_ver1));
580   ver_2 = (is_addr(ccopy,&dot_4) && !strcasecmp(dot_4,nerd_ver2));
581 
582   if (ver_1)
583     {
584       const char *dot = strchr(cname, '.');
585       if (dot != cname+4)
586         {
587           printf("Unexpected CNAME %s (ver_1)\n", cname);
588           free(ccopy);
589           return;
590         }
591     }
592   else if (ver_2)
593     {
594       z0 = TOLOWER(dot_4[1]);
595       z1 = TOLOWER(dot_4[2]);
596       if (z0 != 'z' && z1 != 'z')
597         {
598           printf("Unexpected CNAME %s (ver_2)\n", cname);
599           free(ccopy);
600           return;
601         }
602     }
603   else
604     {
605       printf("Unexpected CNAME %s (ver?)\n", cname);
606       free(ccopy);
607       return;
608     }
609 
610   if (ver_1)
611     {
612       ccode_A2[0] = (char)TOLOWER(cname[2]);
613       ccode_A2[1] = (char)TOLOWER(cname[3]);
614       ccode_A2[2] = '\0';
615     }
616   else
617     ccode_A2[0] = '\0';
618 
619   cnumber = ip & 0xFFFF;
620 
621   TRACE(("Found country-code `%s', number %d\n",
622          ver_1 ? ccode_A2 : "<n/a>", cnumber));
623 
624   country = list_lookup(cnumber, country_list,
625                         sizeof(country_list) / sizeof(country_list[0]));
626   if (!country)
627     printf("Name for country-number %d not found.\n", cnumber);
628   else
629     {
630       if (ver_1)
631         {
632           if ((country->short_name[0] != ccode_A2[0]) ||
633               (country->short_name[1] != ccode_A2[1]) ||
634               (country->short_name[2] != ccode_A2[2]))
635             printf("short-name mismatch; %s vs %s\n",
636                    country->short_name, ccode_A2);
637         }
638       printf("%s (%s), number %d.\n",
639              country->long_name, country->short_name, cnumber);
640     }
641   free(ccopy);
642 }
643 
644 /* Information from the man page. Formatting taken from man -h */
print_help_info_acountry(void)645 static void print_help_info_acountry(void) {
646     printf("acountry, version %s \n\n", ARES_VERSION_STR);
647     printf("usage: acountry [-hdv] {host|addr} ...\n\n"
648     "  d : Print some extra debugging output.\n"
649     "  h : Display this help and exit.\n"
650     "  v : Be more verbose. Print extra information.\n\n");
651     exit(0);
652 }
653