2 * Routines for network object lookup
6 * Laurent Deniel <laurent.deniel@free.fr>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
38 * are most likely to take a long time, given the way address-to-name
39 * lookups are done over NBNS).
41 * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
42 * call in a signal handler, you might crash, because the state of the
43 * resolution code that sends messages to lookupd might be inconsistent
44 * if you jump out of it in middle of a call.
46 * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
47 * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
48 * call (and possibly in other gethostbyaddr() calls), because there's
49 * a mutex lock held in gethostbyaddr() and it doesn't get released
50 * if we longjmp out of it.
52 * There's no guarantee that longjmp()ing out of name resolution calls
53 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
54 * code in tcpdump, to avoid those sorts of problems, and that was
55 * picked up by tcpdump.org tcpdump.
57 * So, for now, we do not define AVOID_DNS_TIMEOUT. If we get a
58 * significantly more complaints about lookups taking a long time,
59 * we can reconsider that decision. (Note that tcpdump originally
60 * added that for the benefit of systems using NIS to look up host
61 * names; that might now be fixed in NIS implementations, for those
62 * sites still using NIS rather than DNS for that....)
69 #ifdef HAVE_NETINET_IN_H
70 # include <netinet/in.h>
77 #ifdef HAVE_ARPA_INET_H
78 #include <arpa/inet.h>
83 #ifdef HAVE_SYS_SOCKET_H
84 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
87 #ifdef HAVE_WINSOCK2_H
88 #include <winsock2.h> /* needed to define AF_ values on Windows */
91 #ifdef AVOID_DNS_TIMEOUT
95 #ifdef NEED_INET_ATON_H
96 # include "inet_aton.h"
99 #ifdef NEED_INET_V6DEFS_H
100 # include "inet_v6defs.h"
108 #if defined(_WIN32) && defined(INET6)
109 # include <ws2tcpip.h>
113 #include "ipv6-utils.h"
114 #include "addr_resolv.h"
115 #include "filesystem.h"
116 #include <epan/prefs.h>
118 #define ENAME_ETHERS "ethers"
119 #define ENAME_IPXNETS "ipxnets"
120 #define ENAME_MANUF "manuf"
122 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
123 #define HASHETHSIZE 1024
124 #define HASHHOSTSIZE 1024
125 #define HASHIPXNETSIZE 256
126 #define HASHMANUFSIZE 256
127 #define HASHPORTSIZE 256
129 /* hash table used for IPv4 lookup */
131 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
133 typedef struct hashipv4 {
135 gchar name[MAXNAMELEN];
136 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
137 struct hashipv4 *next;
140 /* hash table used for IPv6 lookup */
142 #define HASH_IPV6_ADDRESS(addr) \
143 ((((addr).s6_addr8[14] << 8)|((addr).s6_addr8[15])) & (HASHHOSTSIZE - 1))
145 typedef struct hashipv6 {
146 struct e_in6_addr addr;
147 gchar name[MAXNAMELEN];
148 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
149 struct hashipv6 *next;
152 /* hash table used for TCP/UDP/SCTP port lookup */
154 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
156 typedef struct hashport {
158 gchar name[MAXNAMELEN];
159 struct hashport *next;
162 /* hash table used for IPX network lookup */
164 /* XXX - check goodness of hash function */
166 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
168 typedef struct hashipxnet {
170 gchar name[MAXNAMELEN];
171 struct hashipxnet *next;
174 /* hash tables used for ethernet and manufacturer lookup */
176 #define HASH_ETH_ADDRESS(addr) \
177 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
180 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
182 typedef struct hashmanuf {
184 char name[MAXMANUFLEN];
185 struct hashmanuf *next;
188 typedef struct hashether {
190 char name[MAXNAMELEN];
191 gboolean is_dummy_entry; /* not a complete entry */
192 struct hashether *next;
195 /* internal ethernet type */
197 typedef struct _ether
200 char name[MAXNAMELEN];
203 /* internal ipxnet type */
205 typedef struct _ipxnet
208 char name[MAXNAMELEN];
211 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
212 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
213 static hashport_t *udp_port_table[HASHPORTSIZE];
214 static hashport_t *tcp_port_table[HASHPORTSIZE];
215 static hashport_t *sctp_port_table[HASHPORTSIZE];
216 static hashether_t *eth_table[HASHETHSIZE];
217 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
218 static hashether_t *(*wka_table[48])[HASHETHSIZE];
219 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
221 static int eth_resolution_initialized = 0;
222 static int ipxnet_resolution_initialized = 0;
224 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
227 * Flag controlling what names to resolve.
229 guint32 g_resolv_flags;
232 * Global variables (can be changed in GUI sections)
233 * XXX - they could be changed in GUI code, but there's currently no
234 * GUI code to change them.
237 gchar *g_ethers_path = NULL; /* global ethers file */
238 gchar *g_pethers_path = NULL; /* personal ethers file */
239 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
240 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
241 /* first resolving call */
247 static gboolean gnu_adns_initialized = FALSE;
251 int adns_currently_queued = 0;
253 typedef struct _adns_queue_msg
257 struct e_in6_addr ip6_addr;
262 GList *adns_queue_head = NULL;
264 #endif /* HAVE_GNU_ADNS */
267 * Local function definitions
270 static gchar *serv_name_lookup(guint port, port_type proto)
275 char *serv_proto = NULL;
276 struct servent *servp;
280 table = udp_port_table;
284 table = tcp_port_table;
288 table = sctp_port_table;
292 /* not yet implemented */
298 hash_idx = HASH_PORT(port);
299 tp = table[hash_idx];
302 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
305 if( tp->port == port ) {
308 if (tp->next == NULL) {
309 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
317 /* fill in a new entry */
321 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
322 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
324 sprintf(tp->name, "%d", port);
326 strncpy(tp->name, servp->s_name, MAXNAMELEN);
327 tp->name[MAXNAMELEN-1] = '\0';
332 } /* serv_name_lookup */
335 #ifdef AVOID_DNS_TIMEOUT
337 #define DNS_TIMEOUT 2 /* max sec per call */
339 jmp_buf hostname_env;
341 static void abort_network_query(int sig _U_)
343 longjmp(hostname_env, 1);
345 #endif /* AVOID_DNS_TIMEOUT */
347 static gchar *host_name_lookup(guint addr, gboolean *found)
350 hashipv4_t * volatile tp;
351 struct hostent *hostp;
353 adns_queue_msg_t *qmsg;
358 hash_idx = HASH_IPV4_ADDRESS(addr);
360 tp = ipv4_table[hash_idx];
363 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
366 if( tp->addr == addr ) {
367 if (tp->is_dummy_entry)
371 if (tp->next == NULL) {
372 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
380 /* fill in a new entry */
385 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
386 prefs.name_resolve_concurrency > 0 &&
387 gnu_adns_initialized) {
388 qmsg = g_malloc(sizeof(adns_queue_msg_t));
389 qmsg->type = AF_INET;
390 qmsg->ip4_addr = addr;
391 qmsg->submitted = FALSE;
392 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
394 tp->is_dummy_entry = TRUE;
395 ip_to_str_buf((guint8 *)&addr, tp->name);
398 #endif /* HAVE_GNU_ADNS */
401 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
402 * the name of the host on which it's running; to work around that
403 * botch, we don't try to translate an all-zero IP address to a host
406 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
407 /* Use async DNS if possible, else fall back to timeouts,
408 * else call gethostbyaddr and hope for the best
411 # ifdef AVOID_DNS_TIMEOUT
413 /* Quick hack to avoid DNS/YP timeout */
415 if (!setjmp(hostname_env)) {
416 signal(SIGALRM, abort_network_query);
418 # endif /* AVOID_DNS_TIMEOUT */
420 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
422 # ifdef AVOID_DNS_TIMEOUT
424 # endif /* AVOID_DNS_TIMEOUT */
427 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
428 tp->name[MAXNAMELEN-1] = '\0';
429 tp->is_dummy_entry = FALSE;
433 # ifdef AVOID_DNS_TIMEOUT
435 # endif /* AVOID_DNS_TIMEOUT */
439 /* unknown host or DNS timeout */
441 ip_to_str_buf((guint8 *)&addr, tp->name);
442 tp->is_dummy_entry = TRUE;
447 } /* host_name_lookup */
449 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
452 hashipv6_t * volatile tp;
454 struct hostent *hostp;
459 hash_idx = HASH_IPV6_ADDRESS(*addr);
461 tp = ipv6_table[hash_idx];
464 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
467 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
468 if (tp->is_dummy_entry)
472 if (tp->next == NULL) {
473 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
481 /* fill in a new entry */
486 if (g_resolv_flags & RESOLV_NETWORK) {
487 #ifdef AVOID_DNS_TIMEOUT
489 /* Quick hack to avoid DNS/YP timeout */
491 if (!setjmp(hostname_env)) {
492 signal(SIGALRM, abort_network_query);
494 #endif /* AVOID_DNS_TIMEOUT */
495 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
496 #ifdef AVOID_DNS_TIMEOUT
498 # endif /* AVOID_DNS_TIMEOUT */
501 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
502 tp->name[MAXNAMELEN-1] = '\0';
503 tp->is_dummy_entry = FALSE;
507 #ifdef AVOID_DNS_TIMEOUT
509 # endif /* AVOID_DNS_TIMEOUT */
513 /* unknown host or DNS timeout */
515 ip6_to_str_buf(addr, tp->name);
516 tp->is_dummy_entry = TRUE;
520 } /* host_name_lookup6 */
523 * Miscellaneous functions
526 static int fgetline(char **buf, int *size, FILE *fp)
538 if ((*buf = g_malloc(*size)) == NULL)
546 while ((c = getc(fp)) != EOF && c != '\n') {
547 if (len+1 >= *size) {
548 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
554 if (len == 0 && c == EOF)
565 * Ethernet / manufacturer resolution
567 * The following functions implement ethernet address resolution and
568 * ethers files parsing (see ethers(4)).
570 * The manuf file has the same format as ethers(4) except that names are
571 * truncated to MAXMANUFLEN-1 characters and that an address contains
572 * only 3 bytes (instead of 6).
576 * I decide to not use the existing functions (see ethers(3) on some
577 * operating systems) for the following reasons:
578 * - performance gains (use of hash tables and some other enhancements),
579 * - use of two ethers files (system-wide and per user),
580 * - avoid the use of NIS maps,
581 * - lack of these functions on some systems.
583 * So the following functions do _not_ behave as the standard ones.
590 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
591 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
595 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
603 for (i = 0; i < 6; i++) {
604 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
605 if (!isxdigit((unsigned char)*cp))
607 num = strtoul(cp, &p, 16);
609 return FALSE; /* failed */
611 return FALSE; /* not a valid octet */
612 eth->addr[i] = (guint8) num;
613 cp = p; /* skip past the number */
615 /* OK, what character terminated the octet? */
617 /* "/" - this has a mask. */
619 /* Entries with masks are allowed only in the "manuf" files. */
622 cp++; /* skip past the '/' to get to the mask */
623 if (!isdigit((unsigned char)*cp))
624 return FALSE; /* no sign allowed */
625 num = strtoul(cp, &p, 10);
627 return FALSE; /* failed */
628 cp = p; /* skip past the number */
629 if (*cp != '\0' && !isspace((unsigned char)*cp))
630 return FALSE; /* bogus terminator */
631 if (num == 0 || num >= 48)
632 return FALSE; /* bogus mask */
633 /* Mask out the bits not covered by the mask */
635 for (i = 0; num >= 8; i++, num -= 8)
636 ; /* skip octets entirely covered by the mask */
637 /* Mask out the first masked octet */
638 eth->addr[i] &= (0xFF << (8 - num));
640 /* Mask out completely-masked-out octets */
646 /* We're at the end of the address, and there's no mask. */
648 /* We got 3 bytes, so this is a manufacturer ID. */
650 /* Manufacturer IDs are only allowed in the "manuf"
654 /* Indicate that this is a manufacturer ID (0 is not allowed
661 /* We got 6 bytes, so this is a MAC address.
662 If we're reading one of the "manuf" files, indicate that
663 this is a MAC address (48 is not allowed as a mask). */
669 /* We didn't get 3 or 6 bytes, and there's no mask; this is
674 /* We don't know the separator used in this number; it can either
675 be ':', '-', or '.'. */
676 if (*cp != ':' && *cp != '-' && *cp != '.')
678 sep = *cp; /* subsequent separators must be the same */
680 /* It has to be the same as the first separator */
691 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
695 * See the ethers(4) or ethers(5) man page for ethers file format
696 * (not available on all systems).
697 * We allow both ethernet address separators (':' and '-'),
698 * as well as Ethereal's '.' separator.
703 if ((cp = strchr(line, '#')))
706 if ((cp = strtok(line, " \t")) == NULL)
709 if (!parse_ether_address(cp, eth, mask, manuf_file))
712 if ((cp = strtok(NULL, " \t")) == NULL)
715 strncpy(eth->name, cp, MAXNAMELEN);
716 eth->name[MAXNAMELEN-1] = '\0';
720 } /* parse_ether_line */
722 static FILE *eth_p = NULL;
724 static void set_ethent(char *path)
729 eth_p = fopen(path, "r");
732 static void end_ethent(void)
740 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
745 static char *buf = NULL;
750 while (fgetline(&buf, &size, eth_p) >= 0) {
751 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
760 static ether_t *get_ethbyname(const gchar *name)
764 set_ethent(g_ethers_path);
766 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
772 set_ethent(g_pethers_path);
774 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
782 } /* get_ethbyname */
784 static ether_t *get_ethbyaddr(const guint8 *addr)
789 set_ethent(g_ethers_path);
791 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
797 set_ethent(g_pethers_path);
799 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
807 } /* get_ethbyaddr */
809 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
812 /* All but the topmost byte is masked out */
813 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
817 /* All but the topmost 2 bytes are masked out */
818 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
823 /* All but the topmost 3 bytes are masked out */
824 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
829 /* All but the topmost 4 bytes are masked out */
830 return ((((addr[0] << 8) | addr[1]) ^
831 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
836 /* All but the topmost 5 bytes are masked out */
837 return ((((addr[1] << 8) | addr[2]) ^
838 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
842 /* No bytes are fully masked out */
843 return ((((addr[1] << 8) | addr[2]) ^
844 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
848 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
852 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
855 /* This is a well-known MAC address; just add this to the Ethernet
857 add_eth_name(addr, name);
862 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
864 hash_idx = HASH_ETH_MANUF(addr);
866 tp = manuf_table[hash_idx];
869 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
872 if (tp->next == NULL) {
873 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
881 memcpy(tp->addr, addr, sizeof(tp->addr));
882 strncpy(tp->name, name, MAXMANUFLEN);
883 tp->name[MAXMANUFLEN-1] = '\0';
888 /* This is a range of well-known addresses; add it to the appropriate
889 well-known-address table, creating that table if necessary. */
890 wka_tp = wka_table[mask];
892 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
894 hash_idx = hash_eth_wka(addr, mask);
896 etp = (*wka_tp)[hash_idx];
899 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
902 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
903 /* address already known */
906 if (etp->next == NULL) {
907 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
915 memcpy(etp->addr, addr, sizeof(etp->addr));
916 strncpy(etp->name, name, MAXNAMELEN);
917 etp->name[MAXNAMELEN-1] = '\0';
919 etp->is_dummy_entry = FALSE;
921 } /* add_manuf_name */
923 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
928 hash_idx = HASH_ETH_MANUF(addr);
930 tp = manuf_table[hash_idx];
933 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
941 } /* manuf_name_lookup */
943 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
946 hashether_t *(*wka_tp)[HASHETHSIZE];
948 guint8 masked_addr[6];
952 wka_tp = wka_table[mask];
953 if (wka_tp == NULL) {
954 /* There are no entries in the table for that mask value, as there is
955 no table for that mask value. */
959 /* Get the part of the address covered by the mask. */
960 for (i = 0, num = mask; num >= 8; i++, num -= 8)
961 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
962 /* Mask out the first masked octet */
963 masked_addr[i] = addr[i] & (0xFF << (8 - num));
965 /* Zero out completely-masked-out octets */
969 hash_idx = hash_eth_wka(masked_addr, mask);
971 tp = (*wka_tp)[hash_idx];
974 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
982 } /* wka_name_lookup */
984 static void initialize_ethers(void)
990 /* Compute the pathname of the ethers file. */
991 if (g_ethers_path == NULL) {
992 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
993 get_systemfile_dir(), ENAME_ETHERS);
996 /* Set g_pethers_path here, but don't actually do anything
997 * with it. It's used in get_ethbyname() and get_ethbyaddr()
999 if (g_pethers_path == NULL)
1000 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1002 /* manuf hash table initialization */
1004 /* Compute the pathname of the manuf file */
1005 manuf_path = get_datafile_path(ENAME_MANUF);
1007 /* Read it and initialize the hash table */
1008 set_ethent(manuf_path);
1010 while ((eth = get_ethent(&mask, TRUE))) {
1011 add_manuf_name(eth->addr, mask, eth->name);
1018 } /* initialize_ethers */
1020 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1025 hash_idx = HASH_ETH_ADDRESS(addr);
1027 tp = eth_table[hash_idx];
1030 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1033 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1034 /* address already known */
1035 if (!tp->is_dummy_entry) {
1038 /* replace this dummy (manuf) entry with a real name */
1042 if (tp->next == NULL) {
1043 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1051 memcpy(tp->addr, addr, sizeof(tp->addr));
1052 strncpy(tp->name, name, MAXNAMELEN);
1053 tp->name[MAXNAMELEN-1] = '\0';
1055 tp->is_dummy_entry = FALSE;
1059 } /* add_eth_name */
1061 static gchar *eth_name_lookup(const guint8 *addr)
1064 hashmanuf_t *manufp;
1070 hash_idx = HASH_ETH_ADDRESS(addr);
1072 tp = eth_table[hash_idx];
1075 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1078 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1081 if (tp->next == NULL) {
1082 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1090 /* fill in a new entry */
1092 memcpy(tp->addr, addr, sizeof(tp->addr));
1095 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1096 /* Unknown name. Try looking for it in the well-known-address
1097 tables for well-known address ranges smaller than 2^24. */
1100 /* Only the topmost 5 bytes participate fully */
1101 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1102 sprintf(tp->name, "%s_%02x",
1103 etp->name, addr[5] & (0xFF >> mask));
1104 tp->is_dummy_entry = TRUE;
1114 /* Only the topmost 4 bytes participate fully */
1115 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1116 sprintf(tp->name, "%s_%02x:%02x",
1117 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1118 tp->is_dummy_entry = TRUE;
1128 /* Only the topmost 3 bytes participate fully */
1129 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1130 sprintf(tp->name, "%s_%02x:%02x:%02x",
1131 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1132 tp->is_dummy_entry = TRUE;
1140 /* Now try looking in the manufacturer table. */
1141 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1142 sprintf(tp->name, "%s_%02x:%02x:%02x",
1143 manufp->name, addr[3], addr[4], addr[5]);
1144 tp->is_dummy_entry = TRUE;
1148 /* Now try looking for it in the well-known-address
1149 tables for well-known address ranges larger than 2^24. */
1152 /* Only the topmost 2 bytes participate fully */
1153 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1154 sprintf(tp->name, "%s_%02x:%02x:%02x:%02x",
1155 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1157 tp->is_dummy_entry = TRUE;
1167 /* Only the topmost byte participates fully */
1168 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1169 sprintf(tp->name, "%s_%02x:%02x:%02x:%02x:%02x",
1170 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1172 tp->is_dummy_entry = TRUE;
1180 for (mask = 7; mask > 0; mask--) {
1181 /* Not even the topmost byte participates fully */
1182 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1183 sprintf(tp->name, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1184 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1185 addr[3], addr[4], addr[5]);
1186 tp->is_dummy_entry = TRUE;
1191 /* No match whatsoever. */
1192 sprintf(tp->name, "%s", ether_to_str(addr));
1193 tp->is_dummy_entry = TRUE;
1196 strncpy(tp->name, eth->name, MAXNAMELEN);
1197 tp->name[MAXNAMELEN-1] = '\0';
1198 tp->is_dummy_entry = FALSE;
1203 } /* eth_name_lookup */
1205 static guint8 *eth_addr_lookup(const gchar *name)
1209 hashether_t **table = eth_table;
1212 /* to be optimized (hash table from name to addr) */
1213 for (i = 0; i < HASHETHSIZE; i++) {
1216 if (strcmp(tp->name, name) == 0)
1222 /* not in hash table : performs a file lookup */
1224 if ((eth = get_ethbyname(name)) == NULL)
1227 /* add new entry in hash table */
1229 tp = add_eth_name(eth->addr, name);
1233 } /* eth_addr_lookup */
1237 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1240 * We allow three address separators (':', '-', and '.'),
1241 * as well as no separators
1245 guint32 a, a0, a1, a2, a3;
1246 gboolean found_single_number = FALSE;
1248 if ((cp = strchr(line, '#')))
1251 if ((cp = strtok(line, " \t\n")) == NULL)
1254 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1255 * fill a and found_single_number is TRUE,
1258 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1259 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1260 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1261 if (sscanf(cp, "%x", &a) == 1) {
1262 found_single_number = TRUE;
1271 if ((cp = strtok(NULL, " \t\n")) == NULL)
1274 if (found_single_number) {
1278 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1281 strncpy(ipxnet->name, cp, MAXNAMELEN);
1282 ipxnet->name[MAXNAMELEN-1] = '\0';
1286 } /* parse_ipxnets_line */
1288 static FILE *ipxnet_p = NULL;
1290 static void set_ipxnetent(char *path)
1295 ipxnet_p = fopen(path, "r");
1298 static void end_ipxnetent(void)
1306 static ipxnet_t *get_ipxnetent(void)
1309 static ipxnet_t ipxnet;
1310 static int size = 0;
1311 static char *buf = NULL;
1313 if (ipxnet_p == NULL)
1316 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1317 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1324 } /* get_ipxnetent */
1326 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1330 set_ipxnetent(g_ipxnets_path);
1332 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1335 if (ipxnet == NULL) {
1338 set_ipxnetent(g_pipxnets_path);
1340 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1348 } /* get_ipxnetbyname */
1350 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1355 set_ipxnetent(g_ipxnets_path);
1357 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1359 if (ipxnet == NULL) {
1362 set_ipxnetent(g_pipxnets_path);
1364 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1372 } /* get_ipxnetbyaddr */
1374 static void initialize_ipxnets(void)
1376 /* Compute the pathname of the ipxnets file.
1378 * XXX - is there a notion of an "ipxnets file" in any flavor of
1379 * UNIX, or with any add-on Netware package for UNIX? If not,
1380 * should the UNIX version of the ipxnets file be in the datafile
1381 * directory as well?
1383 if (g_ipxnets_path == NULL) {
1384 g_ipxnets_path = g_malloc(strlen(get_systemfile_dir()) +
1385 strlen(ENAME_IPXNETS) + 2);
1386 sprintf(g_ipxnets_path, "%s" G_DIR_SEPARATOR_S "%s",
1387 get_systemfile_dir(), ENAME_IPXNETS);
1390 /* Set g_pipxnets_path here, but don't actually do anything
1391 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1393 if (g_pipxnets_path == NULL)
1394 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1396 } /* initialize_ipxnets */
1398 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1403 hash_idx = HASH_IPX_NET(addr);
1405 tp = ipxnet_table[hash_idx];
1408 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1411 if (tp->next == NULL) {
1412 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1421 strncpy(tp->name, name, MAXNAMELEN);
1422 tp->name[MAXNAMELEN-1] = '\0';
1427 } /* add_ipxnet_name */
1429 static gchar *ipxnet_name_lookup(const guint addr)
1435 hash_idx = HASH_IPX_NET(addr);
1437 tp = ipxnet_table[hash_idx];
1440 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1443 if (tp->addr == addr) {
1446 if (tp->next == NULL) {
1447 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1455 /* fill in a new entry */
1460 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1462 sprintf(tp->name, "%X", addr);
1465 strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1466 tp->name[MAXNAMELEN-1] = '\0';
1471 } /* ipxnet_name_lookup */
1473 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1477 hashipxnet_t **table = ipxnet_table;
1480 /* to be optimized (hash table from name to addr) */
1481 for (i = 0; i < HASHIPXNETSIZE; i++) {
1484 if (strcmp(tp->name, name) == 0)
1490 /* not in hash table : performs a file lookup */
1492 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1497 /* add new entry in hash table */
1499 tp = add_ipxnet_name(ipxnet->addr, name);
1504 } /* ipxnet_addr_lookup */
1506 #ifdef HAVE_GNU_ADNS
1508 read_hosts_file (const char *hostspath)
1514 guint32 host_addr[4]; /* IPv4 or IPv6 */
1515 struct e_in6_addr ipv6_addr;
1520 * See the hosts(4) or hosts(5) man page for hosts file format
1521 * (not available on all systems).
1523 if ((hf = fopen(hostspath, "r")) == NULL)
1526 while (fgetline(&line, &size, hf) >= 0) {
1527 if ((cp = strchr(line, '#')))
1530 if ((cp = strtok(line, " \t")) == NULL)
1531 continue; /* no tokens in the line */
1533 ret = inet_pton(AF_INET6, cp, &host_addr);
1535 continue; /* error parsing */
1540 /* Not valid IPv6 - valid IPv4? */
1541 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1546 if ((cp = strtok(NULL, " \t")) == NULL)
1547 continue; /* no host name */
1550 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1551 add_ipv6_name(&ipv6_addr, cp);
1553 add_ipv4_name(host_addr[0], cp);
1556 * Add the aliases, too, if there are any.
1558 while ((cp = strtok(NULL, " \t")) != NULL) {
1560 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1561 add_ipv6_name(&ipv6_addr, cp);
1563 add_ipv4_name(host_addr[0], cp);
1571 } /* read_hosts_file */
1575 * External Functions
1578 #ifdef HAVE_GNU_ADNS
1581 host_name_lookup_init(void) {
1586 static char rootpath[] = "\\system32\\drivers\\etc\\hosts";
1590 sysroot = getenv("SYSTEMROOT");
1591 if (sysroot != NULL) {
1592 /* The file should be under SYSTEMROOT */
1593 hostspath = g_strconcat(sysroot, rootpath, NULL);
1594 read_hosts_file(hostspath);
1598 * This might be Windows OT (95/98/Me), or it might be NT
1599 * (NT 4.0/2K/XP/etc.) with SYSTEMROOT not set.
1601 * The hosts file is apparently in c:\windows\hosts in
1602 * OT, and under the system root in NT, but the system
1603 * root in NT might be c:\winnt or c:\windows (the latter
1604 * is claimed to be where it is in XP Home, presumably
1605 * for compatibility with OT).
1607 * We try all three of them until we either succeed or run
1610 * XXX - should we use WINDIR in this case, rather than
1611 * guessing? Should we use WINDIR in *all* cases?
1612 * If so, what should we use if WINDIR is null? Should
1613 * we just punt rather than using a default location?
1614 * Windows OT and NT put hosts in a different subdirectory;
1615 * does that mean we can't just use WINDIR, we need to
1616 * check whether it's OT or NT?
1618 if (!read_hosts_file("c:\\windows\\hosts")) {
1619 if (!read_hosts_file("c:\\winnt\\system32\\drivers\\etc\\hosts"))
1620 read_hosts_file("c:\\windows\\system32\\drivers\\etc\\hosts");
1624 read_hosts_file("/etc/hosts");
1627 /* XXX - Any flags we should be using? */
1628 /* XXX - We could provide config settings for DNS servers, and
1629 pass them to ADNS with adns_init_strcfg */
1630 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1632 * XXX - should we report the error? I'm assuming that some crashes
1633 * reported on a Windows machine with TCP/IP not configured are due
1634 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1635 * ADNS in a state where it crashes due to that. We'll still try
1636 * doing name resolution anyway.
1640 gnu_adns_initialized = TRUE;
1641 adns_currently_queued = 0;
1644 /* XXX - The ADNS "documentation" isn't very clear:
1645 * - Do we need to keep our query structures around?
1648 host_name_lookup_process(gpointer data _U_) {
1649 adns_queue_msg_t *almsg;
1651 char addr_str[] = "111.222.333.444.in-addr.arpa.";
1657 adns_queue_head = g_list_first(adns_queue_head);
1659 cur = adns_queue_head;
1660 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1661 almsg = (adns_queue_msg_t *) cur->data;
1662 if (! almsg->submitted && almsg->type == AF_INET) {
1663 addr_bytes = (guint8 *) &almsg->ip4_addr;
1664 sprintf(addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1665 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1666 /* XXX - what if it fails? */
1667 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1668 almsg->submitted = TRUE;
1669 adns_currently_queued++;
1674 cur = adns_queue_head;
1677 almsg = (adns_queue_msg_t *) cur->data;
1678 if (almsg->submitted) {
1679 ret = adns_check(ads, &almsg->query, &ans, NULL);
1681 if (ans->status == adns_s_ok) {
1682 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1689 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1691 adns_currently_queued--;
1695 /* Keep the timeout in place */
1700 host_name_lookup_cleanup(void) {
1703 adns_queue_head = g_list_first(adns_queue_head);
1704 while (adns_queue_head) {
1705 qdata = adns_queue_head->data;
1706 adns_queue_head = g_list_remove(adns_queue_head, qdata);
1710 if (gnu_adns_initialized)
1717 host_name_lookup_init(void) {
1721 host_name_lookup_process(gpointer data _U_) {
1722 /* Kill the timeout, as there's nothing for it to do */
1727 host_name_lookup_cleanup(void) {
1730 #endif /* HAVE_GNU_ADNS */
1732 extern gchar *get_hostname(guint addr)
1736 if (!(g_resolv_flags & RESOLV_NETWORK))
1737 return ip_to_str((guint8 *)&addr);
1739 return host_name_lookup(addr, &found);
1742 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1747 if (!(g_resolv_flags & RESOLV_NETWORK))
1748 return ip6_to_str(addr);
1749 #ifdef SOLARIS8_INET6
1750 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)addr) || IN6_IS_ADDR_MULTICAST((struct in6_addr*)addr))
1752 if (IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MULTICAST(addr))
1754 return ip6_to_str(addr);
1757 return host_name_lookup6(addr, &found);
1760 extern void add_ipv4_name(guint addr, const gchar *name)
1765 hash_idx = HASH_IPV4_ADDRESS(addr);
1767 tp = ipv4_table[hash_idx];
1770 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1773 if (tp->addr == addr) {
1774 /* address already known */
1775 if (!tp->is_dummy_entry) {
1778 /* replace this dummy entry with the new one */
1782 if (tp->next == NULL) {
1783 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1791 strncpy(tp->name, name, MAXNAMELEN);
1792 tp->name[MAXNAMELEN-1] = '\0';
1795 tp->is_dummy_entry = FALSE;
1797 } /* add_ipv4_name */
1799 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
1804 hash_idx = HASH_IPV6_ADDRESS(*addrp);
1806 tp = ipv6_table[hash_idx];
1809 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1812 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
1813 /* address already known */
1814 if (!tp->is_dummy_entry) {
1817 /* replace this dummy entry with the new one */
1821 if (tp->next == NULL) {
1822 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1830 strncpy(tp->name, name, MAXNAMELEN);
1831 tp->name[MAXNAMELEN-1] = '\0';
1834 tp->is_dummy_entry = FALSE;
1836 } /* add_ipv6_name */
1838 extern gchar *get_udp_port(guint port)
1840 static gchar str[3][MAXNAMELEN];
1843 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1844 if (cur == &str[0][0]) {
1846 } else if (cur == &str[1][0]) {
1851 sprintf(cur, "%u", port);
1855 return serv_name_lookup(port, PT_UDP);
1857 } /* get_udp_port */
1859 extern gchar *get_tcp_port(guint port)
1861 static gchar str[3][MAXNAMELEN];
1864 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1865 if (cur == &str[0][0]) {
1867 } else if (cur == &str[1][0]) {
1872 sprintf(cur, "%u", port);
1876 return serv_name_lookup(port, PT_TCP);
1878 } /* get_tcp_port */
1880 extern gchar *get_sctp_port(guint port)
1882 static gchar str[3][MAXNAMELEN];
1885 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1886 if (cur == &str[0][0]) {
1888 } else if (cur == &str[1][0]) {
1893 sprintf(cur, "%u", port);
1897 return serv_name_lookup(port, PT_SCTP);
1899 } /* get_sctp_port */
1901 const gchar *get_addr_name(address *addr)
1904 struct e_in6_addr ipv6_addr;
1906 switch (addr->type) {
1909 return get_ether_name(addr->data);
1912 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
1913 return get_hostname(ipv4_addr);
1916 memcpy(&ipv6_addr.s6_addr, addr->data, sizeof ipv6_addr.s6_addr);
1917 return get_hostname6(&ipv6_addr);
1927 extern gchar *get_ether_name(const guint8 *addr)
1929 if (!(g_resolv_flags & RESOLV_MAC))
1930 return ether_to_str(addr);
1932 if (!eth_resolution_initialized) {
1933 initialize_ethers();
1934 eth_resolution_initialized = 1;
1937 return eth_name_lookup(addr);
1939 } /* get_ether_name */
1941 /* Look for an ether name in the hash, and return it if found.
1942 * If it's not found, simply return NULL. We DO NOT make a new
1943 * hash entry for it with the hex digits turned into a string.
1945 gchar *get_ether_name_if_known(const guint8 *addr)
1950 /* Initialize ether structs if we're the first
1951 * ether-related function called */
1952 if (!(g_resolv_flags & RESOLV_MAC))
1955 if (!eth_resolution_initialized) {
1956 initialize_ethers();
1957 eth_resolution_initialized = 1;
1960 hash_idx = HASH_ETH_ADDRESS(addr);
1962 tp = eth_table[hash_idx];
1965 /* Hash key not found in table.
1966 * Force a lookup (and a hash entry) for addr, then call
1967 * myself. I plan on not getting into an infinite loop because
1968 * eth_name_lookup() is guaranteed to make a hashtable entry,
1969 * so when I call myself again, I can never get into this
1970 * block of code again. Knock on wood...
1972 (void) eth_name_lookup(addr);
1973 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1977 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1978 if (!tp->is_dummy_entry) {
1979 /* A name was found, and its origin is an ethers file */
1983 /* A name was found, but it was created, not found in a file */
1987 if (tp->next == NULL) {
1988 /* Read my reason above for why I'm sure I can't get into an infinite loop */
1989 (void) eth_name_lookup(addr);
1990 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1995 g_assert_not_reached();
2000 extern guint8 *get_ether_addr(const gchar *name)
2003 /* force resolution (do not check g_resolv_flags) */
2005 if (!eth_resolution_initialized) {
2006 initialize_ethers();
2007 eth_resolution_initialized = 1;
2010 return eth_addr_lookup(name);
2012 } /* get_ether_addr */
2014 extern void add_ether_byip(guint ip, const guint8 *eth)
2020 /* first check that IP address can be resolved */
2022 if ((host = host_name_lookup(ip, &found)) == NULL)
2025 /* ok, we can add this entry in the ethers hashtable */
2028 add_eth_name(eth, host);
2030 } /* add_ether_byip */
2032 extern const gchar *get_ipxnet_name(const guint32 addr)
2035 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2036 return ipxnet_to_str_punct(addr, '\0');
2039 if (!ipxnet_resolution_initialized) {
2040 initialize_ipxnets();
2041 ipxnet_resolution_initialized = 1;
2044 return ipxnet_name_lookup(addr);
2046 } /* get_ipxnet_name */
2048 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2053 /* force resolution (do not check g_resolv_flags) */
2055 if (!ipxnet_resolution_initialized) {
2056 initialize_ipxnets();
2057 ipxnet_resolution_initialized = 1;
2060 addr = ipxnet_addr_lookup(name, &success);
2065 } /* get_ipxnet_addr */
2067 extern const gchar *get_manuf_name(const guint8 *addr)
2069 static gchar str[3][MAXMANUFLEN];
2071 hashmanuf_t *manufp;
2073 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2074 initialize_ethers();
2075 eth_resolution_initialized = 1;
2078 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2079 if (cur == &str[0][0]) {
2081 } else if (cur == &str[1][0]) {
2086 sprintf(cur, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2090 return manufp->name;
2092 } /* get_manuf_name */
2096 /* Translate a string, assumed either to be a dotted-quad IP address or
2097 * a host name, to a numeric IP address. Return TRUE if we succeed and
2098 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2099 * Used more in the dfilter parser rather than in packet dissectors */
2100 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2102 struct in_addr ipaddr;
2106 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2107 * inet_pton(AF_INET) does not support hexadecimal notation nor
2108 * less-than-4 octet notation.
2110 if (!inet_aton(host, &ipaddr)) {
2111 /* It's not a valid dotted-quad IP address; is it a valid
2113 hp = gethostbyname(host);
2117 /* Apparently, some versions of gethostbyaddr can
2118 * return IPv6 addresses. */
2119 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2120 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2125 /* Does the string really contain dotted-quad IP?
2126 * Check against inet_atons that accept strings such as
2127 * "130.230" as valid addresses and try to convert them
2128 * to some form of a classful (host.net) notation.
2130 unsigned int a0, a1, a2, a3;
2131 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2135 *addrp = g_ntohl(ipaddr.s_addr);
2140 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2141 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2142 * return FALSE if we fail.
2144 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2148 if (inet_pton(AF_INET6, host, addrp) == 1)
2152 #ifdef HAVE_GETHOSTBYNAME2
2153 hp = gethostbyname2(host, AF_INET6);
2157 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2158 memcpy(addrp, hp->h_addr, hp->h_length);
2166 * Find out whether a hostname resolves to an ip or ipv6 address
2167 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2168 * that we don't know)
2170 const char* host_ip_af(const char *host
2171 #ifndef HAVE_GETHOSTBYNAME2
2176 #ifdef HAVE_GETHOSTBYNAME2
2178 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";