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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
34 static const char rcsid[] =
39 * Routines to handle insertion, deletion, etc on the table
40 * of requests kept by the daemon. Nothing fancy here, linear
41 * search on a double-linked list. A time is kept with each
42 * entry so that overly old invitations can be eliminated.
44 * Consider this a mis-guided attempt at modularity
46 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <protocols/talkd.h>
59 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
61 #define NIL ((TABLE_ENTRY *)0)
63 static struct timespec ts;
65 typedef struct table_entry TABLE_ENTRY;
74 static void delete(TABLE_ENTRY *);
76 static TABLE_ENTRY *table = NIL;
79 * Look in the table for an invitation that matches the current
80 * request looking for an invitation
83 find_match(CTL_MSG *request)
85 TABLE_ENTRY *ptr, *next;
88 clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
89 current_time = ts.tv_sec;
91 print_request("find_match", request);
92 for (ptr = table; ptr != NIL; ptr = next) {
94 if ((ptr->time - current_time) > MAX_LIFE) {
95 /* the entry is too old */
97 print_request("deleting expired entry",
103 print_request("", &ptr->request);
104 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
105 strcmp(request->r_name, ptr->request.l_name) == 0 &&
106 ptr->request.type == LEAVE_INVITE)
107 return (&ptr->request);
109 return ((CTL_MSG *)0);
113 * Look for an identical request, as opposed to a complimentary
114 * one as find_match does
117 find_request(CTL_MSG *request)
119 TABLE_ENTRY *ptr, *next;
122 clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
123 current_time = ts.tv_sec;
125 * See if this is a repeated message, and check for
126 * out of date entries in the table while we are it.
129 print_request("find_request", request);
130 for (ptr = table; ptr != NIL; ptr = next) {
132 if ((ptr->time - current_time) > MAX_LIFE) {
133 /* the entry is too old */
135 print_request("deleting expired entry",
141 print_request("", &ptr->request);
142 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
143 strcmp(request->l_name, ptr->request.l_name) == 0 &&
144 request->type == ptr->request.type &&
145 request->pid == ptr->request.pid) {
146 /* update the time if we 'touch' it */
147 ptr->time = current_time;
148 return (&ptr->request);
151 return ((CTL_MSG *)0);
155 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
160 clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
161 current_time = ts.tv_sec;
162 request->id_num = new_id();
163 response->id_num = htonl(request->id_num);
164 /* insert a new entry into the top of the list */
165 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
167 syslog(LOG_ERR, "insert_table: Out of memory");
170 ptr->time = current_time;
171 ptr->request = *request;
173 if (ptr->next != NIL)
174 ptr->next->last = ptr;
180 * Generate a unique non-zero sequence number
185 static int current_id = 0;
187 current_id = (current_id + 1) % MAX_ID;
188 /* 0 is reserved, helps to pick up bugs */
195 * Delete the invitation with id 'id_num'
198 delete_invite(u_int32_t id_num)
203 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
204 for (ptr = table; ptr != NIL; ptr = ptr->next) {
205 if (ptr->request.id_num == id_num)
208 print_request("", &ptr->request);
218 * Classic delete from a double-linked list
221 delete(TABLE_ENTRY *ptr)
225 print_request("delete", &ptr->request);
228 else if (ptr->last != NIL)
229 ptr->last->next = ptr->next;
230 if (ptr->next != NIL)
231 ptr->next->last = ptr->last;