Merge from vendor branch GROFF:
[dragonfly.git] / libexec / talkd / table.c
CommitLineData
984263bc
MD
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.
1de703da
MD
32 *
33 * @(#)table.c 8.1 (Berkeley) 6/4/93
34 * $FreeBSD: src/libexec/talkd/table.c,v 1.7 1999/08/28 00:10:17 peter Exp $
3eefb696 35 * $DragonFly: src/libexec/talkd/table.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
984263bc
MD
36 */
37
984263bc
MD
38/*
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.
43 *
44 * Consider this a mis-guided attempt at modularity
45 */
46#include <sys/param.h>
47#include <sys/time.h>
48#include <sys/socket.h>
49#include <protocols/talkd.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <syslog.h>
54#include <unistd.h>
55
56#define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
57
58#define NIL ((TABLE_ENTRY *)0)
59
60extern int debug;
61struct timeval tp;
62struct timezone txp;
63
64typedef struct table_entry TABLE_ENTRY;
65
66struct table_entry {
67 CTL_MSG request;
68 long time;
69 TABLE_ENTRY *next;
70 TABLE_ENTRY *last;
71};
72
73TABLE_ENTRY *table = NIL;
74
3eefb696 75void delete (TABLE_ENTRY *);
984263bc
MD
76CTL_MSG *find_request();
77CTL_MSG *find_match();
3eefb696
MD
78int new_id (void);
79void print_request (char *, CTL_MSG *);
984263bc
MD
80
81/*
82 * Look in the table for an invitation that matches the current
83 * request looking for an invitation
84 */
85CTL_MSG *
86find_match(request)
87 register CTL_MSG *request;
88{
89 register TABLE_ENTRY *ptr;
90 time_t current_time;
91
92 gettimeofday(&tp, &txp);
93 current_time = tp.tv_sec;
94 if (debug)
95 print_request("find_match", request);
96 for (ptr = table; ptr != NIL; ptr = ptr->next) {
97 if ((ptr->time - current_time) > MAX_LIFE) {
98 /* the entry is too old */
99 if (debug)
100 print_request("deleting expired entry",
101 &ptr->request);
102 delete(ptr);
103 continue;
104 }
105 if (debug)
106 print_request("", &ptr->request);
107 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
108 strcmp(request->r_name, ptr->request.l_name) == 0 &&
109 ptr->request.type == LEAVE_INVITE)
110 return (&ptr->request);
111 }
112 return ((CTL_MSG *)0);
113}
114
115/*
116 * Look for an identical request, as opposed to a complimentary
117 * one as find_match does
118 */
119CTL_MSG *
120find_request(request)
121 register CTL_MSG *request;
122{
123 register TABLE_ENTRY *ptr;
124 time_t current_time;
125
126 gettimeofday(&tp, &txp);
127 current_time = tp.tv_sec;
128 /*
129 * See if this is a repeated message, and check for
130 * out of date entries in the table while we are it.
131 */
132 if (debug)
133 print_request("find_request", request);
134 for (ptr = table; ptr != NIL; ptr = ptr->next) {
135 if ((ptr->time - current_time) > MAX_LIFE) {
136 /* the entry is too old */
137 if (debug)
138 print_request("deleting expired entry",
139 &ptr->request);
140 delete(ptr);
141 continue;
142 }
143 if (debug)
144 print_request("", &ptr->request);
145 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
146 strcmp(request->l_name, ptr->request.l_name) == 0 &&
147 request->type == ptr->request.type &&
148 request->pid == ptr->request.pid) {
149 /* update the time if we 'touch' it */
150 ptr->time = current_time;
151 return (&ptr->request);
152 }
153 }
154 return ((CTL_MSG *)0);
155}
156
157void
158insert_table(request, response)
159 CTL_MSG *request;
160 CTL_RESPONSE *response;
161{
162 register TABLE_ENTRY *ptr;
163 time_t current_time;
164
165 gettimeofday(&tp, &txp);
166 current_time = tp.tv_sec;
167 request->id_num = new_id();
168 response->id_num = htonl(request->id_num);
169 /* insert a new entry into the top of the list */
170 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
171 if (ptr == NIL) {
172 syslog(LOG_ERR, "insert_table: Out of memory");
173 _exit(1);
174 }
175 ptr->time = current_time;
176 ptr->request = *request;
177 ptr->next = table;
178 if (ptr->next != NIL)
179 ptr->next->last = ptr;
180 ptr->last = NIL;
181 table = ptr;
182}
183
184/*
185 * Generate a unique non-zero sequence number
186 */
187int
188new_id()
189{
190 static int current_id = 0;
191
192 current_id = (current_id + 1) % MAX_ID;
193 /* 0 is reserved, helps to pick up bugs */
194 if (current_id == 0)
195 current_id = 1;
196 return (current_id);
197}
198
199/*
200 * Delete the invitation with id 'id_num'
201 */
202int
203delete_invite(id_num)
204 int id_num;
205{
206 register TABLE_ENTRY *ptr;
207
208 ptr = table;
209 if (debug)
210 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
211 for (ptr = table; ptr != NIL; ptr = ptr->next) {
212 if (ptr->request.id_num == id_num)
213 break;
214 if (debug)
215 print_request("", &ptr->request);
216 }
217 if (ptr != NIL) {
218 delete(ptr);
219 return (SUCCESS);
220 }
221 return (NOT_HERE);
222}
223
224/*
225 * Classic delete from a double-linked list
226 */
227void
228delete(ptr)
229 register TABLE_ENTRY *ptr;
230{
231
232 if (debug)
233 print_request("delete", &ptr->request);
234 if (table == ptr)
235 table = ptr->next;
236 else if (ptr->last != NIL)
237 ptr->last->next = ptr->next;
238 if (ptr->next != NIL)
239 ptr->next->last = ptr->last;
240 free((char *)ptr);
241}