1 |
27 |
unneback |
//==========================================================================
|
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 Red Hat, 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 version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//==========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): gthomas
|
44 |
|
|
// Contributors: gthomas, hmt, andrew.lunn@ascom.ch (Andrew Lunn)
|
45 |
|
|
// Date: 2000-04-06
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
// TFTP server support
|
55 |
|
|
|
56 |
|
|
#include <network.h> // Basic networking support
|
57 |
|
|
#include <arpa/tftp.h> // TFTP protocol definitions
|
58 |
|
|
#include <tftp_support.h> // TFTPD structures
|
59 |
|
|
#include <cyg/kernel/kapi.h>
|
60 |
|
|
#include <stdlib.h> // For malloc
|
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 |
|
|
#define STACK_SIZE (((CYGNUM_HAL_STACK_SIZE_TYPICAL+(3*(SEGSIZE+sizeof(struct tftphdr)))) + CYGARC_ALIGNMENT-1) & ~(CYGARC_ALIGNMENT-1))
|
89 |
|
|
static char *TFTP_tag = "TFTPD";
|
90 |
|
|
struct tftp_server {
|
91 |
|
|
char *tag;
|
92 |
|
|
char stack[STACK_SIZE];
|
93 |
|
|
cyg_thread thread_data;
|
94 |
|
|
cyg_handle_t thread_handle;
|
95 |
|
|
int port;
|
96 |
|
|
struct tftpd_fileops *ops;
|
97 |
|
|
};
|
98 |
|
|
|
99 |
|
|
static char * errmsg[] = {
|
100 |
|
|
"Undefined error code", // 0 nothing defined
|
101 |
|
|
"File not found", // 1 TFTP_ENOTFOUND
|
102 |
|
|
"Access violation", // 2 TFTP_EACCESS
|
103 |
|
|
"Disk full or allocation exceeded", // 3 TFTP_ENOSPACE
|
104 |
|
|
"Illegal TFTP operation", // 4 TFTP_EBADOP
|
105 |
|
|
"Unknown transfer ID", // 5 TFTP_EBADID
|
106 |
|
|
"File already exists", // 6 TFTP_EEXISTS
|
107 |
|
|
"No such user", // 7 TFTP_ENOUSER
|
108 |
|
|
};
|
109 |
|
|
|
110 |
|
|
/* Send an error packet to the client */
|
111 |
|
|
static void
|
112 |
|
|
tftpd_send_error(int s, struct tftphdr * reply, int err,
|
113 |
|
|
struct sockaddr_in *from_addr, int from_len)
|
114 |
|
|
{
|
115 |
|
|
CYG_ASSERT( 0 <= err, "err underflow" );
|
116 |
|
|
CYG_ASSERT( sizeof(errmsg)/sizeof(errmsg[0]) > err, "err overflow" );
|
117 |
|
|
|
118 |
|
|
reply->th_opcode = htons(ERROR);
|
119 |
|
|
reply->th_code = htons(err);
|
120 |
|
|
if ( (0 > err) || (sizeof(errmsg)/sizeof(errmsg[0]) <= err) )
|
121 |
|
|
err = 0; // Do not copy a random string from hyperspace
|
122 |
|
|
strcpy(reply->th_msg, errmsg[err]);
|
123 |
|
|
sendto(s, reply, 4+strlen(reply->th_msg)+1, 0,
|
124 |
|
|
(struct sockaddr *)from_addr, from_len);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
//
|
128 |
|
|
// Receive a file from the client
|
129 |
|
|
//
|
130 |
|
|
static void
|
131 |
|
|
tftpd_write_file(struct tftp_server *server,
|
132 |
|
|
struct tftphdr *hdr,
|
133 |
|
|
struct sockaddr_in *from_addr, int from_len)
|
134 |
|
|
{
|
135 |
|
|
char data_out[SEGSIZE+sizeof(struct tftphdr)];
|
136 |
|
|
char data_in[SEGSIZE+sizeof(struct tftphdr)];
|
137 |
|
|
struct tftphdr *reply = (struct tftphdr *)data_out;
|
138 |
|
|
struct tftphdr *response = (struct tftphdr *)data_in;
|
139 |
|
|
int fd, len, ok, tries, closed, data_len, s;
|
140 |
|
|
unsigned short block;
|
141 |
|
|
struct timeval timeout;
|
142 |
|
|
fd_set fds;
|
143 |
|
|
int total_timeouts = 0;
|
144 |
|
|
struct sockaddr_in client_addr, local_addr;
|
145 |
|
|
int client_len;
|
146 |
|
|
|
147 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
148 |
|
|
if (s < 0) {
|
149 |
|
|
diag_printf("TFTPD: can't open socket for 'write_file'\n");
|
150 |
|
|
return;
|
151 |
|
|
}
|
152 |
|
|
memset((char *)&local_addr, 0, sizeof(local_addr));
|
153 |
|
|
local_addr.sin_family = AF_INET;
|
154 |
|
|
local_addr.sin_len = sizeof(local_addr);
|
155 |
|
|
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
156 |
|
|
local_addr.sin_port = htons(INADDR_ANY);
|
157 |
|
|
if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
|
158 |
|
|
// Problem setting up my end
|
159 |
|
|
diag_printf("TFTPD: can't bind to reply port for 'write_file'\n");
|
160 |
|
|
close(s);
|
161 |
|
|
return;
|
162 |
|
|
}
|
163 |
|
|
if ((fd = (server->ops->open)(hdr->th_stuff, O_WRONLY)) < 0) {
|
164 |
|
|
tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len);
|
165 |
|
|
close(s);
|
166 |
|
|
return;
|
167 |
|
|
}
|
168 |
|
|
ok = true;
|
169 |
|
|
closed = false;
|
170 |
|
|
block = 0;
|
171 |
|
|
while (ok) {
|
172 |
|
|
// Send ACK telling client he can send data
|
173 |
|
|
reply->th_opcode = htons(ACK);
|
174 |
|
|
reply->th_block = htons(block++); // postincrement
|
175 |
|
|
for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) {
|
176 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
177 |
|
|
tftp_server_instrument.ack.send++;
|
178 |
|
|
#endif
|
179 |
|
|
sendto(s, reply, 4, 0, (struct sockaddr *)from_addr, from_len);
|
180 |
|
|
repeat_select:
|
181 |
|
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
182 |
|
|
timeout.tv_usec = 0;
|
183 |
|
|
FD_ZERO(&fds);
|
184 |
|
|
FD_SET(s, &fds);
|
185 |
|
|
if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
|
186 |
|
|
if (++total_timeouts > TFTP_TIMEOUT_MAX) {
|
187 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
188 |
|
|
tftp_server_instrument.err_send++;
|
189 |
|
|
#endif
|
190 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
191 |
|
|
ok = false;
|
192 |
|
|
break;
|
193 |
|
|
}
|
194 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
195 |
|
|
tftp_server_instrument.ack.resend++;
|
196 |
|
|
#endif
|
197 |
|
|
continue; // retry the send, using up one retry.
|
198 |
|
|
}
|
199 |
|
|
// Some data has arrived
|
200 |
|
|
data_len = sizeof(data_in);
|
201 |
|
|
client_len = sizeof(client_addr);
|
202 |
|
|
if ((data_len = recvfrom(s, data_in, data_len, 0,
|
203 |
|
|
(struct sockaddr *)&client_addr, &client_len)) < 0) {
|
204 |
|
|
// What happened? No data here!
|
205 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
206 |
|
|
tftp_server_instrument.ack.resend++;
|
207 |
|
|
#endif
|
208 |
|
|
continue; // retry the send, using up one retry.
|
209 |
|
|
}
|
210 |
|
|
if (ntohs(response->th_opcode) == DATA &&
|
211 |
|
|
ntohs(response->th_block) < block) {
|
212 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
213 |
|
|
tftp_server_instrument.data.rx_repeat++;
|
214 |
|
|
#endif
|
215 |
|
|
// Then it is repeat DATA with an old block; listen again,
|
216 |
|
|
// but do not repeat sending the current ack, and do not
|
217 |
|
|
// use up a retry count. (we do re-send the ack if
|
218 |
|
|
// subsequently we time out)
|
219 |
|
|
goto repeat_select;
|
220 |
|
|
}
|
221 |
|
|
if (ntohs(response->th_opcode) == DATA &&
|
222 |
|
|
ntohs(response->th_block) == block) {
|
223 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
224 |
|
|
tftp_server_instrument.data.rx++;
|
225 |
|
|
#endif
|
226 |
|
|
// Good data - write to file
|
227 |
|
|
len = (server->ops->write)(fd, response->th_data, data_len-4);
|
228 |
|
|
if (len < (data_len-4)) {
|
229 |
|
|
// File is "full"
|
230 |
|
|
tftpd_send_error(s,reply,TFTP_ENOSPACE,
|
231 |
|
|
from_addr, from_len);
|
232 |
|
|
ok = false; // Give up
|
233 |
|
|
break; // out of the retries loop
|
234 |
|
|
}
|
235 |
|
|
if (data_len < (SEGSIZE+4)) {
|
236 |
|
|
// End of file
|
237 |
|
|
closed = true;
|
238 |
|
|
ok = false;
|
239 |
|
|
if ((server->ops->close)(fd) == -1) {
|
240 |
|
|
tftpd_send_error(s,reply,TFTP_EACCESS,
|
241 |
|
|
from_addr, from_len);
|
242 |
|
|
break; // out of the retries loop
|
243 |
|
|
}
|
244 |
|
|
// Exception to the loop structure: we must ACK the last
|
245 |
|
|
// packet, the one that implied EOF:
|
246 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
247 |
|
|
tftp_server_instrument.ack.send++;
|
248 |
|
|
#endif
|
249 |
|
|
reply->th_opcode = htons(ACK);
|
250 |
|
|
reply->th_block = htons(block++); // postincrement
|
251 |
|
|
sendto(s, reply, 4, 0, (struct sockaddr *)from_addr, from_len);
|
252 |
|
|
break; // out of the retries loop
|
253 |
|
|
}
|
254 |
|
|
// Happy! Break out of the retries loop.
|
255 |
|
|
break;
|
256 |
|
|
}
|
257 |
|
|
// Otherwise, we got something we do not understand! So repeat
|
258 |
|
|
// sending the current ACK, and use up a retry count.
|
259 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
260 |
|
|
if ( (ntohs(response->th_opcode) == DATA) )
|
261 |
|
|
tftp_server_instrument.data.rx_skip++;
|
262 |
|
|
tftp_server_instrument.ack.resend++;
|
263 |
|
|
#endif
|
264 |
|
|
} // End of the retries loop.
|
265 |
|
|
if (TFTP_RETRIES_MAX <= tries) {
|
266 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
267 |
|
|
tftp_server_instrument.err_send++;
|
268 |
|
|
#endif
|
269 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
270 |
|
|
ok = false;
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
|
|
close(s);
|
274 |
|
|
if (!closed) {
|
275 |
|
|
(server->ops->close)(fd);
|
276 |
|
|
}
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
//
|
280 |
|
|
// Send a file to the client
|
281 |
|
|
//
|
282 |
|
|
static void
|
283 |
|
|
tftpd_read_file(struct tftp_server *server,
|
284 |
|
|
struct tftphdr *hdr,
|
285 |
|
|
struct sockaddr_in *from_addr, int from_len)
|
286 |
|
|
{
|
287 |
|
|
char data_out[SEGSIZE+sizeof(struct tftphdr)];
|
288 |
|
|
char data_in[SEGSIZE+sizeof(struct tftphdr)];
|
289 |
|
|
struct tftphdr *reply = (struct tftphdr *)data_out;
|
290 |
|
|
struct tftphdr *response = (struct tftphdr *)data_in;
|
291 |
|
|
int fd, len, tries, ok, data_len, s;
|
292 |
|
|
unsigned short block;
|
293 |
|
|
struct timeval timeout;
|
294 |
|
|
fd_set fds;
|
295 |
|
|
int total_timeouts = 0;
|
296 |
|
|
struct sockaddr_in client_addr, local_addr;
|
297 |
|
|
int client_len;
|
298 |
|
|
|
299 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
300 |
|
|
if (s < 0) {
|
301 |
|
|
diag_printf("TFTPD: can't open socket for 'read_file'\n");
|
302 |
|
|
return;
|
303 |
|
|
}
|
304 |
|
|
memset((char *)&local_addr, 0, sizeof(local_addr));
|
305 |
|
|
local_addr.sin_family = AF_INET;
|
306 |
|
|
local_addr.sin_len = sizeof(local_addr);
|
307 |
|
|
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
308 |
|
|
local_addr.sin_port = htons(INADDR_ANY);
|
309 |
|
|
if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
|
310 |
|
|
// Problem setting up my end
|
311 |
|
|
diag_printf("TFTPD: can't bind to reply port for 'read_file'\n");
|
312 |
|
|
close(s);
|
313 |
|
|
return;
|
314 |
|
|
}
|
315 |
|
|
if ((fd = (server->ops->open)(hdr->th_stuff, O_RDONLY)) < 0) {
|
316 |
|
|
tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len);
|
317 |
|
|
close(s);
|
318 |
|
|
return;
|
319 |
|
|
}
|
320 |
|
|
block = 0;
|
321 |
|
|
ok = true;
|
322 |
|
|
while (ok) {
|
323 |
|
|
// Read next chunk of file
|
324 |
|
|
len = (server->ops->read)(fd, reply->th_data, SEGSIZE);
|
325 |
|
|
reply->th_block = htons(++block); // preincrement
|
326 |
|
|
reply->th_opcode = htons(DATA);
|
327 |
|
|
for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) {
|
328 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
329 |
|
|
tftp_server_instrument.data.send++;
|
330 |
|
|
#endif
|
331 |
|
|
if (sendto(s, reply, 4+len, 0,
|
332 |
|
|
(struct sockaddr *)from_addr, from_len) < 0) {
|
333 |
|
|
// Something went wrong with the network!
|
334 |
|
|
ok = false;
|
335 |
|
|
break;
|
336 |
|
|
}
|
337 |
|
|
repeat_select:
|
338 |
|
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
339 |
|
|
timeout.tv_usec = 0;
|
340 |
|
|
FD_ZERO(&fds);
|
341 |
|
|
FD_SET(s, &fds);
|
342 |
|
|
if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
|
343 |
|
|
if (++total_timeouts > TFTP_TIMEOUT_MAX) {
|
344 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
345 |
|
|
tftp_server_instrument.err_send++;
|
346 |
|
|
#endif
|
347 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
348 |
|
|
ok = false;
|
349 |
|
|
break;
|
350 |
|
|
}
|
351 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
352 |
|
|
tftp_server_instrument.data.resend++;
|
353 |
|
|
#endif
|
354 |
|
|
continue; // retry the send, using up one retry.
|
355 |
|
|
}
|
356 |
|
|
data_len = sizeof(data_in);
|
357 |
|
|
client_len = sizeof(client_addr);
|
358 |
|
|
if ((data_len = recvfrom(s, data_in, data_len, 0,
|
359 |
|
|
(struct sockaddr *)&client_addr,
|
360 |
|
|
&client_len)) < 0) {
|
361 |
|
|
// What happened? Maybe someone lied to us...
|
362 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
363 |
|
|
tftp_server_instrument.data.resend++;
|
364 |
|
|
#endif
|
365 |
|
|
continue; // retry the send, using up one retry.
|
366 |
|
|
}
|
367 |
|
|
if ((ntohs(response->th_opcode) == ACK) &&
|
368 |
|
|
(ntohs(response->th_block) < block)) {
|
369 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
370 |
|
|
tftp_server_instrument.ack.rx_repeat++;
|
371 |
|
|
#endif
|
372 |
|
|
// Then it is a repeat ACK for an old block; listen again,
|
373 |
|
|
// but do not repeat sending the current block, and do not
|
374 |
|
|
// use up a retry count. (we do re-send the data if
|
375 |
|
|
// subsequently we time out)
|
376 |
|
|
goto repeat_select;
|
377 |
|
|
}
|
378 |
|
|
if ((ntohs(response->th_opcode) == ACK) &&
|
379 |
|
|
(ntohs(response->th_block) == block)) {
|
380 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
381 |
|
|
tftp_server_instrument.ack.rx++;
|
382 |
|
|
#endif
|
383 |
|
|
// Happy! Break out of the retries loop.
|
384 |
|
|
break;
|
385 |
|
|
}
|
386 |
|
|
// Otherwise, we got something we do not understand! So repeat
|
387 |
|
|
// sending the current block, and use up a retry count.
|
388 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
389 |
|
|
if ( (ntohs(response->th_opcode) == ACK) )
|
390 |
|
|
tftp_server_instrument.ack.rx_skip++;
|
391 |
|
|
tftp_server_instrument.data.resend++;
|
392 |
|
|
#endif
|
393 |
|
|
} // End of the retries loop.
|
394 |
|
|
if (TFTP_RETRIES_MAX <= tries) {
|
395 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
396 |
|
|
tftp_server_instrument.err_send++;
|
397 |
|
|
#endif
|
398 |
|
|
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
|
399 |
|
|
ok = false;
|
400 |
|
|
}
|
401 |
|
|
if (len < SEGSIZE) {
|
402 |
|
|
break; // That's end of file then.
|
403 |
|
|
}
|
404 |
|
|
}
|
405 |
|
|
close(s);
|
406 |
|
|
(server->ops->close)(fd);
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
//
|
410 |
|
|
// Actual TFTP server
|
411 |
|
|
//
|
412 |
|
|
#define CYGSEM_TFTP_SERVER_MULTITHREADED
|
413 |
|
|
#ifdef CYGSEM_TFTP_SERVER_MULTITHREADED
|
414 |
|
|
static cyg_sem_t tftp_server_sem;
|
415 |
|
|
#endif
|
416 |
|
|
|
417 |
|
|
static void
|
418 |
|
|
tftpd_server(cyg_addrword_t p)
|
419 |
|
|
{
|
420 |
|
|
struct tftp_server *server = (struct tftp_server *)p;
|
421 |
|
|
int s;
|
422 |
|
|
int data_len, recv_len, from_len;
|
423 |
|
|
struct sockaddr_in local_addr, from_addr;
|
424 |
|
|
struct servent *server_info;
|
425 |
|
|
char data[SEGSIZE+sizeof(struct tftphdr)];
|
426 |
|
|
struct tftphdr *hdr = (struct tftphdr *)data;
|
427 |
|
|
|
428 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
429 |
|
|
// Otherwise routine printfs fail the test - interrupts disabled too long.
|
430 |
|
|
diag_printf("TFTPD [%x]: port %d\n", p, server->port);
|
431 |
|
|
#endif
|
432 |
|
|
|
433 |
|
|
// Set up port
|
434 |
|
|
if (server->port == 0) {
|
435 |
|
|
server_info = getservbyname("tftp", "udp");
|
436 |
|
|
if (server_info == (struct servent *)0) {
|
437 |
|
|
diag_printf("TFTPD: can't get TFTP service information\n");
|
438 |
|
|
return;
|
439 |
|
|
}
|
440 |
|
|
// Network order in info; host order in server:
|
441 |
|
|
server->port = ntohs( server_info->s_port );
|
442 |
|
|
}
|
443 |
|
|
|
444 |
|
|
while (true) {
|
445 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
446 |
|
|
struct info o = tftp_server_instrument;
|
447 |
|
|
#endif
|
448 |
|
|
// Create socket
|
449 |
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
450 |
|
|
if (s < 0) {
|
451 |
|
|
diag_printf("TFTPD [%x]: can't open socket\n", p);
|
452 |
|
|
return;
|
453 |
|
|
}
|
454 |
|
|
memset((char *)&local_addr, 0, sizeof(local_addr));
|
455 |
|
|
local_addr.sin_family = AF_INET;
|
456 |
|
|
local_addr.sin_len = sizeof(local_addr);
|
457 |
|
|
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
458 |
|
|
local_addr.sin_port = htons(server->port);
|
459 |
|
|
if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
|
460 |
|
|
// Problem setting up my end
|
461 |
|
|
close(s);
|
462 |
|
|
#ifdef CYGSEM_TFTP_SERVER_MULTITHREADED
|
463 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
464 |
|
|
diag_printf("TFTPD [%x]: waiting to bind to service port\n", p);
|
465 |
|
|
#endif
|
466 |
|
|
// Wait until the socket is free...
|
467 |
|
|
cyg_semaphore_wait( &tftp_server_sem );
|
468 |
|
|
continue; // try re-opening and rebinding the socket.
|
469 |
|
|
#else
|
470 |
|
|
diag_printf("TFTPD [%x]: can't bind to service port\n", p);
|
471 |
|
|
return;
|
472 |
|
|
#endif
|
473 |
|
|
}
|
474 |
|
|
|
475 |
|
|
recv_len = sizeof(data);
|
476 |
|
|
from_len = sizeof(from_addr);
|
477 |
|
|
data_len = recvfrom(s, hdr, recv_len, 0,
|
478 |
|
|
(struct sockaddr *)&from_addr, &from_len);
|
479 |
|
|
close(s); // so that other servers can bind to the TFTP socket
|
480 |
|
|
#ifdef CYGSEM_TFTP_SERVER_MULTITHREADED
|
481 |
|
|
// The socket is free...
|
482 |
|
|
cyg_semaphore_post( &tftp_server_sem );
|
483 |
|
|
#endif
|
484 |
|
|
|
485 |
|
|
if ( data_len < 0) {
|
486 |
|
|
diag_printf("TFTPD [%x]: can't read request\n", p);
|
487 |
|
|
} else {
|
488 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
489 |
|
|
diag_printf("TFTPD [%x]: received %x from %s:%d\n", p,
|
490 |
|
|
ntohs(hdr->th_opcode), inet_ntoa(from_addr.sin_addr),
|
491 |
|
|
from_addr.sin_port);
|
492 |
|
|
#endif
|
493 |
|
|
switch (ntohs(hdr->th_opcode)) {
|
494 |
|
|
case WRQ:
|
495 |
|
|
tftpd_write_file(server, hdr, &from_addr, from_len);
|
496 |
|
|
break;
|
497 |
|
|
case RRQ:
|
498 |
|
|
tftpd_read_file(server, hdr, &from_addr, from_len);
|
499 |
|
|
break;
|
500 |
|
|
case ACK:
|
501 |
|
|
case DATA:
|
502 |
|
|
case ERROR:
|
503 |
|
|
// Ignore
|
504 |
|
|
break;
|
505 |
|
|
default:
|
506 |
|
|
diag_printf("TFTPD [%x]: bogus request %x from %s:%d\n", p,
|
507 |
|
|
ntohs(hdr->th_opcode),
|
508 |
|
|
inet_ntoa(from_addr.sin_addr),
|
509 |
|
|
from_addr.sin_port );
|
510 |
|
|
tftpd_send_error(s,hdr,TFTP_EBADOP,&from_addr,from_len);
|
511 |
|
|
}
|
512 |
|
|
}
|
513 |
|
|
#ifdef CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
514 |
|
|
tftp_server_instrument.total_transactions++;
|
515 |
|
|
|
516 |
|
|
o.data.rx -= tftp_server_instrument.data.rx ;
|
517 |
|
|
o.data.rx_repeat -= tftp_server_instrument.data.rx_repeat;
|
518 |
|
|
o.data.rx_skip -= tftp_server_instrument.data.rx_skip ;
|
519 |
|
|
o.data.send -= tftp_server_instrument.data.send ;
|
520 |
|
|
o.data.resend -= tftp_server_instrument.data.resend ;
|
521 |
|
|
|
522 |
|
|
o.ack.rx -= tftp_server_instrument.ack.rx ;
|
523 |
|
|
o.ack.rx_repeat -= tftp_server_instrument.ack.rx_repeat ;
|
524 |
|
|
o.ack.rx_skip -= tftp_server_instrument.ack.rx_skip ;
|
525 |
|
|
o.ack.send -= tftp_server_instrument.ack.send ;
|
526 |
|
|
o.ack.resend -= tftp_server_instrument.ack.resend ;
|
527 |
|
|
|
528 |
|
|
o.err_send -= tftp_server_instrument.err_send ;
|
529 |
|
|
|
530 |
|
|
#ifndef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
531 |
|
|
if ( o.data.rx ) diag_printf( "data rx %4d\n", -o.data.rx );
|
532 |
|
|
if ( o.data.rx_repeat ) diag_printf( "data rx_repeat%4d\n", -o.data.rx_repeat );
|
533 |
|
|
if ( o.data.rx_skip ) diag_printf( "data rx_skip %4d\n", -o.data.rx_skip );
|
534 |
|
|
if ( o.data.send ) diag_printf( "data send %4d\n", -o.data.send );
|
535 |
|
|
if ( o.data.resend ) diag_printf( "data resend %4d\n", -o.data.resend );
|
536 |
|
|
|
537 |
|
|
if ( o.ack.rx ) diag_printf( " ack rx %4d\n", -o.ack.rx );
|
538 |
|
|
if ( o.ack.rx_repeat ) diag_printf( " ack rx_repeat%4d\n", -o.ack.rx_repeat );
|
539 |
|
|
if ( o.ack.rx_skip ) diag_printf( " ack rx_skip %4d\n", -o.ack.rx_skip );
|
540 |
|
|
if ( o.ack.send ) diag_printf( " ack send %4d\n", -o.ack.send );
|
541 |
|
|
if ( o.ack.resend ) diag_printf( " ack resend %4d\n", -o.ack.resend );
|
542 |
|
|
|
543 |
|
|
if ( o.err_send ) diag_printf( "*error sends %4d\n", -o.err_send );
|
544 |
|
|
#endif // CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS
|
545 |
|
|
#endif // CYGOPT_NET_TFTP_SERVER_INSTRUMENT
|
546 |
|
|
}
|
547 |
|
|
}
|
548 |
|
|
|
549 |
|
|
//
|
550 |
|
|
// This function is used to create a new server [thread] which supports
|
551 |
|
|
// the TFTP protocol on the given port. A server 'id' will be returned
|
552 |
|
|
// which can later be used to destroy the server.
|
553 |
|
|
//
|
554 |
|
|
// Note: all [memory] resources for the server thread will be allocated
|
555 |
|
|
// dynamically. If there are insufficient resources, an error will be
|
556 |
|
|
// returned.
|
557 |
|
|
//
|
558 |
|
|
int
|
559 |
|
|
tftpd_start(int port, struct tftpd_fileops *ops)
|
560 |
|
|
{
|
561 |
|
|
struct tftp_server *server;
|
562 |
|
|
#ifdef CYGSEM_TFTP_SERVER_MULTITHREADED
|
563 |
|
|
static char init = 0;
|
564 |
|
|
if ( 0 == init ) {
|
565 |
|
|
init++;
|
566 |
|
|
cyg_semaphore_init( &tftp_server_sem, 0 );
|
567 |
|
|
}
|
568 |
|
|
#endif
|
569 |
|
|
|
570 |
|
|
if ((server = malloc(sizeof(struct tftp_server)))) {
|
571 |
|
|
server->tag = TFTP_tag;
|
572 |
|
|
server->port = port;
|
573 |
|
|
server->ops = ops;
|
574 |
|
|
cyg_thread_create(CYGPKG_NET_TFTPD_THREAD_PRIORITY, // Priority
|
575 |
|
|
tftpd_server, // entry
|
576 |
|
|
(cyg_addrword_t)server, // entry parameter
|
577 |
|
|
"TFTP server", // Name
|
578 |
|
|
&server->stack[0], // Stack
|
579 |
|
|
STACK_SIZE, // Size
|
580 |
|
|
&server->thread_handle, // Handle
|
581 |
|
|
&server->thread_data // Thread data structure
|
582 |
|
|
);
|
583 |
|
|
cyg_thread_resume(server->thread_handle); // Start it
|
584 |
|
|
|
585 |
|
|
}
|
586 |
|
|
return (int)server;
|
587 |
|
|
}
|
588 |
|
|
|
589 |
|
|
//
|
590 |
|
|
// Destroy a TFTP server, using a previously created server 'id'.
|
591 |
|
|
//
|
592 |
|
|
int
|
593 |
|
|
tftpd_stop(int p)
|
594 |
|
|
{
|
595 |
|
|
struct tftp_server *server = (struct tftp_server *)p;
|
596 |
|
|
// Simple sanity check
|
597 |
|
|
if (server->tag == TFTP_tag) {
|
598 |
|
|
cyg_thread_kill(server->thread_handle);
|
599 |
|
|
cyg_thread_set_priority(server->thread_handle, 0);
|
600 |
|
|
cyg_thread_delay(1); // Make sure it gets to die...
|
601 |
|
|
if (cyg_thread_delete(server->thread_handle)) {
|
602 |
|
|
// Success shutting down the thread
|
603 |
|
|
free(server); // Give up memory
|
604 |
|
|
return 1;
|
605 |
|
|
}
|
606 |
|
|
}
|
607 |
|
|
return 0;
|
608 |
|
|
}
|
609 |
|
|
|
610 |
|
|
// EOF tftp_server.c
|