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