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