]>
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.62 2000-10-10 05:03:32 guy Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
42 #include <netdb.h> /* for MAXHOSTNAMELEN on some platforms */
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "ethertype.h"
47 #include "extract.h" /* must come after interface.h */
48 #include "appletalk.h"
51 static struct tok type2str
[] = {
53 { ddpRTMPrequest
, "rtmpReq" },
62 u_int16_t htype
, ptype
;
63 u_int8_t halen
, palen
;
71 static char tstr
[] = "[|atalk]";
73 static void atp_print(const struct atATP
*, u_int
);
74 static void atp_bitmap_print(u_char
);
75 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
76 static const char *print_cstring(const char *, const u_char
*);
77 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
79 u_short
, u_char
, u_char
);
80 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
82 static const char *ataddr_string(u_short
, u_char
);
83 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
84 static const char *ddpskt_string(int);
87 * Print AppleTalk Datagram Delivery Protocol packets.
90 atalk_print(register const u_char
*bp
, u_int length
)
92 register const struct LAP
*lp
;
93 register const struct atDDP
*dp
;
94 register const struct atShortDDP
*sdp
;
97 lp
= (struct LAP
*)bp
;
99 length
-= sizeof(*lp
);
103 if (length
< ddpSSize
) {
104 (void)printf(" [|sddp %d]", length
);
107 sdp
= (const struct atShortDDP
*)bp
;
109 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
111 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
114 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
118 if (length
< ddpSize
) {
119 (void)printf(" [|ddp %d]", length
);
122 dp
= (const struct atDDP
*)bp
;
123 snet
= EXTRACT_16BITS(&dp
->srcNet
);
124 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
125 ddpskt_string(dp
->srcSkt
));
127 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
128 ddpskt_string(dp
->dstSkt
));
131 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
136 klap_print(bp
, length
);
141 printf("%d > %d at-lap#%d %d",
142 lp
->src
, lp
->dst
, lp
->type
, length
);
147 /* XXX should probably pass in the snap header and do checks like arp_print() */
149 aarp_print(register const u_char
*bp
, u_int length
)
151 register const struct aarp
*ap
;
153 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
156 ap
= (const struct aarp
*)bp
;
157 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
158 ap
->halen
== 6 && ap
->palen
== 4 )
159 switch (ntohs(ap
->op
)) {
161 case 1: /* request */
162 (void)printf("who-has %s tell %s",
163 AT(pdaddr
), AT(psaddr
));
166 case 2: /* response */
167 (void)printf("reply %s is-at %s",
168 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
171 case 3: /* probe (oy!) */
172 (void)printf("probe %s tell %s",
173 AT(pdaddr
), AT(psaddr
));
176 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
177 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
178 ap
->halen
, ap
->palen
);
182 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
183 register u_short snet
, register u_char snode
, u_char skt
)
189 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
193 atp_print((const struct atATP
*)bp
, length
);
197 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
203 atp_print(register const struct atATP
*ap
, u_int length
)
208 if ((const u_char
*)(ap
+ 1) > snapend
) {
209 /* Just bail if we don't have the whole chunk. */
213 length
-= sizeof(*ap
);
214 switch (ap
->control
& 0xc0) {
217 (void)printf(" atp-req%s %d",
218 ap
->control
& atpXO
? " " : "*",
219 EXTRACT_16BITS(&ap
->transID
));
221 atp_bitmap_print(ap
->bitmap
);
224 (void)printf(" [len=%d]", length
);
226 switch (ap
->control
& (atpEOM
|atpSTS
)) {
228 (void)printf(" [EOM]");
231 (void)printf(" [STS]");
234 (void)printf(" [EOM,STS]");
240 (void)printf(" atp-resp%s%d:%d (%d)",
241 ap
->control
& atpEOM
? "*" : " ",
242 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
243 switch (ap
->control
& (atpXO
|atpSTS
)) {
245 (void)printf(" [XO]");
248 (void)printf(" [STS]");
251 (void)printf(" [XO,STS]");
257 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
259 atp_bitmap_print(ap
->bitmap
);
261 /* length should be zero */
263 (void)printf(" [len=%d]", length
);
265 /* there shouldn't be any control flags */
266 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
268 if (ap
->control
& atpXO
) {
269 (void)printf("%cXO", c
);
272 if (ap
->control
& atpEOM
) {
273 (void)printf("%cEOM", c
);
276 if (ap
->control
& atpSTS
) {
277 (void)printf("%cSTS", c
);
285 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
286 EXTRACT_16BITS(&ap
->transID
), length
);
289 data
= EXTRACT_32BITS(&ap
->userData
);
291 (void)printf(" 0x%x", data
);
295 atp_bitmap_print(register u_char bm
)
301 * The '& 0xff' below is needed for compilers that want to sign
302 * extend a u_char, which is the case with the Ultrix compiler.
303 * (gcc is smart enough to eliminate it, at least on the Sparc).
305 if ((bm
+ 1) & (bm
& 0xff)) {
307 for (i
= 0; bm
; ++i
) {
309 (void)printf("%c%d", c
, i
);
319 (void)printf("<0-%d>", i
- 1);
326 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
327 register u_char snode
, register u_char skt
)
329 register const struct atNBPtuple
*tp
=
330 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
334 length
-= nbpHeaderSize
;
336 /* must be room for at least one tuple */
337 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
340 /* ep points to end of available data */
342 if ((const u_char
*)tp
> ep
) {
346 switch (i
= np
->control
& 0xf0) {
350 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
352 if ((const u_char
*)(tp
+ 1) > ep
) {
356 (void)nbp_name_print(tp
, ep
);
358 * look for anomalies: the spec says there can only
359 * be one tuple, the address must match the source
360 * address and the enumerator should be zero.
362 if ((np
->control
& 0xf) != 1)
363 (void)printf(" [ntup=%d]", np
->control
& 0xf);
365 (void)printf(" [enum=%d]", tp
->enumerator
);
366 if (EXTRACT_16BITS(&tp
->net
) != snet
||
367 tp
->node
!= snode
|| tp
->skt
!= skt
)
368 (void)printf(" [addr=%s.%d]",
369 ataddr_string(EXTRACT_16BITS(&tp
->net
),
374 (void)printf(" nbp-reply %d:", np
->id
);
376 /* print each of the tuples in the reply */
377 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
378 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
382 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
388 /* print a counted string */
390 print_cstring(register const char *cp
, register const u_char
*ep
)
392 register u_int length
;
394 if (cp
>= (const char *)ep
) {
400 /* Spec says string can be at most 32 bytes long */
402 (void)printf("[len=%u]", length
);
405 while ((int)--length
>= 0) {
406 if (cp
>= (char *)ep
) {
415 static const struct atNBPtuple
*
416 nbp_tuple_print(register const struct atNBPtuple
*tp
,
417 register const u_char
*ep
,
418 register u_short snet
, register u_char snode
,
421 register const struct atNBPtuple
*tpn
;
423 if ((const u_char
*)(tp
+ 1) > ep
) {
427 tpn
= nbp_name_print(tp
, ep
);
429 /* if the enumerator isn't 1, print it */
430 if (tp
->enumerator
!= 1)
431 (void)printf("(%d)", tp
->enumerator
);
433 /* if the socket doesn't match the src socket, print it */
435 (void)printf(" %d", tp
->skt
);
437 /* if the address doesn't match the src address, it's an anomaly */
438 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
439 (void)printf(" [addr=%s]",
440 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
445 static const struct atNBPtuple
*
446 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
448 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
454 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
457 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
460 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
464 return ((const struct atNBPtuple
*)cp
);
468 #define HASHNAMESIZE 4096
473 struct hnamemem
*nxt
;
476 static struct hnamemem hnametable
[HASHNAMESIZE
];
479 ataddr_string(u_short atnet
, u_char athost
)
481 register struct hnamemem
*tp
, *tp2
;
482 register int i
= (atnet
<< 8) | athost
;
483 char nambuf
[MAXHOSTNAMELEN
+ 20];
484 static int first
= 1;
488 * if this is the first call, see if there's an AppleTalk
489 * number to name map file.
491 if (first
&& (first
= 0, !nflag
)
492 && (fp
= fopen("/etc/atalk.names", "r"))) {
496 while (fgets(line
, sizeof(line
), fp
)) {
497 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
499 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
501 /* got a hostname. */
502 i3
|= ((i1
<< 8) | i2
) << 8;
503 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
506 i3
= (((i1
<< 8) | i2
) << 8) | 255;
510 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
511 tp
->nxt
; tp
= tp
->nxt
)
514 tp
->nxt
= newhnamemem();
515 tp
->name
= savestr(nambuf
);
520 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
524 /* didn't have the node name -- see if we've got the net name */
526 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
527 if (tp2
->addr
== i
) {
528 tp
->addr
= (atnet
<< 8) | athost
;
529 tp
->nxt
= newhnamemem();
530 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
532 tp
->name
= savestr(nambuf
);
536 tp
->addr
= (atnet
<< 8) | athost
;
537 tp
->nxt
= newhnamemem();
539 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
540 atnet
>> 8, atnet
& 0xff, athost
);
542 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
544 tp
->name
= savestr(nambuf
);
549 static struct tok skt2str
[] = {
550 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
551 { nbpSkt
, "nis" }, /* name info socket */
552 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
553 { zipSkt
, "zip" }, /* zone info protocol */
558 ddpskt_string(register int skt
)
563 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
566 return (tok2str(skt2str
, "%d", skt
));