]>
The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
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.52 2000-01-17 06:24:24 itojun Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/udp.h>
49 #include <netinet/udp_var.h>
50 #include <netinet/tcp.h>
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "ethertype.h"
59 #include "extract.h" /* must come after interface.h */
60 #include "appletalk.h"
63 static struct tok type2str
[] = {
65 { ddpRTMPrequest
, "rtmpReq" },
83 static char tstr
[] = "[|atalk]";
85 static void atp_print(const struct atATP
*, u_int
);
86 static void atp_bitmap_print(u_char
);
87 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
88 static const char *print_cstring(const char *, const u_char
*);
89 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
91 u_short
, u_char
, u_char
);
92 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
94 static const char *ataddr_string(u_short
, u_char
);
95 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
96 static const char *ddpskt_string(int);
99 * Print AppleTalk Datagram Delivery Protocol packets.
102 atalk_print(register const u_char
*bp
, u_int length
)
104 register const struct LAP
*lp
;
105 register const struct atDDP
*dp
;
106 register const struct atShortDDP
*sdp
;
109 lp
= (struct LAP
*)bp
;
111 length
-= sizeof(*lp
);
115 if (length
< ddpSSize
) {
116 (void)printf(" [|sddp %d]", length
);
119 sdp
= (const struct atShortDDP
*)bp
;
121 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
123 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
126 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
130 if (length
< ddpSize
) {
131 (void)printf(" [|ddp %d]", length
);
134 dp
= (const struct atDDP
*)bp
;
135 snet
= EXTRACT_16BITS(&dp
->srcNet
);
136 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
137 ddpskt_string(dp
->srcSkt
));
139 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
140 ddpskt_string(dp
->dstSkt
));
143 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
148 klap_print(bp
, length
);
153 printf("%d > %d at-lap#%d %d",
154 lp
->src
, lp
->dst
, lp
->type
, length
);
159 /* XXX should probably pass in the snap header and do checks like arp_print() */
161 aarp_print(register const u_char
*bp
, u_int length
)
163 register const struct aarp
*ap
;
165 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
168 ap
= (const struct aarp
*)bp
;
169 if (ap
->htype
== 1 && ap
->ptype
== ETHERTYPE_ATALK
&&
170 ap
->halen
== 6 && ap
->palen
== 4 )
173 case 1: /* request */
174 (void)printf("who-has %s tell %s",
175 AT(pdaddr
), AT(psaddr
));
178 case 2: /* response */
179 (void)printf("reply %s is-at %s",
180 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
183 case 3: /* probe (oy!) */
184 (void)printf("probe %s tell %s",
185 AT(pdaddr
), AT(psaddr
));
188 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
189 length
, ap
->op
, ap
->htype
, ap
->ptype
, ap
->halen
, ap
->palen
);
193 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
194 register u_short snet
, register u_char snode
, u_char skt
)
200 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
204 atp_print((const struct atATP
*)bp
, length
);
208 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
214 atp_print(register const struct atATP
*ap
, u_int length
)
219 if ((const u_char
*)(ap
+ 1) > snapend
) {
220 /* Just bail if we don't have the whole chunk. */
224 length
-= sizeof(*ap
);
225 switch (ap
->control
& 0xc0) {
228 (void)printf(" atp-req%s %d",
229 ap
->control
& atpXO
? " " : "*",
230 EXTRACT_16BITS(&ap
->transID
));
232 atp_bitmap_print(ap
->bitmap
);
235 (void)printf(" [len=%d]", length
);
237 switch (ap
->control
& (atpEOM
|atpSTS
)) {
239 (void)printf(" [EOM]");
242 (void)printf(" [STS]");
245 (void)printf(" [EOM,STS]");
251 (void)printf(" atp-resp%s%d:%d (%d)",
252 ap
->control
& atpEOM
? "*" : " ",
253 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
254 switch (ap
->control
& (atpXO
|atpSTS
)) {
256 (void)printf(" [XO]");
259 (void)printf(" [STS]");
262 (void)printf(" [XO,STS]");
268 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
270 atp_bitmap_print(ap
->bitmap
);
272 /* length should be zero */
274 (void)printf(" [len=%d]", length
);
276 /* there shouldn't be any control flags */
277 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
279 if (ap
->control
& atpXO
) {
280 (void)printf("%cXO", c
);
283 if (ap
->control
& atpEOM
) {
284 (void)printf("%cEOM", c
);
287 if (ap
->control
& atpSTS
) {
288 (void)printf("%cSTS", c
);
296 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
297 EXTRACT_16BITS(&ap
->transID
), length
);
300 data
= EXTRACT_32BITS(&ap
->userData
);
302 (void)printf(" 0x%x", data
);
306 atp_bitmap_print(register u_char bm
)
312 * The '& 0xff' below is needed for compilers that want to sign
313 * extend a u_char, which is the case with the Ultrix compiler.
314 * (gcc is smart enough to eliminate it, at least on the Sparc).
316 if ((bm
+ 1) & (bm
& 0xff)) {
318 for (i
= 0; bm
; ++i
) {
320 (void)printf("%c%d", c
, i
);
330 (void)printf("<0-%d>", i
- 1);
337 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
338 register u_char snode
, register u_char skt
)
340 register const struct atNBPtuple
*tp
=
341 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
345 length
-= nbpHeaderSize
;
347 /* must be room for at least one tuple */
348 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
351 /* ep points to end of available data */
353 if ((const u_char
*)tp
> ep
) {
357 switch (i
= np
->control
& 0xf0) {
361 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
363 if ((const u_char
*)(tp
+ 1) > ep
) {
367 (void)nbp_name_print(tp
, ep
);
369 * look for anomalies: the spec says there can only
370 * be one tuple, the address must match the source
371 * address and the enumerator should be zero.
373 if ((np
->control
& 0xf) != 1)
374 (void)printf(" [ntup=%d]", np
->control
& 0xf);
376 (void)printf(" [enum=%d]", tp
->enumerator
);
377 if (EXTRACT_16BITS(&tp
->net
) != snet
||
378 tp
->node
!= snode
|| tp
->skt
!= skt
)
379 (void)printf(" [addr=%s.%d]",
380 ataddr_string(EXTRACT_16BITS(&tp
->net
),
385 (void)printf(" nbp-reply %d:", np
->id
);
387 /* print each of the tuples in the reply */
388 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
389 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
393 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
399 /* print a counted string */
401 print_cstring(register const char *cp
, register const u_char
*ep
)
403 register u_int length
;
405 if (cp
>= (const char *)ep
) {
411 /* Spec says string can be at most 32 bytes long */
413 (void)printf("[len=%u]", length
);
416 while ((int)--length
>= 0) {
417 if (cp
>= (char *)ep
) {
426 static const struct atNBPtuple
*
427 nbp_tuple_print(register const struct atNBPtuple
*tp
,
428 register const u_char
*ep
,
429 register u_short snet
, register u_char snode
,
432 register const struct atNBPtuple
*tpn
;
434 if ((const u_char
*)(tp
+ 1) > ep
) {
438 tpn
= nbp_name_print(tp
, ep
);
440 /* if the enumerator isn't 1, print it */
441 if (tp
->enumerator
!= 1)
442 (void)printf("(%d)", tp
->enumerator
);
444 /* if the socket doesn't match the src socket, print it */
446 (void)printf(" %d", tp
->skt
);
448 /* if the address doesn't match the src address, it's an anomaly */
449 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
450 (void)printf(" [addr=%s]",
451 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
456 static const struct atNBPtuple
*
457 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
459 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
465 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
468 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
471 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
475 return ((const struct atNBPtuple
*)cp
);
479 #define HASHNAMESIZE 4096
484 struct hnamemem
*nxt
;
487 static struct hnamemem hnametable
[HASHNAMESIZE
];
490 ataddr_string(u_short atnet
, u_char athost
)
492 register struct hnamemem
*tp
, *tp2
;
493 register int i
= (atnet
<< 8) | athost
;
495 static int first
= 1;
499 * if this is the first call, see if there's an AppleTalk
500 * number to name map file.
502 if (first
&& (first
= 0, !nflag
)
503 && (fp
= fopen("/etc/atalk.names", "r"))) {
507 while (fgets(line
, sizeof(line
), fp
)) {
508 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
510 if (sscanf(line
, "%d.%d.%d %s", &i1
, &i2
, &i3
,
512 /* got a hostname. */
513 i3
|= ((i1
<< 8) | i2
) << 8;
514 else if (sscanf(line
, "%d.%d %s", &i1
, &i2
,
517 i3
= (((i1
<< 8) | i2
) << 8) | 255;
521 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
522 tp
->nxt
; tp
= tp
->nxt
)
525 tp
->nxt
= newhnamemem();
526 tp
->name
= savestr(nambuf
);
531 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
535 /* didn't have the node name -- see if we've got the net name */
537 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
538 if (tp2
->addr
== i
) {
539 tp
->addr
= (atnet
<< 8) | athost
;
540 tp
->nxt
= newhnamemem();
541 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
543 tp
->name
= savestr(nambuf
);
547 tp
->addr
= (atnet
<< 8) | athost
;
548 tp
->nxt
= newhnamemem();
550 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
551 atnet
>> 8, atnet
& 0xff, athost
);
553 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
555 tp
->name
= savestr(nambuf
);
560 static struct tok skt2str
[] = {
561 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
562 { nbpSkt
, "nis" }, /* name info socket */
563 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
564 { zipSkt
, "zip" }, /* zone info protocol */
569 ddpskt_string(register int skt
)
574 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
577 return (tok2str(skt2str
, "%d", skt
));