1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * 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 35#if 0 36static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93"; 37#endif 38__attribute__((__used__)) 39static const char rcsid[] = 40 "$FreeBSD: src/libexec/talkd/table.c,v 1.9 2003/04/03 05:13:27 jmallett Exp $"; 41#endif /* not lint */ 42 43#include <sys/cdefs.h> 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 <netinet/in.h> 57#include <protocols/talkd.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61#include <syslog.h> 62#include <unistd.h> 63 64#include "extern.h" 65 66#define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ 67 68#define NIL ((TABLE_ENTRY *)0) 69 70extern int debug; 71struct timeval tp; 72struct timezone txp; 73 74typedef struct table_entry TABLE_ENTRY; 75 76struct table_entry { 77 CTL_MSG request; 78 long time; 79 TABLE_ENTRY *next; 80 TABLE_ENTRY *last; 81}; 82 83static void delete(TABLE_ENTRY *); 84 85TABLE_ENTRY *table = NIL; 86 87/* 88 * Look in the table for an invitation that matches the current 89 * request looking for an invitation 90 */ 91CTL_MSG * 92find_match(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; ptr != NIL; ptr = ptr->next) { 102 if ((ptr->time - current_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 return ((CTL_MSG *)0); 118} 119 120/* 121 * Look for an identical request, as opposed to a complimentary 122 * one as find_match does 123 */ 124CTL_MSG * 125find_request(CTL_MSG *request) 126{ 127 TABLE_ENTRY *ptr; 128 time_t current_time; 129 130 gettimeofday(&tp, &txp); 131 current_time = tp.tv_sec; 132 /* 133 * See if this is a repeated message, and check for 134 * out of date entries in the table while we are it. 135 */ 136 if (debug) 137 print_request("find_request", request); 138 for (ptr = table; ptr != NIL; ptr = ptr->next) { 139 if ((ptr->time - current_time) > MAX_LIFE) { 140 /* the entry is too old */ 141 if (debug) 142 print_request("deleting expired entry", 143 &ptr->request); 144 delete(ptr); 145 continue; 146 } 147 if (debug) 148 print_request("", &ptr->request); 149 if (strcmp(request->r_name, ptr->request.r_name) == 0 && 150 strcmp(request->l_name, ptr->request.l_name) == 0 && 151 request->type == ptr->request.type && 152 request->pid == ptr->request.pid) { 153 /* update the time if we 'touch' it */ 154 ptr->time = current_time; 155 return (&ptr->request); 156 } 157 } 158 return ((CTL_MSG *)0); 159} 160 161void 162insert_table(CTL_MSG *request, CTL_RESPONSE *response) 163{ 164 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 */ 189int 190new_id(void) 191{ 192 static int current_id = 0; 193 194 current_id = (current_id + 1) % MAX_ID; 195 /* 0 is reserved, helps to pick up bugs */ 196 if (current_id == 0) 197 current_id = 1; 198 return (current_id); 199} 200 201/* 202 * Delete the invitation with id 'id_num' 203 */ 204int 205delete_invite(u_int32_t id_num) 206{ 207 TABLE_ENTRY *ptr; 208 209 ptr = table; 210 if (debug) 211 syslog(LOG_DEBUG, "delete_invite(%d)", id_num); 212 for (ptr = table; ptr != NIL; ptr = ptr->next) { 213 if (ptr->request.id_num == id_num) 214 break; 215 if (debug) 216 print_request("", &ptr->request); 217 } 218 if (ptr != NIL) { 219 delete(ptr); 220 return (SUCCESS); 221 } 222 return (NOT_HERE); 223} 224 225/* 226 * Classic delete from a double-linked list 227 */ 228static void 229delete(TABLE_ENTRY *ptr) 230{ 231 232 if (debug) 233 print_request("delete", &ptr->request); 234 if (table == ptr) 235 table = ptr->next; 236 else if (ptr->last != NIL) 237 ptr->last->next = ptr->next; 238 if (ptr->next != NIL) 239 ptr->next->last = ptr->last; 240 free((char *)ptr); 241} 242