1 /* $OpenBSD: table.c,v 1.13 2005/10/14 15:39:19 otto Exp $ */
2
3 /*
4 * Copyright (c) 1983 Regents of the University of California.
5 * 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 #ifndef lint
33 /*static char sccsid[] = "from: @(#)table.c 5.7 (Berkeley) 2/26/91";*/
34 static char rcsid[] = "$Id: table.c,v 1.13 2005/10/14 15:39:19 otto Exp $";
35 #endif /* not lint */
36
37 /*
38 * Routines to handle insertion, deletion, etc on the table
39 * of requests kept by the daemon. Nothing fancy here, linear
40 * search on a double-linked list. A time is kept with each
41 * entry so that overly old invitations can be eliminated.
42 *
43 * Consider this a mis-guided attempt at modularity
44 */
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
48 #include <sys/queue.h>
49 #include <protocols/talkd.h>
50 #include <syslog.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "talkd.h"
56
57 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
58
59 struct timeval tp;
60 struct timezone txp;
61
62 typedef struct table_entry TABLE_ENTRY;
63
64 struct table_entry {
65 CTL_MSG request;
66 time_t time;
67 TAILQ_ENTRY(table_entry) list;
68 };
69 TAILQ_HEAD(, table_entry) table;
70
71 static void delete(TABLE_ENTRY *);
72
73 /*
74 * Init the table
75 */
76 void
init_table(void)77 init_table(void)
78 {
79 TAILQ_INIT(&table);
80 }
81
82 /*
83 * Look in the table for an invitation that matches the current
84 * request looking for an invitation
85 */
86 CTL_MSG *
find_match(CTL_MSG * request)87 find_match(CTL_MSG *request)
88 {
89 TABLE_ENTRY *ptr, *next;
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 = TAILQ_FIRST(&table); ptr != NULL; ptr = next) {
97 next = TAILQ_NEXT(ptr, list);
98 if ((current_time - ptr->time) > MAX_LIFE) {
99 /* the entry is too old */
100 if (debug)
101 print_request("deleting expired entry",
102 &ptr->request);
103 delete(ptr);
104 continue;
105 }
106 if (debug)
107 print_request("", &ptr->request);
108 if (ptr->request.type == LEAVE_INVITE &&
109 strcmp(request->l_name, ptr->request.r_name) == 0 &&
110 strcmp(request->r_name, ptr->request.l_name) == 0)
111 return (&ptr->request);
112 }
113 if (debug)
114 syslog(LOG_DEBUG, "find_match: not found");
115
116 return ((CTL_MSG *)0);
117 }
118
119 /*
120 * Look for an identical request, as opposed to a complimentary
121 * one as find_match does
122 */
123 CTL_MSG *
find_request(CTL_MSG * request)124 find_request(CTL_MSG *request)
125 {
126 TABLE_ENTRY *ptr, *next;
127 time_t current_time;
128
129 gettimeofday(&tp, &txp);
130 current_time = tp.tv_sec;
131 /*
132 * See if this is a repeated message, and check for
133 * out of date entries in the table while we are it.
134 */
135 if (debug)
136 print_request("find_request", request);
137 for (ptr = TAILQ_FIRST(&table); ptr != NULL; ptr = next) {
138 next = TAILQ_NEXT(ptr, list);
139 if ((current_time - ptr->time) > MAX_LIFE) {
140 /* the entry is too old */
141 if (debug)
142 print_request("deleting expired entry",
143 &ptr->request);
144 delete(ptr);
145 continue;
146 }
147 if (debug)
148 print_request("", &ptr->request);
149 if (request->pid == ptr->request.pid &&
150 request->type == ptr->request.type &&
151 strcmp(request->r_name, ptr->request.r_name) == 0 &&
152 strcmp(request->l_name, ptr->request.l_name) == 0) {
153 /* update the time if we 'touch' it */
154 ptr->time = current_time;
155 return (&ptr->request);
156 }
157 }
158 return ((CTL_MSG *)0);
159 }
160
161 void
insert_table(CTL_MSG * request,CTL_RESPONSE * response)162 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
163 {
164 TABLE_ENTRY *ptr;
165 time_t current_time;
166
167 if (debug)
168 print_request( "insert_table", request );
169 gettimeofday(&tp, &txp);
170 current_time = tp.tv_sec;
171 request->id_num = new_id();
172 response->id_num = htonl(request->id_num);
173 /* insert a new entry into the top of the list */
174 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
175 if (ptr == NULL) {
176 syslog(LOG_ERR, "insert_table: Out of memory");
177 _exit(1);
178 }
179 ptr->time = current_time;
180 ptr->request = *request;
181 TAILQ_INSERT_HEAD(&table, ptr, list);
182 }
183
184 /*
185 * Generate a unique non-zero sequence number
186 */
187 int
new_id(void)188 new_id(void)
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 */
202 int
delete_invite(int id_num)203 delete_invite(int id_num)
204 {
205 TABLE_ENTRY *ptr;
206
207 if (debug)
208 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
209 TAILQ_FOREACH(ptr, &table, list) {
210 if (ptr->request.id_num == id_num)
211 break;
212 if (debug)
213 print_request("", &ptr->request);
214 }
215 if (ptr != NULL) {
216 delete(ptr);
217 return (SUCCESS);
218 }
219 return (NOT_HERE);
220 }
221
222 /*
223 * Classic delete from a double-linked list
224 */
225 static void
delete(TABLE_ENTRY * ptr)226 delete(TABLE_ENTRY *ptr)
227 {
228
229 if (debug)
230 print_request("delete", &ptr->request);
231 TAILQ_REMOVE(&table, ptr, list);
232 free((char *)ptr);
233 }
234