-
Notifications
You must be signed in to change notification settings - Fork 5
/
tftpd_mcast.c
357 lines (327 loc) · 7.1 KB
/
tftpd_mcast.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
350
351
352
353
354
355
356
357
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* tftpd_mcast.c
* support routine for multicast server
*
* $Id: tftpd_mcast.c,v 1.6 2003/04/25 00:16:19 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "tftpd.h"
#include "tftp_def.h"
#include "logger.h"
#define START 0
#define GET_ENUM 1
#define GET_GRP 2
#define EXPAND 3
pthread_mutex_t mcast_tid_list = PTHREAD_MUTEX_INITIALIZER;
int parse_ip(char *string, char **res_ip);
int parse_port(char *string, char **res_port);
struct tid {
char *addr;
short port;
int used;
struct tid *next;
};
struct tid *tid_list = NULL;
/*
* Return a free IP/Port for the multicast transfer
*/
int tftpd_mcast_get_tid(char **addr, short *port)
{
struct tid *current = tid_list;
pthread_mutex_lock(&mcast_tid_list);
/* walk the list for a free tid */
while (current != NULL)
{
if (current->used == 0)
{
*addr = current->addr;
*port = current->port;
current->used = 1;
pthread_mutex_unlock(&mcast_tid_list);
return OK;
}
else
current = current->next;
}
pthread_mutex_unlock(&mcast_tid_list);
return ERR;
}
int tftpd_mcast_free_tid(char *addr, short port)
{
struct tid *current = tid_list;
pthread_mutex_lock(&mcast_tid_list);
while (current != NULL)
{
if ((current->used == 1) && (current->port == port) &&
(strcmp(current->addr, addr) == 0))
{
current->used = 0;
pthread_mutex_unlock(&mcast_tid_list);
return OK;
}
else
current = current->next;
}
pthread_mutex_unlock(&mcast_tid_list);
return ERR;
}
/* valid address specification:
239.255.0.0-239.255.0.20
239.255.0.0-20
239.255.0.0,1,2,3,8,10
*/
/* valid port specification
1758
1758-1780
1758,1760,4000
*/
int tftpd_mcast_parse_opt(char *addr, char *ports)
{
char *ip;
char *port;
struct tid *current = NULL;
struct tid *tmp = NULL;
while (1)
{
if (parse_ip(addr, &ip) != OK)
{
printf("unable to parse IP address\n");
return ERR;
}
if (ip == NULL)
return OK;
while (1)
{
if (parse_port(ports, &port) != OK)
{
printf("unable to parse port\n");
return ERR;
}
if (port == NULL)
break;
/* add this ip/port to the tid list */
tmp = malloc(sizeof(struct tid));
tmp->addr = strdup(ip);
tmp->port = (short)atoi(port);
tmp->used = 0;
tmp->next = NULL;
if (tid_list == NULL)
{
tid_list = tmp;
current = tid_list;
}
else
{
current->next = tmp;
current = tmp;
}
}
}
}
void tftpd_mcast_clean(void)
{
}
int parse_ip(char *string, char **res_ip)
{
static int state = START;
static char s[MAXLEN];
static char *saveptr;
static char s2[MAXLEN];
static char *saveptr2;
static int ip[4];
static int tmp_ip[4];
static int i;
int res;
char *tmp = NULL, *tmp2 = NULL;
static char out[MAXLEN];
*res_ip = NULL;
while (1)
{
switch (state)
{
case START:
Strncpy(s, string, MAXLEN);
tmp = strtok_r(s, ",", &saveptr);
if (tmp == NULL)
{
state = START;
return ERR;
}
else
state = GET_GRP;
break;
case GET_ENUM:
tmp = strtok_r(NULL, ",", &saveptr);
if (tmp == NULL)
{
state = START;
return OK;
}
else
state = GET_GRP;
break;
case GET_GRP:
Strncpy(s2, tmp, MAXLEN);
tmp = strtok_r(s2, "-", &saveptr2);
if (tmp == NULL)
{
state = START;
return ERR;
}
res = sscanf(tmp, "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1],
&tmp_ip[2], &tmp_ip[3]);
if (res == 4)
{
for (i=0; i < 4; i++)
ip[i] = tmp_ip[i];
}
else
{
if (res == 1)
{
ip[3] = tmp_ip[0];
}
else
{
state = START;
return ERR;
}
}
tmp2 = strtok_r(NULL, "-", &saveptr2);
if (tmp2 == NULL)
{
state = GET_ENUM;
snprintf(out, sizeof(out), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
*res_ip = out;
return OK;
}
else
{
sscanf(tmp2, "%d", &tmp_ip[0]);
i = ip[3];
if (i >= tmp_ip[0])
{
logger(LOG_ERR, "Bad address range: %d.%d.%d.%d-%d",
ip[0], ip[1], ip[2], ip[3], tmp_ip[0]);
return ERR;
}
state = EXPAND;
}
break;
case EXPAND:
if (i > tmp_ip[0])
{
state = GET_ENUM;
break;
}
snprintf(out, sizeof(out), "%d.%d.%d.%d", ip[0], ip[1], ip[2], i);
i++;
*res_ip = out;
return OK;
break;
}
}
}
int parse_port(char *string, char **res_port)
{
static int state = START;
static char s[MAXLEN];
static char *saveptr;
static char s2[MAXLEN];
static char *saveptr2;
static int port;
static int tmp_port;
static int i;
int res;
char *tmp = NULL, *tmp2 = NULL;
static char out[MAXLEN];
*res_port = NULL;
while (1)
{
switch (state)
{
case START:
Strncpy(s, string, MAXLEN);
tmp = strtok_r(s, ",", &saveptr);
if (tmp == NULL)
{
state = START;
return ERR;
}
else
state = GET_GRP;
break;
case GET_ENUM:
tmp = strtok_r(NULL, ",", &saveptr);
if (tmp == NULL)
{
state = START;
return OK;
}
else
state = GET_GRP;
break;
case GET_GRP:
Strncpy(s2, tmp, MAXLEN);
tmp = strtok_r(s2, "-", &saveptr2);
if (tmp == NULL)
{
state = START;
return ERR;
}
res = sscanf(tmp, "%d", &port);
if (res != 1)
{
state = START;
return ERR;
}
tmp2 = strtok_r(NULL, "-", &saveptr2);
if (tmp2 == NULL)
{
state = GET_ENUM;
snprintf(out, sizeof(out), "%d", port);
*res_port = out;
return OK;
}
else
{
sscanf(tmp2, "%d", &tmp_port);
i = port;
if (i >= tmp_port)
{
logger(LOG_ERR, "Bad port range: %d-%d", i, tmp_port);
return ERR;
}
state = EXPAND;
}
break;
case EXPAND:
if (i > tmp_port)
{
state = GET_ENUM;
break;
}
snprintf(out, sizeof(out), "%d", i);
i++;
*res_port = out;
return OK;
break;
}
}
}