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