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