2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
38 static const char rcsid[] =
43 * Routines to handle insertion, deletion, etc on the table
44 * of requests kept by the daemon. Nothing fancy here, linear
45 * search on a double-linked list. A time is kept with each
46 * entry so that overly old invitations can be eliminated.
48 * Consider this a mis-guided attempt at modularity
50 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <protocols/talkd.h>
63 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
65 #define NIL ((TABLE_ENTRY *)0)
71 typedef struct table_entry TABLE_ENTRY;
80 static void delete(TABLE_ENTRY *);
82 TABLE_ENTRY *table = NIL;
85 * Look in the table for an invitation that matches the current
86 * request looking for an invitation
89 find_match(CTL_MSG *request)
94 gettimeofday(&tp, &txp);
95 current_time = tp.tv_sec;
97 print_request("find_match", request);
98 for (ptr = table; ptr != NIL; ptr = ptr->next) {
99 if ((ptr->time - current_time) > MAX_LIFE) {
100 /* the entry is too old */
102 print_request("deleting expired entry",
108 print_request("", &ptr->request);
109 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
110 strcmp(request->r_name, ptr->request.l_name) == 0 &&
111 ptr->request.type == LEAVE_INVITE)
112 return (&ptr->request);
114 return ((CTL_MSG *)0);
118 * Look for an identical request, as opposed to a complimentary
119 * one as find_match does
122 find_request(CTL_MSG *request)
127 gettimeofday(&tp, &txp);
128 current_time = tp.tv_sec;
130 * See if this is a repeated message, and check for
131 * out of date entries in the table while we are it.
134 print_request("find_request", request);
135 for (ptr = table; ptr != NIL; ptr = ptr->next) {
136 if ((ptr->time - current_time) > MAX_LIFE) {
137 /* the entry is too old */
139 print_request("deleting expired entry",
145 print_request("", &ptr->request);
146 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
147 strcmp(request->l_name, ptr->request.l_name) == 0 &&
148 request->type == ptr->request.type &&
149 request->pid == ptr->request.pid) {
150 /* update the time if we 'touch' it */
151 ptr->time = current_time;
152 return (&ptr->request);
155 return ((CTL_MSG *)0);
159 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
164 gettimeofday(&tp, &txp);
165 current_time = tp.tv_sec;
166 request->id_num = new_id();
167 response->id_num = htonl(request->id_num);
168 /* insert a new entry into the top of the list */
169 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
171 syslog(LOG_ERR, "insert_table: Out of memory");
174 ptr->time = current_time;
175 ptr->request = *request;
177 if (ptr->next != NIL)
178 ptr->next->last = ptr;
184 * Generate a unique non-zero sequence number
189 static int current_id = 0;
191 current_id = (current_id + 1) % MAX_ID;
192 /* 0 is reserved, helps to pick up bugs */
199 * Delete the invitation with id 'id_num'
202 delete_invite(u_int32_t id_num)
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)
213 print_request("", &ptr->request);
223 * Classic delete from a double-linked list
226 delete(TABLE_ENTRY *ptr)
230 print_request("delete", &ptr->request);
233 else if (ptr->last != NIL)
234 ptr->last->next = ptr->next;
235 if (ptr->next != NIL)
236 ptr->next->last = ptr->last;