table.c revision 241777
1184610Salfred/* 2184610Salfred * Copyright (c) 1983, 1993 3184610Salfred * The Regents of the University of California. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 3. All advertising materials mentioning features or use of this software 14184610Salfred * must display the following acknowledgement: 15184610Salfred * This product includes software developed by the University of 16184610Salfred * California, Berkeley and its contributors. 17184610Salfred * 4. Neither the name of the University nor the names of its contributors 18184610Salfred * may be used to endorse or promote products derived from this software 19184610Salfred * without specific prior written permission. 20184610Salfred * 21184610Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31184610Salfred * SUCH DAMAGE. 32184610Salfred */ 33184610Salfred 34184610Salfred#ifndef lint 35184610Salfred#if 0 36184610Salfredstatic char sccsid[] = "@(#)table.c 8.1 (Berkeley) 6/4/93"; 37184610Salfred#endif 38184610Salfredstatic const char rcsid[] = 39184610Salfred "$FreeBSD: head/libexec/talkd/table.c 241777 2012-10-20 10:33:15Z ed $"; 40184610Salfred#endif /* not lint */ 41184610Salfred 42184610Salfred/* 43184610Salfred * Routines to handle insertion, deletion, etc on the table 44184610Salfred * of requests kept by the daemon. Nothing fancy here, linear 45184610Salfred * search on a double-linked list. A time is kept with each 46184610Salfred * entry so that overly old invitations can be eliminated. 47184610Salfred * 48184610Salfred * Consider this a mis-guided attempt at modularity 49184610Salfred */ 50184610Salfred#include <sys/param.h> 51184610Salfred#include <sys/time.h> 52184610Salfred#include <sys/socket.h> 53184610Salfred#include <netinet/in.h> 54184610Salfred#include <protocols/talkd.h> 55184610Salfred#include <stdio.h> 56184610Salfred#include <stdlib.h> 57184610Salfred#include <string.h> 58184610Salfred#include <syslog.h> 59184610Salfred#include <unistd.h> 60184610Salfred 61184610Salfred#include "extern.h" 62184610Salfred 63184610Salfred#define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ 64184610Salfred 65184610Salfred#define NIL ((TABLE_ENTRY *)0) 66184610Salfred 67184610Salfredstatic struct timeval tp; 68184610Salfred 69184610Salfredtypedef struct table_entry TABLE_ENTRY; 70184610Salfred 71184610Salfredstruct table_entry { 72184610Salfred CTL_MSG request; 73184610Salfred long time; 74184610Salfred TABLE_ENTRY *next; 75184610Salfred TABLE_ENTRY *last; 76184610Salfred}; 77184610Salfred 78184610Salfredstatic void delete(TABLE_ENTRY *); 79184610Salfred 80184610Salfredstatic TABLE_ENTRY *table = NIL; 81 82/* 83 * Look in the table for an invitation that matches the current 84 * request looking for an invitation 85 */ 86CTL_MSG * 87find_match(CTL_MSG *request) 88{ 89 TABLE_ENTRY *ptr; 90 time_t current_time; 91 92 gettimeofday(&tp, NULL); 93 current_time = tp.tv_sec; 94 if (debug) 95 print_request("find_match", request); 96 for (ptr = table; ptr != NIL; ptr = ptr->next) { 97 if ((ptr->time - current_time) > MAX_LIFE) { 98 /* the entry is too old */ 99 if (debug) 100 print_request("deleting expired entry", 101 &ptr->request); 102 delete(ptr); 103 continue; 104 } 105 if (debug) 106 print_request("", &ptr->request); 107 if (strcmp(request->l_name, ptr->request.r_name) == 0 && 108 strcmp(request->r_name, ptr->request.l_name) == 0 && 109 ptr->request.type == LEAVE_INVITE) 110 return (&ptr->request); 111 } 112 return ((CTL_MSG *)0); 113} 114 115/* 116 * Look for an identical request, as opposed to a complimentary 117 * one as find_match does 118 */ 119CTL_MSG * 120find_request(CTL_MSG *request) 121{ 122 TABLE_ENTRY *ptr; 123 time_t current_time; 124 125 gettimeofday(&tp, NULL); 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 156void 157insert_table(CTL_MSG *request, CTL_RESPONSE *response) 158{ 159 TABLE_ENTRY *ptr; 160 time_t current_time; 161 162 gettimeofday(&tp, NULL); 163 current_time = tp.tv_sec; 164 request->id_num = new_id(); 165 response->id_num = htonl(request->id_num); 166 /* insert a new entry into the top of the list */ 167 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY)); 168 if (ptr == NIL) { 169 syslog(LOG_ERR, "insert_table: Out of memory"); 170 _exit(1); 171 } 172 ptr->time = current_time; 173 ptr->request = *request; 174 ptr->next = table; 175 if (ptr->next != NIL) 176 ptr->next->last = ptr; 177 ptr->last = NIL; 178 table = ptr; 179} 180 181/* 182 * Generate a unique non-zero sequence number 183 */ 184int 185new_id(void) 186{ 187 static int current_id = 0; 188 189 current_id = (current_id + 1) % MAX_ID; 190 /* 0 is reserved, helps to pick up bugs */ 191 if (current_id == 0) 192 current_id = 1; 193 return (current_id); 194} 195 196/* 197 * Delete the invitation with id 'id_num' 198 */ 199int 200delete_invite(u_int32_t id_num) 201{ 202 TABLE_ENTRY *ptr; 203 204 ptr = table; 205 if (debug) 206 syslog(LOG_DEBUG, "delete_invite(%d)", id_num); 207 for (ptr = table; ptr != NIL; ptr = ptr->next) { 208 if (ptr->request.id_num == id_num) 209 break; 210 if (debug) 211 print_request("", &ptr->request); 212 } 213 if (ptr != NIL) { 214 delete(ptr); 215 return (SUCCESS); 216 } 217 return (NOT_HERE); 218} 219 220/* 221 * Classic delete from a double-linked list 222 */ 223static void 224delete(TABLE_ENTRY *ptr) 225{ 226 227 if (debug) 228 print_request("delete", &ptr->request); 229 if (table == ptr) 230 table = ptr->next; 231 else if (ptr->last != NIL) 232 ptr->last->next = ptr->next; 233 if (ptr->next != NIL) 234 ptr->next->last = ptr->last; 235 free((char *)ptr); 236} 237