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