]> git.saurik.com Git - apple/network_cmds.git/blob - talkd.tproj/table.c
5b6ee3f49f86764d79e40c2b3eafe90b7cbcc56b
[apple/network_cmds.git] / talkd.tproj / table.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #ifndef lint
59 static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
60 #endif /* not lint */
61
62 /*
63 * Routines to handle insertion, deletion, etc on the table
64 * of requests kept by the daemon. Nothing fancy here, linear
65 * search on a double-linked list. A time is kept with each
66 * entry so that overly old invitations can be eliminated.
67 *
68 * Consider this a mis-guided attempt at modularity
69 */
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/socket.h>
73 #include <protocols/talkd.h>
74 #include <syslog.h>
75 #include <unistd.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include "talkd.h"
80
81 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
82
83 #define NIL ((TABLE_ENTRY *)0)
84
85 extern int debug;
86 struct timeval tp;
87 struct timezone txp;
88
89 typedef struct table_entry TABLE_ENTRY;
90
91 struct table_entry {
92 CTL_MSG request;
93 long time;
94 TABLE_ENTRY *next;
95 TABLE_ENTRY *last;
96 };
97
98 TABLE_ENTRY *table = NIL;
99 CTL_MSG *find_request();
100 CTL_MSG *find_match();
101
102 void delete __P((TABLE_ENTRY *ptr));
103
104 /*
105 * Look in the table for an invitation that matches the current
106 * request looking for an invitation
107 */
108 CTL_MSG *
109 find_match(request)
110 register CTL_MSG *request;
111 {
112 register TABLE_ENTRY *ptr;
113 time_t current_time;
114
115 gettimeofday(&tp, &txp);
116 current_time = tp.tv_sec;
117 if (debug)
118 print_request("find_match", request);
119 for (ptr = table; ptr != NIL; ptr = ptr->next) {
120 if ((ptr->time - current_time) > MAX_LIFE) {
121 /* the entry is too old */
122 if (debug)
123 print_request("deleting expired entry",
124 &ptr->request);
125 delete(ptr);
126 continue;
127 }
128 if (debug)
129 print_request("", &ptr->request);
130 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
131 strcmp(request->r_name, ptr->request.l_name) == 0 &&
132 ptr->request.type == LEAVE_INVITE)
133 return (&ptr->request);
134 }
135 return ((CTL_MSG *)0);
136 }
137
138 /*
139 * Look for an identical request, as opposed to a complimentary
140 * one as find_match does
141 */
142 CTL_MSG *
143 find_request(request)
144 register CTL_MSG *request;
145 {
146 register TABLE_ENTRY *ptr;
147 time_t current_time;
148
149 gettimeofday(&tp, &txp);
150 current_time = tp.tv_sec;
151 /*
152 * See if this is a repeated message, and check for
153 * out of date entries in the table while we are it.
154 */
155 if (debug)
156 print_request("find_request", request);
157 for (ptr = table; ptr != NIL; ptr = ptr->next) {
158 if ((ptr->time - current_time) > MAX_LIFE) {
159 /* the entry is too old */
160 if (debug)
161 print_request("deleting expired entry",
162 &ptr->request);
163 delete(ptr);
164 continue;
165 }
166 if (debug)
167 print_request("", &ptr->request);
168 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
169 strcmp(request->l_name, ptr->request.l_name) == 0 &&
170 request->type == ptr->request.type &&
171 request->pid == ptr->request.pid) {
172 /* update the time if we 'touch' it */
173 ptr->time = current_time;
174 return (&ptr->request);
175 }
176 }
177 return ((CTL_MSG *)0);
178 }
179
180 void
181 insert_table(request, response)
182 CTL_MSG *request;
183 CTL_RESPONSE *response;
184 {
185 register TABLE_ENTRY *ptr;
186 time_t current_time;
187
188 gettimeofday(&tp, &txp);
189 current_time = tp.tv_sec;
190 request->id_num = new_id();
191 response->id_num = htonl(request->id_num);
192 /* insert a new entry into the top of the list */
193 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
194 if (ptr == NIL) {
195 syslog(LOG_ERR, "insert_table: Out of memory");
196 _exit(1);
197 }
198 ptr->time = current_time;
199 ptr->request = *request;
200 ptr->next = table;
201 if (ptr->next != NIL)
202 ptr->next->last = ptr;
203 ptr->last = NIL;
204 table = ptr;
205 }
206
207 /*
208 * Generate a unique non-zero sequence number
209 */
210 int
211 new_id()
212 {
213 static int current_id = 0;
214
215 current_id = (current_id + 1) % MAX_ID;
216 /* 0 is reserved, helps to pick up bugs */
217 if (current_id == 0)
218 current_id = 1;
219 return (current_id);
220 }
221
222 /*
223 * Delete the invitation with id 'id_num'
224 */
225 int
226 delete_invite(id_num)
227 int id_num;
228 {
229 register TABLE_ENTRY *ptr;
230
231 ptr = table;
232 if (debug)
233 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
234 for (ptr = table; ptr != NIL; ptr = ptr->next) {
235 if (ptr->request.id_num == id_num)
236 break;
237 if (debug)
238 print_request("", &ptr->request);
239 }
240 if (ptr != NIL) {
241 delete(ptr);
242 return (SUCCESS);
243 }
244 return (NOT_HERE);
245 }
246
247 /*
248 * Classic delete from a double-linked list
249 */
250 void
251 delete(ptr)
252 register TABLE_ENTRY *ptr;
253 {
254
255 if (debug)
256 print_request("delete", &ptr->request);
257 if (table == ptr)
258 table = ptr->next;
259 else if (ptr->last != NIL)
260 ptr->last->next = ptr->next;
261 if (ptr->next != NIL)
262 ptr->next->last = ptr->last;
263 free((char *)ptr);
264 }