1 /* $OpenBSD: table.c,v 1.8 2002/02/16 21:27:31 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1983 Regents of the University of California. 5 * 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 #ifndef lint 37 /*static char sccsid[] = "from: @(#)table.c 5.7 (Berkeley) 2/26/91";*/ 38 static char rcsid[] = "$Id: table.c,v 1.8 2002/02/16 21:27:31 millert Exp $"; 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 <sys/queue.h> 53 #include <protocols/talkd.h> 54 #include <syslog.h> 55 #include <unistd.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include "talkd.h" 60 61 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ 62 63 struct timeval tp; 64 struct timezone txp; 65 66 typedef struct table_entry TABLE_ENTRY; 67 68 struct table_entry { 69 CTL_MSG request; 70 time_t time; 71 TAILQ_ENTRY(table_entry) list; 72 }; 73 TAILQ_HEAD(, table_entry) table; 74 75 static void delete(TABLE_ENTRY *); 76 77 /* 78 * Init the table 79 */ 80 void 81 init_table() 82 { 83 TAILQ_INIT(&table); 84 } 85 86 /* 87 * Look in the table for an invitation that matches the current 88 * request looking for an invitation 89 */ 90 CTL_MSG * 91 find_match(request) 92 CTL_MSG *request; 93 { 94 TABLE_ENTRY *ptr; 95 time_t current_time; 96 97 gettimeofday(&tp, &txp); 98 current_time = tp.tv_sec; 99 if (debug) 100 print_request("find_match", request); 101 for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) { 102 if ((current_time - ptr->time) > MAX_LIFE) { 103 /* the entry is too old */ 104 if (debug) 105 print_request("deleting expired entry", 106 &ptr->request); 107 delete(ptr); 108 continue; 109 } 110 if (debug) 111 print_request("", &ptr->request); 112 if (strcmp(request->l_name, ptr->request.r_name) == 0 && 113 strcmp(request->r_name, ptr->request.l_name) == 0 && 114 ptr->request.type == LEAVE_INVITE) 115 return (&ptr->request); 116 } 117 if (debug) 118 syslog(LOG_DEBUG, "find_match: not found"); 119 120 return ((CTL_MSG *)0); 121 } 122 123 /* 124 * Look for an identical request, as opposed to a complimentary 125 * one as find_match does 126 */ 127 CTL_MSG * 128 find_request(request) 129 CTL_MSG *request; 130 { 131 TABLE_ENTRY *ptr; 132 time_t current_time; 133 134 gettimeofday(&tp, &txp); 135 current_time = tp.tv_sec; 136 /* 137 * See if this is a repeated message, and check for 138 * out of date entries in the table while we are it. 139 */ 140 if (debug) 141 print_request("find_request", request); 142 for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) { 143 if ((current_time - ptr->time) > MAX_LIFE) { 144 /* the entry is too old */ 145 if (debug) 146 print_request("deleting expired entry", 147 &ptr->request); 148 delete(ptr); 149 continue; 150 } 151 if (debug) 152 print_request("", &ptr->request); 153 if (strcmp(request->r_name, ptr->request.r_name) == 0 && 154 strcmp(request->l_name, ptr->request.l_name) == 0 && 155 request->type == ptr->request.type && 156 request->pid == ptr->request.pid) { 157 /* update the time if we 'touch' it */ 158 ptr->time = current_time; 159 return (&ptr->request); 160 } 161 } 162 return ((CTL_MSG *)0); 163 } 164 165 void 166 insert_table(request, response) 167 CTL_MSG *request; 168 CTL_RESPONSE *response; 169 { 170 TABLE_ENTRY *ptr; 171 time_t current_time; 172 173 if (debug) 174 print_request( "insert_table", request ); 175 gettimeofday(&tp, &txp); 176 current_time = tp.tv_sec; 177 request->id_num = new_id(); 178 response->id_num = htonl(request->id_num); 179 /* insert a new entry into the top of the list */ 180 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY)); 181 if (ptr == NULL) { 182 syslog(LOG_ERR, "insert_table: Out of memory"); 183 _exit(1); 184 } 185 ptr->time = current_time; 186 ptr->request = *request; 187 TAILQ_INSERT_HEAD(&table, ptr, list); 188 } 189 190 /* 191 * Generate a unique non-zero sequence number 192 */ 193 int 194 new_id(void) 195 { 196 static int current_id = 0; 197 198 current_id = (current_id + 1) % MAX_ID; 199 /* 0 is reserved, helps to pick up bugs */ 200 if (current_id == 0) 201 current_id = 1; 202 return (current_id); 203 } 204 205 /* 206 * Delete the invitation with id 'id_num' 207 */ 208 int 209 delete_invite(id_num) 210 int id_num; 211 { 212 TABLE_ENTRY *ptr; 213 214 if (debug) 215 syslog(LOG_DEBUG, "delete_invite(%d)", id_num); 216 for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) { 217 if (ptr->request.id_num == id_num) 218 break; 219 if (debug) 220 print_request("", &ptr->request); 221 } 222 if (ptr != NULL) { 223 delete(ptr); 224 return (SUCCESS); 225 } 226 return (NOT_HERE); 227 } 228 229 /* 230 * Classic delete from a double-linked list 231 */ 232 static void 233 delete(ptr) 234 TABLE_ENTRY *ptr; 235 { 236 237 if (debug) 238 print_request("delete", &ptr->request); 239 TAILQ_REMOVE(&table, ptr, list); 240 free((char *)ptr); 241 } 242