/*
* Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/* XXX should probably pass in the snap header and do checks like arp_print() */
void
aarp_print(netdissect_options *ndo,
const u_char *bp, u_int length)
{
const struct aarp *ap;
if ((const u_char *)(ap + 1) > ndo->ndo_snapend) {
/* Just bail if we don't have the whole chunk. */
nd_print_trunc(ndo);
return;
}
if (length < sizeof(*ap)) {
ND_PRINT(" [|atp %u]", length);
return;
}
length -= sizeof(*ap);
control = GET_U_1(ap->control);
switch (control & 0xc0) {
case atpReqCode:
ND_PRINT(" atp-req%s %u",
control & atpXO? " " : "*",
GET_BE_U_2(ap->transID));
atp_bitmap_print(ndo, GET_U_1(ap->bitmap));
if (length != 0)
ND_PRINT(" [len=%u]", length);
switch (control & (atpEOM|atpSTS)) {
case atpEOM:
ND_PRINT(" [EOM]");
break;
case atpSTS:
ND_PRINT(" [STS]");
break;
case atpEOM|atpSTS:
ND_PRINT(" [EOM,STS]");
break;
}
break;
case atpRspCode:
ND_PRINT(" atp-resp%s%u:%u (%u)",
control & atpEOM? "*" : " ",
GET_BE_U_2(ap->transID), GET_U_1(ap->bitmap),
length);
switch (control & (atpXO|atpSTS)) {
case atpXO:
ND_PRINT(" [XO]");
break;
case atpSTS:
ND_PRINT(" [STS]");
break;
case atpXO|atpSTS:
ND_PRINT(" [XO,STS]");
break;
}
break;
case atpRelCode:
ND_PRINT(" atp-rel %u", GET_BE_U_2(ap->transID));
atp_bitmap_print(ndo, GET_U_1(ap->bitmap));
/* length should be zero */
if (length)
ND_PRINT(" [len=%u]", length);
/* there shouldn't be any control flags */
if (control & (atpXO|atpEOM|atpSTS)) {
char c = '[';
if (control & atpXO) {
ND_PRINT("%cXO", c);
c = ',';
}
if (control & atpEOM) {
ND_PRINT("%cEOM", c);
c = ',';
}
if (control & atpSTS) {
ND_PRINT("%cSTS", c);
}
ND_PRINT("]");
}
break;
/*
* The '& 0xff' below is needed for compilers that want to sign
* extend a u_char, which is the case with the Ultrix compiler.
* (gcc is smart enough to eliminate it, at least on the Sparc).
*/
if ((bm + 1) & (bm & 0xff)) {
char c = '<';
for (i = 0; bm; ++i) {
if (bm & 1) {
ND_PRINT("%c%u", c, i);
c = ',';
}
bm >>= 1;
}
ND_PRINT(">");
} else {
for (i = 0; bm; ++i)
bm >>= 1;
if (i > 1)
ND_PRINT("<0-%u>", i - 1);
else
ND_PRINT("<0>");
}
}
length -= nbpHeaderSize;
if (length < 8) {
/* must be room for at least one tuple */
ND_PRINT(" truncated-nbp %u", length + nbpHeaderSize);
return;
}
/* ep points to end of available data */
ep = ndo->ndo_snapend;
if ((const u_char *)tp > ep) {
nd_print_trunc(ndo);
return;
}
control = GET_U_1(np->control);
switch (i = (control & 0xf0)) {
case nbpBrRq:
case nbpLkUp:
ND_PRINT(i == nbpLkUp? " nbp-lkup %u:":" nbp-brRq %u:",
GET_U_1(np->id));
if ((const u_char *)(tp + 1) > ep) {
nd_print_trunc(ndo);
return;
}
(void)nbp_name_print(ndo, tp, ep);
/*
* look for anomalies: the spec says there can only
* be one tuple, the address must match the source
* address and the enumerator should be zero.
*/
if ((control & 0xf) != 1)
ND_PRINT(" [ntup=%u]", control & 0xf);
if (GET_U_1(tp->enumerator))
ND_PRINT(" [enum=%u]", GET_U_1(tp->enumerator));
if (GET_BE_U_2(tp->net) != snet ||
GET_U_1(tp->node) != snode ||
GET_U_1(tp->skt) != skt)
ND_PRINT(" [addr=%s.%u]",
ataddr_string(ndo, GET_BE_U_2(tp->net),
GET_U_1(tp->node)),
GET_U_1(tp->skt));
break;
case nbpLkUpReply:
ND_PRINT(" nbp-reply %u:", GET_U_1(np->id));
/* print each of the tuples in the reply */
for (i = control & 0xf; i != 0 && tp; i--)
tp = nbp_tuple_print(ndo, tp, ep, snet, snode, skt);
break;
/* if the enumerator isn't 1, print it */
if (GET_U_1(tp->enumerator) != 1)
ND_PRINT("(%u)", GET_U_1(tp->enumerator));
/* if the socket doesn't match the src socket, print it */
if (GET_U_1(tp->skt) != skt)
ND_PRINT(" %u", GET_U_1(tp->skt));
/* if the address doesn't match the src address, it's an anomaly */
if (GET_BE_U_2(tp->net) != snet ||
GET_U_1(tp->node) != snode)
ND_PRINT(" [addr=%s]",
ataddr_string(ndo, GET_BE_U_2(tp->net), GET_U_1(tp->node)));
/*
* Are we doing address to name resolution?
*/
if (!ndo->ndo_nflag) {
/*
* Yes. Have we tried to open and read an AppleTalk
* number to name map file?
*/
if (!first) {
/*
* No; try to do so.
*/
first = 0;
fp = fopen("/etc/atalk.names", "r");
if (fp != NULL) {
char line[256];
u_int i1, i2;
while (fgets(line, sizeof(line), fp)) {
if (line[0] == '\n' || line[0] == 0 ||
line[0] == '#')
continue;
if (sscanf(line, "%u.%u %256s", &i1,
&i2, nambuf) == 3)
/* got a hostname. */
i2 |= (i1 << 8);
else if (sscanf(line, "%u %256s", &i1,
nambuf) == 2)
/* got a net name */
i2 = (i1 << 8) | 255;
else
continue;
/*
* Now try to look up the address in the table.
*/
for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
if (tp->addr == i)
return (tp->name);
/* didn't have the node name -- see if we've got the net name */
i |= 255;
for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
if (tp2->addr == i) {
tp->addr = (atnet << 8) | athost;
tp->nxt = newhnamemem(ndo);
(void)snprintf(nambuf, sizeof(nambuf), "%s.%u",
tp2->name, athost);
tp->name = strdup(nambuf);
if (tp->name == NULL)
(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
"%s: strdup(nambuf)", __func__);
return (tp->name);
}