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.
38 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
39 * are most likely to take a long time, given the way address-to-name
40 * lookups are done over NBNS).
42 * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
43 * call in a signal handler, you might crash, because the state of the
44 * resolution code that sends messages to lookupd might be inconsistent
45 * if you jump out of it in middle of a call.
47 * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
48 * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
49 * call (and possibly in other gethostbyaddr() calls), because there's
50 * a mutex lock held in gethostbyaddr() and it doesn't get released
51 * if we longjmp out of it.
53 * There's no guarantee that longjmp()ing out of name resolution calls
54 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
55 * code in tcpdump, to avoid those sorts of problems, and that was
56 * picked up by tcpdump.org tcpdump.
58 * So, for now, we do not define AVOID_DNS_TIMEOUT. If we get a
59 * significantly more complaints about lookups taking a long time,
60 * we can reconsider that decision. (Note that tcpdump originally
61 * added that for the benefit of systems using NIS to look up host
62 * names; that might now be fixed in NIS implementations, for those
63 * sites still using NIS rather than DNS for that....)
70 #ifdef HAVE_NETINET_IN_H
71 # include <netinet/in.h>
78 #ifdef HAVE_ARPA_INET_H
79 #include <arpa/inet.h>
84 #ifdef HAVE_SYS_SOCKET_H
85 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
88 #ifdef HAVE_WINSOCK2_H
89 #include <winsock2.h> /* needed to define AF_ values on Windows */
92 #ifdef AVOID_DNS_TIMEOUT
96 #ifdef NEED_INET_ATON_H
97 # include "inet_aton.h"
100 #ifdef NEED_INET_V6DEFS_H
101 # include "inet_v6defs.h"
109 #if defined(_WIN32) && defined(INET6)
110 # include <ws2tcpip.h>
115 #include "report_err.h"
117 #include "ipv6-utils.h"
118 #include "addr_resolv.h"
119 #include "filesystem.h"
120 #include "file_util.h"
121 #include <epan/prefs.h>
122 #include <epan/emem.h>
124 #define ENAME_HOSTS "hosts"
125 #define ENAME_ETHERS "ethers"
126 #define ENAME_IPXNETS "ipxnets"
127 #define ENAME_MANUF "manuf"
129 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
130 #define HASHETHSIZE 1024
131 #define HASHHOSTSIZE 1024
132 #define HASHIPXNETSIZE 256
133 #define HASHMANUFSIZE 256
134 #define HASHPORTSIZE 256
136 /* hash table used for IPv4 lookup */
138 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
140 typedef struct hashipv4 {
142 gchar name[MAXNAMELEN];
143 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
144 struct hashipv4 *next;
147 /* hash table used for IPv6 lookup */
149 #define HASH_IPV6_ADDRESS(addr) \
150 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
152 typedef struct hashipv6 {
153 struct e_in6_addr addr;
154 gchar name[MAXNAMELEN];
155 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
156 struct hashipv6 *next;
159 /* hash table used for TCP/UDP/SCTP port lookup */
161 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
163 typedef struct hashport {
165 gchar name[MAXNAMELEN];
166 struct hashport *next;
169 /* hash table used for IPX network lookup */
171 /* XXX - check goodness of hash function */
173 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
175 typedef struct hashipxnet {
177 gchar name[MAXNAMELEN];
178 struct hashipxnet *next;
181 /* hash tables used for ethernet and manufacturer lookup */
183 #define HASH_ETH_ADDRESS(addr) \
184 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
187 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
189 typedef struct hashmanuf {
191 char name[MAXMANUFLEN];
192 struct hashmanuf *next;
195 typedef struct hashether {
197 char name[MAXNAMELEN];
198 gboolean is_dummy_entry; /* not a complete entry */
199 struct hashether *next;
202 /* internal ethernet type */
204 typedef struct _ether
207 char name[MAXNAMELEN];
210 /* internal ipxnet type */
212 typedef struct _ipxnet
215 char name[MAXNAMELEN];
218 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
219 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
220 static hashport_t *udp_port_table[HASHPORTSIZE];
221 static hashport_t *tcp_port_table[HASHPORTSIZE];
222 static hashport_t *sctp_port_table[HASHPORTSIZE];
223 static hashport_t *dccp_port_table[HASHPORTSIZE];
224 static hashether_t *eth_table[HASHETHSIZE];
225 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
226 static hashether_t *(*wka_table[48])[HASHETHSIZE];
227 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
229 static int eth_resolution_initialized = 0;
230 static int ipxnet_resolution_initialized = 0;
232 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
235 * Flag controlling what names to resolve.
237 guint32 g_resolv_flags;
240 * Global variables (can be changed in GUI sections)
241 * XXX - they could be changed in GUI code, but there's currently no
242 * GUI code to change them.
245 gchar *g_ethers_path = NULL; /* global ethers file */
246 gchar *g_pethers_path = NULL; /* personal ethers file */
247 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
248 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
249 /* first resolving call */
255 static gboolean gnu_adns_initialized = FALSE;
259 int adns_currently_queued = 0;
261 typedef struct _adns_queue_msg
265 struct e_in6_addr ip6_addr;
270 GList *adns_queue_head = NULL;
272 #endif /* HAVE_GNU_ADNS */
275 * Local function definitions
278 static gchar *serv_name_lookup(guint port, port_type proto)
283 const char *serv_proto = NULL;
284 struct servent *servp;
288 table = udp_port_table;
292 table = tcp_port_table;
296 table = sctp_port_table;
300 table = dccp_port_table;
304 /* not yet implemented */
310 hash_idx = HASH_PORT(port);
311 tp = table[hash_idx];
314 tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
317 if( tp->port == port ) {
320 if (tp->next == NULL) {
321 tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
329 /* fill in a new entry */
333 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
334 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
336 g_snprintf(tp->name, MAXNAMELEN, "%d", port);
338 strncpy(tp->name, servp->s_name, MAXNAMELEN);
339 tp->name[MAXNAMELEN-1] = '\0';
344 } /* serv_name_lookup */
347 #ifdef AVOID_DNS_TIMEOUT
349 #define DNS_TIMEOUT 2 /* max sec per call */
351 jmp_buf hostname_env;
353 static void abort_network_query(int sig _U_)
355 longjmp(hostname_env, 1);
357 #endif /* AVOID_DNS_TIMEOUT */
359 static gchar *host_name_lookup(guint addr, gboolean *found)
362 hashipv4_t * volatile tp;
363 struct hostent *hostp;
365 adns_queue_msg_t *qmsg;
370 hash_idx = HASH_IPV4_ADDRESS(addr);
372 tp = ipv4_table[hash_idx];
375 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
378 if( tp->addr == addr ) {
379 if (tp->is_dummy_entry)
383 if (tp->next == NULL) {
384 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
392 /* fill in a new entry */
397 if ((g_resolv_flags & RESOLV_CONCURRENT) &&
398 prefs.name_resolve_concurrency > 0 &&
399 gnu_adns_initialized) {
400 qmsg = g_malloc(sizeof(adns_queue_msg_t));
401 qmsg->type = AF_INET;
402 qmsg->ip4_addr = addr;
403 qmsg->submitted = FALSE;
404 adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
406 tp->is_dummy_entry = TRUE;
407 ip_to_str_buf((guint8 *)&addr, tp->name);
410 #endif /* HAVE_GNU_ADNS */
413 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
414 * the name of the host on which it's running; to work around that
415 * botch, we don't try to translate an all-zero IP address to a host
418 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
419 /* Use async DNS if possible, else fall back to timeouts,
420 * else call gethostbyaddr and hope for the best
423 # ifdef AVOID_DNS_TIMEOUT
425 /* Quick hack to avoid DNS/YP timeout */
427 if (!setjmp(hostname_env)) {
428 signal(SIGALRM, abort_network_query);
430 # endif /* AVOID_DNS_TIMEOUT */
432 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
434 # ifdef AVOID_DNS_TIMEOUT
436 # endif /* AVOID_DNS_TIMEOUT */
439 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
440 tp->name[MAXNAMELEN-1] = '\0';
441 tp->is_dummy_entry = FALSE;
445 # ifdef AVOID_DNS_TIMEOUT
447 # endif /* AVOID_DNS_TIMEOUT */
451 /* unknown host or DNS timeout */
453 ip_to_str_buf((guint8 *)&addr, tp->name);
454 tp->is_dummy_entry = TRUE;
459 } /* host_name_lookup */
461 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
464 hashipv6_t * volatile tp;
466 struct hostent *hostp;
471 hash_idx = HASH_IPV6_ADDRESS(*addr);
473 tp = ipv6_table[hash_idx];
476 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
479 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
480 if (tp->is_dummy_entry)
484 if (tp->next == NULL) {
485 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
493 /* fill in a new entry */
498 if (g_resolv_flags & RESOLV_NETWORK) {
499 #ifdef AVOID_DNS_TIMEOUT
501 /* Quick hack to avoid DNS/YP timeout */
503 if (!setjmp(hostname_env)) {
504 signal(SIGALRM, abort_network_query);
506 #endif /* AVOID_DNS_TIMEOUT */
507 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
508 #ifdef AVOID_DNS_TIMEOUT
510 # endif /* AVOID_DNS_TIMEOUT */
513 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
514 tp->name[MAXNAMELEN-1] = '\0';
515 tp->is_dummy_entry = FALSE;
519 #ifdef AVOID_DNS_TIMEOUT
521 # endif /* AVOID_DNS_TIMEOUT */
525 /* unknown host or DNS timeout */
527 ip6_to_str_buf(addr, tp->name);
528 tp->is_dummy_entry = TRUE;
532 } /* host_name_lookup6 */
534 static const gchar *solve_address_to_name(address *addr)
537 struct e_in6_addr ipv6_addr;
539 switch (addr->type) {
542 return get_ether_name(addr->data);
545 memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
546 return get_hostname(ipv4_addr);
549 memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
550 return get_hostname6(&ipv6_addr);
558 } /* solve_address_to_name */
562 * Miscellaneous functions
565 static int fgetline(char **buf, int *size, FILE *fp)
577 if ((*buf = g_malloc(*size)) == NULL)
585 while ((c = getc(fp)) != EOF && c != '\n') {
586 if (len+1 >= *size) {
587 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
593 if (len == 0 && c == EOF)
604 * Ethernet / manufacturer resolution
606 * The following functions implement ethernet address resolution and
607 * ethers files parsing (see ethers(4)).
609 * The manuf file has the same format as ethers(4) except that names are
610 * truncated to MAXMANUFLEN-1 characters and that an address contains
611 * only 3 bytes (instead of 6).
615 * I decide to not use the existing functions (see ethers(3) on some
616 * operating systems) for the following reasons:
617 * - performance gains (use of hash tables and some other enhancements),
618 * - use of two ethers files (system-wide and per user),
619 * - avoid the use of NIS maps,
620 * - lack of these functions on some systems.
622 * So the following functions do _not_ behave as the standard ones.
629 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
630 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
634 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
642 for (i = 0; i < 6; i++) {
643 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
644 if (!isxdigit((unsigned char)*cp))
646 num = strtoul(cp, &p, 16);
648 return FALSE; /* failed */
650 return FALSE; /* not a valid octet */
651 eth->addr[i] = (guint8) num;
652 cp = p; /* skip past the number */
654 /* OK, what character terminated the octet? */
656 /* "/" - this has a mask. */
658 /* Entries with masks are allowed only in the "manuf" files. */
661 cp++; /* skip past the '/' to get to the mask */
662 if (!isdigit((unsigned char)*cp))
663 return FALSE; /* no sign allowed */
664 num = strtoul(cp, &p, 10);
666 return FALSE; /* failed */
667 cp = p; /* skip past the number */
668 if (*cp != '\0' && !isspace((unsigned char)*cp))
669 return FALSE; /* bogus terminator */
670 if (num == 0 || num >= 48)
671 return FALSE; /* bogus mask */
672 /* Mask out the bits not covered by the mask */
674 for (i = 0; num >= 8; i++, num -= 8)
675 ; /* skip octets entirely covered by the mask */
676 /* Mask out the first masked octet */
677 eth->addr[i] &= (0xFF << (8 - num));
679 /* Mask out completely-masked-out octets */
685 /* We're at the end of the address, and there's no mask. */
687 /* We got 3 bytes, so this is a manufacturer ID. */
689 /* Manufacturer IDs are only allowed in the "manuf"
693 /* Indicate that this is a manufacturer ID (0 is not allowed
700 /* We got 6 bytes, so this is a MAC address.
701 If we're reading one of the "manuf" files, indicate that
702 this is a MAC address (48 is not allowed as a mask). */
708 /* We didn't get 3 or 6 bytes, and there's no mask; this is
713 /* We don't know the separator used in this number; it can either
714 be ':', '-', or '.'. */
715 if (*cp != ':' && *cp != '-' && *cp != '.')
717 sep = *cp; /* subsequent separators must be the same */
719 /* It has to be the same as the first separator */
730 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
734 * See the ethers(4) or ethers(5) man page for ethers file format
735 * (not available on all systems).
736 * We allow both ethernet address separators (':' and '-'),
737 * as well as Ethereal's '.' separator.
742 if ((cp = strchr(line, '#')))
745 if ((cp = strtok(line, " \t")) == NULL)
748 if (!parse_ether_address(cp, eth, mask, manuf_file))
751 if ((cp = strtok(NULL, " \t")) == NULL)
754 strncpy(eth->name, cp, MAXNAMELEN);
755 eth->name[MAXNAMELEN-1] = '\0';
759 } /* parse_ether_line */
761 static FILE *eth_p = NULL;
763 static void set_ethent(char *path)
768 eth_p = eth_fopen(path, "r");
771 static void end_ethent(void)
779 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
784 static char *buf = NULL;
789 while (fgetline(&buf, &size, eth_p) >= 0) {
790 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
799 static ether_t *get_ethbyname(const gchar *name)
803 set_ethent(g_pethers_path);
805 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
811 set_ethent(g_ethers_path);
813 while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
821 } /* get_ethbyname */
823 static ether_t *get_ethbyaddr(const guint8 *addr)
828 set_ethent(g_pethers_path);
830 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
836 set_ethent(g_ethers_path);
838 while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
846 } /* get_ethbyaddr */
848 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
851 /* All but the topmost byte is masked out */
852 return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
856 /* All but the topmost 2 bytes are masked out */
857 return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
862 /* All but the topmost 3 bytes are masked out */
863 return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
868 /* All but the topmost 4 bytes are masked out */
869 return ((((addr[0] << 8) | addr[1]) ^
870 ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
875 /* All but the topmost 5 bytes are masked out */
876 return ((((addr[1] << 8) | addr[2]) ^
877 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
881 /* No bytes are fully masked out */
882 return ((((addr[1] << 8) | addr[2]) ^
883 ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
887 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
891 hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
894 /* This is a well-known MAC address; just add this to the Ethernet
896 add_eth_name(addr, name);
901 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
903 hash_idx = HASH_ETH_MANUF(addr);
905 tp = manuf_table[hash_idx];
908 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
911 if (tp->next == NULL) {
912 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
920 memcpy(tp->addr, addr, sizeof(tp->addr));
921 strncpy(tp->name, name, MAXMANUFLEN);
922 tp->name[MAXMANUFLEN-1] = '\0';
927 /* This is a range of well-known addresses; add it to the appropriate
928 well-known-address table, creating that table if necessary. */
929 wka_tp = wka_table[mask];
931 wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
933 hash_idx = hash_eth_wka(addr, mask);
935 etp = (*wka_tp)[hash_idx];
938 etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
941 if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
942 /* address already known */
945 if (etp->next == NULL) {
946 etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
954 memcpy(etp->addr, addr, sizeof(etp->addr));
955 strncpy(etp->name, name, MAXNAMELEN);
956 etp->name[MAXNAMELEN-1] = '\0';
958 etp->is_dummy_entry = FALSE;
960 } /* add_manuf_name */
962 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
967 hash_idx = HASH_ETH_MANUF(addr);
969 tp = manuf_table[hash_idx];
972 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
980 } /* manuf_name_lookup */
982 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
985 hashether_t *(*wka_tp)[HASHETHSIZE];
987 guint8 masked_addr[6];
991 wka_tp = wka_table[mask];
992 if (wka_tp == NULL) {
993 /* There are no entries in the table for that mask value, as there is
994 no table for that mask value. */
998 /* Get the part of the address covered by the mask. */
999 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1000 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1001 /* Mask out the first masked octet */
1002 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1004 /* Zero out completely-masked-out octets */
1008 hash_idx = hash_eth_wka(masked_addr, mask);
1010 tp = (*wka_tp)[hash_idx];
1013 if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1021 } /* wka_name_lookup */
1023 static void initialize_ethers(void)
1029 /* Compute the pathname of the ethers file. */
1030 if (g_ethers_path == NULL) {
1031 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1032 get_systemfile_dir(), ENAME_ETHERS);
1035 /* Set g_pethers_path here, but don't actually do anything
1036 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1038 if (g_pethers_path == NULL)
1039 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1041 /* manuf hash table initialization */
1043 /* Compute the pathname of the manuf file */
1044 manuf_path = get_datafile_path(ENAME_MANUF);
1046 /* Read it and initialize the hash table */
1047 set_ethent(manuf_path);
1049 while ((eth = get_ethent(&mask, TRUE))) {
1050 add_manuf_name(eth->addr, mask, eth->name);
1057 } /* initialize_ethers */
1059 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1064 hash_idx = HASH_ETH_ADDRESS(addr);
1066 tp = eth_table[hash_idx];
1069 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1072 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1073 /* address already known */
1074 if (!tp->is_dummy_entry) {
1077 /* replace this dummy (manuf) entry with a real name */
1081 if (tp->next == NULL) {
1082 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1090 memcpy(tp->addr, addr, sizeof(tp->addr));
1091 strncpy(tp->name, name, MAXNAMELEN);
1092 tp->name[MAXNAMELEN-1] = '\0';
1094 tp->is_dummy_entry = FALSE;
1098 } /* add_eth_name */
1100 static gchar *eth_name_lookup(const guint8 *addr)
1103 hashmanuf_t *manufp;
1109 hash_idx = HASH_ETH_ADDRESS(addr);
1111 tp = eth_table[hash_idx];
1114 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1117 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1120 if (tp->next == NULL) {
1121 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1129 /* fill in a new entry */
1131 memcpy(tp->addr, addr, sizeof(tp->addr));
1134 if ( (eth = get_ethbyaddr(addr)) == NULL) {
1135 /* Unknown name. Try looking for it in the well-known-address
1136 tables for well-known address ranges smaller than 2^24. */
1139 /* Only the topmost 5 bytes participate fully */
1140 if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1141 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1142 etp->name, addr[5] & (0xFF >> mask));
1143 tp->is_dummy_entry = TRUE;
1153 /* Only the topmost 4 bytes participate fully */
1154 if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1155 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1156 etp->name, addr[4] & (0xFF >> mask), addr[5]);
1157 tp->is_dummy_entry = TRUE;
1167 /* Only the topmost 3 bytes participate fully */
1168 if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1169 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1170 etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1171 tp->is_dummy_entry = TRUE;
1179 /* Now try looking in the manufacturer table. */
1180 if ((manufp = manuf_name_lookup(addr)) != NULL) {
1181 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1182 manufp->name, addr[3], addr[4], addr[5]);
1183 tp->is_dummy_entry = TRUE;
1187 /* Now try looking for it in the well-known-address
1188 tables for well-known address ranges larger than 2^24. */
1191 /* Only the topmost 2 bytes participate fully */
1192 if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1193 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1194 etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1196 tp->is_dummy_entry = TRUE;
1206 /* Only the topmost byte participates fully */
1207 if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1208 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1209 etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1211 tp->is_dummy_entry = TRUE;
1219 for (mask = 7; mask > 0; mask--) {
1220 /* Not even the topmost byte participates fully */
1221 if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1222 g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1223 etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1224 addr[3], addr[4], addr[5]);
1225 tp->is_dummy_entry = TRUE;
1230 /* No match whatsoever. */
1231 g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1232 tp->is_dummy_entry = TRUE;
1235 strncpy(tp->name, eth->name, MAXNAMELEN);
1236 tp->name[MAXNAMELEN-1] = '\0';
1237 tp->is_dummy_entry = FALSE;
1242 } /* eth_name_lookup */
1244 static guint8 *eth_addr_lookup(const gchar *name)
1248 hashether_t **table = eth_table;
1251 /* to be optimized (hash table from name to addr) */
1252 for (i = 0; i < HASHETHSIZE; i++) {
1255 if (strcmp(tp->name, name) == 0)
1261 /* not in hash table : performs a file lookup */
1263 if ((eth = get_ethbyname(name)) == NULL)
1266 /* add new entry in hash table */
1268 tp = add_eth_name(eth->addr, name);
1272 } /* eth_addr_lookup */
1276 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1279 * We allow three address separators (':', '-', and '.'),
1280 * as well as no separators
1284 guint32 a, a0, a1, a2, a3;
1285 gboolean found_single_number = FALSE;
1287 if ((cp = strchr(line, '#')))
1290 if ((cp = strtok(line, " \t\n")) == NULL)
1293 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1294 * fill a and found_single_number is TRUE,
1297 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1298 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1299 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1300 if (sscanf(cp, "%x", &a) == 1) {
1301 found_single_number = TRUE;
1310 if ((cp = strtok(NULL, " \t\n")) == NULL)
1313 if (found_single_number) {
1317 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1320 strncpy(ipxnet->name, cp, MAXNAMELEN);
1321 ipxnet->name[MAXNAMELEN-1] = '\0';
1325 } /* parse_ipxnets_line */
1327 static FILE *ipxnet_p = NULL;
1329 static void set_ipxnetent(char *path)
1334 ipxnet_p = eth_fopen(path, "r");
1337 static void end_ipxnetent(void)
1345 static ipxnet_t *get_ipxnetent(void)
1348 static ipxnet_t ipxnet;
1349 static int size = 0;
1350 static char *buf = NULL;
1352 if (ipxnet_p == NULL)
1355 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1356 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1363 } /* get_ipxnetent */
1365 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1369 set_ipxnetent(g_ipxnets_path);
1371 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1374 if (ipxnet == NULL) {
1377 set_ipxnetent(g_pipxnets_path);
1379 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1387 } /* get_ipxnetbyname */
1389 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1394 set_ipxnetent(g_ipxnets_path);
1396 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1398 if (ipxnet == NULL) {
1401 set_ipxnetent(g_pipxnets_path);
1403 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1411 } /* get_ipxnetbyaddr */
1413 static void initialize_ipxnets(void)
1415 /* Compute the pathname of the ipxnets file.
1417 * XXX - is there a notion of an "ipxnets file" in any flavor of
1418 * UNIX, or with any add-on Netware package for UNIX? If not,
1419 * should the UNIX version of the ipxnets file be in the datafile
1420 * directory as well?
1422 if (g_ipxnets_path == NULL) {
1423 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1424 get_systemfile_dir(), ENAME_IPXNETS);
1427 /* Set g_pipxnets_path here, but don't actually do anything
1428 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1430 if (g_pipxnets_path == NULL)
1431 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1433 } /* initialize_ipxnets */
1435 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1440 hash_idx = HASH_IPX_NET(addr);
1442 tp = ipxnet_table[hash_idx];
1445 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1448 if (tp->next == NULL) {
1449 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1458 strncpy(tp->name, name, MAXNAMELEN);
1459 tp->name[MAXNAMELEN-1] = '\0';
1464 } /* add_ipxnet_name */
1466 static gchar *ipxnet_name_lookup(const guint addr)
1472 hash_idx = HASH_IPX_NET(addr);
1474 tp = ipxnet_table[hash_idx];
1477 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1480 if (tp->addr == addr) {
1483 if (tp->next == NULL) {
1484 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1492 /* fill in a new entry */
1497 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1499 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1502 strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1503 tp->name[MAXNAMELEN-1] = '\0';
1508 } /* ipxnet_name_lookup */
1510 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1514 hashipxnet_t **table = ipxnet_table;
1517 /* to be optimized (hash table from name to addr) */
1518 for (i = 0; i < HASHIPXNETSIZE; i++) {
1521 if (strcmp(tp->name, name) == 0) {
1529 /* not in hash table : performs a file lookup */
1531 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1536 /* add new entry in hash table */
1538 tp = add_ipxnet_name(ipxnet->addr, name);
1543 } /* ipxnet_addr_lookup */
1546 read_hosts_file (const char *hostspath)
1552 guint32 host_addr[4]; /* IPv4 or IPv6 */
1553 struct e_in6_addr ipv6_addr;
1558 * See the hosts(4) or hosts(5) man page for hosts file format
1559 * (not available on all systems).
1561 if ((hf = eth_fopen(hostspath, "r")) == NULL)
1564 while (fgetline(&line, &size, hf) >= 0) {
1565 if ((cp = strchr(line, '#')))
1568 if ((cp = strtok(line, " \t")) == NULL)
1569 continue; /* no tokens in the line */
1571 ret = inet_pton(AF_INET6, cp, &host_addr);
1573 continue; /* error parsing */
1578 /* Not valid IPv6 - valid IPv4? */
1579 if (inet_pton(AF_INET, cp, &host_addr) != 1)
1584 if ((cp = strtok(NULL, " \t")) == NULL)
1585 continue; /* no host name */
1588 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1589 add_ipv6_name(&ipv6_addr, cp);
1591 add_ipv4_name(host_addr[0], cp);
1594 * Add the aliases, too, if there are any.
1596 while ((cp = strtok(NULL, " \t")) != NULL) {
1598 memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1599 add_ipv6_name(&ipv6_addr, cp);
1601 add_ipv4_name(host_addr[0], cp);
1609 } /* read_hosts_file */
1612 * External Functions
1616 host_name_lookup_init(void) {
1619 #ifdef HAVE_GNU_ADNS
1622 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
1623 static char rootpath_ot[] = "\\hosts";
1625 #endif /*GNU_ADNS */
1628 * Load the user's hosts file, if they have one.
1630 hostspath = get_persconffile_path(ENAME_HOSTS, FALSE);
1631 if (!read_hosts_file(hostspath) && errno != ENOENT) {
1632 report_open_failure(hostspath, errno, FALSE);
1636 #ifdef HAVE_GNU_ADNS
1638 * We're using GNU ADNS, which doesn't check the system hosts file;
1639 * we load that file ourselves.
1643 sysroot = getenv("WINDIR");
1644 if (sysroot != NULL) {
1646 * The file should be under WINDIR.
1647 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
1648 * %WINDIR%\system32\drivers\etc\hosts.
1649 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
1651 * XXX - should we base it on the dwPlatformId value from
1654 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
1655 if (!read_hosts_file(hostspath)) {
1657 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
1658 read_hosts_file(hostspath);
1663 read_hosts_file("/etc/hosts");
1666 /* XXX - Any flags we should be using? */
1667 /* XXX - We could provide config settings for DNS servers, and
1668 pass them to ADNS with adns_init_strcfg */
1669 if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1671 * XXX - should we report the error? I'm assuming that some crashes
1672 * reported on a Windows machine with TCP/IP not configured are due
1673 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1674 * ADNS in a state where it crashes due to that. We'll still try
1675 * doing name resolution anyway.
1679 gnu_adns_initialized = TRUE;
1680 adns_currently_queued = 0;
1681 #endif /* HAVE_GNU_ADNS */
1684 #ifdef HAVE_GNU_ADNS
1686 /* XXX - The ADNS "documentation" isn't very clear:
1687 * - Do we need to keep our query structures around?
1690 host_name_lookup_process(gpointer data _U_) {
1691 adns_queue_msg_t *almsg;
1693 char addr_str[] = "111.222.333.444.in-addr.arpa.";
1699 adns_queue_head = g_list_first(adns_queue_head);
1701 cur = adns_queue_head;
1702 while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1703 almsg = (adns_queue_msg_t *) cur->data;
1704 if (! almsg->submitted && almsg->type == AF_INET) {
1705 addr_bytes = (guint8 *) &almsg->ip4_addr;
1706 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1707 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1708 /* XXX - what if it fails? */
1709 adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1710 almsg->submitted = TRUE;
1711 adns_currently_queued++;
1716 cur = adns_queue_head;
1719 almsg = (adns_queue_msg_t *) cur->data;
1720 if (almsg->submitted) {
1721 ret = adns_check(ads, &almsg->query, &ans, NULL);
1723 if (ans->status == adns_s_ok) {
1724 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1731 adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1733 adns_currently_queued--;
1737 /* Keep the timeout in place */
1742 host_name_lookup_cleanup(void) {
1745 adns_queue_head = g_list_first(adns_queue_head);
1746 while (adns_queue_head) {
1747 qdata = adns_queue_head->data;
1748 adns_queue_head = g_list_remove(adns_queue_head, qdata);
1752 if (gnu_adns_initialized)
1759 host_name_lookup_process(gpointer data _U_) {
1760 /* Kill the timeout, as there's nothing for it to do */
1765 host_name_lookup_cleanup(void) {
1768 #endif /* HAVE_GNU_ADNS */
1770 extern gchar *get_hostname(guint addr)
1774 if (!(g_resolv_flags & RESOLV_NETWORK))
1775 return ip_to_str((guint8 *)&addr);
1777 return host_name_lookup(addr, &found);
1780 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1784 if (!(g_resolv_flags & RESOLV_NETWORK))
1785 return ip6_to_str(addr);
1786 if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
1787 return ip6_to_str(addr);
1788 return host_name_lookup6(addr, &found);
1791 extern void add_ipv4_name(guint addr, const gchar *name)
1796 hash_idx = HASH_IPV4_ADDRESS(addr);
1798 tp = ipv4_table[hash_idx];
1801 tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1804 if (tp->addr == addr) {
1805 /* address already known */
1806 if (!tp->is_dummy_entry) {
1809 /* replace this dummy entry with the new one */
1813 if (tp->next == NULL) {
1814 tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1822 strncpy(tp->name, name, MAXNAMELEN);
1823 tp->name[MAXNAMELEN-1] = '\0';
1826 tp->is_dummy_entry = FALSE;
1828 } /* add_ipv4_name */
1830 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
1835 hash_idx = HASH_IPV6_ADDRESS(*addrp);
1837 tp = ipv6_table[hash_idx];
1840 tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1843 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
1844 /* address already known */
1845 if (!tp->is_dummy_entry) {
1848 /* replace this dummy entry with the new one */
1852 if (tp->next == NULL) {
1853 tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
1861 strncpy(tp->name, name, MAXNAMELEN);
1862 tp->name[MAXNAMELEN-1] = '\0';
1865 tp->is_dummy_entry = FALSE;
1867 } /* add_ipv6_name */
1869 extern gchar *get_udp_port(guint port)
1873 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1874 cur=ep_alloc(MAXNAMELEN);
1875 g_snprintf(cur, MAXNAMELEN, "%u", port);
1879 return serv_name_lookup(port, PT_UDP);
1881 } /* get_udp_port */
1883 extern gchar *get_dccp_port(guint port)
1887 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1888 cur=ep_alloc(MAXNAMELEN);
1889 g_snprintf(cur, MAXNAMELEN, "%u", port);
1893 return serv_name_lookup(port, PT_DCCP);
1895 } /* get_dccp_port */
1898 extern gchar *get_tcp_port(guint port)
1902 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1903 cur=ep_alloc(MAXNAMELEN);
1904 g_snprintf(cur, MAXNAMELEN, "%u", port);
1908 return serv_name_lookup(port, PT_TCP);
1910 } /* get_tcp_port */
1912 extern gchar *get_sctp_port(guint port)
1916 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1917 cur=ep_alloc(MAXNAMELEN);
1918 g_snprintf(cur, MAXNAMELEN, "%u", port);
1922 return serv_name_lookup(port, PT_SCTP);
1924 } /* get_sctp_port */
1927 const gchar *get_addr_name(address *addr)
1929 const gchar *result;
1931 result = solve_address_to_name(addr);
1937 /* if it gets here, either it is of type AT_NONE, */
1938 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
1940 if (addr->type == AT_NONE){
1944 return(address_to_str(addr));
1945 } /* get_addr_name */
1948 void get_addr_name_buf(address *addr, gchar *buf, guint size)
1950 const gchar *result;
1952 result = get_addr_name(addr);
1954 strncpy(buf,result,size);
1958 } /* get_addr_name_buf */
1961 extern gchar *get_ether_name(const guint8 *addr)
1963 if (!(g_resolv_flags & RESOLV_MAC))
1964 return ether_to_str(addr);
1966 if (!eth_resolution_initialized) {
1967 initialize_ethers();
1968 eth_resolution_initialized = 1;
1971 return eth_name_lookup(addr);
1973 } /* get_ether_name */
1975 /* Look for an ether name in the hash, and return it if found.
1976 * If it's not found, simply return NULL. We DO NOT make a new
1977 * hash entry for it with the hex digits turned into a string.
1979 gchar *get_ether_name_if_known(const guint8 *addr)
1984 /* Initialize ether structs if we're the first
1985 * ether-related function called */
1986 if (!(g_resolv_flags & RESOLV_MAC))
1989 if (!eth_resolution_initialized) {
1990 initialize_ethers();
1991 eth_resolution_initialized = 1;
1994 hash_idx = HASH_ETH_ADDRESS(addr);
1996 tp = eth_table[hash_idx];
1999 /* Hash key not found in table.
2000 * Force a lookup (and a hash entry) for addr, then call
2001 * myself. I plan on not getting into an infinite loop because
2002 * eth_name_lookup() is guaranteed to make a hashtable entry,
2003 * so when I call myself again, I can never get into this
2004 * block of code again. Knock on wood...
2006 (void) eth_name_lookup(addr);
2007 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2011 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2012 if (!tp->is_dummy_entry) {
2013 /* A name was found, and its origin is an ethers file */
2017 /* A name was found, but it was created, not found in a file */
2021 if (tp->next == NULL) {
2022 /* Read my reason above for why I'm sure I can't get into an infinite loop */
2023 (void) eth_name_lookup(addr);
2024 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2029 g_assert_not_reached();
2034 extern guint8 *get_ether_addr(const gchar *name)
2037 /* force resolution (do not check g_resolv_flags) */
2039 if (!eth_resolution_initialized) {
2040 initialize_ethers();
2041 eth_resolution_initialized = 1;
2044 return eth_addr_lookup(name);
2046 } /* get_ether_addr */
2048 extern void add_ether_byip(guint ip, const guint8 *eth)
2054 /* first check that IP address can be resolved */
2056 if ((host = host_name_lookup(ip, &found)) == NULL)
2059 /* ok, we can add this entry in the ethers hashtable */
2062 add_eth_name(eth, host);
2064 } /* add_ether_byip */
2066 extern const gchar *get_ipxnet_name(const guint32 addr)
2069 if (!(g_resolv_flags & RESOLV_NETWORK)) {
2070 return ipxnet_to_str_punct(addr, '\0');
2073 if (!ipxnet_resolution_initialized) {
2074 initialize_ipxnets();
2075 ipxnet_resolution_initialized = 1;
2078 return ipxnet_name_lookup(addr);
2080 } /* get_ipxnet_name */
2082 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2087 /* force resolution (do not check g_resolv_flags) */
2089 if (!ipxnet_resolution_initialized) {
2090 initialize_ipxnets();
2091 ipxnet_resolution_initialized = 1;
2094 addr = ipxnet_addr_lookup(name, &success);
2099 } /* get_ipxnet_addr */
2101 extern const gchar *get_manuf_name(const guint8 *addr)
2104 hashmanuf_t *manufp;
2106 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2107 initialize_ethers();
2108 eth_resolution_initialized = 1;
2111 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2112 cur=ep_alloc(MAXMANUFLEN);
2113 g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2117 return manufp->name;
2119 } /* get_manuf_name */
2122 const gchar *get_manuf_name_if_known(const guint8 *addr)
2124 hashmanuf_t *manufp;
2126 if (!eth_resolution_initialized) {
2127 initialize_ethers();
2128 eth_resolution_initialized = 1;
2131 if ((manufp = manuf_name_lookup(addr)) == NULL) {
2135 return manufp->name;
2137 } /* get_manuf_name_if_known */
2140 /* Translate a string, assumed either to be a dotted-quad IP address or
2141 * a host name, to a numeric IP address. Return TRUE if we succeed and
2142 * set "*addrp" to that numeric IP address; return FALSE if we fail.
2143 * Used more in the dfilter parser rather than in packet dissectors */
2144 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2146 struct in_addr ipaddr;
2150 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2151 * inet_pton(AF_INET) does not support hexadecimal notation nor
2152 * less-than-4 octet notation.
2154 if (!inet_aton(host, &ipaddr)) {
2155 /* It's not a valid dotted-quad IP address; is it a valid
2157 hp = gethostbyname(host);
2161 /* Apparently, some versions of gethostbyaddr can
2162 * return IPv6 addresses. */
2163 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2164 memcpy(&ipaddr, hp->h_addr, hp->h_length);
2169 /* Does the string really contain dotted-quad IP?
2170 * Check against inet_atons that accept strings such as
2171 * "130.230" as valid addresses and try to convert them
2172 * to some form of a classful (host.net) notation.
2174 unsigned int a0, a1, a2, a3;
2175 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2179 *addrp = g_ntohl(ipaddr.s_addr);
2184 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2185 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2186 * return FALSE if we fail.
2188 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2192 if (inet_pton(AF_INET6, host, addrp) == 1)
2196 #ifdef HAVE_GETHOSTBYNAME2
2197 hp = gethostbyname2(host, AF_INET6);
2201 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2202 memcpy(addrp, hp->h_addr, hp->h_length);
2210 * Find out whether a hostname resolves to an ip or ipv6 address
2211 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2212 * that we don't know)
2214 const char* host_ip_af(const char *host
2215 #ifndef HAVE_GETHOSTBYNAME2
2220 #ifdef HAVE_GETHOSTBYNAME2
2222 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";