home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / netimp / if_imphost.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  6.4 KB  |  266 lines

  1. /*
  2.  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)if_imphost.c    7.10 (Berkeley) 6/28/90
  34.  */
  35.  
  36. #include "imp.h"
  37. #if NIMP > 0
  38. /*
  39.  * Host table manipulation routines.
  40.  * Only needed when shipping stuff through an IMP.
  41.  *
  42.  * Everything in here is called at splimp from
  43.  * from the IMP protocol code (if_imp.c), or
  44.  * interlocks with the code at splimp.
  45.  */
  46. #include "param.h"
  47. #include "mbuf.h"
  48. #include "socket.h"
  49. #include "syslog.h"
  50.  
  51. #include "../net/if.h"
  52.  
  53. #include "../netinet/in.h"
  54. #include "../netinet/in_systm.h"
  55.  
  56. #include "if_imp.h"
  57. #include "if_imphost.h"
  58.  
  59. extern struct imp_softc imp_softc[];
  60.  
  61. /*
  62.  * Given an internet address
  63.  * return a host structure (if it exists).
  64.  */
  65. struct host *
  66. hostlookup(imp, host, unit)
  67.     int imp, host, unit;
  68. {
  69.     register struct host *hp;
  70.     register struct mbuf *m;
  71.     register int hash = HOSTHASH(imp, host);
  72.  
  73.     for (m = imp_softc[unit].imp_hosts; m; m = m->m_next) {
  74.         hp = &mtod(m, struct hmbuf *)->hm_hosts[hash];
  75.             if (hp->h_imp == imp && hp->h_host == host) {
  76.             if ((hp->h_flags & HF_INUSE) == 0)
  77.                 mtod(dtom(hp), struct hmbuf *)->hm_count++;
  78.             hp->h_flags |= HF_INUSE;
  79.             return (hp);
  80.         }
  81.     }
  82.     return ((struct host *)0);
  83. }
  84.  
  85. /*
  86.  * Enter a reference to this host's internet
  87.  * address.  If no host structure exists, create
  88.  * one and hook it into the host database.
  89.  */
  90. struct host *
  91. hostenter(imp, host, unit)
  92.     int imp, host, unit;
  93. {
  94.     register struct mbuf *m, **mprev;
  95.     register struct host *hp, *hp0 = 0;
  96.     register int hash = HOSTHASH(imp, host);
  97.  
  98.     mprev = &imp_softc[unit].imp_hosts;
  99.     while (m = *mprev) {
  100.         mprev = &m->m_next;
  101.         hp = &mtod(m, struct hmbuf *)->hm_hosts[hash];
  102.             if (hp->h_imp == imp && hp->h_host == host) {
  103.             if ((hp->h_flags & HF_INUSE) == 0)
  104.                 mtod(dtom(hp), struct hmbuf *)->hm_count++;
  105.             goto foundhost;
  106.         }
  107.         if ((hp->h_flags & HF_INUSE) == 0) {
  108.             if (hp0 == 0)
  109.                 hp0 = hp;
  110.             continue;
  111.         }
  112.     }
  113.  
  114.     /*
  115.      * No current host structure, make one.
  116.      * If our search ran off the end of the
  117.      * chain of mbuf's, allocate another.
  118.      */
  119.     if (hp0 == 0) {
  120.         m = m_getclr(M_DONTWAIT, MT_HTABLE);
  121.         if (m == NULL)
  122.             return ((struct host *)0);
  123.         *mprev = m;
  124.         hp0 = &mtod(m, struct hmbuf *)->hm_hosts[hash];
  125.     }
  126.     hp = hp0;
  127.     mtod(dtom(hp), struct hmbuf *)->hm_count++;
  128.     hp->h_imp = imp;
  129.     hp->h_host = host;
  130.     hp->h_timer = 0;
  131.     hp->h_flags = 0;
  132.  
  133. foundhost:
  134.     hp->h_flags |= HF_INUSE;
  135.     return (hp);
  136. }
  137.  
  138. /*
  139.  * Reset a given imp unit's host entries.
  140.  * Must be called at splimp.
  141.  */
  142. hostreset(unit)
  143.     int unit;
  144. {
  145.     register struct mbuf *m;
  146.     register struct host *hp, *lp;
  147.     struct hmbuf *hm;
  148.  
  149.     for (m = imp_softc[unit].imp_hosts; m; m = m->m_next) {
  150.         hm = mtod(m, struct hmbuf *);
  151.         hp = hm->hm_hosts; 
  152.         lp = hp + HPMBUF;
  153.         while (hm->hm_count > 0 && hp < lp) {
  154.             hostrelease(hp);
  155.             hp++;
  156.         }
  157.     }
  158.     hostcompress(unit);
  159. }
  160.  
  161. /*
  162.  * Remove a host structure and release
  163.  * any resources it's accumulated.
  164.  */
  165. hostrelease(hp)
  166.     register struct host *hp;
  167. {
  168.  
  169.     if (hp->h_q)
  170.         hostflush(hp);
  171.     hp->h_rfnm = 0;
  172.     if (hp->h_flags & HF_INUSE)
  173.         --mtod(dtom(hp), struct hmbuf *)->hm_count;
  174.     hp->h_flags = 0;
  175. }
  176.  
  177. /*
  178.  * Flush the message queue for a host.
  179.  */
  180. hostflush(hp)
  181.     register struct host *hp;
  182. {
  183.     register struct mbuf *m;
  184.  
  185.     /*
  186.      * Discard any packets left on the waiting q
  187.      */
  188.     if (m = hp->h_q) {
  189.         register struct mbuf *n;
  190.  
  191.         do {
  192.             n = m->m_act;
  193.             m_freem(m);
  194.             m = n;
  195.         } while (m != hp->h_q);
  196.         hp->h_q = 0;
  197.         hp->h_qcnt = 0;
  198.     }
  199. }
  200.  
  201. /*
  202.  * Release mbufs in host table that contain no entries
  203.  * currently in use.  Must be called at splimp.
  204.  */
  205. hostcompress(unit)
  206.     int unit;
  207. {
  208.     register struct mbuf *m, **mprev;
  209.     struct imp_softc *sc = &imp_softc[unit];
  210.  
  211.     mprev = &sc->imp_hosts;
  212.     sc->imp_hostq = 0;
  213.     while (m = *mprev) {
  214.         if (mtod(m, struct hmbuf *)->hm_count == 0)
  215.             *mprev = m_free(m);
  216.         else
  217.             mprev = &m->m_next;
  218.     }
  219. }
  220.  
  221. /*
  222.  * Host data base timer routine.
  223.  * Decrement timers on structures which are
  224.  * waiting to be deallocated.  On expiration
  225.  * release resources, possibly deallocating
  226.  * mbuf associated with structure.
  227.  */
  228. hostslowtimo()
  229. {
  230.     register struct mbuf *m;
  231.     register struct host *hp, *lp;
  232.     struct imp_softc *sc;
  233.     struct hmbuf *hm;
  234.     int s = splimp(), unit, any;
  235.  
  236.     for (unit = 0; unit < NIMP; unit++) {
  237.         any = 0;
  238.         sc = &imp_softc[unit];
  239.         for (m = sc->imp_hosts; m; m = m->m_next) {
  240.         hm = mtod(m, struct hmbuf *);
  241.         hp = hm->hm_hosts; 
  242.         lp = hp + HPMBUF;
  243.         for (; hm->hm_count > 0 && hp < lp; hp++) {
  244.             if (hp->h_timer && --hp->h_timer == 0) {
  245.             if (hp->h_rfnm) {
  246.                 log(LOG_INFO,            /* XXX */
  247.                     "imp%d: host %d/imp %d, lost rfnm\n",
  248.                     unit, hp->h_host, ntohs(hp->h_imp));
  249.                 sc->imp_lostrfnm++;
  250.                 imprestarthost(sc, hp);
  251.             } else {
  252.                 any = 1;
  253.                 hostrelease(hp);
  254.                 if (sc->imp_hostq == m)
  255.                     sc->imp_hostq = 0;
  256.             }
  257.             }
  258.         }
  259.         }
  260.         if (any)
  261.         hostcompress(unit);
  262.     }
  263.     splx(s);
  264. }
  265. #endif
  266.