Deleted Added
full compact
print-atalk.c (26183) print-atalk.c (39300)
1/*
1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
2 * Copyright (c) 1988, 1989, 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 * Format and print AppleTalk packets.
22 */
23
24#ifndef lint
25static const char rcsid[] =
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 * Format and print AppleTalk packets.
22 */
23
24#ifndef lint
25static const char rcsid[] =
26 "@(#) $Header: print-atalk.c,v 1.45 96/12/10 23:24:07 leres Exp $ (LBL)";
26 "@(#) $Header: print-atalk.c,v 1.48 97/05/28 12:50:58 leres Exp $ (LBL)";
27#endif
28
29#include <sys/param.h>
30#include <sys/time.h>
31#include <sys/socket.h>
32
33#if __STDC__
34struct mbuf;
35struct rtentry;
36#endif
37#include <net/if.h>
38
39#include <netinet/in.h>
40#include <netinet/in_systm.h>
41#include <netinet/ip.h>
42#include <netinet/ip_var.h>
43#include <net/ethernet.h>
44#include <netinet/udp.h>
45#include <netinet/udp_var.h>
46#include <netinet/tcp.h>
47#include <netinet/tcpip.h>
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "interface.h"
54#include "addrtoname.h"
55#include "ethertype.h"
56#include "extract.h" /* must come after interface.h */
57#include "appletalk.h"
27#endif
28
29#include <sys/param.h>
30#include <sys/time.h>
31#include <sys/socket.h>
32
33#if __STDC__
34struct mbuf;
35struct rtentry;
36#endif
37#include <net/if.h>
38
39#include <netinet/in.h>
40#include <netinet/in_systm.h>
41#include <netinet/ip.h>
42#include <netinet/ip_var.h>
43#include <net/ethernet.h>
44#include <netinet/udp.h>
45#include <netinet/udp_var.h>
46#include <netinet/tcp.h>
47#include <netinet/tcpip.h>
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#include "interface.h"
54#include "addrtoname.h"
55#include "ethertype.h"
56#include "extract.h" /* must come after interface.h */
57#include "appletalk.h"
58#include "savestr.h"
58
59static struct tok type2str[] = {
60 { ddpRTMP, "rtmp" },
61 { ddpRTMPrequest, "rtmpReq" },
62 { ddpECHO, "echo" },
63 { ddpIP, "IP" },
64 { ddpARP, "ARP" },
65 { ddpKLAP, "KLAP" },
66 { 0, NULL }
67};
68
69struct aarp {
70 u_short htype, ptype;
71 u_char halen, palen;
72 u_short op;
73 u_char hsaddr[6];
74 u_char psaddr[4];
75 u_char hdaddr[6];
76 u_char pdaddr[4];
77};
78
79static char tstr[] = "[|atalk]";
80
81static void atp_print(const struct atATP *, u_int);
82static void atp_bitmap_print(u_char);
83static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
84static const char *print_cstring(const char *, const u_char *);
85static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
86 const u_char *,
87 u_short, u_char, u_char);
88static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
89 const u_char *);
90static const char *ataddr_string(u_short, u_char);
91static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
92static const char *ddpskt_string(int);
93
94/*
95 * Print AppleTalk Datagram Delivery Protocol packets.
96 */
97void
98atalk_print(register const u_char *bp, u_int length)
99{
100 register const struct LAP *lp;
101 register const struct atDDP *dp;
102 register const struct atShortDDP *sdp;
103 u_short snet;
104
105#if 0
106 lp = (struct LAP *)bp;
107 bp += sizeof(*lp);
108 length -= sizeof(*lp);
109#else
110 {
111 static struct LAP lp_ = {0, 0, lapDDP};
112 lp = &lp_;
113 }
114#endif
115 switch (lp->type) {
116
117 case lapShortDDP:
118 if (length < ddpSSize) {
119 (void)printf(" [|sddp %d]", length);
120 return;
121 }
122 sdp = (const struct atShortDDP *)bp;
123 printf("%s.%s",
124 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
125 printf(" > %s.%s:",
126 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
127 bp += ddpSSize;
128 length -= ddpSSize;
129 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
130 break;
131
132 case lapDDP:
133 if (length < ddpSize) {
134 (void)printf(" [|ddp %d]", length);
135 return;
136 }
137 dp = (const struct atDDP *)bp;
138 snet = EXTRACT_16BITS(&dp->srcNet);
139 printf("%s.%s", ataddr_string(snet, dp->srcNode),
140 ddpskt_string(dp->srcSkt));
141 printf(" > %s.%s:",
142 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
143 ddpskt_string(dp->dstSkt));
144 bp += ddpSize;
145 length -= ddpSize;
146 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
147 break;
148
149#ifdef notdef
150 case lapKLAP:
151 klap_print(bp, length);
152 break;
153#endif
154
155 default:
156 printf("%d > %d at-lap#%d %d",
157 lp->src, lp->dst, lp->type, length);
158 break;
159 }
160}
161
162/* XXX should probably pass in the snap header and do checks like arp_print() */
163void
164aarp_print(register const u_char *bp, u_int length)
165{
166 register const struct aarp *ap;
167
168#define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
169
170 printf("aarp ");
171 ap = (const struct aarp *)bp;
172 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
173 ap->halen == 6 && ap->palen == 4 )
174 switch (ntohs(ap->op)) {
175
176 case 1: /* request */
177 (void)printf("who-has %s tell %s",
178 AT(pdaddr), AT(psaddr));
179 return;
180
181 case 2: /* response */
182 (void)printf("reply %s is-at %s",
183 AT(psaddr), etheraddr_string(ap->hsaddr));
184 return;
185
186 case 3: /* probe (oy!) */
187 (void)printf("probe %s tell %s",
188 AT(pdaddr), AT(psaddr));
189 return;
190 }
191 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
192 length, ap->op, ap->htype, ap->ptype, ap->halen, ap->palen );
193}
194
195static void
196ddp_print(register const u_char *bp, register u_int length, register int t,
197 register u_short snet, register u_char snode, u_char skt)
198{
199
200 switch (t) {
201
202 case ddpNBP:
203 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
204 break;
205
206 case ddpATP:
207 atp_print((const struct atATP *)bp, length);
208 break;
209
210 default:
211 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
212 break;
213 }
214}
215
216static void
217atp_print(register const struct atATP *ap, u_int length)
218{
219 char c;
220 u_int32_t data;
221
222 if ((const u_char *)(ap + 1) > snapend) {
223 /* Just bail if we don't have the whole chunk. */
224 fputs(tstr, stdout);
225 return;
226 }
227 length -= sizeof(*ap);
228 switch (ap->control & 0xc0) {
229
230 case atpReqCode:
231 (void)printf(" atp-req%s %d",
232 ap->control & atpXO? " " : "*",
233 EXTRACT_16BITS(&ap->transID));
234
235 atp_bitmap_print(ap->bitmap);
236
237 if (length != 0)
238 (void)printf(" [len=%d]", length);
239
240 switch (ap->control & (atpEOM|atpSTS)) {
241 case atpEOM:
242 (void)printf(" [EOM]");
243 break;
244 case atpSTS:
245 (void)printf(" [STS]");
246 break;
247 case atpEOM|atpSTS:
248 (void)printf(" [EOM,STS]");
249 break;
250 }
251 break;
252
253 case atpRspCode:
254 (void)printf(" atp-resp%s%d:%d (%d)",
255 ap->control & atpEOM? "*" : " ",
256 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
257 switch (ap->control & (atpXO|atpSTS)) {
258 case atpXO:
259 (void)printf(" [XO]");
260 break;
261 case atpSTS:
262 (void)printf(" [STS]");
263 break;
264 case atpXO|atpSTS:
265 (void)printf(" [XO,STS]");
266 break;
267 }
268 break;
269
270 case atpRelCode:
271 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
272
273 atp_bitmap_print(ap->bitmap);
274
275 /* length should be zero */
276 if (length)
277 (void)printf(" [len=%d]", length);
278
279 /* there shouldn't be any control flags */
280 if (ap->control & (atpXO|atpEOM|atpSTS)) {
281 c = '[';
282 if (ap->control & atpXO) {
283 (void)printf("%cXO", c);
284 c = ',';
285 }
286 if (ap->control & atpEOM) {
287 (void)printf("%cEOM", c);
288 c = ',';
289 }
290 if (ap->control & atpSTS) {
291 (void)printf("%cSTS", c);
292 c = ',';
293 }
294 (void)printf("]");
295 }
296 break;
297
298 default:
299 (void)printf(" atp-0x%x %d (%d)", ap->control,
300 EXTRACT_16BITS(&ap->transID), length);
301 break;
302 }
303 data = EXTRACT_32BITS(&ap->userData);
304 if (data != 0)
305 (void)printf(" 0x%x", data);
306}
307
308static void
309atp_bitmap_print(register u_char bm)
310{
311 register char c;
312 register int i;
313
314 /*
315 * The '& 0xff' below is needed for compilers that want to sign
316 * extend a u_char, which is the case with the Ultrix compiler.
317 * (gcc is smart enough to eliminate it, at least on the Sparc).
318 */
319 if ((bm + 1) & (bm & 0xff)) {
320 c = '<';
321 for (i = 0; bm; ++i) {
322 if (bm & 1) {
323 (void)printf("%c%d", c, i);
324 c = ',';
325 }
326 bm >>= 1;
327 }
328 (void)printf(">");
329 } else {
330 for (i = 0; bm; ++i)
331 bm >>= 1;
332 if (i > 1)
333 (void)printf("<0-%d>", i - 1);
334 else
335 (void)printf("<0>");
336 }
337}
338
339static void
340nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
341 register u_char snode, register u_char skt)
342{
343 register const struct atNBPtuple *tp =
344 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
345 int i;
346 const u_char *ep;
347
348 length -= nbpHeaderSize;
349 if (length < 8) {
350 /* must be room for at least one tuple */
351 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
352 return;
353 }
354 /* ep points to end of available data */
355 ep = snapend;
356 if ((const u_char *)tp > ep) {
357 fputs(tstr, stdout);
358 return;
359 }
360 switch (i = np->control & 0xf0) {
361
362 case nbpBrRq:
363 case nbpLkUp:
364 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
365 np->id);
366 if ((const u_char *)(tp + 1) > ep) {
367 fputs(tstr, stdout);
368 return;
369 }
370 (void)nbp_name_print(tp, ep);
371 /*
372 * look for anomalies: the spec says there can only
373 * be one tuple, the address must match the source
374 * address and the enumerator should be zero.
375 */
376 if ((np->control & 0xf) != 1)
377 (void)printf(" [ntup=%d]", np->control & 0xf);
378 if (tp->enumerator)
379 (void)printf(" [enum=%d]", tp->enumerator);
380 if (EXTRACT_16BITS(&tp->net) != snet ||
381 tp->node != snode || tp->skt != skt)
382 (void)printf(" [addr=%s.%d]",
383 ataddr_string(EXTRACT_16BITS(&tp->net),
384 tp->node), tp->skt);
385 break;
386
387 case nbpLkUpReply:
388 (void)printf(" nbp-reply %d:", np->id);
389
390 /* print each of the tuples in the reply */
391 for (i = np->control & 0xf; --i >= 0 && tp; )
392 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
393 break;
394
395 default:
396 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
397 length);
398 break;
399 }
400}
401
402/* print a counted string */
403static const char *
404print_cstring(register const char *cp, register const u_char *ep)
405{
406 register u_int length;
407
408 if (cp >= (const char *)ep) {
409 fputs(tstr, stdout);
410 return (0);
411 }
412 length = *cp++;
413
414 /* Spec says string can be at most 32 bytes long */
59
60static struct tok type2str[] = {
61 { ddpRTMP, "rtmp" },
62 { ddpRTMPrequest, "rtmpReq" },
63 { ddpECHO, "echo" },
64 { ddpIP, "IP" },
65 { ddpARP, "ARP" },
66 { ddpKLAP, "KLAP" },
67 { 0, NULL }
68};
69
70struct aarp {
71 u_short htype, ptype;
72 u_char halen, palen;
73 u_short op;
74 u_char hsaddr[6];
75 u_char psaddr[4];
76 u_char hdaddr[6];
77 u_char pdaddr[4];
78};
79
80static char tstr[] = "[|atalk]";
81
82static void atp_print(const struct atATP *, u_int);
83static void atp_bitmap_print(u_char);
84static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
85static const char *print_cstring(const char *, const u_char *);
86static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
87 const u_char *,
88 u_short, u_char, u_char);
89static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
90 const u_char *);
91static const char *ataddr_string(u_short, u_char);
92static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
93static const char *ddpskt_string(int);
94
95/*
96 * Print AppleTalk Datagram Delivery Protocol packets.
97 */
98void
99atalk_print(register const u_char *bp, u_int length)
100{
101 register const struct LAP *lp;
102 register const struct atDDP *dp;
103 register const struct atShortDDP *sdp;
104 u_short snet;
105
106#if 0
107 lp = (struct LAP *)bp;
108 bp += sizeof(*lp);
109 length -= sizeof(*lp);
110#else
111 {
112 static struct LAP lp_ = {0, 0, lapDDP};
113 lp = &lp_;
114 }
115#endif
116 switch (lp->type) {
117
118 case lapShortDDP:
119 if (length < ddpSSize) {
120 (void)printf(" [|sddp %d]", length);
121 return;
122 }
123 sdp = (const struct atShortDDP *)bp;
124 printf("%s.%s",
125 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
126 printf(" > %s.%s:",
127 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
128 bp += ddpSSize;
129 length -= ddpSSize;
130 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
131 break;
132
133 case lapDDP:
134 if (length < ddpSize) {
135 (void)printf(" [|ddp %d]", length);
136 return;
137 }
138 dp = (const struct atDDP *)bp;
139 snet = EXTRACT_16BITS(&dp->srcNet);
140 printf("%s.%s", ataddr_string(snet, dp->srcNode),
141 ddpskt_string(dp->srcSkt));
142 printf(" > %s.%s:",
143 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
144 ddpskt_string(dp->dstSkt));
145 bp += ddpSize;
146 length -= ddpSize;
147 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
148 break;
149
150#ifdef notdef
151 case lapKLAP:
152 klap_print(bp, length);
153 break;
154#endif
155
156 default:
157 printf("%d > %d at-lap#%d %d",
158 lp->src, lp->dst, lp->type, length);
159 break;
160 }
161}
162
163/* XXX should probably pass in the snap header and do checks like arp_print() */
164void
165aarp_print(register const u_char *bp, u_int length)
166{
167 register const struct aarp *ap;
168
169#define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
170
171 printf("aarp ");
172 ap = (const struct aarp *)bp;
173 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
174 ap->halen == 6 && ap->palen == 4 )
175 switch (ntohs(ap->op)) {
176
177 case 1: /* request */
178 (void)printf("who-has %s tell %s",
179 AT(pdaddr), AT(psaddr));
180 return;
181
182 case 2: /* response */
183 (void)printf("reply %s is-at %s",
184 AT(psaddr), etheraddr_string(ap->hsaddr));
185 return;
186
187 case 3: /* probe (oy!) */
188 (void)printf("probe %s tell %s",
189 AT(pdaddr), AT(psaddr));
190 return;
191 }
192 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
193 length, ap->op, ap->htype, ap->ptype, ap->halen, ap->palen );
194}
195
196static void
197ddp_print(register const u_char *bp, register u_int length, register int t,
198 register u_short snet, register u_char snode, u_char skt)
199{
200
201 switch (t) {
202
203 case ddpNBP:
204 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
205 break;
206
207 case ddpATP:
208 atp_print((const struct atATP *)bp, length);
209 break;
210
211 default:
212 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
213 break;
214 }
215}
216
217static void
218atp_print(register const struct atATP *ap, u_int length)
219{
220 char c;
221 u_int32_t data;
222
223 if ((const u_char *)(ap + 1) > snapend) {
224 /* Just bail if we don't have the whole chunk. */
225 fputs(tstr, stdout);
226 return;
227 }
228 length -= sizeof(*ap);
229 switch (ap->control & 0xc0) {
230
231 case atpReqCode:
232 (void)printf(" atp-req%s %d",
233 ap->control & atpXO? " " : "*",
234 EXTRACT_16BITS(&ap->transID));
235
236 atp_bitmap_print(ap->bitmap);
237
238 if (length != 0)
239 (void)printf(" [len=%d]", length);
240
241 switch (ap->control & (atpEOM|atpSTS)) {
242 case atpEOM:
243 (void)printf(" [EOM]");
244 break;
245 case atpSTS:
246 (void)printf(" [STS]");
247 break;
248 case atpEOM|atpSTS:
249 (void)printf(" [EOM,STS]");
250 break;
251 }
252 break;
253
254 case atpRspCode:
255 (void)printf(" atp-resp%s%d:%d (%d)",
256 ap->control & atpEOM? "*" : " ",
257 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
258 switch (ap->control & (atpXO|atpSTS)) {
259 case atpXO:
260 (void)printf(" [XO]");
261 break;
262 case atpSTS:
263 (void)printf(" [STS]");
264 break;
265 case atpXO|atpSTS:
266 (void)printf(" [XO,STS]");
267 break;
268 }
269 break;
270
271 case atpRelCode:
272 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
273
274 atp_bitmap_print(ap->bitmap);
275
276 /* length should be zero */
277 if (length)
278 (void)printf(" [len=%d]", length);
279
280 /* there shouldn't be any control flags */
281 if (ap->control & (atpXO|atpEOM|atpSTS)) {
282 c = '[';
283 if (ap->control & atpXO) {
284 (void)printf("%cXO", c);
285 c = ',';
286 }
287 if (ap->control & atpEOM) {
288 (void)printf("%cEOM", c);
289 c = ',';
290 }
291 if (ap->control & atpSTS) {
292 (void)printf("%cSTS", c);
293 c = ',';
294 }
295 (void)printf("]");
296 }
297 break;
298
299 default:
300 (void)printf(" atp-0x%x %d (%d)", ap->control,
301 EXTRACT_16BITS(&ap->transID), length);
302 break;
303 }
304 data = EXTRACT_32BITS(&ap->userData);
305 if (data != 0)
306 (void)printf(" 0x%x", data);
307}
308
309static void
310atp_bitmap_print(register u_char bm)
311{
312 register char c;
313 register int i;
314
315 /*
316 * The '& 0xff' below is needed for compilers that want to sign
317 * extend a u_char, which is the case with the Ultrix compiler.
318 * (gcc is smart enough to eliminate it, at least on the Sparc).
319 */
320 if ((bm + 1) & (bm & 0xff)) {
321 c = '<';
322 for (i = 0; bm; ++i) {
323 if (bm & 1) {
324 (void)printf("%c%d", c, i);
325 c = ',';
326 }
327 bm >>= 1;
328 }
329 (void)printf(">");
330 } else {
331 for (i = 0; bm; ++i)
332 bm >>= 1;
333 if (i > 1)
334 (void)printf("<0-%d>", i - 1);
335 else
336 (void)printf("<0>");
337 }
338}
339
340static void
341nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
342 register u_char snode, register u_char skt)
343{
344 register const struct atNBPtuple *tp =
345 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
346 int i;
347 const u_char *ep;
348
349 length -= nbpHeaderSize;
350 if (length < 8) {
351 /* must be room for at least one tuple */
352 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
353 return;
354 }
355 /* ep points to end of available data */
356 ep = snapend;
357 if ((const u_char *)tp > ep) {
358 fputs(tstr, stdout);
359 return;
360 }
361 switch (i = np->control & 0xf0) {
362
363 case nbpBrRq:
364 case nbpLkUp:
365 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
366 np->id);
367 if ((const u_char *)(tp + 1) > ep) {
368 fputs(tstr, stdout);
369 return;
370 }
371 (void)nbp_name_print(tp, ep);
372 /*
373 * look for anomalies: the spec says there can only
374 * be one tuple, the address must match the source
375 * address and the enumerator should be zero.
376 */
377 if ((np->control & 0xf) != 1)
378 (void)printf(" [ntup=%d]", np->control & 0xf);
379 if (tp->enumerator)
380 (void)printf(" [enum=%d]", tp->enumerator);
381 if (EXTRACT_16BITS(&tp->net) != snet ||
382 tp->node != snode || tp->skt != skt)
383 (void)printf(" [addr=%s.%d]",
384 ataddr_string(EXTRACT_16BITS(&tp->net),
385 tp->node), tp->skt);
386 break;
387
388 case nbpLkUpReply:
389 (void)printf(" nbp-reply %d:", np->id);
390
391 /* print each of the tuples in the reply */
392 for (i = np->control & 0xf; --i >= 0 && tp; )
393 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
394 break;
395
396 default:
397 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
398 length);
399 break;
400 }
401}
402
403/* print a counted string */
404static const char *
405print_cstring(register const char *cp, register const u_char *ep)
406{
407 register u_int length;
408
409 if (cp >= (const char *)ep) {
410 fputs(tstr, stdout);
411 return (0);
412 }
413 length = *cp++;
414
415 /* Spec says string can be at most 32 bytes long */
415 if (length < 0 || length > 32) {
416 (void)printf("[len=%d]", length);
416 if (length > 32) {
417 (void)printf("[len=%u]", length);
417 return (0);
418 }
418 return (0);
419 }
419 while (--length >= 0) {
420 while ((int)--length >= 0) {
420 if (cp >= (char *)ep) {
421 fputs(tstr, stdout);
422 return (0);
423 }
424 putchar(*cp++);
425 }
426 return (cp);
427}
428
429static const struct atNBPtuple *
430nbp_tuple_print(register const struct atNBPtuple *tp,
431 register const u_char *ep,
432 register u_short snet, register u_char snode,
433 register u_char skt)
434{
435 register const struct atNBPtuple *tpn;
436
437 if ((const u_char *)(tp + 1) > ep) {
438 fputs(tstr, stdout);
439 return 0;
440 }
441 tpn = nbp_name_print(tp, ep);
442
443 /* if the enumerator isn't 1, print it */
444 if (tp->enumerator != 1)
445 (void)printf("(%d)", tp->enumerator);
446
447 /* if the socket doesn't match the src socket, print it */
448 if (tp->skt != skt)
449 (void)printf(" %d", tp->skt);
450
451 /* if the address doesn't match the src address, it's an anomaly */
452 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
453 (void)printf(" [addr=%s]",
454 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
455
456 return (tpn);
457}
458
459static const struct atNBPtuple *
460nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
461{
462 register const char *cp = (const char *)tp + nbpTupleSize;
463
464 putchar(' ');
465
466 /* Object */
467 putchar('"');
468 if ((cp = print_cstring(cp, ep)) != NULL) {
469 /* Type */
470 putchar(':');
471 if ((cp = print_cstring(cp, ep)) != NULL) {
472 /* Zone */
473 putchar('@');
474 if ((cp = print_cstring(cp, ep)) != NULL)
475 putchar('"');
476 }
477 }
478 return ((const struct atNBPtuple *)cp);
479}
480
481
482#define HASHNAMESIZE 4096
483
484struct hnamemem {
485 int addr;
486 char *name;
487 struct hnamemem *nxt;
488};
489
490static struct hnamemem hnametable[HASHNAMESIZE];
491
492static const char *
493ataddr_string(u_short atnet, u_char athost)
494{
495 register struct hnamemem *tp, *tp2;
496 register int i = (atnet << 8) | athost;
497 char nambuf[256];
498 static int first = 1;
499 FILE *fp;
500
501 /*
502 * if this is the first call, see if there's an AppleTalk
503 * number to name map file.
504 */
505 if (first && (first = 0, !nflag)
506 && (fp = fopen("/etc/atalk.names", "r"))) {
507 char line[256];
508 int i1, i2;
509
510 while (fgets(line, sizeof(line), fp)) {
511 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
512 continue;
513 if (sscanf(line, "%d.%d %s", &i1, &i2, nambuf) == 3)
514 /* got a hostname. */
515 i2 |= (i1 << 8);
516 else if (sscanf(line, "%d %s", &i1, nambuf) == 2)
517 /* got a net name */
518 i2 = (i1 << 8) | 255;
519 else
520 continue;
521
522 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
523 tp->nxt; tp = tp->nxt)
524 ;
525 tp->addr = i2;
526 tp->nxt = newhnamemem();
527 tp->name = savestr(nambuf);
528 }
529 fclose(fp);
530 }
531
532 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
533 if (tp->addr == i)
534 return (tp->name);
535
536 /* didn't have the node name -- see if we've got the net name */
537 i |= 255;
538 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
539 if (tp2->addr == i) {
540 tp->addr = (atnet << 8) | athost;
541 tp->nxt = newhnamemem();
542 (void)sprintf(nambuf, "%s.%d", tp2->name, athost);
543 tp->name = savestr(nambuf);
544 return (tp->name);
545 }
546
547 tp->addr = (atnet << 8) | athost;
548 tp->nxt = newhnamemem();
549 if (athost != 255)
550 (void)sprintf(nambuf, "%d.%d", atnet, athost);
551 else
552 (void)sprintf(nambuf, "%d", atnet);
553 tp->name = savestr(nambuf);
554
555 return (tp->name);
556}
557
558static struct tok skt2str[] = {
559 { rtmpSkt, "rtmp" }, /* routing table maintenance */
560 { nbpSkt, "nis" }, /* name info socket */
561 { echoSkt, "echo" }, /* AppleTalk echo protocol */
562 { zipSkt, "zip" }, /* zone info protocol */
563 { 0, NULL }
564};
565
566static const char *
567ddpskt_string(register int skt)
568{
569 static char buf[8];
570
571 if (nflag) {
572 (void)sprintf(buf, "%d", skt);
573 return (buf);
574 }
575 return (tok2str(skt2str, "%d", skt));
576}
421 if (cp >= (char *)ep) {
422 fputs(tstr, stdout);
423 return (0);
424 }
425 putchar(*cp++);
426 }
427 return (cp);
428}
429
430static const struct atNBPtuple *
431nbp_tuple_print(register const struct atNBPtuple *tp,
432 register const u_char *ep,
433 register u_short snet, register u_char snode,
434 register u_char skt)
435{
436 register const struct atNBPtuple *tpn;
437
438 if ((const u_char *)(tp + 1) > ep) {
439 fputs(tstr, stdout);
440 return 0;
441 }
442 tpn = nbp_name_print(tp, ep);
443
444 /* if the enumerator isn't 1, print it */
445 if (tp->enumerator != 1)
446 (void)printf("(%d)", tp->enumerator);
447
448 /* if the socket doesn't match the src socket, print it */
449 if (tp->skt != skt)
450 (void)printf(" %d", tp->skt);
451
452 /* if the address doesn't match the src address, it's an anomaly */
453 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
454 (void)printf(" [addr=%s]",
455 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
456
457 return (tpn);
458}
459
460static const struct atNBPtuple *
461nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
462{
463 register const char *cp = (const char *)tp + nbpTupleSize;
464
465 putchar(' ');
466
467 /* Object */
468 putchar('"');
469 if ((cp = print_cstring(cp, ep)) != NULL) {
470 /* Type */
471 putchar(':');
472 if ((cp = print_cstring(cp, ep)) != NULL) {
473 /* Zone */
474 putchar('@');
475 if ((cp = print_cstring(cp, ep)) != NULL)
476 putchar('"');
477 }
478 }
479 return ((const struct atNBPtuple *)cp);
480}
481
482
483#define HASHNAMESIZE 4096
484
485struct hnamemem {
486 int addr;
487 char *name;
488 struct hnamemem *nxt;
489};
490
491static struct hnamemem hnametable[HASHNAMESIZE];
492
493static const char *
494ataddr_string(u_short atnet, u_char athost)
495{
496 register struct hnamemem *tp, *tp2;
497 register int i = (atnet << 8) | athost;
498 char nambuf[256];
499 static int first = 1;
500 FILE *fp;
501
502 /*
503 * if this is the first call, see if there's an AppleTalk
504 * number to name map file.
505 */
506 if (first && (first = 0, !nflag)
507 && (fp = fopen("/etc/atalk.names", "r"))) {
508 char line[256];
509 int i1, i2;
510
511 while (fgets(line, sizeof(line), fp)) {
512 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
513 continue;
514 if (sscanf(line, "%d.%d %s", &i1, &i2, nambuf) == 3)
515 /* got a hostname. */
516 i2 |= (i1 << 8);
517 else if (sscanf(line, "%d %s", &i1, nambuf) == 2)
518 /* got a net name */
519 i2 = (i1 << 8) | 255;
520 else
521 continue;
522
523 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
524 tp->nxt; tp = tp->nxt)
525 ;
526 tp->addr = i2;
527 tp->nxt = newhnamemem();
528 tp->name = savestr(nambuf);
529 }
530 fclose(fp);
531 }
532
533 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
534 if (tp->addr == i)
535 return (tp->name);
536
537 /* didn't have the node name -- see if we've got the net name */
538 i |= 255;
539 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
540 if (tp2->addr == i) {
541 tp->addr = (atnet << 8) | athost;
542 tp->nxt = newhnamemem();
543 (void)sprintf(nambuf, "%s.%d", tp2->name, athost);
544 tp->name = savestr(nambuf);
545 return (tp->name);
546 }
547
548 tp->addr = (atnet << 8) | athost;
549 tp->nxt = newhnamemem();
550 if (athost != 255)
551 (void)sprintf(nambuf, "%d.%d", atnet, athost);
552 else
553 (void)sprintf(nambuf, "%d", atnet);
554 tp->name = savestr(nambuf);
555
556 return (tp->name);
557}
558
559static struct tok skt2str[] = {
560 { rtmpSkt, "rtmp" }, /* routing table maintenance */
561 { nbpSkt, "nis" }, /* name info socket */
562 { echoSkt, "echo" }, /* AppleTalk echo protocol */
563 { zipSkt, "zip" }, /* zone info protocol */
564 { 0, NULL }
565};
566
567static const char *
568ddpskt_string(register int skt)
569{
570 static char buf[8];
571
572 if (nflag) {
573 (void)sprintf(buf, "%d", skt);
574 return (buf);
575 }
576 return (tok2str(skt2str, "%d", skt));
577}