summaryrefslogtreecommitdiff
path: root/usr.bin/netstat/atalk.c
blob: 2a2c9677abf76744f0bedd50c082c235749ddeb8 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*	$OpenBSD: atalk.c,v 1.11 2003/06/03 02:56:13 millert Exp $	*/
/*	$NetBSD: atalk.c,v 1.2 1997/05/22 17:21:26 christos Exp $	*/

/*
 * Copyright (c) 1983, 1988, 1993
 *	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 the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. 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 BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
#if 0
static char sccsid[] = "from @(#)atalk.c	1.1 (Whistle) 6/6/96";
#else
static char rcsid[] = "$OpenBSD: atalk.c,v 1.11 2003/06/03 02:56:13 millert Exp $";
#endif
#endif /* not lint */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <netdb.h>

#include <net/route.h>
#include <net/if.h>

/* #include <netinet/tcp_fsm.h> */

#include <netatalk/at.h>
#include <netatalk/ddp_var.h>

#include <nlist.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "netstat.h"

struct ddpcb    ddpcb;
struct socket   sockb;

static int first = 1;

static char *at_pr_net(struct sockaddr_at *, int);
static char *at_pr_host(struct sockaddr_at *, int);
static char *at_pr_range(struct sockaddr_at *);
static char *at_pr_port(struct sockaddr_at *);

/*
 * Print a summary of connections related to a Network Systems
 * protocol.  For XXX, also give state of connection.
 * Listening processes (aflag) are suppressed unless the
 * -a (all) flag is specified.
 */

static char *
at_pr_net(struct sockaddr_at *sat, int numeric)
{
	static char mybuf[50];

	if (!numeric) {
		switch (sat->sat_addr.s_net) {
		case 0xffff:
			return "????";
		case ATADDR_ANYNET:
			return ("*");
		}
	}
	(void) snprintf(mybuf, sizeof(mybuf), "%hu",
	    ntohs(sat->sat_addr.s_net));
	return mybuf;
}

static char *
at_pr_host(struct sockaddr_at *sat, int numeric)
{
	static char mybuf[50];

	if (!numeric) {
		switch (sat->sat_addr.s_node) {
		case ATADDR_BCAST:
			return "bcast";
		case ATADDR_ANYNODE:
			return ("*");
		}
	}
	(void) snprintf(mybuf, sizeof(mybuf), "%d",
	    (unsigned int) sat->sat_addr.s_node);
	return mybuf;
}

static char *
at_pr_port(struct sockaddr_at *sat)
{
	static char mybuf[50];
	struct servent *serv;

	switch (sat->sat_port) {
	case ATADDR_ANYPORT:
		return ("*");
	case 0xff:
		return "????";
	default:
		if (nflag)
			(void) snprintf(mybuf, sizeof(mybuf), "%d",
			    (unsigned int) sat->sat_port);
		else {
			serv = getservbyport(sat->sat_port, "ddp");
			if (serv == NULL)
				(void) snprintf(mybuf, sizeof(mybuf), "%d",
				    (unsigned int) sat->sat_port);
			else
				(void) snprintf(mybuf, sizeof(mybuf), "%s",
				    serv->s_name);
		}

		return mybuf;
	}
}

static char *
at_pr_range(struct sockaddr_at *sat)
{
	static char mybuf[50];

	if (sat->sat_range.r_netrange.nr_firstnet !=
	    sat->sat_range.r_netrange.nr_lastnet) {
		(void) snprintf(mybuf, sizeof(mybuf), "%d-%d",
		    ntohs(sat->sat_range.r_netrange.nr_firstnet),
		    ntohs(sat->sat_range.r_netrange.nr_lastnet));
	} else {
		(void) snprintf(mybuf, sizeof(mybuf), "%d",
		    ntohs(sat->sat_range.r_netrange.nr_firstnet));
	}
	return mybuf;
}


/* what == 0 for addr only == 3
 *	1 for net
 *	2 for host
 *	4 for port
 *	8 for numeric only
 */
char *
atalk_print(const struct sockaddr *sa, int what)
{
	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
	static char mybuf[50];
	int numeric = (what & 0x08);

	mybuf[0] = 0;
	switch (what & 0x13) {
	case 0:
		mybuf[0] = 0;
		break;
	case 1:
		(void) snprintf(mybuf, sizeof(mybuf), "%s",
		    at_pr_net(sat, numeric));
		break;
	case 2:
		(void) snprintf(mybuf, sizeof(mybuf), "%s",
		    at_pr_host(sat, numeric));
		break;
	case 3:
		(void) snprintf(mybuf, sizeof(mybuf), "%s.%s",
		    at_pr_net(sat, numeric),
		    at_pr_host(sat, numeric));
		break;
	case 0x10:
		(void) snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
	}
	if (what & 4) {
		(void) snprintf(mybuf + strlen(mybuf),
		    sizeof(mybuf) - strlen(mybuf), ".%s",
		    at_pr_port(sat));
	}
	return mybuf;
}

