table.c revision 22989
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 *	$Id$
34 */
35
36#ifndef lint
37static char sccsid[] = "@(#)table.c	8.1 (Berkeley) 6/4/93";
38#endif /* not lint */
39
40/*
41 * Routines to handle insertion, deletion, etc on the table
42 * of requests kept by the daemon. Nothing fancy here, linear
43 * search on a double-linked list. A time is kept with each
44 * entry so that overly old invitations can be eliminated.
45 *
46 * Consider this a mis-guided attempt at modularity
47 */
48#include <sys/param.h>
49#include <sys/time.h>
50#include <sys/socket.h>
51#include <protocols/talkd.h>
52#include <syslog.h>
53#include <unistd.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57
58#define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
59
60#define NIL ((TABLE_ENTRY *)0)
61
62extern	int debug;
63struct	timeval tp;
64struct	timezone txp;
65
66typedef struct table_entry TABLE_ENTRY;
67
68struct table_entry {
69	CTL_MSG request;
70	long	time;
71	TABLE_ENTRY *next;
72	TABLE_ENTRY *last;
73};
74
75TABLE_ENTRY *table = NIL;
76CTL_MSG *find_request();
77CTL_MSG *find_match();
78
79/*
80 * Look in the table for an invitation that matches the current
81 * request looking for an invitation
82 */
83CTL_MSG *
84find_match(request)
85	register CTL_MSG *request;
86{
87	register TABLE_ENTRY *ptr;
88	time_t current_time;
89
90	gettimeofday(&tp, &txp);
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 */
117CTL_MSG *
118find_request(request)
119	register CTL_MSG *request;
120{
121	register TABLE_ENTRY *ptr;
122	time_t current_time;
123
124	gettimeofday(&tp, &txp);
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
155insert_table(request, response)
156	CTL_MSG *request;
157	CTL_RESPONSE *response;
158{
159	register TABLE_ENTRY *ptr;
160	time_t current_time;
161
162	gettimeofday(&tp, &txp);
163	current_time = tp.tv_sec;
164	request->id_num = new_id();
165	response->id_num = htonl(request->id_num);
166	/* insert a new entry into the top of the list */
167	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
168	if (ptr == NIL) {
169		syslog(LOG_ERR, "insert_table: Out of memory");
170		_exit(1);
171	}
172	ptr->time = current_time;
173	ptr->request = *request;
174	ptr->next = table;
175	if (ptr->next != NIL)
176		ptr->next->last = ptr;
177	ptr->last = NIL;
178	table = ptr;
179}
180
181/*
182 * Generate a unique non-zero sequence number
183 */
184new_id()
185{
186	static int current_id = 0;
187
188	current_id = (current_id + 1) % MAX_ID;
189	/* 0 is reserved, helps to pick up bugs */
190	if (current_id == 0)
191		current_id = 1;
192	return (current_id);
193}
194
195/*
196 * Delete the invitation with id 'id_num'
197 */
198delete_invite(id_num)
199	int id_num;
200{
201	register TABLE_ENTRY *ptr;
202
203	ptr = table;
204	if (debug)
205		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
206	for (ptr = table; ptr != NIL; ptr = ptr->next) {
207		if (ptr->request.id_num == id_num)
208			break;
209		if (debug)
210			print_request("", &ptr->request);
211	}
212	if (ptr != NIL) {
213		delete(ptr);
214		return (SUCCESS);
215	}
216	return (NOT_HERE);
217}
218
219/*
220 * Classic delete from a double-linked list
221 */
222delete(ptr)
223	register TABLE_ENTRY *ptr;
224{
225
226	if (debug)
227		print_request("delete", &ptr->request);
228	if (table == ptr)
229		table = ptr->next;
230	else if (ptr->last != NIL)
231		ptr->last->next = ptr->next;
232	if (ptr->next != NIL)
233		ptr->next->last = ptr->last;
234	free((char *)ptr);
235}
236