📄 basictftp.c
字号:
break;
}
continue; // retry the send, using up one retry.
}
vParTestToggleLED( TFTP_LED );
// Some data has arrived
data_len = sizeof(data_in);
client_len = sizeof(client_addr);
if ((data_len = recvfrom(s, data_in, data_len, 0,
(struct sockaddr *)&client_addr, &client_len)) < 0) {
// What happened? No data here!
continue; // retry the send, using up one retry.
}
if (ntohs(response->th_opcode) == DATA &&
ntohs(response->th_block) < block) {
// Then it is repeat DATA with an old block; listen again,
// but do not repeat sending the current ack, and do not
// use up a retry count. (we do re-send the ack if
// subsequently we time out)
goto repeat_select;
}
if (ntohs(response->th_opcode) == DATA &&
ntohs(response->th_block) == block) {
// Good data - write to file
len = tftpd_write_data_file(fd, response->th_data, data_len-4);
if (len < (data_len-4)) {
// File is "full"
tftpd_send_error(s,reply,TFTP_ENOSPACE,
from_addr, from_len);
ok = pdFALSE; // Give up
break; // out of the retries loop
}
if (data_len < (SEGSIZE+4)) {
// End of file
closed = pdTRUE;
ok = pdFALSE;
vParTestSetLED( 0 , 0 );
if (tftpd_close_data_file(fd) == -1) {
tftpd_send_error(s,reply,TFTP_EACCESS,
from_addr, from_len);
break; // out of the retries loop
}
// Exception to the loop structure: we must ACK the last
// packet, the one that implied EOF:
reply->th_opcode = htons(ACK);
reply->th_block = htons(block++); // postincrement
sendto(s, reply, 4, 0, (struct sockaddr *)from_addr, from_len);
break; // out of the retries loop
}
// Happy! Break out of the retries loop.
break;
}
} // End of the retries loop.
if (TFTP_RETRIES_MAX <= tries) {
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
ok = pdFALSE;
}
}
close(s);
if (!closed) {
tftpd_close_data_file(fd);
}
}
//
// Send a file to the client
//
static void
tftpd_read_file(struct tftphdr *hdr,
struct sockaddr_in *from_addr, int from_len)
{
struct tftphdr *reply = (struct tftphdr *)data_out;
struct tftphdr *response = (struct tftphdr *)data_in;
int fd, len, tries, ok, data_len, s;
unsigned short block;
struct timeval timeout;
fd_set fds;
int total_timeouts = 0;
struct sockaddr_in client_addr, local_addr;
int client_len;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
return;
}
memset((char *)&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_len = sizeof(local_addr);
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(INADDR_ANY);
if (bind(s, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
// Problem setting up my end
close(s);
return;
}
if ((fd = tftpd_open_data_file((int)hdr->th_stuff, O_RDONLY)) < 0) {
tftpd_send_error(s,reply,TFTP_ENOTFOUND,from_addr, from_len);
close(s);
return;
}
block = 0;
ok = pdTRUE;
while (ok) {
// Read next chunk of file
len = tftpd_read_data_file(fd, reply->th_data, SEGSIZE);
reply->th_block = htons(++block); // preincrement
reply->th_opcode = htons(DATA);
for (tries = 0; tries < TFTP_RETRIES_MAX; tries++) {
if (sendto(s, reply, 4+len, 0,
(struct sockaddr *)from_addr, from_len) < 0) {
// Something went wrong with the network!
ok = pdFALSE;
break;
}
repeat_select:
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(s, &fds);
vParTestToggleLED( TFTP_LED );
if (select(s+1, &fds, 0, 0, &timeout) <= 0) {
if (++total_timeouts > TFTP_TIMEOUT_MAX) {
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
ok = pdFALSE;
break;
}
continue; // retry the send, using up one retry.
}
vParTestToggleLED( TFTP_LED );
data_len = sizeof(data_in);
client_len = sizeof(client_addr);
if ((data_len = recvfrom(s, data_in, data_len, 0,
(struct sockaddr *)&client_addr,
&client_len)) < 0) {
// What happened? Maybe someone lied to us...
continue; // retry the send, using up one retry.
}
if ((ntohs(response->th_opcode) == ACK) &&
(ntohs(response->th_block) < block)) {
// Then it is a repeat ACK for an old block; listen again,
// but do not repeat sending the current block, and do not
// use up a retry count. (we do re-send the data if
// subsequently we time out)
goto repeat_select;
}
if ((ntohs(response->th_opcode) == ACK) &&
(ntohs(response->th_block) == block)) {
// Happy! Break out of the retries loop.
break;
}
} // End of the retries loop.
if (TFTP_RETRIES_MAX <= tries) {
tftpd_send_error(s,reply,TFTP_EBADOP,from_addr, from_len);
ok = pdFALSE;
}
if (len < SEGSIZE) {
break; // That's end of file then.
}
}
close(s);
tftpd_close_data_file(fd);
}
portTASK_FUNCTION( vBasicTFTPServer, pvParameters )
{
int lSocket;
int lDataLen, lRecvLen, lFromLen;
struct sockaddr_in sLocalAddr, sFromAddr;
portCHAR cData[SEGSIZE+sizeof(struct tftphdr)];
struct tftphdr *sHdr = (struct tftphdr *)cData;
// Set up port
// Network order in info; host order in server:
for (;;) {
// Create socket
lSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (lSocket < 0) {
return;
}
memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
sLocalAddr.sin_family = AF_INET;
sLocalAddr.sin_len = sizeof(sLocalAddr);
sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sLocalAddr.sin_port = TFTP_PORT;
if (bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0) {
// Problem setting up my end
close(lSocket);
return;
}
lRecvLen = sizeof(cData);
lFromLen = sizeof(sFromAddr);
lDataLen = recvfrom(lSocket, sHdr, lRecvLen, 0,
(struct sockaddr *)&sFromAddr, &lFromLen);
vParTestSetLED( TFTP_LED , pdTRUE );
close(lSocket); // so that other servers can bind to the TFTP socket
if ( lDataLen < 0) {
} else {
switch (ntohs(sHdr->th_opcode)) {
case WRQ:
tftpd_write_file(sHdr, &sFromAddr, lFromLen);
vParTestSetLED( TFTP_LED , pdFALSE );
break;
case RRQ:
tftpd_read_file(sHdr, &sFromAddr, lFromLen);
vParTestSetLED( TFTP_LED , pdFALSE );
break;
case ACK:
case DATA:
case ERROR:
vParTestSetLED( TFTP_LED , pdFALSE );
// Ignore
break;
default:
for(;;)
{
vParTestToggleLED( TFTP_LED );
vTaskDelay(200);
}
}
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -