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