xref: /csrg-svn/sys/netiso/tuba_table.c (revision 56483)
1 /*
2  * Copyright (c) 1980, 1986, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tuba_table.c	7.1 (Berkeley) 10/09/92
8  */
9 #include "param.h"
10 #include "systm.h"
11 #include "proc.h"
12 #include "mbuf.h"
13 #include "socket.h"
14 #include "socketvar.h"
15 #include "domain.h"
16 #include "protosw.h"
17 #include "ioctl.h"
18 
19 #include "net/if.h"
20 #include "net/af.h"
21 #include "net/radix.h"
22 
23 #include "tuba_addr.h"
24 
25 #include "netiso/iso.h"
26 
27 #define	SA(p) ((struct sockaddr *)(p))
28 
29 int	tuba_table_size;
30 struct	tuba_cache **tuba_table;
31 struct	radix_node_head *tuba_tree;
32 extern	int arpt_keep, arpt_prune;	/* use same values as arp cache */
33 
34 void
35 tuba_timer()
36 {
37 	int s = splnet();
38 	int	i;
39 	register struct	tuba_cache **tc;
40 	long	timelimit = time.tv_sec - arpt_keep;
41 
42 	timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
43 	for (i = tuba_table_size; i > 0; i--)
44 		if ((tc = tuba_table[i]) && (tc->tc_refcnt == 0) &&
45 		    (tc->tc_time < timelimit)) {
46 			tuba_table[i] = 0;
47 			rn_delete((caddr_t)&tc->tc_addr, (caddr_t)0, tuba_tree);
48 			free((caddr_t)tc, M_RTABLE);
49 		}
50 	splx(s);
51 }
52 
53 tuba_init()
54 {
55 	rn_inithead((void **)&tuba_tree, 40);
56 	timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
57 }
58 
59 int
60 tuba_lookup(isoa, flags, wait)
61 	register struct iso_addr *isoa;
62 	int flags;
63 {
64 	struct radix_node *rn;
65 	register struct tuba_cache *tc;
66 	int dupentry = 0, sum_even = 0, sum_odd = 0, delta, i;
67 
68 	if (rn = rn_match(tuba_tree, (caddr_t)isoa)) {
69 		tc = (struct tuba_cache *)rn;
70 		tc->tc_time = time.tv_sec;
71 		return (tc->tc_index);
72 	}
73 	if ((tc = (struct tuba_cache *)malloc(sizeof(*tc), M_RTABLE, wait))
74 		== NULL)
75 		return (0);
76 	bzero((caddr_t)tc, sizeof (*tc))
77 	bcopy((caddr_t)isoa, (caddr_t)&tc->tc_addr, 1 + isoa->isoa_len);
78 	rn_insert((caddr_t)&tc->tc_addr, tuba_tree, &dupentry, tc->tc_nodes);
79 	if (dupentry)
80 		panic("tuba_lookup 1");
81 	tc->tc_time = time.tv_sec;
82 	tc->tc_flags = flags;
83 	sum_even = isoa->isoa_len;
84 	for (i = sum_even; --i >= 0; ) {
85 		delta = isoa->isoa_genaddr[i];
86 		i & 1 ? sum_even += delta : sum_odd += delta;
87 	}
88 	ICKSUM(tc->tc_sum_in, (sum_even << 8) + sum_odd);
89 	ICKSUM(tc->tc_sum_out, tc->sum_in + 0x1fffe - tc->tc_index);
90 	for (i = tuba_table_size; i > 0; i--)
91 		if (tuba_table[i] == 0)
92 			break;
93 	if (i) {
94 		tc->tc_index = 1;
95 		tuba_table[i] = tc;
96 		return (i);
97 	}
98 	if (tuba_table_size == 0)
99 		tuba_table_size = 15;
100 	if (tuba_table_size > 0x7fff)
101 		return (0);
102 	tuba_table_size = 1 + 2 * tuba_table_size;
103 	i = (tuba_table_size + 1) * sizeof(tc);
104 	new = (struct tuba_cache **)malloc((unsigned)i, M_RTABLE, wait);
105 	if (new == 0) {
106 		tuba_table_size = old_size;
107 		rn_delete((caddr_t)&tc->tc_addr, (caddr_t)0, tuba_tree);
108 		free((caddr_t)tc, M_RTABLE);
109 		return (0);
110 	}
111 	bzero((caddr_t)new, (unsigned)i);
112 	if (tuba_table)
113 		bcopy((caddr_t)tuba_table, (caddr_t)new, i >> 1);
114 	tuba_table[tc->tc_index = tuba_table_size] = tc;
115 	return (tc->tc_index);
116 }
117