dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / in.talkd / table.c
blob849f7c6410748553243addffa7a59b0d6f4422f4
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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
38 * contributors.
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
50 #include <sys/time.h>
51 #include <string.h>
52 #include <stdio.h>
53 #include <malloc.h>
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;
60 struct table_entry {
61 CTL_MSG request;
62 long time;
63 TABLE_ENTRY *next;
64 TABLE_ENTRY *last;
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.
77 CTL_MSG *
78 find_match(CTL_MSG *request)
80 TABLE_ENTRY *ptr;
81 TABLE_ENTRY *prevp;
82 long current_time;
84 (void) gettimeofday(&tp, NULL);
85 current_time = tp.tv_sec;
87 ptr = table;
89 if (debug) {
90 (void) printf("Entering Look-Up with : \n");
91 print_request(request);
94 while (ptr != NULL) {
96 if ((ptr->time - current_time) > MAX_LIFE) {
97 /* the entry is too old */
98 if (debug) {
99 (void) printf("Deleting expired entry : \n");
100 print_request(&ptr->request);
102 prevp = ptr;
103 ptr = ptr->next;
104 delete(prevp);
105 continue;
108 if (debug)
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);
117 ptr = ptr->next;
120 return (NULL);
124 * Look for an identical request, as opposed to a complimentary
125 * one as find_match does.
128 CTL_MSG *
129 find_request(CTL_MSG *request)
131 TABLE_ENTRY *ptr;
132 TABLE_ENTRY *prevp;
133 long current_time;
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.
143 ptr = table;
145 if (debug) {
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 */
154 if (debug) {
155 (void) printf("Deleting expired entry : \n");
156 print_request(&ptr->request);
158 prevp = ptr;
159 ptr = ptr->next;
160 delete(prevp);
161 continue;
164 if (debug)
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);
177 ptr = ptr->next;
180 return (NULL);
183 void
184 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
186 TABLE_ENTRY *ptr;
187 long current_time;
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));
199 if (ptr == NULL) {
200 print_error("malloc in insert_table");
203 ptr->time = current_time;
204 ptr->request = *request;
206 ptr->next = table;
207 if (ptr->next != NULL) {
208 ptr->next->last = ptr;
210 ptr->last = NULL;
211 table = ptr;
215 * Generate a unique non-zero sequence number.
219 new_id(void)
221 static int current_id = 0;
223 current_id = (current_id + 1) % MAX_ID;
225 /* 0 is reserved, helps to pick up bugs */
226 if (current_id == 0)
227 current_id = 1;
229 return (current_id);
233 * Delete the invitation with id 'id_num'.
237 delete_invite(int id_num)
239 TABLE_ENTRY *ptr;
241 ptr = table;
243 if (debug)
244 (void) printf("Entering delete_invite with %d\n", id_num);
246 while (ptr != NULL && ptr->request.id_num != id_num) {
247 if (debug)
248 print_request(&ptr->request);
249 ptr = ptr->next;
252 if (ptr != NULL) {
253 delete(ptr);
254 return (SUCCESS);
257 return (NOT_HERE);
261 * Classic delete from a double-linked list.
264 static void
265 delete(TABLE_ENTRY *ptr)
267 if (debug) {
268 (void) printf("Deleting : ");
269 print_request(&ptr->request);
271 if (table == ptr) {
272 table = ptr->next;
273 } else if (ptr->last != NULL) {
274 ptr->last->next = ptr->next;
277 if (ptr->next != NULL) {
278 ptr->next->last = ptr->last;
281 free(ptr);