]>
git.saurik.com Git - apple/network_cmds.git/blob - talkd.tproj/table.c
5b6ee3f49f86764d79e40c2b3eafe90b7cbcc56b
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
59 static char sccsid
[] = "@(#)table.c 8.1 (Berkeley) 6/4/93";
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.
68 * Consider this a mis-guided attempt at modularity
70 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <protocols/talkd.h>
81 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
83 #define NIL ((TABLE_ENTRY *)0)
89 typedef struct table_entry TABLE_ENTRY
;
98 TABLE_ENTRY
*table
= NIL
;
99 CTL_MSG
*find_request();
100 CTL_MSG
*find_match();
102 void delete __P((TABLE_ENTRY
*ptr
));
105 * Look in the table for an invitation that matches the current
106 * request looking for an invitation
110 register CTL_MSG
*request
;
112 register TABLE_ENTRY
*ptr
;
115 gettimeofday(&tp
, &txp
);
116 current_time
= tp
.tv_sec
;
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 */
123 print_request("deleting expired entry",
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
);
135 return ((CTL_MSG
*)0);
139 * Look for an identical request, as opposed to a complimentary
140 * one as find_match does
143 find_request(request
)
144 register CTL_MSG
*request
;
146 register TABLE_ENTRY
*ptr
;
149 gettimeofday(&tp
, &txp
);
150 current_time
= tp
.tv_sec
;
152 * See if this is a repeated message, and check for
153 * out of date entries in the table while we are it.
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 */
161 print_request("deleting expired entry",
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
);
177 return ((CTL_MSG
*)0);
181 insert_table(request
, response
)
183 CTL_RESPONSE
*response
;
185 register TABLE_ENTRY
*ptr
;
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
));
195 syslog(LOG_ERR
, "insert_table: Out of memory");
198 ptr
->time
= current_time
;
199 ptr
->request
= *request
;
201 if (ptr
->next
!= NIL
)
202 ptr
->next
->last
= ptr
;
208 * Generate a unique non-zero sequence number
213 static int current_id
= 0;
215 current_id
= (current_id
+ 1) % MAX_ID
;
216 /* 0 is reserved, helps to pick up bugs */
223 * Delete the invitation with id 'id_num'
226 delete_invite(id_num
)
229 register TABLE_ENTRY
*ptr
;
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
)
238 print_request("", &ptr
->request
);
248 * Classic delete from a double-linked list
252 register TABLE_ENTRY
*ptr
;
256 print_request("delete", &ptr
->request
);
259 else if (ptr
->last
!= NIL
)
260 ptr
->last
->next
= ptr
->next
;
261 if (ptr
->next
!= NIL
)
262 ptr
->next
->last
= ptr
->last
;