summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop_apple.c
blob: bf6a6b4777f5c322db85a0e787fb561a8b4b5585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <at.h>
#include <snoop.h>

extern char *src_name, *dst_name;

struct socktable {
	int	pt_num;
	char	*pt_short;
};

static struct socktable pt_ddp[] = {
	{1,	"RTMP"},
	{2,	"NIS"},
	{4,	"Echoer"},
	{6,	"ZIS"},
	{0,	NULL},
};

static struct socktable pt_ddp_types[] = {
	{1,	"RTMP Resp"},
	{2,	"NBP"},
	{3,	"ATP"},
	{4,	"AEP"},
	{5,	"RTMP Req"},
	{6,	"ZIP"},
	{7,	"ADSP"},
	{0,	NULL},
};

static char *
apple_ddp_type(struct socktable *p, uint16_t port)
{
	for (; p->pt_num != 0; p++) {
		if (port == p->pt_num)
			return (p->pt_short);
	}
	return (NULL);
}

/*
 * return the short at p, regardless of alignment
 */

uint16_t
get_short(uint8_t *p)
{
	return (p[0] << 8 | p[1]);
}

/*
 * return the long at p, regardless of alignment
 */
uint32_t
get_long(uint8_t *p)
{
	return (p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]);
}

/*
 * format a MAC address
 */

char *
print_macaddr(uint8_t *ha, int len)
{
	static char buf[128];
	char *p = buf;

	while (len-- != 0) {
		p += snprintf(p, sizeof (buf) - (p - buf),
		    len > 0 ? "%x:" : "%x", *ha++);
	}
	return (buf);
}

/* ARGSUSED */
void
interpret_at(int flags, struct ddp_hdr *ddp, int len)
{
	int ddplen;
	char *pname;
	char buff [32];
	static char src_buf[16];
	static char dst_buf[16];

	if (ddp_pad(ddp) != 0)
		return;			/* unknown AppleTalk proto */

	ddplen = ddp_len(ddp);

	(void) snprintf(src_buf, sizeof (src_buf),
	    "%u.%u", ntohs(ddp->ddp_src_net), ddp->ddp_src_id);
	src_name = src_buf;

	(void) snprintf(dst_buf, sizeof (dst_buf),
	    "%u.%u", ntohs(ddp->ddp_dest_net), ddp->ddp_dest_id);
	if (ddp->ddp_dest_id == NODE_ID_BROADCAST)
		dst_name = "(broadcast)";
	else
		dst_name = dst_buf;

	if (flags & F_SUM) {
		(void) snprintf(get_sum_line(), MAXLINE,
		    "DDP S=%u.%u:%u D=%u.%u:%u LEN=%d",
		    ntohs(ddp->ddp_src_net),
		    ddp->ddp_src_id,
		    ddp->ddp_src_sock,
		    ntohs(ddp->ddp_dest_net),
		    ddp->ddp_dest_id,
		    ddp->ddp_dest_sock,
		    ddp_len(ddp));
	}

	if (flags & F_DTAIL) {
		show_header("DDP:  ", "DDP Header", ddplen - DDPHDR_SIZE);
		show_space();
		pname = apple_ddp_type(pt_ddp, ddp->ddp_src_sock);
		if (pname == NULL) {
			pname = "";
		} else {
			(void) snprintf(buff, sizeof (buff), "(%s)", pname);
			pname = buff;
		}

		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Source = %s, Socket = %u %s",
		    src_name, ddp->ddp_src_sock, pname);
		pname = apple_ddp_type(pt_ddp, ddp->ddp_dest_sock);
		if (pname == NULL) {
			pname = "";
		} else {
			(void) snprintf(buff, sizeof (buff), "(%s)", pname);
			pname = buff;
		}
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Destination = %s, Socket = %u %s",
		    dst_name, ddp->ddp_dest_sock, pname);
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Hop count = %d",
		    ddp_hop(ddp));
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Length = %d",
		    ddp_len(ddp));
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "Checksum = %04x %s",
		    ntohs(ddp->ddp_cksum),
		    ddp->ddp_cksum == 0 ? "(no checksum)" : "");
		(void) snprintf(get_line(0, 0), get_line_remain(),
		    "DDP type = %d (%s)",
		    ddp->ddp_type,
		    apple_ddp_type(pt_ddp_types, ddp->ddp_type));
		show_space();
	}


	/* go to the next protocol layer */

	switch (ddp->ddp_type) {
	case DDP_TYPE_NBP:
		interpret_nbp(flags, (struct nbp_hdr *)ddp, ddplen);
		break;
	case DDP_TYPE_AEP:
		interpret_aecho(flags, ddp, ddplen);
		break;
	case DDP_TYPE_ATP:
		interpret_atp(flags, ddp, ddplen);
		break;
	case DDP_TYPE_ZIP:
		interpret_ddp_zip(flags, (struct zip_hdr *)ddp, ddplen);
		break;
	case DDP_TYPE_ADSP:
		interpret_adsp(flags, (struct ddp_adsphdr *)ddp, ddplen);
		break;
	case DDP_TYPE_RTMPRQ:
	case DDP_TYPE_RTMPRESP:
		interpret_rtmp(flags, ddp, ddplen);
		break;
	}
}