xref: /netbsd-src/libexec/talkd/table.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * 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 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)table.c	5.7 (Berkeley) 2/26/91";*/
36 static char rcsid[] = "$Id: table.c,v 1.2 1993/08/01 18:29:32 mycroft Exp $";
37 #endif /* not lint */
38 
39 /*
40  * Routines to handle insertion, deletion, etc on the table
41  * of requests kept by the daemon. Nothing fancy here, linear
42  * search on a double-linked list. A time is kept with each
43  * entry so that overly old invitations can be eliminated.
44  *
45  * Consider this a mis-guided attempt at modularity
46  */
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <protocols/talkd.h>
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
58 
59 #define NIL ((TABLE_ENTRY *)0)
60 
61 extern	int debug;
62 struct	timeval tp;
63 struct	timezone txp;
64 
65 typedef struct table_entry TABLE_ENTRY;
66 
67 struct table_entry {
68 	CTL_MSG request;
69 	long	time;
70 	TABLE_ENTRY *next;
71 	TABLE_ENTRY *last;
72 };
73 
74 TABLE_ENTRY *table = NIL;
75 CTL_MSG *find_request();
76 CTL_MSG *find_match();
77 
78 /*
79  * Look in the table for an invitation that matches the current
80  * request looking for an invitation
81  */
82 CTL_MSG *
83 find_match(request)
84 	register CTL_MSG *request;
85 {
86 	register TABLE_ENTRY *ptr;
87 	time_t current_time;
88 
89 	gettimeofday(&tp, &txp);
90 	current_time = tp.tv_sec;
91 	if (debug)
92 		print_request("find_match", request);
93 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
94 		if ((ptr->time - current_time) > MAX_LIFE) {
95 			/* the entry is too old */
96 			if (debug)
97 				print_request("deleting expired entry",
98 				    &ptr->request);
99 			delete(ptr);
100 			continue;
101 		}
102 		if (debug)
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);
108 	}
109 	return ((CTL_MSG *)0);
110 }
111 
112 /*
113  * Look for an identical request, as opposed to a complimentary
114  * one as find_match does
115  */
116 CTL_MSG *
117 find_request(request)
118 	register CTL_MSG *request;
119 {
120 	register TABLE_ENTRY *ptr;
121 	time_t current_time;
122 
123 	gettimeofday(&tp, &txp);
124 	current_time = tp.tv_sec;
125 	/*
126 	 * See if this is a repeated message, and check for
127 	 * out of date entries in the table while we are it.
128 	 */
129 	if (debug)
130 		print_request("find_request", request);
131 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
132 		if ((ptr->time - current_time) > MAX_LIFE) {
133 			/* the entry is too old */
134 			if (debug)
135 				print_request("deleting expired entry",
136 				    &ptr->request);
137 			delete(ptr);
138 			continue;
139 		}
140 		if (debug)
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);
149 		}
150 	}
151 	return ((CTL_MSG *)0);
152 }
153 
154 insert_table(request, response)
155 	CTL_MSG *request;
156 	CTL_RESPONSE *response;
157 {
158 	register TABLE_ENTRY *ptr;
159 	time_t current_time;
160 
161 	gettimeofday(&tp, &txp);
162 	current_time = tp.tv_sec;
163 	request->id_num = new_id();
164 	response->id_num = htonl(request->id_num);
165 	/* insert a new entry into the top of the list */
166 	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
167 	if (ptr == NIL) {
168 		syslog(LOG_ERR, "insert_table: Out of memory");
169 		_exit(1);
170 	}
171 	ptr->time = current_time;
172 	ptr->request = *request;
173 	ptr->next = table;
174 	if (ptr->next != NIL)
175 		ptr->next->last = ptr;
176 	ptr->last = NIL;
177 	table = ptr;
178 }
179 
180 /*
181  * Generate a unique non-zero sequence number
182  */
183 new_id()
184 {
185 	static int current_id = 0;
186 
187 	current_id = (current_id + 1) % MAX_ID;
188 	/* 0 is reserved, helps to pick up bugs */
189 	if (current_id == 0)
190 		current_id = 1;
191 	return (current_id);
192 }
193 
194 /*
195  * Delete the invitation with id 'id_num'
196  */
197 delete_invite(id_num)
198 	int id_num;
199 {
200 	register TABLE_ENTRY *ptr;
201 
202 	ptr = table;
203 	if (debug)
204 		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
205 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
206 		if (ptr->request.id_num == id_num)
207 			break;
208 		if (debug)
209 			print_request("", &ptr->request);
210 	}
211 	if (ptr != NIL) {
212 		delete(ptr);
213 		return (SUCCESS);
214 	}
215 	return (NOT_HERE);
216 }
217 
218 /*
219  * Classic delete from a double-linked list
220  */
221 delete(ptr)
222 	register TABLE_ENTRY *ptr;
223 {
224 
225 	if (debug)
226 		print_request("delete", &ptr->request);
227 	if (table == ptr)
228 		table = ptr->next;
229 	else if (ptr->last != NIL)
230 		ptr->last->next = ptr->next;
231 	if (ptr->next != NIL)
232 		ptr->next->last = ptr->last;
233 	free((char *)ptr);
234 }
235