1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright (c) 1995 John Hay. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by John Hay.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY John Hay AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL John Hay OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: sap_tables.c,v 1.4 1997/07/01 00:33:41 bde Exp $
*/
#include "defs.h"
#include <string.h>
#include <stdlib.h>
#define FIXLEN(s) { if ((s)->sa_len == 0) (s)->sa_len = sizeof (*(s));}
sap_hash sap_head[SAPHASHSIZ];
void
sapinit(void)
{
int i;
for (i=0; i<SAPHASHSIZ; i++)
sap_head[i].forw = sap_head[i].back =
(struct sap_entry *)&sap_head[i];
}
/*
* This hash use the first 14 letters of the ServName and the ServType
* to create a 32 bit hash value.
*/
int
saphash(u_short ServType, char *ServName)
{
int hsh, i;
char name[SERVNAMELEN];
bzero(name, SERVNAMELEN);
strncpy(name, ServName, SERVNAMELEN);
ServName = name;
hsh = 0;
#define SMVAL 33
hsh = hsh * SMVAL + (ServType & 0xff);
hsh = hsh * SMVAL + (ServType >> 8);
for (i=0;i<14;i++) {
hsh = hsh * SMVAL + *ServName++;
ServName++;
}
#undef SMVAL
return hsh;
}
/*
* Look for an exact match on ServType and ServName. It is
* mostly used by the function that process SAP RESPONSE packets.
*
* A hash is created and used to index into the hash table. Then
* that list is walk through searching for a match.
*
* If no match is found NULL is returned.
*/
struct sap_entry *
sap_lookup(u_short ServType, char *ServName)
{
register struct sap_entry *sap;
register struct sap_hash *sh;
int hsh;
hsh = saphash(ServType, ServName);
sh = &sap_head[hsh & SAPHASHMASK];
for(sap = sh->forw; sap != (sap_entry *)sh; sap = sap->forw) {
if ((hsh == sap->hash) &&
(ServType == sap->sap.ServType) &&
(strncmp(ServName, sap->sap.ServName, SERVNAMELEN) == 0)) {
return sap;
}
}
return NULL;
}
/*
* This returns the nearest service of the specified type. If no
* suitable service is found or if that service is on the interface
* where the request came from, NULL is returned.
*
* When checking interfaces clones must be considered also.
*
* XXX TODO:
* Maybe we can use RIP tables to get the fastest service (ticks).
*/
struct sap_entry *
sap_nearestserver(ushort ServType, struct interface *ifp)
{
register struct sap_entry *sap;
register struct sap_entry *csap;
struct sap_hash *sh;
register struct sap_entry *best = NULL;
register int besthops = HOPCNT_INFINITY;
sh = sap_head;
for (; sh < &sap_head[SAPHASHSIZ]; sh++)
for(sap = sh->forw; sap != (sap_entry *)sh; sap = sap->forw) {
if (ServType != sap->sap.ServType)
continue;
if (ifp == sap->ifp)
continue;
csap = sap->clone;
while (csap) {
if (ifp == csap->ifp)
/*
* I would have loved to use
* something else here.
*/
goto next;
csap = csap->clone;
}
if (ntohs(sap->sap.hops) < besthops) {
best = sap;
besthops = ntohs(best->sap.hops);
}
next:;
}
return best;
}
/*
* Add a entry to the SAP table.
*
* If the malloc fail, the entry will silently be thrown away.
*/
void
sap_add(struct sap_info *si, struct sockaddr *from)
{
register struct sap_entry *nsap;
register struct sap_hash *sh;
if (ntohs(si->hops) == HOPCNT_INFINITY)
return;
FIXLEN(from);
nsap = malloc(sizeof(struct sap_entry));
if (nsap == NULL)
return;
nsap->sap = *si;
nsap->source = *from;
nsap->clone = NULL;
nsap->ifp = if_ifwithnet(from);
nsap->state = RTS_CHANGED;
nsap->timer = 0;
nsap->hash = saphash(si->ServType, si->ServName);
sh = &sap_head[nsap->hash & SAPHASHMASK];
insque(nsap, sh);
TRACE_SAP_ACTION("ADD", nsap);
}
/*
* Change an existing SAP entry. If a clone exist for the old one,
* check if it is cheaper. If it is change tothe clone, otherwise
* delete all the clones.
*/
void
sap_change(struct sap_entry *sap,
struct sap_info *si,
struct sockaddr *from)
{
struct sap_entry *osap = NULL;
FIXLEN(from);
TRACE_SAP_ACTION("CHANGE FROM", sap);
/*
* If the hopcount (metric) is HOPCNT_INFINITY (16) it means that
* a service has gone down. We should keep it like that for 30
* seconds, so that it will get broadcast and then change to a
* clone if one exist.
*/
if (sap->clone && (ntohs(si->hops) != HOPCNT_INFINITY)) {
/*
* There are three possibilities:
* 1. The new path is cheaper than the old one.
* Free all the clones.
*
* 2. The new path is the same cost as the old ones.
* If it is on the list of clones remove it
* from the clone list and free it.
*
* 3. The new path is more expensive than the old one.
* Use the values of the first clone and take it
* out of the list, to be freed at the end.
*/
osap = sap->clone;
if (ntohs(osap->sap.hops) > ntohs(si->hops)) {
struct sap_entry *nsap;
while (osap) {
nsap = osap->clone;
TRACE_SAP_ACTION("DELETE", osap);
free(osap);
osap = nsap;
}
sap->clone = NULL;
} else if (ntohs(osap->sap.hops) == ntohs(si->hops)) {
struct sap_entry *psap;
psap = sap;
while (osap) {
if (equal(&osap->source, from)) {
psap->clone = osap->clone;
TRACE_SAP_ACTION("DELETE", osap);
free(osap);
osap = psap->clone;
} else {
psap = osap;
osap = osap->clone;
}
}
} else {
from = &osap->source;
si = &osap->sap;
sap->clone = osap->clone;
}
}
sap->sap = *si;
sap->source = *from;
sap->ifp = if_ifwithnet(from);
sap->state = RTS_CHANGED;
if (ntohs(si->hops) == HOPCNT_INFINITY)
sap->timer = EXPIRE_TIME;
else
sap->timer = 0;
if (osap) {
TRACE_SAP_ACTION("DELETE", osap);
free(osap);
}
TRACE_SAP_ACTION("CHANGE TO", sap);
}
/*
* Add a clone to the specified SAP entry. A clone is a different
* route to the same service. We must know about them when we use
* the split horizon algorithm.
*
* If the malloc fail, the entry will silently be thrown away.
*/
void
sap_add_clone(struct sap_entry *sap,
struct sap_info *clone,
struct sockaddr *from)
{
register struct sap_entry *nsap;
register struct sap_entry *csap;
if (ntohs(clone->hops) == HOPCNT_INFINITY)
return;
FIXLEN(from);
nsap = malloc(sizeof(struct sap_entry));
if (nsap == NULL)
return;
if (ftrace)
fprintf(ftrace, "CLONE ADD %04.4X %s.\n",
ntohs(clone->ServType),
clone->ServName);
nsap->sap = *clone;
nsap->source = *from;
nsap->clone = NULL;
nsap->ifp = if_ifwithnet(from);
nsap->state = RTS_CHANGED;
nsap->timer = 0;
nsap->hash = saphash(clone->ServType, clone->ServName);
csap = sap;
while (csap->clone)
csap = csap->clone;
csap->clone = nsap;
TRACE_SAP_ACTION("ADD CLONE", nsap);
}
/*
* Remove a SAP entry from the table and free the memory
* used by it.
*
* If the service have clone, do a sap_change to it and free
* the clone.
*/
void
sap_delete(struct sap_entry *sap)
{
if (sap->clone) {
sap_change(sap, &sap->clone->sap, &sap->clone->source);
return;
}
remque(sap);
TRACE_SAP_ACTION("DELETE", sap);
free(sap);
}
|