/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1983, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* Routines to handle insertion, deletion, etc on the table33* of requests kept by the daemon. Nothing fancy here, linear34* search on a double-linked list. A time is kept with each35* entry so that overly old invitations can be eliminated.36*37* Consider this a mis-guided attempt at modularity38*/39#include <sys/param.h>40#include <sys/time.h>41#include <sys/socket.h>42#include <netinet/in.h>43#include <protocols/talkd.h>44#include <stdio.h>45#include <stdlib.h>46#include <string.h>47#include <syslog.h>48#include <unistd.h>4950#include "extern.h"5152#define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */5354#define NIL ((TABLE_ENTRY *)0)5556static struct timespec ts;5758typedef struct table_entry TABLE_ENTRY;5960struct table_entry {61CTL_MSG request;62long time;63TABLE_ENTRY *next;64TABLE_ENTRY *last;65};6667static void delete(TABLE_ENTRY *);6869static TABLE_ENTRY *table = NIL;7071/*72* Look in the table for an invitation that matches the current73* request looking for an invitation74*/75CTL_MSG *76find_match(CTL_MSG *request)77{78TABLE_ENTRY *ptr, *next;79time_t current_time;8081clock_gettime(CLOCK_MONOTONIC_FAST, &ts);82current_time = ts.tv_sec;83if (debug)84print_request("find_match", request);85for (ptr = table; ptr != NIL; ptr = next) {86next = ptr->next;87if ((ptr->time - current_time) > MAX_LIFE) {88/* the entry is too old */89if (debug)90print_request("deleting expired entry",91&ptr->request);92delete(ptr);93continue;94}95if (debug)96print_request("", &ptr->request);97if (strcmp(request->l_name, ptr->request.r_name) == 0 &&98strcmp(request->r_name, ptr->request.l_name) == 0 &&99ptr->request.type == LEAVE_INVITE)100return (&ptr->request);101}102return ((CTL_MSG *)0);103}104105/*106* Look for an identical request, as opposed to a complimentary107* one as find_match does108*/109CTL_MSG *110find_request(CTL_MSG *request)111{112TABLE_ENTRY *ptr, *next;113time_t current_time;114115clock_gettime(CLOCK_MONOTONIC_FAST, &ts);116current_time = ts.tv_sec;117/*118* See if this is a repeated message, and check for119* out of date entries in the table while we are it.120*/121if (debug)122print_request("find_request", request);123for (ptr = table; ptr != NIL; ptr = next) {124next = ptr->next;125if ((ptr->time - current_time) > MAX_LIFE) {126/* the entry is too old */127if (debug)128print_request("deleting expired entry",129&ptr->request);130delete(ptr);131continue;132}133if (debug)134print_request("", &ptr->request);135if (strcmp(request->r_name, ptr->request.r_name) == 0 &&136strcmp(request->l_name, ptr->request.l_name) == 0 &&137request->type == ptr->request.type &&138request->pid == ptr->request.pid) {139/* update the time if we 'touch' it */140ptr->time = current_time;141return (&ptr->request);142}143}144return ((CTL_MSG *)0);145}146147void148insert_table(CTL_MSG *request, CTL_RESPONSE *response)149{150TABLE_ENTRY *ptr;151time_t current_time;152153clock_gettime(CLOCK_MONOTONIC_FAST, &ts);154current_time = ts.tv_sec;155request->id_num = new_id();156response->id_num = htonl(request->id_num);157/* insert a new entry into the top of the list */158ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));159if (ptr == NIL) {160syslog(LOG_ERR, "insert_table: Out of memory");161_exit(1);162}163ptr->time = current_time;164ptr->request = *request;165ptr->next = table;166if (ptr->next != NIL)167ptr->next->last = ptr;168ptr->last = NIL;169table = ptr;170}171172/*173* Generate a unique non-zero sequence number174*/175int176new_id(void)177{178static int current_id = 0;179180current_id = (current_id + 1) % MAX_ID;181/* 0 is reserved, helps to pick up bugs */182if (current_id == 0)183current_id = 1;184return (current_id);185}186187/*188* Delete the invitation with id 'id_num'189*/190int191delete_invite(u_int32_t id_num)192{193TABLE_ENTRY *ptr;194195if (debug)196syslog(LOG_DEBUG, "delete_invite(%d)", id_num);197for (ptr = table; ptr != NIL; ptr = ptr->next) {198if (ptr->request.id_num == id_num)199break;200if (debug)201print_request("", &ptr->request);202}203if (ptr != NIL) {204delete(ptr);205return (SUCCESS);206}207return (NOT_HERE);208}209210/*211* Classic delete from a double-linked list212*/213static void214delete(TABLE_ENTRY *ptr)215{216217if (debug)218print_request("delete", &ptr->request);219if (table == ptr)220table = ptr->next;221else if (ptr->last != NIL)222ptr->last->next = ptr->next;223if (ptr->next != NIL)224ptr->next->last = ptr->last;225free((char *)ptr);226}227228229