1 /* $NetBSD: table.c,v 1.5 1998/07/04 19:31:05 mrg 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.5 1998/07/04 19:31:05 mrg 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 #include "extern.h" 63 64 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ 65 66 #define NIL ((TABLE_ENTRY *)0) 67 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 82 static void delete __P((TABLE_ENTRY *)); 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 CTL_MSG *request; 91 { 92 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 CTL_MSG *request; 125 { 126 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 void 161 insert_table(request, response) 162 CTL_MSG *request; 163 CTL_RESPONSE *response; 164 { 165 TABLE_ENTRY *ptr; 166 time_t current_time; 167 168 gettimeofday(&tp, &txp); 169 current_time = tp.tv_sec; 170 request->id_num = new_id(); 171 response->id_num = htonl(request->id_num); 172 /* insert a new entry into the top of the list */ 173 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY)); 174 if (ptr == NIL) { 175 syslog(LOG_ERR, "insert_table: Out of memory"); 176 _exit(1); 177 } 178 ptr->time = current_time; 179 ptr->request = *request; 180 ptr->next = table; 181 if (ptr->next != NIL) 182 ptr->next->last = ptr; 183 ptr->last = NIL; 184 table = ptr; 185 } 186 187 /* 188 * Generate a unique non-zero sequence number 189 */ 190 int 191 new_id() 192 { 193 static int current_id = 0; 194 195 current_id = (current_id + 1) % MAX_ID; 196 /* 0 is reserved, helps to pick up bugs */ 197 if (current_id == 0) 198 current_id = 1; 199 return (current_id); 200 } 201 202 /* 203 * Delete the invitation with id 'id_num' 204 */ 205 int 206 delete_invite(id_num) 207 int id_num; 208 { 209 TABLE_ENTRY *ptr; 210 211 ptr = table; 212 if (debug) 213 syslog(LOG_DEBUG, "delete_invite(%d)", id_num); 214 for (ptr = table; ptr != NIL; ptr = ptr->next) { 215 if (ptr->request.id_num == id_num) 216 break; 217 if (debug) 218 print_request("", &ptr->request); 219 } 220 if (ptr != NIL) { 221 delete(ptr); 222 return (SUCCESS); 223 } 224 return (NOT_HERE); 225 } 226 227 /* 228 * Classic delete from a double-linked list 229 */ 230 static void 231 delete(ptr) 232 TABLE_ENTRY *ptr; 233 { 234 235 if (debug) 236 print_request("delete", &ptr->request); 237 if (table == ptr) 238 table = ptr->next; 239 else if (ptr->last != NIL) 240 ptr->last->next = ptr->next; 241 if (ptr->next != NIL) 242 ptr->next->last = ptr->last; 243 free((char *)ptr); 244 } 245