libdialog: Increase MAX_LEN to 4096 (bug 2480)
[dragonfly.git] / libexec / talkd / table.c
CommitLineData
984263bc
MD
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.
1de703da
MD
32 *
33 * @(#)table.c 8.1 (Berkeley) 6/4/93
34 * $FreeBSD: src/libexec/talkd/table.c,v 1.7 1999/08/28 00:10:17 peter Exp $
984263bc
MD
35 */
36
984263bc
MD
37/*
38 * Routines to handle insertion, deletion, etc on the table
39 * of requests kept by the daemon. Nothing fancy here, linear
40 * search on a double-linked list. A time is kept with each
41 * entry so that overly old invitations can be eliminated.
42 *
43 * Consider this a mis-guided attempt at modularity
44 */
45#include <sys/param.h>
46#include <sys/time.h>
47#include <sys/socket.h>
48#include <protocols/talkd.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <syslog.h>
53#include <unistd.h>
54
30b2e94a
SW
55#include "extern.h"
56
984263bc
MD
57#define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
58
984263bc
MD
59extern int debug;
60struct timeval tp;
984263bc
MD
61
62typedef struct table_entry TABLE_ENTRY;
63
64struct table_entry {
65 CTL_MSG request;
66 long time;
67 TABLE_ENTRY *next;
68 TABLE_ENTRY *last;
69};
70
30b2e94a 71static void delete(TABLE_ENTRY *);
984263bc 72
30b2e94a 73TABLE_ENTRY *table = NULL;
984263bc
MD
74
75/*
76 * Look in the table for an invitation that matches the current
77 * request looking for an invitation
78 */
79CTL_MSG *
30b2e94a 80find_match(CTL_MSG *request)
984263bc 81{
30b2e94a 82 TABLE_ENTRY *ptr;
984263bc
MD
83 time_t current_time;
84
30b2e94a 85 gettimeofday(&tp, NULL);
984263bc
MD
86 current_time = tp.tv_sec;
87 if (debug)
88 print_request("find_match", request);
60233e58 89 for (ptr = table; ptr != NULL; ptr = ptr->next) {
984263bc
MD
90 if ((ptr->time - current_time) > MAX_LIFE) {
91 /* the entry is too old */
92 if (debug)
93 print_request("deleting expired entry",
94 &ptr->request);
95 delete(ptr);
96 continue;
97 }
98 if (debug)
99 print_request("", &ptr->request);
100 if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
101 strcmp(request->r_name, ptr->request.l_name) == 0 &&
102 ptr->request.type == LEAVE_INVITE)
103 return (&ptr->request);
104 }
60233e58 105 return (NULL);
984263bc
MD
106}
107
108/*
109 * Look for an identical request, as opposed to a complimentary
110 * one as find_match does
111 */
112CTL_MSG *
30b2e94a 113find_request(CTL_MSG *request)
984263bc 114{
30b2e94a 115 TABLE_ENTRY *ptr;
984263bc
MD
116 time_t current_time;
117
30b2e94a 118 gettimeofday(&tp, NULL);
984263bc
MD
119 current_time = tp.tv_sec;
120 /*
121 * See if this is a repeated message, and check for
122 * out of date entries in the table while we are it.
123 */
124 if (debug)
125 print_request("find_request", request);
60233e58 126 for (ptr = table; ptr != NULL; ptr = ptr->next) {
984263bc
MD
127 if ((ptr->time - current_time) > MAX_LIFE) {
128 /* the entry is too old */
129 if (debug)
130 print_request("deleting expired entry",
131 &ptr->request);
132 delete(ptr);
133 continue;
134 }
135 if (debug)
136 print_request("", &ptr->request);
137 if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
138 strcmp(request->l_name, ptr->request.l_name) == 0 &&
139 request->type == ptr->request.type &&
140 request->pid == ptr->request.pid) {
141 /* update the time if we 'touch' it */
142 ptr->time = current_time;
143 return (&ptr->request);
144 }
145 }
60233e58 146 return (NULL);
984263bc
MD
147}
148
149void
30b2e94a 150insert_table(CTL_MSG *request, CTL_RESPONSE *response)
984263bc 151{
30b2e94a 152 TABLE_ENTRY *ptr;
984263bc
MD
153 time_t current_time;
154
30b2e94a 155 gettimeofday(&tp, NULL);
984263bc
MD
156 current_time = tp.tv_sec;
157 request->id_num = new_id();
158 response->id_num = htonl(request->id_num);
159 /* insert a new entry into the top of the list */
160 ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
60233e58 161 if (ptr == NULL) {
984263bc
MD
162 syslog(LOG_ERR, "insert_table: Out of memory");
163 _exit(1);
164 }
165 ptr->time = current_time;
166 ptr->request = *request;
167 ptr->next = table;
60233e58 168 if (ptr->next != NULL)
984263bc 169 ptr->next->last = ptr;
60233e58 170 ptr->last = NULL;
984263bc
MD
171 table = ptr;
172}
173
174/*
175 * Generate a unique non-zero sequence number
176 */
177int
30b2e94a 178new_id(void)
984263bc
MD
179{
180 static int current_id = 0;
181
182 current_id = (current_id + 1) % MAX_ID;
183 /* 0 is reserved, helps to pick up bugs */
184 if (current_id == 0)
185 current_id = 1;
186 return (current_id);
187}
188
189/*
190 * Delete the invitation with id 'id_num'
191 */
192int
30b2e94a 193delete_invite(unsigned int id_num)
984263bc 194{
30b2e94a 195 TABLE_ENTRY *ptr;
984263bc
MD
196
197 ptr = table;
198 if (debug)
199 syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
60233e58 200 for (ptr = table; ptr != NULL; ptr = ptr->next) {
984263bc
MD
201 if (ptr->request.id_num == id_num)
202 break;
203 if (debug)
204 print_request("", &ptr->request);
205 }
60233e58 206 if (ptr != NULL) {
984263bc
MD
207 delete(ptr);
208 return (SUCCESS);
209 }
210 return (NOT_HERE);
211}
212
213/*
214 * Classic delete from a double-linked list
215 */
30b2e94a
SW
216static void
217delete(TABLE_ENTRY *ptr)
984263bc
MD
218{
219
220 if (debug)
221 print_request("delete", &ptr->request);
222 if (table == ptr)
223 table = ptr->next;
60233e58 224 else if (ptr->last != NULL)
984263bc 225 ptr->last->next = ptr->next;
60233e58 226 if (ptr->next != NULL)
984263bc
MD
227 ptr->next->last = ptr->last;
228 free((char *)ptr);
229}