Annotation of researchv9/ipc/src/libin/in_host.c, revision 1.1.1.1

1.1       root        1: #include <sys/types.h>
                      2: #include <sys/stat.h>
                      3: #include <sys/inet/in.h>
                      4: #include <stdio.h>
                      5: #include <ctype.h>
                      6: #include "config.h"
                      7: 
                      8: struct namefile {
                      9:        char *name;
                     10:        char *data;
                     11:        long time;
                     12: };
                     13: 
                     14: static struct namefile files[] = {
                     15:        { "/usr/ipc/lib/inaddr.local", 0, 0 },
                     16:        { "/usr/ipc/lib/inaddr", 0, 0 },
                     17:        { "/usr/inet/lib/hosts", 0, 0 },
                     18:        { "/usr/inet/lib/networks", 0, 0 },
                     19: };
                     20: #define NFIL (sizeof(files)/sizeof(struct namefile))
                     21: 
                     22: struct trbuf {
                     23:        struct trbuf *next;
                     24:        in_addr addr;
                     25:        char *name;
                     26: };
                     27: 
                     28: #define TLBSIZE 1023
                     29: static struct trbuf tlb[TLBSIZE];
                     30: 
                     31: /* imported */
                     32: extern char *in_getw();
                     33: extern char *malloc();
                     34: 
                     35: /* exported */
                     36: int in_host_dostat=1;
                     37: 
                     38: static int
                     39: getfile(i)
                     40:        int i;
                     41: {
                     42:        int fd;
                     43:        struct stat sbuf;
                     44:        unsigned int x;
                     45: 
                     46:        if (in_host_dostat)
                     47:                stat(files[i].name, &sbuf);
                     48:        else
                     49:                sbuf.st_mtime = files[i].time;
                     50:        if (files[i].data == 0 || sbuf.st_mtime!=files[i].time) {
                     51:                if (!in_host_dostat)
                     52:                        stat(files[i].name, &sbuf);
                     53:                fd = open(files[i].name, 0);
                     54:                if (fd<0)
                     55:                        return -1;
                     56:                if (files[i].data!=0)
                     57:                        free(files[i].data);
                     58:                x = sbuf.st_size;
                     59:                files[i].data = malloc(x+2);
                     60:                if (files[i].data==0) {
                     61:                        close(fd);
                     62:                        return -1;
                     63:                }
                     64:                if (read(fd, files[i].data, x)!=x){
                     65:                        close(fd);
                     66:                        return -1;
                     67:                }
                     68:                close(fd);
                     69:                files[i].data[sbuf.st_size] = '\n';
                     70:                files[i].data[sbuf.st_size+1] = 0;
                     71:                files[i].time = sbuf.st_mtime;
                     72:                for (i=0; i<TLBSIZE; i++)
                     73:                        tlb[i].addr = 0;
                     74:        }
                     75:        return 0;
                     76: }
                     77: 
                     78: #define SKIPWHITE while(*p==' ' || *p=='\t') p++
                     79: #define GETBLACK       xp = b;\
                     80:                        while(*p!=' ' && *p!='\t' && *p!='\n' && *p!='\0')\
                     81:                                *xp++ = *p++;\
                     82:                        *xp = '\0'
                     83: 
                     84: char *
                     85: in_host(addr)
                     86: in_addr addr;
                     87: {
                     88:        register char *p, *xp, *op;
                     89:        unsigned char *yp;
                     90:        static char b[32];
                     91:        int f;
                     92:        in_addr x, in_netof(), in_aton();
                     93: 
                     94:        if (addr==0)
                     95:                return "*";
                     96:        for(f=0; f<NFIL; f++){
                     97:                if (getfile(f) < 0) {
                     98:                        continue;
                     99:                } else if (addr == tlb[addr%TLBSIZE].addr) {
                    100:                        p = tlb[addr%TLBSIZE].name;
                    101:                        GETBLACK;
                    102:                        return(b);
                    103:                } else for (p = files[f].data; *p; p++){
                    104:                        /* get number */
                    105:                        SKIPWHITE;
                    106:                        GETBLACK;
                    107:                        x = in_aton(b);
                    108:                        SKIPWHITE;
                    109:                        if (x != 0 && *p != '\n') {
                    110:                                /* get name */
                    111:                                op = p;
                    112:                                GETBLACK;
                    113:        
                    114:                                /* see if this is the one */
                    115:                                if (x==addr){
                    116:                                        int i;
                    117: 
                    118:                                        i = x%TLBSIZE;
                    119:                                        tlb[i].addr = x;
                    120:                                        tlb[i].name = op;
                    121:                                        return b;
                    122:                                }
                    123:                        }
                    124:                        /* drop rest of line */
                    125:                        while(*p!='\n'&&*p!='\0') p++;
                    126:                }
                    127:        }
                    128:        yp = (unsigned char *) &addr;
                    129:        sprintf(b, "%d.%d.%d.%d", yp[3], yp[2], yp[1], yp[0]);
                    130:        return(b);
                    131: }

unix.superglobalmegacorp.com

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