Annotation of cci/sys/netimp/if_imphost.c, revision 1.1.1.1

1.1       root        1: /*     if_imphost.c    6.1     83/07/29        */
                      2: 
                      3: #include "imp.h"
                      4: #if NIMP > 0
                      5: /*
                      6:  * Host table manipulation routines.
                      7:  * Only needed when shipping stuff through an IMP.
                      8:  *
                      9:  * Everything in here is called at splimp from
                     10:  * from the IMP protocol code (if_imp.c), or
                     11:  * interlocks with the code at splimp.
                     12:  */
                     13: #include "../h/param.h"
                     14: #include "../h/mbuf.h"
                     15: 
                     16: #include "../netinet/in.h"
                     17: #include "../netinet/in_systm.h"
                     18: 
                     19: #include "../netimp/if_imp.h"
                     20: #include "../netimp/if_imphost.h"
                     21: 
                     22: /*
                     23:  * Head of host table hash chains.
                     24:  */
                     25: struct mbuf *hosts;
                     26: 
                     27: /*
                     28:  * Given an internet address
                     29:  * return a host structure (if it exists).
                     30:  */
                     31: struct host *
                     32: hostlookup(addr)
                     33:        struct in_addr addr;
                     34: {
                     35:        register struct host *hp;
                     36:        register struct mbuf *m;
                     37:        register int hash = HOSTHASH(addr);
                     38: 
                     39:        for (m = hosts; m; m = m->m_next) {
                     40:                hp = &mtod(m, struct hmbuf *)->hm_hosts[hash];
                     41:                if (hp->h_addr.s_addr == addr.s_addr) {
                     42:                        hp->h_flags |= HF_INUSE;
                     43:                        return (hp);
                     44:                }
                     45:        }
                     46:        return ((struct host *)0);
                     47: }
                     48: 
                     49: /*
                     50:  * Enter a reference to this host's internet
                     51:  * address.  If no host structure exists, create
                     52:  * one and hook it into the host database.
                     53:  */
                     54: struct host *
                     55: hostenter(addr)                 
                     56:        struct in_addr addr;
                     57: {
                     58:        register struct mbuf *m, **mprev;
                     59:        register struct host *hp, *hp0 = 0;
                     60:        register int hash = HOSTHASH(addr);
                     61: 
                     62:        mprev = &hosts;
                     63:        while (m = *mprev) {
                     64:                mprev = &m->m_next;
                     65:                hp = &mtod(m, struct hmbuf *)->hm_hosts[hash];
                     66:                if ((hp->h_flags & HF_INUSE) == 0) {
                     67:                        if (hp->h_addr.s_addr == addr.s_addr)
                     68:                                goto foundhost;
                     69:                        if (hp0 == 0)
                     70:                                hp0 = hp;
                     71:                        continue;
                     72:                }
                     73:                if (hp->h_addr.s_addr == addr.s_addr)    
                     74:                        goto foundhost;
                     75:        }
                     76: 
                     77:        /*
                     78:         * No current host structure, make one.
                     79:         * If our search ran off the end of the
                     80:         * chain of mbuf's, allocate another.
                     81:         */
                     82:        if (hp0 == 0) {
                     83:                m = m_getclr(M_DONTWAIT, MT_HTABLE);
                     84:                MBUFNULL(44, m);                /* for debugging */
                     85:                if (m == NULL)
                     86:                        return ((struct host *)0);
                     87:                *mprev = m;
                     88:                hp0 = &mtod(m, struct hmbuf *)->hm_hosts[hash];
                     89:        }
                     90:        mtod(dtom(hp0), struct hmbuf *)->hm_count++;
                     91:        hp = hp0;
                     92:        hp->h_addr = addr;
                     93:        hp->h_timer = 0;
                     94:        hp->h_flags = 0;
                     95: 
                     96: foundhost:
                     97:        hp->h_flags |= HF_INUSE;
                     98:        return (hp);
                     99: }
                    100: 
                    101: /*
                    102:  * Mark a host structure free and set it's
                    103:  * timer going.
                    104:  */
                    105: hostfree(hp)                               
                    106:        register struct host *hp;
                    107: {
                    108: 
                    109:        hp->h_flags &= ~HF_INUSE;
                    110:        hp->h_timer = HOSTTIMER;
                    111:        hp->h_rfnm = 0;
                    112: }
                    113: 
                    114: /*
                    115:  * Reset a given network's host entries.
                    116:  */
                    117: hostreset(net)     
                    118:        int net;
                    119: {
                    120:        register struct mbuf *m;
                    121:        register struct host *hp, *lp;
                    122:        struct hmbuf *hm;
                    123: 
                    124:        for (m = hosts; m; m = m->m_next) {
                    125:                hm = mtod(m, struct hmbuf *);
                    126:                hp = hm->hm_hosts; 
                    127:                lp = hp + HPMBUF;
                    128:                while (hm->hm_count > 0 && hp < lp) {
                    129:                        if (hp->h_addr.s_net == net) {
                    130:                                hp->h_flags &= ~HF_INUSE;
                    131:                                hostrelease(hp);
                    132:                        }
                    133:                        hp++;
                    134:                }
                    135:        }
                    136: }
                    137: 
                    138: /*
                    139:  * Remove a host structure and release
                    140:  * any resources it's accumulated.
                    141:  */
                    142: hostrelease(hp)
                    143:        register struct host *hp;
                    144: {
                    145:        register struct mbuf *m, **mprev, *mh = dtom(hp);
                    146: 
                    147:        /*
                    148:         * Discard any packets left on the waiting q
                    149:         */
                    150:        if (m = hp->h_q) {
                    151:                register struct mbuf *n;
                    152: 
                    153:                do {
                    154:                        n = m->m_act;
                    155:                        m_freem(m);
                    156:                        m = n;
                    157:                } while (m != hp->h_q);
                    158:                hp->h_q = 0;
                    159:        }
                    160:        hp->h_flags = 0;
                    161:        if (--mtod(mh, struct hmbuf *)->hm_count)
                    162:                return;
                    163:        mprev = &hosts;
                    164:        while ((m = *mprev) != mh)
                    165:                mprev = &m->m_next;
                    166:        *mprev = m_free(mh);
                    167: }
                    168: 
                    169: /*
                    170:  * Remove a packet from the holding q.
                    171:  * The RFNM counter is also bumped.
                    172:  */
                    173: struct mbuf *
                    174: hostdeque(hp)
                    175:        register struct host *hp;
                    176: {
                    177:        register struct mbuf *m;
                    178: 
                    179:        hp->h_rfnm--;
                    180:        HOST_DEQUE(hp, m);
                    181:        if (m)
                    182:                return (m);
                    183:        if (hp->h_rfnm == 0)
                    184:                hostfree(hp);
                    185:        return (0);
                    186: }
                    187: 
                    188: /*
                    189:  * Host data base timer routine.
                    190:  * Decrement timers on structures which are
                    191:  * waiting to be deallocated.  On expiration
                    192:  * release resources, possibly deallocating
                    193:  * mbuf associated with structure.
                    194:  */
                    195: hostslowtimo()
                    196: {
                    197:        register struct mbuf *m;
                    198:        register struct host *hp, *lp;
                    199:        struct hmbuf *hm;
                    200:        int s = splimp();
                    201: 
                    202:        for (m = hosts; m; m = m->m_next) {
                    203:                hm = mtod(m, struct hmbuf *);
                    204:                hp = hm->hm_hosts; 
                    205:                lp = hp + HPMBUF;
                    206:                for (; hm->hm_count > 0 && hp < lp; hp++) {
                    207:                        if (hp->h_flags & HF_INUSE)
                    208:                                continue;
                    209:                        if (hp->h_timer && --hp->h_timer == 0)
                    210:                                hostrelease(hp);
                    211:                }
                    212:        }
                    213:        splx(s);
                    214: }
                    215: #endif

unix.superglobalmegacorp.com

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