xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.talkd/table.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28*0Sstevel@tonic-gate  * All Rights Reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
33*0Sstevel@tonic-gate  * The Regents of the University of California.
34*0Sstevel@tonic-gate  * All Rights Reserved.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
37*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
38*0Sstevel@tonic-gate  * contributors.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * Routines to handle insertion, deletion, etc on the table
46*0Sstevel@tonic-gate  * of requests kept by the daemon. Nothing fancy here, linear
47*0Sstevel@tonic-gate  * search on a double-linked list. A time is kept with each
48*0Sstevel@tonic-gate  * entry so that overly old invitations can be eliminated.
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * Consider this a mis-guided attempt at modularity
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <sys/time.h>
54*0Sstevel@tonic-gate #include <string.h>
55*0Sstevel@tonic-gate #include <stdio.h>
56*0Sstevel@tonic-gate #include <malloc.h>
57*0Sstevel@tonic-gate #include "talkd_impl.h"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate typedef struct table_entry TABLE_ENTRY;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate struct table_entry {
64*0Sstevel@tonic-gate     CTL_MSG request;
65*0Sstevel@tonic-gate     long time;
66*0Sstevel@tonic-gate     TABLE_ENTRY *next;
67*0Sstevel@tonic-gate     TABLE_ENTRY *last;
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static struct timeval tp;
71*0Sstevel@tonic-gate static TABLE_ENTRY *table = NULL;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void delete(TABLE_ENTRY *ptr);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * Look in the table for an invitation that matches the current
77*0Sstevel@tonic-gate  * request looking for an invitation.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate CTL_MSG *
find_match(CTL_MSG * request)81*0Sstevel@tonic-gate find_match(CTL_MSG *request)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
84*0Sstevel@tonic-gate 	TABLE_ENTRY *prevp;
85*0Sstevel@tonic-gate 	long current_time;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
88*0Sstevel@tonic-gate 	current_time = tp.tv_sec;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	ptr = table;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (debug) {
93*0Sstevel@tonic-gate 		(void) printf("Entering Look-Up with : \n");
94*0Sstevel@tonic-gate 		print_request(request);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	while (ptr != NULL) {
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 		if ((ptr->time - current_time) > MAX_LIFE) {
100*0Sstevel@tonic-gate 		/* the entry is too old */
101*0Sstevel@tonic-gate 			if (debug) {
102*0Sstevel@tonic-gate 				(void) printf("Deleting expired entry : \n");
103*0Sstevel@tonic-gate 				print_request(&ptr->request);
104*0Sstevel@tonic-gate 			}
105*0Sstevel@tonic-gate 			prevp = ptr;
106*0Sstevel@tonic-gate 			ptr = ptr->next;
107*0Sstevel@tonic-gate 			delete(prevp);
108*0Sstevel@tonic-gate 			continue;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		if (debug)
112*0Sstevel@tonic-gate 			print_request(&ptr->request);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
115*0Sstevel@tonic-gate 		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
116*0Sstevel@tonic-gate 		    ptr->request.type == LEAVE_INVITE) {
117*0Sstevel@tonic-gate 			return (&ptr->request);
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		ptr = ptr->next;
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	return (NULL);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * Look for an identical request, as opposed to a complimentary
128*0Sstevel@tonic-gate  * one as find_match does.
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate CTL_MSG *
find_request(CTL_MSG * request)132*0Sstevel@tonic-gate find_request(CTL_MSG *request)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
135*0Sstevel@tonic-gate 	TABLE_ENTRY *prevp;
136*0Sstevel@tonic-gate 	long current_time;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
139*0Sstevel@tonic-gate 	current_time = tp.tv_sec;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * See if this is a repeated message, and check for
143*0Sstevel@tonic-gate 	 * out of date entries in the table while we are it.
144*0Sstevel@tonic-gate 	 */
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	ptr = table;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (debug) {
149*0Sstevel@tonic-gate 		(void) printf("Entering find_request with : \n");
150*0Sstevel@tonic-gate 		print_request(request);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	while (ptr != NULL) {
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 		if ((ptr->time - current_time) > MAX_LIFE) {
156*0Sstevel@tonic-gate 			/* the entry is too old */
157*0Sstevel@tonic-gate 			if (debug) {
158*0Sstevel@tonic-gate 				(void) printf("Deleting expired entry : \n");
159*0Sstevel@tonic-gate 				print_request(&ptr->request);
160*0Sstevel@tonic-gate 			}
161*0Sstevel@tonic-gate 			prevp = ptr;
162*0Sstevel@tonic-gate 			ptr = ptr->next;
163*0Sstevel@tonic-gate 			delete(prevp);
164*0Sstevel@tonic-gate 			continue;
165*0Sstevel@tonic-gate 		}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 		if (debug)
168*0Sstevel@tonic-gate 			print_request(&ptr->request);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
171*0Sstevel@tonic-gate 		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
172*0Sstevel@tonic-gate 		    request->type == ptr->request.type &&
173*0Sstevel@tonic-gate 		    request->pid == ptr->request.pid) {
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 			/* update the time if we 'touch' it */
176*0Sstevel@tonic-gate 			ptr->time = current_time;
177*0Sstevel@tonic-gate 			return (&ptr->request);
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		ptr = ptr->next;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	return (NULL);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate void
insert_table(CTL_MSG * request,CTL_RESPONSE * response)187*0Sstevel@tonic-gate insert_table(CTL_MSG *request, CTL_RESPONSE *response)
188*0Sstevel@tonic-gate {
189*0Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
190*0Sstevel@tonic-gate 	long current_time;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
193*0Sstevel@tonic-gate 	current_time = tp.tv_sec;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	response->id_num = request->id_num = new_id();
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/*
198*0Sstevel@tonic-gate 	 * Insert a new entry into the top of the list.
199*0Sstevel@tonic-gate 	 */
200*0Sstevel@tonic-gate 	ptr = (TABLE_ENTRY *) malloc(sizeof (TABLE_ENTRY));
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (ptr == NULL) {
203*0Sstevel@tonic-gate 		print_error("malloc in insert_table");
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	ptr->time = current_time;
207*0Sstevel@tonic-gate 	ptr->request = *request;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	ptr->next = table;
210*0Sstevel@tonic-gate 	if (ptr->next != NULL) {
211*0Sstevel@tonic-gate 		ptr->next->last = ptr;
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	ptr->last = NULL;
214*0Sstevel@tonic-gate 	table = ptr;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*
218*0Sstevel@tonic-gate  * Generate a unique non-zero sequence number.
219*0Sstevel@tonic-gate  */
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate int
new_id(void)222*0Sstevel@tonic-gate new_id(void)
223*0Sstevel@tonic-gate {
224*0Sstevel@tonic-gate 	static int current_id = 0;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	current_id = (current_id + 1) % MAX_ID;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	/* 0 is reserved, helps to pick up bugs */
229*0Sstevel@tonic-gate 	if (current_id == 0)
230*0Sstevel@tonic-gate 		current_id = 1;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return (current_id);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate /*
236*0Sstevel@tonic-gate  * Delete the invitation with id 'id_num'.
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate int
delete_invite(int id_num)240*0Sstevel@tonic-gate delete_invite(int id_num)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	ptr = table;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (debug)
247*0Sstevel@tonic-gate 		(void) printf("Entering delete_invite with %d\n", id_num);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	while (ptr != NULL && ptr->request.id_num != id_num) {
250*0Sstevel@tonic-gate 		if (debug)
251*0Sstevel@tonic-gate 			print_request(&ptr->request);
252*0Sstevel@tonic-gate 			ptr = ptr->next;
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	if (ptr != NULL) {
256*0Sstevel@tonic-gate 		delete(ptr);
257*0Sstevel@tonic-gate 		return (SUCCESS);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	return (NOT_HERE);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate /*
264*0Sstevel@tonic-gate  * Classic delete from a double-linked list.
265*0Sstevel@tonic-gate  */
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate static void
delete(TABLE_ENTRY * ptr)268*0Sstevel@tonic-gate delete(TABLE_ENTRY *ptr)
269*0Sstevel@tonic-gate {
270*0Sstevel@tonic-gate 	if (debug) {
271*0Sstevel@tonic-gate 		(void) printf("Deleting : ");
272*0Sstevel@tonic-gate 		print_request(&ptr->request);
273*0Sstevel@tonic-gate 	}
274*0Sstevel@tonic-gate 	if (table == ptr) {
275*0Sstevel@tonic-gate 		table = ptr->next;
276*0Sstevel@tonic-gate 	} else if (ptr->last != NULL) {
277*0Sstevel@tonic-gate 		ptr->last->next = ptr->next;
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	if (ptr->next != NULL) {
281*0Sstevel@tonic-gate 		ptr->next->last = ptr->last;
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	free(ptr);
285*0Sstevel@tonic-gate }
286