1*5868Sroot /* if_imphost.c 4.4 82/02/16 */ 25712Ssam 35712Ssam #include "imp.h" 45712Ssam #if NIMP > 0 55712Ssam /* 65712Ssam * Host table manipulation routines. 75712Ssam * Only needed when shipping stuff through an IMP. 85712Ssam */ 95712Ssam 105712Ssam #include "../h/param.h" 115712Ssam #include "../h/mbuf.h" 125712Ssam #include "../net/in.h" 135712Ssam #include "../net/in_systm.h" 145712Ssam #include "../net/if_imp.h" 155860Sroot #include "../net/if_imphost.h" 165712Ssam 175712Ssam /* 185712Ssam * Head of host table hash chains. 195712Ssam */ 205860Sroot struct mbuf *hosts; 215712Ssam 225712Ssam /* 235712Ssam * Given an internet address 245712Ssam * return a host structure (if it exists). 255712Ssam */ 265712Ssam struct host * 275771Swnj hostlookup(addr) 285712Ssam struct in_addr addr; 295712Ssam { 305712Ssam register struct host *hp; 315712Ssam register struct mbuf *m; 325712Ssam register int hash = HOSTHASH(addr); 335712Ssam 345771Swnj COUNT(HOSTLOOKUP); 355860Sroot for (m = hosts; m; m = m->m_next) { 365712Ssam hp = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 375712Ssam if (hp->h_refcnt == 0) 385860Sroot continue; 395712Ssam if (hp->h_addr.s_addr == addr.s_addr) 405712Ssam return (hp); 415712Ssam } 425712Ssam return (0); 435712Ssam } 445712Ssam 455712Ssam /* 465712Ssam * Enter a reference to this host's internet 475712Ssam * address. If no host structure exists, create 485712Ssam * one and hook it into the host database. 495712Ssam */ 505712Ssam struct host * 515771Swnj hostenter(addr) 525712Ssam struct in_addr addr; 535712Ssam { 545860Sroot register struct mbuf *m, **mprev; 555860Sroot register struct host *hp, *hp0 = 0; 565712Ssam register int hash = HOSTHASH(addr); 575712Ssam 585771Swnj COUNT(HOSTENTER); 595860Sroot mprev = &hosts; 605860Sroot while (m = *mprev) { 615712Ssam hp = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 625860Sroot if (hp->h_refcnt == 0) { 635860Sroot if (hp0 == 0) 645860Sroot hp0 = hp; 655860Sroot continue; 665860Sroot } 675712Ssam if (hp->h_addr.s_addr == addr.s_addr) 685712Ssam goto foundhost; 695860Sroot mprev = &m->m_next; 705712Ssam } 715712Ssam 725712Ssam /* 735712Ssam * No current host structure, make one. 745712Ssam * If our search ran off the end of the 755712Ssam * chain of mbuf's, allocate another. 765712Ssam */ 775860Sroot if (hp0 == 0) { 785712Ssam m = m_getclr(M_DONTWAIT); 795712Ssam if (m == 0) 805712Ssam return (0); 815860Sroot *mprev = m; 825860Sroot m->m_off = MMINOFF; 835860Sroot hp0 = &mtod(m, struct hmbuf *)->hm_hosts[hash]; 845712Ssam } 855860Sroot mtod(dtom(hp0), struct hmbuf *)->hm_count++; 865860Sroot hp = hp0; 875712Ssam hp->h_addr = addr; 885712Ssam hp->h_status = HOSTS_UP; 895712Ssam 905712Ssam foundhost: 915712Ssam hp->h_refcnt++; /* know new structures have 0 val */ 925712Ssam return (hp); 935712Ssam } 945712Ssam 955712Ssam /* 965712Ssam * Free a reference to a host. If this causes the 975712Ssam * host structure to be released do so. 985712Ssam */ 995860Sroot hostfree(hp) 1005860Sroot register struct host *hp; 1015712Ssam { 1025712Ssam register struct mbuf *m; 1035712Ssam 1045771Swnj COUNT(HOSTFREE); 1055860Sroot if (--hp->h_refcnt) 1065860Sroot return; 1075860Sroot hostrelease(hp); 1085712Ssam } 1095712Ssam 1105712Ssam /* 1115712Ssam * Reset a given network's host entries. 1125712Ssam * This involves clearing all packet queue's 1135712Ssam * and releasing host structures. 1145712Ssam */ 1155771Swnj hostreset(net) 1165712Ssam int net; 1175712Ssam { 1185712Ssam register struct mbuf *m; 1195712Ssam register struct host *hp, *lp; 1205860Sroot struct hmbuf *hm; 1215860Sroot int x; 1225712Ssam 1235771Swnj COUNT(HOSTRESET); 1245860Sroot x = splimp(); 1255860Sroot for (m = hosts; m; m = m->m_next) { 1265860Sroot hm = mtod(m, struct hmbuf *); 1275860Sroot hp = hm->hm_hosts; 1285712Ssam lp = hp + HPMBUF; 1295860Sroot while (hm->hm_count != 0 && hp < lp) { 1305712Ssam if (hp->h_addr.s_net == net) 1315771Swnj hostrelease(mtod(m, struct hmbuf *), hp); 1325712Ssam hp++; 1335712Ssam } 1345712Ssam } 1355860Sroot splx(x); 1365712Ssam } 1375712Ssam 1385712Ssam /* 1395712Ssam * Remove a host structure and release 1405712Ssam * any resources it's accumulated. 1415712Ssam */ 1425860Sroot hostrelease(hp) 1435712Ssam register struct host *hp; 1445712Ssam { 1455860Sroot register struct mbuf *m, **mprev, *mh = dtom(hp); 1465712Ssam 1475771Swnj COUNT(HOSTRELEASE); 1485712Ssam /* 1495712Ssam * Discard any packets left on the waiting q 1505712Ssam */ 1515771Swnj if (m = hp->h_q) { 152*5868Sroot register struct mbuf *n; 153*5868Sroot 154*5868Sroot do { 155*5868Sroot n = m->m_act; 156*5868Sroot m_freem(m); 157*5868Sroot m = n; 158*5868Sroot } while (m != hp->h_q); 1595771Swnj hp->h_q = 0; 1605712Ssam } 1615860Sroot if (--mtod(mh, struct hmbuf *)->hm_count) 1625712Ssam return; 1635860Sroot mprev = &hosts; 1645860Sroot while ((m = *mprev) != mh) 1655860Sroot mprev = &m->m_next; 1665860Sroot *mprev = mh->m_next; 1675860Sroot (void) m_free(mh); 1685712Ssam } 169