1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
29
30
33
38
39
42
46
47
50
56
57
60
66
67
70
74
75
78
84
85
88
92
93
96
97
98
99
100
101
102
103
104
105
106
107
/* ... */
#include <string.h>
#include "tftputils.h"
/* ... */
tftp_opcode tftp_decode_op(char *buf)
{
return (tftp_opcode)(buf[1]);
}{ ... }
/* ... */
u16_t tftp_extract_block(char *buf)
{
u16_t *b = (u16_t*)buf;
return ntohs(b[1]);
}{ ... }
/* ... */
void tftp_extract_filename(char *fname, char *buf)
{
strcpy(fname, buf + 2);
}{ ... }
/* ... */
void tftp_set_opcode(char *buffer, tftp_opcode opcode)
{
buffer[0] = 0;
buffer[1] = (u8_t)opcode;
}{ ... }
/* ... */
void tftp_set_errorcode(char *buffer, tftp_errorcode errCode)
{
buffer[2] = 0;
buffer[3] = (u8_t)errCode;
}{ ... }
/* ... */
void tftp_set_errormsg(char * buffer, char* errormsg)
{
strcpy(buffer + 4, errormsg);
}{ ... }
/* ... */
void tftp_set_block(char* packet, u16_t block)
{
u16_t *p = (u16_t *)packet;
p[1] = htons(block);
}{ ... }
/* ... */
void tftp_set_data_message(char* packet, char* buf, int buflen)
{
memcpy(packet + 4, buf, buflen);
}{ ... }
/* ... */
u32_t tftp_is_correct_ack(char *buf, int block)
{
if (tftp_decode_op(buf) != TFTP_ACK)
return 0;
if (block != tftp_extract_block(buf))
return 0;
return 1;
}{ ... }