Home | History | Annotate | Line # | Download | only in talkd
table.c revision 1.7
      1  1.7  dholland /*	$NetBSD: table.c,v 1.7 2008/03/04 02:57:33 dholland Exp $	*/
      2  1.3  christos 
      3  1.1       cgd /*
      4  1.3  christos  * Copyright (c) 1983, 1993
      5  1.3  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.6       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1       cgd  *    may be used to endorse or promote products derived from this software
     17  1.1       cgd  *    without specific prior written permission.
     18  1.1       cgd  *
     19  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1       cgd  * SUCH DAMAGE.
     30  1.1       cgd  */
     31  1.1       cgd 
     32  1.3  christos #include <sys/cdefs.h>
     33  1.1       cgd #ifndef lint
     34  1.3  christos #if 0
     35  1.3  christos static char sccsid[] = "@(#)table.c	8.1 (Berkeley) 6/4/93";
     36  1.3  christos #else
     37  1.7  dholland __RCSID("$NetBSD: table.c,v 1.7 2008/03/04 02:57:33 dholland Exp $");
     38  1.3  christos #endif
     39  1.1       cgd #endif /* not lint */
     40  1.1       cgd 
     41  1.1       cgd /*
     42  1.1       cgd  * Routines to handle insertion, deletion, etc on the table
     43  1.1       cgd  * of requests kept by the daemon. Nothing fancy here, linear
     44  1.1       cgd  * search on a double-linked list. A time is kept with each
     45  1.1       cgd  * entry so that overly old invitations can be eliminated.
     46  1.1       cgd  *
     47  1.1       cgd  * Consider this a mis-guided attempt at modularity
     48  1.1       cgd  */
     49  1.1       cgd #include <sys/param.h>
     50  1.1       cgd #include <sys/time.h>
     51  1.1       cgd #include <sys/socket.h>
     52  1.1       cgd #include <protocols/talkd.h>
     53  1.1       cgd #include <syslog.h>
     54  1.1       cgd #include <unistd.h>
     55  1.1       cgd #include <stdio.h>
     56  1.1       cgd #include <stdlib.h>
     57  1.1       cgd #include <string.h>
     58  1.4  christos #include "extern.h"
     59  1.1       cgd 
     60  1.1       cgd #define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
     61  1.1       cgd 
     62  1.1       cgd #define NIL ((TABLE_ENTRY *)0)
     63  1.1       cgd 
     64  1.1       cgd struct	timeval tp;
     65  1.1       cgd 
     66  1.1       cgd typedef struct table_entry TABLE_ENTRY;
     67  1.1       cgd 
     68  1.1       cgd struct table_entry {
     69  1.1       cgd 	CTL_MSG request;
     70  1.1       cgd 	long	time;
     71  1.1       cgd 	TABLE_ENTRY *next;
     72  1.1       cgd 	TABLE_ENTRY *last;
     73  1.1       cgd };
     74  1.1       cgd 
     75  1.1       cgd TABLE_ENTRY *table = NIL;
     76  1.4  christos 
     77  1.4  christos static void delete __P((TABLE_ENTRY *));
     78  1.1       cgd 
     79  1.1       cgd /*
     80  1.1       cgd  * Look in the table for an invitation that matches the current
     81  1.1       cgd  * request looking for an invitation
     82  1.1       cgd  */
     83  1.1       cgd CTL_MSG *
     84  1.1       cgd find_match(request)
     85  1.4  christos 	CTL_MSG *request;
     86  1.1       cgd {
     87  1.4  christos 	TABLE_ENTRY *ptr;
     88  1.1       cgd 	time_t current_time;
     89  1.1       cgd 
     90  1.7  dholland 	gettimeofday(&tp, NULL);
     91  1.1       cgd 	current_time = tp.tv_sec;
     92  1.1       cgd 	if (debug)
     93  1.1       cgd 		print_request("find_match", request);
     94  1.1       cgd 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
     95  1.1       cgd 		if ((ptr->time - current_time) > MAX_LIFE) {
     96  1.1       cgd 			/* the entry is too old */
     97  1.1       cgd 			if (debug)
     98  1.1       cgd 				print_request("deleting expired entry",
     99  1.1       cgd 				    &ptr->request);
    100  1.1       cgd 			delete(ptr);
    101  1.1       cgd 			continue;
    102  1.1       cgd 		}
    103  1.1       cgd 		if (debug)
    104  1.1       cgd 			print_request("", &ptr->request);
    105  1.1       cgd 		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
    106  1.1       cgd 		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
    107  1.1       cgd 		     ptr->request.type == LEAVE_INVITE)
    108  1.1       cgd 			return (&ptr->request);
    109  1.1       cgd 	}
    110  1.1       cgd 	return ((CTL_MSG *)0);
    111  1.1       cgd }
    112  1.1       cgd 
    113  1.1       cgd /*
    114  1.1       cgd  * Look for an identical request, as opposed to a complimentary
    115  1.1       cgd  * one as find_match does
    116  1.1       cgd  */
    117  1.1       cgd CTL_MSG *
    118  1.1       cgd find_request(request)
    119  1.4  christos 	CTL_MSG *request;
    120  1.1       cgd {
    121  1.4  christos 	TABLE_ENTRY *ptr;
    122  1.1       cgd 	time_t current_time;
    123  1.1       cgd 
    124  1.7  dholland 	gettimeofday(&tp, NULL);
    125  1.1       cgd 	current_time = tp.tv_sec;
    126  1.1       cgd 	/*
    127  1.1       cgd 	 * See if this is a repeated message, and check for
    128  1.1       cgd 	 * out of date entries in the table while we are it.
    129  1.1       cgd 	 */
    130  1.1       cgd 	if (debug)
    131  1.1       cgd 		print_request("find_request", request);
    132  1.1       cgd 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
    133  1.1       cgd 		if ((ptr->time - current_time) > MAX_LIFE) {
    134  1.1       cgd 			/* the entry is too old */
    135  1.1       cgd 			if (debug)
    136  1.1       cgd 				print_request("deleting expired entry",
    137  1.1       cgd 				    &ptr->request);
    138  1.1       cgd 			delete(ptr);
    139  1.1       cgd 			continue;
    140  1.1       cgd 		}
    141  1.1       cgd 		if (debug)
    142  1.1       cgd 			print_request("", &ptr->request);
    143  1.1       cgd 		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
    144  1.1       cgd 		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
    145  1.1       cgd 		    request->type == ptr->request.type &&
    146  1.1       cgd 		    request->pid == ptr->request.pid) {
    147  1.1       cgd 			/* update the time if we 'touch' it */
    148  1.1       cgd 			ptr->time = current_time;
    149  1.1       cgd 			return (&ptr->request);
    150  1.1       cgd 		}
    151  1.1       cgd 	}
    152  1.1       cgd 	return ((CTL_MSG *)0);
    153  1.1       cgd }
    154  1.1       cgd 
    155  1.4  christos void
    156  1.1       cgd insert_table(request, response)
    157  1.1       cgd 	CTL_MSG *request;
    158  1.1       cgd 	CTL_RESPONSE *response;
    159  1.1       cgd {
    160  1.4  christos 	TABLE_ENTRY *ptr;
    161  1.1       cgd 	time_t current_time;
    162  1.1       cgd 
    163  1.7  dholland 	gettimeofday(&tp, NULL);
    164  1.1       cgd 	current_time = tp.tv_sec;
    165  1.1       cgd 	request->id_num = new_id();
    166  1.1       cgd 	response->id_num = htonl(request->id_num);
    167  1.1       cgd 	/* insert a new entry into the top of the list */
    168  1.1       cgd 	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
    169  1.1       cgd 	if (ptr == NIL) {
    170  1.1       cgd 		syslog(LOG_ERR, "insert_table: Out of memory");
    171  1.1       cgd 		_exit(1);
    172  1.1       cgd 	}
    173  1.1       cgd 	ptr->time = current_time;
    174  1.1       cgd 	ptr->request = *request;
    175  1.1       cgd 	ptr->next = table;
    176  1.1       cgd 	if (ptr->next != NIL)
    177  1.1       cgd 		ptr->next->last = ptr;
    178  1.1       cgd 	ptr->last = NIL;
    179  1.1       cgd 	table = ptr;
    180  1.1       cgd }
    181  1.1       cgd 
    182  1.1       cgd /*
    183  1.1       cgd  * Generate a unique non-zero sequence number
    184  1.1       cgd  */
    185  1.4  christos int
    186  1.1       cgd new_id()
    187  1.1       cgd {
    188  1.1       cgd 	static int current_id = 0;
    189  1.1       cgd 
    190  1.1       cgd 	current_id = (current_id + 1) % MAX_ID;
    191  1.1       cgd 	/* 0 is reserved, helps to pick up bugs */
    192  1.1       cgd 	if (current_id == 0)
    193  1.1       cgd 		current_id = 1;
    194  1.1       cgd 	return (current_id);
    195  1.1       cgd }
    196  1.1       cgd 
    197  1.1       cgd /*
    198  1.1       cgd  * Delete the invitation with id 'id_num'
    199  1.1       cgd  */
    200  1.4  christos int
    201  1.1       cgd delete_invite(id_num)
    202  1.1       cgd 	int id_num;
    203  1.1       cgd {
    204  1.4  christos 	TABLE_ENTRY *ptr;
    205  1.1       cgd 
    206  1.1       cgd 	ptr = table;
    207  1.1       cgd 	if (debug)
    208  1.1       cgd 		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
    209  1.1       cgd 	for (ptr = table; ptr != NIL; ptr = ptr->next) {
    210  1.1       cgd 		if (ptr->request.id_num == id_num)
    211  1.1       cgd 			break;
    212  1.1       cgd 		if (debug)
    213  1.1       cgd 			print_request("", &ptr->request);
    214  1.1       cgd 	}
    215  1.1       cgd 	if (ptr != NIL) {
    216  1.1       cgd 		delete(ptr);
    217  1.1       cgd 		return (SUCCESS);
    218  1.1       cgd 	}
    219  1.1       cgd 	return (NOT_HERE);
    220  1.1       cgd }
    221  1.1       cgd 
    222  1.1       cgd /*
    223  1.1       cgd  * Classic delete from a double-linked list
    224  1.1       cgd  */
    225  1.4  christos static void
    226  1.1       cgd delete(ptr)
    227  1.4  christos 	TABLE_ENTRY *ptr;
    228  1.1       cgd {
    229  1.1       cgd 
    230  1.1       cgd 	if (debug)
    231  1.1       cgd 		print_request("delete", &ptr->request);
    232  1.1       cgd 	if (table == ptr)
    233  1.1       cgd 		table = ptr->next;
    234  1.1       cgd 	else if (ptr->last != NIL)
    235  1.1       cgd 		ptr->last->next = ptr->next;
    236  1.1       cgd 	if (ptr->next != NIL)
    237  1.1       cgd 		ptr->next->last = ptr->last;
    238  1.1       cgd 	free((char *)ptr);
    239  1.1       cgd }
    240