Annotation of researchv10no/ipc/internet/transform.c, revision 1.1.1.1

1.1       root        1: #include <sys/types.h>
                      2: #include <sys/types.h>
                      3: #include <sys/stat.h>
                      4: #include <sys/inet/in.h>
                      5: #include <stdio.h>
                      6: #include <ctype.h>
                      7: 
                      8: 
                      9: struct namefile {
                     10:        char *name;
                     11:        char *data;
                     12:        long time;
                     13: };
                     14: 
                     15: static struct namefile files[] = {
                     16:        { "/tmp/networks", 0, 0 },
                     17:        { 0, 0, 0},
                     18: };
                     19: 
                     20: struct hash {
                     21:        struct hash *next;
                     22:        in_addr addr;
                     23:        char *name;
                     24: };
                     25: 
                     26: #define HSIZE 1023
                     27: static struct hash ht[HSIZE];
                     28: 
                     29: /* imported */
                     30: extern char *in_getw();
                     31: extern char *malloc();
                     32: 
                     33: /* exported */
                     34: int in_host_dostat=1;
                     35: 
                     36: static int
                     37: getfile(i)
                     38:        int i;
                     39: {
                     40:        int fd;
                     41:        struct stat sbuf;
                     42:        unsigned int x;
                     43: 
                     44:        if (in_host_dostat)
                     45:                stat(files[i].name, &sbuf);
                     46:        else
                     47:                sbuf.st_mtime = files[i].time;
                     48:        if (files[i].data == 0 || sbuf.st_mtime>files[i].time) {
                     49:                if (!in_host_dostat)
                     50:                        stat(files[i].name, &sbuf);
                     51:                fd = open(files[i].name, 0);
                     52:                if (fd<0)
                     53:                        return -1;
                     54:                if (files[i].data!=0)
                     55:                        free(files[i].data);
                     56:                x = sbuf.st_size;
                     57:                files[i].data = malloc(x+2);
                     58:                if (files[i].data==0) {
                     59:                        close(fd);
                     60:                        return -1;
                     61:                }
                     62:                if (read(fd, files[i].data, x)!=x){
                     63:                        close(fd);
                     64:                        return -1;
                     65:                }
                     66:                close(fd);
                     67:                files[i].data[sbuf.st_size] = '\n';
                     68:                files[i].data[sbuf.st_size+1] = 0;
                     69:                files[i].time = sbuf.st_mtime;
                     70:                for (i=0; i<HSIZE; i++)
                     71:                        ht[i].addr = 0;
                     72:        }
                     73:        return 0;
                     74: }
                     75: 
                     76: #define SKIPWHITE while(*p==' ' || *p=='\t') p++
                     77: #define GETBLACK       xp = b;\
                     78:                        while(*p!=' ' && *p!='\t' && *p!='\n' && *p!='\0')\
                     79:                                *xp++ = *p++;\
                     80:                        *xp = '\0'
                     81: 
                     82: char *
                     83: in_host(addr)
                     84: in_addr addr;
                     85: {
                     86:        register char *p, *xp;
                     87:        unsigned char *yp;
                     88:        static char b[32];
                     89:        int i;
                     90:        in_addr x, in_netof(), in_aton();
                     91: 
                     92:        if (addr==0)
                     93:                return "*";
                     94:        i = addr==in_netof(addr) ? 0 : 1;
                     95:        if (getfile(i) < 0) {
                     96:                perror(files[i].name);
                     97:        } else if (addr == ht[addr%HSIZE].addr) {
                     98:                p = ht[addr%HSIZE].name;
                     99:                GETBLACK;
                    100:                return(b);
                    101:        } else for (p = files[i].data; *p; p++){
                    102:                /* get number */
                    103:                SKIPWHITE;
                    104:                GETBLACK;
                    105:                x = in_aton(b);
                    106:                SKIPWHITE;
                    107:                if (x != 0 && *p != '\n') {
                    108:                        /* get name & insert into hash table */
                    109:                        i = x%HSIZE;
                    110:                        ht[i].addr = x;
                    111:                        ht[i].name = p;
                    112:                        GETBLACK;
                    113: 
                    114:                        /* see if this is the one */
                    115:                        if (x==addr)
                    116:                                return b;
                    117:                }
                    118:                /* drop rest of line */
                    119:                while(*p!='\n'&&*p!='\0') p++;
                    120:        }
                    121:        yp = (unsigned char *) &addr;
                    122:        sprintf(b, "%d.%d.%d.%d", yp[3], yp[2], yp[1], yp[0]);
                    123:        return(b);
                    124: }
                    125: 
                    126: main()
                    127: {
                    128:        in_addr net, addr, onet;
                    129:        char line[128];
                    130:        char *name, *cp;
                    131: 
                    132:        onet = -1;
                    133:        while(gets(line)!=NULL) {
                    134:                /* comments */
                    135:                if (line[0]=='#'||line[0]=='\n' || line[0]==' ' || line[0]=='\t'){
                    136:                        puts(line);
                    137:                        continue;
                    138:                }
                    139: 
                    140:                /* get addr and net */
                    141:                for (name=line; *name&&*name!=' '&&*name!='\t'; name++)
                    142:                        ;
                    143:                if (*name=='\0')
                    144:                        continue;
                    145:                *name++ = '\0';
                    146:                if ((addr=in_aton(line))==0 || (net=in_netof(addr))==0) {
                    147:                        printf("%s      %s\n", line, name);
                    148:                        continue;
                    149:                }
                    150:                if (net!=onet) {
                    151:                        onet = net;
                    152:                        printf("%s      %s\n", in_ntoa(net), in_host(net));
                    153:                }
                    154: 
                    155:                /* get name */
                    156:                for (; *name==' '||*name=='\t'; name++)
                    157:                        ;
                    158:                if (*name=='\0')
                    159:                        continue;
                    160:                for (cp=name; *cp&&*cp!=' '&&*cp!='\t'; cp++)
                    161:                        ;
                    162:                if (*cp!='\0')
                    163:                        *cp++ = '\0';
                    164: 
                    165:                /* print out both addresses */
                    166:                printf("%s      %s/%s   %s %s\n", line, in_host(net),name,name,cp);
                    167:        }
                    168: }

unix.superglobalmegacorp.com

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