2 * Routines for network object lookup
4 * $Id: resolv.c,v 1.24 2002/08/02 21:29:40 jmayer Exp $
6 * Laurent Deniel <deniel@worldnet.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.
36 #ifndef AVOID_DNS_TIMEOUT
37 #define AVOID_DNS_TIMEOUT
45 #ifdef HAVE_NETINET_IN_H
46 # include <netinet/in.h>
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
63 #ifdef AVOID_DNS_TIMEOUT
67 #ifdef NEED_INET_ATON_H
68 # include "inet_aton.h"
71 #ifdef NEED_INET_V6DEFS_H
72 # include "inet_v6defs.h"
76 #include "ipv6-utils.h"
78 #include "filesystem.h"
80 #define ENAME_ETHERS "ethers"
81 #define ENAME_IPXNETS "ipxnets"
82 #define ENAME_MANUF "manuf"
84 #define MAXMANUFLEN 9 /* max vendor name length with ending '\0' */
85 #define HASHETHSIZE 1024
86 #define HASHHOSTSIZE 1024
87 #define HASHIPXNETSIZE 256
88 #define HASHMANUFSIZE 256
89 #define HASHPORTSIZE 256
91 /* hash table used for host and port lookup */
93 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
95 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
97 typedef struct hashname {
99 guchar name[MAXNAMELEN];
100 gboolean is_dummy_entry; /* name is IP address in dot format */
101 struct hashname *next;
104 /* hash table used for IPX network lookup */
106 /* XXX - check goodness of hash function */
108 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
110 typedef struct hashname hashipxnet_t;
112 /* hash tables used for ethernet and manufacturer lookup */
114 #define HASH_ETH_ADDRESS(addr) \
115 (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
118 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
120 typedef struct hashmanuf {
122 char name[MAXMANUFLEN];
123 struct hashmanuf *next;
126 typedef struct hashether {
128 char name[MAXNAMELEN];
129 gboolean is_dummy_entry; /* not a complete entry */
130 struct hashether *next;
133 /* internal ethernet type */
135 typedef struct _ether
138 char name[MAXNAMELEN];
141 /* internal ipxnet type */
143 typedef struct _ipxnet
146 char name[MAXNAMELEN];
149 static hashname_t *host_table[HASHHOSTSIZE];
150 static hashname_t *udp_port_table[HASHPORTSIZE];
151 static hashname_t *tcp_port_table[HASHPORTSIZE];
152 static hashname_t *sctp_port_table[HASHPORTSIZE];
153 static hashether_t *eth_table[HASHETHSIZE];
154 static hashmanuf_t *manuf_table[HASHMANUFSIZE];
155 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
157 static int eth_resolution_initialized = 0;
158 static int ipxnet_resolution_initialized = 0;
161 * Flag controlling what names to resolve.
163 guint32 g_resolv_flags;
166 * Global variables (can be changed in GUI sections)
167 * XXX - they could be changed in GUI code, but there's currently no
168 * GUI code to change them.
171 gchar *g_ethers_path = NULL; /* global ethers file */
172 gchar *g_pethers_path = NULL; /* personal ethers file */
173 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
174 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
175 /* first resolving call */
178 * Local function definitions
181 static guchar *serv_name_lookup(guint port, port_type proto)
186 char *serv_proto = NULL;
187 struct servent *servp;
191 table = udp_port_table;
195 table = tcp_port_table;
199 table = sctp_port_table;
203 /* not yet implemented */
209 hash_idx = HASH_PORT(port);
210 tp = table[hash_idx];
213 tp = table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
216 if( tp->addr == port ) {
219 if (tp->next == NULL) {
220 tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
228 /* fill in a new entry */
232 if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
233 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
235 sprintf(tp->name, "%d", port);
237 strncpy(tp->name, servp->s_name, MAXNAMELEN);
238 tp->name[MAXNAMELEN-1] = '\0';
243 } /* serv_name_lookup */
245 #ifdef AVOID_DNS_TIMEOUT
247 #define DNS_TIMEOUT 2 /* max sec per call */
249 jmp_buf hostname_env;
251 static void abort_network_query(int sig _U_)
253 longjmp(hostname_env, 1);
255 #endif /* AVOID_DNS_TIMEOUT */
257 static guchar *host_name_lookup(guint addr, gboolean *found)
260 hashname_t * volatile tp;
261 struct hostent *hostp;
265 hash_idx = HASH_IPV4_ADDRESS(addr);
267 tp = host_table[hash_idx];
270 tp = host_table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
273 if( tp->addr == addr ) {
274 if (tp->is_dummy_entry)
278 if (tp->next == NULL) {
279 tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
287 /* fill in a new entry */
292 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
293 * the name of the host on which it's running; to work around that
294 * botch, we don't try to translate an all-zero IP address to a host
297 if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
298 #ifdef AVOID_DNS_TIMEOUT
300 /* Quick hack to avoid DNS/YP timeout */
302 if (!setjmp(hostname_env)) {
303 signal(SIGALRM, abort_network_query);
306 hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
307 #ifdef AVOID_DNS_TIMEOUT
311 strncpy(tp->name, hostp->h_name, MAXNAMELEN);
312 tp->name[MAXNAMELEN-1] = '\0';
313 tp->is_dummy_entry = FALSE;
316 #ifdef AVOID_DNS_TIMEOUT
321 /* unknown host or DNS timeout */
323 ip_to_str_buf((guint8 *)&addr, tp->name);
324 tp->is_dummy_entry = TRUE;
329 } /* host_name_lookup */
331 static guchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
333 static guchar name[MAXNAMELEN];
335 struct hostent *hostp;
337 if (g_resolv_flags & RESOLV_NETWORK) {
338 #ifdef AVOID_DNS_TIMEOUT
340 /* Quick hack to avoid DNS/YP timeout */
342 if (!setjmp(hostname_env)) {
343 signal(SIGALRM, abort_network_query);
345 #endif /* AVOID_DNS_TIMEOUT */
346 hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
347 #ifdef AVOID_DNS_TIMEOUT
351 strncpy(name, hostp->h_name, MAXNAMELEN);
352 name[MAXNAMELEN-1] = '\0';
356 #ifdef AVOID_DNS_TIMEOUT
361 /* unknown host or DNS timeout */
364 sprintf(name, "%s", ip6_to_str(addr));
369 * Miscellaneous functions
372 static int fgetline(char **buf, int *size, FILE *fp)
384 if ((*buf = g_malloc(*size)) == NULL)
392 while ((c = getc(fp)) != EOF && c != '\n') {
393 if (len+1 >= *size) {
394 if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
400 if (len == 0 && c == EOF)
411 * Ethernet / manufacturer resolution
413 * The following functions implement ethernet address resolution and
414 * ethers files parsing (see ethers(4)).
416 * The manuf file has the same format as ethers(4) except that names are
417 * truncated to MAXMANUFLEN-1 characters and that an address contains
418 * only 3 bytes (instead of 6).
422 * I decide to not use the existing functions (see ethers(3) on some
423 * operating systems) for the following reasons:
424 * - performance gains (use of hash tables and some other enhancements),
425 * - use of two ethers files (system-wide and per user),
426 * - avoid the use of NIS maps,
427 * - lack of these functions on some systems.
429 * So the following functions do _not_ behave as the standard ones.
435 static int parse_ether_line(char *line, ether_t *eth, int six_bytes)
438 * See man ethers(4) for ethers file format
439 * (not available on all systems).
440 * We allow both ethernet address separators (':' and '-'),
441 * as well as Ethereal's '.' separator.
445 int a0, a1, a2, a3, a4, a5;
447 if ((cp = strchr(line, '#')))
450 if ((cp = strtok(line, " \t\n")) == NULL)
454 if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6) {
455 if (sscanf(cp, "%x-%x-%x-%x-%x-%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6) {
456 if (sscanf(cp, "%x.%x.%x.%x.%x.%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6)
461 if (sscanf(cp, "%x:%x:%x", &a0, &a1, &a2) != 3) {
462 if (sscanf(cp, "%x-%x-%x", &a0, &a1, &a2) != 3) {
463 if (sscanf(cp, "%x.%x.%x", &a0, &a1, &a2) != 3)
469 if ((cp = strtok(NULL, " \t\n")) == NULL)
485 strncpy(eth->name, cp, MAXNAMELEN);
486 eth->name[MAXNAMELEN-1] = '\0';
490 } /* parse_ether_line */
492 static FILE *eth_p = NULL;
494 static void set_ethent(char *path)
499 eth_p = fopen(path, "r");
502 static void end_ethent(void)
510 static ether_t *get_ethent(int six_bytes)
515 static char *buf = NULL;
520 while (fgetline(&buf, &size, eth_p) >= 0) {
521 if (parse_ether_line(buf, ð, six_bytes) == 0) {
530 static ether_t *get_ethbyname(const guchar *name)
534 set_ethent(g_ethers_path);
536 while ((eth = get_ethent(1)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
542 set_ethent(g_pethers_path);
544 while ((eth = get_ethent(1)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
552 } /* get_ethbyname */
554 static ether_t *get_ethbyaddr(const guint8 *addr)
559 set_ethent(g_ethers_path);
561 while ((eth = get_ethent(1)) && memcmp(addr, eth->addr, 6) != 0)
567 set_ethent(g_pethers_path);
569 while ((eth = get_ethent(1)) && memcmp(addr, eth->addr, 6) != 0)
577 } /* get_ethbyaddr */
579 static void add_manuf_name(guint8 *addr, guchar *name)
584 hash_idx = HASH_ETH_MANUF(addr);
586 tp = manuf_table[hash_idx];
589 tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
592 if (tp->next == NULL) {
593 tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
601 memcpy(tp->addr, addr, sizeof(tp->addr));
602 strncpy(tp->name, name, MAXMANUFLEN);
603 tp->name[MAXMANUFLEN-1] = '\0';
606 } /* add_manuf_name */
608 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
613 hash_idx = HASH_ETH_MANUF(addr);
615 tp = manuf_table[hash_idx];
618 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
626 } /* manuf_name_lookup */
628 static void initialize_ethers(void)
633 /* Compute the pathname of the ethers file. */
634 if (g_ethers_path == NULL) {
635 g_ethers_path = g_malloc(strlen(get_systemfile_dir()) +
636 strlen(ENAME_ETHERS) + 2);
637 sprintf(g_ethers_path, "%s" G_DIR_SEPARATOR_S "%s",
638 get_systemfile_dir(), ENAME_ETHERS);
641 /* Set g_pethers_path here, but don't actually do anything
642 * with it. It's used in get_ethbyname() and get_ethbyaddr()
644 if (g_pethers_path == NULL)
645 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
647 /* manuf hash table initialization */
649 /* Compute the pathname of the manuf file */
650 manuf_path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
651 strlen(ENAME_MANUF) + 2);
652 sprintf(manuf_path, "%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
655 /* Read it and initialize the hash table */
656 set_ethent(manuf_path);
658 while ((eth = get_ethent(0))) {
659 add_manuf_name(eth->addr, eth->name);
666 } /* initialize_ethers */
668 static hashether_t *add_eth_name(const guint8 *addr, const guchar *name)
673 hash_idx = HASH_ETH_ADDRESS(addr);
675 tp = eth_table[hash_idx];
678 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
681 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
682 /* address already known */
683 if (!tp->is_dummy_entry) {
686 /* replace this dummy (manuf) entry with a real name */
690 if (tp->next == NULL) {
691 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
699 memcpy(tp->addr, addr, sizeof(tp->addr));
700 strncpy(tp->name, name, MAXNAMELEN);
701 tp->name[MAXNAMELEN-1] = '\0';
703 tp->is_dummy_entry = FALSE;
709 static guchar *eth_name_lookup(const guint8 *addr)
716 hash_idx = HASH_ETH_ADDRESS(addr);
718 tp = eth_table[hash_idx];
721 tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
724 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
727 if (tp->next == NULL) {
728 tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
736 /* fill in a new entry */
738 memcpy(tp->addr, addr, sizeof(tp->addr));
741 if ( (eth = get_ethbyaddr(addr)) == NULL) {
744 if ((manufp = manuf_name_lookup(addr)) == NULL)
745 sprintf(tp->name, "%s", ether_to_str((guint8 *)addr));
747 sprintf(tp->name, "%s_%02x:%02x:%02x",
748 manufp->name, addr[3], addr[4], addr[5]);
750 tp->is_dummy_entry = TRUE;
753 strncpy(tp->name, eth->name, MAXNAMELEN);
754 tp->name[MAXNAMELEN-1] = '\0';
755 tp->is_dummy_entry = FALSE;
760 } /* eth_name_lookup */
762 static guint8 *eth_addr_lookup(const guchar *name)
766 hashether_t **table = eth_table;
769 /* to be optimized (hash table from name to addr) */
770 for (i = 0; i < HASHETHSIZE; i++) {
773 if (strcmp(tp->name, name) == 0)
779 /* not in hash table : performs a file lookup */
781 if ((eth = get_ethbyname(name)) == NULL)
784 /* add new entry in hash table */
786 tp = add_eth_name(eth->addr, name);
790 } /* eth_addr_lookup */
794 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
797 * We allow three address separators (':', '-', and '.'),
798 * as well as no separators
802 guint32 a, a0, a1, a2, a3;
803 gboolean found_single_number = FALSE;
805 if ((cp = strchr(line, '#')))
808 if ((cp = strtok(line, " \t\n")) == NULL)
811 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
812 * fill a and found_single_number is TRUE,
815 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
816 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
817 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
818 if (sscanf(cp, "%x", &a) == 1) {
819 found_single_number = TRUE;
828 if ((cp = strtok(NULL, " \t\n")) == NULL)
831 if (found_single_number) {
835 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
838 strncpy(ipxnet->name, cp, MAXNAMELEN);
839 ipxnet->name[MAXNAMELEN-1] = '\0';
843 } /* parse_ipxnets_line */
845 static FILE *ipxnet_p = NULL;
847 static void set_ipxnetent(char *path)
852 ipxnet_p = fopen(path, "r");
855 static void end_ipxnetent(void)
863 static ipxnet_t *get_ipxnetent(void)
866 static ipxnet_t ipxnet;
868 static char *buf = NULL;
870 if (ipxnet_p == NULL)
873 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
874 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
881 } /* get_ipxnetent */
883 static ipxnet_t *get_ipxnetbyname(const guchar *name)
887 set_ipxnetent(g_ipxnets_path);
889 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
892 if (ipxnet == NULL) {
895 set_ipxnetent(g_pipxnets_path);
897 while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
905 } /* get_ipxnetbyname */
907 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
912 set_ipxnetent(g_ipxnets_path);
914 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
916 if (ipxnet == NULL) {
919 set_ipxnetent(g_pipxnets_path);
921 while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
929 } /* get_ipxnetbyaddr */
931 static void initialize_ipxnets(void)
933 /* Compute the pathname of the ipxnets file.
935 * XXX - is there a notion of an "ipxnets file" in any flavor of
936 * UNIX, or with any add-on Netware package for UNIX? If not,
937 * should the UNIX version of the ipxnets file be in the datafile
940 if (g_ipxnets_path == NULL) {
941 g_ipxnets_path = g_malloc(strlen(get_systemfile_dir()) +
942 strlen(ENAME_IPXNETS) + 2);
943 sprintf(g_ipxnets_path, "%s" G_DIR_SEPARATOR_S "%s",
944 get_systemfile_dir(), ENAME_IPXNETS);
947 /* Set g_pipxnets_path here, but don't actually do anything
948 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
950 if (g_pipxnets_path == NULL)
951 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
953 } /* initialize_ipxnets */
955 static hashipxnet_t *add_ipxnet_name(guint addr, const guchar *name)
960 hash_idx = HASH_IPX_NET(addr);
962 tp = ipxnet_table[hash_idx];
965 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
968 if (tp->next == NULL) {
969 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
978 strncpy(tp->name, name, MAXNAMELEN);
979 tp->name[MAXNAMELEN-1] = '\0';
984 } /* add_ipxnet_name */
986 static guchar *ipxnet_name_lookup(const guint addr)
992 hash_idx = HASH_IPX_NET(addr);
994 tp = ipxnet_table[hash_idx];
997 tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1000 if (tp->addr == addr) {
1003 if (tp->next == NULL) {
1004 tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1012 /* fill in a new entry */
1017 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1019 sprintf(tp->name, "%X", addr);
1022 strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1023 tp->name[MAXNAMELEN-1] = '\0';
1028 } /* ipxnet_name_lookup */
1030 static guint ipxnet_addr_lookup(const guchar *name, gboolean *success)
1034 hashipxnet_t **table = ipxnet_table;
1037 /* to be optimized (hash table from name to addr) */
1038 for (i = 0; i < HASHIPXNETSIZE; i++) {
1041 if (strcmp(tp->name, name) == 0)
1047 /* not in hash table : performs a file lookup */
1049 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1054 /* add new entry in hash table */
1056 tp = add_ipxnet_name(ipxnet->addr, name);
1061 } /* ipxnet_addr_lookup */
1065 * External Functions
1068 extern guchar *get_hostname(guint addr)
1072 if (!(g_resolv_flags & RESOLV_NETWORK))
1073 return ip_to_str((guint8 *)&addr);
1075 return host_name_lookup(addr, &found);
1078 extern const guchar *get_hostname6(struct e_in6_addr *addr)
1083 if (!(g_resolv_flags & RESOLV_NETWORK))
1084 return ip6_to_str(addr);
1085 #ifdef SOLARIS8_INET6
1086 if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)addr) || IN6_IS_ADDR_MULTICAST((struct in6_addr*)addr))
1088 if (IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MULTICAST(addr))
1090 return ip6_to_str(addr);
1093 return host_name_lookup6(addr, &found);
1096 extern void add_host_name(guint addr, const guchar *name)
1101 hash_idx = HASH_IPV4_ADDRESS(addr);
1103 tp = host_table[hash_idx];
1106 tp = host_table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
1109 if (tp->addr == addr) {
1110 /* address already known */
1111 if (!tp->is_dummy_entry) {
1114 /* replace this dummy entry with the new one */
1118 if (tp->next == NULL) {
1119 tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
1127 strncpy(tp->name, name, MAXNAMELEN);
1128 tp->name[MAXNAMELEN-1] = '\0';
1131 tp->is_dummy_entry = FALSE;
1133 } /* add_host_name */
1135 extern guchar *get_udp_port(guint port)
1137 static gchar str[3][MAXNAMELEN];
1140 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1141 if (cur == &str[0][0]) {
1143 } else if (cur == &str[1][0]) {
1148 sprintf(cur, "%u", port);
1152 return serv_name_lookup(port, PT_UDP);
1154 } /* get_udp_port */
1156 extern guchar *get_tcp_port(guint port)
1158 static gchar str[3][MAXNAMELEN];
1161 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1162 if (cur == &str[0][0]) {
1164 } else if (cur == &str[1][0]) {
1169 sprintf(cur, "%u", port);
1173 return serv_name_lookup(port, PT_TCP);
1175 } /* get_tcp_port */
1177 extern guchar *get_sctp_port(guint port)
1179 static gchar str[3][MAXNAMELEN];
1182 if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1183 if (cur == &str[0][0]) {
1185 } else if (cur == &str[1][0]) {
1190 sprintf(cur, "%u", port);
1194 return serv_name_lookup(port, PT_SCTP);
1196 } /* get_sctp_port */
1198 extern guchar *get_ether_name(const guint8 *addr)
1200 if (!(g_resolv_flags & RESOLV_MAC))
1201 return ether_to_str((guint8 *)addr);
1203 if (!eth_resolution_initialized) {
1204 initialize_ethers();
1205 eth_resolution_initialized = 1;
1208 return eth_name_lookup(addr);
1210 } /* get_ether_name */
1212 /* Look for an ether name in the hash, and return it if found.
1213 * If it's not found, simply return NULL. We DO NOT make a new
1214 * hash entry for it with the hex digits turned into a string.
1216 guchar *get_ether_name_if_known(const guint8 *addr)
1221 /* Initialize ether structs if we're the first
1222 * ether-related function called */
1223 if (!(g_resolv_flags & RESOLV_MAC))
1226 if (!eth_resolution_initialized) {
1227 initialize_ethers();
1228 eth_resolution_initialized = 1;
1231 hash_idx = HASH_ETH_ADDRESS(addr);
1233 tp = eth_table[hash_idx];
1236 /* Hash key not found in table.
1237 * Force a lookup (and a hash entry) for addr, then call
1238 * myself. I plan on not getting into an infinite loop because
1239 * eth_name_lookup() is guaranteed to make a hashtable entry,
1240 * so when I call myself again, I can never get into this
1241 * block of code again. Knock on wood...
1243 (void) eth_name_lookup(addr);
1244 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1248 if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1249 if (!tp->is_dummy_entry) {
1250 /* A name was found, and its origin is an ethers file */
1254 /* A name was found, but it was created, not found in a file */
1258 if (tp->next == NULL) {
1259 /* Read my reason above for why I'm sure I can't get into an infinite loop */
1260 (void) eth_name_lookup(addr);
1261 return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1266 g_assert_not_reached();
1271 extern guint8 *get_ether_addr(const guchar *name)
1274 /* force resolution (do not check g_resolv_flags) */
1276 if (!eth_resolution_initialized) {
1277 initialize_ethers();
1278 eth_resolution_initialized = 1;
1281 return eth_addr_lookup(name);
1283 } /* get_ether_addr */
1285 extern void add_ether_byip(guint ip, const guint8 *eth)
1291 /* first check that IP address can be resolved */
1293 if ((host = host_name_lookup(ip, &found)) == NULL)
1296 /* ok, we can add this entry in the ethers hashtable */
1299 add_eth_name(eth, host);
1301 } /* add_ether_byip */
1303 extern const guchar *get_ipxnet_name(const guint32 addr)
1306 if (!(g_resolv_flags & RESOLV_NETWORK)) {
1307 return ipxnet_to_str_punct(addr, '\0');
1310 if (!ipxnet_resolution_initialized) {
1311 initialize_ipxnets();
1312 ipxnet_resolution_initialized = 1;
1315 return ipxnet_name_lookup(addr);
1317 } /* get_ipxnet_name */
1319 extern guint32 get_ipxnet_addr(const guchar *name, gboolean *known)
1324 /* force resolution (do not check g_resolv_flags) */
1326 if (!ipxnet_resolution_initialized) {
1327 initialize_ipxnets();
1328 ipxnet_resolution_initialized = 1;
1331 addr = ipxnet_addr_lookup(name, &success);
1336 } /* get_ipxnet_addr */
1338 extern const guchar *get_manuf_name(const guint8 *addr)
1340 static gchar str[3][MAXMANUFLEN];
1342 hashmanuf_t *manufp;
1344 if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
1345 initialize_ethers();
1346 eth_resolution_initialized = 1;
1349 if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
1350 if (cur == &str[0][0]) {
1352 } else if (cur == &str[1][0]) {
1357 sprintf(cur, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
1361 return manufp->name;
1363 } /* get_manuf_name */
1367 /* Translate a string, assumed either to be a dotted-quad IP address or
1368 * a host name, to a numeric IP address. Return TRUE if we succeed and
1369 * set "*addrp" to that numeric IP address; return FALSE if we fail.
1370 * Used more in the dfilter parser rather than in packet dissectors */
1371 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
1373 struct in_addr ipaddr;
1377 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
1378 * inet_pton(AF_INET) does not support hexadecimal notation nor
1379 * less-than-4 octet notation.
1381 if (!inet_aton(host, &ipaddr)) {
1382 /* It's not a valid dotted-quad IP address; is it a valid
1384 hp = gethostbyname(host);
1388 /* Apparently, some versions of gethostbyaddr can
1389 * return IPv6 addresses. */
1390 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
1391 memcpy(&ipaddr, hp->h_addr, hp->h_length);
1396 /* Does the string really contain dotted-quad IP?
1397 * Check against inet_atons that accept strings such as
1398 * "130.230" as valid addresses and try to convert them
1399 * to some form of a classful (host.net) notation.
1401 unsigned int a0, a1, a2, a3;
1402 if (sscanf(host, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1406 *addrp = g_ntohl(ipaddr.s_addr);
1411 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
1412 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
1413 * return FALSE if we fail.
1415 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
1419 if (inet_pton(AF_INET6, host, addrp) == 1)
1423 #ifdef HAVE_GETHOSTBYNAME2
1424 hp = gethostbyname2(host, AF_INET6);
1428 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
1429 memcpy(addrp, hp->h_addr, hp->h_length);