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