]>
The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
fd75f756dc37be71390e1385fc9ce11febd8512e
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Format and print AppleTalk packets.
25 static const char rcsid
[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.60 2000-10-05 04:10:01 itojun Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
42 #include <netinet/in.h>
48 #include "interface.h"
49 #include "addrtoname.h"
50 #include "ethertype.h"
51 #include "extract.h" /* must come after interface.h */
52 #include "appletalk.h"
55 static struct tok type2str
[] = {
57 { ddpRTMPrequest
, "rtmpReq" },
66 u_int16_t htype
, ptype
;
67 u_int8_t halen
, palen
;
75 static char tstr
[] = "[|atalk]";
77 static void atp_print(const struct atATP
*, u_int
);
78 static void atp_bitmap_print(u_char
);
79 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
80 static const char *print_cstring(const char *, const u_char
*);
81 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
83 u_short
, u_char
, u_char
);
84 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
86 static const char *ataddr_string(u_short
, u_char
);
87 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
88 static const char *ddpskt_string(int);
91 * Print AppleTalk Datagram Delivery Protocol packets.
94 atalk_print(register const u_char
*bp
, u_int length
)
96 register const struct LAP
*lp
;
97 register const struct atDDP
*dp
;
98 register const struct atShortDDP
*sdp
;
101 lp
= (struct LAP
*)bp
;
103 length
-= sizeof(*lp
);
107 if (length
< ddpSSize
) {
108 (void)printf(" [|sddp %d]", length
);
111 sdp
= (const struct atShortDDP
*)bp
;
113 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
115 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
118 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
122 if (length
< ddpSize
) {
123 (void)printf(" [|ddp %d]", length
);
126 dp
= (const struct atDDP
*)bp
;
127 snet
= EXTRACT_16BITS(&dp
->srcNet
);
128 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
129 ddpskt_string(dp
->srcSkt
));
131 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
132 ddpskt_string(dp
->dstSkt
));
135 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
140 klap_print(bp
, length
);
145 printf("%d > %d at-lap#%d %d",
146 lp
->src
, lp
->dst
, lp
->type
, length
);
151 /* XXX should probably pass in the snap header and do checks like arp_print() */
153 aarp_print(register const u_char
*bp
, u_int length
)
155 register const struct aarp
*ap
;
157 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
160 ap
= (const struct aarp
*)bp
;
161 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
162 ap
->halen
== 6 && ap
->palen
== 4 )
163 switch (ntohs(ap
->op
)) {
165 case 1: /* request */
166 (void)printf("who-has %s tell %s",
167 AT(pdaddr
), AT(psaddr
));
170 case 2: /* response */
171 (void)printf("reply %s is-at %s",
172 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
175 case 3: /* probe (oy!) */
176 (void)printf("probe %s tell %s",
177 AT(pdaddr
), AT(psaddr
));
180 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
181 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
182 ap
->halen
, ap
->palen
);
186 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
187 register u_short snet
, register u_char snode
, u_char skt
)
193 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
197 atp_print((const struct atATP
*)bp
, length
);
201 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
207 atp_print(register const struct atATP
*ap
, u_int length
)
212 if ((const u_char
*)(ap
+ 1) > snapend
) {
213 /* Just bail if we don't have the whole chunk. */
217 length
-= sizeof(*ap
);
218 switch (ap
->control
& 0xc0) {
221 (void)printf(" atp-req%s %d",
222 ap
->control
& atpXO
? " " : "*",
223 EXTRACT_16BITS(&ap
->transID
));
225 atp_bitmap_print(ap
->bitmap
);
228 (void)printf(" [len=%d]", length
);
230 switch (ap
->control
& (atpEOM
|atpSTS
)) {
232 (void)printf(" [EOM]");
235 (void)printf(" [STS]");
238 (void)printf(" [EOM,STS]");
244 (void)printf(" atp-resp%s%d:%d (%d)",
245 ap
->control
& atpEOM
? "*" : " ",
246 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
247 switch (ap
->control
& (atpXO
|atpSTS
)) {
249 (void)printf(" [XO]");
252 (void)printf(" [STS]");
255 (void)printf(" [XO,STS]");
261 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
263 atp_bitmap_print(ap
->bitmap
);
265 /* length should be zero */
267 (void)printf(" [len=%d]", length
);
269 /* there shouldn't be any control flags */
270 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
272 if (ap
->control
& atpXO
) {
273 (void)printf("%cXO", c
);
276 if (ap
->control
& atpEOM
) {
277 (void)printf("%cEOM", c
);
280 if (ap
->control
& atpSTS
) {
281 (void)printf("%cSTS", c
);
289 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
290 EXTRACT_16BITS(&ap
->transID
), length
);
293 data
= EXTRACT_32BITS(&ap
->userData
);
295 (void)printf(" 0x%x", data
);
299 atp_bitmap_print(register u_char bm
)
305 * The '& 0xff' below is needed for compilers that want to sign
306 * extend a u_char, which is the case with the Ultrix compiler.
307 * (gcc is smart enough to eliminate it, at least on the Sparc).
309 if ((bm
+ 1) & (bm
& 0xff)) {
311 for (i
= 0; bm
; ++i
) {
313 (void)printf("%c%d", c
, i
);
323 (void)printf("<0-%d>", i
- 1);
330 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
331 register u_char snode
, register u_char skt
)
333 register const struct atNBPtuple
*tp
=
334 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
338 length
-= nbpHeaderSize
;
340 /* must be room for at least one tuple */
341 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
344 /* ep points to end of available data */
346 if ((const u_char
*)tp
> ep
) {
350 switch (i
= np
->control
& 0xf0) {
354 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
356 if ((const u_char
*)(tp
+ 1) > ep
) {
360 (void)nbp_name_print(tp
, ep
);
362 * look for anomalies: the spec says there can only
363 * be one tuple, the address must match the source
364 * address and the enumerator should be zero.
366 if ((np
->control
& 0xf) != 1)
367 (void)printf(" [ntup=%d]", np
->control
& 0xf);
369 (void)printf(" [enum=%d]", tp
->enumerator
);
370 if (EXTRACT_16BITS(&tp
->net
) != snet
||
371 tp
->node
!= snode
|| tp
->skt
!= skt
)
372 (void)printf(" [addr=%s.%d]",
373 ataddr_string(EXTRACT_16BITS(&tp
->net
),
378 (void)printf(" nbp-reply %d:", np
->id
);
380 /* print each of the tuples in the reply */
381 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
382 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
386 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
392 /* print a counted string */
394 print_cstring(register const char *cp
, register const u_char
*ep
)
396 register u_int length
;
398 if (cp
>= (const char *)ep
) {
404 /* Spec says string can be at most 32 bytes long */
406 (void)printf("[len=%u]", length
);
409 while ((int)--length
>= 0) {
410 if (cp
>= (char *)ep
) {
419 static const struct atNBPtuple
*
420 nbp_tuple_print(register const struct atNBPtuple
*tp
,
421 register const u_char
*ep
,
422 register u_short snet
, register u_char snode
,
425 register const struct atNBPtuple
*tpn
;
427 if ((const u_char
*)(tp
+ 1) > ep
) {
431 tpn
= nbp_name_print(tp
, ep
);
433 /* if the enumerator isn't 1, print it */
434 if (tp
->enumerator
!= 1)
435 (void)printf("(%d)", tp
->enumerator
);
437 /* if the socket doesn't match the src socket, print it */
439 (void)printf(" %d", tp
->skt
);
441 /* if the address doesn't match the src address, it's an anomaly */
442 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
443 (void)printf(" [addr=%s]",
444 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
449 static const struct atNBPtuple
*
450 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
452 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
458 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
461 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
464 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
468 return ((const struct atNBPtuple
*)cp
);
472 #define HASHNAMESIZE 4096
477 struct hnamemem
*nxt
;
480 static struct hnamemem hnametable
[HASHNAMESIZE
];
483 ataddr_string(u_short atnet
, u_char athost
)
485 register struct hnamemem
*tp
, *tp2
;
486 register int i
= (atnet
<< 8) | athost
;
487 char nambuf
[MAXHOSTNAMELEN
+ 20];
488 static int first
= 1;
492 * if this is the first call, see if there's an AppleTalk
493 * number to name map file.
495 if (first
&& (first
= 0, !nflag
)
496 && (fp
= fopen("/etc/atalk.names", "r"))) {
500 while (fgets(line
, sizeof(line
), fp
)) {
501 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
503 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
505 /* got a hostname. */
506 i3
|= ((i1
<< 8) | i2
) << 8;
507 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
510 i3
= (((i1
<< 8) | i2
) << 8) | 255;
514 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
515 tp
->nxt
; tp
= tp
->nxt
)
518 tp
->nxt
= newhnamemem();
519 tp
->name
= savestr(nambuf
);
524 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
528 /* didn't have the node name -- see if we've got the net name */
530 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
531 if (tp2
->addr
== i
) {
532 tp
->addr
= (atnet
<< 8) | athost
;
533 tp
->nxt
= newhnamemem();
534 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
536 tp
->name
= savestr(nambuf
);
540 tp
->addr
= (atnet
<< 8) | athost
;
541 tp
->nxt
= newhnamemem();
543 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
544 atnet
>> 8, atnet
& 0xff, athost
);
546 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
548 tp
->name
= savestr(nambuf
);
553 static struct tok skt2str
[] = {
554 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
555 { nbpSkt
, "nis" }, /* name info socket */
556 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
557 { zipSkt
, "zip" }, /* zone info protocol */
562 ddpskt_string(register int skt
)
567 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
570 return (tok2str(skt2str
, "%d", skt
));