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