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