Import netstat from 4.4Lite-2 - this version eliminates some of the

severely bogus mask guessing in the netname code, and also reports the
mask length of the route (like linux's netstat, but in a much more compact
form)

The current code is doing bogus things like shifting c-class subnets by
4 bits and looking them up, causing a DNS lookup of 0x0C0CBxxx which
takes an eternity to fail.
This commit is contained in:
Peter Wemm 1996-01-14 23:20:31 +00:00
parent 37cd666fcf
commit 6889b16a1c
4 changed files with 1835 additions and 0 deletions

375
usr.bin/netstat/if.c Normal file
View File

@ -0,0 +1,375 @@
/*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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
static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
#endif /* not lint */
#include <sys/types.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netns/ns.h>
#include <netns/ns_if.h>
#include <netiso/iso.h>
#include <netiso/iso_var.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "netstat.h"
#define YES 1
#define NO 0
static void sidewaysintpr __P((u_int, u_long));
static void catchalarm __P((int));
/*
* Print a description of the network interfaces.
*/
void
intpr(interval, ifnetaddr)
int interval;
u_long ifnetaddr;
{
struct ifnet ifnet;
union {
struct ifaddr ifa;
struct in_ifaddr in;
struct ns_ifaddr ns;
struct iso_ifaddr iso;
} ifaddr;
u_long ifaddraddr;
struct sockaddr *sa;
char name[16];
if (ifnetaddr == 0) {
printf("ifnet: symbol not defined\n");
return;
}
if (interval) {
sidewaysintpr((unsigned)interval, ifnetaddr);
return;
}
if (kread(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr))
return;
printf("%-5.5s %-5.5s %-11.11s %-15.15s %8.8s %5.5s %8.8s %5.5s",
"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
"Opkts", "Oerrs");
printf(" %5s", "Coll");
if (tflag)
printf(" %s", "Time");
if (dflag)
printf(" %s", "Drop");
putchar('\n');
ifaddraddr = 0;
while (ifnetaddr || ifaddraddr) {
struct sockaddr_in *sin;
register char *cp;
int n, m;
if (ifaddraddr == 0) {
if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
kread((u_long)ifnet.if_name, name, 16))
return;
name[15] = '\0';
ifnetaddr = (u_long)ifnet.if_next;
if (interface != 0 && (strcmp(name, interface) != 0 ||
unit != ifnet.if_unit))
continue;
cp = index(name, '\0');
cp += sprintf(cp, "%d", ifnet.if_unit);
if ((ifnet.if_flags&IFF_UP) == 0)
*cp++ = '*';
*cp = '\0';
ifaddraddr = (u_long)ifnet.if_addrlist;
}
printf("%-5.5s %-5d ", name, ifnet.if_mtu);
if (ifaddraddr == 0) {
printf("%-11.11s ", "none");
printf("%-15.15s ", "none");
} else {
if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
ifaddraddr = 0;
continue;
}
#define CP(x) ((char *)(x))
cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
CP(&ifaddr); sa = (struct sockaddr *)cp;
switch (sa->sa_family) {
case AF_UNSPEC:
printf("%-11.11s ", "none");
printf("%-15.15s ", "none");
break;
case AF_INET:
sin = (struct sockaddr_in *)sa;
#ifdef notdef
/* can't use inet_makeaddr because kernel
* keeps nets unshifted.
*/
in = inet_makeaddr(ifaddr.in.ia_subnet,
INADDR_ANY);
printf("%-11.11s ", netname(in.s_addr,
ifaddr.in.ia_subnetmask));
#else
printf("%-11.11s ",
netname(htonl(ifaddr.in.ia_subnet),
ifaddr.in.ia_subnetmask));
#endif
printf("%-15.15s ",
routename(sin->sin_addr.s_addr));
break;
case AF_NS:
{
struct sockaddr_ns *sns =
(struct sockaddr_ns *)sa;
u_long net;
char netnum[8];
*(union ns_net *) &net = sns->sns_addr.x_net;
sprintf(netnum, "%lxH", ntohl(net));
upHex(netnum);
printf("ns:%-8s ", netnum);
printf("%-15s ",
ns_phost((struct sockaddr *)sns));
}
break;
case AF_LINK:
{
struct sockaddr_dl *sdl =
(struct sockaddr_dl *)sa;
cp = (char *)LLADDR(sdl);
n = sdl->sdl_alen;
}
m = printf("%-11.11s ", "<Link>");
goto hexprint;
default:
m = printf("(%d)", sa->sa_family);
for (cp = sa->sa_len + (char *)sa;
--cp > sa->sa_data && (*cp == 0);) {}
n = cp - sa->sa_data + 1;
cp = sa->sa_data;
hexprint:
while (--n >= 0)
m += printf("%x%c", *cp++ & 0xff,
n > 0 ? '.' : ' ');
m = 28 - m;
while (m-- > 0)
putchar(' ');
break;
}
ifaddraddr = (u_long)ifaddr.ifa.ifa_next;
}
printf("%8d %5d %8d %5d %5d",
ifnet.if_ipackets, ifnet.if_ierrors,
ifnet.if_opackets, ifnet.if_oerrors,
ifnet.if_collisions);
if (tflag)
printf(" %3d", ifnet.if_timer);
if (dflag)
printf(" %3d", ifnet.if_snd.ifq_drops);
putchar('\n');
}
}
#define MAXIF 10
struct iftot {
char ift_name[16]; /* interface name */
int ift_ip; /* input packets */
int ift_ie; /* input errors */
int ift_op; /* output packets */
int ift_oe; /* output errors */
int ift_co; /* collisions */
int ift_dr; /* drops */
} iftot[MAXIF];
u_char signalled; /* set if alarm goes off "early" */
/*
* Print a running summary of interface statistics.
* Repeat display every interval seconds, showing statistics
* collected over that interval. Assumes that interval is non-zero.
* First line printed at top of screen is always cumulative.
*/
static void
sidewaysintpr(interval, off)
unsigned interval;
u_long off;
{
struct ifnet ifnet;
u_long firstifnet;
register struct iftot *ip, *total;
register int line;
struct iftot *lastif, *sum, *interesting;
int oldmask;
if (kread(off, (char *)&firstifnet, sizeof (u_long)))
return;
lastif = iftot;
sum = iftot + MAXIF - 1;
total = sum - 1;
interesting = iftot;
for (off = firstifnet, ip = iftot; off;) {
char *cp;
if (kread(off, (char *)&ifnet, sizeof ifnet))
break;
ip->ift_name[0] = '(';
if (kread((u_long)ifnet.if_name, ip->ift_name + 1, 15))
break;
if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
unit == ifnet.if_unit)
interesting = ip;
ip->ift_name[15] = '\0';
cp = index(ip->ift_name, '\0');
sprintf(cp, "%d)", ifnet.if_unit);
ip++;
if (ip >= iftot + MAXIF - 2)
break;
off = (u_long) ifnet.if_next;
}
lastif = ip;
(void)signal(SIGALRM, catchalarm);
signalled = NO;
(void)alarm(interval);
banner:
printf(" input %-6.6s output ", interesting->ift_name);
if (lastif - iftot > 0) {
if (dflag)
printf(" ");
printf(" input (Total) output");
}
for (ip = iftot; ip < iftot + MAXIF; ip++) {
ip->ift_ip = 0;
ip->ift_ie = 0;
ip->ift_op = 0;
ip->ift_oe = 0;
ip->ift_co = 0;
ip->ift_dr = 0;
}
putchar('\n');
printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
"packets", "errs", "packets", "errs", "colls");
if (dflag)
printf("%5.5s ", "drops");
if (lastif - iftot > 0)
printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
"packets", "errs", "packets", "errs", "colls");
if (dflag)
printf(" %5.5s", "drops");
putchar('\n');
fflush(stdout);
line = 0;
loop:
sum->ift_ip = 0;
sum->ift_ie = 0;
sum->ift_op = 0;
sum->ift_oe = 0;
sum->ift_co = 0;
sum->ift_dr = 0;
for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
if (kread(off, (char *)&ifnet, sizeof ifnet)) {
off = 0;
continue;
}
if (ip == interesting) {
printf("%8d %5d %8d %5d %5d",
ifnet.if_ipackets - ip->ift_ip,
ifnet.if_ierrors - ip->ift_ie,
ifnet.if_opackets - ip->ift_op,
ifnet.if_oerrors - ip->ift_oe,
ifnet.if_collisions - ip->ift_co);
if (dflag)
printf(" %5d",
ifnet.if_snd.ifq_drops - ip->ift_dr);
}
ip->ift_ip = ifnet.if_ipackets;
ip->ift_ie = ifnet.if_ierrors;
ip->ift_op = ifnet.if_opackets;
ip->ift_oe = ifnet.if_oerrors;
ip->ift_co = ifnet.if_collisions;
ip->ift_dr = ifnet.if_snd.ifq_drops;
sum->ift_ip += ip->ift_ip;
sum->ift_ie += ip->ift_ie;
sum->ift_op += ip->ift_op;
sum->ift_oe += ip->ift_oe;
sum->ift_co += ip->ift_co;
sum->ift_dr += ip->ift_dr;
off = (u_long) ifnet.if_next;
}
if (lastif - iftot > 0) {
printf(" %8d %5d %8d %5d %5d",
sum->ift_ip - total->ift_ip,
sum->ift_ie - total->ift_ie,
sum->ift_op - total->ift_op,
sum->ift_oe - total->ift_oe,
sum->ift_co - total->ift_co);
if (dflag)
printf(" %5d", sum->ift_dr - total->ift_dr);
}
*total = *sum;
putchar('\n');
fflush(stdout);
line++;
oldmask = sigblock(sigmask(SIGALRM));
if (! signalled) {
sigpause(0);
}
sigsetmask(oldmask);
signalled = NO;
(void)alarm(interval);
if (line == 21)
goto banner;
goto loop;
/*NOTREACHED*/
}
/*
* Called if an interval expires before sidewaysintpr has completed a loop.
* Sets a flag to not wait for the alarm.
*/
static void
catchalarm(signo)
int signo;
{
signalled = YES;
}

