table.c revision 1.3 1 1.3 christos /* $NetBSD: table.c,v 1.3 1997/06/29 18:01:15 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.3 christos __RCSID("$NetBSD: table.c,v 1.3 1997/06/29 18:01:15 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.1 cgd
63 1.1 cgd #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
64 1.1 cgd
65 1.1 cgd #define NIL ((TABLE_ENTRY *)0)
66 1.1 cgd
67 1.1 cgd extern int debug;
68 1.1 cgd struct timeval tp;
69 1.1 cgd struct timezone txp;
70 1.1 cgd
71 1.1 cgd typedef struct table_entry TABLE_ENTRY;
72 1.1 cgd
73 1.1 cgd struct table_entry {
74 1.1 cgd CTL_MSG request;
75 1.1 cgd long time;
76 1.1 cgd TABLE_ENTRY *next;
77 1.1 cgd TABLE_ENTRY *last;
78 1.1 cgd };
79 1.1 cgd
80 1.1 cgd TABLE_ENTRY *table = NIL;
81 1.1 cgd CTL_MSG *find_request();
82 1.1 cgd CTL_MSG *find_match();
83 1.1 cgd
84 1.1 cgd /*
85 1.1 cgd * Look in the table for an invitation that matches the current
86 1.1 cgd * request looking for an invitation
87 1.1 cgd */
88 1.1 cgd CTL_MSG *
89 1.1 cgd find_match(request)
90 1.1 cgd register CTL_MSG *request;
91 1.1 cgd {
92 1.1 cgd register TABLE_ENTRY *ptr;
93 1.1 cgd time_t current_time;
94 1.1 cgd
95 1.1 cgd gettimeofday(&tp, &txp);
96 1.1 cgd current_time = tp.tv_sec;
97 1.1 cgd if (debug)
98 1.1 cgd print_request("find_match", request);
99 1.1 cgd for (ptr = table; ptr != NIL; ptr = ptr->next) {
100 1.1 cgd if ((ptr->time - current_time) > MAX_LIFE) {
101 1.1 cgd /* the entry is too old */
102 1.1 cgd if (debug)
103 1.1 cgd print_request("deleting expired entry",
104 1.1 cgd &ptr->request);
105 1.1 cgd delete(ptr);
106 1.1 cgd continue;
107 1.1 cgd }
108 1.1 cgd if (debug)
109 1.1 cgd print_request("", &ptr->request);
110 1.1 cgd if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
111 1.1 cgd strcmp(request->r_name, ptr->request.l_name) == 0 &&
112 1.1 cgd ptr->request.type == LEAVE_INVITE)
113 1.1 cgd return (&ptr->request);
114 1.1 cgd }
115 1.1 cgd return ((CTL_MSG *)0);
116 1.1 cgd }
117 1.1 cgd
118 1.1 cgd /*
119 1.1 cgd * Look for an identical request, as opposed to a complimentary
120 1.1 cgd * one as find_match does
121 1.1 cgd */
122 1.1 cgd CTL_MSG *
123 1.1 cgd find_request(request)
124 1.1 cgd register CTL_MSG *request;
125 1.1 cgd {
126 1.1 cgd register TABLE_ENTRY *ptr;
127 1.1 cgd time_t current_time;
128 1.1 cgd
129 1.1 cgd gettimeofday(&tp, &txp);
130 1.1 cgd current_time = tp.tv_sec;
131 1.1 cgd /*
132 1.1 cgd * See if this is a repeated message, and check for
133 1.1 cgd * out of date entries in the table while we are it.
134 1.1 cgd */
135 1.1 cgd if (debug)
136 1.1 cgd print_request("find_request", request);
137 1.1 cgd for (ptr = table; ptr != NIL; ptr = ptr->next) {
138 1.1 cgd if ((ptr->time - current_time) > MAX_LIFE) {
139 1.1 cgd /* the entry is too old */
140 1.1 cgd if (debug)
141 1.1 cgd print_request("deleting expired entry",
142 1.1 cgd &ptr->request);
143 1.1 cgd delete(ptr);
144 1.1 cgd continue;
145 1.1 cgd }
146 1.1 cgd if (debug)
147 1.1 cgd print_request("", &ptr->request);
148 1.1 cgd if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
149 1.1 cgd strcmp(request->l_name, ptr->request.l_name) == 0 &&
150 1.1 cgd request->type == ptr->request.type &&
151 1.1 cgd request->pid == ptr->request.pid) {
152 1.1 cgd /* update the time if we 'touch' it */
153 1.1 cgd ptr->time = current_time;
154 1.1 cgd return (&ptr->request);
155 1.1 cgd }
156 1.1 cgd }
157 1.1 cgd return ((CTL_MSG *)0);
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd insert_table(request, response)
161 1.1 cgd CTL_MSG *request;
162 1.1 cgd CTL_RESPONSE *response;
163 1.1 cgd {
164 1.1 cgd register TABLE_ENTRY *ptr;
165 1.1 cgd time_t current_time;
166 1.1 cgd
167 1.1 cgd gettimeofday(&tp, &txp);
168 1.1 cgd current_time = tp.tv_sec;
169 1.1 cgd request->id_num = new_id();
170 1.1 cgd response->id_num = htonl(request->id_num);
171 1.1 cgd /* insert a new entry into the top of the list */
172 1.1 cgd ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
173 1.1 cgd if (ptr == NIL) {
174 1.1 cgd syslog(LOG_ERR, "insert_table: Out of memory");
175 1.1 cgd _exit(1);
176 1.1 cgd }
177 1.1 cgd ptr->time = current_time;
178 1.1 cgd ptr->request = *request;
179 1.1 cgd ptr->next = table;
180 1.1 cgd if (ptr->next != NIL)
181 1.1 cgd ptr->next->last = ptr;
182 1.1 cgd ptr->last = NIL;
183 1.1 cgd table = ptr;
184 1.1 cgd }
185 1.1 cgd
186 1.1 cgd /*
187 1.1 cgd * Generate a unique non-zero sequence number
188 1.1 cgd */
189 1.1 cgd new_id()
190 1.1 cgd {
191 1.1 cgd static int current_id = 0;
192 1.1 cgd
193 1.1 cgd current_id = (current_id + 1) % MAX_ID;
194 1.1 cgd /* 0 is reserved, helps to pick up bugs */
195 1.1 cgd if (current_id == 0)
196 1.1 cgd current_id = 1;
197 1.1 cgd return (current_id);
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Delete the invitation with id 'id_num'
202 1.1 cgd */
203 1.1 cgd delete_invite(id_num)
204 1.1 cgd int id_num;
205 1.1 cgd {
206 1.1 cgd register TABLE_ENTRY *ptr;
207 1.1 cgd
208 1.1 cgd ptr = table;
209 1.1 cgd if (debug)
210 1.1 cgd syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
211 1.1 cgd for (ptr = table; ptr != NIL; ptr = ptr->next) {
212 1.1 cgd if (ptr->request.id_num == id_num)
213 1.1 cgd break;
214 1.1 cgd if (debug)
215 1.1 cgd print_request("", &ptr->request);
216 1.1 cgd }
217 1.1 cgd if (ptr != NIL) {
218 1.1 cgd delete(ptr);
219 1.1 cgd return (SUCCESS);
220 1.1 cgd }
221 1.1 cgd return (NOT_HERE);
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd /*
225 1.1 cgd * Classic delete from a double-linked list
226 1.1 cgd */
227 1.1 cgd delete(ptr)
228 1.1 cgd register TABLE_ENTRY *ptr;
229 1.1 cgd {
230 1.1 cgd
231 1.1 cgd if (debug)
232 1.1 cgd print_request("delete", &ptr->request);
233 1.1 cgd if (table == ptr)
234 1.1 cgd table = ptr->next;
235 1.1 cgd else if (ptr->last != NIL)
236 1.1 cgd ptr->last->next = ptr->next;
237 1.1 cgd if (ptr->next != NIL)
238 1.1 cgd ptr->next->last = ptr->last;
239 1.1 cgd free((char *)ptr);
240 1.1 cgd }
241