xref: /freebsd/libexec/talkd/table.c (revision 525fe93dc7487a1e63a90f6a2b956abc601963c1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Routines to handle insertion, deletion, etc on the table
34  * of requests kept by the daemon. Nothing fancy here, linear
35  * search on a double-linked list. A time is kept with each
36  * entry so that overly old invitations can be eliminated.
37  *
38  * Consider this a mis-guided attempt at modularity
39  */
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <protocols/talkd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <unistd.h>
50 
51 #include "extern.h"
52 
53 #define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
54 
55 #define NIL ((TABLE_ENTRY *)0)
56 
57 static struct timespec ts;
58 
59 typedef struct table_entry TABLE_ENTRY;
60 
61 struct table_entry {
62 	CTL_MSG request;
63 	long	time;
64 	TABLE_ENTRY *next;
65 	TABLE_ENTRY *last;
66 };
67 
68 static void delete(TABLE_ENTRY *);
69 
70 static TABLE_ENTRY *table = NIL;
71 
72 /*
73  * Look in the table for an invitation that matches the current
74  * request looking for an invitation
75  */
76 CTL_MSG *
77 find_match(CTL_MSG *request)
78 {
79 	TABLE_ENTRY *ptr, *next;
80 	time_t current_time;
81 
82 	clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
83 	current_time = ts.tv_sec;
84 	if (debug)
85 		print_request("find_match", request);
86 	for (ptr = table; ptr != NIL; ptr = next) {
87 		next = ptr->next;
88 		if ((ptr->time - current_time) > MAX_LIFE) {
89 			/* the entry is too old */
90 			if (debug)
91 				print_request("deleting expired entry",
92 				    &ptr->request);
93 			delete(ptr);
94 			continue;
95 		}
96 		if (debug)
97 			print_request("", &ptr->request);
98 		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
99 		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
100 		     ptr->request.type == LEAVE_INVITE)
101 			return (&ptr->request);
102 	}
103 	return ((CTL_MSG *)0);
104 }
105 
106 /*
107  * Look for an identical request, as opposed to a complimentary
108  * one as find_match does
109  */
110 CTL_MSG *
111 find_request(CTL_MSG *request)
112 {
113 	TABLE_ENTRY *ptr, *next;
114 	time_t current_time;
115 
116 	clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
117 	current_time = ts.tv_sec;
118 	/*
119 	 * See if this is a repeated message, and check for
120 	 * out of date entries in the table while we are it.
121 	 */
122 	if (debug)
123 		print_request("find_request", request);
124 	for (ptr = table; ptr != NIL; ptr = next) {
125 		next = ptr->next;
126 		if ((ptr->time - current_time) > MAX_LIFE) {
127 			/* the entry is too old */
128 			if (debug)
129 				print_request("deleting expired entry",
130 				    &ptr->request);
131 			delete(ptr);
132 			continue;
133 		}
134 		if (debug)
135 			print_request("", &ptr->request);
136 		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
137 		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
138 		    request->type == ptr->request.type &&
139 		    request->pid == ptr->request.pid) {
140 			/* update the time if we 'touch' it */
141 			ptr->time = current_time;
142 			return (&ptr->request);
143 		}
144 	}
145 	return ((CTL_MSG *)0);
146 }
147 
148 void
149 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
150 {
151 	TABLE_ENTRY *ptr;
152 	time_t current_time;
153 
154 	clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
155 	current_time = ts.tv_sec;
156 	request->id_num = new_id();
157 	response->id_num = htonl(request->id_num);
158 	/* insert a new entry into the top of the list */
159 	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
160 	if (ptr == NIL) {
161 		syslog(LOG_ERR, "insert_table: Out of memory");
162 		_exit(1);
163 	}
164 	ptr->time = current_time;
165 	ptr->request = *request;
166 	ptr->next = table;
167 	if (ptr->next != NIL)
168 		ptr->next->last = ptr;
169 	ptr->last = NIL;
170 	table = ptr;
171 }
172 
173 /*
174  * Generate a unique non-zero sequence number
175  */
176 int
177 new_id(void)
178 {
179 	static int current_id = 0;
180 
181 	current_id = (current_id + 1) % MAX_ID;
182 	/* 0 is reserved, helps to pick up bugs */
183 	if (current_id == 0)
184 		current_id = 1;
185 	return (current_id);
186 }
187 
188 /*
189  * Delete the invitation with id 'id_num'
190  */
191 int
192 delete_invite(u_int32_t id_num)
193 {
194 	TABLE_ENTRY *ptr;
195 
196 	if (debug)
197 		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
198 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
199 		if (ptr->request.id_num == id_num)
200 			break;
201 		if (debug)
202 			print_request("", &ptr->request);
203 	}
204 	if (ptr != NIL) {
205 		delete(ptr);
206 		return (SUCCESS);
207 	}
208 	return (NOT_HERE);
209 }
210 
211 /*
212  * Classic delete from a double-linked list
213  */
214 static void
215 delete(TABLE_ENTRY *ptr)
216 {
217 
218 	if (debug)
219 		print_request("delete", &ptr->request);
220 	if (table == ptr)
221 		table = ptr->next;
222 	else if (ptr->last != NIL)
223 		ptr->last->next = ptr->next;
224 	if (ptr->next != NIL)
225 		ptr->next->last = ptr->last;
226 	free((char *)ptr);
227 }
228