496
usr.bin/netstat/inet.c Normal file
View File

@ -0,0 +1,496 @@
/*
* Copyright (c) 1983, 1988, 1993, 1995
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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
static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
#endif /* not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/igmp_var.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_seq.h>
#define TCPSTATES
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_debug.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "netstat.h"
struct inpcb inpcb;
struct tcpcb tcpcb;
struct socket sockb;
char *inetname __P((struct in_addr *));
void inetprint __P((struct in_addr *, int, char *));
/*
* Print a summary of connections related to an Internet
* protocol. For TCP, also give state of connection.
* Listening processes (aflag) are suppressed unless the
* -a (all) flag is specified.
*/
void
protopr(off, name)
u_long off;
char *name;
{
struct inpcb cb;
register struct inpcb *prev, *next;
int istcp;
static int first = 1;
if (off == 0)
return;
istcp = strcmp(name, "tcp") == 0;
kread(off, (char *)&cb, sizeof (struct inpcb));
inpcb = cb;
prev = (struct inpcb *)off;
if (inpcb.inp_next == (struct inpcb *)off)
return;
while (inpcb.inp_next != (struct inpcb *)off) {
next = inpcb.inp_next;
kread((u_long)next, (char *)&inpcb, sizeof (inpcb));
if (inpcb.inp_prev != prev) {
printf("???\n");
break;
}
if (!aflag &&
inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
prev = next;
continue;
}
kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
if (istcp) {
kread((u_long)inpcb.inp_ppcb,
(char *)&tcpcb, sizeof (tcpcb));
}
if (first) {
printf("Active Internet 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)
if (istcp)
printf("%8x ", inpcb.inp_ppcb);
else
printf("%8x ", next);
printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
sockb.so_snd.sb_cc);
inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport, name);
inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport, name);
if (istcp) {
if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
printf(" %d", tcpcb.t_state);
else
printf(" %s", tcpstates[tcpcb.t_state]);
}
putchar('\n');
prev = next;
}
}
/*
* Dump TCP statistics structure.
*/
void
tcp_stats(off, name)
u_long off;
char *name;
{
struct tcpstat tcpstat;
if (off == 0)
return;
printf ("%s:\n", name);
kread(off, (char *)&tcpstat, sizeof (tcpstat));
#define p(f, m) if (tcpstat.f || sflag <= 1) \
printf(m, tcpstat.f, plural(tcpstat.f))
#define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
#define p3(f, m) if (tcpstat.f || sflag <= 1) \
printf(m, tcpstat.f, plurales(tcpstat.f))
p(tcps_sndtotal, "\t%d packet%s sent\n");
p2(tcps_sndpack,tcps_sndbyte,
"\t\t%d data packet%s (%d byte%s)\n");
p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
"\t\t%d data packet%s (%d byte%s) retransmitted\n");
p2(tcps_sndacks, tcps_delack,
"\t\t%d ack-only packet%s (%d delayed)\n");
p(tcps_sndurg, "\t\t%d URG only packet%s\n");
p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
p(tcps_sndwinup, "\t\t%d window update packet%s\n");
p(tcps_sndctrl, "\t\t%d control packet%s\n");
p(tcps_rcvtotal, "\t%d packet%s received\n");
p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
p2(tcps_rcvpack, tcps_rcvbyte,
"\t\t%d packet%s (%d byte%s) received in-sequence\n");
p2(tcps_rcvduppack, tcps_rcvdupbyte,
"\t\t%d completely duplicate packet%s (%d byte%s)\n");
p(tcps_pawsdrop, "\t\t%d old duplicate packet%s\n");
p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
"\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
p2(tcps_rcvoopack, tcps_rcvoobyte,
"\t\t%d out-of-order packet%s (%d byte%s)\n");
p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
"\t\t%d packet%s (%d byte%s) of data after window\n");
p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
p(tcps_connattempt, "\t%d connection request%s\n");
p(tcps_accepts, "\t%d connection accept%s\n");
p(tcps_badsyn, "\t%d bad connection attempt%s\n");
p(tcps_connects, "\t%d connection%s established (including accepts)\n");
p2(tcps_closed, tcps_drops,
"\t%d connection%s closed (including %d drop%s)\n");
p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
p2(tcps_rttupdated, tcps_segstimed,
"\t%d segment%s updated rtt (of %d attempt%s)\n");
p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
p(tcps_persisttimeo, "\t%d persist timeout%s\n");
p(tcps_persistdrop, "\t%d connection%s timed out in persist\n");
p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
p(tcps_predack, "\t%d correct ACK header prediction%s\n");
p(tcps_preddat, "\t%d correct data packet header prediction%s\n");
p3(tcps_pcbcachemiss, "\t%d PCB cache miss%s\n");
#undef p
#undef p2
#undef p3
}
/*
* Dump UDP statistics structure.
*/
void
udp_stats(off, name)
u_long off;
char *name;
{
struct udpstat udpstat;
u_long delivered;
if (off == 0)
return;
kread(off, (char *)&udpstat, sizeof (udpstat));
printf("%s:\n", name);
#define p(f, m) if (udpstat.f || sflag <= 1) \
printf(m, udpstat.f, plural(udpstat.f))
p(udps_ipackets, "\t%u datagram%s received\n");
p(udps_hdrops, "\t%u with incomplete header\n");
p(udps_badlen, "\t%u with bad data length field\n");
p(udps_badsum, "\t%u with bad checksum\n");
p(udps_noport, "\t%u dropped due to no socket\n");
p(udps_noportbcast, "\t%u broadcast/multicast datagram%s dropped due to no socket\n");
p(udps_fullsock, "\t%u dropped due to full socket buffers\n");
delivered = udpstat.udps_ipackets -
udpstat.udps_hdrops -
udpstat.udps_badlen -
udpstat.udps_badsum -
udpstat.udps_noport -
udpstat.udps_noportbcast -
udpstat.udps_fullsock;
if (delivered || sflag <= 1)
printf("\t%u delivered\n", delivered);
p(udps_opackets, "\t%u datagram%s output\n");
#undef p
}
/*
* Dump IP statistics structure.
*/
void
ip_stats(off, name)
u_long off;
char *name;
{
struct ipstat ipstat;
if (off == 0)
return;
kread(off, (char *)&ipstat, sizeof (ipstat));
printf("%s:\n", name);
#define p(f, m) if (ipstat.f || sflag <= 1) \
printf(m, ipstat.f, plural(ipstat.f))
p(ips_total, "\t%u total packet%s received\n");
p(ips_badsum, "\t%u bad header checksum%s\n");
p(ips_toosmall, "\t%u with size smaller than minimum\n");
p(ips_tooshort, "\t%u with data size < data length\n");
p(ips_badhlen, "\t%u with header length < data size\n");
p(ips_badlen, "\t%u with data length < header length\n");
p(ips_badoptions, "\t%u with bad options\n");
p(ips_badvers, "\t%u with incorrect version number\n");
p(ips_fragments, "\t%u fragment%s received\n");
p(ips_fragdropped, "\t%u fragment%s dropped (dup or out of space)\n");
p(ips_fragtimeout, "\t%u fragment%s dropped after timeout\n");
p(ips_reassembled, "\t%u packet%s reassembled ok\n");
p(ips_delivered, "\t%u packet%s for this host\n");
p(ips_noproto, "\t%u packet%s for unknown/unsupported protocol\n");
p(ips_forward, "\t%u packet%s forwarded\n");
p(ips_cantforward, "\t%u packet%s not forwardable\n");
p(ips_redirectsent, "\t%u redirect%s sent\n");
p(ips_localout, "\t%u packet%s sent from this host\n");
p(ips_rawout, "\t%u packet%s sent with fabricated ip header\n");
p(ips_odropped, "\t%u output packet%s dropped due to no bufs, etc.\n");
p(ips_noroute, "\t%u output packet%s discarded due to no route\n");
p(ips_fragmented, "\t%u output datagram%s fragmented\n");
p(ips_ofragments, "\t%u fragment%s created\n");
p(ips_cantfrag, "\t%u datagram%s that can't be fragmented\n");
#undef p
}
static char *icmpnames[] = {
"echo reply",
"#1",
"#2",
"destination unreachable",
"source quench",
"routing redirect",
"#6",
"#7",
"echo",
"#9",
"#10",
"time exceeded",
"parameter problem",
"time stamp",
"time stamp reply",
"information request",
"information request reply",
"address mask request",
"address mask reply",
};
/*
* Dump ICMP statistics.
*/
void
icmp_stats(off, name)
u_long off;
char *name;
{
struct icmpstat icmpstat;
register int i, first;
if (off == 0)
return;
kread(off, (char *)&icmpstat, sizeof (icmpstat));
printf("%s:\n", name);
#define p(f, m) if (icmpstat.f || sflag <= 1) \
printf(m, icmpstat.f, plural(icmpstat.f))
p(icps_error, "\t%u call%s to icmp_error\n");
p(icps_oldicmp,
"\t%u error%s not generated 'cuz old message was icmp\n");
for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
if (icmpstat.icps_outhist[i] != 0) {
if (first) {
printf("\tOutput histogram:\n");
first = 0;
}
printf("\t\t%s: %u\n", icmpnames[i],
icmpstat.icps_outhist[i]);
}
p(icps_badcode, "\t%u message%s with bad code fields\n");
p(icps_tooshort, "\t%u message%s < minimum length\n");
p(icps_checksum, "\t%u bad checksum%s\n");
p(icps_badlen, "\t%u message%s with bad length\n");
for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
if (icmpstat.icps_inhist[i] != 0) {
if (first) {
printf("\tInput histogram:\n");
first = 0;
}
printf("\t\t%s: %u\n", icmpnames[i],
icmpstat.icps_inhist[i]);
}
p(icps_reflect, "\t%u message response%s generated\n");
#undef p
}
/*
* Dump IGMP statistics structure.
*/
void
igmp_stats(off, name)
u_long off;
char *name;
{
struct igmpstat igmpstat;
if (off == 0)
return;
kread(off, (char *)&igmpstat, sizeof (igmpstat));
printf("%s:\n", name);
#define p(f, m) if (igmpstat.f || sflag <= 1) \
printf(m, igmpstat.f, plural(igmpstat.f))
#define py(f, m) if (igmpstat.f || sflag <= 1) \
printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
p(igps_rcv_total, "\t%u message%s received\n");
p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
py(igps_rcv_queries, "\t%u membership quer%s received\n");
py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
p(igps_rcv_reports, "\t%u membership report%s received\n");
p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
p(igps_snd_reports, "\t%u membership report%s sent\n");
#undef p
#undef py
}
/*
* Pretty print an Internet address (net address + port).
* If the nflag was specified, use numbers instead of names.
*/
void
inetprint(in, port, proto)
register struct in_addr *in;
int port;
char *proto;
{
struct servent *sp = 0;
char line[80], *cp;
int width;
sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(in));
cp = index(line, '\0');
if (!nflag && port)
sp = getservbyport((int)port, proto);
if (sp || port == 0)
sprintf(cp, "%.8s", sp ? sp->s_name : "*");
else
sprintf(cp, "%d", ntohs((u_short)port));
width = Aflag ? 18 : 22;
printf(" %-*.*s", width, width, line);
}
/*
* Construct an Internet address representation.
* If the nflag has been supplied, give
* numeric value, otherwise try for symbolic name.
*/
char *
inetname(inp)
struct in_addr *inp;
{
register char *cp;
static char line[50];
struct hostent *hp;
struct netent *np;
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
if (first && !nflag) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = index(domain, '.')))
(void) strcpy(domain, cp + 1);
else
domain[0] = 0;
}
cp = 0;
if (!nflag && inp->s_addr != INADDR_ANY) {
int net = inet_netof(*inp);
int lna = inet_lnaof(*inp);
if (lna == INADDR_ANY) {
np = getnetbyaddr(net, AF_INET);
if (np)
cp = np->n_name;
}
if (cp == 0) {
hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
if (hp) {
if ((cp = index(hp->h_name, '.')) &&
!strcmp(cp + 1, domain))
*cp = 0;
cp = hp->h_name;
}
}
}
if (inp->s_addr == INADDR_ANY)
strcpy(line, "*");
else if (cp)
strcpy(line, cp);
else {
inp->s_addr = ntohl(inp->s_addr);
#define C(x) ((x) & 0xff)
sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
}
return (line);
}

