Home | History | Annotate | Line # | Download | only in netstat
atalk.c revision 1.1
      1 /*	$NetBSD: atalk.c,v 1.1 1997/04/03 04:46:45 christos 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from @(#)atalk.c	1.1 (Whistle) 6/6/96";
     39 #else
     40 static char rcsid[] = "$NetBSD: atalk.c,v 1.1 1997/04/03 04:46:45 christos Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/queue.h>
     46 #include <sys/socket.h>
     47 #include <sys/socketvar.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/protosw.h>
     50 
     51 #include <net/route.h>
     52 #include <net/if.h>
     53 
     54 #include <netinet/tcp_fsm.h>
     55 
     56 #include <netatalk/at.h>
     57 #include <netatalk/ddp_var.h>
     58 
     59 #include <nlist.h>
     60 #include <errno.h>
     61 #include <stdio.h>
     62 #include <string.h>
     63 #include "netstat.h"
     64 
     65 struct ddpcb    ddpcb;
     66 struct socket   sockb;
     67 
     68 static int first = 1;
     69 
     70 static char *at_pr_net __P((struct sockaddr_at *, int));
     71 static char *at_pr_host __P((struct sockaddr_at *, int));
     72 static char *at_pr_range __P((struct sockaddr_at *));
     73 static char *at_pr_port __P((struct sockaddr_at *));
     74 
     75 /*
     76  * Print a summary of connections related to a Network Systems
     77  * protocol.  For XXX, also give state of connection.
     78  * Listening processes (aflag) are suppressed unless the
     79  * -a (all) flag is specified.
     80  */
     81 
     82 static char *
     83 at_pr_net(sat, numeric)
     84 	struct sockaddr_at *sat;
     85 	int numeric;
     86 {
     87 	static char mybuf[50];
     88 
     89 	if (!numeric) {
     90 		switch (sat->sat_addr.s_net) {
     91 		case 0xffff:
     92 			return "????";
     93 		case ATADDR_ANYNET:
     94 			return ("*");
     95 		}
     96 	}
     97 	(void) snprintf(mybuf, sizeof(mybuf), "%hu",
     98 	    ntohs(sat->sat_addr.s_net));
     99 	return mybuf;
    100 }
    101 
    102 static char *
    103 at_pr_host(sat, numeric)
    104 	struct sockaddr_at *sat;
    105 	int numeric;
    106 {
    107 	static char mybuf[50];
    108 
    109 	if (!numeric) {
    110 		switch (sat->sat_addr.s_node) {
    111 		case ATADDR_BCAST:
    112 			return "bcast";
    113 		case ATADDR_ANYNODE:
    114 			return ("*");
    115 		}
    116 	}
    117 	(void) snprintf(mybuf, sizeof(mybuf), "%d",
    118 	    (unsigned int) sat->sat_addr.s_node);
    119 	return mybuf;
    120 }
    121 
    122 static char *
    123 at_pr_port(sat)
    124 	struct sockaddr_at *sat;
    125 {
    126 	static char mybuf[50];
    127 
    128 	switch (sat->sat_port) {
    129 	case ATADDR_ANYPORT:
    130 		return ("*");
    131 	case 0xff:
    132 		return "????";
    133 	default:
    134 		(void) snprintf(mybuf, sizeof(mybuf), "%d",
    135 		    (unsigned int) sat->sat_port);
    136 		return mybuf;
    137 	}
    138 }
    139 
    140 static char *
    141 at_pr_range(sat)
    142 	struct sockaddr_at *sat;
    143 {
    144 	static char mybuf[50];
    145 
    146 	if (sat->sat_range.r_netrange.nr_firstnet
    147 	    != sat->sat_range.r_netrange.nr_lastnet) {
    148 		(void) snprintf(mybuf, sizeof(mybuf), "%d-%d",
    149 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
    150 			ntohs(sat->sat_range.r_netrange.nr_lastnet));
    151 	} else {
    152 		(void) snprintf(mybuf, sizeof(mybuf), "%d",
    153 			ntohs(sat->sat_range.r_netrange.nr_firstnet));
    154 	}
    155 	return mybuf;
    156 }
    157 
    158 
    159 /* what == 0 for addr only == 3
    160  *	1 for net
    161  *	2 for host
    162  *	4 for port
    163  *	8 for numeric only
    164  */
    165 char *
    166 atalk_print(sa, what)
    167 	const struct sockaddr *sa;
    168 	int what;
    169 {
    170 	struct sockaddr_at *sat = (struct sockaddr_at *) sa;
    171 	static char mybuf[50];
    172 	int numeric = (what & 0x08);
    173 
    174 	mybuf[0] = 0;
    175 	switch (what & 0x13) {
    176 	case 0:
    177 		mybuf[0] = 0;
    178 		break;
    179 	case 1:
    180 		(void) snprintf(mybuf, sizeof(mybuf), "%s",
    181 		    at_pr_net(sat, numeric));
    182 		break;
    183 	case 2:
    184 		(void) snprintf(mybuf, sizeof(mybuf), "%s",
    185 		    at_pr_host(sat, numeric));
    186 		break;
    187 	case 3:
    188 		(void) snprintf(mybuf, sizeof(mybuf), "%s.%s",
    189 			at_pr_net(sat, numeric),
    190 			at_pr_host(sat, numeric));
    191 		break;
    192 	case 0x10:
    193 		(void) snprintf(mybuf, sizeof(mybuf), "%s", at_pr_range(sat));
    194 	}
    195 	if (what & 4) {
    196 		(void) snprintf(mybuf + strlen(mybuf),
    197 		    sizeof(mybuf) - strlen(mybuf), ".%s", at_pr_port(sat));
    198 	}
    199 	return mybuf;
    200 }
    201 
    202 char *
    203 atalk_print2(sa, mask, what)
    204 	const struct sockaddr *sa;
    205 	const struct sockaddr *mask;
    206 	int what;
    207 {
    208 	int             n;
    209 	static char     buf[100];
    210 	struct sockaddr_at *sat1, *sat2;
    211 	struct sockaddr_at thesockaddr;
    212 	struct sockaddr *sa2;
    213 
    214 	sat1 = (struct sockaddr_at *) sa;
    215 	sat2 = (struct sockaddr_at *) mask;
    216 	sa2 = (struct sockaddr *) & thesockaddr;
    217 
    218 	thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net &
    219 	    sat2->sat_addr.s_net;
    220 	n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 | (what & 8)));
    221 	if (sat2->sat_addr.s_net != 0xFFFF) {
    222 		thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net |
    223 		    ~sat2->sat_addr.s_net;
    224 		n += snprintf(buf + n, sizeof(buf) - n,
    225 		    "-%s", atalk_print(sa2, 1 | (what & 8)));
    226 	}
    227 	if (what & 2)
    228 		n += snprintf(buf + n, sizeof(buf) - n, ".%s",
    229 		    atalk_print(sa, what & (~1)));
    230 	return (buf);
    231 }
    232 
    233 void
    234 atalkprotopr(off, name)
    235 	u_long off;
    236 	char  *name;
    237 {
    238 	struct ddpcb    cb;
    239 	register struct ddpcb *prev, *next;
    240 	struct ddpcb   *initial;
    241 
    242 	if (off == 0)
    243 		return;
    244 	if (kread(off, (char *) &initial, sizeof(struct ddpcb *)) < 0)
    245 		return;
    246 	ddpcb = cb;
    247 	prev = (struct ddpcb *) off;
    248 	for (next = initial; next != NULL; prev = next) {
    249 		u_long          ppcb;
    250 
    251 		if (kread((u_long) next, (char *) &ddpcb, sizeof(ddpcb)) < 0)
    252 			return;
    253 		next = ddpcb.ddp_next;
    254 #if 0
    255 		if (!aflag && atalk_nullhost(ddpcb.ddp_lsat)) {
    256 			continue;
    257 		}
    258 #endif
    259 		if (kread((u_long) ddpcb.ddp_socket,
    260 			  (char *) &sockb, sizeof(sockb)) < 0)
    261 			return;
    262 		if (first) {
    263 			printf("Active ATALK connections");
    264 			if (aflag)
    265 				printf(" (including servers)");
    266 			putchar('\n');
    267 			if (Aflag)
    268 				printf("%-8.8s ", "PCB");
    269 			printf(Aflag ?
    270 			    "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
    271 			     "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
    272 			       "Proto", "Recv-Q", "Send-Q",
    273 			     "Local Address", "Foreign Address", "(state)");
    274 			first = 0;
    275 		}
    276 		if (Aflag)
    277 			printf("0x%lx ", ppcb);
    278 		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
    279 		       sockb.so_snd.sb_cc);
    280 		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
    281 				  (struct sockaddr *) & ddpcb.ddp_lsat, 7));
    282 		printf(Aflag ? " %-18.18s" : " %-22.22s", atalk_print(
    283 				  (struct sockaddr *) & ddpcb.ddp_fsat, 7));
    284 		putchar('\n');
    285 	}
    286 }
    287 #define ANY(x,y,z) \
    288 	((x) ? printf("\t%ld %s%s%s\n",x,y,plural(x),z) : 0)
    289 
    290 /*
    291  * Dump DDP statistics structure.
    292  */
    293 void
    294 ddp_stats(off, name)
    295 	u_long off;
    296 	char *name;
    297 {
    298 	struct ddpstat  ddpstat;
    299 
    300 	if (off == 0)
    301 		return;
    302 	if (kread(off, (char *) &ddpstat, sizeof(ddpstat)) < 0)
    303 		return;
    304 	printf("%s:\n", name);
    305 	ANY(ddpstat.ddps_short, "packet", " with short headers ");
    306 	ANY(ddpstat.ddps_long, "packet", " with long headers ");
    307 	ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
    308 	ANY(ddpstat.ddps_tooshort, "packet", " too short ");
    309 	ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
    310 	ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
    311 	ANY(ddpstat.ddps_forward, "packet", " forwarded ");
    312 	ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
    313 	ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
    314 	ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
    315 }
    316 #undef ANY
    317