Debian Codegraph @

talkd/table.c

raw
1 /*
2 Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <intalkd.h>
22 #include "attribute.h"
23
24 typedef struct request_table table_t;
25
26 struct request_table
27 {
28 table_t *next, *prev;
29 CTL_MSG request;
30 time_t time;
31 };
32
33 static table_t *table;
34
35 static void
36 table_delete (table_t *ptr)
37 {
38 table_t *t;
39
40 if ((t = ptr->prev) != NULL)
41 t->next = ptr->next;
42 else
43 table = ptr->next;
44 if ((t = ptr->next) != NULL)
45 t->prev = ptr->prev;
46 free (ptr);
47 }
48
49 /* Look in the table for an invitation that matches given criteria.
50 Linear search: premature optimisation is the root of all
51 Evil) */
52
53 static CTL_MSG *
54 lookup_request (CTL_MSG *request,
55 int (*comp) (table_t *, CTL_MSG *, time_t *))
56 {
57 table_t *ptr;
58 time_t now;
59
60 time (&now);
61
62 if (debug)
63 print_request ("lookup_request", request);
64
65 for (ptr = table; ptr;)
66 {
67 table_t *next = ptr->next;
68
69 if (debug)
70 print_request ("comparing against: ", &ptr->request);
71
72 if ((ptr->time - now) > max_request_ttl)
73 {
74 /* the entry is too old */
75 if (debug)
76 print_request ("deleting expired entry", &ptr->request);
77 table_delete (ptr);
78 }
79 else
80 {
81 if (comp (ptr, request, &now) == 0)
82 {
83 if (debug)
84 print_request ("found", &ptr->request);
85 return &ptr->request;
86 }
87 }
88
89 ptr = next;
90 }
91 if (debug)
92 syslog (LOG_DEBUG, "not found");
93 return NULL;
94 }
95
96 static int
97 fuzzy_comp (table_t *ptr, CTL_MSG *request, time_t *unused MAYBE_UNUSED)
98 {
99 if (ptr->request.type == LEAVE_INVITE
100 && strcmp (request->l_name, ptr->request.r_name) == 0
101 && strcmp (request->r_name, ptr->request.l_name) == 0)
102 return 0;
103 return 1;
104 }
105
106 /* Look in the table for an invitation that matches the current
107 request looking for an invitation */
108 CTL_MSG *
109 find_match (CTL_MSG *request)
110 {
111 return lookup_request (request, fuzzy_comp);
112 }
113
114 static int
115 exact_comp (table_t *ptr, CTL_MSG *request, time_t *now)
116 {
117 if (request->type == ptr->request.type
118 && request->pid == ptr->request.pid
119 && strcmp (request->r_name, ptr->request.r_name) == 0
120 && strcmp (request->l_name, ptr->request.l_name) == 0)
121 {
122 ptr->time = *now;
123 return 0;
124 }
125 return 1;
126 }
127
128 /* Look for an identical request, as opposed to a complementary
129 one as find_match does */
130
131 CTL_MSG *
132 find_request (CTL_MSG *request)
133 {
134 return lookup_request (request, exact_comp);
135 }
136
137 #define MAX_ID 16000
138
139 /* Generate a unique non-zero sequence number */
140 int
141 new_id (void)
142 {
143 static int current_id = 0;
144
145 current_id = (current_id + 1) % MAX_ID;
146 /* 0 is reserved, helps to pick up bugs */
147 if (current_id == 0)
148 current_id = 1;
149 return current_id;
150 }
151
152 int
153 insert_table (CTL_MSG *request, CTL_RESPONSE *response)
154 {
155 table_t *ptr;
156
157 ptr = malloc (sizeof *ptr);
158 if (!ptr)
159 {
160 syslog (LOG_ERR, "Out of memory");
161 exit (EXIT_FAILURE);
162 }
163
164 request->id_num = new_id ();
165
166 if (debug)
167 print_request ("insert_table", request);
168
169 response->id_num = htonl (request->id_num);
170
171 time (&ptr->time);
172 ptr->request = *request;
173 ptr->prev = NULL;
174 ptr->next = table;
175 table = ptr;
176 return 0;
177 }
178
179 /* Delete the invitation with id 'id_num' */
180 int
181 delete_invite (unsigned long id_num)
182 {
183 table_t *ptr;
184
185 for (ptr = table; ptr; ptr = ptr->next)
186 if (ptr->request.id_num == id_num)
187 {
188 table_delete (ptr);
189 return SUCCESS;
190 }
191 return NOT_HERE;
192 }