Home | History | Annotate | Line # | Download | only in netstat
atalk.c revision 1.3
      1 /*	$NetBSD: atalk.c,v 1.3 1997/10/19 05:49:56 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "from @(#)atalk.c	1.1 (Whistle) 6/6/96";
     40 #else
     41 __RCSID("$NetBSD: atalk.c,v 1.3 1997/10/19 05:49:56 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/queue.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/protosw.h>
     51 
     52 #include <net/route.h>
     53 #include <net/if.h>
     54 
     55 #include <netinet/tcp_fsm.h>
     56 
     57 #include <netatalk/at.h>
     58 #include <netatalk/ddp_var.h>
     59 
     60 #include <nlist.h>
     61 #include <errno.h>
     62 #include <stdio.h>
     63 #include <string.h>
     64 #include "netstat.h"
     65 
     66 struct ddpcb    ddpcb;
     67 struct socket   sockb;
     68 
     69 static int first = 1;
     70 
     71 static char *at_pr_net __P((struct sockaddr_at *, int));
     72 static char *at_pr_host __P((struct sockaddr_at *, int));
     73 static char *at_pr_range __P((struct sockaddr_at *));
     74 static char *at_pr_port __P((struct sockaddr_at *));
     75 
     76 /*
     77  * Print a summary of connections related to a Network Systems
     78  * protocol.  For XXX, also give state of connection.
     79  * Listening processes (aflag) are suppressed unless the
     80  * -a (all) flag is specified.
     81  */
     82 
     83 static char *
     84 at_pr_net(sat, numeric)
     85 	struct sockaddr_at *sat;
     86 	int numeric;
     87 {
     88 	static char mybuf[50];
     89 
     90 	if (!numeric) {
     91 		switch (sat->sat_addr.s_net) {
     92 		case 0xffff:
     93 			return "????";
     94 		case ATADDR_ANYNET:
     95 			return ("*");
     96 		}
     97 	}
     98 	(void) snprintf(mybuf, sizeof(mybuf), "%hu",
     99 	    ntohs(sat->sat_addr.s_net));
    100 	return mybuf;
    101 }
    102 
    103 static char *
    104 at_pr_host(sat, numeric)
    105 	struct sockaddr_at *sat;
    106 	int numeric;
    107 {
    108 	static char mybuf[50];
    109 
    110 	if (!numeric) {
    111 		switch (sat->sat_addr.s_node) {
    112 		case ATADDR_BCAST:
    113 			return "bcast";
    114 		case ATADDR_ANYNODE:
    115 			return ("*");
    116 		}
    117 	}
    118 	(void) snprintf(mybuf, sizeof(mybuf), "%d",
    119 	    (unsigned int) sat->sat_addr.s_node);
    120 	return mybuf;
    121 }
    122 
    123 static char *
    124 at_pr_port(sat)
    125 	struct sockaddr_at *sat;
    126 {
    127 	static char mybuf[50];
    128 
    129 	switch (sat->sat_port) {
    130 	case ATADDR_ANYPORT:
    131 		return ("*");
    132 	case 0xff:
    133 		return "????";
    134 	default:
    135 		(void) snprintf(mybuf, sizeof(mybuf), "%d",
    136 		    (unsigned int) sat->sat_port);
    137 		return mybuf;
    138 	}
    139 }
    140 
    141 static char *
    142 at_pr_range(sat)
    143 	struct sockaddr_at *sat;
    144 {
    145 	static char mybuf[50];
    146 
    147 	if (sat->sat_range.r_netrange.nr_firstnet
    148 	    != sat->sat_range.r_netrange.nr_lastnet) {
    149 		(void) snprintf(mybuf, sizeof(mybuf), "%d-%d",
    150 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
    151 			ntohs(sat->sat_range.r_netrange.nr_lastnet));
    152 	} else {
    153 		(void) snprintf(mybuf, sizeof(mybuf), "%d",
    154 			ntohs(sat->sat_range.r_netrange.nr_firstnet));
    155 	}
    156 	return mybuf;
    157 }
    158 
    159 
    160 /* what == 0 for addr only == 3
    161  *	1 for net
    162  *	2 for host
    163  *	4 for port
    164  *	8 for numeric only
    165  */
    166 char *
    167 atalk_print(sa, what)
    168 	const struct sockaddr *sa;
    169 	int what;
    170 {
    171 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
    172 	static char mybuf[50];
    173 	int numeric = (what & 0x08);
    174 
    175 	mybuf[0] = 0;
    176 	switch (what & 0x13) {
    177 	case 0:
    178 		mybuf[0] = 0;
    179 		break;
    180 	case 1:
    181 		(void) snprintf(mybuf, sizeof(mybuf), "%s",
    182 		    at_pr_net(sat, numeric));
    183 		break;
    184 	case 2:
    185 		(void) snprintf(mybuf, sizeof(mybuf), "%s",
    186 		    at_pr_host(sat, numeric));
    187 		break;
    188 	case 3:
    189 		(void) snprintf(mybuf, sizeof(mybuf), "%s.%s",
    190 			at_pr_net(sat, numeric),
    191 			at_pr_host(sat, numeric));
    192 		break;
    193 	case 0x10:
    194 		(void) snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
    195 	}
    196 	if (what & 4) {
    197 		(void) snprintf(mybuf + strlen(mybuf),
    198 		    sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
    199 	}
    200 	return mybuf;
    201 }
    202 
    203 char *
    204 atalk_print2(sa, mask, what)
    205 	const struct sockaddr *sa;
    206 	const struct sockaddr *mask;
    207 	int what;
    208 {
    209 	int             n;
    210 	static char     buf[100];
    211 	struct sockaddr_at *sat1, *sat2;
    212 	struct sockaddr_at thesockaddr;
    213 	struct sockaddr *sa2;
    214 
    215 	sat1 = (struct sockaddr_at *) sa;
    216 	sat2 = (struct sockaddr_at *) mask;
    217 	sa2 = (struct sockaddr *) & thesockaddr;
    218 
    219 	thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
    220 	    sat2->sat_addr.s_net;
    221 	n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
    222 	if (sat2->sat_addr.s_net != 0xFFFF) {
    223 		thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
    224 		    ~sat2->sat_addr.s_net;
    225 		n += snprintf(buf + n, sizeof(buf) - n,
    226 		    "-%s", atalk_print(sa2, 1 | (what & 8)));
    227 	}
    228 	if (what & 2)
    229 		n += snprintf(buf + n, sizeof(buf) - n, ".%s",
    230 		    atalk_print(sa, what & (~1)));
    231 	return (buf);
    232 }
    233 
    234 void
    235 atalkprotopr(off, name)
    236 	u_long off;
    237 	char  *name;
    238 {
    239 	struct ddpcb    cb;
    240 	struct ddpcb *prev, *next;
    241 	struct ddpcb   *initial;
    242 
    243 	if (off == 0)
    244 		return;
    245 	if (kread(off, (char *) &initial, sizeof(struct ddpcb *)) < 0)
    246 		return;
    247 	ddpcb = cb;
    248 	prev = (struct ddpcb *) off;
    249 	for (next = initial; next != NULL; prev = next) {
    250 		u_long          ppcb = (u_long) next;
    251 
    252 		if (kread((u_long) next, (char *) &ddpcb, sizeof(ddpcb)) < 0)
    253 			return;
    254 		next = ddpcb.ddp_next;
    255 #if 0
    256 		if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
    257 			continue;
    258 		}
    259 #endif
    260 		if (kread((u_long) ddpcb.ddp_socket,
    261 			  (char *) &sockb, sizeof(sockb)) < 0)
    262 			return;
    263 		if (first) {
    264 			printf("Active ATALK connections");
    265 			if (aflag)
    266 				printf(" (including servers)");
    267 			putchar('\n');
    268 			if (Aflag)
    269 				printf("%-8.8s ", "PCB");
    270 			printf(Aflag ?
    271 			    "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
    272 			     "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
    273 			       "Proto", "Recv-Q", "Send-Q",
    274 			     "Local Address", "Foreign Address", "(state)");
    275 			first = 0;
    276 		}
    277 		if (Aflag)
    278 			printf("%8lx ", ppcb);
    279 		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
    280 		       sockb.so_snd.sb_cc);
    281 		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
    282 				  (struct sockaddr *) & ddpcb.ddp_lsat, 7));
    283 		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
    284 				  (struct sockaddr *) & ddpcb.ddp_fsat, 7));
    285 		putchar('\n');
    286 	}
    287 }
    288 #define ANY(x,y,z) \
    289 	((x) ? printf("\t%ld %s%s%s\n",x,y,plural(x),z) : 0)
    290 
    291 /*
    292  * Dump DDP statistics structure.
    293  */
    294 void
    295 ddp_stats(off, name)
    296 	u_long off;
    297 	char *name;
    298 {
    299 	struct ddpstat  ddpstat;
    300 
    301 	if (off == 0)
    302 		return;
    303 	if (kread(off, (char *) &ddpstat, sizeof(ddpstat)) < 0)
    304 		return;
    305 	printf("%s:\n", name);
    306 	ANY(ddpstat.ddps_short, "packet", " with short headers ");
    307 	ANY(ddpstat.ddps_long, "packet", " with long headers ");
    308 	ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
    309 	ANY(ddpstat.ddps_tooshort, "packet", " too short ");
    310 	ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
    311 	ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
    312 	ANY(ddpstat.ddps_forward, "packet", " forwarded ");
    313 	ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
    314 	ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
    315 	ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
    316 }
    317 #undef ANY
    318