2013-04-16 12:55:25 -04:00
|
|
|
#include <stdint.h>
|
2012-05-21 16:57:12 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "microudp.h"
|
|
|
|
#include "tftp.h"
|
|
|
|
|
|
|
|
#define PORT_OUT 69
|
|
|
|
#define PORT_IN 7642
|
|
|
|
|
2013-04-16 12:55:21 -04:00
|
|
|
enum {
|
|
|
|
TFTP_RRQ = 1, /* Read request */
|
|
|
|
TFTP_WRQ = 2, /* Write request */
|
|
|
|
TFTP_DATA = 3, /* Data */
|
|
|
|
TFTP_ACK = 4, /* Acknowledgment */
|
|
|
|
TFTP_ERROR = 5, /* Error */
|
|
|
|
};
|
|
|
|
|
2013-04-16 12:55:27 -04:00
|
|
|
#define BLOCK_SIZE 512 /* block size in bytes */
|
|
|
|
|
|
|
|
|
2013-04-16 12:55:26 -04:00
|
|
|
static int format_request(uint8_t *buf, uint16_t op, const char *filename)
|
2012-05-21 16:57:12 -04:00
|
|
|
{
|
2013-04-16 12:55:23 -04:00
|
|
|
int len = strlen(filename);
|
|
|
|
|
2013-04-16 12:55:26 -04:00
|
|
|
*buf++ = op >> 8; /* Opcode */
|
|
|
|
*buf++ = op;
|
2013-04-16 12:55:23 -04:00
|
|
|
memcpy(buf, filename, len);
|
|
|
|
buf += len;
|
2012-05-21 16:57:12 -04:00
|
|
|
*buf++ = 0x00;
|
|
|
|
*buf++ = 'o';
|
|
|
|
*buf++ = 'c';
|
|
|
|
*buf++ = 't';
|
|
|
|
*buf++ = 'e';
|
|
|
|
*buf++ = 't';
|
|
|
|
*buf++ = 0x00;
|
|
|
|
return 9+strlen(filename);
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:55:25 -04:00
|
|
|
static int format_ack(uint8_t *buf, uint16_t block)
|
2012-05-21 16:57:12 -04:00
|
|
|
{
|
|
|
|
*buf++ = 0x00; /* Opcode: Ack */
|
2013-04-16 12:55:21 -04:00
|
|
|
*buf++ = TFTP_ACK;
|
2012-05-21 16:57:12 -04:00
|
|
|
*buf++ = (block & 0xff00) >> 8;
|
|
|
|
*buf++ = (block & 0x00ff);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:55:28 -04:00
|
|
|
static int format_data(uint8_t *buf, uint16_t block, const void *data, int len)
|
|
|
|
{
|
|
|
|
*buf++ = 0x00; /* Opcode: Data*/
|
|
|
|
*buf++ = TFTP_DATA;
|
|
|
|
*buf++ = (block & 0xff00) >> 8;
|
|
|
|
*buf++ = (block & 0x00ff);
|
|
|
|
memcpy(buf, data, len);
|
|
|
|
return len+4;
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:55:25 -04:00
|
|
|
static uint8_t *packet_data;
|
2012-05-21 16:57:12 -04:00
|
|
|
static int total_length;
|
|
|
|
static int transfer_finished;
|
2013-04-16 12:55:25 -04:00
|
|
|
static uint8_t *dst_buffer;
|
2013-04-16 12:55:28 -04:00
|
|
|
static int last_ack; /* signed, so we can use -1 */
|
|
|
|
static uint16_t data_port;
|
2012-05-21 16:57:12 -04:00
|
|
|
|
2013-04-16 12:55:25 -04:00
|
|
|
static void rx_callback(uint32_t src_ip, uint16_t src_port,
|
|
|
|
uint16_t dst_port, void *_data, unsigned int length)
|
2012-05-21 16:57:12 -04:00
|
|
|
{
|
2013-04-16 12:55:25 -04:00
|
|
|
uint8_t *data = _data;
|
|
|
|
uint16_t opcode;
|
|
|
|
uint16_t block;
|
2012-05-21 16:57:12 -04:00
|
|
|
int i;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
if(length < 4) return;
|
|
|
|
if(dst_port != PORT_IN) return;
|
2013-04-16 12:55:22 -04:00
|
|
|
opcode = data[0] << 8 | data[1];
|
|
|
|
block = data[2] << 8 | data[3];
|
2013-04-16 12:55:28 -04:00
|
|
|
if(opcode == TFTP_ACK) { /* Acknowledgement */
|
|
|
|
data_port = src_port;
|
|
|
|
last_ack = block;
|
|
|
|
return;
|
|
|
|
}
|
2012-05-21 16:57:12 -04:00
|
|
|
if(block < 1) return;
|
2013-04-16 12:55:21 -04:00
|
|
|
if(opcode == TFTP_DATA) { /* Data */
|
2012-05-21 16:57:12 -04:00
|
|
|
length -= 4;
|
2013-04-16 12:55:27 -04:00
|
|
|
offset = (block-1)*BLOCK_SIZE;
|
2012-05-21 16:57:12 -04:00
|
|
|
for(i=0;i<length;i++)
|
|
|
|
dst_buffer[offset+i] = data[i+4];
|
|
|
|
total_length += length;
|
2013-04-16 12:55:27 -04:00
|
|
|
if(length < BLOCK_SIZE)
|
2012-05-21 16:57:12 -04:00
|
|
|
transfer_finished = 1;
|
|
|
|
|
|
|
|
length = format_ack(packet_data, block);
|
|
|
|
microudp_send(PORT_IN, src_port, length);
|
|
|
|
}
|
2013-04-16 12:55:21 -04:00
|
|
|
if(opcode == TFTP_ERROR) { /* Error */
|
2012-05-21 16:57:12 -04:00
|
|
|
total_length = -1;
|
|
|
|
transfer_finished = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:55:25 -04:00
|
|
|
int tftp_get(uint32_t ip, const char *filename, void *buffer)
|
2012-05-21 16:57:12 -04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int tries;
|
|
|
|
int i;
|
|
|
|
int length_before;
|
|
|
|
|
|
|
|
if(!microudp_arp_resolve(ip))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
microudp_set_callback(rx_callback);
|
|
|
|
|
|
|
|
packet_data = microudp_get_tx_buffer();
|
|
|
|
dst_buffer = buffer;
|
|
|
|
|
|
|
|
total_length = 0;
|
|
|
|
transfer_finished = 0;
|
|
|
|
tries = 5;
|
|
|
|
while(1) {
|
2013-04-16 12:55:26 -04:00
|
|
|
len = format_request(packet_data, TFTP_RRQ, filename);
|
2012-05-21 16:57:12 -04:00
|
|
|
microudp_send(PORT_IN, PORT_OUT, len);
|
|
|
|
for(i=0;i<2000000;i++) {
|
|
|
|
microudp_service();
|
|
|
|
if((total_length > 0) || transfer_finished) break;
|
|
|
|
}
|
|
|
|
if((total_length > 0) || transfer_finished) break;
|
|
|
|
tries--;
|
|
|
|
if(tries == 0) {
|
|
|
|
microudp_set_callback(NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length_before = total_length;
|
|
|
|
while(!transfer_finished) {
|
|
|
|
if(length_before != total_length) {
|
|
|
|
i = 12000000;
|
|
|
|
length_before = total_length;
|
|
|
|
}
|
|
|
|
if(i-- == 0) {
|
|
|
|
microudp_set_callback(NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
microudp_service();
|
|
|
|
}
|
|
|
|
|
|
|
|
microudp_set_callback(NULL);
|
|
|
|
|
|
|
|
return total_length;
|
|
|
|
}
|
2013-04-16 12:55:28 -04:00
|
|
|
|
|
|
|
int tftp_put(uint32_t ip, const char *filename, const void *buffer, int size)
|
|
|
|
{
|
|
|
|
int len, send;
|
|
|
|
int tries;
|
|
|
|
int i;
|
|
|
|
int block = 0, sent = 0;
|
|
|
|
|
|
|
|
if(!microudp_arp_resolve(ip))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
microudp_set_callback(rx_callback);
|
|
|
|
|
|
|
|
packet_data = microudp_get_tx_buffer();
|
|
|
|
|
|
|
|
total_length = 0;
|
|
|
|
transfer_finished = 0;
|
|
|
|
tries = 5;
|
|
|
|
while(1) {
|
|
|
|
len = format_request(packet_data, TFTP_WRQ, filename);
|
|
|
|
microudp_send(PORT_IN, PORT_OUT, len);
|
|
|
|
for(i=0;i<2000000;i++) {
|
|
|
|
last_ack = -1;
|
|
|
|
microudp_service();
|
|
|
|
if(last_ack == block)
|
|
|
|
goto send_data;
|
|
|
|
if(transfer_finished)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
tries--;
|
|
|
|
if(tries == 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_data:
|
|
|
|
do {
|
|
|
|
block++;
|
|
|
|
send = sent+BLOCK_SIZE > size ? size-sent : BLOCK_SIZE;
|
|
|
|
tries = 5;
|
|
|
|
while(1) {
|
|
|
|
len = format_data(packet_data, block, buffer, send);
|
|
|
|
microudp_send(PORT_IN, data_port, len);
|
|
|
|
for(i=0;i<12000000;i++) {
|
|
|
|
microudp_service();
|
|
|
|
if(transfer_finished)
|
|
|
|
goto fail;
|
|
|
|
if(last_ack == block)
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (!--tries)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
next:
|
|
|
|
sent += send;
|
|
|
|
buffer += send;
|
|
|
|
} while (send == BLOCK_SIZE);
|
|
|
|
|
|
|
|
microudp_set_callback(NULL);
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
microudp_set_callback(NULL);
|
|
|
|
return -1;
|
|
|
|
}
|