-
Notifications
You must be signed in to change notification settings - Fork 6
/
tftpd_list.c
349 lines (309 loc) · 10.1 KB
/
tftpd_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* tftpd_list.c
* thread_data and client list related functions
*
* $Id: tftpd_list.c,v 1.9 2004/02/27 02:05:26 jp Exp $
*
* Copyright (c) 2000 Jean-Pierre Lefebvre <[email protected]>
* and Remi Lefebvre <[email protected]>
*
* atftp is free software; you can redistribute them and/or modify them
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "tftpd.h"
#include "logger.h"
/*
* thread_data is a double link list of server threads. Server threads
* are started by the main thread when a new request arrives. The number
* of threads running is held in number_of_thread. Any access to the
* thread_data list: insertion and extraction of elements must be protected
* by the mutex. This is done in this file by all functions playing with the thread list.
* Note that individual threads do not need to lock the list when playing in their data.
* See tftpd.h.
*
* In addition, it is needed to use mutex when working on the client list of
* individual thread. In some case, the thread data mutex is needed also.
* Again, the functions in this file take care of this.
*/
struct thread_data *thread_data = NULL; /* head of thread list */
static int number_of_thread = 0;
pthread_mutex_t thread_list_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Add a new thread_data structure to the list. Thread list mutex is locked
* before walking the list and doing the insertion.
*/
int tftpd_list_add(struct thread_data *new)
{
pthread_mutex_lock(&thread_list_mutex);
struct thread_data *current = thread_data;
int ret;
number_of_thread++;
ret = number_of_thread;
if (thread_data == NULL)
{
thread_data = new;
new->prev = NULL;
new->next = NULL;
}
else
{
while (current->next != NULL)
current = current->next;
current->next = new;
new->prev = current;
new->next = NULL;
}
pthread_mutex_unlock(&thread_list_mutex);
return ret;
}
/*
* Remove a thread_data structure from the list.
*/
int tftpd_list_remove(struct thread_data *old)
{
pthread_mutex_lock(&thread_list_mutex);
struct thread_data *current = thread_data;
int ret;
number_of_thread--;
ret = number_of_thread;
if (thread_data == old)
{
if (thread_data->next != NULL)
{
thread_data = thread_data->next;
thread_data->prev = NULL;
}
else
thread_data = NULL;
}
else
{
while (current != old)
current = current->next;
if (current->next && current->prev)
{
current->prev->next = current->next;
current->next->prev = current->prev;
}
else
current->prev->next = NULL;
}
pthread_mutex_unlock(&thread_list_mutex);
return ret;
}
/*
* Return the number of threads actually started.
*/
int tftpd_list_num_of_thread(void)
{
int ret;
pthread_mutex_lock(&thread_list_mutex);
ret = number_of_thread;
pthread_mutex_unlock(&thread_list_mutex);
return ret;
}
/*
* This function looks for a thread serving exactly the same
* file and with the same options as another client. This implies a
* multicast enabled client.
*/
int tftpd_list_find_multicast_server_and_add(struct thread_data **thread,
struct thread_data *data,
struct client_info *client)
{
/* lock the whole list before walking it */
pthread_mutex_lock(&thread_list_mutex);
struct thread_data *current = thread_data; /* head of the list */
struct tftp_opt *tftp_options = data->tftp_options;
struct client_info *tmp;
char options[MAXLEN];
char string[MAXLEN];
char *index;
int len;
*thread = NULL;
opt_request_to_string(tftp_options, options, MAXLEN);
index = strstr(options, "multicast");
len = (int)((unsigned long)index - (unsigned long)options);
while (current)
{
if (current != data)
{
/* Lock the client list before reading client ready state */
pthread_mutex_lock(¤t->client_mutex);
if (current->client_ready == 1)
{
opt_request_to_string(current->tftp_options, string, MAXLEN);
/* must have exact same option string */
if (strncmp(string, options, len) == 0)
{
*thread = current;
/* insert the new client at the end. If the client is already
in the list, don't add it again. */
tmp = current->client_info;
while (1)
{
if (sockaddr_equal(&tmp->client, &client->client) &&
(tmp->done == 0))
{
/* unlock mutex and exit */
pthread_mutex_unlock(¤t->client_mutex);
pthread_mutex_unlock(&thread_list_mutex);
return 2;
}
if (tmp->next == NULL)
break;
tmp = tmp->next;
}
tmp->next = client;
/* unlock mutex and exit */
pthread_mutex_unlock(¤t->client_mutex);
pthread_mutex_unlock(&thread_list_mutex);
return 1;
}
}
pthread_mutex_unlock(¤t->client_mutex);
}
current = current->next;
}
pthread_mutex_unlock(&thread_list_mutex);
return 0;
}
inline void tftpd_clientlist_ready(struct thread_data *thread)
{
pthread_mutex_lock(&thread->client_mutex);
thread->client_ready = 1;
pthread_mutex_unlock(&thread->client_mutex);
}
/*
* As of the the above comment, we never remove the head element.
*/
void tftpd_clientlist_remove(struct thread_data *thread,
struct client_info *client)
{
pthread_mutex_lock(&thread->client_mutex);
struct client_info *tmp = thread->client_info;
while ((tmp->next != client) && (tmp->next != NULL))
tmp = tmp->next;
if (tmp->next == NULL)
return;
tmp->next = tmp->next->next;
pthread_mutex_unlock(&thread->client_mutex);
}
/*
* Free all allocated client structure.
*/
void tftpd_clientlist_free(struct thread_data *thread)
{
pthread_mutex_lock(&thread->client_mutex);
struct client_info *tmp;
struct client_info *head = thread->client_info;
while (head)
{
tmp = head;
head = head->next;
free(tmp);
}
pthread_mutex_unlock(&thread->client_mutex);
}
/*
* Mark a client as done
*/
int tftpd_clientlist_done(struct thread_data *thread,
struct client_info *client,
struct sockaddr_storage *sock)
{
pthread_mutex_lock(&thread->client_mutex);
struct client_info *head = thread->client_info;
if (client)
{
client->done = 1;
pthread_mutex_unlock(&thread->client_mutex);
return 1;
}
if (sock)
{
/* walk the list to find this client */
while (head)
{
if (sockaddr_equal(sock, &head->client))
{
head->done = 1;
pthread_mutex_unlock(&thread->client_mutex);
return 1;
}
else
head = head->next;
}
}
pthread_mutex_unlock(&thread->client_mutex);
return 0;
}
/*
* Return the next client in the list.
* If a client is found, *client got the address and return 1.
* If no more client are available, 0 is returned and client_ready is unset
* If the new client is the same, -1 is returned.
*
* List is search from the current client address and wrap around.
*/
int tftpd_clientlist_next(struct thread_data *thread,
struct client_info **client)
{
struct client_info *tmp;
pthread_mutex_lock(&thread->client_mutex);
/* If there's a next, start with it. Else search from the
beginning */
if ((*client)->next)
tmp = (*client)->next;
else
tmp = thread->client_info;
/* search from the current client */
while (tmp)
{
if (!tmp->done)
{
/* If this is the same as *client */
if (tmp == *client)
{
pthread_mutex_unlock(&thread->client_mutex);
return -1;
}
*client = tmp;
pthread_mutex_unlock(&thread->client_mutex);
return 1;
}
tmp = tmp->next;
/* if no more entry, start at begining */
if (tmp == NULL)
tmp = thread->client_info;
/* if we fall back on the current client, there is no more
client to serve */
if (tmp == *client)
tmp = NULL;
}
/* There is no more client to server */
thread->client_ready = 0;
*client = NULL;
pthread_mutex_unlock(&thread->client_mutex);
return 0;
}
void tftpd_list_kill_threads(void)
{
pthread_mutex_lock(&thread_list_mutex);
struct thread_data *current = thread_data; /* head of list */
while (current != NULL)
{
pthread_kill(current->tid, SIGTERM);
current = current->next;
}
pthread_mutex_unlock(&thread_list_mutex);
}