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

Defined functions

hostdeque defined in line 181; never used
hostenter defined in line 60; never used
hostfree defined in line 110; used 1 times
hostlookup defined in line 37; never used
hostrelease defined in line 149; used 2 times
hostreset defined in line 122; never used
hostslowtimo defined in line 203; never used

Defined variables

hosts defined in line 31; used 5 times
Last modified: 1990-04-11
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 4069
Valid CSS Valid XHTML 1.0 Strict