Make compat netbsd32 sendmsg follow the native version by allowing
[netbsd-mini2440.git] / libexec / talkd / table.c
blob6c26b789dbe9f3633c1135a7fe385905dd32392c
1 /* $NetBSD: table.c,v 1.9 2009/03/16 01:04:32 lukem Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: table.c,v 1.9 2009/03/16 01:04:32 lukem Exp $");
38 #endif
39 #endif /* not lint */
42 * Routines to handle insertion, deletion, etc on the table
43 * of requests kept by the daemon. Nothing fancy here, linear
44 * search on a double-linked list. A time is kept with each
45 * entry so that overly old invitations can be eliminated.
47 * Consider this a mis-guided attempt at modularity
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/socket.h>
52 #include <protocols/talkd.h>
53 #include <inttypes.h>
54 #include <syslog.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include "extern.h"
61 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
63 #define NIL ((TABLE_ENTRY *)0)
65 struct timeval tp;
67 typedef struct table_entry TABLE_ENTRY;
69 struct table_entry {
70 CTL_MSG request;
71 time_t time;
72 TABLE_ENTRY *next;
73 TABLE_ENTRY *last;
76 TABLE_ENTRY *table = NIL;
78 static void delete(TABLE_ENTRY *);
81 * Look in the table for an invitation that matches the current
82 * request looking for an invitation
84 CTL_MSG *
85 find_match(CTL_MSG *request)
87 TABLE_ENTRY *ptr;
88 time_t current_time;
90 gettimeofday(&tp, NULL);
91 current_time = tp.tv_sec;
92 if (debug)
93 print_request("find_match", request);
94 for (ptr = table; ptr != NIL; ptr = ptr->next) {
95 if ((ptr->time - current_time) > MAX_LIFE) {
96 /* the entry is too old */
97 if (debug)
98 print_request("deleting expired entry",
99 &ptr->request);
100 delete(ptr);
101 continue;
103 if (debug)
104 print_request("", &ptr->request);
105 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
106 strcmp(request->r_name, ptr->request.l_name) == 0 &&
107 ptr->request.type == LEAVE_INVITE)
108 return (&ptr->request);
110 return ((CTL_MSG *)0);
114 * Look for an identical request, as opposed to a complimentary
115 * one as find_match does
117 CTL_MSG *
118 find_request(CTL_MSG *request)
120 TABLE_ENTRY *ptr;
121 time_t current_time;
123 gettimeofday(&tp, NULL);
124 current_time = tp.tv_sec;
126 * See if this is a repeated message, and check for
127 * out of date entries in the table while we are it.
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;
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);
151 return ((CTL_MSG *)0);
154 void
155 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
157 TABLE_ENTRY *ptr;
158 time_t current_time;
160 gettimeofday(&tp, NULL);
161 current_time = tp.tv_sec;
162 request->id_num = new_id();
163 response->id_num = htonl(request->id_num);
164 /* insert a new entry into the top of the list */
165 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
166 if (ptr == NIL) {
167 syslog(LOG_ERR, "insert_table: Out of memory");
168 _exit(1);
170 ptr->time = current_time;
171 ptr->request = *request;
172 ptr->next = table;
173 if (ptr->next != NIL)
174 ptr->next->last = ptr;
175 ptr->last = NIL;
176 table = ptr;
180 * Generate a unique non-zero sequence number
182 uint32_t
183 new_id(void)
185 static uint32_t current_id = 0;
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);
195 * Delete the invitation with id 'id_num'
197 u_char
198 delete_invite(uint32_t id_num)
200 TABLE_ENTRY *ptr;
202 ptr = table;
203 if (debug)
204 syslog(LOG_DEBUG, "delete_invite(%"PRIu32")", 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);
211 if (ptr != NIL) {
212 delete(ptr);
213 return (SUCCESS);
215 return (NOT_HERE);
219 * Classic delete from a double-linked list
221 static void
222 delete(TABLE_ENTRY *ptr)
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);