Annotation of researchv10dc/ipc/internet/arp.c, revision 1.1.1.1

1.1       root        1: #include <values.h>
                      2: #include <stdio.h>
                      3: #include <sys/param.h>
                      4: #include <sys/filio.h>
                      5: #include <sys/inio.h>
                      6: #include <sys/enio.h>
                      7: #include <sys/inet/in.h>
                      8: #include <sys/inet/ip_var.h>
                      9: #include <nlist.h>
                     10: 
                     11: extern errno;
                     12: extern char *optarg;
                     13: extern int optind;
                     14: extern int ip_ld;
                     15: 
                     16: struct nlist nl[] = {
                     17:        {"_ip_arps", 0},
                     18:        {"_arpcnt", 0},
                     19:        {0, 0}
                     20: };
                     21: #define NL_ARP 0
                     22: #define NL_NARP 1
                     23: 
                     24: #define MAXARP 512
                     25: struct x{
                     26:        unsigned int inaddr;
                     27:        unsigned char enaddr[6];
                     28: } goo;
                     29: struct ip_arp arp[MAXARP];
                     30: int kern_fd;
                     31: 
                     32: usage()
                     33: {
                     34:        fprintf(stderr, "usage:\tarp hostname\n");
                     35:        fprintf(stderr, "\tarp -a [unix] [kmem]\n");
                     36:        fprintf(stderr, "\tarp -d hostname\n");
                     37:        fprintf(stderr, "\tarp -s hostname ether_addr\n");
                     38:        exit(1);
                     39: }
                     40: 
                     41: main(argc, argv)
                     42: char *argv[];
                     43: {
                     44:        int c, narp;
                     45:        int allf;
                     46:        int deletef;
                     47:        int setf;
                     48:        char *xinu;     
                     49:        char *kmem;
                     50: 
                     51:        allf = deletef = setf = 0;
                     52:        xinu = "/unix";
                     53:        kmem = "/dev/kmem";
                     54: 
                     55:        while((c = getopt(argc, argv, "ads")) != -1) {
                     56:                switch(c){
                     57:                case 'a':
                     58:                        allf = 1;
                     59:                        break;
                     60:                case 'd':
                     61:                        deletef = 1;
                     62:                        break;
                     63:                case 's':
                     64:                        setf = 1;
                     65:                        break;
                     66:                default:
                     67:                        usage();
                     68:                }
                     69:        }
                     70: 
                     71:        if(allf+setf+deletef > 1)
                     72:                usage();
                     73: 
                     74:        if(allf){
                     75:                if (argc > optind+1)
                     76:                        xinu = argv[optind++];
                     77:                if (argc > optind)
                     78:                        kmem = argv[optind];
                     79:                kern_init(xinu, kmem);
                     80:                displayall();
                     81:        } else if(setf){
                     82:                if(optind != argc-2)
                     83:                        usage();
                     84:                set(argv[optind], argv[optind+1]);
                     85:        } else if(deletef){
                     86:                if(optind != argc-1)
                     87:                        usage();
                     88:                delete(argv[optind]);
                     89:        } else {
                     90:                if(optind >= argc)
                     91:                        usage();
                     92:                kern_init(xinu, kmem);
                     93:                narp = readarps();
                     94:                for(; optind < argc; optind++)
                     95:                        display(narp, argv[optind]);
                     96:        }
                     97: }
                     98: 
                     99: etherparse(to, from)
                    100:        unsigned char *to;
                    101:        char *from;
                    102: {
                    103:        int tdig;
                    104:        int fdig;
                    105:        int i;
                    106: 
                    107:        if(strlen(from) != 12){
                    108:                fprintf(stderr, "illegal ether address %s\n", from);
                    109:                exit(1);
                    110:        }
                    111: 
                    112:        for(i = 0; i < 6; i++){
                    113:                fdig = *from++;
                    114:                tdig = fdig > 'a' ? (fdig - 'a' + 10)
                    115:                                : (fdig > 'A' ? (fdig - 'A' + 10) : (fdig - '0'));
                    116:                fdig = *from++;
                    117:                tdig <<= 4;
                    118:                tdig |= fdig > 'a' ? (fdig - 'a' + 10)
                    119:                                : (fdig > 'A' ? (fdig - 'A' + 10) : (fdig - '0'));
                    120:                *to++ = tdig;
                    121:        }
                    122: }
                    123: 
                    124: set(host, address)
                    125:        char* host;
                    126:        char *address;
                    127: {
                    128:        int pfd[2];
                    129:        int i;
                    130: 
                    131:        goo.inaddr = in_address(host);
                    132:        if(goo.inaddr == 0){
                    133:                fprintf(stderr, "can't find ip address for %s\n", host);
                    134:                return;
                    135:        }
                    136:        etherparse(goo.enaddr, address);
                    137:        if(pipe(pfd) < 0){
                    138:                perror("setting");
                    139:                exit(1);
                    140:        }
                    141:        if(ioctl(pfd[0], FIOPUSHLD, &ip_ld) < 0){
                    142:                perror("setting");
                    143:                exit(1);
                    144:        }
                    145:        if(ioctl(pfd[0], IPIORESOLVE, &goo) < 0)
                    146:                perror("can't set ether address");
                    147:        close(pfd[0]);
                    148:        close(pfd[1]);
                    149: }
                    150: 
                    151: delete(host)
                    152:        char *host;
                    153: {
                    154:        int i;
                    155:        int pfd[2];
                    156: 
                    157: 
                    158:        goo.inaddr = in_address(host);
                    159:        if(goo.inaddr == 0){
                    160:                fprintf(stderr, "can't find ip address for %s\n", host);
                    161:                return;
                    162:        }
                    163: 
                    164:        if(pipe(pfd) < 0){
                    165:                perror("setting");
                    166:                exit(1);
                    167:        }
                    168:        if(ioctl(pfd[0], FIOPUSHLD, &ip_ld) < 0){
                    169:                perror("setting");
                    170:                exit(1);
                    171:        }
                    172: 
                    173:        /*
                    174:         *  this assumes the hashing algorithm is the same as that of
                    175:         *  the system
                    176:         */
                    177:        for(i = 0; i < 256; i++){
                    178:                goo.inaddr = i;
                    179:                if(ioctl(pfd[0], IPIORESOLVE, &goo) < 0){
                    180:                        fprintf(stderr, "can't delete ether address for %s\n", host);
                    181:                        break;
                    182:                }
                    183:        }
                    184:        close(pfd[0]);
                    185:        close(pfd[1]);
                    186: }
                    187: 
                    188: kern_init(xinu, kmem)
                    189: char *xinu, *kmem;
                    190: {
                    191:        int i;
                    192: 
                    193:        nlist(xinu, nl);
                    194:        if((long)nl[0].n_value == 0){
                    195:                fprintf(stderr, "nlist %s failed\n", xinu);
                    196:                exit(1);
                    197:        }
                    198:        if(strcmp(kmem, "/dev/kmem") != 0){
                    199:                for(i = 0; nl[i].n_name; i++){
                    200:                        nl[i].n_value &= 0xffffff;
                    201:                }
                    202:        }
                    203:        kern_fd = open(kmem, 0);
                    204:        if(kern_fd < 0){
                    205:                perror(kmem);
                    206:                exit(1);
                    207:        }
                    208: }
                    209: 
                    210: doseek(nlitem)
                    211:        unsigned int nlitem;
                    212: {
                    213:        if (nl[nlitem].n_value == 0)
                    214:                return -1;
                    215:        if(lseek(kern_fd, (long)nl[nlitem].n_value, 0) == -1){
                    216:                perror("seek");
                    217:                exit(1);
                    218:        }
                    219:        return 0;
                    220: }
                    221: 
                    222: void
                    223: doseekoff(nlitem, offset)
                    224:        unsigned int nlitem;
                    225:        unsigned int offset;
                    226: {
                    227:        if(lseek(kern_fd, (long)(nl[nlitem].n_value+offset), 0) == -1){
                    228:                perror("seek");
                    229:                exit(1);
                    230:        }
                    231: }
                    232: 
                    233: void
                    234: doread(addr, size)
                    235:        char *addr;
                    236:        unsigned int size;
                    237: {
                    238:        if(read(kern_fd, addr, size) < 0){
                    239:                perror("read");
                    240:                exit(1);
                    241:        }
                    242: }
                    243: 
                    244: readarps()
                    245: {
                    246:        int narp;
                    247: 
                    248:        if (doseek(NL_NARP) < 0) {
                    249:                fprintf(stderr, "Arp not compiled into this kernel\n");
                    250:                exit(1);
                    251:        }
                    252:        doread((char *)&narp, sizeof narp);
                    253:        if (narp > MAXARP){
                    254:                fprintf(stderr, "more arps than I can handle\n");
                    255:                narp = MAXARP;
                    256:        }
                    257:        doseek(NL_ARP);
                    258:        doread((char *)arp, narp*sizeof(struct ip_arp));
                    259:        return narp;
                    260: }
                    261: 
                    262: displayall()
                    263: {
                    264:        int narp, i, j;
                    265: 
                    266:        narp = readarps();
                    267:        for(i = 0; i < narp; i++){
                    268:                if(arp[i].inaddr >= MAXARP){
                    269:                        printf("%s %28.28s ", in_ntoa(arp[i].inaddr), in_host(arp[i].inaddr));
                    270:                        for(j = 0; j < 6; j++){
                    271:                                printf("%02x", arp[i].enaddr[j]);
                    272:                        }
                    273:                        printf("\n");
                    274:                }
                    275:        }
                    276: }
                    277: 
                    278: display(narp, host)
                    279:        int narp;
                    280:        char *host;
                    281: {
                    282:        int i, j;
                    283: 
                    284:        goo.inaddr = in_address(host);
                    285:        if(goo.inaddr == 0){
                    286:                fprintf(stderr, "can't find ip address for %s\n", host);
                    287:                return;
                    288:        }
                    289:        for(i = 0; i < narp; i++){
                    290:                if(arp[i].inaddr == goo.inaddr){
                    291:                        printf("%.15s %.28s ", in_ntoa(arp[i].inaddr), in_host(arp[i].inaddr));
                    292:                        for(j = 0; j < 6; j++){
                    293:                                printf("%02x", arp[i].enaddr[j]);
                    294:                        }
                    295:                        printf("\n");
                    296:                }
                    297:        }
                    298: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.