📄 netshow.c
字号:
/* netShow.c - network information display routines *//* Copyright 1984-1997 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------03h,26aug98,fle doc : added .CS in EXAMPLE of arpShow header03g,14dec97,jdi doc: cleanup.03f,10dec97,kbw minor man page fixes03e,03dec97,vin added netStackDataPoolShow() netStackSysPoolShow().03d,26oct97,kbw minor man page fixes03c,26sep97,rjc fix for SPR 932603b,26aug97,spm removed compiler warnings (SPR #7866)03a,12aug97,vin added netpool specific stuff for mbufShow.02z,12aug97,rjc added if type to ifShow SPR7555.02y,11aug97,vin changed clPoolShow.02x,01aug97,kbw fix man page problems found in beta review02w,07jul97,rjc modifications for newer version of rtentry.02v,30jun97,rjc restored old rtentry structure size.02u,03jun97,rjc stopped converting all 0 mask to all 1.02t,02jun97,spm corrected modification history entry02s,29may97,rjc fixed mRouteShow byte order in mask.02r,08mar97,vin added changes to accomodate changes in pcb structure. moved out igmp, icmp, udp, tcp code to igmpShow, icmpShow, udpShow, tcpShow.02q,07apr97,spm removed DHCP client dependencies02p,13feb97,rjc added mRouteShow02o,31jan97,vin fixed a bug in clPoolShow().02n,31jan97,spm moved DHCP client show routines to dhcpShowLib.c02m,29jan97,spm made global variables for DHCP client unique.02l,29jan97,spm removed conditional includes from DHCP client code.02k,07jan97,vin added unknown protocol in ipstatShow.02j,05dec96,vin added new mbufTypes MT_IPMADDR,MT_IFMADDR,MT_MRTABLE.02i,03dec96,spm correcting errors in DHCP client show routines.02h,27nov96,spm added show routines for DHCP client.02g,17nov96,vin modified show routines to include new clusters. deleted cluster related fields from mbufShow().02f,11sep96,vin modified for BSD4.4.02e,13mar95,dzb changed tcpDebugShow() to call tcpReportRtn (SPR #3928).02d,18jan95,rhp add pointers to literature in library man page (SPR#3591)02c,18jan95,jdi doc cleanup for tcpDebugShow().02b,11aug94,dzb added tcpDebugShow() to get at tcp_debug.c:tcp_report(). warning cleanup.02a,11aug93,jmm Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h01z,02feb93,jdi documentation cleanup for 5.1.01y,13nov92,dnw added include of semLibP.h01x,12oct92,jdi fixed mangen problem in routestatShow().01w,04sep92,jmm fixed typo in printf in inpcbPrint() changed field width in inetPrint() to 15 from 12 (spr 1470)01v,29jul92,smb changed putchar() to printf().01u,27jul92,elh Moved routeShow and hostShow here.01t,04jul92,smb added include vxWorks.h01s,26jun92,wmd modified inetPrint() to always print port address. 01r,11jun92,elh moved arpShow here from arpLib.01q,26may92,rrr the tree shuffle -changed includes to have absolute path from h/01p,26may92,kdl changed ifShow() to more robustly avoid dereferencing null ptr (SPR #1389).01o,01jan92,elh changed arptabShow to call arpShow.01n,13dec91,gae ANSI fixes.01m,17nov91,elh added routestatShow().01l,07nov91,elh changed calls to inet_ntoa to inet_ntoa_b, so memory isn't lost.01k,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed VOID to void -changed copyright notice01j,14aug91,jrb (intel) improved instrumentation for udp in udpstatShow command with the following fields: udptotal - total packets, ipackets - input, opackets - output, fullsock - socket overflows, noportbcast - broadcast port messages with no listener. Taken from latest BSD reno udp.01i,17jun91,del added include of strLib.h.01h,07may91,jdi documentation tweaks.01g,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by elh.01f,20feb91,jaa documentation.01e,08oct90,hjb moved include of "ip.h" above "tcp.h" to avoid redefinition of IP_MSS.01d,02oct90,hjb deleted "netdb.h". made ipstatShow() void.01c,12aug90,hjb added arptabShow().01b,10aug90,kdl added forward declarations for functions returning void.01a,26jun90,hjb created by moving Show routines out of various files.*//*DESCRIPTIONThis library provides routines to show various network-relatedstatistics, such as configuration parameters for network interfaces,protocol statistics, socket statistics, and so on.Interpreting these statistics requires detailed knowledge of Internetnetwork protocols. Information on these protocols can be found inthe following books:.iP.I "Internetworking with TCP/IP Volume III,"by Douglas Comer and David Stevens.iP.I "UNIX Network Programming,"by Richard Stevens.iP.I "The Design and Implementation of the 4.3 BSD UNIX Operating System,"by Leffler, McKusick, Karels and Quarterman.LPThe netShowInit() routine links the network show facility into the VxWorkssystem. This is performed automatically if INCLUDE_NET_SHOW is definedin configAll.h.SEE ALSO: ifLib, icmpShow, igmpShow, tcpShow, udpShow.pG "Network"*/#include "vxWorks.h"#include "sys/types.h"#include "netinet/in.h"#include "net/socketvar.h"#include "sys/socket.h"#include "netinet/in_systm.h"#include "netinet/ip.h"#include "netinet/ip_var.h"#include "net/route.h"#include "net/radix.h"#include "netinet/in_pcb.h"#include "hostLib.h"#include "net/if.h"#include "net/if_dl.h"#include "netinet/if_ether.h"#include "netinet/in_var.h"#include "errno.h"#include "net/mbuf.h"#include "inetLib.h"#include "string.h"#include "stdio.h"#include "arpLib.h"#include "private/semLibP.h"/* externs */IMPORT struct rtstat rtstat; /* routing statistics */IMPORT struct ifnet *ifnet; /* list of all network interfaces */IMPORT struct in_ifaddr *in_ifaddr; /* list of addr for net interfaces */IMPORT struct ipstat ipstat;IMPORT struct radix_node_head *rt_tables[]; /* table of radix nodes */IMPORT LIST hostList;IMPORT SEMAPHORE hostListSem;IMPORT NET_POOL_ID _pNetDpool;/* globals */struct inpcbhead * _pUdpPcbHead = NULL; /* initialized in udpShow.c */struct inpcbhead * _pTcpPcbHead = NULL; /* initialized in tcpShow.c */VOIDFUNCPTR _pTcpPcbPrint = NULL; /* initialized in tcpShow.c *//* locals */LOCAL char * pIfTypes [] = /* interface types from mib-2 */ { "UNDEFINED", "OTHER", "REGULAR_1822", "HDH1822", "DDN_X25", "RFC877_X25", "ETHERNET_CSMACD", "ISO88023_CSMACD", "ISO88024_TOKENBUS", "ISO88025_TOKENRING", "ISO88026_MAN", "STARLAN", "PROTEON_10MBIT", "PROTEON_80MBIT", "HYPERCHANNEL", "FDDI", "LAPB", "SDLC", "DS1", "E1", "BASIC_ISDN", "PRIMARY_ISDN", "PROP_POINT_TO_POINT_SERIAL", "PPP", "SOFTWARE_LOOPBACK", "EON", "ETHERNET_3MBIT", "NSIP", "SLIP", "ULTRA", "DS3", "SIP", "FRAME_RELAY", };/* forward static functions */static void ifAddrPrint (char *str, struct sockaddr *addr);static void ifFlagPrint (u_short flag);static void ifEtherPrint (u_char enaddr [ 6 ]);static void inetPrint (struct in_addr *in, u_short port);static int rtEntryPrint (struct radix_node *rn, void * pRtType);static int rtTblShow (int rtType);static void inpcbPrint (char *name, struct inpcb *pInpcb);#define plural(num) ((num) > 1 ? "s": "")#define RT_NET 0x001 /* network route */#define RT_HST 0x010 /* host route */#define RT_ARP 0x100 /* arp rotue entry *//********************************************************************************* ifShow - display the attached network interfaces** This routine displays the attached network interfaces for debugging and* diagnostic purposes. If <ifName> is given, only the interfaces belonging* to that group are displayed. If <ifName> is omitted, all attached* interfaces are displayed.** For each interface selected, the following are shown: Internet* address, point-to-point peer address (if using SLIP), broadcast address,* netmask, subnet mask, Ethernet address, route metric, maximum transfer* unit, number of packets sent and received on this interface, number of* input and output errors, and flags (such as loopback, point-to-point,* broadcast, promiscuous, ARP, running, and debug).** EXAMPLE:* The following call displays all interfaces whose names begin with "ln",* (such as "ln0", "ln1", and "ln2"):* .CS* -> ifShow "ln"* .CE* The following call displays just the interface "ln0":* .CS* -> ifShow "ln0"* .CE** RETURNS: N/A** SEE ALSO: routeShow(), ifLib*/void ifShow ( char *ifName /* name of the interface to show */ ) { FAST struct ifnet *ifp; FAST struct ifaddr *ifa; FAST struct in_ifaddr *ia = 0; FAST int nameLen; BOOL found; int ifUnit; found = FALSE; for (ifp = ifnet; ifp!= NULL; ifp = ifp->if_next) { nameLen = strlen (ifp->if_name); /* * If no name is specified, every interface is a match. */ if (ifName != NULL) /* if a name was specified */ { if (strncmp (ifName, ifp->if_name, nameLen) != 0) continue; /* no match - wrong name */ /* * If the user specified unit ID in ifName compare the unit number */ if (nameLen < strlen (ifName)) { sscanf (&ifName [nameLen], "%d", &ifUnit); if (ifp->if_unit != ifUnit) continue; /* no match - wrong unit ID */ } } /* * Find address for this interface, if it exists. */ for (ia = in_ifaddr; ia != NULL; ia = ia->ia_next) { if (ia->ia_ifp == ifp) break; } found = TRUE; printf ("%s (unit number %d):\n", ifp->if_name, ifp->if_unit); ifFlagPrint (ifp->if_flags); printf (" Type: %s\n", pIfTypes [ifp->if_type]); for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family == AF_INET) { ifAddrPrint ("Internet address", ifa->ifa_addr); if (ifp->if_flags & IFF_POINTOPOINT) ifAddrPrint ("Destination Internet address", ifa->ifa_dstaddr); else if (ifp->if_flags & IFF_BROADCAST) ifAddrPrint ("Broadcast address", ifa->ifa_broadaddr); } } printf (" Netmask 0x%lx Subnetmask 0x%lx\n", ia->ia_netmask, ia->ia_subnetmask); /* * if the interface is not LOOPBACK or SLIP, print the link * level address. */ if (!(ifp->if_flags & IFF_POINTOPOINT) && !(ifp->if_flags & IFF_LOOPBACK)) ifEtherPrint (((struct arpcom *)ifp)->ac_enaddr); printf(" Metric is %ld\n", ifp->if_metric); printf(" Maximum Transfer Unit size is %ld\n", ifp->if_mtu); printf(" %ld packets received; %ld packets sent\n", ifp->if_ipackets, ifp->if_opackets); printf(" %ld multicast packets received\n", ifp->if_imcasts); printf(" %ld multicast packets sent\n", ifp->if_omcasts); printf(" %ld input errors; %ld output errors\n", ifp->if_ierrors, ifp->if_oerrors); printf(" %ld collisions; %ld dropped\n", ifp->if_collisions, ifp->if_iqdrops); } if (found == FALSE) { if (ifName != NULL) printErr("\"%s\" - No such interface\n", ifName); else printErr("No network interface active.\n"); } }/********************************************************************************* ifAddrPrint - print Internet address for the interface** Prints the Internet address (for the network interfaces)* in readable format.** RETURNS: N/A.*/LOCAL void ifAddrPrint ( char *str, /* comment string to be printed */ struct sockaddr *addr /* internet address for the interface */ ) { char addrString [INET_ADDR_LEN]; inet_ntoa_b (((struct sockaddr_in *)addr)->sin_addr, addrString); printf(" %s: %s\n", str, addrString); }/********************************************************************************* ifFlagPrint - print flags for the network interface** ifFlagPrint prints each of the flags specified for the network* interfaces in user-readable format.** RETURNS: N/A.*/LOCAL void ifFlagPrint ( FAST ushort flag /* network interface flags */ ) { printf(" Flags: (0x%x) ", flag); if (flag & IFF_UP) printf ("UP "); else /* shouldn't happen if the interface is deleted properly */ printf ("DOWN "); if (flag & IFF_LOOPBACK) printf ("LOOPBACK "); if (flag & IFF_POINTOPOINT) printf ("POINT-TO-POINT "); if (flag & IFF_BROADCAST) printf ("BROADCAST "); if (flag & IFF_MULTICAST) printf ("MULTICAST "); if (flag & IFF_PROMISC)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -