11592Srgrimes/*
21592Srgrimes * Copyright (c) 1983, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
3531491Scharnier#if 0
361592Srgrimesstatic char sccsid[] = "@(#)table.c	8.1 (Berkeley) 6/4/93";
3731491Scharnier#endif
3831491Scharnierstatic const char rcsid[] =
3950476Speter  "$FreeBSD$";
401592Srgrimes#endif /* not lint */
411592Srgrimes
421592Srgrimes/*
431592Srgrimes * Routines to handle insertion, deletion, etc on the table
441592Srgrimes * of requests kept by the daemon. Nothing fancy here, linear
458870Srgrimes * search on a double-linked list. A time is kept with each
461592Srgrimes * entry so that overly old invitations can be eliminated.
471592Srgrimes *
481592Srgrimes * Consider this a mis-guided attempt at modularity
491592Srgrimes */
501592Srgrimes#include <sys/param.h>
511592Srgrimes#include <sys/time.h>
521592Srgrimes#include <sys/socket.h>
53112998Sjmallett#include <netinet/in.h>
541592Srgrimes#include <protocols/talkd.h>
551592Srgrimes#include <stdio.h>
561592Srgrimes#include <stdlib.h>
571592Srgrimes#include <string.h>
5831491Scharnier#include <syslog.h>
5931491Scharnier#include <unistd.h>
601592Srgrimes
6190261Simp#include "extern.h"
6290261Simp
631592Srgrimes#define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
641592Srgrimes
651592Srgrimes#define NIL ((TABLE_ENTRY *)0)
661592Srgrimes
67241777Sedstatic struct timeval tp;
681592Srgrimes
691592Srgrimestypedef struct table_entry TABLE_ENTRY;
701592Srgrimes
711592Srgrimesstruct table_entry {
721592Srgrimes	CTL_MSG request;
731592Srgrimes	long	time;
741592Srgrimes	TABLE_ENTRY *next;
751592Srgrimes	TABLE_ENTRY *last;
761592Srgrimes};
771592Srgrimes
7890261Simpstatic void delete(TABLE_ENTRY *);
7990261Simp
80241777Sedstatic TABLE_ENTRY *table = NIL;
8131491Scharnier
821592Srgrimes/*
831592Srgrimes * Look in the table for an invitation that matches the current
841592Srgrimes * request looking for an invitation
851592Srgrimes */
861592SrgrimesCTL_MSG *
8790261Simpfind_match(CTL_MSG *request)
881592Srgrimes{
8990261Simp	TABLE_ENTRY *ptr;
901592Srgrimes	time_t current_time;
911592Srgrimes
92211056Sed	gettimeofday(&tp, NULL);
931592Srgrimes	current_time = tp.tv_sec;
941592Srgrimes	if (debug)
951592Srgrimes		print_request("find_match", request);
961592Srgrimes	for (ptr = table; ptr != NIL; ptr = ptr->next) {
971592Srgrimes		if ((ptr->time - current_time) > MAX_LIFE) {
981592Srgrimes			/* the entry is too old */
991592Srgrimes			if (debug)
1001592Srgrimes				print_request("deleting expired entry",
1011592Srgrimes				    &ptr->request);
1021592Srgrimes			delete(ptr);
1031592Srgrimes			continue;
1041592Srgrimes		}
1051592Srgrimes		if (debug)
1061592Srgrimes			print_request("", &ptr->request);
1071592Srgrimes		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
1081592Srgrimes		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
1091592Srgrimes		     ptr->request.type == LEAVE_INVITE)
1101592Srgrimes			return (&ptr->request);
1111592Srgrimes	}
1121592Srgrimes	return ((CTL_MSG *)0);
1131592Srgrimes}
1141592Srgrimes
1151592Srgrimes/*
1161592Srgrimes * Look for an identical request, as opposed to a complimentary
1178870Srgrimes * one as find_match does
1181592Srgrimes */
1191592SrgrimesCTL_MSG *
12090261Simpfind_request(CTL_MSG *request)
1211592Srgrimes{
12290261Simp	TABLE_ENTRY *ptr;
1231592Srgrimes	time_t current_time;
1241592Srgrimes
125211056Sed	gettimeofday(&tp, NULL);
1261592Srgrimes	current_time = tp.tv_sec;
1271592Srgrimes	/*
1281592Srgrimes	 * See if this is a repeated message, and check for
1291592Srgrimes	 * out of date entries in the table while we are it.
1301592Srgrimes	 */
1311592Srgrimes	if (debug)
1321592Srgrimes		print_request("find_request", request);
1331592Srgrimes	for (ptr = table; ptr != NIL; ptr = ptr->next) {
1341592Srgrimes		if ((ptr->time - current_time) > MAX_LIFE) {
1351592Srgrimes			/* the entry is too old */
1361592Srgrimes			if (debug)
1371592Srgrimes				print_request("deleting expired entry",
1381592Srgrimes				    &ptr->request);
1391592Srgrimes			delete(ptr);
1401592Srgrimes			continue;
1411592Srgrimes		}
1421592Srgrimes		if (debug)
1431592Srgrimes			print_request("", &ptr->request);
1441592Srgrimes		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
1451592Srgrimes		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
1461592Srgrimes		    request->type == ptr->request.type &&
1471592Srgrimes		    request->pid == ptr->request.pid) {
1481592Srgrimes			/* update the time if we 'touch' it */
1491592Srgrimes			ptr->time = current_time;
1501592Srgrimes			return (&ptr->request);
1511592Srgrimes		}
1521592Srgrimes	}
1531592Srgrimes	return ((CTL_MSG *)0);
1541592Srgrimes}
1551592Srgrimes
15631491Scharniervoid
15790261Simpinsert_table(CTL_MSG *request, CTL_RESPONSE *response)
1581592Srgrimes{
15990261Simp	TABLE_ENTRY *ptr;
1601592Srgrimes	time_t current_time;
1611592Srgrimes
162211056Sed	gettimeofday(&tp, NULL);
1631592Srgrimes	current_time = tp.tv_sec;
1641592Srgrimes	request->id_num = new_id();
1651592Srgrimes	response->id_num = htonl(request->id_num);
1661592Srgrimes	/* insert a new entry into the top of the list */
1671592Srgrimes	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
1681592Srgrimes	if (ptr == NIL) {
1691592Srgrimes		syslog(LOG_ERR, "insert_table: Out of memory");
1701592Srgrimes		_exit(1);
1711592Srgrimes	}
1721592Srgrimes	ptr->time = current_time;
1731592Srgrimes	ptr->request = *request;
1741592Srgrimes	ptr->next = table;
1751592Srgrimes	if (ptr->next != NIL)
1761592Srgrimes		ptr->next->last = ptr;
1771592Srgrimes	ptr->last = NIL;
1781592Srgrimes	table = ptr;
1791592Srgrimes}
1801592Srgrimes
1811592Srgrimes/*
1821592Srgrimes * Generate a unique non-zero sequence number
1831592Srgrimes */
18431491Scharnierint
18590261Simpnew_id(void)
1861592Srgrimes{
1871592Srgrimes	static int current_id = 0;
1881592Srgrimes
1891592Srgrimes	current_id = (current_id + 1) % MAX_ID;
1901592Srgrimes	/* 0 is reserved, helps to pick up bugs */
1911592Srgrimes	if (current_id == 0)
1921592Srgrimes		current_id = 1;
1931592Srgrimes	return (current_id);
1941592Srgrimes}
1951592Srgrimes
1961592Srgrimes/*
1971592Srgrimes * Delete the invitation with id 'id_num'
1981592Srgrimes */
19931491Scharnierint
200112998Sjmallettdelete_invite(u_int32_t id_num)
2011592Srgrimes{
20290261Simp	TABLE_ENTRY *ptr;
2031592Srgrimes
2041592Srgrimes	ptr = table;
2051592Srgrimes	if (debug)
2061592Srgrimes		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
2071592Srgrimes	for (ptr = table; ptr != NIL; ptr = ptr->next) {
2081592Srgrimes		if (ptr->request.id_num == id_num)
2091592Srgrimes			break;
2101592Srgrimes		if (debug)
2111592Srgrimes			print_request("", &ptr->request);
2121592Srgrimes	}
2131592Srgrimes	if (ptr != NIL) {
2141592Srgrimes		delete(ptr);
2151592Srgrimes		return (SUCCESS);
2161592Srgrimes	}
2171592Srgrimes	return (NOT_HERE);
2181592Srgrimes}
2191592Srgrimes
2201592Srgrimes/*
2211592Srgrimes * Classic delete from a double-linked list
2221592Srgrimes */
22390261Simpstatic void
22490261Simpdelete(TABLE_ENTRY *ptr)
2251592Srgrimes{
2261592Srgrimes
2271592Srgrimes	if (debug)
2281592Srgrimes		print_request("delete", &ptr->request);
2291592Srgrimes	if (table == ptr)
2301592Srgrimes		table = ptr->next;
2311592Srgrimes	else if (ptr->last != NIL)
2321592Srgrimes		ptr->last->next = ptr->next;
2331592Srgrimes	if (ptr->next != NIL)
2341592Srgrimes		ptr->next->last = ptr->last;
2351592Srgrimes	free((char *)ptr);
2361592Srgrimes}
237