-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtftp_base.c
329 lines (283 loc) · 7.7 KB
/
tftp_base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "tftp_base.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
const char* tftp_error_message(uint16_t error_code)
{
static const char* msg[] =
{
"unknow error",
"file not found",
"Access violation",
"Disk full or allocation exceeded",
"Illegal TFTP operation",
"Unknown transfer ID",
"File already exists",
"No such user"
};
if (error_code >= TFTP_ERROR_END)
{
error_code = 0;
}
return msg[error_code];
}
static char* write_information(tftp_t* tftp, char* buffer, const char* information, int value)
{
// 计算包末端的地址
char* buffer_end = ((char*)&tftp->tx_packet) + sizeof(tftp_packet_t);
size_t len = strlen(information) + 1; // +1是因为算了'\0'
if (buffer + len > buffer_end)
{
printf("tftp: send buffer too small\n");
return NULL;
}
strcpy(buffer, information);
buffer += len;
if (value >= 0)
{
if (buffer + 16 >= buffer_end)
{
printf("tftp: send buffer too small\n");
return NULL;
}
sprintf(buffer, "%d", value);
buffer += strlen(buffer) + 1;
}
return buffer;
}
int tftp_send_packet(tftp_t* tftp, tftp_packet_t* pkt, int size)
{
ssize_t send_size = sendto(tftp->socket, (const void*)pkt, size, 0, &tftp->remote, sizeof(tftp->remote));
if (send_size < 0)
{
printf("tftp: send error\n");
return -1;
}
tftp->tx_size = size;
return 0;
}
int tftp_send_request(tftp_t* tftp, int is_read, const char* filename, uint32_t file_size, int option)
{
tftp_packet_t* pkt = &tftp->tx_packet;
pkt->opcode = htons(is_read ? TFTP_PACKET_RRQ : TFTP_PACKET_WRQ);
char* buffer = (char*)pkt->req.args;
buffer = write_information(tftp, buffer, filename, -1);
if (buffer == NULL)
{
printf("tftp: filename too long: %s\n", filename);
return -1;
}
buffer = write_information(tftp, buffer, "octet", -1);
if (buffer == NULL)
{
// 文件名太长导致选项没有空间了
printf("tftp: filename too long: %s\n", filename);
return -1;
}
if (option)
{
buffer = write_information(tftp, buffer, "blksize", tftp->block_size);
if (buffer == NULL)
{
return -1;
}
buffer = write_information(tftp, buffer, "tsize", file_size);
if (buffer == NULL)
{
return -1;
}
}
int size = (int)(buffer - (char*)pkt->req.args) + 2;
int error = tftp_send_packet(tftp, pkt, size);
if (error < 0)
{
printf("tftp: send req failed.\n");
return -1;
}
return 0;
}
int tftp_send_ack(tftp_t* tftp, uint16_t block_num)
{
tftp_packet_t* pkt = &tftp->tx_packet;
pkt->opcode = htons(TFTP_PACKET_ACK);
pkt->ack.block_num = htons(block_num);
int error = tftp_send_packet(tftp, pkt, 4);
if (error < 0)
{
printf("tftp: send ack failed. block_num = %d\n", block_num);
return -1;
}
return 0;
}
int tftp_send_data(tftp_t* tftp, uint16_t block_num, size_t size)
{
tftp_packet_t* pkt = &tftp->tx_packet;
pkt->opcode = htons(TFTP_PACKET_DATA);
pkt->data.block_num = htons(block_num);
int error = tftp_send_packet(tftp, pkt, 4 + (int)size);
if (error < 0)
{
printf("tftp: send data failed. block_num = %d\n", block_num);
return -1;
}
return 0;
}
int tftp_send_error(tftp_t* tftp, uint16_t error_code)
{
tftp_packet_t* pkt = &tftp->tx_packet;
pkt->opcode = htons(TFTP_PACKET_ERROR);
pkt->error.error_code = htons(error_code);
const char* msg = tftp_error_message(error_code);
strcpy(pkt->error.error_msg, msg);
int error = tftp_send_packet(tftp, pkt, 4 + (int)strlen(msg) + 1);
if (error < 0)
{
printf("tftp: send data failed. error_code = %d", error_code);
return -1;
}
return 0;
}
int tftp_resend(tftp_t* tftp)
{
tftp_packet_t* pkt = &tftp->tx_packet;
if (tftp_send_packet(tftp, pkt, tftp->tx_size))
{
printf("tftp: resend error\n");
return -1;
}
return 0;
}
int tftp_wait_packet(tftp_t* tftp, tftp_op_t opcode, uint16_t block_num, size_t* pkt_size)
{
tftp_packet_t* pkt = &tftp->rx_packet;
tftp->tmo_retry = TFTP_MAX_RETYR;
while (1)
{
socklen_t len = sizeof(struct sockaddr);
ssize_t size = recvfrom(tftp->socket, (uint8_t*)pkt, sizeof(tftp_packet_t), 0, &tftp->remote, &len);
if (size < 0)
{
if (--tftp->tmo_retry == 0)
{
printf("tftp: wait tmo\n");
return -1;
}
else
{
tftp_resend(tftp);
continue;
}
}
*pkt_size = (size_t)size;
uint16_t _opcode = htons(pkt->opcode);
if (opcode == TFTP_PACKET_REQ)
{
if ((_opcode != TFTP_PACKET_RRQ) && (_opcode != TFTP_PACKET_WRQ))
{
continue;
}
}
else if (_opcode != opcode)
{
tftp_resend(tftp);
continue;
}
switch (_opcode)
{
case TFTP_PACKET_DATA:
case TFTP_PACKET_ACK:
{
if (htons(pkt->data.block_num) != block_num)
{
tftp_resend(tftp);
break;
}
return 0;
}
case TFTP_PACKET_RRQ:
case TFTP_PACKET_WRQ:
{
return 0;
}
case TFTP_PACKET_ERROR:
{
pkt->error.error_msg[tftp->block_size - 1] = '\0';
printf("tftp: recv error=%d, reason: %s\n", ntohs(pkt->error.error_code), pkt->error.error_msg);
return -1;
}
case TFTP_PACKET_OACK:
{
tftp_parse_oack(tftp);
return 0;
}
default:
{
tftp_resend(tftp);
break;
}
}
}
}
int tftp_parse_oack(tftp_t* tftp)
{
char* buffer = (char*)&tftp->rx_packet.oack.option;
char* end = (char*)&tftp->rx_packet + sizeof(tftp_packet_t);
while ((buffer < end) && (*buffer))
{
if (strcmp(buffer, "blksize") == 0)
{
buffer += (strlen(buffer) + 1);
int blksize = atoi(buffer);
if (blksize == 0)
{
printf("tftp: unknown blksize \n");
return -1;
}
else if (blksize < tftp->block_size)
{
tftp->block_size = blksize;
printf("tftp: use new blksize %d\n", blksize);
}
else if (blksize > tftp->block_size)
{
printf("tftp: block size %d\n", blksize);
return -1;
}
buffer += (strlen(buffer) + 1);
}
else if (strcmp(buffer, "tsize") == 0)
{
buffer += strlen(buffer) + 1;
tftp->file_size = atoi(buffer);
buffer += (strlen(buffer) + 1);
}
else
{
buffer += (strlen(buffer) + 1);
}
}
return 0;
}
int tftp_send_oack(tftp_t* tftp)
{
tftp_packet_t* pkt = &tftp->tx_packet;
pkt->opcode = htons(TFTP_PACKET_OACK);
char* buffer = pkt->oack.option;
buffer = write_information(tftp, buffer, "blksize", tftp->block_size);
if (buffer == NULL)
{
return -1;
}
buffer = write_information(tftp, buffer, "tsize", tftp->file_size);
if (buffer == NULL)
{
return -1;
}
int error = tftp_send_packet(tftp, pkt, buffer - (char*)pkt);
if (error < 0)
{
printf("tftp: send oack failed\n");
return -1;
}
return 0;
}