1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// lib/tftp_server.c
|
4 |
|
|
//
|
5 |
|
|
// TFTP server support
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): gthomas
|
43 |
|
|
// Contributors: gthomas, hmt, andrew.lunn@ascom.ch (Andrew Lunn)
|
44 |
|
|
// Date: 2000-04-06
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
//
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//==========================================================================
|
52 |
|
|
|
53 |
|
|
// TFTP server support
|
54 |
|
|
|
55 |
|
|
#include <network.h> // Basic networking support
|
56 |
|
|
#include <arpa/tftp.h> // TFTP protocol definitions
|
57 |
|
|
#include <tftp_support.h> // TFTPD structures
|
58 |
|
|
#include <cyg/kernel/kapi.h>
|
59 |
|
|
#include <stdlib.h> // For malloc
|
60 |
|
|
#include <fcntl.h>
|
61 |
|
|
|
62 |
|
|
#define nCYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
63 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
64 |
|
|
|
65 |
|
|
struct subinfo {
|
66 |
|
|
int rx;
|
67 |
|
|
int rx_repeat;
|
68 |
|
|
int rx_skip;
|
69 |
|
|
int send;
|
70 |
|
|
int resend;
|
71 |
|
|
};
|
72 |
|
|
|
73 |
|
|
struct info {
|
74 |
|
|
struct subinfo ack, data;
|
75 |
|
|
int err_send;
|
76 |
|
|
int total_transactions;
|
77 |
|
|
};
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
static struct info tftp_server_instrument = {
|
81 |
|
|
{ 0,0,0,0,0 },
|
82 |
|
|
{ 0,0,0,0,0 },
|
83 |
|
|
0, 0,
|
84 |
|
|
};
|
85 |
|
|
|
86 |
|
|
#endif // CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
87 |
|
|
|
88 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
89 |
|
|
struct tftpd_sem {
|
90 |
|
|
int port;
|
91 |
|
|
cyg_sem_t sem;
|
92 |
|
|
};
|
93 |
|
|
|
94 |
|
|
static struct tftpd_sem tftpd_sems[CYGNUM_NET_TFTPD_MULTITHREADED_PORTS];
|
95 |
|
|
#endif //CYGSEM_NET_TFTPD_MULTITHREADED
|
96 |
|
|
|
97 |
|
|
#define STACK_SIZE (((CYGPKG_NET_TFTPD_THREAD_STACK_SIZE) + CYGARC_ALIGNMENT-1) & ~(CYGARC_ALIGNMENT-1))
|
98 |
|
|
static char *TFTP_tag = "TFTPD";
|
99 |
|
|
#define CYGNUM_NET_MAX_INET_PROTOS 2
|
100 |
|
|
struct tftp_server {
|
101 |
|
|
char *tag;
|
102 |
|
|
char stack[STACK_SIZE];
|
103 |
|
|
cyg_thread thread_data;
|
104 |
|
|
cyg_handle_t thread_handle;
|
105 |
|
|
int port;
|
106 |
|
|
struct tftpd_fileops *ops;
|
107 |
|
|
int s[CYGNUM_NET_MAX_INET_PROTOS], num_s;
|
108 |
|
|
struct addrinfo *res;
|
109 |
|
|
};
|
110 |
|
|
|
111 |
|
|
static char * errmsg[] = {
|
112 |
|
|
"Undefined error code", // 0 nothing defined
|
113 |
|
|
"File not found", // 1 TFTP_ENOTFOUND
|
114 |
|
|
"Access violation", // 2 TFTP_EACCESS
|
115 |
|
|
"Disk full or allocation exceeded", // 3 TFTP_ENOSPACE
|
116 |
|
|
"Illegal TFTP operation", // 4 TFTP_EBADOP
|
117 |
|
|
"Unknown transfer ID", // 5 TFTP_EBADID
|
118 |
|
|
"File already exists", // 6 TFTP_EEXISTS
|
119 |
|
|
"No such user", // 7 TFTP_ENOUSER
|
120 |
|
|
};
|
121 |
|
|
|
122 |
|
|
// Little helper function to set the port number in an address
|
123 |
|
|
static void set_port(struct sockaddr * address, int port) {
|
124 |
|
|
|
125 |
|
|
switch (address->sa_family) {
|
126 |
|
|
case AF_INET:{
|
127 |
|
|
struct sockaddr_in *addr = (struct sockaddr_in *)address;
|
128 |
|
|
addr->sin_port = ntohs(port);
|
129 |
|
|
break;
|
130 |
|
|
}
|
131 |
|
|
#ifdef CYGPKG_NET_INET6
|
132 |
|
|
case AF_INET6:
|
133 |
|
|
{
|
134 |
|
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)address;
|
135 |
|
|
addr->sin6_port = ntohs(port);
|
136 |
|
|
break;
|
137 |
|
|
}
|
138 |
|
|
#endif
|
139 |
|
|
default:
|
140 |
|
|
break;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
145 |
|
|
|
146 |
|
|
// Allocate a semaphore for a given port number if one is not already
|
147 |
|
|
// allocated.
|
148 |
|
|
static void sem_alloc(int port) {
|
149 |
|
|
int i;
|
150 |
|
|
|
151 |
|
|
CYG_ASSERT(port != 0, "Invalid port number");
|
152 |
|
|
|
153 |
|
|
cyg_scheduler_lock(); // Avoid race with other tftpd's
|
154 |
|
|
for (i=0; i < CYGNUM_NET_TFTPD_MULTITHREADED_PORTS; i++) {
|
155 |
|
|
if (tftpd_sems[i].port == port) {
|
156 |
|
|
cyg_scheduler_unlock();
|
157 |
|
|
return;
|
158 |
|
|
}
|
159 |
|
|
if (tftpd_sems[i].port == 0) {
|
160 |
|
|
tftpd_sems[i].port = port;
|
161 |
|
|
cyg_semaphore_init(&tftpd_sems[i].sem,1);
|
162 |
|
|
cyg_scheduler_unlock();
|
163 |
|
|
return ;
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
cyg_scheduler_unlock();
|
167 |
|
|
diag_printf("TFTPD: Unable to allocate a semaphore for port %d\n",port);
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
// Wait on the semaphore for a given port number.
|
171 |
|
|
static void sem_wait(int port) {
|
172 |
|
|
int i;
|
173 |
|
|
CYG_ASSERT(port != 0, "Invalid port number");
|
174 |
|
|
|
175 |
|
|
for (i=0; i < CYGNUM_NET_TFTPD_MULTITHREADED_PORTS; i++) {
|
176 |
|
|
if (tftpd_sems[i].port == port) {
|
177 |
|
|
cyg_semaphore_wait(&tftpd_sems[i].sem);
|
178 |
|
|
return;
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
diag_printf("TFTPD: No semaphore for port %d\n",port);
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
// Release the semaphore for a given port number.
|
185 |
|
|
static void sem_post(int port) {
|
186 |
|
|
int i;
|
187 |
|
|
CYG_ASSERT(port != 0, "Invalid port number");
|
188 |
|
|
|
189 |
|
|
for (i=0; i < CYGNUM_NET_TFTPD_MULTITHREADED_PORTS; i++) {
|
190 |
|
|
if (tftpd_sems[i].port == port) {
|
191 |
|
|
cyg_semaphore_post(&tftpd_sems[i].sem);
|
192 |
|
|
return;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
diag_printf("TFTPD: No semaphore for port %d\n",port);
|
196 |
|
|
}
|
197 |
|
|
#endif
|
198 |
|
|
|
199 |
|
|
/* Send an error packet to the client */
|
200 |
|
|
static void
|
201 |
|
|
tftpd_send_error(int s, struct tftphdr * reply, int err,
|
202 |
|
|
struct sockaddr *from_addr, int from_len)
|
203 |
|
|
{
|
204 |
|
|
CYG_ASSERT( 0 <= err, "err underflow" );
|
205 |
|
|
CYG_ASSERT( sizeof(errmsg)/sizeof(errmsg[0]) > err, "err overflow" );
|
206 |
|
|
|
207 |
|
|
reply->th_opcode = htons(ERROR);
|
208 |
|
|
reply->th_code = htons(err);
|
209 |
|
|
if ( (0 > err) || (sizeof(errmsg)/sizeof(errmsg[0]) <= err) )
|
210 |
|
|
err = 0; // Do not copy a random string from hyperspace
|
211 |
|
|
strcpy(reply->th_msg, errmsg[err]);
|
212 |
|
|
sendto(s, reply, 4+strlen(reply->th_msg)+1, 0,
|
213 |
|
|
from_addr, from_len);
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
//
|
217 |
|
|
// Receive a file from the client
|
218 |
|
|
//
|
219 |
|
|
static void
|
220 |
|
|
tftpd_write_file(struct tftp_server *server,
|
221 |
|
|
struct tftphdr *hdr,
|
222 |
|
|
struct sockaddr *from_addr, int from_len)
|
223 |
|
|
{
|
224 |
|
|
char data_out[SEGSIZE+sizeof(struct tftphdr)];
|
225 |
|
|
char data_in[SEGSIZE+sizeof(struct tftphdr)];
|
226 |
|
|
struct tftphdr *reply = (struct tftphdr *)data_out;
|
227 |
|
|
struct tftphdr *response = (struct tftphdr *)data_in;
|
228 |
|
|
int fd, len, ok, tries, closed, data_len, s;
|
229 |
|
|
unsigned short block;
|
230 |
|
|
struct timeval timeout;
|
231 |
|
|
fd_set fds;
|
232 |
|
|
int total_timeouts = 0;
|
233 |
|
|
struct sockaddr client_addr;
|
234 |
|
|
struct addrinfo hints;
|
235 |
|
|
struct addrinfo *res;
|
236 |
|
|
socklen_t client_len;
|
237 |
|
|
int error;
|
238 |
|
|
|
239 |
|
|
memset(&hints,0,sizeof(hints));
|
240 |
|
|
hints.ai_family = from_addr->sa_family;
|
241 |
|
|
hints.ai_flags = AI_PASSIVE;
|
242 |
|
|
|
243 |
|
|
error = getaddrinfo(NULL,"tftp",&hints, &res);
|
244 |
|
|
if (0 != error) {
|
245 |
|
|
diag_printf("TFTPD: can't get a suitable local address: %s\n",
|
246 |
|
|
gai_strerror(error));
|
247 |
|
|
return;
|
248 |
|
|
}
|
249 |
|
|
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
250 |
|
|
if (s < 0) {
|
251 |
|
|
diag_printf("TFTPD: can't open socket for 'write_file'\n");
|
252 |
|
|
freeaddrinfo(res);
|
253 |
|
|
return;
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
// We want the stack to pick a free local port number
|
257 |
|
|
set_port(res->ai_addr,0);
|
258 |
|
|
|
259 |
|
|
if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
|
260 |
|
|
// Problem setting up my end
|
261 |
|
|
diag_printf("TFTPD: can't bind to reply port for 'write_file'\n");
|
262 |
|
|
close(s);
|
263 |
|
|
freeaddrinfo(res);
|
264 |
|
|
return;
|
265 |
|
|
}
|
266 |
|
|
if ((fd = (server->ops->open)(hdr->th_stuff, O_WRONLY|O_TRUNC|O_CREAT)) < 0) {
|
267 |
|
|
tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len);
|
268 |
|
|
close(s);
|
269 |
|
|
freeaddrinfo(res);
|
270 |
|
|
return;
|
271 |
|
|
}
|
272 |
|
|
ok = true;
|
273 |
|
|
closed = false;
|
274 |
|
|
block = 0;
|
275 |
|
|
while (ok) {
|
276 |
|
|
// Send ACK telling client he can send data
|
277 |
|
|
reply->th_opcode = htons(ACK);
|
278 |
|
|
reply->th_block = htons(block++); // postincrement
|
279 |
|
|
for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) {
|
280 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
281 |
|
|
tftp_server_instrument.ack.send++;
|
282 |
|
|
#endif
|
283 |
|
|
sendto(s, reply, 4, 0, from_addr, from_len);
|
284 |
|
|
repeat_select:
|
285 |
|
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
286 |
|
|
timeout.tv_usec = 0;
|
287 |
|
|
FD_ZERO(&fds);
|
288 |
|
|
FD_SET(s, &fds);
|
289 |
|
|
if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
|
290 |
|
|
if (++total_timeouts > TFTP_TIMEOUT_MAX) {
|
291 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
292 |
|
|
tftp_server_instrument.err_send++;
|
293 |
|
|
#endif
|
294 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
295 |
|
|
ok = false;
|
296 |
|
|
break;
|
297 |
|
|
}
|
298 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
299 |
|
|
tftp_server_instrument.ack.resend++;
|
300 |
|
|
#endif
|
301 |
|
|
continue; // retry the send, using up one retry.
|
302 |
|
|
}
|
303 |
|
|
// Some data has arrived
|
304 |
|
|
data_len = sizeof(data_in);
|
305 |
|
|
client_len = sizeof(client_addr);
|
306 |
|
|
if ((data_len = recvfrom(s, data_in, data_len, 0,
|
307 |
|
|
&client_addr, &client_len)) < 0) {
|
308 |
|
|
// What happened? No data here!
|
309 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
310 |
|
|
tftp_server_instrument.ack.resend++;
|
311 |
|
|
#endif
|
312 |
|
|
continue; // retry the send, using up one retry.
|
313 |
|
|
}
|
314 |
|
|
if (ntohs(response->th_opcode) == DATA &&
|
315 |
|
|
ntohs(response->th_block) < block) {
|
316 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
317 |
|
|
tftp_server_instrument.data.rx_repeat++;
|
318 |
|
|
#endif
|
319 |
|
|
// Then it is repeat DATA with an old block; listen again,
|
320 |
|
|
// but do not repeat sending the current ack, and do not
|
321 |
|
|
// use up a retry count. (we do re-send the ack if
|
322 |
|
|
// subsequently we time out)
|
323 |
|
|
goto repeat_select;
|
324 |
|
|
}
|
325 |
|
|
if (ntohs(response->th_opcode) == DATA &&
|
326 |
|
|
ntohs(response->th_block) == block) {
|
327 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
328 |
|
|
tftp_server_instrument.data.rx++;
|
329 |
|
|
#endif
|
330 |
|
|
// Good data - write to file
|
331 |
|
|
len = (server->ops->write)(fd, response->th_data, data_len-4);
|
332 |
|
|
if (len < (data_len-4)) {
|
333 |
|
|
// File is "full"
|
334 |
|
|
tftpd_send_error(s,reply,TFTP_ENOSPACE,
|
335 |
|
|
from_addr, from_len);
|
336 |
|
|
ok = false; // Give up
|
337 |
|
|
break; // out of the retries loop
|
338 |
|
|
}
|
339 |
|
|
if (data_len < (SEGSIZE+4)) {
|
340 |
|
|
// End of file
|
341 |
|
|
closed = true;
|
342 |
|
|
ok = false;
|
343 |
|
|
if ((server->ops->close)(fd) == -1) {
|
344 |
|
|
tftpd_send_error(s,reply,TFTP_EACCESS,
|
345 |
|
|
from_addr, from_len);
|
346 |
|
|
break; // out of the retries loop
|
347 |
|
|
}
|
348 |
|
|
// Exception to the loop structure: we must ACK the last
|
349 |
|
|
// packet, the one that implied EOF:
|
350 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
351 |
|
|
tftp_server_instrument.ack.send++;
|
352 |
|
|
#endif
|
353 |
|
|
reply->th_opcode = htons(ACK);
|
354 |
|
|
reply->th_block = htons(block++); // postincrement
|
355 |
|
|
sendto(s, reply, 4, 0, from_addr, from_len);
|
356 |
|
|
break; // out of the retries loop
|
357 |
|
|
}
|
358 |
|
|
// Happy! Break out of the retries loop.
|
359 |
|
|
break;
|
360 |
|
|
}
|
361 |
|
|
// Otherwise, we got something we do not understand! So repeat
|
362 |
|
|
// sending the current ACK, and use up a retry count.
|
363 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
364 |
|
|
if ( (ntohs(response->th_opcode) == DATA) )
|
365 |
|
|
tftp_server_instrument.data.rx_skip++;
|
366 |
|
|
tftp_server_instrument.ack.resend++;
|
367 |
|
|
#endif
|
368 |
|
|
} // End of the retries loop.
|
369 |
|
|
if (TFTP_RETRIES_MAX <= tries) {
|
370 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
371 |
|
|
tftp_server_instrument.err_send++;
|
372 |
|
|
#endif
|
373 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
374 |
|
|
ok = false;
|
375 |
|
|
}
|
376 |
|
|
}
|
377 |
|
|
close(s);
|
378 |
|
|
freeaddrinfo(res);
|
379 |
|
|
if (!closed) {
|
380 |
|
|
(server->ops->close)(fd);
|
381 |
|
|
}
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
//
|
385 |
|
|
// Send a file to the client
|
386 |
|
|
//
|
387 |
|
|
static void
|
388 |
|
|
tftpd_read_file(struct tftp_server *server,
|
389 |
|
|
struct tftphdr *hdr,
|
390 |
|
|
struct sockaddr *from_addr, int from_len)
|
391 |
|
|
{
|
392 |
|
|
char data_out[SEGSIZE+sizeof(struct tftphdr)];
|
393 |
|
|
char data_in[SEGSIZE+sizeof(struct tftphdr)];
|
394 |
|
|
struct tftphdr *reply = (struct tftphdr *)data_out;
|
395 |
|
|
struct tftphdr *response = (struct tftphdr *)data_in;
|
396 |
|
|
int fd, len, tries, ok, data_len, s;
|
397 |
|
|
unsigned short block;
|
398 |
|
|
struct timeval timeout;
|
399 |
|
|
fd_set fds;
|
400 |
|
|
int total_timeouts = 0;
|
401 |
|
|
struct sockaddr client_addr;
|
402 |
|
|
struct addrinfo hints;
|
403 |
|
|
struct addrinfo *res;
|
404 |
|
|
socklen_t client_len;
|
405 |
|
|
int error;
|
406 |
|
|
|
407 |
|
|
memset(&hints,0,sizeof(hints));
|
408 |
|
|
hints.ai_family = from_addr->sa_family;
|
409 |
|
|
hints.ai_flags = AI_PASSIVE;
|
410 |
|
|
|
411 |
|
|
error = getaddrinfo(NULL,"tftp",&hints, &res);
|
412 |
|
|
if (0 != error) {
|
413 |
|
|
diag_printf("TFTPD: can't get a suitable local address: %s\n",
|
414 |
|
|
gai_strerror(error));
|
415 |
|
|
return;
|
416 |
|
|
}
|
417 |
|
|
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
418 |
|
|
if (s < 0) {
|
419 |
|
|
diag_printf("TFTPD: can't open socket for 'write_file'\n");
|
420 |
|
|
freeaddrinfo(res);
|
421 |
|
|
return;
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
// We want the stack to pick a free local port number
|
425 |
|
|
set_port(res->ai_addr,0);
|
426 |
|
|
|
427 |
|
|
if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
|
428 |
|
|
// Problem setting up my end
|
429 |
|
|
diag_printf("TFTPD: can't bind to reply port for 'write_file'\n");
|
430 |
|
|
close(s);
|
431 |
|
|
freeaddrinfo(res);
|
432 |
|
|
return;
|
433 |
|
|
}
|
434 |
|
|
|
435 |
|
|
if ((fd = (server->ops->open)(hdr->th_stuff, O_RDONLY)) < 0) {
|
436 |
|
|
tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len);
|
437 |
|
|
close(s);
|
438 |
|
|
freeaddrinfo(res);
|
439 |
|
|
return;
|
440 |
|
|
}
|
441 |
|
|
block = 0;
|
442 |
|
|
ok = true;
|
443 |
|
|
while (ok) {
|
444 |
|
|
// Read next chunk of file
|
445 |
|
|
len = (server->ops->read)(fd, reply->th_data, SEGSIZE);
|
446 |
|
|
reply->th_block = htons(++block); // preincrement
|
447 |
|
|
reply->th_opcode = htons(DATA);
|
448 |
|
|
for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) {
|
449 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
450 |
|
|
tftp_server_instrument.data.send++;
|
451 |
|
|
#endif
|
452 |
|
|
if (sendto(s, reply, 4+len, 0,
|
453 |
|
|
from_addr, from_len) < 0) {
|
454 |
|
|
// Something went wrong with the network!
|
455 |
|
|
ok = false;
|
456 |
|
|
break;
|
457 |
|
|
}
|
458 |
|
|
repeat_select:
|
459 |
|
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
460 |
|
|
timeout.tv_usec = 0;
|
461 |
|
|
FD_ZERO(&fds);
|
462 |
|
|
FD_SET(s, &fds);
|
463 |
|
|
if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
|
464 |
|
|
if (++total_timeouts > TFTP_TIMEOUT_MAX) {
|
465 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
466 |
|
|
tftp_server_instrument.err_send++;
|
467 |
|
|
#endif
|
468 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
469 |
|
|
ok = false;
|
470 |
|
|
break;
|
471 |
|
|
}
|
472 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
473 |
|
|
tftp_server_instrument.data.resend++;
|
474 |
|
|
#endif
|
475 |
|
|
continue; // retry the send, using up one retry.
|
476 |
|
|
}
|
477 |
|
|
data_len = sizeof(data_in);
|
478 |
|
|
client_len = sizeof(client_addr);
|
479 |
|
|
if ((data_len = recvfrom(s, data_in, data_len, 0,
|
480 |
|
|
&client_addr,
|
481 |
|
|
&client_len)) < 0) {
|
482 |
|
|
// What happened? Maybe someone lied to us...
|
483 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
484 |
|
|
tftp_server_instrument.data.resend++;
|
485 |
|
|
#endif
|
486 |
|
|
continue; // retry the send, using up one retry.
|
487 |
|
|
}
|
488 |
|
|
if ((ntohs(response->th_opcode) == ACK) &&
|
489 |
|
|
(ntohs(response->th_block) < block)) {
|
490 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
491 |
|
|
tftp_server_instrument.ack.rx_repeat++;
|
492 |
|
|
#endif
|
493 |
|
|
// Then it is a repeat ACK for an old block; listen again,
|
494 |
|
|
// but do not repeat sending the current block, and do not
|
495 |
|
|
// use up a retry count. (we do re-send the data if
|
496 |
|
|
// subsequently we time out)
|
497 |
|
|
goto repeat_select;
|
498 |
|
|
}
|
499 |
|
|
if ((ntohs(response->th_opcode) == ACK) &&
|
500 |
|
|
(ntohs(response->th_block) == block)) {
|
501 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
502 |
|
|
tftp_server_instrument.ack.rx++;
|
503 |
|
|
#endif
|
504 |
|
|
// Happy! Break out of the retries loop.
|
505 |
|
|
break;
|
506 |
|
|
}
|
507 |
|
|
// Otherwise, we got something we do not understand! So repeat
|
508 |
|
|
// sending the current block, and use up a retry count.
|
509 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
510 |
|
|
if ( (ntohs(response->th_opcode) == ACK) )
|
511 |
|
|
tftp_server_instrument.ack.rx_skip++;
|
512 |
|
|
tftp_server_instrument.data.resend++;
|
513 |
|
|
#endif
|
514 |
|
|
} // End of the retries loop.
|
515 |
|
|
if (TFTP_RETRIES_MAX <= tries) {
|
516 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
517 |
|
|
tftp_server_instrument.err_send++;
|
518 |
|
|
#endif
|
519 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
520 |
|
|
ok = false;
|
521 |
|
|
}
|
522 |
|
|
if (len < SEGSIZE) {
|
523 |
|
|
break; // That's end of file then.
|
524 |
|
|
}
|
525 |
|
|
}
|
526 |
|
|
close(s);
|
527 |
|
|
freeaddrinfo(res);
|
528 |
|
|
(server->ops->close)(fd);
|
529 |
|
|
}
|
530 |
|
|
|
531 |
|
|
//
|
532 |
|
|
// Actual TFTP server
|
533 |
|
|
//
|
534 |
|
|
|
535 |
|
|
static void
|
536 |
|
|
tftpd_server(cyg_addrword_t p)
|
537 |
|
|
{
|
538 |
|
|
struct tftp_server *server = (struct tftp_server *)p;
|
539 |
|
|
int max_s = 0;
|
540 |
|
|
int data_len, recv_len;
|
541 |
|
|
socklen_t from_len;
|
542 |
|
|
struct sockaddr from_addr;
|
543 |
|
|
char data[SEGSIZE+sizeof(struct tftphdr)];
|
544 |
|
|
struct tftphdr *hdr = (struct tftphdr *)data;
|
545 |
|
|
struct addrinfo hints;
|
546 |
|
|
struct addrinfo *ai;
|
547 |
|
|
fd_set readfds;
|
548 |
|
|
char name[64];
|
549 |
|
|
int error;
|
550 |
|
|
int i;
|
551 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
552 |
|
|
struct info o = tftp_server_instrument;
|
553 |
|
|
#endif
|
554 |
|
|
|
555 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
556 |
|
|
// Otherwise routine printfs fail the test - interrupts disabled too long.
|
557 |
|
|
diag_printf("TFTPD [%x]: port %d\n", p, server->port);
|
558 |
|
|
#endif
|
559 |
|
|
|
560 |
|
|
memset(&hints,0,sizeof(hints));
|
561 |
|
|
hints.ai_family = PF_UNSPEC;
|
562 |
|
|
hints.ai_flags = AI_PASSIVE;
|
563 |
|
|
|
564 |
|
|
error = getaddrinfo(NULL,"tftp",&hints, &server->res);
|
565 |
|
|
if (0 != error) {
|
566 |
|
|
diag_printf("TFTPD [%d] : can't get a local server address to bind to: %s\n",
|
567 |
|
|
p, gai_strerror(error));
|
568 |
|
|
return;
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
// If the port is 0, we need to use the default TFTP port. Extrace
|
572 |
|
|
// the port number from one of the addresses returned by
|
573 |
|
|
// getaddrinfo.
|
574 |
|
|
if (server->port == 0) {
|
575 |
|
|
switch (server->res->ai_family) {
|
576 |
|
|
case AF_INET:
|
577 |
|
|
{
|
578 |
|
|
struct sockaddr_in *addr = (struct sockaddr_in *)server->res->ai_addr;
|
579 |
|
|
server->port = ntohs(addr->sin_port);
|
580 |
|
|
break;
|
581 |
|
|
}
|
582 |
|
|
#ifdef CYGPKG_NET_INET6
|
583 |
|
|
case AF_INET6:
|
584 |
|
|
{
|
585 |
|
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)server->res->ai_addr;
|
586 |
|
|
server->port = ntohs(addr->sin6_port);
|
587 |
|
|
break;
|
588 |
|
|
}
|
589 |
|
|
#endif
|
590 |
|
|
default:
|
591 |
|
|
break;
|
592 |
|
|
}
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
596 |
|
|
sem_alloc(server->port);
|
597 |
|
|
while (true) {
|
598 |
|
|
#endif
|
599 |
|
|
// Iterate over the addresses and create a local port to listen for requests
|
600 |
|
|
ai = server->res;
|
601 |
|
|
memset(server->s,0,sizeof(server->s));
|
602 |
|
|
server->num_s = 0;
|
603 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
604 |
|
|
sem_wait(server->port);
|
605 |
|
|
#endif
|
606 |
|
|
while (ai && (server->num_s < CYGNUM_NET_MAX_INET_PROTOS)) {
|
607 |
|
|
server->s[server->num_s] = socket(ai->ai_family,
|
608 |
|
|
ai->ai_socktype,
|
609 |
|
|
ai->ai_protocol);
|
610 |
|
|
if (server->s[server->num_s] < 0 ) {
|
611 |
|
|
diag_printf("TFTPD [%x]: can't open socket\n", p);
|
612 |
|
|
freeaddrinfo(server->res);
|
613 |
|
|
server->res = NULL;
|
614 |
|
|
return;
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
set_port(ai->ai_addr, server->port);
|
618 |
|
|
|
619 |
|
|
if (bind(server->s[server->num_s],ai->ai_addr, ai->ai_addrlen) < 0) {
|
620 |
|
|
// Problem setting up my end
|
621 |
|
|
diag_printf("TFTPD [%x]: can't bind to server port\n",p);
|
622 |
|
|
close(server->s[server->num_s]);
|
623 |
|
|
server->s[server->num_s] = 0;
|
624 |
|
|
ai = ai->ai_next;
|
625 |
|
|
continue;
|
626 |
|
|
}
|
627 |
|
|
// We need to know the highest socket number for select.
|
628 |
|
|
if (server->s[server->num_s] > max_s)
|
629 |
|
|
max_s = server->s[server->num_s];
|
630 |
|
|
server->num_s++;
|
631 |
|
|
ai = ai->ai_next;
|
632 |
|
|
}
|
633 |
|
|
|
634 |
|
|
#ifndef CYGSEM_NET_TFTPD_MULTITHREADED
|
635 |
|
|
while (true) {
|
636 |
|
|
#endif
|
637 |
|
|
FD_ZERO(&readfds);
|
638 |
|
|
for (i=0; i < CYGNUM_NET_MAX_INET_PROTOS; i++) {
|
639 |
|
|
if (server->s[i]) {
|
640 |
|
|
FD_SET(server->s[i],&readfds);
|
641 |
|
|
}
|
642 |
|
|
}
|
643 |
|
|
error = select(max_s+1,&readfds,NULL,NULL,NULL);
|
644 |
|
|
if ( -1 == error) {
|
645 |
|
|
diag_printf("TFTPD [%x]: error in select\n",p);
|
646 |
|
|
}
|
647 |
|
|
for (i=0; i < CYGNUM_NET_MAX_INET_PROTOS; i++) {
|
648 |
|
|
if (server->s[i] && FD_ISSET(server->s[i],&readfds)) {
|
649 |
|
|
recv_len = sizeof(data);
|
650 |
|
|
from_len = sizeof(from_addr);
|
651 |
|
|
data_len = recvfrom(server->s[i], hdr, recv_len, 0,
|
652 |
|
|
&from_addr, &from_len);
|
653 |
|
|
if ( data_len < 0) {
|
654 |
|
|
diag_printf("TFTPD [%x]: can't read request\n", p);
|
655 |
|
|
} else {
|
656 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
657 |
|
|
// Close the socket and post on the semaphore some
|
658 |
|
|
// another thread can start listening for requests. This
|
659 |
|
|
// is not quite right. select could of returned with more than
|
660 |
|
|
// one socket with data to read. Here we only deal with one of them
|
661 |
|
|
for (i=0; i < CYGNUM_NET_MAX_INET_PROTOS; i++) {
|
662 |
|
|
if (server->s[i]) {
|
663 |
|
|
close (server->s[i]);
|
664 |
|
|
server->s[i] = 0;
|
665 |
|
|
}
|
666 |
|
|
}
|
667 |
|
|
sem_post(server->port);
|
668 |
|
|
#endif
|
669 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
670 |
|
|
getnameinfo(&from_addr,sizeof(from_addr), name, sizeof(name),0,0,0);
|
671 |
|
|
diag_printf("TFTPD [%x]: received %x from %s\n", p,
|
672 |
|
|
ntohs(hdr->th_opcode), name);
|
673 |
|
|
#endif
|
674 |
|
|
switch (ntohs(hdr->th_opcode)) {
|
675 |
|
|
case WRQ:
|
676 |
|
|
tftpd_write_file(server, hdr, &from_addr, from_len);
|
677 |
|
|
break;
|
678 |
|
|
case RRQ:
|
679 |
|
|
tftpd_read_file(server, hdr, &from_addr, from_len);
|
680 |
|
|
break;
|
681 |
|
|
case ACK:
|
682 |
|
|
case DATA:
|
683 |
|
|
case ERROR:
|
684 |
|
|
// Ignore
|
685 |
|
|
break;
|
686 |
|
|
default:
|
687 |
|
|
getnameinfo(&from_addr,sizeof(from_addr), name, sizeof(name),0,0,0);
|
688 |
|
|
diag_printf("TFTPD [%x]: bogus request %x from %s\n", p,
|
689 |
|
|
ntohs(hdr->th_opcode),
|
690 |
|
|
name);
|
691 |
|
|
tftpd_send_error(server->s[i],hdr,TFTP_EBADOP,&from_addr,from_len);
|
692 |
|
|
}
|
693 |
|
|
|
694 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
695 |
|
|
tftp_server_instrument.total_transactions++;
|
696 |
|
|
|
697 |
|
|
o.data.rx -= tftp_server_instrument.data.rx ;
|
698 |
|
|
o.data.rx_repeat -= tftp_server_instrument.data.rx_repeat;
|
699 |
|
|
o.data.rx_skip -= tftp_server_instrument.data.rx_skip ;
|
700 |
|
|
o.data.send -= tftp_server_instrument.data.send ;
|
701 |
|
|
o.data.resend -= tftp_server_instrument.data.resend ;
|
702 |
|
|
|
703 |
|
|
o.ack.rx -= tftp_server_instrument.ack.rx ;
|
704 |
|
|
o.ack.rx_repeat -= tftp_server_instrument.ack.rx_repeat ;
|
705 |
|
|
o.ack.rx_skip -= tftp_server_instrument.ack.rx_skip ;
|
706 |
|
|
o.ack.send -= tftp_server_instrument.ack.send ;
|
707 |
|
|
o.ack.resend -= tftp_server_instrument.ack.resend ;
|
708 |
|
|
|
709 |
|
|
o.err_send -= tftp_server_instrument.err_send ;
|
710 |
|
|
|
711 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
712 |
|
|
if ( o.data.rx ) diag_printf( "data rx %4d\n", -o.data.rx );
|
713 |
|
|
if ( o.data.rx_repeat ) diag_printf( "data rx_repeat%4d\n", -o.data.rx_repeat );
|
714 |
|
|
if ( o.data.rx_skip ) diag_printf( "data rx_skip %4d\n", -o.data.rx_skip );
|
715 |
|
|
if ( o.data.send ) diag_printf( "data send %4d\n", -o.data.send );
|
716 |
|
|
if ( o.data.resend ) diag_printf( "data resend %4d\n", -o.data.resend );
|
717 |
|
|
|
718 |
|
|
if ( o.ack.rx ) diag_printf( " ack rx %4d\n", -o.ack.rx );
|
719 |
|
|
if ( o.ack.rx_repeat ) diag_printf( " ack rx_repeat%4d\n", -o.ack.rx_repeat );
|
720 |
|
|
if ( o.ack.rx_skip ) diag_printf( " ack rx_skip %4d\n", -o.ack.rx_skip );
|
721 |
|
|
if ( o.ack.send ) diag_printf( " ack send %4d\n", -o.ack.send );
|
722 |
|
|
if ( o.ack.resend ) diag_printf( " ack resend %4d\n", -o.ack.resend );
|
723 |
|
|
|
724 |
|
|
if ( o.err_send ) diag_printf( "*error sends %4d\n", -o.err_send );
|
725 |
|
|
#endif // CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
726 |
|
|
#endif // CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
727 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
728 |
|
|
break;
|
729 |
|
|
#endif
|
730 |
|
|
}
|
731 |
|
|
}
|
732 |
|
|
}
|
733 |
|
|
// The following looks a little strange, but it keeps emacs's
|
734 |
|
|
// auto indention happy.
|
735 |
|
|
#ifndef CYGSEM_NET_TFTPD_MULTITHREADED
|
736 |
|
|
}
|
737 |
|
|
#endif
|
738 |
|
|
#ifdef CYGSEM_NET_TFTPD_MULTITHREADED
|
739 |
|
|
}
|
740 |
|
|
#endif
|
741 |
|
|
}
|
742 |
|
|
|
743 |
|
|
|
744 |
|
|
//
|
745 |
|
|
// This function is used to create a new server [thread] which supports
|
746 |
|
|
// the TFTP protocol on the given port. A server 'id' will be returned
|
747 |
|
|
// which can later be used to destroy the server.
|
748 |
|
|
//
|
749 |
|
|
// Note: all [memory] resources for the server thread will be allocated
|
750 |
|
|
// dynamically. If there are insufficient resources, an error will be
|
751 |
|
|
// returned.
|
752 |
|
|
//
|
753 |
|
|
int
|
754 |
|
|
tftpd_start(int port, struct tftpd_fileops *ops)
|
755 |
|
|
{
|
756 |
|
|
struct tftp_server *server;
|
757 |
|
|
#ifdef CYGSEM_TFTP_SERVER_MULTITHREADED
|
758 |
|
|
static char init = 0;
|
759 |
|
|
if ( 0 == init ) {
|
760 |
|
|
init++;
|
761 |
|
|
cyg_semaphore_init( &tftp_server_sem, 0 );
|
762 |
|
|
}
|
763 |
|
|
#endif
|
764 |
|
|
|
765 |
|
|
if ((server = malloc(sizeof(struct tftp_server)))) {
|
766 |
|
|
server->tag = TFTP_tag;
|
767 |
|
|
server->port = port;
|
768 |
|
|
server->ops = ops;
|
769 |
|
|
cyg_thread_create(CYGPKG_NET_TFTPD_THREAD_PRIORITY, // Priority
|
770 |
|
|
tftpd_server, // entry
|
771 |
|
|
(cyg_addrword_t)server, // entry parameter
|
772 |
|
|
"TFTP server", // Name
|
773 |
|
|
&server->stack[0], // Stack
|
774 |
|
|
STACK_SIZE, // Size
|
775 |
|
|
&server->thread_handle, // Handle
|
776 |
|
|
&server->thread_data // Thread data structure
|
777 |
|
|
);
|
778 |
|
|
cyg_thread_resume(server->thread_handle); // Start it
|
779 |
|
|
|
780 |
|
|
}
|
781 |
|
|
return (int)server;
|
782 |
|
|
}
|
783 |
|
|
|
784 |
|
|
//
|
785 |
|
|
// Destroy a TFTP server, using a previously created server 'id'.
|
786 |
|
|
//
|
787 |
|
|
int
|
788 |
|
|
tftpd_stop(int p)
|
789 |
|
|
{
|
790 |
|
|
struct tftp_server *server = (struct tftp_server *)p;
|
791 |
|
|
// Simple sanity check
|
792 |
|
|
if (server->tag == TFTP_tag) {
|
793 |
|
|
cyg_thread_kill(server->thread_handle);
|
794 |
|
|
cyg_thread_set_priority(server->thread_handle, 0);
|
795 |
|
|
cyg_thread_delay(1); // Make sure it gets to die...
|
796 |
|
|
if (cyg_thread_delete(server->thread_handle)) {
|
797 |
|
|
// Success shutting down the thread. Close all its sockets.
|
798 |
|
|
int i;
|
799 |
|
|
for (i = 0 ; i < CYGNUM_NET_MAX_INET_PROTOS; i++) {
|
800 |
|
|
if (server->s[i]) {
|
801 |
|
|
close (server->s[i]);
|
802 |
|
|
}
|
803 |
|
|
}
|
804 |
|
|
freeaddrinfo(server->res);
|
805 |
|
|
free(server); // Give up memory
|
806 |
|
|
return 1;
|
807 |
|
|
}
|
808 |
|
|
}
|
809 |
|
|
return 0;
|
810 |
|
|
}
|
811 |
|
|
|
812 |
|
|
// EOF tftp_server.c
|