table.c revision 1.4 1 /* $NetBSD: table.c,v 1.4 1997/06/29 19:13:04 christos Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: table.c,v 1.4 1997/06/29 19:13:04 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 /*
46 * Routines to handle insertion, deletion, etc on the table
47 * of requests kept by the daemon. Nothing fancy here, linear
48 * search on a double-linked list. A time is kept with each
49 * entry so that overly old invitations can be eliminated.
50 *
51 * Consider this a mis-guided attempt at modularity
52 */
53 #include <sys/param.h>
54 #include <sys/time.h>
55 #include <sys/socket.h>
56 #include <protocols/talkd.h>
57 #include <syslog.h>
58 #include <unistd.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include "extern.h"
63
64 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
65
66 #define NIL ((TABLE_ENTRY *)0)
67
68 extern int debug;
69 struct timeval tp;
70 struct timezone txp;
71
72 typedef struct table_entry TABLE_ENTRY;
73
74 struct table_entry {
75 CTL_MSG request;
76 long time;
77 TABLE_ENTRY *next;
78 TABLE_ENTRY *last;
79 };
80
81 TABLE_ENTRY *table = NIL;
82
83 static void delete __P((TABLE_ENTRY *));
84
85 /*
86 * Look in the table for an invitation that matches the current
87 * request looking for an invitation
88 */
89 CTL_MSG *
90 find_match(request)
91 CTL_MSG *request;
92 {
93 TABLE_ENTRY *ptr;
94 time_t current_time;
95
96 gettimeofday(&tp, &txp);
97 current_time = tp.tv_sec;
98 if (debug)
99 print_request("find_match", request);
100 for (ptr = table; ptr != NIL; ptr = ptr->next) {
101 if ((ptr->time - current_time) > MAX_LIFE) {
102 /* the entry is too old */
103 if (debug)
104 print_request("deleting expired entry",
105 &ptr->request);
106 delete(ptr);
107 continue;
108 }
109 if (debug)
110 print_request("", &ptr->request);
111 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
112 strcmp(request->r_name, ptr->request.l_name) == 0 &&
113 ptr->request.type == LEAVE_INVITE)
114 return (&ptr->request);
115 }
116 return ((CTL_MSG *)0);
117 }
118
119 /*
120 * Look for an identical request, as opposed to a complimentary
121 * one as find_match does
122 */
123 CTL_MSG *
124 find_request(request)
125 CTL_MSG *request;
126 {
127 TABLE_ENTRY *ptr;
128 time_t current_time;
129
130 gettimeofday(&tp, &txp);
131 current_time = tp.tv_sec;
132 /*
133 * See if this is a repeated message, and check for
134 * out of date entries in the table while we are it.
135 */
136 if (debug)
137 print_request("find_request", request);
138 for (ptr = table; ptr != NIL; ptr = ptr->next) {
139 if ((ptr->time - current_time) > MAX_LIFE) {
140 /* the entry is too old */
141 if (debug)
142 print_request("deleting expired entry",
143 &ptr->request);
144 delete(ptr);
145 continue;
146 }
147 if (debug)
148 print_request("", &ptr->request);
149 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
150 strcmp(request->l_name, ptr->request.l_name) == 0 &&
151 request->type == ptr->request.type &&
152 request->pid == ptr->request.pid) {
153 /* update the time if we 'touch' it */
154 ptr->time = current_time;
155 return (&ptr->request);
156 }
157 }
158 return ((CTL_MSG *)0);
159 }
160
161 void
162 insert_table(request, response)
163 CTL_MSG *request;
164 CTL_RESPONSE *response;
165 {
166 TABLE_ENTRY *ptr;
167 time_t current_time;
168
169 gettimeofday(&tp, &txp);
170 current_time = tp.tv_sec;
171 request->id_num = new_id();
172 response->id_num = htonl(request->id_num);
173 /* insert a new entry into the top of the list */
174 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
175 if (ptr == NIL) {
176 syslog(LOG_ERR, "insert_table: Out of memory");
177 _exit(1);
178 }
179 ptr->time = current_time;
180 ptr->request = *request;
181 ptr->next = table;
182 if (ptr->next != NIL)
183 ptr->next->last = ptr;
184 ptr->last = NIL;
185 table = ptr;
186 }
187
188 /*
189 * Generate a unique non-zero sequence number
190 */
191 int
192 new_id()
193 {
194 static int current_id = 0;
195
196 current_id = (current_id + 1) % MAX_ID;
197 /* 0 is reserved, helps to pick up bugs */
198 if (current_id == 0)
199 current_id = 1;
200 return (current_id);
201 }
202
203 /*
204 * Delete the invitation with id 'id_num'
205 */
206 int
207 delete_invite(id_num)
208 int id_num;
209 {
210 TABLE_ENTRY *ptr;
211
212 ptr = table;
213 if (debug)
214 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
215 for (ptr = table; ptr != NIL; ptr = ptr->next) {
216 if (ptr->request.id_num == id_num)
217 break;
218 if (debug)
219 print_request("", &ptr->request);
220 }
221 if (ptr != NIL) {
222 delete(ptr);
223 return (SUCCESS);
224 }
225 return (NOT_HERE);
226 }
227
228 /*
229 * Classic delete from a double-linked list
230 */
231 static void
232 delete(ptr)
233 TABLE_ENTRY *ptr;
234 {
235
236 if (debug)
237 print_request("delete", &ptr->request);
238 if (table == ptr)
239 table = ptr->next;
240 else if (ptr->last != NIL)
241 ptr->last->next = ptr->next;
242 if (ptr->next != NIL)
243 ptr->next->last = ptr->last;
244 free((char *)ptr);
245 }
246