|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)if.c 4.4 82/11/14"; ! 3: #endif ! 4: ! 5: #include <sys/types.h> ! 6: #include <sys/socket.h> ! 7: ! 8: #include <net/if.h> ! 9: #include <netinet/in.h> ! 10: #include <netinet/if_ether.h> ! 11: #include <tahoeif/if_enp.h> ! 12: #include <tahoeif/if_acereg.h> ! 13: #include <netdb.h> ! 14: #include <sys/ioctl.h> ! 15: #include <strings.h> ! 16: #include <errno.h> ! 17: #include <nlist.h> ! 18: ! 19: #include <stdio.h> ! 20: #include "nstat.h" ! 21: ! 22: ! 23: /* ! 24: * Internet to ethernet address resolution table. ! 25: */ ! 26: struct arptab { ! 27: struct in_addr at_iaddr; /* internet address */ ! 28: u_char at_enaddr[6]; /* ethernet address */ ! 29: struct mbuf *at_hold; /* last packet until resolved/timeout */ ! 30: u_char at_timer; /* minutes since last reference */ ! 31: u_char at_flags; /* flags */ ! 32: }; ! 33: /* at_flags field values */ ! 34: #define ATF_INUSE 1 /* entry in use */ ! 35: #define ATF_COM 2 /* completed entry (enaddr valid) */ ! 36: ! 37: #define ARPTAB_BSIZ 5 /* bucket size */ ! 38: #define ARPTAB_NB 19 /* number of buckets */ ! 39: #define ARPTAB_SIZE (ARPTAB_BSIZ * ARPTAB_NB) ! 40: ! 41: #define ACE 0 /* ace driver type */ ! 42: #define ENP 1 /* enp driver type */ ! 43: ! 44: ! 45: struct nlist arptabnl[] = { ! 46: # define N_ARPTAB 0 ! 47: { "_arptab" }, ! 48: # define N_NUMACE 1 ! 49: { "_numace" }, ! 50: # define N_NUMENP 2 ! 51: { "_numenp" }, ! 52: # define N_ACESOFTC 3 ! 53: { "_ace_softc" }, ! 54: # define N_ENPSOFTC 4 ! 55: { "_enp_softc" }, ! 56: { "" }, ! 57: }; ! 58: extern int kmem; ! 59: extern char *system; ! 60: extern struct ace_softc acesoftc[]; ! 61: extern struct enp_softc enpsoftc[]; ! 62: extern int s; /* socket id */ ! 63: char what_host[] = "NOT in /etc/hosts"; ! 64: ! 65: /* ! 66: * Print a description of the network interfaces. ! 67: */ ! 68: ! 69: arptab() ! 70: { ! 71: static off_t arptabaddr, ! 72: numaceaddr, ! 73: numenpaddr, ! 74: enpsoftcaddr, ! 75: acesoftcaddr; ! 76: ! 77: struct arptab arp_tab[ARPTAB_SIZE]; ! 78: struct arptab *at = arp_tab; ! 79: struct hostent *h; ! 80: char hostname[64]; ! 81: static int getvm = 0; ! 82: int i, num, dontrepeat = 0; ! 83: ! 84: ! 85: if (!getvm) { ! 86: nlist(system, arptabnl); ! 87: arptabaddr = arptabnl[N_ARPTAB].n_value; ! 88: numaceaddr = arptabnl[N_NUMACE].n_value; ! 89: numenpaddr = arptabnl[N_NUMENP].n_value; ! 90: acesoftcaddr = arptabnl[N_ACESOFTC].n_value; ! 91: enpsoftcaddr = arptabnl[N_ENPSOFTC].n_value; ! 92: if (arptabaddr == 0 || numaceaddr == 0 || numenpaddr == 0 || ! 93: acesoftcaddr == 0 || enpsoftcaddr == 0) { ! 94: printf("arptab: symbol not defined\n"); ! 95: return; ! 96: } ! 97: ++getvm; ! 98: } ! 99: printf("%-20s %-15.10s %-10.10s","Hostname","Internet","Ethernet"); ! 100: putchar('\n'); ! 101: ! 102: /* Search for its own Ethernet Addresses */ ! 103: ! 104: klseek(kmem, numaceaddr, 0); ! 105: read(kmem, (char *)&num, sizeof (int)); ! 106: if ( num ) { /* if there is any ace */ ! 107: klseek(kmem, acesoftcaddr, 0); ! 108: read(kmem, acesoftc, sizeof (struct ace_softc)*num); ! 109: while (--num >= 0) { ! 110: struct ace_softc *is = &acesoftc[num]; ! 111: struct ifnet *ifp = &is->is_if; ! 112: ! 113: if (ifp->if_flags & IFF_RUNNING) {/*is it operating ?*/ ! 114: ! 115: gethostname(hostname, sizeof(hostname)); ! 116: h = gethostbyname(hostname); ! 117: output(h, h->h_addr, is->is_addr); ! 118: if (ifp->if_flags & IFF_NOARP) ! 119: continue; ! 120: if (dontrepeat < 5) { ! 121: whoisup(ifp); ! 122: dontrepeat++; ! 123: } ! 124: } ! 125: } ! 126: } ! 127: ! 128: klseek(kmem, numenpaddr, 0); ! 129: read(kmem, (char *)&num, sizeof (int)); ! 130: ! 131: if ( num ) { /* if there is any enp */ ! 132: klseek(kmem, enpsoftcaddr, 0); ! 133: read(kmem, enpsoftc, sizeof(struct enp_softc)*num); ! 134: while (--num >= 0) { ! 135: struct enp_softc *es = &enpsoftc[num]; ! 136: struct ifnet *ifp = &es->es_if; ! 137: ! 138: if (ifp->if_flags & IFF_RUNNING) {/*is it up ?*/ ! 139: ! 140: gethostname(hostname, sizeof(hostname)); ! 141: h = gethostbyname(hostname); ! 142: output(h, h->h_addr, es->es_enaddr); ! 143: if (ifp->if_flags & IFF_NOARP) ! 144: continue; ! 145: if (dontrepeat < 5) { ! 146: whoisup(ifp); ! 147: dontrepeat++; ! 148: } ! 149: } ! 150: } ! 151: ! 152: } ! 153: ! 154: ! 155: /* And then, foreign hosts' Ethernet Addresses */ ! 156: ! 157: klseek(kmem, arptabaddr, 0); ! 158: read(kmem, (char *)arp_tab, sizeof (struct arptab)*ARPTAB_SIZE); ! 159: gethostname(hostname, sizeof(hostname)); ! 160: for (i = 0; i < ARPTAB_SIZE; i++, at++) { ! 161: if (at->at_flags) { /* if entry not empty */ ! 162: struct hostent *h; ! 163: ! 164: /* Read /etc/hosts to find out hostname */ ! 165: h = gethostbyaddr(&at->at_iaddr,sizeof(struct in_addr), ! 166: AF_INET); ! 167: /* Print hostname and internet addr, excluding ! 168: local host entries that are already printed ! 169: */ ! 170: if (strcmp(h->h_name, hostname)) ! 171: output(h, &at->at_iaddr, at->at_enaddr); ! 172: } ! 173: } ! 174: } ! 175: ! 176: ! 177: ! 178: output(hent, inaddr, enaddr) ! 179: struct hostent *hent; ! 180: struct in_addr *inaddr; ! 181: u_char *enaddr; ! 182: { ! 183: char line[20], *h_name; ! 184: u_char *ucp = (u_char *)inaddr; ! 185: int j, lna; ! 186: ! 187: lna = inet_lnaof( *inaddr ); ! 188: if (lna != INADDR_ANY) { ! 189: sprintf(line, "%u.%u.%u.%u", ucp[0], ucp[1], ucp[2], ucp[3]); ! 190: ! 191: if (hent == NULL) ! 192: h_name = what_host; ! 193: else ! 194: h_name = hent->h_name; ! 195: ! 196: printf("%-20s %-10.10s ", h_name, line); ! 197: for (j = 0 ; j < ETHADDR_SIZE-1; j++) ! 198: printf("%x.", (enaddr[j] & 0xff)); ! 199: printf("%x\n", (enaddr[j] & 0xff)); ! 200: } ! 201: } ! 202: ! 203: ! 204: /* probe around to see who is up & then ask for its ethernet addr */ ! 205: ! 206: whoisup(ifp) ! 207: struct ifnet *ifp; ! 208: { ! 209: register struct ifreq ifr; ! 210: register struct hostent *p; ! 211: struct in_addr in; ! 212: int net; ! 213: char *cp; ! 214: ! 215: klseek(kmem, (off_t)ifp->if_name, 0); ! 216: read(kmem, ifr.ifr_name, sizeof(ifr.ifr_name)); ! 217: ifr.ifr_name[IFNAMSIZ] = '\0'; ! 218: cp = index(ifr.ifr_name, '\0'); ! 219: *cp++ = '0' + ifp->if_unit; ! 220: *cp = 0; ! 221: sethostent(0); ! 222: ! 223: while (p = gethostent()) { ! 224: net = inet_netof(*(struct in_addr *)p->h_addr); ! 225: if (net == ifp->if_net) { ! 226: ifr.ifr_data = p->h_addr; ! 227: if (ioctl(s, SIOCGETETADDR, (caddr_t)&ifr) < 0) { ! 228: Perror("ioctl (SIOCGETETADDR)"); ! 229: exit(1); ! 230: } ! 231: } ! 232: } ! 233: endhostent(); ! 234: return; /* at this point arptab is updated & ready for nstat */ ! 235: } ! 236: ! 237: Perror(cmd) ! 238: char *cmd; ! 239: { ! 240: extern int errno; ! 241: ! 242: fprintf(stderr, "arptab: "); ! 243: switch (errno) { ! 244: ! 245: case ENXIO: ! 246: fprintf(stderr, "%s: ", cmd); ! 247: fprintf(stderr, "no such interface\n"); ! 248: break; ! 249: ! 250: case EPERM: ! 251: fprintf(stderr, "%s: permission denied\n", cmd); ! 252: break; ! 253: ! 254: default: ! 255: perror(cmd); ! 256: } ! 257: exit(1); ! 258: } ! 259:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.