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