2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
35 * $DragonFly: src/libexec/talkd/table.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
39 * Routines to handle insertion, deletion, etc on the table
40 * of requests kept by the daemon. Nothing fancy here, linear
41 * search on a double-linked list. A time is kept with each
42 * entry so that overly old invitations can be eliminated.
44 * Consider this a mis-guided attempt at modularity
46 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <protocols/talkd.h>
56 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
62 typedef struct table_entry TABLE_ENTRY
;
71 TABLE_ENTRY
*table
= NULL
;
73 void delete (TABLE_ENTRY
*);
74 CTL_MSG
*find_request();
75 CTL_MSG
*find_match();
77 void print_request (char *, CTL_MSG
*);
80 * Look in the table for an invitation that matches the current
81 * request looking for an invitation
85 register CTL_MSG
*request
;
87 register TABLE_ENTRY
*ptr
;
90 gettimeofday(&tp
, &txp
);
91 current_time
= tp
.tv_sec
;
93 print_request("find_match", request
);
94 for (ptr
= table
; ptr
!= NULL
; ptr
= ptr
->next
) {
95 if ((ptr
->time
- current_time
) > MAX_LIFE
) {
96 /* the entry is too old */
98 print_request("deleting expired entry",
104 print_request("", &ptr
->request
);
105 if (strcmp(request
->l_name
, ptr
->request
.r_name
) == 0 &&
106 strcmp(request
->r_name
, ptr
->request
.l_name
) == 0 &&
107 ptr
->request
.type
== LEAVE_INVITE
)
108 return (&ptr
->request
);
114 * Look for an identical request, as opposed to a complimentary
115 * one as find_match does
118 find_request(request
)
119 register CTL_MSG
*request
;
121 register TABLE_ENTRY
*ptr
;
124 gettimeofday(&tp
, &txp
);
125 current_time
= tp
.tv_sec
;
127 * See if this is a repeated message, and check for
128 * out of date entries in the table while we are it.
131 print_request("find_request", request
);
132 for (ptr
= table
; ptr
!= NULL
; ptr
= ptr
->next
) {
133 if ((ptr
->time
- current_time
) > MAX_LIFE
) {
134 /* the entry is too old */
136 print_request("deleting expired entry",
142 print_request("", &ptr
->request
);
143 if (strcmp(request
->r_name
, ptr
->request
.r_name
) == 0 &&
144 strcmp(request
->l_name
, ptr
->request
.l_name
) == 0 &&
145 request
->type
== ptr
->request
.type
&&
146 request
->pid
== ptr
->request
.pid
) {
147 /* update the time if we 'touch' it */
148 ptr
->time
= current_time
;
149 return (&ptr
->request
);
156 insert_table(request
, response
)
158 CTL_RESPONSE
*response
;
160 register TABLE_ENTRY
*ptr
;
163 gettimeofday(&tp
, &txp
);
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
));
170 syslog(LOG_ERR
, "insert_table: Out of memory");
173 ptr
->time
= current_time
;
174 ptr
->request
= *request
;
176 if (ptr
->next
!= NULL
)
177 ptr
->next
->last
= ptr
;
183 * Generate a unique non-zero sequence number
188 static int current_id
= 0;
190 current_id
= (current_id
+ 1) % MAX_ID
;
191 /* 0 is reserved, helps to pick up bugs */
198 * Delete the invitation with id 'id_num'
201 delete_invite(id_num
)
204 register TABLE_ENTRY
*ptr
;
208 syslog(LOG_DEBUG
, "delete_invite(%d)", id_num
);
209 for (ptr
= table
; ptr
!= NULL
; ptr
= ptr
->next
) {
210 if (ptr
->request
.id_num
== id_num
)
213 print_request("", &ptr
->request
);
223 * Classic delete from a double-linked list
227 register TABLE_ENTRY
*ptr
;
231 print_request("delete", &ptr
->request
);
234 else if (ptr
->last
!= NULL
)
235 ptr
->last
->next
= ptr
->next
;
236 if (ptr
->next
!= NULL
)
237 ptr
->next
->last
= ptr
->last
;