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