4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28 * All Rights Reserved.
32 * University Copyright- Copyright (c) 1982, 1986, 1988
33 * The Regents of the University of California.
34 * All Rights Reserved.
36 * University Acknowledgment- Portions of this document are derived from
37 * software developed by the University of California, Berkeley, and its
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
54 #include "talkd_impl.h"
56 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
58 typedef struct table_entry TABLE_ENTRY
;
67 static struct timeval tp
;
68 static TABLE_ENTRY
*table
= NULL
;
70 static void delete(TABLE_ENTRY
*ptr
);
73 * Look in the table for an invitation that matches the current
74 * request looking for an invitation.
78 find_match(CTL_MSG
*request
)
84 (void) gettimeofday(&tp
, NULL
);
85 current_time
= tp
.tv_sec
;
90 (void) printf("Entering Look-Up with : \n");
91 print_request(request
);
96 if ((ptr
->time
- current_time
) > MAX_LIFE
) {
97 /* the entry is too old */
99 (void) printf("Deleting expired entry : \n");
100 print_request(&ptr
->request
);
109 print_request(&ptr
->request
);
111 if (strcmp(request
->l_name
, ptr
->request
.r_name
) == 0 &&
112 strcmp(request
->r_name
, ptr
->request
.l_name
) == 0 &&
113 ptr
->request
.type
== LEAVE_INVITE
) {
114 return (&ptr
->request
);
124 * Look for an identical request, as opposed to a complimentary
125 * one as find_match does.
129 find_request(CTL_MSG
*request
)
135 (void) gettimeofday(&tp
, NULL
);
136 current_time
= tp
.tv_sec
;
139 * See if this is a repeated message, and check for
140 * out of date entries in the table while we are it.
146 (void) printf("Entering find_request with : \n");
147 print_request(request
);
150 while (ptr
!= NULL
) {
152 if ((ptr
->time
- current_time
) > MAX_LIFE
) {
153 /* the entry is too old */
155 (void) printf("Deleting expired entry : \n");
156 print_request(&ptr
->request
);
165 print_request(&ptr
->request
);
167 if (strcmp(request
->r_name
, ptr
->request
.r_name
) == 0 &&
168 strcmp(request
->l_name
, ptr
->request
.l_name
) == 0 &&
169 request
->type
== ptr
->request
.type
&&
170 request
->pid
== ptr
->request
.pid
) {
172 /* update the time if we 'touch' it */
173 ptr
->time
= current_time
;
174 return (&ptr
->request
);
184 insert_table(CTL_MSG
*request
, CTL_RESPONSE
*response
)
189 (void) gettimeofday(&tp
, NULL
);
190 current_time
= tp
.tv_sec
;
192 response
->id_num
= request
->id_num
= new_id();
195 * Insert a new entry into the top of the list.
197 ptr
= (TABLE_ENTRY
*) malloc(sizeof (TABLE_ENTRY
));
200 print_error("malloc in insert_table");
203 ptr
->time
= current_time
;
204 ptr
->request
= *request
;
207 if (ptr
->next
!= NULL
) {
208 ptr
->next
->last
= ptr
;
215 * Generate a unique non-zero sequence number.
221 static int current_id
= 0;
223 current_id
= (current_id
+ 1) % MAX_ID
;
225 /* 0 is reserved, helps to pick up bugs */
233 * Delete the invitation with id 'id_num'.
237 delete_invite(int id_num
)
244 (void) printf("Entering delete_invite with %d\n", id_num
);
246 while (ptr
!= NULL
&& ptr
->request
.id_num
!= id_num
) {
248 print_request(&ptr
->request
);
261 * Classic delete from a double-linked list.
265 delete(TABLE_ENTRY
*ptr
)
268 (void) printf("Deleting : ");
269 print_request(&ptr
->request
);
273 } else if (ptr
->last
!= NULL
) {
274 ptr
->last
->next
= ptr
->next
;
277 if (ptr
->next
!= NULL
) {
278 ptr
->next
->last
= ptr
->last
;