- 论坛徽章:
- 0
|
atalk.c
- /*
- * 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[] = "@(#)atalk.c 1.1 (Whistle) 6/6/96";
- */
- static const char rcsid[] =
- "$FreeBSD: src/usr.bin/netstat/atalk.c,v 1.13.2.2 2001/09/17 14:53:17 ru Exp $";
- #endif /* not lint */
- #include <sys/param.h>;
- #include <sys/queue.h>;
- #include <sys/socket.h>;
- #include <sys/socketvar.h>;
- #include <sys/protosw.h>;
- #include <net/route.h>;
- #include <netatalk/at.h>;
- #include <netatalk/ddp_var.h>;
- #include <errno.h>;
- #include <nlist.h>;
- #include <netdb.h>;
- #include <stdio.h>;
- #include <string.h>;
- #include "netstat.h"
- struct ddpcb ddpcb;
- struct socket sockb;
- static int first = 1;
- /*
- * 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("*");
- }
- }
- sprintf(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("*");
- }
- }
- sprintf(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 (numeric_port) {
- (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) {
- sprintf(mybuf,"%d-%d",
- ntohs(sat->;sat_range.r_netrange.nr_firstnet),
- ntohs(sat->;sat_range.r_netrange.nr_lastnet));
- } else {
- sprintf(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(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:
- sprintf(mybuf,"%s",at_pr_net(sat, numeric));
- break;
- case 2:
- sprintf(mybuf,"%s",at_pr_host(sat, numeric));
- break;
- case 3:
- sprintf(mybuf,"%s.%s",
- at_pr_net(sat, numeric),
- at_pr_host(sat, numeric));
- break;
- case 0x10:
- sprintf(mybuf,"%s", at_pr_range(sat));
- }
- if (what & 4) {
- sprintf(mybuf+strlen(mybuf),".%s",at_pr_port(sat));
- }
- return mybuf;
- }
- char *
- atalk_print2(struct sockaddr *sa, struct sockaddr *mask, int what)
- {
- int n;
- 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.sat_addr.s_net = sat1->;sat_addr.s_net & sat2->;sat_addr.s_net;
- n = snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 |(what & 8)));
- if(sat2->;sat_addr.s_net != 0xFFFF) {
- thesockaddr.sat_addr.s_net = sat1->;sat_addr.s_net | ~sat2->;sat_addr.s_net;
- n += snprintf(buf + n, sizeof(buf) - n,
- "-%s", atalk_print(sa2, 1 |(what & 8)));
- }
- if(what & 2)
- n += snprintf(buf + n, sizeof(buf) - n, ".%s", atalk_print(sa, what&(~1)));
- return(buf);
- }
- void
- atalkprotopr(u_long off __unused, char *name, int af __unused)
- {
- struct ddpcb *this, *next;
- if (off == 0)
- return;
- kread(off, (char *)&this, sizeof (struct ddpcb *));
- for ( ; this != NULL; this = next) {
- kread((u_long)this, (char *)&ddpcb, sizeof (ddpcb));
- next = ddpcb.ddp_next;
- #if 0
- if (!aflag && atalk_nullhost(ddpcb.ddp_lsat) ) {
- continue;
- }
- #endif
- kread((u_long)ddpcb.ddp_socket, (char *)&sockb, sizeof (sockb));
- 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 ", (u_long) this);
- printf("%-5.5s %6lu %6lu ", 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 ANY(x,y,z) if (x || sflag <= 1) \
- printf("\t%lu %s%s%s\n",x,y,plural(x),z)
- /*
- * Dump DDP statistics structure.
- */
- void
- ddp_stats(u_long off __unused, char *name, int af __unused)
- {
- struct ddpstat ddpstat;
- if (off == 0)
- return;
- kread(off, (char *)&ddpstat, sizeof (ddpstat));
- printf("%s:\n", name);
- ANY(ddpstat.ddps_short, "packet", " with short headers ");
- ANY(ddpstat.ddps_long, "packet", " with long headers ");
- ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
- ANY(ddpstat.ddps_tooshort, "packet", " too short ");
- ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
- ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
- ANY(ddpstat.ddps_forward, "packet", " forwarded ");
- ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
- ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
- ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
- }
复制代码
route.c
- /*
- * 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
- #if 0
- static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
- #endif
- static const char rcsid[] =
- "$FreeBSD: src/usr.bin/netstat/route.c,v 1.41.2.11 2001/10/18 10:33:25 ru Exp $";
- #endif /* not lint */
- #include <sys/param.h>;
- #include <sys/protosw.h>;
- #include <sys/socket.h>;
- #include <sys/time.h>;
- #include <net/if.h>;
- #include <net/if_var.h>;
- #include <net/if_dl.h>;
- #include <net/if_types.h>;
- #include <net/route.h>;
- #include <netinet/in.h>;
- #include <netipx/ipx.h>;
- #include <netatalk/at.h>;
- #include <netgraph/ng_socket.h>;
- #ifdef NS
- #include <netns/ns.h>;
- #endif
- #include <sys/sysctl.h>;
- #include <arpa/inet.h>;
- #include <libutil.h>;
- #include <netdb.h>;
- #include <stdio.h>;
- #include <stdlib.h>;
- #include <string.h>;
- #include <unistd.h>;
- #include <err.h>;
- #include <time.h>;
- #include "netstat.h"
- #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
- /* alignment constraint for routing socket */
- #define ROUNDUP(a) \
- ((a) >; 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
- #define ADVANCE(x, n) (x += ROUNDUP((n)->;sa_len))
- /*
- * Definitions for showing gateway flags.
- */
- struct bits {
- u_long 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_CLONING, 'C' },
- { RTF_XRESOLVE, 'X' },
- { RTF_LLINFO, 'L' },
- { RTF_STATIC, 'S' },
- { RTF_PROTO1, '1' },
- { RTF_PROTO2, '2' },
- { RTF_WASCLONED,'W' },
- { RTF_PRCLONING,'c' },
- { RTF_PROTO3, '3' },
- { RTF_BLACKHOLE,'B' },
- { RTF_BROADCAST,'b' },
- { 0 }
- };
- typedef union {
- long dummy; /* Helps align structure. */
- struct sockaddr u_sa;
- u_short u_data[128];
- } sa_u;
- static sa_u pt_u;
- int do_rtent = 0;
- struct rtentry rtentry;
- struct radix_node rnode;
- struct radix_mask rmask;
- struct radix_node_head *rt_tables[AF_MAX+1];
- int NewTree = 0;
- static struct sockaddr *kgetsa (struct sockaddr *);
- static void p_tree (struct radix_node *);
- static void p_rtnode (void);
- static void ntreestuff (void);
- static void np_rtentry (struct rt_msghdr *);
- static void p_sockaddr (struct sockaddr *, struct sockaddr *, int, int);
- static void p_flags (int, char *);
- static void p_rtentry (struct rtentry *);
- static u_long forgemask (u_long);
- static void domask (char *, u_long, u_long);
- /*
- * Print routing tables.
- */
- void
- routepr(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(i);
- p_tree(head.rnh_treetop);
- }
- }
- }
- }
- /*
- * Print address family header before a section of the routing table.
- */
- void
- pr_family(int af)
- {
- char *afname;
- switch (af) {
- case AF_INET:
- afname = "Internet";
- break;
- #ifdef INET6
- case AF_INET6:
- afname = "Internet6";
- break;
- #endif /*INET6*/
- case AF_IPX:
- afname = "IPX";
- break;
- #ifdef NS
- case AF_NS:
- afname = "XNS";
- break;
- #endif
- case AF_ISO:
- afname = "ISO";
- break;
- case AF_APPLETALK:
- afname = "AppleTalk";
- break;
- case AF_CCITT:
- afname = "X.25";
- break;
- case AF_NETGRAPH:
- afname = "Netgraph";
- 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 */
- #ifndef INET6
- #define WID_DST(af) 18 /* width of destination column */
- #define WID_GW(af) 18 /* width of gateway column */
- #define WID_IF(af) 6 /* width of netif column */
- #else
- #define WID_DST(af) \
- ((af) == AF_INET6 ? (Wflag ? 39 : (numeric_addr ? 33: 18)) : 18)
- #define WID_GW(af) \
- ((af) == AF_INET6 ? (Wflag ? 31 : (numeric_addr ? 29 : 18)) : 18)
- #define WID_IF(af) ((af) == AF_INET6 ? 8 : 6)
- #endif /*INET6*/
- /*
- * Print header for routing table columns.
- */
- void
- pr_rthdr(int af)
- {
- if (Aflag)
- printf("%-8.8s ","Address");
- if (af == AF_INET || Wflag)
- if (Wflag)
- printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n",
- WID_DST(af), WID_DST(af), "Destination",
- WID_GW(af), WID_GW(af), "Gateway",
- "Flags", "Refs", "Use", "Mtu",
- WID_IF(af), WID_IF(af), "Netif", "Expire");
- else
- printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n",
- WID_DST(af), WID_DST(af), "Destination",
- WID_GW(af), WID_GW(af), "Gateway",
- "Flags", "Refs", "Use",
- WID_IF(af), WID_IF(af), "Netif", "Expire");
- else
- printf("%-*.*s %-*.*s %-6.6s %8.8s %6s\n",
- WID_DST(af), WID_DST(af), "Destination",
- WID_GW(af), WID_GW(af), "Gateway",
- "Flags", "Netif", "Expire");
- }
- static struct sockaddr *
- kgetsa(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(struct radix_node *rn)
- {
- again:
- kget(rn, rnode);
- if (rnode.rn_bit < 0) {
- if (Aflag)
- printf("%-8.8lx ", (u_long)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.8lx ", (u_long)rn);
- p_rtnode();
- }
- rn = rnode.rn_right;
- p_tree(rnode.rn_left);
- p_tree(rn);
- }
- }
- char nbuf[20];
- static void
- p_rtnode(void)
- {
- struct radix_mask *rm = rnode.rn_mklist;
- if (rnode.rn_bit < 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_bit);
- printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
- }
- while (rm) {
- kget(rm, rmask);
- sprintf(nbuf, " %d refs, ", rmask.rm_refs);
- printf(" mk = %8.8lx {(%d),%s",
- (u_long)rm, -1 - rmask.rm_bit, 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(void)
- {
- 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) {
- err(1, "sysctl: net.route.0.0.dump estimate");
- }
- if ((buf = malloc(needed)) == 0) {
- err(2, "malloc(%lu)", (unsigned long)needed);
- }
- if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
- err(1, "sysctl: net.route.0.0.dump");
- }
- lim = buf + needed;
- for (next = buf; next < lim; next += rtm->;rtm_msglen) {
- rtm = (struct rt_msghdr *)next;
- np_rtentry(rtm);
- }
- }
- static void
- np_rtentry(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);
- sa = (struct sockaddr *)(ROUNDUP(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(struct sockaddr *sa, struct sockaddr *mask, int flags, int 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) &&
- mask &&
- ntohl(((struct sockaddr_in *)mask)->;sin_addr.s_addr)
- ==0L)
- 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;
- }
- #ifdef INET6
- case AF_INET6:
- {
- struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
- struct in6_addr *in6 = &sa6->;sin6_addr;
- /*
- * XXX: This is a special workaround for KAME kernels.
- * sin6_scope_id field of SA should be set in the future.
- */
- if (IN6_IS_ADDR_LINKLOCAL(in6) ||
- IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
- /* XXX: override is ok? */
- sa6->;sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->;s6_addr[2]);
- *(u_short *)&in6->;s6_addr[2] = 0;
- }
- if (flags & RTF_HOST)
- cp = routename6(sa6);
- else if (mask)
- cp = netname6(sa6,
- &((struct sockaddr_in6 *)mask)->;sin6_addr);
- else {
- cp = netname6(sa6, NULL);
- }
- break;
- }
- #endif /*INET6*/
- case AF_IPX:
- {
- struct ipx_addr work = ((struct sockaddr_ipx *)sa)->;sipx_addr;
- if (ipx_nullnet(satoipx_addr(work)))
- cp = "default";
- else
- cp = ipx_print(sa);
- break;
- }
- case AF_APPLETALK:
- {
- if (!(flags & RTF_HOST) && mask)
- cp = atalk_print2(sa,mask,9);
- else
- cp = atalk_print(sa,11);
- break;
- }
- case AF_NETGRAPH:
- {
- printf("%s", ((struct sockaddr_ng *)sa)->;sg_data);
- break;
- }
- #ifdef NS
- case AF_NS:
- cp = ns_print(sa);
- break;
- #endif
- 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 (numeric_addr)
- printf("%-*s ", width, cp);
- else
- printf("%-*.*s ", width, width, cp);
- }
- }
- static void
- p_flags(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(struct rtentry *rt)
- {
- static struct ifnet ifnet, *lastif;
- struct rtentry parent;
- static char name[16];
- static char prettyname[9];
- struct sockaddr *sa;
- sa_u addr, mask;
- /*
- * Don't print protocol-cloned routes unless -a.
- */
- if (rt->;rt_flags & RTF_WASCLONED && !aflag) {
- kget(rt->;rt_parent, parent);
- if (parent.rt_flags & RTF_PRCLONING)
- return;
- }
- bzero(&addr, sizeof(addr));
- if ((sa = kgetsa(rt_key(rt))))
- bcopy(sa, &addr, sa->;sa_len);
- bzero(&mask, sizeof(mask));
- if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
- bcopy(sa, &mask, sa->;sa_len);
- p_sockaddr(&addr.u_sa, &mask.u_sa, rt->;rt_flags,
- WID_DST(addr.u_sa.sa_family));
- p_sockaddr(kgetsa(rt->;rt_gateway), NULL, RTF_HOST,
- WID_GW(addr.u_sa.sa_family));
- p_flags(rt->;rt_flags, "%-6.6s ");
- if (addr.u_sa.sa_family == AF_INET || Wflag) {
- printf("%6ld %8ld ", rt->;rt_refcnt, rt->;rt_use);
- if (Wflag) {
- if (rt->;rt_rmx.rmx_mtu != 0)
- printf("%6lu ", rt->;rt_rmx.rmx_mtu);
- else
- printf("%6s ", "");
- }
- }
- 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;
- snprintf(prettyname, sizeof prettyname,
- "%s%d", name, ifnet.if_unit);
- }
- printf("%*.*s", WID_IF(addr.u_sa.sa_family),
- WID_IF(addr.u_sa.sa_family), prettyname);
- if (rt->;rt_rmx.rmx_expire) {
- time_t expire_time;
- if ((expire_time =
- rt->;rt_rmx.rmx_expire - time((time_t *)0)) >; 0)
- printf(" %6d", (int)expire_time);
- }
- if (rt->;rt_nodes[0].rn_dupedkey)
- printf(" =>;");
- }
- putchar('\n');
- }
- char *
- routename(u_long in)
- {
- register char *cp;
- static char line[MAXHOSTNAMELEN];
- struct hostent *hp;
- cp = 0;
- if (!numeric_addr) {
- hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
- AF_INET);
- if (hp) {
- cp = hp->;h_name;
- trimdomain(cp, strlen(cp));
- }
- }
- if (cp) {
- strncpy(line, cp, sizeof(line) - 1);
- line[sizeof(line) - 1] = '\0';
- } else {
- #define C(x) ((x) & 0xff)
- in = ntohl(in);
- sprintf(line, "%lu.%lu.%lu.%lu",
- C(in >;>; 24), C(in >;>; 16), C(in >;>; 8), C(in));
- }
- return (line);
- }
- static u_long
- forgemask(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(char *dst, u_long addr, u_long 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(u_long in, u_long mask)
- {
- char *cp = 0;
- static char line[MAXHOSTNAMELEN];
- struct netent *np = 0;
- u_long dmask;
- register u_long i;
- #define NSHIFT(m) ( \
- (m) == IN_CLASSA_NET ? IN_CLASSA_NSHIFT : \
- (m) == IN_CLASSB_NET ? IN_CLASSB_NSHIFT : \
- (m) == IN_CLASSC_NET ? IN_CLASSC_NSHIFT : \
- 0)
- i = ntohl(in);
- dmask = forgemask(i);
- if (!numeric_addr && i) {
- np = getnetbyaddr(i >;>; NSHIFT(mask), AF_INET);
- if (np == NULL && mask == 0)
- np = getnetbyaddr(i >;>; NSHIFT(dmask), AF_INET);
- if (np != NULL) {
- cp = np->;n_name;
- trimdomain(cp, strlen(cp));
- }
- }
- #undef NSHIFT
- if (cp != NULL) {
- strncpy(line, cp, sizeof(line) - 1);
- line[sizeof(line) - 1] = '\0';
- } else {
- switch (dmask) {
- case IN_CLASSA_NET:
- if ((i & IN_CLASSA_HOST) == 0) {
- sprintf(line, "%lu", C(i >;>; 24));
- break;
- }
- /* FALLTHROUGH */
- case IN_CLASSB_NET:
- if ((i & IN_CLASSB_HOST) == 0) {
- sprintf(line, "%lu.%lu",
- C(i >;>; 24), C(i >;>; 16));
- break;
- }
- /* FALLTHROUGH */
- case IN_CLASSC_NET:
- if ((i & IN_CLASSC_HOST) == 0) {
- sprintf(line, "%lu.%lu.%lu",
- C(i >;>; 24), C(i >;>; 16), C(i >;>; 8));
- break;
- }
- /* FALLTHROUGH */
- default:
- sprintf(line, "%lu.%lu.%lu.%lu",
- C(i >;>; 24), C(i >;>; 16), C(i >;>; 8), C(i));
- break;
- }
- }
- domask(line + strlen(line), i, mask);
- return (line);
- }
- #ifdef INET6
- char *
- netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask)
- {
- static char line[MAXHOSTNAMELEN];
- u_char *p = (u_char *)mask;
- u_char *lim;
- int masklen, illegal = 0, flag = NI_WITHSCOPEID;
- if (mask) {
- for (masklen = 0, lim = p + 16; p < lim; p++) {
- switch (*p) {
- case 0xff:
- masklen += 8;
- break;
- case 0xfe:
- masklen += 7;
- break;
- case 0xfc:
- masklen += 6;
- break;
- case 0xf8:
- masklen += 5;
- break;
- case 0xf0:
- masklen += 4;
- break;
- case 0xe0:
- masklen += 3;
- break;
- case 0xc0:
- masklen += 2;
- break;
- case 0x80:
- masklen += 1;
- break;
- case 0x00:
- break;
- default:
- illegal ++;
- break;
- }
- }
- if (illegal)
- fprintf(stderr, "illegal prefixlen\n");
- }
- else
- masklen = 128;
- if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->;sin6_addr))
- return("default");
- if (numeric_addr)
- flag |= NI_NUMERICHOST;
- getnameinfo((struct sockaddr *)sa6, sa6->;sin6_len, line, sizeof(line),
- NULL, 0, flag);
- if (numeric_addr)
- sprintf(&line[strlen(line)], "/%d", masklen);
- return line;
- }
- char *
- routename6(struct sockaddr_in6 *sa6)
- {
- static char line[MAXHOSTNAMELEN];
- int flag = NI_WITHSCOPEID;
- /* use local variable for safety */
- struct sockaddr_in6 sa6_local = {AF_INET6, sizeof(sa6_local),};
- sa6_local.sin6_addr = sa6->;sin6_addr;
- sa6_local.sin6_scope_id = sa6->;sin6_scope_id;
- if (numeric_addr)
- flag |= NI_NUMERICHOST;
- getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
- line, sizeof(line), NULL, 0, flag);
- return line;
- }
- #endif /*INET6*/
- /*
- * Print routing statistics
- */
- void
- rt_stats(u_long rtsaddr, u_long rttaddr)
- {
- struct rtstat rtstat;
- int rttrash;
- if (rtsaddr == 0) {
- printf("rtstat: symbol not in namelist\n");
- return;
- }
- if (rttaddr == 0) {
- printf("rttrash: symbol not in namelist\n");
- return;
- }
- kread(rtsaddr, (char *)&rtstat, sizeof (rtstat));
- kread(rttaddr, (char *)&rttrash, sizeof (rttrash));
- printf("routing:\n");
- #define p(f, m) if (rtstat.f || sflag <= 1) \
- printf(m, rtstat.f, plural(rtstat.f))
- p(rts_badredirect, "\t%u bad routing redirect%s\n");
- p(rts_dynamic, "\t%u dynamically created route%s\n");
- p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
- p(rts_unreach, "\t%u destination%s found unreachable\n");
- p(rts_wildcard, "\t%u use%s of a wildcard route\n");
- #undef p
- if (rttrash || sflag <= 1)
- printf("\t%u route%s not in table but not freed\n",
- rttrash, plural(rttrash));
- }
- char *
- ipx_print(struct sockaddr *sa)
- {
- u_short port;
- struct servent *sp = 0;
- char *net = "", *host = "";
- register char *p;
- register u_char *q;
- struct ipx_addr work = ((struct sockaddr_ipx *)sa)->;sipx_addr;
- static char mybuf[50];
- char cport[10], chost[15], cnet[15];
- port = ntohs(work.x_port);
- if (ipx_nullnet(work) && ipx_nullhost(work)) {
- if (port) {
- if (sp)
- sprintf(mybuf, "*.%s", sp->;s_name);
- else
- sprintf(mybuf, "*.%x", port);
- } else
- sprintf(mybuf, "*.*");
- return (mybuf);
- }
- if (ipx_wildnet(work))
- net = "any";
- else if (ipx_nullnet(work))
- net = "*";
- else {
- q = work.x_net.c_net;
- sprintf(cnet, "%02x%02x%02x%02x",
- q[0], q[1], q[2], q[3]);
- for (p = cnet; *p == '0' && p < cnet + 8; p++)
- continue;
- net = p;
- }
- if (ipx_wildhost(work))
- host = "any";
- else if (ipx_nullhost(work))
- host = "*";
- else {
- q = work.x_host.c_host;
- sprintf(chost, "%02x%02x%02x%02x%02x%02x",
- 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) {
- if (strcmp(host, "*") == 0)
- host = "";
- if (sp)
- snprintf(cport, sizeof(cport),
- "%s%s", *host ? "." : "", sp->;s_name);
- else
- snprintf(cport, sizeof(cport),
- "%s%x", *host ? "." : "", port);
- } else
- *cport = 0;
- snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
- return(mybuf);
- }
- char *
- ipx_phost(struct sockaddr *sa)
- {
- register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
- struct sockaddr_ipx work;
- static union ipx_net ipx_zeronet;
- char *p;
- struct ipx_addr in;
- work = *sipx;
- in = work.sipx_addr;
- work.sipx_addr.x_port = 0;
- work.sipx_addr.x_net = ipx_zeronet;
- p = ipx_print((struct sockaddr *)&work);
- if (strncmp("*.", p, 2) == 0) p += 2;
- return(p);
- }
- #ifdef NS
- short ns_nullh[] = {0,0,0};
- short ns_bh[] = {-1,-1,-1};
- char *
- ns_print(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(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);
- }
- #endif
- void
- upHex(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');
- break;
- }
- }
复制代码
netstat.h
- /*
- * Copyright (c) 1992, 1993
- * 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.
- *
- * @(#)netstat.h 8.2 (Berkeley) 1/4/94
- * $FreeBSD: src/usr.bin/netstat/netstat.h,v 1.16.2.7 2001/09/17 15:17:46 ru Exp $
- */
- #include <sys/cdefs.h>;
- extern int Aflag; /* show addresses of protocol control block */
- extern int aflag; /* show all sockets (including servers) */
- extern int bflag; /* show i/f total bytes in/out */
- extern int dflag; /* show i/f dropped packets */
- extern int gflag; /* show group (multicast) routing or stats */
- extern int iflag; /* show interfaces */
- extern int Lflag; /* show size of listen queues */
- extern int mflag; /* show memory stats */
- extern int numeric_addr; /* show addresses numerically */
- extern int numeric_port; /* show ports numerically */
- extern int rflag; /* show routing tables (or routing stats) */
- extern int sflag; /* show protocol statistics */
- extern int tflag; /* show i/f watchdog timers */
- extern int Wflag; /* wide display */
- extern int zflag; /* zero stats */
- extern int interval; /* repeat interval for i/f stats */
- extern char *interface; /* desired i/f for stats, or NULL for all i/fs */
- extern int unit; /* unit number for above */
- extern int af; /* address family */
- int kread (u_long addr, char *buf, int size);
- char *plural (int);
- char *plurales (int);
- void protopr (u_long, char *, int);
- void tcp_stats (u_long, char *, int);
- void udp_stats (u_long, char *, int);
- void ip_stats (u_long, char *, int);
- void icmp_stats (u_long, char *, int);
- void igmp_stats (u_long, char *, int);
- #ifdef IPSEC
- void ipsec_stats (u_long, char *, int);
- #endif
- #ifdef INET6
- void ip6_stats (u_long, char *, int);
- void ip6_ifstats (char *);
- void icmp6_stats (u_long, char *, int);
- void icmp6_ifstats (char *);
- void pim6_stats (u_long, char *, int);
- void rip6_stats (u_long, char *, int);
- void mroute6pr (u_long, u_long);
- void mrt6_stats (u_long);
- struct sockaddr_in6;
- struct in6_addr;
- char *routename6 (struct sockaddr_in6 *);
- char *netname6 (struct sockaddr_in6 *, struct in6_addr *);
- #endif /*INET6*/
- #ifdef IPSEC
- void pfkey_stats (u_long, char *, int);
- #endif
- void bdg_stats (u_long, char *, int);
- void mbpr (u_long, u_long, u_long, u_long);
- void hostpr (u_long, u_long);
- void impstats (u_long, u_long);
- void intpr (int, u_long, void (*)(char *));
- void pr_rthdr (int);
- void pr_family (int);
- void rt_stats (u_long, u_long);
- char *ipx_pnet (struct sockaddr *);
- char *ipx_phost (struct sockaddr *);
- char *ns_phost (struct sockaddr *);
- void upHex (char *);
- char *routename (u_long);
- char *netname (u_long, u_long);
- char *atalk_print (struct sockaddr *, int);
- char *atalk_print2 (struct sockaddr *, struct sockaddr *, int);
- char *ipx_print (struct sockaddr *);
- char *ns_print (struct sockaddr *);
- void routepr (u_long);
- void ipxprotopr (u_long, char *, int);
- void spx_stats (u_long, char *, int);
- void ipx_stats (u_long, char *, int);
- void ipxerr_stats (u_long, char *, int);
- void nsprotopr (u_long, char *, int);
- void spp_stats (u_long, char *, int);
- void idp_stats (u_long, char *, int);
- void nserr_stats (u_long, char *, int);
- void atalkprotopr (u_long, char *, int);
- void ddp_stats (u_long, char *, int);
- void netgraphprotopr (u_long, char *, int);
- void unixpr (void);
- void esis_stats (u_long, char *, int);
- void clnp_stats (u_long, char *, int);
- void cltp_stats (u_long, char *, int);
- void iso_protopr (u_long, char *, int);
- void iso_protopr1 (u_long, int);
- void tp_protopr (u_long, char *, int);
- void tp_inproto (u_long);
- void tp_stats (caddr_t, caddr_t);
- void mroutepr (u_long, u_long);
- void mrt_stats (u_long);
复制代码
netgraph.h
- /*
- * Copyright (c) 1996-1999 Whistle Communications, Inc.
- * All rights reserved.
- *
- * Subject to the following obligations and disclaimer of warranty, use and
- * redistribution of this software, in source or object code forms, with or
- * without modifications are expressly permitted by Whistle Communications;
- * provided, however, that:
- * 1. Any and all reproductions of the source or object code must include the
- * copyright notice above and the following disclaimer of warranties; and
- * 2. No rights are granted, in any manner or form, to use Whistle
- * Communications, Inc. trademarks, including the mark "WHISTLE
- * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
- * such appears in the above copyright notice or in the software.
- *
- * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
- * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
- * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
- * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
- * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
- * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
- * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
- * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
- * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
- * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- *
- * $FreeBSD: src/usr.bin/netstat/netgraph.c,v 1.3.2.2 2001/08/10 09:07:09 ru Exp $
- */
- #ifndef lint
- static const char rcsid[] =
- "$Id: atalk.c,v 1.11 1998/07/06 21:01:22 bde Exp $";
- #endif /* not lint */
- #include <sys/param.h>;
- #include <sys/queue.h>;
- #include <sys/socket.h>;
- #include <sys/socketvar.h>;
- #include <sys/protosw.h>;
- #include <sys/linker.h>;
- #include <net/route.h>;
- #include <netgraph.h>;
- #include <netgraph/ng_message.h>;
- #include <netgraph/ng_socket.h>;
- #include <netgraph/ng_socketvar.h>;
- #include <nlist.h>;
- #include <errno.h>;
- #include <stdio.h>;
- #include <string.h>;
- #include <unistd.h>;
- #include <err.h>;
- #include "netstat.h"
- static int first = 1;
- static int csock = -1;
- void
- netgraphprotopr(u_long off, char *name, int af __unused)
- {
- struct ngpcb *this, *next;
- struct ngpcb ngpcb;
- struct ngsock info;
- struct socket sockb;
- int debug = 1;
- /* If symbol not found, try looking in the KLD module */
- if (off == 0) {
- const char *const modname = "ng_socket.ko";
- /* XXX We should get "mpath" from "sysctl kern.module_path" */
- const char *mpath[] = { "/", "/boot/", "/modules/", NULL };
- struct nlist sym[] = { { "_ngsocklist" }, { NULL } };
- const char **pre;
- struct kld_file_stat ks;
- int fileid;
- /* See if module is loaded */
- if ((fileid = kldfind(modname)) < 0) {
- if (debug)
- warn("kldfind(%s)", modname);
- return;
- }
- /* Get module info */
- memset(&ks, 0, sizeof(ks));
- ks.version = sizeof(struct kld_file_stat);
- if (kldstat(fileid, &ks) < 0) {
- if (debug)
- warn("kldstat(%d)", fileid);
- return;
- }
- /* Get symbol table from module file */
- for (pre = mpath; *pre; pre++) {
- char path[MAXPATHLEN];
- snprintf(path, sizeof(path), "%s%s", *pre, modname);
- if (nlist(path, sym) == 0)
- break;
- }
- /* Did we find it? */
- if (sym[0].n_value == 0) {
- if (debug)
- warnx("%s not found", modname);
- return;
- }
- /* Symbol found at load address plus symbol offset */
- off = (u_long) ks.address + sym[0].n_value;
- }
- /* Get pointer to first socket */
- kread(off, (char *)&this, sizeof(this));
- /* Get my own socket node */
- if (csock == -1)
- NgMkSockNode(NULL, &csock, NULL);
- for (; this != NULL; this = next) {
- u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
- struct ng_mesg *resp = (struct ng_mesg *) rbuf;
- struct nodeinfo *ni = (struct nodeinfo *) resp->;data;
- char path[64];
- /* Read in ngpcb structure */
- kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
- next = LIST_NEXT(&ngpcb, socks);
- /* Read in socket structure */
- kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
- /* Check type of socket */
- if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
- continue;
- if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
- continue;
- /* Do headline */
- if (first) {
- printf("Netgraph sockets\n");
- if (Aflag)
- printf("%-8.8s ", "PCB");
- printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
- "Type", "Recv-Q", "Send-Q",
- "Node Address", "#Hooks");
- first = 0;
- }
- /* Show socket */
- if (Aflag)
- printf("%8lx ", (u_long) this);
- printf("%-5.5s %6lu %6lu ",
- name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
- /* Get ngsock structure */
- if (ngpcb.sockdata == 0) /* unconnected data socket */
- goto finish;
- kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
- /* Get info on associated node */
- if (info.node == 0 || csock == -1)
- goto finish;
- snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
- if (NgSendMsg(csock, path,
- NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
- goto finish;
- if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
- goto finish;
- /* Display associated node info */
- if (*ni->;name != '\0')
- snprintf(path, sizeof(path), "%s:", ni->;name);
- printf("%-14.14s %4d", path, ni->;hooks);
- finish:
- putchar('\n');
- }
- }
复制代码
mroute6.c
[code]
/*
* Copyright (C) 1998 WIDE Project.
* 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 project 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 PROJECT 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 PROJECT 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.
*/
/*
* 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
* $FreeBSD: src/usr.bin/netstat/mroute6.c,v 1.1.2.8 2001/09/17 14:53:17 ru Exp $
*/
#ifdef INET6
#include <sys/param.h>;
#include <sys/queue.h>;
#include <sys/socket.h>;
#include <sys/socketvar.h>;
#include <sys/protosw.h>;
#include <net/if.h>;
#include <net/if_var.h>;
#include <net/route.h>;
#include <netinet/in.h>;
#include <stdio.h>;
#define KERNEL 1
#include <netinet6/ip6_mroute.h>;
#undef KERNEL
#include "netstat.h"
#define WID_ORG (Wflag ? 39 : (numeric_addr ? 29 : 1 ) /* width of origin column */
#define WID_GRP (Wflag ? 18 : (numeric_addr ? 16 : 1 ) /* width of group column */
void
mroute6pr(u_long mfcaddr, u_long mifaddr)
{
struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
struct mif6 mif6table[MAXMIFS];
struct mf6c mfc;
struct rtdetq rte, *rtep;
register struct mif6 *mifp;
register mifi_t mifi;
register int i;
register int banner_printed;
register int saved_numeric_addr;
mifi_t maxmif = 0;
long int waitings;
if (mfcaddr == 0 || mifaddr == 0) {
printf("No IPv6 multicast routing compiled into this"
" system.\n" ;
return;
}
saved_numeric_addr = numeric_addr;
numeric_addr = 1;
kread(mifaddr, (char *)&mif6table, sizeof(mif6table));
banner_printed = 0;
for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
struct ifnet ifnet;
char ifname[IFNAMSIZ];
if (mifp->;m6_ifp == NULL)
continue;
kread((u_long)mifp->;m6_ifp, (char *)&ifnet, sizeof(ifnet));
maxmif = mifi;
if (!banner_printed) {
printf("\nIPv6 Multicast Interface Table\n"
" Mif Rate PhyIF "
" kts-In Pkts-Out\n" ;
banner_printed = 1;
}
printf(" %2u %4d",
mifi, mifp->;m6_rate_limit);
printf(" %5s", (mifp->;m6_flags & MIFF_REGISTER) ?
"reg0" : if_indextoname(ifnet.if_index, ifname));
printf(" %9llu %9llu\n", (unsigned long long)mifp->;m6_pkt_in,
(unsigned long long)mifp->;m6_pkt_out);
}
if (!banner_printed)
printf("\nIPv6 Multicast Interface Table is empty\n" ;
kread(mfcaddr, (char *)&mf6ctable, sizeof(mf6ctable));
banner_printed = 0;
for (i = 0; i < MF6CTBLSIZ; ++i) {
mfcp = mf6ctable;
while(mfcp) {
kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
if (!banner_printed) {
printf ("\nIPv6 Multicast Forwarding Cache\n" ;
printf(" %-*.*s %-*.*s %s",
WID_ORG, WID_ORG, "Origin",
WID_GRP, WID_GRP, "Group",
" Packets Waits In-Mif Out-Mifs\n" ;
banner_printed = 1;
}
printf(" %-*.*s", WID_ORG, WID_ORG,
routename6(&mfc.mf6c_origin));
printf(" %-*.*s", WID_GRP, WID_GRP,
routename6(&mfc.mf6c_mcastgrp));
printf(" %9llu", (unsigned long long)mfc.mf6c_pkt_cnt);
for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
waitings++;
kread((u_long)rtep, (char *)&rte, sizeof(rte));
rtep = rte.next;
}
printf(" %3ld", waitings);
if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
printf(" --- " ;
else
printf(" %3d ", mfc.mf6c_parent);
for (mifi = 0; mifi <= maxmif; mifi++) {
if (IF_ISSET(mifi, &mfc.mf6c_ifset))
printf(" %u", mifi);
}
printf("\n" ;
mfcp = mfc.mf6c_next;
}
}
if (!banner_printed)
printf("\nIPv6 Multicast Routing Table is empty\n" ;
printf("\n" ;
numeric_addr = saved_numeric_addr;
}
void
mrt6_stats(u_long mstaddr)
{
struct mrt6stat mrtstat;
if (mstaddr == 0) {
printf("No IPv6 multicast routing compiled into this"
" system.\n" ;
return;
}
kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
printf("IPv6 multicast forwarding:\n");
#define p(f, m) if (mrtstat.f || sflag <= 1) \
printf(m, (unsigned long long)mrtstat.f, plural(mrtstat.f))
#define p2(f, m) if (mrtstat.f || sflag <= 1) \
pri |
|