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