Deleted Added
full compact
addrtoname.c (172686) addrtoname.c (190207)
1/*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
23 *
1/*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
23 *
24 * $FreeBSD: head/contrib/tcpdump/addrtoname.c 172686 2007-10-16 02:31:48Z mlaier $
24 * $FreeBSD: head/contrib/tcpdump/addrtoname.c 190207 2009-03-21 18:30:25Z rpaulo $
25 */
26#ifndef lint
27static const char rcsid[] _U_ =
25 */
26#ifndef lint
27static const char rcsid[] _U_ =
28 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.108.2.9 2007/09/14 00:26:18 guy Exp $ (LBL)";
28 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.119 2007-08-08 14:06:34 hannes Exp $ (LBL)";
29#endif
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <tcpdump-stdinc.h>
36
37#ifdef USE_ETHER_NTOHOST
38#ifdef HAVE_NETINET_IF_ETHER_H
39struct mbuf; /* Squelch compiler warnings on some platforms for */
40struct rtentry; /* declarations in <net/if.h> */
41#include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
42#include <netinet/if_ether.h>
43#endif /* HAVE_NETINET_IF_ETHER_H */
44#ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
45#include <netinet/ether.h>
46#endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
47
48#if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
49#ifndef HAVE_STRUCT_ETHER_ADDR
50struct ether_addr {
51 unsigned char ether_addr_octet[6];
52};
53#endif
54extern int ether_ntohost(char *, const struct ether_addr *);
55#endif
56
57#endif /* USE_ETHER_NTOHOST */
58
59#include <pcap.h>
60#include <pcap-namedb.h>
61#include <signal.h>
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65
66#include "interface.h"
67#include "addrtoname.h"
68#include "llc.h"
69#include "setsignal.h"
70#include "extract.h"
71#include "oui.h"
72
73#ifndef ETHER_ADDR_LEN
74#define ETHER_ADDR_LEN 6
75#endif
76
77/*
78 * hash tables for whatever-to-name translations
79 *
80 * XXX there has to be error checks against strdup(3) failure
81 */
82
83#define HASHNAMESIZE 4096
29#endif
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <tcpdump-stdinc.h>
36
37#ifdef USE_ETHER_NTOHOST
38#ifdef HAVE_NETINET_IF_ETHER_H
39struct mbuf; /* Squelch compiler warnings on some platforms for */
40struct rtentry; /* declarations in <net/if.h> */
41#include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
42#include <netinet/if_ether.h>
43#endif /* HAVE_NETINET_IF_ETHER_H */
44#ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
45#include <netinet/ether.h>
46#endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
47
48#if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
49#ifndef HAVE_STRUCT_ETHER_ADDR
50struct ether_addr {
51 unsigned char ether_addr_octet[6];
52};
53#endif
54extern int ether_ntohost(char *, const struct ether_addr *);
55#endif
56
57#endif /* USE_ETHER_NTOHOST */
58
59#include <pcap.h>
60#include <pcap-namedb.h>
61#include <signal.h>
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65
66#include "interface.h"
67#include "addrtoname.h"
68#include "llc.h"
69#include "setsignal.h"
70#include "extract.h"
71#include "oui.h"
72
73#ifndef ETHER_ADDR_LEN
74#define ETHER_ADDR_LEN 6
75#endif
76
77/*
78 * hash tables for whatever-to-name translations
79 *
80 * XXX there has to be error checks against strdup(3) failure
81 */
82
83#define HASHNAMESIZE 4096
84#define BUFSIZE 128
85
86struct hnamemem {
87 u_int32_t addr;
88 const char *name;
89 struct hnamemem *nxt;
90};
91
92struct hnamemem hnametable[HASHNAMESIZE];
93struct hnamemem tporttable[HASHNAMESIZE];
94struct hnamemem uporttable[HASHNAMESIZE];
95struct hnamemem eprototable[HASHNAMESIZE];
96struct hnamemem dnaddrtable[HASHNAMESIZE];
97struct hnamemem ipxsaptable[HASHNAMESIZE];
98
99#if defined(INET6) && defined(WIN32)
100/*
101 * fake gethostbyaddr for Win2k/XP
102 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
103 * to 3rd argument.
104 *
105 * h_name in struct hostent is only valid.
106 */
107static struct hostent *
108win32_gethostbyaddr(const char *addr, int len, int type)
109{
110 static struct hostent host;
111 static char hostbuf[NI_MAXHOST];
112 char hname[NI_MAXHOST];
113 struct sockaddr_in6 addr6;
114
115 host.h_name = hostbuf;
116 switch (type) {
117 case AF_INET:
118 return gethostbyaddr(addr, len, type);
119 break;
120 case AF_INET6:
121 memset(&addr6, 0, sizeof(addr6));
122 addr6.sin6_family = AF_INET6;
123 memcpy(&addr6.sin6_addr, addr, len);
124 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
125 hname, sizeof(hname), NULL, 0, 0)) {
126 return NULL;
127 } else {
128 strcpy(host.h_name, hname);
129 return &host;
130 }
131 break;
132 default:
133 return NULL;
134 }
135}
136#define gethostbyaddr win32_gethostbyaddr
137#endif /* INET6 & WIN32 */
138
139#ifdef INET6
140struct h6namemem {
141 struct in6_addr addr;
142 char *name;
143 struct h6namemem *nxt;
144};
145
146struct h6namemem h6nametable[HASHNAMESIZE];
147#endif /* INET6 */
148
149struct enamemem {
150 u_short e_addr0;
151 u_short e_addr1;
152 u_short e_addr2;
153 const char *e_name;
154 u_char *e_nsap; /* used only for nsaptable[] */
155#define e_bs e_nsap /* for bytestringtable */
156 struct enamemem *e_nxt;
157};
158
159struct enamemem enametable[HASHNAMESIZE];
160struct enamemem nsaptable[HASHNAMESIZE];
161struct enamemem bytestringtable[HASHNAMESIZE];
162
163struct protoidmem {
164 u_int32_t p_oui;
165 u_short p_proto;
166 const char *p_name;
167 struct protoidmem *p_nxt;
168};
169
170struct protoidmem protoidtable[HASHNAMESIZE];
171
172/*
173 * A faster replacement for inet_ntoa().
174 */
175const char *
176intoa(u_int32_t addr)
177{
178 register char *cp;
179 register u_int byte;
180 register int n;
181 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
182
183 NTOHL(addr);
184 cp = buf + sizeof(buf);
185 *--cp = '\0';
186
187 n = 4;
188 do {
189 byte = addr & 0xff;
190 *--cp = byte % 10 + '0';
191 byte /= 10;
192 if (byte > 0) {
193 *--cp = byte % 10 + '0';
194 byte /= 10;
195 if (byte > 0)
196 *--cp = byte + '0';
197 }
198 *--cp = '.';
199 addr >>= 8;
200 } while (--n > 0);
201
202 return cp + 1;
203}
204
205static u_int32_t f_netmask;
206static u_int32_t f_localnet;
207
208/*
209 * Return a name for the IP address pointed to by ap. This address
210 * is assumed to be in network byte order.
211 *
212 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
213 * this is being called with the "ipaddr_string()" macro), so you
214 * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it. Furthermore,
215 * even in cases where it *is* part of the packet data, the caller
216 * would still have to check for a null return value, even if it's
217 * just printing the return value with "%s" - not all versions of
218 * printf print "(null)" with "%s" and a null pointer, some of them
219 * don't check for a null pointer and crash in that case.
220 *
221 * The callers of this routine should, before handing this routine
222 * a pointer to packet data, be sure that the data is present in
223 * the packet buffer. They should probably do those checks anyway,
224 * as other data at that layer might not be IP addresses, and it
225 * also needs to check whether they're present in the packet buffer.
226 */
227const char *
228getname(const u_char *ap)
229{
230 register struct hostent *hp;
231 u_int32_t addr;
232 static struct hnamemem *p; /* static for longjmp() */
233
234 memcpy(&addr, ap, sizeof(addr));
235 p = &hnametable[addr & (HASHNAMESIZE-1)];
236 for (; p->nxt; p = p->nxt) {
237 if (p->addr == addr)
238 return (p->name);
239 }
240 p->addr = addr;
241 p->nxt = newhnamemem();
242
243 /*
244 * Print names unless:
245 * (1) -n was given.
246 * (2) Address is foreign and -f was given. (If -f was not
247 * given, f_netmask and f_localnet are 0 and the test
248 * evaluates to true)
249 */
250 if (!nflag &&
251 (addr & f_netmask) == f_localnet) {
252 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
253 if (hp) {
254 char *dotp;
255
256 p->name = strdup(hp->h_name);
257 if (Nflag) {
258 /* Remove domain qualifications */
259 dotp = strchr(p->name, '.');
260 if (dotp)
261 *dotp = '\0';
262 }
263 return (p->name);
264 }
265 }
266 p->name = strdup(intoa(addr));
267 return (p->name);
268}
269
270#ifdef INET6
271/*
272 * Return a name for the IP6 address pointed to by ap. This address
273 * is assumed to be in network byte order.
274 */
275const char *
276getname6(const u_char *ap)
277{
278 register struct hostent *hp;
279 struct in6_addr addr;
280 static struct h6namemem *p; /* static for longjmp() */
281 register const char *cp;
282 char ntop_buf[INET6_ADDRSTRLEN];
283
284 memcpy(&addr, ap, sizeof(addr));
285 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
286 for (; p->nxt; p = p->nxt) {
287 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
288 return (p->name);
289 }
290 p->addr = addr;
291 p->nxt = newh6namemem();
292
293 /*
294 * Do not print names if -n was given.
295 */
296 if (!nflag) {
297 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
298 if (hp) {
299 char *dotp;
300
301 p->name = strdup(hp->h_name);
302 if (Nflag) {
303 /* Remove domain qualifications */
304 dotp = strchr(p->name, '.');
305 if (dotp)
306 *dotp = '\0';
307 }
308 return (p->name);
309 }
310 }
311 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
312 p->name = strdup(cp);
313 return (p->name);
314}
315#endif /* INET6 */
316
317static char hex[] = "0123456789abcdef";
318
319
320/* Find the hash node that corresponds the ether address 'ep' */
321
322static inline struct enamemem *
323lookup_emem(const u_char *ep)
324{
325 register u_int i, j, k;
326 struct enamemem *tp;
327
328 k = (ep[0] << 8) | ep[1];
329 j = (ep[2] << 8) | ep[3];
330 i = (ep[4] << 8) | ep[5];
331
332 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
333 while (tp->e_nxt)
334 if (tp->e_addr0 == i &&
335 tp->e_addr1 == j &&
336 tp->e_addr2 == k)
337 return tp;
338 else
339 tp = tp->e_nxt;
340 tp->e_addr0 = i;
341 tp->e_addr1 = j;
342 tp->e_addr2 = k;
343 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
344 if (tp->e_nxt == NULL)
345 error("lookup_emem: calloc");
346
347 return tp;
348}
349
350/*
351 * Find the hash node that corresponds to the bytestring 'bs'
352 * with length 'nlen'
353 */
354
355static inline struct enamemem *
356lookup_bytestring(register const u_char *bs, const unsigned int nlen)
357{
358 struct enamemem *tp;
359 register u_int i, j, k;
360
361 if (nlen >= 6) {
362 k = (bs[0] << 8) | bs[1];
363 j = (bs[2] << 8) | bs[3];
364 i = (bs[4] << 8) | bs[5];
365 } else if (nlen >= 4) {
366 k = (bs[0] << 8) | bs[1];
367 j = (bs[2] << 8) | bs[3];
368 i = 0;
369 } else
370 i = j = k = 0;
371
372 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
373 while (tp->e_nxt)
374 if (tp->e_addr0 == i &&
375 tp->e_addr1 == j &&
376 tp->e_addr2 == k &&
377 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
378 return tp;
379 else
380 tp = tp->e_nxt;
381
382 tp->e_addr0 = i;
383 tp->e_addr1 = j;
384 tp->e_addr2 = k;
385
386 tp->e_bs = (u_char *) calloc(1, nlen + 1);
387 memcpy(tp->e_bs, bs, nlen);
388 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
389 if (tp->e_nxt == NULL)
390 error("lookup_bytestring: calloc");
391
392 return tp;
393}
394
395/* Find the hash node that corresponds the NSAP 'nsap' */
396
397static inline struct enamemem *
398lookup_nsap(register const u_char *nsap)
399{
400 register u_int i, j, k;
401 unsigned int nlen = *nsap;
402 struct enamemem *tp;
403 const u_char *ensap = nsap + nlen - 6;
404
405 if (nlen > 6) {
406 k = (ensap[0] << 8) | ensap[1];
407 j = (ensap[2] << 8) | ensap[3];
408 i = (ensap[4] << 8) | ensap[5];
409 }
410 else
411 i = j = k = 0;
412
413 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
414 while (tp->e_nxt)
415 if (tp->e_addr0 == i &&
416 tp->e_addr1 == j &&
417 tp->e_addr2 == k &&
418 tp->e_nsap[0] == nlen &&
419 memcmp((const char *)&(nsap[1]),
420 (char *)&(tp->e_nsap[1]), nlen) == 0)
421 return tp;
422 else
423 tp = tp->e_nxt;
424 tp->e_addr0 = i;
425 tp->e_addr1 = j;
426 tp->e_addr2 = k;
427 tp->e_nsap = (u_char *)malloc(nlen + 1);
428 if (tp->e_nsap == NULL)
429 error("lookup_nsap: malloc");
430 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
431 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
432 if (tp->e_nxt == NULL)
433 error("lookup_nsap: calloc");
434
435 return tp;
436}
437
438/* Find the hash node that corresponds the protoid 'pi'. */
439
440static inline struct protoidmem *
441lookup_protoid(const u_char *pi)
442{
443 register u_int i, j;
444 struct protoidmem *tp;
445
446 /* 5 octets won't be aligned */
447 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
448 j = (pi[3] << 8) + pi[4];
449 /* XXX should be endian-insensitive, but do big-endian testing XXX */
450
451 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
452 while (tp->p_nxt)
453 if (tp->p_oui == i && tp->p_proto == j)
454 return tp;
455 else
456 tp = tp->p_nxt;
457 tp->p_oui = i;
458 tp->p_proto = j;
459 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
460 if (tp->p_nxt == NULL)
461 error("lookup_protoid: calloc");
462
463 return tp;
464}
465
466const char *
467etheraddr_string(register const u_char *ep)
468{
469 register int i;
470 register char *cp;
471 register struct enamemem *tp;
472 int oui;
473 char buf[BUFSIZE];
474
475 tp = lookup_emem(ep);
476 if (tp->e_name)
477 return (tp->e_name);
478#ifdef USE_ETHER_NTOHOST
479 if (!nflag) {
480 char buf2[BUFSIZE];
481
482 /*
483 * We don't cast it to "const struct ether_addr *"
484 * because some systems fail to declare the second
485 * argument as a "const" pointer, even though they
486 * don't modify what it points to.
487 */
488 if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
489 tp->e_name = strdup(buf2);
490 return (tp->e_name);
491 }
492 }
493#endif
494 cp = buf;
495 oui = EXTRACT_24BITS(ep);
496 *cp++ = hex[*ep >> 4 ];
497 *cp++ = hex[*ep++ & 0xf];
498 for (i = 5; --i >= 0;) {
499 *cp++ = ':';
500 *cp++ = hex[*ep >> 4 ];
501 *cp++ = hex[*ep++ & 0xf];
502 }
503
504 if (!nflag) {
505 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
506 tok2str(oui_values, "Unknown", oui));
507 } else
508 *cp = '\0';
509 tp->e_name = strdup(buf);
510 return (tp->e_name);
511}
512
513const char *
84
85struct hnamemem {
86 u_int32_t addr;
87 const char *name;
88 struct hnamemem *nxt;
89};
90
91struct hnamemem hnametable[HASHNAMESIZE];
92struct hnamemem tporttable[HASHNAMESIZE];
93struct hnamemem uporttable[HASHNAMESIZE];
94struct hnamemem eprototable[HASHNAMESIZE];
95struct hnamemem dnaddrtable[HASHNAMESIZE];
96struct hnamemem ipxsaptable[HASHNAMESIZE];
97
98#if defined(INET6) && defined(WIN32)
99/*
100 * fake gethostbyaddr for Win2k/XP
101 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
102 * to 3rd argument.
103 *
104 * h_name in struct hostent is only valid.
105 */
106static struct hostent *
107win32_gethostbyaddr(const char *addr, int len, int type)
108{
109 static struct hostent host;
110 static char hostbuf[NI_MAXHOST];
111 char hname[NI_MAXHOST];
112 struct sockaddr_in6 addr6;
113
114 host.h_name = hostbuf;
115 switch (type) {
116 case AF_INET:
117 return gethostbyaddr(addr, len, type);
118 break;
119 case AF_INET6:
120 memset(&addr6, 0, sizeof(addr6));
121 addr6.sin6_family = AF_INET6;
122 memcpy(&addr6.sin6_addr, addr, len);
123 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
124 hname, sizeof(hname), NULL, 0, 0)) {
125 return NULL;
126 } else {
127 strcpy(host.h_name, hname);
128 return &host;
129 }
130 break;
131 default:
132 return NULL;
133 }
134}
135#define gethostbyaddr win32_gethostbyaddr
136#endif /* INET6 & WIN32 */
137
138#ifdef INET6
139struct h6namemem {
140 struct in6_addr addr;
141 char *name;
142 struct h6namemem *nxt;
143};
144
145struct h6namemem h6nametable[HASHNAMESIZE];
146#endif /* INET6 */
147
148struct enamemem {
149 u_short e_addr0;
150 u_short e_addr1;
151 u_short e_addr2;
152 const char *e_name;
153 u_char *e_nsap; /* used only for nsaptable[] */
154#define e_bs e_nsap /* for bytestringtable */
155 struct enamemem *e_nxt;
156};
157
158struct enamemem enametable[HASHNAMESIZE];
159struct enamemem nsaptable[HASHNAMESIZE];
160struct enamemem bytestringtable[HASHNAMESIZE];
161
162struct protoidmem {
163 u_int32_t p_oui;
164 u_short p_proto;
165 const char *p_name;
166 struct protoidmem *p_nxt;
167};
168
169struct protoidmem protoidtable[HASHNAMESIZE];
170
171/*
172 * A faster replacement for inet_ntoa().
173 */
174const char *
175intoa(u_int32_t addr)
176{
177 register char *cp;
178 register u_int byte;
179 register int n;
180 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
181
182 NTOHL(addr);
183 cp = buf + sizeof(buf);
184 *--cp = '\0';
185
186 n = 4;
187 do {
188 byte = addr & 0xff;
189 *--cp = byte % 10 + '0';
190 byte /= 10;
191 if (byte > 0) {
192 *--cp = byte % 10 + '0';
193 byte /= 10;
194 if (byte > 0)
195 *--cp = byte + '0';
196 }
197 *--cp = '.';
198 addr >>= 8;
199 } while (--n > 0);
200
201 return cp + 1;
202}
203
204static u_int32_t f_netmask;
205static u_int32_t f_localnet;
206
207/*
208 * Return a name for the IP address pointed to by ap. This address
209 * is assumed to be in network byte order.
210 *
211 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
212 * this is being called with the "ipaddr_string()" macro), so you
213 * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it. Furthermore,
214 * even in cases where it *is* part of the packet data, the caller
215 * would still have to check for a null return value, even if it's
216 * just printing the return value with "%s" - not all versions of
217 * printf print "(null)" with "%s" and a null pointer, some of them
218 * don't check for a null pointer and crash in that case.
219 *
220 * The callers of this routine should, before handing this routine
221 * a pointer to packet data, be sure that the data is present in
222 * the packet buffer. They should probably do those checks anyway,
223 * as other data at that layer might not be IP addresses, and it
224 * also needs to check whether they're present in the packet buffer.
225 */
226const char *
227getname(const u_char *ap)
228{
229 register struct hostent *hp;
230 u_int32_t addr;
231 static struct hnamemem *p; /* static for longjmp() */
232
233 memcpy(&addr, ap, sizeof(addr));
234 p = &hnametable[addr & (HASHNAMESIZE-1)];
235 for (; p->nxt; p = p->nxt) {
236 if (p->addr == addr)
237 return (p->name);
238 }
239 p->addr = addr;
240 p->nxt = newhnamemem();
241
242 /*
243 * Print names unless:
244 * (1) -n was given.
245 * (2) Address is foreign and -f was given. (If -f was not
246 * given, f_netmask and f_localnet are 0 and the test
247 * evaluates to true)
248 */
249 if (!nflag &&
250 (addr & f_netmask) == f_localnet) {
251 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
252 if (hp) {
253 char *dotp;
254
255 p->name = strdup(hp->h_name);
256 if (Nflag) {
257 /* Remove domain qualifications */
258 dotp = strchr(p->name, '.');
259 if (dotp)
260 *dotp = '\0';
261 }
262 return (p->name);
263 }
264 }
265 p->name = strdup(intoa(addr));
266 return (p->name);
267}
268
269#ifdef INET6
270/*
271 * Return a name for the IP6 address pointed to by ap. This address
272 * is assumed to be in network byte order.
273 */
274const char *
275getname6(const u_char *ap)
276{
277 register struct hostent *hp;
278 struct in6_addr addr;
279 static struct h6namemem *p; /* static for longjmp() */
280 register const char *cp;
281 char ntop_buf[INET6_ADDRSTRLEN];
282
283 memcpy(&addr, ap, sizeof(addr));
284 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
285 for (; p->nxt; p = p->nxt) {
286 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
287 return (p->name);
288 }
289 p->addr = addr;
290 p->nxt = newh6namemem();
291
292 /*
293 * Do not print names if -n was given.
294 */
295 if (!nflag) {
296 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
297 if (hp) {
298 char *dotp;
299
300 p->name = strdup(hp->h_name);
301 if (Nflag) {
302 /* Remove domain qualifications */
303 dotp = strchr(p->name, '.');
304 if (dotp)
305 *dotp = '\0';
306 }
307 return (p->name);
308 }
309 }
310 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
311 p->name = strdup(cp);
312 return (p->name);
313}
314#endif /* INET6 */
315
316static char hex[] = "0123456789abcdef";
317
318
319/* Find the hash node that corresponds the ether address 'ep' */
320
321static inline struct enamemem *
322lookup_emem(const u_char *ep)
323{
324 register u_int i, j, k;
325 struct enamemem *tp;
326
327 k = (ep[0] << 8) | ep[1];
328 j = (ep[2] << 8) | ep[3];
329 i = (ep[4] << 8) | ep[5];
330
331 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
332 while (tp->e_nxt)
333 if (tp->e_addr0 == i &&
334 tp->e_addr1 == j &&
335 tp->e_addr2 == k)
336 return tp;
337 else
338 tp = tp->e_nxt;
339 tp->e_addr0 = i;
340 tp->e_addr1 = j;
341 tp->e_addr2 = k;
342 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
343 if (tp->e_nxt == NULL)
344 error("lookup_emem: calloc");
345
346 return tp;
347}
348
349/*
350 * Find the hash node that corresponds to the bytestring 'bs'
351 * with length 'nlen'
352 */
353
354static inline struct enamemem *
355lookup_bytestring(register const u_char *bs, const unsigned int nlen)
356{
357 struct enamemem *tp;
358 register u_int i, j, k;
359
360 if (nlen >= 6) {
361 k = (bs[0] << 8) | bs[1];
362 j = (bs[2] << 8) | bs[3];
363 i = (bs[4] << 8) | bs[5];
364 } else if (nlen >= 4) {
365 k = (bs[0] << 8) | bs[1];
366 j = (bs[2] << 8) | bs[3];
367 i = 0;
368 } else
369 i = j = k = 0;
370
371 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
372 while (tp->e_nxt)
373 if (tp->e_addr0 == i &&
374 tp->e_addr1 == j &&
375 tp->e_addr2 == k &&
376 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
377 return tp;
378 else
379 tp = tp->e_nxt;
380
381 tp->e_addr0 = i;
382 tp->e_addr1 = j;
383 tp->e_addr2 = k;
384
385 tp->e_bs = (u_char *) calloc(1, nlen + 1);
386 memcpy(tp->e_bs, bs, nlen);
387 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
388 if (tp->e_nxt == NULL)
389 error("lookup_bytestring: calloc");
390
391 return tp;
392}
393
394/* Find the hash node that corresponds the NSAP 'nsap' */
395
396static inline struct enamemem *
397lookup_nsap(register const u_char *nsap)
398{
399 register u_int i, j, k;
400 unsigned int nlen = *nsap;
401 struct enamemem *tp;
402 const u_char *ensap = nsap + nlen - 6;
403
404 if (nlen > 6) {
405 k = (ensap[0] << 8) | ensap[1];
406 j = (ensap[2] << 8) | ensap[3];
407 i = (ensap[4] << 8) | ensap[5];
408 }
409 else
410 i = j = k = 0;
411
412 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
413 while (tp->e_nxt)
414 if (tp->e_addr0 == i &&
415 tp->e_addr1 == j &&
416 tp->e_addr2 == k &&
417 tp->e_nsap[0] == nlen &&
418 memcmp((const char *)&(nsap[1]),
419 (char *)&(tp->e_nsap[1]), nlen) == 0)
420 return tp;
421 else
422 tp = tp->e_nxt;
423 tp->e_addr0 = i;
424 tp->e_addr1 = j;
425 tp->e_addr2 = k;
426 tp->e_nsap = (u_char *)malloc(nlen + 1);
427 if (tp->e_nsap == NULL)
428 error("lookup_nsap: malloc");
429 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
430 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
431 if (tp->e_nxt == NULL)
432 error("lookup_nsap: calloc");
433
434 return tp;
435}
436
437/* Find the hash node that corresponds the protoid 'pi'. */
438
439static inline struct protoidmem *
440lookup_protoid(const u_char *pi)
441{
442 register u_int i, j;
443 struct protoidmem *tp;
444
445 /* 5 octets won't be aligned */
446 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
447 j = (pi[3] << 8) + pi[4];
448 /* XXX should be endian-insensitive, but do big-endian testing XXX */
449
450 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
451 while (tp->p_nxt)
452 if (tp->p_oui == i && tp->p_proto == j)
453 return tp;
454 else
455 tp = tp->p_nxt;
456 tp->p_oui = i;
457 tp->p_proto = j;
458 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
459 if (tp->p_nxt == NULL)
460 error("lookup_protoid: calloc");
461
462 return tp;
463}
464
465const char *
466etheraddr_string(register const u_char *ep)
467{
468 register int i;
469 register char *cp;
470 register struct enamemem *tp;
471 int oui;
472 char buf[BUFSIZE];
473
474 tp = lookup_emem(ep);
475 if (tp->e_name)
476 return (tp->e_name);
477#ifdef USE_ETHER_NTOHOST
478 if (!nflag) {
479 char buf2[BUFSIZE];
480
481 /*
482 * We don't cast it to "const struct ether_addr *"
483 * because some systems fail to declare the second
484 * argument as a "const" pointer, even though they
485 * don't modify what it points to.
486 */
487 if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
488 tp->e_name = strdup(buf2);
489 return (tp->e_name);
490 }
491 }
492#endif
493 cp = buf;
494 oui = EXTRACT_24BITS(ep);
495 *cp++ = hex[*ep >> 4 ];
496 *cp++ = hex[*ep++ & 0xf];
497 for (i = 5; --i >= 0;) {
498 *cp++ = ':';
499 *cp++ = hex[*ep >> 4 ];
500 *cp++ = hex[*ep++ & 0xf];
501 }
502
503 if (!nflag) {
504 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
505 tok2str(oui_values, "Unknown", oui));
506 } else
507 *cp = '\0';
508 tp->e_name = strdup(buf);
509 return (tp->e_name);
510}
511
512const char *
514linkaddr_string(const u_char *ep, const unsigned int len)
513linkaddr_string(const u_char *ep, const unsigned int type, const unsigned int len)
515{
516 register u_int i;
517 register char *cp;
518 register struct enamemem *tp;
519
514{
515 register u_int i;
516 register char *cp;
517 register struct enamemem *tp;
518
520 if (len == ETHER_ADDR_LEN) /* XXX not totally correct... */
521 return etheraddr_string(ep);
519 if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN) {
520 return etheraddr_string(ep);
521 }
522
522
523 if (type == LINKADDR_FRELAY) {
524 return q922_string(ep);
525 }
526
523 tp = lookup_bytestring(ep, len);
524 if (tp->e_name)
525 return (tp->e_name);
526
527 tp->e_name = cp = (char *)malloc(len*3);
528 if (tp->e_name == NULL)
529 error("linkaddr_string: malloc");
530 *cp++ = hex[*ep >> 4];
531 *cp++ = hex[*ep++ & 0xf];
532 for (i = len-1; i > 0 ; --i) {
533 *cp++ = ':';
534 *cp++ = hex[*ep >> 4];
535 *cp++ = hex[*ep++ & 0xf];
536 }
537 *cp = '\0';
538 return (tp->e_name);
539}
540
541const char *
542etherproto_string(u_short port)
543{
544 register char *cp;
545 register struct hnamemem *tp;
546 register u_int32_t i = port;
547 char buf[sizeof("0000")];
548
549 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
550 if (tp->addr == i)
551 return (tp->name);
552
553 tp->addr = i;
554 tp->nxt = newhnamemem();
555
556 cp = buf;
557 NTOHS(port);
558 *cp++ = hex[port >> 12 & 0xf];
559 *cp++ = hex[port >> 8 & 0xf];
560 *cp++ = hex[port >> 4 & 0xf];
561 *cp++ = hex[port & 0xf];
562 *cp++ = '\0';
563 tp->name = strdup(buf);
564 return (tp->name);
565}
566
567const char *
568protoid_string(register const u_char *pi)
569{
570 register u_int i, j;
571 register char *cp;
572 register struct protoidmem *tp;
573 char buf[sizeof("00:00:00:00:00")];
574
575 tp = lookup_protoid(pi);
576 if (tp->p_name)
577 return tp->p_name;
578
579 cp = buf;
580 if ((j = *pi >> 4) != 0)
581 *cp++ = hex[j];
582 *cp++ = hex[*pi++ & 0xf];
583 for (i = 4; (int)--i >= 0;) {
584 *cp++ = ':';
585 if ((j = *pi >> 4) != 0)
586 *cp++ = hex[j];
587 *cp++ = hex[*pi++ & 0xf];
588 }
589 *cp = '\0';
590 tp->p_name = strdup(buf);
591 return (tp->p_name);
592}
593
594#define ISONSAP_MAX_LENGTH 20
595const char *
596isonsap_string(const u_char *nsap, register u_int nsap_length)
597{
598 register u_int nsap_idx;
599 register char *cp;
600 register struct enamemem *tp;
601
602 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
603 return ("isonsap_string: illegal length");
604
605 tp = lookup_nsap(nsap);
606 if (tp->e_name)
607 return tp->e_name;
608
609 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
610 if (cp == NULL)
611 error("isonsap_string: malloc");
612
613 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
614 *cp++ = hex[*nsap >> 4];
615 *cp++ = hex[*nsap++ & 0xf];
616 if (((nsap_idx & 1) == 0) &&
617 (nsap_idx + 1 < nsap_length)) {
618 *cp++ = '.';
619 }
620 }
621 *cp = '\0';
622 return (tp->e_name);
623}
624
625const char *
626tcpport_string(u_short port)
627{
628 register struct hnamemem *tp;
629 register u_int32_t i = port;
630 char buf[sizeof("00000")];
631
632 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
633 if (tp->addr == i)
634 return (tp->name);
635
636 tp->addr = i;
637 tp->nxt = newhnamemem();
638
639 (void)snprintf(buf, sizeof(buf), "%u", i);
640 tp->name = strdup(buf);
641 return (tp->name);
642}
643
644const char *
645udpport_string(register u_short port)
646{
647 register struct hnamemem *tp;
648 register u_int32_t i = port;
649 char buf[sizeof("00000")];
650
651 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
652 if (tp->addr == i)
653 return (tp->name);
654
655 tp->addr = i;
656 tp->nxt = newhnamemem();
657
658 (void)snprintf(buf, sizeof(buf), "%u", i);
659 tp->name = strdup(buf);
660 return (tp->name);
661}
662
663const char *
664ipxsap_string(u_short port)
665{
666 register char *cp;
667 register struct hnamemem *tp;
668 register u_int32_t i = port;
669 char buf[sizeof("0000")];
670
671 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
672 if (tp->addr == i)
673 return (tp->name);
674
675 tp->addr = i;
676 tp->nxt = newhnamemem();
677
678 cp = buf;
679 NTOHS(port);
680 *cp++ = hex[port >> 12 & 0xf];
681 *cp++ = hex[port >> 8 & 0xf];
682 *cp++ = hex[port >> 4 & 0xf];
683 *cp++ = hex[port & 0xf];
684 *cp++ = '\0';
685 tp->name = strdup(buf);
686 return (tp->name);
687}
688
689static void
690init_servarray(void)
691{
692 struct servent *sv;
693 register struct hnamemem *table;
694 register int i;
695 char buf[sizeof("0000000000")];
696
697 while ((sv = getservent()) != NULL) {
698 int port = ntohs(sv->s_port);
699 i = port & (HASHNAMESIZE-1);
700 if (strcmp(sv->s_proto, "tcp") == 0)
701 table = &tporttable[i];
702 else if (strcmp(sv->s_proto, "udp") == 0)
703 table = &uporttable[i];
704 else
705 continue;
706
707 while (table->name)
708 table = table->nxt;
709 if (nflag) {
710 (void)snprintf(buf, sizeof(buf), "%d", port);
711 table->name = strdup(buf);
712 } else
713 table->name = strdup(sv->s_name);
714 table->addr = port;
715 table->nxt = newhnamemem();
716 }
717 endservent();
718}
719
720/* in libpcap.a (nametoaddr.c) */
721#if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
722__declspec(dllimport)
723#else
724extern
725#endif
726const struct eproto {
727 const char *s;
728 u_short p;
729} eproto_db[];
730
731static void
732init_eprotoarray(void)
733{
734 register int i;
735 register struct hnamemem *table;
736
737 for (i = 0; eproto_db[i].s; i++) {
738 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
739 table = &eprototable[j];
740 while (table->name)
741 table = table->nxt;
742 table->name = eproto_db[i].s;
743 table->addr = htons(eproto_db[i].p);
744 table->nxt = newhnamemem();
745 }
746}
747
748static struct protoidlist {
749 const u_char protoid[5];
750 const char *name;
751} protoidlist[] = {
752 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
753 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
754 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
755 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
756 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
757 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
758};
759
760/*
761 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
762 * types.
763 */
764static void
765init_protoidarray(void)
766{
767 register int i;
768 register struct protoidmem *tp;
769 struct protoidlist *pl;
770 u_char protoid[5];
771
772 protoid[0] = 0;
773 protoid[1] = 0;
774 protoid[2] = 0;
775 for (i = 0; eproto_db[i].s; i++) {
776 u_short etype = htons(eproto_db[i].p);
777
778 memcpy((char *)&protoid[3], (char *)&etype, 2);
779 tp = lookup_protoid(protoid);
780 tp->p_name = strdup(eproto_db[i].s);
781 }
782 /* Hardwire some SNAP proto ID names */
783 for (pl = protoidlist; pl->name != NULL; ++pl) {
784 tp = lookup_protoid(pl->protoid);
785 /* Don't override existing name */
786 if (tp->p_name != NULL)
787 continue;
788
789 tp->p_name = pl->name;
790 }
791}
792
793static struct etherlist {
794 const u_char addr[6];
795 const char *name;
796} etherlist[] = {
797 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
798 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
799};
800
801/*
802 * Initialize the ethers hash table. We take two different approaches
803 * depending on whether or not the system provides the ethers name
804 * service. If it does, we just wire in a few names at startup,
805 * and etheraddr_string() fills in the table on demand. If it doesn't,
806 * then we suck in the entire /etc/ethers file at startup. The idea
807 * is that parsing the local file will be fast, but spinning through
808 * all the ethers entries via NIS & next_etherent might be very slow.
809 *
810 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
811 * since the pcap module already does name-to-address translation,
812 * it's already does most of the work for the ethernet address-to-name
813 * translation, so we just pcap_next_etherent as a convenience.
814 */
815static void
816init_etherarray(void)
817{
818 register struct etherlist *el;
819 register struct enamemem *tp;
820#ifdef USE_ETHER_NTOHOST
821 char name[256];
822#else
823 register struct pcap_etherent *ep;
824 register FILE *fp;
825
826 /* Suck in entire ethers file */
827 fp = fopen(PCAP_ETHERS_FILE, "r");
828 if (fp != NULL) {
829 while ((ep = pcap_next_etherent(fp)) != NULL) {
830 tp = lookup_emem(ep->addr);
831 tp->e_name = strdup(ep->name);
832 }
833 (void)fclose(fp);
834 }
835#endif
836
837 /* Hardwire some ethernet names */
838 for (el = etherlist; el->name != NULL; ++el) {
839 tp = lookup_emem(el->addr);
840 /* Don't override existing name */
841 if (tp->e_name != NULL)
842 continue;
843
844#ifdef USE_ETHER_NTOHOST
845 /*
846 * Use YP/NIS version of name if available.
847 *
848 * We don't cast it to "const struct ether_addr *"
849 * because some systems don't modify the Ethernet
850 * address but fail to declare the second argument
851 * as a "const" pointer.
852 */
853 if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
854 tp->e_name = strdup(name);
855 continue;
856 }
857#endif
858 tp->e_name = el->name;
859 }
860}
861
862static struct tok ipxsap_db[] = {
863 { 0x0000, "Unknown" },
864 { 0x0001, "User" },
865 { 0x0002, "User Group" },
866 { 0x0003, "PrintQueue" },
867 { 0x0004, "FileServer" },
868 { 0x0005, "JobServer" },
869 { 0x0006, "Gateway" },
870 { 0x0007, "PrintServer" },
871 { 0x0008, "ArchiveQueue" },
872 { 0x0009, "ArchiveServer" },
873 { 0x000a, "JobQueue" },
874 { 0x000b, "Administration" },
875 { 0x000F, "Novell TI-RPC" },
876 { 0x0017, "Diagnostics" },
877 { 0x0020, "NetBIOS" },
878 { 0x0021, "NAS SNA Gateway" },
879 { 0x0023, "NACS AsyncGateway" },
880 { 0x0024, "RemoteBridge/RoutingService" },
881 { 0x0026, "BridgeServer" },
882 { 0x0027, "TCP/IP Gateway" },
883 { 0x0028, "Point-to-point X.25 BridgeServer" },
884 { 0x0029, "3270 Gateway" },
885 { 0x002a, "CHI Corp" },
886 { 0x002c, "PC Chalkboard" },
887 { 0x002d, "TimeSynchServer" },
888 { 0x002e, "ARCserve5.0/PalindromeBackup" },
889 { 0x0045, "DI3270 Gateway" },
890 { 0x0047, "AdvertisingPrintServer" },
891 { 0x004a, "NetBlazerModems" },
892 { 0x004b, "BtrieveVAP" },
893 { 0x004c, "NetwareSQL" },
894 { 0x004d, "XtreeNetwork" },
895 { 0x0050, "BtrieveVAP4.11" },
896 { 0x0052, "QuickLink" },
897 { 0x0053, "PrintQueueUser" },
898 { 0x0058, "Multipoint X.25 Router" },
899 { 0x0060, "STLB/NLM" },
900 { 0x0064, "ARCserve" },
901 { 0x0066, "ARCserve3.0" },
902 { 0x0072, "WAN CopyUtility" },
903 { 0x007a, "TES-NetwareVMS" },
904 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
905 { 0x0095, "DDA OBGYN" },
906 { 0x0098, "NetwareAccessServer" },
907 { 0x009a, "Netware for VMS II/NamedPipeServer" },
908 { 0x009b, "NetwareAccessServer" },
909 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
910 { 0x00a1, "PowerchuteAPC UPS" },
911 { 0x00aa, "LAWserve" },
912 { 0x00ac, "CompaqIDA StatusMonitor" },
913 { 0x0100, "PIPE STAIL" },
914 { 0x0102, "LAN ProtectBindery" },
915 { 0x0103, "OracleDataBaseServer" },
916 { 0x0107, "Netware386/RSPX RemoteConsole" },
917 { 0x010f, "NovellSNA Gateway" },
918 { 0x0111, "TestServer" },
919 { 0x0112, "HP PrintServer" },
920 { 0x0114, "CSA MUX" },
921 { 0x0115, "CSA LCA" },
922 { 0x0116, "CSA CM" },
923 { 0x0117, "CSA SMA" },
924 { 0x0118, "CSA DBA" },
925 { 0x0119, "CSA NMA" },
926 { 0x011a, "CSA SSA" },
927 { 0x011b, "CSA STATUS" },
928 { 0x011e, "CSA APPC" },
929 { 0x0126, "SNA TEST SSA Profile" },
930 { 0x012a, "CSA TRACE" },
931 { 0x012b, "NetwareSAA" },
932 { 0x012e, "IKARUS VirusScan" },
933 { 0x0130, "CommunicationsExecutive" },
934 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
935 { 0x0135, "NetwareNamingServicesProfile" },
936 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
937 { 0x0141, "LAN SpoolServer" },
938 { 0x0152, "IRMALAN Gateway" },
939 { 0x0154, "NamedPipeServer" },
940 { 0x0166, "NetWareManagement" },
941 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
942 { 0x0173, "Compaq" },
943 { 0x0174, "Compaq SNMP Agent" },
944 { 0x0175, "Compaq" },
945 { 0x0180, "XTreeServer/XTreeTools" },
946 { 0x018A, "NASI ServicesBroadcastServer" },
947 { 0x01b0, "GARP Gateway" },
948 { 0x01b1, "Binfview" },
949 { 0x01bf, "IntelLanDeskManager" },
950 { 0x01ca, "AXTEC" },
951 { 0x01cb, "ShivaNetModem/E" },
952 { 0x01cc, "ShivaLanRover/E" },
953 { 0x01cd, "ShivaLanRover/T" },
954 { 0x01ce, "ShivaUniversal" },
955 { 0x01d8, "CastelleFAXPressServer" },
956 { 0x01da, "CastelleLANPressPrintServer" },
957 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
958 { 0x01f0, "LEGATO" },
959 { 0x01f5, "LEGATO" },
960 { 0x0233, "NMS Agent/NetwareManagementAgent" },
961 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
962 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
963 { 0x023a, "LANtern" },
964 { 0x023c, "MAVERICK" },
965 { 0x023f, "NovellSMDR" },
966 { 0x024e, "NetwareConnect" },
967 { 0x024f, "NASI ServerBroadcast Cisco" },
968 { 0x026a, "NMS ServiceConsole" },
969 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
970 { 0x0278, "DirectoryServer Netware 4.x" },
971 { 0x027b, "NetwareManagementAgent" },
972 { 0x0280, "Novell File and Printer Sharing Service for PC" },
973 { 0x0304, "NovellSAA Gateway" },
974 { 0x0308, "COM/VERMED" },
975 { 0x030a, "GalacticommWorldgroupServer" },
976 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
977 { 0x0320, "AttachmateGateway" },
978 { 0x0327, "MicrosoftDiagnostiocs" },
979 { 0x0328, "WATCOM SQL Server" },
980 { 0x0335, "MultiTechSystems MultisynchCommServer" },
981 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
982 { 0x0355, "ArcadaBackupExec" },
983 { 0x0358, "MSLCD1" },
984 { 0x0361, "NETINELO" },
985 { 0x037e, "Powerchute UPS Monitoring" },
986 { 0x037f, "ViruSafeNotify" },
987 { 0x0386, "HP Bridge" },
988 { 0x0387, "HP Hub" },
989 { 0x0394, "NetWare SAA Gateway" },
990 { 0x039b, "LotusNotes" },
991 { 0x03b7, "CertusAntiVirus" },
992 { 0x03c4, "ARCserve4.0" },
993 { 0x03c7, "LANspool3.5" },
994 { 0x03d7, "LexmarkPrinterServer" },
995 { 0x03d8, "LexmarkXLE PrinterServer" },
996 { 0x03dd, "BanyanENS NetwareClient" },
997 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
998 { 0x03e1, "UnivelUnixware" },
999 { 0x03e4, "UnivelUnixware" },
1000 { 0x03fc, "IntelNetport" },
1001 { 0x03fd, "PrintServerQueue" },
1002 { 0x040A, "ipnServer" },
1003 { 0x040D, "LVERRMAN" },
1004 { 0x040E, "LVLIC" },
1005 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1006 { 0x0429, "SiteLockVirus" },
1007 { 0x0432, "UFHELPR???" },
1008 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1009 { 0x0444, "MicrosoftNT SNA Server" },
1010 { 0x0448, "Oracle" },
1011 { 0x044c, "ARCserve5.01" },
1012 { 0x0457, "CanonGP55" },
1013 { 0x045a, "QMS Printers" },
1014 { 0x045b, "DellSCSI Array" },
1015 { 0x0491, "NetBlazerModems" },
1016 { 0x04ac, "OnTimeScheduler" },
1017 { 0x04b0, "CD-Net" },
1018 { 0x0513, "EmulexNQA" },
1019 { 0x0520, "SiteLockChecks" },
1020 { 0x0529, "SiteLockChecks" },
1021 { 0x052d, "CitrixOS2 AppServer" },
1022 { 0x0535, "Tektronix" },
1023 { 0x0536, "Milan" },
1024 { 0x055d, "Attachmate SNA gateway" },
1025 { 0x056b, "IBM8235 ModemServer" },
1026 { 0x056c, "ShivaLanRover/E PLUS" },
1027 { 0x056d, "ShivaLanRover/T PLUS" },
1028 { 0x0580, "McAfeeNetShield" },
1029 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1030 { 0x05BA, "CompatibleSystemsRouters" },
1031 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1032 { 0x0606, "JCWatermarkImaging" },
1033 { 0x060c, "AXISNetworkPrinter" },
1034 { 0x0610, "AdaptecSCSIManagement" },
1035 { 0x0621, "IBM AntiVirus" },
1036 { 0x0640, "Windows95 RemoteRegistryService" },
1037 { 0x064e, "MicrosoftIIS" },
1038 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1039 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1040 { 0x076C, "Xerox" },
1041 { 0x079b, "ShivaLanRover/E 115" },
1042 { 0x079c, "ShivaLanRover/T 115" },
1043 { 0x07B4, "CubixWorldDesk" },
1044 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1045 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1046 { 0x0810, "ELAN License Server Demo" },
1047 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1048 { 0x086a, "ISSC Collector" },
1049 { 0x087f, "ISSC DAS AgentAIX" },
1050 { 0x0880, "Intel Netport PRO" },
1051 { 0x0881, "Intel Netport PRO" },
1052 { 0x0b29, "SiteLock" },
1053 { 0x0c29, "SiteLockApplications" },
1054 { 0x0c2c, "LicensingServer" },
1055 { 0x2101, "PerformanceTechnologyInstantInternet" },
1056 { 0x2380, "LAI SiteLock" },
1057 { 0x238c, "MeetingMaker" },
1058 { 0x4808, "SiteLockServer/SiteLockMetering" },
1059 { 0x5555, "SiteLockUser" },
1060 { 0x6312, "Tapeware" },
1061 { 0x6f00, "RabbitGateway" },
1062 { 0x7703, "MODEM" },
1063 { 0x8002, "NetPortPrinters" },
1064 { 0x8008, "WordPerfectNetworkVersion" },
1065 { 0x85BE, "Cisco EIGRP" },
1066 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1067 { 0x9000, "McAfeeNetShield" },
1068 { 0x9604, "CSA-NT_MON" },
1069 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1070 { 0xf11f, "SiteLockMetering" },
1071 { 0xf1ff, "SiteLock" },
1072 { 0xf503, "Microsoft SQL Server" },
1073 { 0xF905, "IBM TimeAndPlace" },
1074 { 0xfbfb, "TopCallIII FaxServer" },
1075 { 0xffff, "AnyService/Wildcard" },
1076 { 0, (char *)0 }
1077};
1078
1079static void
1080init_ipxsaparray(void)
1081{
1082 register int i;
1083 register struct hnamemem *table;
1084
1085 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1086 int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1087 table = &ipxsaptable[j];
1088 while (table->name)
1089 table = table->nxt;
1090 table->name = ipxsap_db[i].s;
1091 table->addr = htons(ipxsap_db[i].v);
1092 table->nxt = newhnamemem();
1093 }
1094}
1095
1096/*
1097 * Initialize the address to name translation machinery. We map all
1098 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
1099 * to prevent blocking on the nameserver). localnet is the IP address
1100 * of the local network. mask is its subnet mask.
1101 */
1102void
1103init_addrtoname(u_int32_t localnet, u_int32_t mask)
1104{
1105 if (fflag) {
1106 f_localnet = localnet;
1107 f_netmask = mask;
1108 }
1109 if (nflag)
1110 /*
1111 * Simplest way to suppress names.
1112 */
1113 return;
1114
1115 init_etherarray();
1116 init_servarray();
1117 init_eprotoarray();
1118 init_protoidarray();
1119 init_ipxsaparray();
1120}
1121
1122const char *
1123dnaddr_string(u_short dnaddr)
1124{
1125 register struct hnamemem *tp;
1126
1127 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1128 tp = tp->nxt)
1129 if (tp->addr == dnaddr)
1130 return (tp->name);
1131
1132 tp->addr = dnaddr;
1133 tp->nxt = newhnamemem();
1134 if (nflag)
1135 tp->name = dnnum_string(dnaddr);
1136 else
1137 tp->name = dnname_string(dnaddr);
1138
1139 return(tp->name);
1140}
1141
1142/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1143struct hnamemem *
1144newhnamemem(void)
1145{
1146 register struct hnamemem *p;
1147 static struct hnamemem *ptr = NULL;
1148 static u_int num = 0;
1149
1150 if (num <= 0) {
1151 num = 64;
1152 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1153 if (ptr == NULL)
1154 error("newhnamemem: calloc");
1155 }
1156 --num;
1157 p = ptr++;
1158 return (p);
1159}
1160
1161#ifdef INET6
1162/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1163struct h6namemem *
1164newh6namemem(void)
1165{
1166 register struct h6namemem *p;
1167 static struct h6namemem *ptr = NULL;
1168 static u_int num = 0;
1169
1170 if (num <= 0) {
1171 num = 64;
1172 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1173 if (ptr == NULL)
1174 error("newh6namemem: calloc");
1175 }
1176 --num;
1177 p = ptr++;
1178 return (p);
1179}
1180#endif /* INET6 */
527 tp = lookup_bytestring(ep, len);
528 if (tp->e_name)
529 return (tp->e_name);
530
531 tp->e_name = cp = (char *)malloc(len*3);
532 if (tp->e_name == NULL)
533 error("linkaddr_string: malloc");
534 *cp++ = hex[*ep >> 4];
535 *cp++ = hex[*ep++ & 0xf];
536 for (i = len-1; i > 0 ; --i) {
537 *cp++ = ':';
538 *cp++ = hex[*ep >> 4];
539 *cp++ = hex[*ep++ & 0xf];
540 }
541 *cp = '\0';
542 return (tp->e_name);
543}
544
545const char *
546etherproto_string(u_short port)
547{
548 register char *cp;
549 register struct hnamemem *tp;
550 register u_int32_t i = port;
551 char buf[sizeof("0000")];
552
553 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
554 if (tp->addr == i)
555 return (tp->name);
556
557 tp->addr = i;
558 tp->nxt = newhnamemem();
559
560 cp = buf;
561 NTOHS(port);
562 *cp++ = hex[port >> 12 & 0xf];
563 *cp++ = hex[port >> 8 & 0xf];
564 *cp++ = hex[port >> 4 & 0xf];
565 *cp++ = hex[port & 0xf];
566 *cp++ = '\0';
567 tp->name = strdup(buf);
568 return (tp->name);
569}
570
571const char *
572protoid_string(register const u_char *pi)
573{
574 register u_int i, j;
575 register char *cp;
576 register struct protoidmem *tp;
577 char buf[sizeof("00:00:00:00:00")];
578
579 tp = lookup_protoid(pi);
580 if (tp->p_name)
581 return tp->p_name;
582
583 cp = buf;
584 if ((j = *pi >> 4) != 0)
585 *cp++ = hex[j];
586 *cp++ = hex[*pi++ & 0xf];
587 for (i = 4; (int)--i >= 0;) {
588 *cp++ = ':';
589 if ((j = *pi >> 4) != 0)
590 *cp++ = hex[j];
591 *cp++ = hex[*pi++ & 0xf];
592 }
593 *cp = '\0';
594 tp->p_name = strdup(buf);
595 return (tp->p_name);
596}
597
598#define ISONSAP_MAX_LENGTH 20
599const char *
600isonsap_string(const u_char *nsap, register u_int nsap_length)
601{
602 register u_int nsap_idx;
603 register char *cp;
604 register struct enamemem *tp;
605
606 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
607 return ("isonsap_string: illegal length");
608
609 tp = lookup_nsap(nsap);
610 if (tp->e_name)
611 return tp->e_name;
612
613 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
614 if (cp == NULL)
615 error("isonsap_string: malloc");
616
617 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
618 *cp++ = hex[*nsap >> 4];
619 *cp++ = hex[*nsap++ & 0xf];
620 if (((nsap_idx & 1) == 0) &&
621 (nsap_idx + 1 < nsap_length)) {
622 *cp++ = '.';
623 }
624 }
625 *cp = '\0';
626 return (tp->e_name);
627}
628
629const char *
630tcpport_string(u_short port)
631{
632 register struct hnamemem *tp;
633 register u_int32_t i = port;
634 char buf[sizeof("00000")];
635
636 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
637 if (tp->addr == i)
638 return (tp->name);
639
640 tp->addr = i;
641 tp->nxt = newhnamemem();
642
643 (void)snprintf(buf, sizeof(buf), "%u", i);
644 tp->name = strdup(buf);
645 return (tp->name);
646}
647
648const char *
649udpport_string(register u_short port)
650{
651 register struct hnamemem *tp;
652 register u_int32_t i = port;
653 char buf[sizeof("00000")];
654
655 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
656 if (tp->addr == i)
657 return (tp->name);
658
659 tp->addr = i;
660 tp->nxt = newhnamemem();
661
662 (void)snprintf(buf, sizeof(buf), "%u", i);
663 tp->name = strdup(buf);
664 return (tp->name);
665}
666
667const char *
668ipxsap_string(u_short port)
669{
670 register char *cp;
671 register struct hnamemem *tp;
672 register u_int32_t i = port;
673 char buf[sizeof("0000")];
674
675 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
676 if (tp->addr == i)
677 return (tp->name);
678
679 tp->addr = i;
680 tp->nxt = newhnamemem();
681
682 cp = buf;
683 NTOHS(port);
684 *cp++ = hex[port >> 12 & 0xf];
685 *cp++ = hex[port >> 8 & 0xf];
686 *cp++ = hex[port >> 4 & 0xf];
687 *cp++ = hex[port & 0xf];
688 *cp++ = '\0';
689 tp->name = strdup(buf);
690 return (tp->name);
691}
692
693static void
694init_servarray(void)
695{
696 struct servent *sv;
697 register struct hnamemem *table;
698 register int i;
699 char buf[sizeof("0000000000")];
700
701 while ((sv = getservent()) != NULL) {
702 int port = ntohs(sv->s_port);
703 i = port & (HASHNAMESIZE-1);
704 if (strcmp(sv->s_proto, "tcp") == 0)
705 table = &tporttable[i];
706 else if (strcmp(sv->s_proto, "udp") == 0)
707 table = &uporttable[i];
708 else
709 continue;
710
711 while (table->name)
712 table = table->nxt;
713 if (nflag) {
714 (void)snprintf(buf, sizeof(buf), "%d", port);
715 table->name = strdup(buf);
716 } else
717 table->name = strdup(sv->s_name);
718 table->addr = port;
719 table->nxt = newhnamemem();
720 }
721 endservent();
722}
723
724/* in libpcap.a (nametoaddr.c) */
725#if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
726__declspec(dllimport)
727#else
728extern
729#endif
730const struct eproto {
731 const char *s;
732 u_short p;
733} eproto_db[];
734
735static void
736init_eprotoarray(void)
737{
738 register int i;
739 register struct hnamemem *table;
740
741 for (i = 0; eproto_db[i].s; i++) {
742 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
743 table = &eprototable[j];
744 while (table->name)
745 table = table->nxt;
746 table->name = eproto_db[i].s;
747 table->addr = htons(eproto_db[i].p);
748 table->nxt = newhnamemem();
749 }
750}
751
752static struct protoidlist {
753 const u_char protoid[5];
754 const char *name;
755} protoidlist[] = {
756 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
757 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
758 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
759 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
760 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
761 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
762};
763
764/*
765 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
766 * types.
767 */
768static void
769init_protoidarray(void)
770{
771 register int i;
772 register struct protoidmem *tp;
773 struct protoidlist *pl;
774 u_char protoid[5];
775
776 protoid[0] = 0;
777 protoid[1] = 0;
778 protoid[2] = 0;
779 for (i = 0; eproto_db[i].s; i++) {
780 u_short etype = htons(eproto_db[i].p);
781
782 memcpy((char *)&protoid[3], (char *)&etype, 2);
783 tp = lookup_protoid(protoid);
784 tp->p_name = strdup(eproto_db[i].s);
785 }
786 /* Hardwire some SNAP proto ID names */
787 for (pl = protoidlist; pl->name != NULL; ++pl) {
788 tp = lookup_protoid(pl->protoid);
789 /* Don't override existing name */
790 if (tp->p_name != NULL)
791 continue;
792
793 tp->p_name = pl->name;
794 }
795}
796
797static struct etherlist {
798 const u_char addr[6];
799 const char *name;
800} etherlist[] = {
801 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
802 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
803};
804
805/*
806 * Initialize the ethers hash table. We take two different approaches
807 * depending on whether or not the system provides the ethers name
808 * service. If it does, we just wire in a few names at startup,
809 * and etheraddr_string() fills in the table on demand. If it doesn't,
810 * then we suck in the entire /etc/ethers file at startup. The idea
811 * is that parsing the local file will be fast, but spinning through
812 * all the ethers entries via NIS & next_etherent might be very slow.
813 *
814 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
815 * since the pcap module already does name-to-address translation,
816 * it's already does most of the work for the ethernet address-to-name
817 * translation, so we just pcap_next_etherent as a convenience.
818 */
819static void
820init_etherarray(void)
821{
822 register struct etherlist *el;
823 register struct enamemem *tp;
824#ifdef USE_ETHER_NTOHOST
825 char name[256];
826#else
827 register struct pcap_etherent *ep;
828 register FILE *fp;
829
830 /* Suck in entire ethers file */
831 fp = fopen(PCAP_ETHERS_FILE, "r");
832 if (fp != NULL) {
833 while ((ep = pcap_next_etherent(fp)) != NULL) {
834 tp = lookup_emem(ep->addr);
835 tp->e_name = strdup(ep->name);
836 }
837 (void)fclose(fp);
838 }
839#endif
840
841 /* Hardwire some ethernet names */
842 for (el = etherlist; el->name != NULL; ++el) {
843 tp = lookup_emem(el->addr);
844 /* Don't override existing name */
845 if (tp->e_name != NULL)
846 continue;
847
848#ifdef USE_ETHER_NTOHOST
849 /*
850 * Use YP/NIS version of name if available.
851 *
852 * We don't cast it to "const struct ether_addr *"
853 * because some systems don't modify the Ethernet
854 * address but fail to declare the second argument
855 * as a "const" pointer.
856 */
857 if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
858 tp->e_name = strdup(name);
859 continue;
860 }
861#endif
862 tp->e_name = el->name;
863 }
864}
865
866static struct tok ipxsap_db[] = {
867 { 0x0000, "Unknown" },
868 { 0x0001, "User" },
869 { 0x0002, "User Group" },
870 { 0x0003, "PrintQueue" },
871 { 0x0004, "FileServer" },
872 { 0x0005, "JobServer" },
873 { 0x0006, "Gateway" },
874 { 0x0007, "PrintServer" },
875 { 0x0008, "ArchiveQueue" },
876 { 0x0009, "ArchiveServer" },
877 { 0x000a, "JobQueue" },
878 { 0x000b, "Administration" },
879 { 0x000F, "Novell TI-RPC" },
880 { 0x0017, "Diagnostics" },
881 { 0x0020, "NetBIOS" },
882 { 0x0021, "NAS SNA Gateway" },
883 { 0x0023, "NACS AsyncGateway" },
884 { 0x0024, "RemoteBridge/RoutingService" },
885 { 0x0026, "BridgeServer" },
886 { 0x0027, "TCP/IP Gateway" },
887 { 0x0028, "Point-to-point X.25 BridgeServer" },
888 { 0x0029, "3270 Gateway" },
889 { 0x002a, "CHI Corp" },
890 { 0x002c, "PC Chalkboard" },
891 { 0x002d, "TimeSynchServer" },
892 { 0x002e, "ARCserve5.0/PalindromeBackup" },
893 { 0x0045, "DI3270 Gateway" },
894 { 0x0047, "AdvertisingPrintServer" },
895 { 0x004a, "NetBlazerModems" },
896 { 0x004b, "BtrieveVAP" },
897 { 0x004c, "NetwareSQL" },
898 { 0x004d, "XtreeNetwork" },
899 { 0x0050, "BtrieveVAP4.11" },
900 { 0x0052, "QuickLink" },
901 { 0x0053, "PrintQueueUser" },
902 { 0x0058, "Multipoint X.25 Router" },
903 { 0x0060, "STLB/NLM" },
904 { 0x0064, "ARCserve" },
905 { 0x0066, "ARCserve3.0" },
906 { 0x0072, "WAN CopyUtility" },
907 { 0x007a, "TES-NetwareVMS" },
908 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
909 { 0x0095, "DDA OBGYN" },
910 { 0x0098, "NetwareAccessServer" },
911 { 0x009a, "Netware for VMS II/NamedPipeServer" },
912 { 0x009b, "NetwareAccessServer" },
913 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
914 { 0x00a1, "PowerchuteAPC UPS" },
915 { 0x00aa, "LAWserve" },
916 { 0x00ac, "CompaqIDA StatusMonitor" },
917 { 0x0100, "PIPE STAIL" },
918 { 0x0102, "LAN ProtectBindery" },
919 { 0x0103, "OracleDataBaseServer" },
920 { 0x0107, "Netware386/RSPX RemoteConsole" },
921 { 0x010f, "NovellSNA Gateway" },
922 { 0x0111, "TestServer" },
923 { 0x0112, "HP PrintServer" },
924 { 0x0114, "CSA MUX" },
925 { 0x0115, "CSA LCA" },
926 { 0x0116, "CSA CM" },
927 { 0x0117, "CSA SMA" },
928 { 0x0118, "CSA DBA" },
929 { 0x0119, "CSA NMA" },
930 { 0x011a, "CSA SSA" },
931 { 0x011b, "CSA STATUS" },
932 { 0x011e, "CSA APPC" },
933 { 0x0126, "SNA TEST SSA Profile" },
934 { 0x012a, "CSA TRACE" },
935 { 0x012b, "NetwareSAA" },
936 { 0x012e, "IKARUS VirusScan" },
937 { 0x0130, "CommunicationsExecutive" },
938 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
939 { 0x0135, "NetwareNamingServicesProfile" },
940 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
941 { 0x0141, "LAN SpoolServer" },
942 { 0x0152, "IRMALAN Gateway" },
943 { 0x0154, "NamedPipeServer" },
944 { 0x0166, "NetWareManagement" },
945 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
946 { 0x0173, "Compaq" },
947 { 0x0174, "Compaq SNMP Agent" },
948 { 0x0175, "Compaq" },
949 { 0x0180, "XTreeServer/XTreeTools" },
950 { 0x018A, "NASI ServicesBroadcastServer" },
951 { 0x01b0, "GARP Gateway" },
952 { 0x01b1, "Binfview" },
953 { 0x01bf, "IntelLanDeskManager" },
954 { 0x01ca, "AXTEC" },
955 { 0x01cb, "ShivaNetModem/E" },
956 { 0x01cc, "ShivaLanRover/E" },
957 { 0x01cd, "ShivaLanRover/T" },
958 { 0x01ce, "ShivaUniversal" },
959 { 0x01d8, "CastelleFAXPressServer" },
960 { 0x01da, "CastelleLANPressPrintServer" },
961 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
962 { 0x01f0, "LEGATO" },
963 { 0x01f5, "LEGATO" },
964 { 0x0233, "NMS Agent/NetwareManagementAgent" },
965 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
966 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
967 { 0x023a, "LANtern" },
968 { 0x023c, "MAVERICK" },
969 { 0x023f, "NovellSMDR" },
970 { 0x024e, "NetwareConnect" },
971 { 0x024f, "NASI ServerBroadcast Cisco" },
972 { 0x026a, "NMS ServiceConsole" },
973 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
974 { 0x0278, "DirectoryServer Netware 4.x" },
975 { 0x027b, "NetwareManagementAgent" },
976 { 0x0280, "Novell File and Printer Sharing Service for PC" },
977 { 0x0304, "NovellSAA Gateway" },
978 { 0x0308, "COM/VERMED" },
979 { 0x030a, "GalacticommWorldgroupServer" },
980 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
981 { 0x0320, "AttachmateGateway" },
982 { 0x0327, "MicrosoftDiagnostiocs" },
983 { 0x0328, "WATCOM SQL Server" },
984 { 0x0335, "MultiTechSystems MultisynchCommServer" },
985 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
986 { 0x0355, "ArcadaBackupExec" },
987 { 0x0358, "MSLCD1" },
988 { 0x0361, "NETINELO" },
989 { 0x037e, "Powerchute UPS Monitoring" },
990 { 0x037f, "ViruSafeNotify" },
991 { 0x0386, "HP Bridge" },
992 { 0x0387, "HP Hub" },
993 { 0x0394, "NetWare SAA Gateway" },
994 { 0x039b, "LotusNotes" },
995 { 0x03b7, "CertusAntiVirus" },
996 { 0x03c4, "ARCserve4.0" },
997 { 0x03c7, "LANspool3.5" },
998 { 0x03d7, "LexmarkPrinterServer" },
999 { 0x03d8, "LexmarkXLE PrinterServer" },
1000 { 0x03dd, "BanyanENS NetwareClient" },
1001 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1002 { 0x03e1, "UnivelUnixware" },
1003 { 0x03e4, "UnivelUnixware" },
1004 { 0x03fc, "IntelNetport" },
1005 { 0x03fd, "PrintServerQueue" },
1006 { 0x040A, "ipnServer" },
1007 { 0x040D, "LVERRMAN" },
1008 { 0x040E, "LVLIC" },
1009 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1010 { 0x0429, "SiteLockVirus" },
1011 { 0x0432, "UFHELPR???" },
1012 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1013 { 0x0444, "MicrosoftNT SNA Server" },
1014 { 0x0448, "Oracle" },
1015 { 0x044c, "ARCserve5.01" },
1016 { 0x0457, "CanonGP55" },
1017 { 0x045a, "QMS Printers" },
1018 { 0x045b, "DellSCSI Array" },
1019 { 0x0491, "NetBlazerModems" },
1020 { 0x04ac, "OnTimeScheduler" },
1021 { 0x04b0, "CD-Net" },
1022 { 0x0513, "EmulexNQA" },
1023 { 0x0520, "SiteLockChecks" },
1024 { 0x0529, "SiteLockChecks" },
1025 { 0x052d, "CitrixOS2 AppServer" },
1026 { 0x0535, "Tektronix" },
1027 { 0x0536, "Milan" },
1028 { 0x055d, "Attachmate SNA gateway" },
1029 { 0x056b, "IBM8235 ModemServer" },
1030 { 0x056c, "ShivaLanRover/E PLUS" },
1031 { 0x056d, "ShivaLanRover/T PLUS" },
1032 { 0x0580, "McAfeeNetShield" },
1033 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1034 { 0x05BA, "CompatibleSystemsRouters" },
1035 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1036 { 0x0606, "JCWatermarkImaging" },
1037 { 0x060c, "AXISNetworkPrinter" },
1038 { 0x0610, "AdaptecSCSIManagement" },
1039 { 0x0621, "IBM AntiVirus" },
1040 { 0x0640, "Windows95 RemoteRegistryService" },
1041 { 0x064e, "MicrosoftIIS" },
1042 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1043 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1044 { 0x076C, "Xerox" },
1045 { 0x079b, "ShivaLanRover/E 115" },
1046 { 0x079c, "ShivaLanRover/T 115" },
1047 { 0x07B4, "CubixWorldDesk" },
1048 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1049 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1050 { 0x0810, "ELAN License Server Demo" },
1051 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1052 { 0x086a, "ISSC Collector" },
1053 { 0x087f, "ISSC DAS AgentAIX" },
1054 { 0x0880, "Intel Netport PRO" },
1055 { 0x0881, "Intel Netport PRO" },
1056 { 0x0b29, "SiteLock" },
1057 { 0x0c29, "SiteLockApplications" },
1058 { 0x0c2c, "LicensingServer" },
1059 { 0x2101, "PerformanceTechnologyInstantInternet" },
1060 { 0x2380, "LAI SiteLock" },
1061 { 0x238c, "MeetingMaker" },
1062 { 0x4808, "SiteLockServer/SiteLockMetering" },
1063 { 0x5555, "SiteLockUser" },
1064 { 0x6312, "Tapeware" },
1065 { 0x6f00, "RabbitGateway" },
1066 { 0x7703, "MODEM" },
1067 { 0x8002, "NetPortPrinters" },
1068 { 0x8008, "WordPerfectNetworkVersion" },
1069 { 0x85BE, "Cisco EIGRP" },
1070 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1071 { 0x9000, "McAfeeNetShield" },
1072 { 0x9604, "CSA-NT_MON" },
1073 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1074 { 0xf11f, "SiteLockMetering" },
1075 { 0xf1ff, "SiteLock" },
1076 { 0xf503, "Microsoft SQL Server" },
1077 { 0xF905, "IBM TimeAndPlace" },
1078 { 0xfbfb, "TopCallIII FaxServer" },
1079 { 0xffff, "AnyService/Wildcard" },
1080 { 0, (char *)0 }
1081};
1082
1083static void
1084init_ipxsaparray(void)
1085{
1086 register int i;
1087 register struct hnamemem *table;
1088
1089 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1090 int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1091 table = &ipxsaptable[j];
1092 while (table->name)
1093 table = table->nxt;
1094 table->name = ipxsap_db[i].s;
1095 table->addr = htons(ipxsap_db[i].v);
1096 table->nxt = newhnamemem();
1097 }
1098}
1099
1100/*
1101 * Initialize the address to name translation machinery. We map all
1102 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
1103 * to prevent blocking on the nameserver). localnet is the IP address
1104 * of the local network. mask is its subnet mask.
1105 */
1106void
1107init_addrtoname(u_int32_t localnet, u_int32_t mask)
1108{
1109 if (fflag) {
1110 f_localnet = localnet;
1111 f_netmask = mask;
1112 }
1113 if (nflag)
1114 /*
1115 * Simplest way to suppress names.
1116 */
1117 return;
1118
1119 init_etherarray();
1120 init_servarray();
1121 init_eprotoarray();
1122 init_protoidarray();
1123 init_ipxsaparray();
1124}
1125
1126const char *
1127dnaddr_string(u_short dnaddr)
1128{
1129 register struct hnamemem *tp;
1130
1131 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1132 tp = tp->nxt)
1133 if (tp->addr == dnaddr)
1134 return (tp->name);
1135
1136 tp->addr = dnaddr;
1137 tp->nxt = newhnamemem();
1138 if (nflag)
1139 tp->name = dnnum_string(dnaddr);
1140 else
1141 tp->name = dnname_string(dnaddr);
1142
1143 return(tp->name);
1144}
1145
1146/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1147struct hnamemem *
1148newhnamemem(void)
1149{
1150 register struct hnamemem *p;
1151 static struct hnamemem *ptr = NULL;
1152 static u_int num = 0;
1153
1154 if (num <= 0) {
1155 num = 64;
1156 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1157 if (ptr == NULL)
1158 error("newhnamemem: calloc");
1159 }
1160 --num;
1161 p = ptr++;
1162 return (p);
1163}
1164
1165#ifdef INET6
1166/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1167struct h6namemem *
1168newh6namemem(void)
1169{
1170 register struct h6namemem *p;
1171 static struct h6namemem *ptr = NULL;
1172 static u_int num = 0;
1173
1174 if (num <= 0) {
1175 num = 64;
1176 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1177 if (ptr == NULL)
1178 error("newh6namemem: calloc");
1179 }
1180 --num;
1181 p = ptr++;
1182 return (p);
1183}
1184#endif /* INET6 */