17905Ssam #ifndef lint 2*21906Skarels static char sccsid[] = "@(#)if.c 4.7 85/06/03"; 37905Ssam #endif 47905Ssam 57905Ssam #include <sys/types.h> 67905Ssam #include <sys/socket.h> 79203Ssam 87905Ssam #include <net/if.h> 99203Ssam #include <netinet/in.h> 10*21906Skarels #include <netinet/in_var.h> 119203Ssam 128331Ssam #include <stdio.h> 137905Ssam 147905Ssam extern int kmem; 157905Ssam extern int tflag; 167905Ssam extern int nflag; 1717402Sedward extern char *interface; 1817402Sedward extern int unit; 19*21906Skarels extern char *routename(), *netname(); 207905Ssam 217905Ssam /* 227905Ssam * Print a description of the network interfaces. 237905Ssam */ 247905Ssam intpr(interval, ifnetaddr) 257905Ssam int interval; 267905Ssam off_t ifnetaddr; 277905Ssam { 287905Ssam struct ifnet ifnet; 29*21906Skarels union { 30*21906Skarels struct ifaddr ifa; 31*21906Skarels struct in_ifaddr in; 32*21906Skarels } ifaddr; 33*21906Skarels off_t ifaddraddr; 347905Ssam char name[16]; 357905Ssam 367905Ssam if (ifnetaddr == 0) { 377905Ssam printf("ifnet: symbol not defined\n"); 387905Ssam return; 397905Ssam } 407905Ssam if (interval) { 417905Ssam sidewaysintpr(interval, ifnetaddr); 427905Ssam return; 437905Ssam } 447905Ssam klseek(kmem, ifnetaddr, 0); 457905Ssam read(kmem, &ifnetaddr, sizeof ifnetaddr); 468372Ssam printf("%-5.5s %-5.5s %-10.10s %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s", 477905Ssam "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs", 487905Ssam "Opkts", "Oerrs"); 497905Ssam printf(" %-6.6s", "Collis"); 507905Ssam if (tflag) 517905Ssam printf(" %-6.6s", "Timer"); 527905Ssam putchar('\n'); 53*21906Skarels ifaddraddr = 0; 54*21906Skarels while (ifnetaddr || ifaddraddr) { 557905Ssam struct sockaddr_in *sin; 567905Ssam register char *cp; 57*21906Skarels int n; 587905Ssam char *index(); 598372Ssam struct in_addr in, inet_makeaddr(); 607905Ssam 61*21906Skarels if (ifaddraddr == 0) { 62*21906Skarels klseek(kmem, ifnetaddr, 0); 63*21906Skarels read(kmem, &ifnet, sizeof ifnet); 64*21906Skarels klseek(kmem, (off_t)ifnet.if_name, 0); 65*21906Skarels read(kmem, name, 16); 66*21906Skarels name[15] = '\0'; 67*21906Skarels ifnetaddr = (off_t) ifnet.if_next; 68*21906Skarels if (interface != 0 && 69*21906Skarels (strcmp(name, interface) != 0 || unit != ifnet.if_unit)) 70*21906Skarels continue; 71*21906Skarels cp = index(name, '\0'); 72*21906Skarels *cp++ = ifnet.if_unit + '0'; 73*21906Skarels if ((ifnet.if_flags&IFF_UP) == 0) 74*21906Skarels *cp++ = '*'; 75*21906Skarels *cp = '\0'; 76*21906Skarels ifaddraddr = (off_t)ifnet.if_addrlist; 77*21906Skarels } 787905Ssam printf("%-5.5s %-5d ", name, ifnet.if_mtu); 79*21906Skarels if (ifaddraddr == 0) { 80*21906Skarels printf("%-10.10s ", "none"); 81*21906Skarels printf("%-12.12s ", "none"); 82*21906Skarels } else { 83*21906Skarels klseek(kmem, ifaddraddr, 0); 84*21906Skarels read(kmem, &ifaddr, sizeof ifaddr); 85*21906Skarels ifaddraddr = (off_t)ifaddr.ifa.ifa_next; 86*21906Skarels switch (ifaddr.ifa.ifa_addr.sa_family) { 87*21906Skarels case AF_UNSPEC: 88*21906Skarels printf("%-10.10s ", "none"); 89*21906Skarels printf("%-12.12s ", "none"); 90*21906Skarels break; 91*21906Skarels case AF_INET: 92*21906Skarels sin = (struct sockaddr_in *)&ifaddr.in.ia_addr; 93*21906Skarels #ifdef notdef 94*21906Skarels /* can't use inet_makeaddr because kernel 95*21906Skarels * keeps nets unshifted. 96*21906Skarels */ 97*21906Skarels in = inet_makeaddr(ifaddr.in.ia_subnet, 98*21906Skarels INADDR_ANY); 99*21906Skarels printf("%-10.10s ", netname(in)); 100*21906Skarels #else 101*21906Skarels printf("%-10.10s ", 102*21906Skarels netname(htonl(ifaddr.in.ia_subnet), 103*21906Skarels ifaddr.in.ia_subnetmask)); 104*21906Skarels #endif 105*21906Skarels printf("%-12.12s ", routename(sin->sin_addr)); 106*21906Skarels break; 107*21906Skarels default: 108*21906Skarels printf("af%2d: ", ifaddr.ifa.ifa_addr.sa_family); 109*21906Skarels for (cp = (char *)&ifaddr.ifa.ifa_addr + 110*21906Skarels sizeof(struct sockaddr) - 1; 111*21906Skarels cp >= ifaddr.ifa.ifa_addr.sa_data; --cp) 112*21906Skarels if (*cp != 0) 113*21906Skarels break; 114*21906Skarels n = cp - (char *)ifaddr.ifa.ifa_addr.sa_data + 1; 115*21906Skarels cp = (char *)ifaddr.ifa.ifa_addr.sa_data; 116*21906Skarels if (n <= 6) 117*21906Skarels while (--n) 118*21906Skarels printf("%02d.", *cp++ & 0xff); 119*21906Skarels else 120*21906Skarels while (--n) 121*21906Skarels printf("%02d", *cp++ & 0xff); 122*21906Skarels printf("%02d ", *cp & 0xff); 123*21906Skarels break; 124*21906Skarels } 125*21906Skarels } 126*21906Skarels printf("%-7d %-5d %-7d %-5d %-6d", 1277905Ssam ifnet.if_ipackets, ifnet.if_ierrors, 1287905Ssam ifnet.if_opackets, ifnet.if_oerrors, 1297905Ssam ifnet.if_collisions); 1307905Ssam if (tflag) 1317905Ssam printf(" %-6d", ifnet.if_timer); 1327905Ssam putchar('\n'); 1337905Ssam } 1347905Ssam } 1357905Ssam 1368331Ssam #define MAXIF 10 1378331Ssam struct iftot { 1388331Ssam char ift_name[16]; /* interface name */ 1398331Ssam int ift_ip; /* input packets */ 1408331Ssam int ift_ie; /* input errors */ 1418331Ssam int ift_op; /* output packets */ 1428331Ssam int ift_oe; /* output errors */ 1438331Ssam int ift_co; /* collisions */ 1448331Ssam } iftot[MAXIF]; 1458331Ssam 1468331Ssam /* 1478331Ssam * Print a running summary of interface statistics. 1488331Ssam * Repeat display every interval seconds, showing 1498331Ssam * statistics collected over that interval. First 1508331Ssam * line printed at top of screen is always cumulative. 1518331Ssam */ 1527905Ssam sidewaysintpr(interval, off) 1537905Ssam int interval; 1547905Ssam off_t off; 1557905Ssam { 1567905Ssam struct ifnet ifnet; 1578331Ssam off_t firstifnet; 1588331Ssam register struct iftot *ip, *total; 1598331Ssam register int line; 1608331Ssam struct iftot *lastif, *sum, *interesting; 1618331Ssam int maxtraffic; 1627905Ssam 1637905Ssam klseek(kmem, off, 0); 1648331Ssam read(kmem, &firstifnet, sizeof (off_t)); 1658331Ssam lastif = iftot; 1668331Ssam sum = iftot + MAXIF - 1; 1678331Ssam total = sum - 1; 16817402Sedward interesting = iftot; 1698331Ssam for (off = firstifnet, ip = iftot; off;) { 1708331Ssam char *cp; 1717905Ssam 1727905Ssam klseek(kmem, off, 0); 1737905Ssam read(kmem, &ifnet, sizeof ifnet); 1747905Ssam klseek(kmem, (int)ifnet.if_name, 0); 1758331Ssam ip->ift_name[0] = '('; 1768331Ssam read(kmem, ip->ift_name + 1, 15); 17717402Sedward if (interface && strcmp(ip->ift_name + 1, interface) == 0 && 17817402Sedward unit == ifnet.if_unit) 17917402Sedward interesting = ip; 1808331Ssam ip->ift_name[15] = '\0'; 1818331Ssam cp = index(ip->ift_name, '\0'); 1828331Ssam sprintf(cp, "%d)", ifnet.if_unit); 1838331Ssam ip++; 1848331Ssam if (ip >= iftot + MAXIF - 2) 1858331Ssam break; 1867905Ssam off = (off_t) ifnet.if_next; 1877905Ssam } 1888331Ssam lastif = ip; 1898331Ssam banner: 1908331Ssam printf(" input %-6.6s output ", interesting->ift_name); 1918331Ssam if (lastif - iftot > 0) 19217402Sedward printf(" input (Total) output "); 1938331Ssam for (ip = iftot; ip < iftot + MAXIF; ip++) { 1948331Ssam ip->ift_ip = 0; 1958331Ssam ip->ift_ie = 0; 1968331Ssam ip->ift_op = 0; 1978331Ssam ip->ift_oe = 0; 1988331Ssam ip->ift_co = 0; 1998331Ssam } 2008331Ssam putchar('\n'); 2018331Ssam printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ", 2028331Ssam "packets", "errs", "packets", "errs", "colls"); 2038331Ssam if (lastif - iftot > 0) 2048331Ssam printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ", 2058331Ssam "packets", "errs", "packets", "errs", "colls"); 2068331Ssam putchar('\n'); 2078331Ssam fflush(stdout); 2088331Ssam line = 0; 2098331Ssam loop: 2108331Ssam sum->ift_ip = 0; 2118331Ssam sum->ift_ie = 0; 2128331Ssam sum->ift_op = 0; 2138331Ssam sum->ift_oe = 0; 2148331Ssam sum->ift_co = 0; 2158331Ssam for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) { 2168331Ssam klseek(kmem, off, 0); 2178331Ssam read(kmem, &ifnet, sizeof ifnet); 2188331Ssam if (ip == interesting) 2198331Ssam printf("%-7d %-5d %-7d %-5d %-5d ", 2208331Ssam ifnet.if_ipackets - ip->ift_ip, 2218331Ssam ifnet.if_ierrors - ip->ift_ie, 2228331Ssam ifnet.if_opackets - ip->ift_op, 2238331Ssam ifnet.if_oerrors - ip->ift_oe, 2248331Ssam ifnet.if_collisions - ip->ift_co); 2258331Ssam ip->ift_ip = ifnet.if_ipackets; 2268331Ssam ip->ift_ie = ifnet.if_ierrors; 2278331Ssam ip->ift_op = ifnet.if_opackets; 2288331Ssam ip->ift_oe = ifnet.if_oerrors; 2298331Ssam ip->ift_co = ifnet.if_collisions; 2308331Ssam sum->ift_ip += ip->ift_ip; 2318331Ssam sum->ift_ie += ip->ift_ie; 2328331Ssam sum->ift_op += ip->ift_op; 2338331Ssam sum->ift_oe += ip->ift_oe; 2348331Ssam sum->ift_co += ip->ift_co; 2358331Ssam off = (off_t) ifnet.if_next; 2368331Ssam } 2378331Ssam if (lastif - iftot > 0) 2388331Ssam printf("%-7d %-5d %-7d %-5d %-5d\n", 2398331Ssam sum->ift_ip - total->ift_ip, 2408331Ssam sum->ift_ie - total->ift_ie, 2418331Ssam sum->ift_op - total->ift_op, 2428331Ssam sum->ift_oe - total->ift_oe, 2438331Ssam sum->ift_co - total->ift_co); 2448331Ssam *total = *sum; 2458331Ssam fflush(stdout); 2468331Ssam line++; 2478331Ssam if (interval) 2488331Ssam sleep(interval); 2498331Ssam if (line == 21) 2508331Ssam goto banner; 2518331Ssam goto loop; 2528331Ssam /*NOTREACHED*/ 2537905Ssam } 254