GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/talkd/table.c Lines: 0 73 0.0 %
Date: 2017-11-13 Branches: 0 54 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: table.c,v 1.19 2016/08/26 08:44:04 guenther Exp $	*/
2
3
/*
4
 * Copyright (c) 1983 Regents of the University of California.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the University nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
/*
33
 * Routines to handle insertion, deletion, etc on the table
34
 * of requests kept by the daemon. Nothing fancy here, linear
35
 * search on a double-linked list. A time is kept with each
36
 * entry so that overly old invitations can be eliminated.
37
 *
38
 * Consider this a mis-guided attempt at modularity
39
 */
40
#include <sys/queue.h>
41
#include <sys/socket.h>
42
#include <sys/time.h>
43
#include <protocols/talkd.h>
44
45
#include <stdlib.h>
46
#include <string.h>
47
#include <syslog.h>
48
#include <unistd.h>
49
50
#include "talkd.h"
51
52
#define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
53
54
struct	timeval tp;
55
struct	timezone txp;
56
57
typedef struct table_entry TABLE_ENTRY;
58
59
struct table_entry {
60
	CTL_MSG request;
61
	time_t	time;
62
	TAILQ_ENTRY(table_entry) list;
63
};
64
TAILQ_HEAD(, table_entry)	table;
65
66
static void	delete(TABLE_ENTRY *);
67
68
/*
69
 * Init the table
70
 */
71
void
72
init_table(void)
73
{
74
	TAILQ_INIT(&table);
75
}
76
77
/*
78
 * Look in the table for an invitation that matches the current
79
 * request looking for an invitation
80
 */
81
CTL_MSG *
82
find_match(CTL_MSG *request)
83
{
84
	TABLE_ENTRY *ptr, *next;
85
	time_t current_time;
86
87
	gettimeofday(&tp, &txp);
88
	current_time = tp.tv_sec;
89
	if (debug)
90
		print_request("find_match", request);
91
	for (ptr = TAILQ_FIRST(&table); ptr != NULL; ptr = next) {
92
		next = TAILQ_NEXT(ptr, list);
93
		if ((current_time - ptr->time) > MAX_LIFE) {
94
			/* the entry is too old */
95
			if (debug)
96
				print_request("deleting expired entry",
97
				    &ptr->request);
98
			delete(ptr);
99
			continue;
100
		}
101
		if (debug)
102
			print_request("", &ptr->request);
103
		if (ptr->request.type == LEAVE_INVITE &&
104
		    strcmp(request->l_name, ptr->request.r_name) == 0 &&
105
		    strcmp(request->r_name, ptr->request.l_name) == 0)
106
			return (&ptr->request);
107
	}
108
	if (debug)
109
		syslog(LOG_DEBUG, "find_match: not found");
110
111
	return (NULL);
112
}
113
114
/*
115
 * Look for an identical request, as opposed to a complimentary
116
 * one as find_match does
117
 */
118
CTL_MSG *
119
find_request(CTL_MSG *request)
120
{
121
	TABLE_ENTRY *ptr, *next;
122
	time_t current_time;
123
124
	gettimeofday(&tp, &txp);
125
	current_time = tp.tv_sec;
126
	/*
127
	 * See if this is a repeated message, and check for
128
	 * out of date entries in the table while we are it.
129
	 */
130
	if (debug)
131
		print_request("find_request", request);
132
	for (ptr = TAILQ_FIRST(&table); ptr != NULL; ptr = next) {
133
		next = TAILQ_NEXT(ptr, list);
134
		if ((current_time - ptr->time) > MAX_LIFE) {
135
			/* the entry is too old */
136
			if (debug)
137
				print_request("deleting expired entry",
138
				    &ptr->request);
139
			delete(ptr);
140
			continue;
141
		}
142
		if (debug)
143
			print_request("", &ptr->request);
144
		if (request->pid == ptr->request.pid &&
145
		    request->type == ptr->request.type &&
146
		    strcmp(request->r_name, ptr->request.r_name) == 0 &&
147
		    strcmp(request->l_name, ptr->request.l_name) == 0) {
148
			/* update the time if we 'touch' it */
149
			ptr->time = current_time;
150
			return (&ptr->request);
151
		}
152
	}
153
	return (NULL);
154
}
155
156
void
157
insert_table(CTL_MSG *request, CTL_RESPONSE *response)
158
{
159
	TABLE_ENTRY *ptr;
160
	time_t current_time;
161
162
	if (debug)
163
		print_request( "insert_table", request );
164
	gettimeofday(&tp, &txp);
165
	current_time = tp.tv_sec;
166
	request->id_num = new_id();
167
	response->id_num = htonl(request->id_num);
168
	/* insert a new entry into the top of the list */
169
	ptr = malloc(sizeof(TABLE_ENTRY));
170
	if (ptr == NULL) {
171
		syslog(LOG_ERR, "insert_table: Out of memory");
172
		_exit(1);
173
	}
174
	ptr->time = current_time;
175
	ptr->request = *request;
176
	TAILQ_INSERT_HEAD(&table, ptr, list);
177
}
178
179
/*
180
 * Generate a unique non-zero sequence number
181
 */
182
int
183
new_id(void)
184
{
185
	static int current_id = 0;
186
187
	current_id = (current_id + 1) % MAX_ID;
188
	/* 0 is reserved, helps to pick up bugs */
189
	if (current_id == 0)
190
		current_id = 1;
191
	return (current_id);
192
}
193
194
/*
195
 * Delete the invitation with id 'id_num'
196
 */
197
int
198
delete_invite(int id_num)
199
{
200
	TABLE_ENTRY *ptr;
201
202
	if (debug)
203
		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
204
	TAILQ_FOREACH(ptr, &table, list) {
205
		if (ptr->request.id_num == id_num)
206
			break;
207
		if (debug)
208
			print_request("", &ptr->request);
209
	}
210
	if (ptr != NULL) {
211
		delete(ptr);
212
		return (SUCCESS);
213
	}
214
	return (NOT_HERE);
215
}
216
217
/*
218
 * Classic delete from a double-linked list
219
 */
220
static void
221
delete(TABLE_ENTRY *ptr)
222
{
223
224
	if (debug)
225
		print_request("delete", &ptr->request);
226
	TAILQ_REMOVE(&table, ptr, list);
227
	free((char *)ptr);
228
}