17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  * All Rights Reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California.
347c478bd9Sstevel@tonic-gate  * All Rights Reserved.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Routines to handle insertion, deletion, etc on the table
437c478bd9Sstevel@tonic-gate  * of requests kept by the daemon. Nothing fancy here, linear
447c478bd9Sstevel@tonic-gate  * search on a double-linked list. A time is kept with each
457c478bd9Sstevel@tonic-gate  * entry so that overly old invitations can be eliminated.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * Consider this a mis-guided attempt at modularity
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/time.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <stdio.h>
537c478bd9Sstevel@tonic-gate #include <malloc.h>
547c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate typedef struct table_entry TABLE_ENTRY;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate struct table_entry {
617c478bd9Sstevel@tonic-gate     CTL_MSG request;
627c478bd9Sstevel@tonic-gate     long time;
637c478bd9Sstevel@tonic-gate     TABLE_ENTRY *next;
647c478bd9Sstevel@tonic-gate     TABLE_ENTRY *last;
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static struct timeval tp;
687c478bd9Sstevel@tonic-gate static TABLE_ENTRY *table = NULL;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static void delete(TABLE_ENTRY *ptr);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Look in the table for an invitation that matches the current
747c478bd9Sstevel@tonic-gate  * request looking for an invitation.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate CTL_MSG *
find_match(CTL_MSG * request)787c478bd9Sstevel@tonic-gate find_match(CTL_MSG *request)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
817c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *prevp;
827c478bd9Sstevel@tonic-gate 	long current_time;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
857c478bd9Sstevel@tonic-gate 	current_time = tp.tv_sec;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	ptr = table;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (debug) {
907c478bd9Sstevel@tonic-gate 		(void) printf("Entering Look-Up with : \n");
917c478bd9Sstevel@tonic-gate 		print_request(request);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		if ((ptr->time - current_time) > MAX_LIFE) {
977c478bd9Sstevel@tonic-gate 		/* the entry is too old */
987c478bd9Sstevel@tonic-gate 			if (debug) {
997c478bd9Sstevel@tonic-gate 				(void) printf("Deleting expired entry : \n");
1007c478bd9Sstevel@tonic-gate 				print_request(&ptr->request);
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 			prevp = ptr;
1037c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
1047c478bd9Sstevel@tonic-gate 			delete(prevp);
1057c478bd9Sstevel@tonic-gate 			continue;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		if (debug)
1097c478bd9Sstevel@tonic-gate 			print_request(&ptr->request);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
1127c478bd9Sstevel@tonic-gate 		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
1137c478bd9Sstevel@tonic-gate 		    ptr->request.type == LEAVE_INVITE) {
1147c478bd9Sstevel@tonic-gate 			return (&ptr->request);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (NULL);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Look for an identical request, as opposed to a complimentary
1257c478bd9Sstevel@tonic-gate  * one as find_match does.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate CTL_MSG *
find_request(CTL_MSG * request)1297c478bd9Sstevel@tonic-gate find_request(CTL_MSG *request)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
1327c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *prevp;
1337c478bd9Sstevel@tonic-gate 	long current_time;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
1367c478bd9Sstevel@tonic-gate 	current_time = tp.tv_sec;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * See if this is a repeated message, and check for
1407c478bd9Sstevel@tonic-gate 	 * out of date entries in the table while we are it.
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	ptr = table;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (debug) {
1467c478bd9Sstevel@tonic-gate 		(void) printf("Entering find_request with : \n");
1477c478bd9Sstevel@tonic-gate 		print_request(request);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if ((ptr->time - current_time) > MAX_LIFE) {
1537c478bd9Sstevel@tonic-gate 			/* the entry is too old */
1547c478bd9Sstevel@tonic-gate 			if (debug) {
1557c478bd9Sstevel@tonic-gate 				(void) printf("Deleting expired entry : \n");
1567c478bd9Sstevel@tonic-gate 				print_request(&ptr->request);
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 			prevp = ptr;
1597c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
1607c478bd9Sstevel@tonic-gate 			delete(prevp);
1617c478bd9Sstevel@tonic-gate 			continue;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		if (debug)
1657c478bd9Sstevel@tonic-gate 			print_request(&ptr->request);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
1687c478bd9Sstevel@tonic-gate 		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
1697c478bd9Sstevel@tonic-gate 		    request->type == ptr->request.type &&
1707c478bd9Sstevel@tonic-gate 		    request->pid == ptr->request.pid) {
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			/* update the time if we 'touch' it */
1737c478bd9Sstevel@tonic-gate 			ptr->time = current_time;
1747c478bd9Sstevel@tonic-gate 			return (&ptr->request);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return (NULL);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate void
insert_table(CTL_MSG * request,CTL_RESPONSE * response)1847c478bd9Sstevel@tonic-gate insert_table(CTL_MSG *request, CTL_RESPONSE *response)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
1877c478bd9Sstevel@tonic-gate 	long current_time;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tp, NULL);
1907c478bd9Sstevel@tonic-gate 	current_time = tp.tv_sec;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	response->id_num = request->id_num = new_id();
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * Insert a new entry into the top of the list.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	ptr = (TABLE_ENTRY *) malloc(sizeof (TABLE_ENTRY));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
2007c478bd9Sstevel@tonic-gate 		print_error("malloc in insert_table");
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	ptr->time = current_time;
2047c478bd9Sstevel@tonic-gate 	ptr->request = *request;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	ptr->next = table;
2077c478bd9Sstevel@tonic-gate 	if (ptr->next != NULL) {
2087c478bd9Sstevel@tonic-gate 		ptr->next->last = ptr;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	ptr->last = NULL;
2117c478bd9Sstevel@tonic-gate 	table = ptr;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * Generate a unique non-zero sequence number.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate int
new_id(void)2197c478bd9Sstevel@tonic-gate new_id(void)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	static int current_id = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	current_id = (current_id + 1) % MAX_ID;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* 0 is reserved, helps to pick up bugs */
2267c478bd9Sstevel@tonic-gate 	if (current_id == 0)
2277c478bd9Sstevel@tonic-gate 		current_id = 1;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (current_id);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Delete the invitation with id 'id_num'.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate int
delete_invite(int id_num)2377c478bd9Sstevel@tonic-gate delete_invite(int id_num)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	TABLE_ENTRY *ptr;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	ptr = table;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (debug)
2447c478bd9Sstevel@tonic-gate 		(void) printf("Entering delete_invite with %d\n", id_num);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	while (ptr != NULL && ptr->request.id_num != id_num) {
2477c478bd9Sstevel@tonic-gate 		if (debug)
2487c478bd9Sstevel@tonic-gate 			print_request(&ptr->request);
249*7adeceedSToomas Soome 		ptr = ptr->next;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
2537c478bd9Sstevel@tonic-gate 		delete(ptr);
2547c478bd9Sstevel@tonic-gate 		return (SUCCESS);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (NOT_HERE);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Classic delete from a double-linked list.
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static void
delete(TABLE_ENTRY * ptr)2657c478bd9Sstevel@tonic-gate delete(TABLE_ENTRY *ptr)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	if (debug) {
2687c478bd9Sstevel@tonic-gate 		(void) printf("Deleting : ");
2697c478bd9Sstevel@tonic-gate 		print_request(&ptr->request);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	if (table == ptr) {
2727c478bd9Sstevel@tonic-gate 		table = ptr->next;
2737c478bd9Sstevel@tonic-gate 	} else if (ptr->last != NULL) {
2747c478bd9Sstevel@tonic-gate 		ptr->last->next = ptr->next;
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (ptr->next != NULL) {
2787c478bd9Sstevel@tonic-gate 		ptr->next->last = ptr->last;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	free(ptr);
2827c478bd9Sstevel@tonic-gate }
283