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