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