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