char *
atalk_print2(const struct sockaddr *sa, const struct sockaddr *mask, int what)
{
	size_t		n, l;
	static char     buf[100];
	struct sockaddr_at *sat1, *sat2;
	struct sockaddr_at thesockaddr;
	struct sockaddr *sa2;

	sat1 = (struct sockaddr_at *) sa;
	sat2 = (struct sockaddr_at *) mask;
	sa2 = (struct sockaddr *) & thesockaddr;

	thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
	    sat2->sat_addr.s_net;
	if ((n = snprintf(buf, sizeof(buf), "%s",
	    atalk_print(sa2, 1 | (what & 8)))) >= sizeof(buf))
		n = sizeof(buf) - 1;
	else if (n < 0)
		n = 0;	/* What else can be done ? */
	if (sat2->sat_addr.s_net != 0xFFFF) {
		thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
		    ~sat2->sat_addr.s_net;
		if ((l = snprintf(buf + n, sizeof(buf) - n,
		    "-%s", atalk_print(sa2, 1 | (what & 8)))) >= sizeof(buf) - n)
			l = sizeof(buf) - n - 1;
		if (l > 0)
			n += l;
	}
	if (what & 2) {
		l = snprintf(buf + n, sizeof(buf) - n, ".%s",
		    atalk_print(sa, what & (~1)));
		if (l >= sizeof(buf) - n)
			l = sizeof(buf) - n - 1;
		if (l > 0)
			n += l;
	}
	return (buf);
}

void
atalkprotopr(u_long off, char *name)
{
	struct ddpcb    cb;
	struct ddpcb *prev, *next;
	struct ddpcb   *initial;

	if (off == 0)
		return;
	if (kread(off, (char *) &initial, sizeof(struct ddpcb *)) < 0)
		return;
	ddpcb = cb;
	prev = (struct ddpcb *) off;
	for (next = initial; next != NULL; prev = next) {
		u_long	ppcb = (u_long) next;

		if (kread((u_long) next, (char *) &ddpcb, sizeof(ddpcb)) < 0)
			return;
		next = ddpcb.ddp_next;
#if 0
		if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
			continue;
		}
#endif
		if (kread((u_long) ddpcb.ddp_socket,
		    (char *) &sockb, sizeof(sockb)) < 0)
			return;
		if (first) {
			printf("Active ATALK connections");
			if (aflag)
				printf(" (including servers)");
			putchar('\n');
			if (Aflag)
				printf("%-8.8s ", "PCB");
			printf(Aflag ?
			    "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
			    "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
			    "Proto", "Recv-Q", "Send-Q",
			    "Local Address", "Foreign Address", "(state)");
			first = 0;
		}
		if (Aflag)
			printf("%8lx ", ppcb);
		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
		    sockb.so_snd.sb_cc);
		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
		    (struct sockaddr *) & ddpcb.ddp_lsat, 7));
		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
		    (struct sockaddr *) & ddpcb.ddp_fsat, 7));
		putchar('\n');
	}
}

#define p(f, m) if (ddpstat.f || sflag <= 1) \
	printf(m, ddpstat.f, plural(ddpstat.f))
#define p2(f1, f2, m) if (ddpstat.f1 || ddpstat.f2 || sflag <= 1) \
	printf(m, ddpstat.f1, plural(ddpstat.f1), ddpstat.f2, plural(ddpstat.f2))
#define p3(f, m) if (ddpstat.f || sflag <= 1) \
	printf(m, ddpstat.f, plurales(ddpstat.f))

/*
 * Dump DDP statistics structure.
 */
void
ddp_stats(u_long off, char *name)
{
	struct ddpstat  ddpstat;

	if (off == 0)
		return;
	if (kread(off, (char *) &ddpstat, sizeof(ddpstat)) < 0)
		return;
	printf("%s:\n", name);
	p(ddps_short, "\t%ld packet%s with short headers\n");
	p(ddps_long, "\t%ld packet%s with long headers\n");
	p(ddps_nosum, "\t%ld packet%s with no checksum\n");
	p(ddps_tooshort, "\t%ld packet%s were too short\n");
	p(ddps_badsum, "\t%ld packet%s with bad checksum\n");
	p(ddps_toosmall, "\t%ld packet%s with not enough data\n");
	p(ddps_forward, "\t%ld packet%s forwarded\n");
	p(ddps_cantforward, "\t%ld packet%s rcvd for unreachable dest\n");
	p(ddps_nosockspace, "\t%ld packet%s dropped due to no socket space\n");
}