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