224
usr.bin/netstat/mroute.c Normal file
View File

@ -0,0 +1,224 @@
/*
* Copyright (c) 1989 Stephen Deering
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)mroute.c 8.2 (Berkeley) 4/28/95
*/
/*
* Print DVMRP multicast routing structures and statistics.
*
* MROUTING 1.0
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <netinet/in.h>
#include <netinet/igmp.h>
#define KERNEL 1
#include <netinet/ip_mroute.h>
#undef KERNEL
#include <stdio.h>
#include <stdlib.h>
#include "netstat.h"
void
mroutepr(mrpaddr, mrtaddr, vifaddr)
u_long mrpaddr, mrtaddr, vifaddr;
{
u_int mrtproto;
struct mrt *mrttable[MRTHASHSIZ];
struct vif viftable[MAXVIFS];
register struct mrt *mrt;
struct mrt smrt;
register struct vif *v;
register vifi_t vifi;
register struct in_addr *grp;
register int i, n;
register int banner_printed;
register int saved_nflag;
if (mrpaddr == 0) {
printf("ip_mrtproto: symbol not in namelist\n");
return;
}
kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
switch (mrtproto) {
case 0:
printf("no multicast routing compiled into this system\n");
return;
case IGMP_DVMRP:
break;
default:
printf("multicast routing protocol %u, unknown\n", mrtproto);
return;
}
if (mrtaddr == 0) {
printf("mrttable: symbol not in namelist\n");
return;
}
if (vifaddr == 0) {
printf("viftable: symbol not in namelist\n");
return;
}
saved_nflag = nflag;
nflag = 1;
kread(vifaddr, (char *)&viftable, sizeof(viftable));
banner_printed = 0;
for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
if (v->v_lcl_addr.s_addr == 0)
continue;
if (!banner_printed) {
printf("\nVirtual Interface Table\n%s%s",
" Vif Threshold Local-Address ",
"Remote-Address Groups\n");
banner_printed = 1;
}
printf(" %2u %3u %-15.15s",
vifi, v->v_threshold, routename(v->v_lcl_addr.s_addr));
printf(" %-15.15s\n", (v->v_flags & VIFF_TUNNEL) ?
routename(v->v_rmt_addr.s_addr) : "");
n = v->v_lcl_grps_n;
grp = (struct in_addr *)malloc(n * sizeof(*grp));
if (grp == NULL) {
printf("v_lcl_grps_n: malloc failed\n");
return;
}
kread((u_long)v->v_lcl_grps, (caddr_t)grp, n * sizeof(*grp));
for (i = 0; i < n; ++i)
printf("%51s %-15.15s\n",
"", routename((grp++)->s_addr));
free(grp);
}
if (!banner_printed)
printf("\nVirtual Interface Table is empty\n");
kread(mrtaddr, (char *)&mrttable, sizeof(mrttable));
banner_printed = 0;
for (i = 0; i < MRTHASHSIZ; ++i) {
for (mrt = mrttable[i]; mrt != NULL; mrt = mrt->mrt_next) {
if (!banner_printed) {
printf("\nMulticast Routing Table\n%s",
" Hash Origin-Subnet In-Vif Out-Vifs\n");
banner_printed = 1;
}
kread((u_long)mrt, (char *)&smrt, sizeof(*mrt));
mrt = &smrt;
printf(" %3u %-15.15s %2u ",
i, netname(mrt->mrt_origin.s_addr,
ntohl(mrt->mrt_originmask.s_addr)),
mrt->mrt_parent);
for (vifi = 0; vifi < MAXVIFS; ++vifi)
if (VIFM_ISSET(vifi, mrt->mrt_children))
printf(" %u%c",
vifi,
VIFM_ISSET(vifi, mrt->mrt_leaves) ?
'*' : ' ');
printf("\n");
}
}
if (!banner_printed)
printf("\nMulticast Routing Table is empty\n");
printf("\n");
nflag = saved_nflag;
}
void
mrt_stats(mrpaddr, mstaddr)
u_long mrpaddr, mstaddr;
{
u_int mrtproto;
struct mrtstat mrtstat;
if(mrpaddr == 0) {
printf("ip_mrtproto: symbol not in namelist\n");
return;
}
kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
switch (mrtproto) {
case 0:
printf("no multicast routing compiled into this system\n");
return;
case IGMP_DVMRP:
break;
default:
printf("multicast routing protocol %u, unknown\n", mrtproto);
return;
}
if (mstaddr == 0) {
printf("mrtstat: symbol not in namelist\n");
return;
}
kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
printf("multicast routing:\n");
printf(" %10u multicast route lookup%s\n",
mrtstat.mrts_mrt_lookups, plural(mrtstat.mrts_mrt_lookups));
printf(" %10u multicast route cache miss%s\n",
mrtstat.mrts_mrt_misses, plurales(mrtstat.mrts_mrt_misses));
printf(" %10u group address lookup%s\n",
mrtstat.mrts_grp_lookups, plural(mrtstat.mrts_grp_lookups));
printf(" %10u group address cache miss%s\n",
mrtstat.mrts_grp_misses, plurales(mrtstat.mrts_grp_misses));
printf(" %10u datagram%s with no route for origin\n",
mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
printf(" %10u datagram%s with malformed tunnel options\n",
mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
printf(" %10u datagram%s with no room for tunnel options\n",
mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
printf(" %10u datagram%s arrived on the wrong interface\n",
mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
}

740
usr.bin/netstat/route.c Normal file
View File

@ -0,0 +1,740 @@
/*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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
static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95";
#endif /* not lint */
#include <sys/param.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/mbuf.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#define KERNEL
#include <net/route.h>
#undef KERNEL
#include <netinet/in.h>
#include <netns/ns.h>
#include <sys/sysctl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "netstat.h"
#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
/*
* Definitions for showing gateway flags.
*/
struct bits {
short b_mask;
char b_val;
} bits[] = {
{ RTF_UP, 'U' },
{ RTF_GATEWAY, 'G' },
{ RTF_HOST, 'H' },
{ RTF_REJECT, 'R' },
{ RTF_DYNAMIC, 'D' },
{ RTF_MODIFIED, 'M' },
{ RTF_DONE, 'd' }, /* Completed -- for routing messages only */
{ RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
{ RTF_CLONING, 'C' },
{ RTF_XRESOLVE, 'X' },
{ RTF_LLINFO, 'L' },
{ RTF_STATIC, 'S' },
{ RTF_PROTO1, '1' },
{ RTF_PROTO2, '2' },
{ 0 }
};
static union {
struct sockaddr u_sa;
u_short u_data[128];
} pt_u;
int do_rtent = 0;
struct rtentry rtentry;
struct radix_node rnode;
struct radix_mask rmask;
int NewTree = 0;
static struct sockaddr *kgetsa __P((struct sockaddr *));
static void p_tree __P((struct radix_node *));
static void p_rtnode __P(());
static void ntreestuff __P(());
static void np_rtentry __P((struct rt_msghdr *));
static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
static void p_flags __P((int, char *));
static void p_rtentry __P((struct rtentry *));
/*
* Print routing tables.
*/
void
routepr(rtree)
u_long rtree;
{
struct radix_node_head *rnh, head;
int i;
printf("Routing tables\n");
if (Aflag == 0 && NewTree)
ntreestuff();
else {
if (rtree == 0) {
printf("rt_tables: symbol not in namelist\n");
return;
}
kget(rtree, rt_tables);
for (i = 0; i <= AF_MAX; i++) {
if ((rnh = rt_tables[i]) == 0)
continue;
kget(rnh, head);
if (i == AF_UNSPEC) {
if (Aflag && af == 0) {
printf("Netmasks:\n");
p_tree(head.rnh_treetop);
}
} else if (af == AF_UNSPEC || af == i) {
pr_family(i);
do_rtent = 1;
pr_rthdr();
p_tree(head.rnh_treetop);
}
}
}
}
/*
* Print address family header before a section of the routing table.
*/
void
pr_family(af)
int af;
{
char *afname;
switch (af) {
case AF_INET:
afname = "Internet";
break;
case AF_NS:
afname = "XNS";
break;
case AF_ISO:
afname = "ISO";
break;
case AF_CCITT:
afname = "X.25";
break;
default:
afname = NULL;
break;
}
if (afname)
printf("\n%s:\n", afname);
else
printf("\nProtocol Family %d:\n", af);
}
/* column widths; each followed by one space */
#define WID_DST 16 /* width of destination column */
#define WID_GW 18 /* width of gateway column */
/*
* Print header for routing table columns.
*/
void
pr_rthdr()
{
if (Aflag)
printf("%-8.8s ","Address");
printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %s\n",
WID_DST, WID_DST, "Destination",
WID_GW, WID_GW, "Gateway",
"Flags", "Refs", "Use", "Interface");
}
static struct sockaddr *
kgetsa(dst)
register struct sockaddr *dst;
{
kget(dst, pt_u.u_sa);
if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
return (&pt_u.u_sa);
}
static void
p_tree(rn)
struct radix_node *rn;
{
again:
kget(rn, rnode);
if (rnode.rn_b < 0) {
if (Aflag)
printf("%-8.8x ", rn);
if (rnode.rn_flags & RNF_ROOT) {
if (Aflag)
printf("(root node)%s",
rnode.rn_dupedkey ? " =>\n" : "\n");
} else if (do_rtent) {
kget(rn, rtentry);
p_rtentry(&rtentry);
if (Aflag)
p_rtnode();
} else {
p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
NULL, 0, 44);
putchar('\n');
}
if (rn = rnode.rn_dupedkey)
goto again;
} else {
if (Aflag && do_rtent) {
printf("%-8.8x ", rn);
p_rtnode();
}
rn = rnode.rn_r;
p_tree(rnode.rn_l);
p_tree(rn);
}
}
char nbuf[20];
static void
p_rtnode()
{
struct radix_mask *rm = rnode.rn_mklist;
if (rnode.rn_b < 0) {
if (rnode.rn_mask) {
printf("\t mask ");
p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
NULL, 0, -1);
} else if (rm == 0)
return;
} else {
sprintf(nbuf, "(%d)", rnode.rn_b);
printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r);
}
while (rm) {
kget(rm, rmask);
sprintf(nbuf, " %d refs, ", rmask.rm_refs);
printf(" mk = %8.8x {(%d),%s",
rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
if (rmask.rm_flags & RNF_NORMAL) {
struct radix_node rnode_aux;
printf(" <normal>, ");
kget(rmask.rm_leaf, rnode_aux);
p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
NULL, 0, -1);
} else
p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
NULL, 0, -1);
putchar('}');
if (rm = rmask.rm_mklist)
printf(" ->");
}
putchar('\n');
}
static void
ntreestuff()
{
size_t needed;
int mib[6];
char *buf, *next, *lim;
register struct rt_msghdr *rtm;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = 0;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
{ perror("route-sysctl-estimate"); exit(1);}
if ((buf = malloc(needed)) == 0)
{ printf("out of space\n"); exit(1);}
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
{ perror("sysctl of routing table"); exit(1);}
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
np_rtentry(rtm);
}
}
static void
np_rtentry(rtm)
register struct rt_msghdr *rtm;
{
register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
#ifdef notdef
static int masks_done, banner_printed;
#endif
static int old_af;
int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
#ifdef notdef
/* for the moment, netmasks are skipped over */
if (!banner_printed) {
printf("Netmasks:\n");
banner_printed = 1;
}
if (masks_done == 0) {
if (rtm->rtm_addrs != RTA_DST ) {
masks_done = 1;
af = sa->sa_family;
}
} else
#endif
af = sa->sa_family;
if (af != old_af) {
pr_family(af);
old_af = af;
}
if (rtm->rtm_addrs == RTA_DST)
p_sockaddr(sa, NULL, 0, 36);
else {
p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
if (sa->sa_len == 0)
sa->sa_len = sizeof(long);
sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
p_sockaddr(sa, NULL, 0, 18);
}
p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
putchar('\n');
}
static void
p_sockaddr(sa, mask, flags, width)
struct sockaddr *sa, *mask;
int flags, width;
{
char workbuf[128], *cplim;
register char *cp = workbuf;
switch(sa->sa_family) {
case AF_INET:
{
register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if (sin->sin_addr.s_addr == INADDR_ANY)
cp = "default";
else if (flags & RTF_HOST)
cp = routename(sin->sin_addr.s_addr);
else if (mask)
cp = netname(sin->sin_addr.s_addr,
ntohl(((struct sockaddr_in *)mask)
->sin_addr.s_addr));
else
cp = netname(sin->sin_addr.s_addr, 0L);
break;
}
case AF_NS:
cp = ns_print(sa);
break;
case AF_LINK:
{
register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
sdl->sdl_slen == 0)
(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
else switch (sdl->sdl_type) {
case IFT_ETHER:
{
register int i;
register u_char *lla = (u_char *)sdl->sdl_data +
sdl->sdl_nlen;
cplim = "";
for (i = 0; i < sdl->sdl_alen; i++, lla++) {
cp += sprintf(cp, "%s%x", cplim, *lla);
cplim = ":";
}
cp = workbuf;
break;
}
default:
cp = link_ntoa(sdl);
break;
}
break;
}
default:
{
register u_char *s = (u_char *)sa->sa_data, *slim;
slim = sa->sa_len + (u_char *) sa;
cplim = cp + sizeof(workbuf) - 6;
cp += sprintf(cp, "(%d)", sa->sa_family);
while (s < slim && cp < cplim) {
cp += sprintf(cp, " %02x", *s++);
if (s < slim)
cp += sprintf(cp, "%02x", *s++);
}
cp = workbuf;
}
}
if (width < 0 )
printf("%s ", cp);
else {
if (nflag)
printf("%-*s ", width, cp);
else
printf("%-*.*s ", width, width, cp);
}
}
static void
p_flags(f, format)
register int f;
char *format;
{
char name[33], *flags;
register struct bits *p = bits;
for (flags = name; p->b_mask; p++)
if (p->b_mask & f)
*flags++ = p->b_val;
*flags = '\0';
printf(format, name);
}
static void
p_rtentry(rt)
register struct rtentry *rt;
{
static struct ifnet ifnet, *lastif;
static char name[16];
register struct sockaddr *sa;
struct sockaddr addr, mask;
if (!(sa = kgetsa(rt_key(rt))))
bzero(&addr, sizeof addr);
else
addr = *sa;
if (!rt_mask(rt) || !(sa = kgetsa(rt_mask(rt))))
bzero(&mask, sizeof mask);
else
mask = *sa;
p_sockaddr(&addr, &mask, rt->rt_flags, WID_DST);
p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, WID_GW);
p_flags(rt->rt_flags, "%-6.6s ");
printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
if (rt->rt_ifp) {
if (rt->rt_ifp != lastif) {
kget(rt->rt_ifp, ifnet);
kread((u_long)ifnet.if_name, name, 16);
lastif = rt->rt_ifp;
}
printf(" %.15s%d%s", name, ifnet.if_unit,
rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
}
putchar('\n');
}
char *
routename(in)
u_long in;
{
register char *cp;
static char line[MAXHOSTNAMELEN + 1];
struct hostent *hp;
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
if (first) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = index(domain, '.')))
(void) strcpy(domain, cp + 1);
else
domain[0] = 0;
}
cp = 0;
if (!nflag) {
hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
AF_INET);
if (hp) {
if ((cp = index(hp->h_name, '.')) &&
!strcmp(cp + 1, domain))
*cp = 0;
cp = hp->h_name;
}
}
if (cp)
strncpy(line, cp, sizeof(line) - 1);
else {
#define C(x) ((x) & 0xff)
in = ntohl(in);
sprintf(line, "%u.%u.%u.%u",
C(in >> 24), C(in >> 16), C(in >> 8), C(in));
}
return (line);
}
static u_long
forgemask(a)
u_long a;
{
u_long m;
if (IN_CLASSA(a))
m = IN_CLASSA_NET;
else if (IN_CLASSB(a))
m = IN_CLASSB_NET;
else
m = IN_CLASSC_NET;
return (m);
}
static void
domask(dst, addr, mask)
char *dst;
u_long addr, mask;
{
register int b, i;
if (!mask || (forgemask(addr) == mask)) {
*dst = '\0';
return;
}
i = 0;
for (b = 0; b < 32; b++)
if (mask & (1 << b)) {
register int bb;
i = b;
for (bb = b+1; bb < 32; bb++)
if (!(mask & (1 << bb))) {
i = -1; /* noncontig */
break;
}
break;
}
if (i == -1)
sprintf(dst, "&0x%lx", mask);
else
sprintf(dst, "/%d", 32-i);
}
/*
* Return the name of the network whose address is given.
* The address is assumed to be that of a net or subnet, not a host.
*/
char *
netname(in, mask)
u_long in, mask;
{
char *cp = 0;
static char line[MAXHOSTNAMELEN + 1];
struct netent *np = 0;
u_long net, omask;
register u_long i;
int subnetshift;
i = ntohl(in);
omask = mask;
if (!nflag && i) {
if (mask == 0) {
switch (mask = forgemask(i)) {
case IN_CLASSA_NET:
subnetshift = 8;
break;
case IN_CLASSB_NET:
subnetshift = 8;
break;
case IN_CLASSC_NET:
subnetshift = 4;
break;
default:
abort();
}
/*
* If there are more bits than the standard mask
* would suggest, subnets must be in use.
* Guess at the subnet mask, assuming reasonable
* width subnet fields.
*/
while (i &~ mask)
mask = (long)mask >> subnetshift;
}
net = i & mask;
while ((mask & 1) == 0)
mask >>= 1, net >>= 1;
np = getnetbyaddr(net, AF_INET);
if (np)
cp = np->n_name;
}
if (cp)
strncpy(line, cp, sizeof(line) - 1);
else if ((i & 0xffffff) == 0)
sprintf(line, "%u", C(i >> 24));
else if ((i & 0xffff) == 0)
sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16));
else if ((i & 0xff) == 0)
sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
else
sprintf(line, "%u.%u.%u.%u", C(i >> 24),
C(i >> 16), C(i >> 8), C(i));
domask(line+strlen(line), i, omask);
return (line);
}
/*
* Print routing statistics
*/
void
rt_stats(off)
u_long off;
{
struct rtstat rtstat;
if (off == 0) {
printf("rtstat: symbol not in namelist\n");
return;
}
kread(off, (char *)&rtstat, sizeof (rtstat));
printf("routing:\n");
printf("\t%u bad routing redirect%s\n",
rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
printf("\t%u dynamically created route%s\n",
rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
printf("\t%u new gateway%s due to redirects\n",
rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
printf("\t%u destination%s found unreachable\n",
rtstat.rts_unreach, plural(rtstat.rts_unreach));
printf("\t%u use%s of a wildcard route\n",
rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
}
short ns_nullh[] = {0,0,0};
short ns_bh[] = {-1,-1,-1};
char *
ns_print(sa)
register struct sockaddr *sa;
{
register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
struct ns_addr work;
union { union ns_net net_e; u_long long_e; } net;
u_short port;
static char mybuf[50], cport[10], chost[25];
char *host = "";
register char *p; register u_char *q;
work = sns->sns_addr;
port = ntohs(work.x_port);
work.x_port = 0;
net.net_e = work.x_net;
if (ns_nullhost(work) && net.long_e == 0) {
if (port ) {
sprintf(mybuf, "*.%xH", port);
upHex(mybuf);
} else
sprintf(mybuf, "*.*");
return (mybuf);
}
if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
host = "any";
} else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
host = "*";
} else {
q = work.x_host.c_host;
sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
q[0], q[1], q[2], q[3], q[4], q[5]);
for (p = chost; *p == '0' && p < chost + 12; p++)
continue;
host = p;
}
if (port)
sprintf(cport, ".%xH", htons(port));
else
*cport = 0;
sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
upHex(mybuf);
return(mybuf);
}
char *
ns_phost(sa)
struct sockaddr *sa;
{
register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
struct sockaddr_ns work;
static union ns_net ns_zeronet;
char *p;
work = *sns;
work.sns_addr.x_port = 0;
work.sns_addr.x_net = ns_zeronet;
p = ns_print((struct sockaddr *)&work);
if (strncmp("0H.", p, 3) == 0) p += 3;
return(p);
}
void
upHex(p0)
char *p0;
{
register char *p = p0;
for (; *p; p++) switch (*p) {
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
*p += ('A' - 'a');
}
}