Annotation of 3BSD/cmd/ucbmail/hash.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Hashing routines to salt away machines seen scanning
                      3:  * networks paths that we don't know about.
                      4:  */
                      5: 
                      6: #define        XHSIZE          19              /* Size of extra hash table */
                      7: #define        NXMID           (XHSIZE*3/4)    /* Max extra machines */
                      8: 
                      9: struct xtrahash {
                     10:        char    *xh_name;               /* Name of machine */
                     11:        short   xh_mid;                 /* Machine ID */
                     12:        short   xh_attnet;              /* Attached networks */
                     13: } xtrahash[XHSIZE];
                     14: 
                     15: struct xtrahash        *xtab[XHSIZE];          /* F: mid-->machine name */
                     16: 
                     17: short  midfree;                        /* Next free machine id */
                     18: 
                     19: /*
                     20:  * Initialize the extra host hash table.
                     21:  * Called by sreset.
                     22:  */
                     23: 
                     24: minit()
                     25: {
                     26:        register struct xtrahash *xp, **tp;
                     27:        register int i;
                     28: 
                     29:        midfree = 0;
                     30:        tp = &xtab[0];
                     31:        for (xp = &xtrahash[0]; xp < &xtrahash[XHSIZE]; xp++) {
                     32:                xp->xh_name = NOSTR;
                     33:                xp->xh_mid = 0;
                     34:                xp->xh_attnet = 0;
                     35:                *tp++ = (struct xtrahash *) 0;
                     36:        }
                     37: }
                     38: 
                     39: /*
                     40:  * Stash a net name in the extra host hash table.
                     41:  * If a new entry is put in the hash table, deduce what
                     42:  * net the machine is attached to from the net character.
                     43:  *
                     44:  * If the machine is already known, add the given attached
                     45:  * net to those already known.
                     46:  */
                     47: 
                     48: mstash(name, attnet)
                     49:        char name[];
                     50: {
                     51:        register struct xtrahash *xp;
                     52:        struct xtrahash *xlocate();
                     53: 
                     54:        xp = xlocate(name);
                     55:        if (xp == (struct xtrahash *) 0) {
                     56:                printf("Ran out of machine id spots\n");
                     57:                return(0);
                     58:        }
                     59:        if (xp->xh_name == NOSTR) {
                     60:                if (midfree >= XHSIZE) {
                     61:                        printf("Out of machine ids\n");
                     62:                        return(0);
                     63:                }
                     64:                xtab[midfree] = xp;
                     65:                xp->xh_name = savestr(name);
                     66:                xp->xh_mid = 0200 + midfree++;
                     67:        }
                     68:        switch (attnet) {
                     69:        case '!':
                     70:        case '^':
                     71:                xp->xh_attnet |= BN;
                     72:                break;
                     73: 
                     74:        default:
                     75:        case ':':
                     76:                xp->xh_attnet |= SN;
                     77:                break;
                     78: 
                     79:        case '@':
                     80:                xp->xh_attnet |= AN;
                     81:                break;
                     82:        }
                     83:        return(xp->xh_mid);
                     84: }
                     85: 
                     86: /*
                     87:  * Search for the given name in the hash table
                     88:  * and return the pointer to it if found, or to the first
                     89:  * empty slot if not found.
                     90:  *
                     91:  * If no free slots can be found, return 0.
                     92:  */
                     93: 
                     94: struct xtrahash *
                     95: xlocate(name)
                     96:        char name[];
                     97: {
                     98:        register int h, q, i;
                     99:        register char *cp;
                    100:        register struct xtrahash *xp;
                    101: 
                    102:        for (h = 0, cp = name; *cp; h = (h << 2) + *cp++)
                    103:                ;
                    104:        if (h < 0 && (h = -h) < 0)
                    105:                h = 0;
                    106:        h = h % XHSIZE;
                    107:        cp = name;
                    108:        for (i = 0, q = 0; q < XHSIZE; i++, q = i * i) {
                    109:                xp = &xtrahash[(h + q) % XHSIZE];
                    110:                if (xp->xh_name == NOSTR)
                    111:                        return(xp);
                    112:                if (strcmp(cp, xp->xh_name) == 0)
                    113:                        return(xp);
                    114:                if (h - q < 0)
                    115:                        q += XHSIZE;
                    116:                xp = &xtrahash[(h - q) % XHSIZE];
                    117:                if (xp->xh_name == NOSTR)
                    118:                        return(xp);
                    119:                if (strcmp(cp, xp->xh_name) == 0)
                    120:                        return(xp);
                    121:        }
                    122:        return((struct xtrahash *) 0);
                    123: }
                    124: 
                    125: /*
                    126:  * Return the name from the extra host hash table corresponding
                    127:  * to the passed machine id.
                    128:  */
                    129: 
                    130: char *
                    131: mlook(mid)
                    132: {
                    133:        register int m;
                    134: 
                    135:        if ((mid & 0200) == 0)
                    136:                return(NOSTR);
                    137:        m = mid & ~0200;
                    138:        if (m >= midfree) {
                    139:                printf("Use made of undefined machine id\n");
                    140:                return(NOSTR);
                    141:        }
                    142:        return(xtrahash[xtab[m]].xh_name);
                    143: }

unix.superglobalmegacorp.com

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