1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifndef CURL_DISABLE_TFTP
26
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_NET_IF_H
37 #include <net/if.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46
47 #include "urldata.h"
48 #include <curl/curl.h>
49 #include "transfer.h"
50 #include "sendf.h"
51 #include "tftp.h"
52 #include "progress.h"
53 #include "connect.h"
54 #include "strerror.h"
55 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
56 #include "multiif.h"
57 #include "url.h"
58 #include "strcase.h"
59 #include "speedcheck.h"
60 #include "select.h"
61 #include "escape.h"
62
63 /* The last 3 #include files should be in this order */
64 #include "curl_printf.h"
65 #include "curl_memory.h"
66 #include "memdebug.h"
67
68 /* RFC2348 allows the block size to be negotiated */
69 #define TFTP_BLKSIZE_DEFAULT 512
70 #define TFTP_BLKSIZE_MIN 8
71 #define TFTP_BLKSIZE_MAX 65464
72 #define TFTP_OPTION_BLKSIZE "blksize"
73
74 /* from RFC2349: */
75 #define TFTP_OPTION_TSIZE "tsize"
76 #define TFTP_OPTION_INTERVAL "timeout"
77
78 typedef enum {
79 TFTP_MODE_NETASCII = 0,
80 TFTP_MODE_OCTET
81 } tftp_mode_t;
82
83 typedef enum {
84 TFTP_STATE_START = 0,
85 TFTP_STATE_RX,
86 TFTP_STATE_TX,
87 TFTP_STATE_FIN
88 } tftp_state_t;
89
90 typedef enum {
91 TFTP_EVENT_NONE = -1,
92 TFTP_EVENT_INIT = 0,
93 TFTP_EVENT_RRQ = 1,
94 TFTP_EVENT_WRQ = 2,
95 TFTP_EVENT_DATA = 3,
96 TFTP_EVENT_ACK = 4,
97 TFTP_EVENT_ERROR = 5,
98 TFTP_EVENT_OACK = 6,
99 TFTP_EVENT_TIMEOUT
100 } tftp_event_t;
101
102 typedef enum {
103 TFTP_ERR_UNDEF = 0,
104 TFTP_ERR_NOTFOUND,
105 TFTP_ERR_PERM,
106 TFTP_ERR_DISKFULL,
107 TFTP_ERR_ILLEGAL,
108 TFTP_ERR_UNKNOWNID,
109 TFTP_ERR_EXISTS,
110 TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
111
112 /* The remaining error codes are internal to curl */
113 TFTP_ERR_NONE = -100,
114 TFTP_ERR_TIMEOUT,
115 TFTP_ERR_NORESPONSE
116 } tftp_error_t;
117
118 struct tftp_packet {
119 unsigned char *data;
120 };
121
122 struct tftp_state_data {
123 tftp_state_t state;
124 tftp_mode_t mode;
125 tftp_error_t error;
126 tftp_event_t event;
127 struct connectdata *conn;
128 curl_socket_t sockfd;
129 int retries;
130 int retry_time;
131 int retry_max;
132 time_t start_time;
133 time_t max_time;
134 time_t rx_time;
135 unsigned short block;
136 struct Curl_sockaddr_storage local_addr;
137 struct Curl_sockaddr_storage remote_addr;
138 curl_socklen_t remote_addrlen;
139 int rbytes;
140 int sbytes;
141 int blksize;
142 int requested_blksize;
143 struct tftp_packet rpacket;
144 struct tftp_packet spacket;
145 };
146
147
148 /* Forward declarations */
149 static CURLcode tftp_rx(struct tftp_state_data *state, tftp_event_t event);
150 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event);
151 static CURLcode tftp_connect(struct connectdata *conn, bool *done);
152 static CURLcode tftp_disconnect(struct connectdata *conn,
153 bool dead_connection);
154 static CURLcode tftp_do(struct connectdata *conn, bool *done);
155 static CURLcode tftp_done(struct connectdata *conn,
156 CURLcode, bool premature);
157 static CURLcode tftp_setup_connection(struct connectdata *conn);
158 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done);
159 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done);
160 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks);
161 static CURLcode tftp_translate_code(tftp_error_t error);
162
163
164 /*
165 * TFTP protocol handler.
166 */
167
168 const struct Curl_handler Curl_handler_tftp = {
169 "TFTP", /* scheme */
170 tftp_setup_connection, /* setup_connection */
171 tftp_do, /* do_it */
172 tftp_done, /* done */
173 ZERO_NULL, /* do_more */
174 tftp_connect, /* connect_it */
175 tftp_multi_statemach, /* connecting */
176 tftp_doing, /* doing */
177 tftp_getsock, /* proto_getsock */
178 tftp_getsock, /* doing_getsock */
179 ZERO_NULL, /* domore_getsock */
180 ZERO_NULL, /* perform_getsock */
181 tftp_disconnect, /* disconnect */
182 ZERO_NULL, /* readwrite */
183 ZERO_NULL, /* connection_check */
184 PORT_TFTP, /* defport */
185 CURLPROTO_TFTP, /* protocol */
186 CURLPROTO_TFTP, /* family */
187 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
188 };
189
190 /**********************************************************
191 *
192 * tftp_set_timeouts -
193 *
194 * Set timeouts based on state machine state.
195 * Use user provided connect timeouts until DATA or ACK
196 * packet is received, then use user-provided transfer timeouts
197 *
198 *
199 **********************************************************/
tftp_set_timeouts(struct tftp_state_data * state)200 static CURLcode tftp_set_timeouts(struct tftp_state_data *state)
201 {
202 time_t maxtime, timeout;
203 timediff_t timeout_ms;
204 bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
205
206 time(&state->start_time);
207
208 /* Compute drop-dead time */
209 timeout_ms = Curl_timeleft(state->conn->data, NULL, start);
210
211 if(timeout_ms < 0) {
212 /* time-out, bail out, go home */
213 failf(state->conn->data, "Connection time-out");
214 return CURLE_OPERATION_TIMEDOUT;
215 }
216
217 if(start) {
218
219 maxtime = (time_t)(timeout_ms + 500) / 1000;
220 state->max_time = state->start_time + maxtime;
221
222 /* Set per-block timeout to total */
223 timeout = maxtime;
224
225 /* Average restart after 5 seconds */
226 state->retry_max = (int)timeout/5;
227
228 if(state->retry_max < 1)
229 /* avoid division by zero below */
230 state->retry_max = 1;
231
232 /* Compute the re-start interval to suit the timeout */
233 state->retry_time = (int)timeout/state->retry_max;
234 if(state->retry_time<1)
235 state->retry_time = 1;
236
237 }
238 else {
239 if(timeout_ms > 0)
240 maxtime = (time_t)(timeout_ms + 500) / 1000;
241 else
242 maxtime = 3600;
243
244 state->max_time = state->start_time + maxtime;
245
246 /* Set per-block timeout to total */
247 timeout = maxtime;
248
249 /* Average reposting an ACK after 5 seconds */
250 state->retry_max = (int)timeout/5;
251 }
252 /* But bound the total number */
253 if(state->retry_max<3)
254 state->retry_max = 3;
255
256 if(state->retry_max>50)
257 state->retry_max = 50;
258
259 /* Compute the re-ACK interval to suit the timeout */
260 state->retry_time = (int)(timeout/state->retry_max);
261 if(state->retry_time<1)
262 state->retry_time = 1;
263
264 infof(state->conn->data,
265 "set timeouts for state %d; Total %ld, retry %d maxtry %d\n",
266 (int)state->state, (long)(state->max_time-state->start_time),
267 state->retry_time, state->retry_max);
268
269 /* init RX time */
270 time(&state->rx_time);
271
272 return CURLE_OK;
273 }
274
275 /**********************************************************
276 *
277 * tftp_set_send_first
278 *
279 * Event handler for the START state
280 *
281 **********************************************************/
282
setpacketevent(struct tftp_packet * packet,unsigned short num)283 static void setpacketevent(struct tftp_packet *packet, unsigned short num)
284 {
285 packet->data[0] = (unsigned char)(num >> 8);
286 packet->data[1] = (unsigned char)(num & 0xff);
287 }
288
289
setpacketblock(struct tftp_packet * packet,unsigned short num)290 static void setpacketblock(struct tftp_packet *packet, unsigned short num)
291 {
292 packet->data[2] = (unsigned char)(num >> 8);
293 packet->data[3] = (unsigned char)(num & 0xff);
294 }
295
getrpacketevent(const struct tftp_packet * packet)296 static unsigned short getrpacketevent(const struct tftp_packet *packet)
297 {
298 return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
299 }
300
getrpacketblock(const struct tftp_packet * packet)301 static unsigned short getrpacketblock(const struct tftp_packet *packet)
302 {
303 return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
304 }
305
Curl_strnlen(const char * string,size_t maxlen)306 static size_t Curl_strnlen(const char *string, size_t maxlen)
307 {
308 const char *end = memchr(string, '\0', maxlen);
309 return end ? (size_t) (end - string) : maxlen;
310 }
311
tftp_option_get(const char * buf,size_t len,const char ** option,const char ** value)312 static const char *tftp_option_get(const char *buf, size_t len,
313 const char **option, const char **value)
314 {
315 size_t loc;
316
317 loc = Curl_strnlen(buf, len);
318 loc++; /* NULL term */
319
320 if(loc >= len)
321 return NULL;
322 *option = buf;
323
324 loc += Curl_strnlen(buf + loc, len-loc);
325 loc++; /* NULL term */
326
327 if(loc > len)
328 return NULL;
329 *value = &buf[strlen(*option) + 1];
330
331 return &buf[loc];
332 }
333
tftp_parse_option_ack(struct tftp_state_data * state,const char * ptr,int len)334 static CURLcode tftp_parse_option_ack(struct tftp_state_data *state,
335 const char *ptr, int len)
336 {
337 const char *tmp = ptr;
338 struct Curl_easy *data = state->conn->data;
339
340 /* if OACK doesn't contain blksize option, the default (512) must be used */
341 state->blksize = TFTP_BLKSIZE_DEFAULT;
342
343 while(tmp < ptr + len) {
344 const char *option, *value;
345
346 tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value);
347 if(tmp == NULL) {
348 failf(data, "Malformed ACK packet, rejecting");
349 return CURLE_TFTP_ILLEGAL;
350 }
351
352 infof(data, "got option=(%s) value=(%s)\n", option, value);
353
354 if(checkprefix(option, TFTP_OPTION_BLKSIZE)) {
355 long blksize;
356
357 blksize = strtol(value, NULL, 10);
358
359 if(!blksize) {
360 failf(data, "invalid blocksize value in OACK packet");
361 return CURLE_TFTP_ILLEGAL;
362 }
363 if(blksize > TFTP_BLKSIZE_MAX) {
364 failf(data, "%s (%d)", "blksize is larger than max supported",
365 TFTP_BLKSIZE_MAX);
366 return CURLE_TFTP_ILLEGAL;
367 }
368 else if(blksize < TFTP_BLKSIZE_MIN) {
369 failf(data, "%s (%d)", "blksize is smaller than min supported",
370 TFTP_BLKSIZE_MIN);
371 return CURLE_TFTP_ILLEGAL;
372 }
373 else if(blksize > state->requested_blksize) {
374 /* could realloc pkt buffers here, but the spec doesn't call out
375 * support for the server requesting a bigger blksize than the client
376 * requests */
377 failf(data, "%s (%ld)",
378 "server requested blksize larger than allocated", blksize);
379 return CURLE_TFTP_ILLEGAL;
380 }
381
382 state->blksize = (int)blksize;
383 infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
384 state->blksize, "requested", state->requested_blksize);
385 }
386 else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
387 long tsize = 0;
388
389 tsize = strtol(value, NULL, 10);
390 infof(data, "%s (%ld)\n", "tsize parsed from OACK", tsize);
391
392 /* tsize should be ignored on upload: Who cares about the size of the
393 remote file? */
394 if(!data->set.upload) {
395 if(!tsize) {
396 failf(data, "invalid tsize -:%s:- value in OACK packet", value);
397 return CURLE_TFTP_ILLEGAL;
398 }
399 Curl_pgrsSetDownloadSize(data, tsize);
400 }
401 }
402 }
403
404 return CURLE_OK;
405 }
406
tftp_option_add(struct tftp_state_data * state,size_t * csize,char * buf,const char * option)407 static CURLcode tftp_option_add(struct tftp_state_data *state, size_t *csize,
408 char *buf, const char *option)
409 {
410 if(( strlen(option) + *csize + 1) > (size_t)state->blksize)
411 return CURLE_TFTP_ILLEGAL;
412 strcpy(buf, option);
413 *csize += strlen(option) + 1;
414 return CURLE_OK;
415 }
416
tftp_connect_for_tx(struct tftp_state_data * state,tftp_event_t event)417 static CURLcode tftp_connect_for_tx(struct tftp_state_data *state,
418 tftp_event_t event)
419 {
420 CURLcode result;
421 #ifndef CURL_DISABLE_VERBOSE_STRINGS
422 struct Curl_easy *data = state->conn->data;
423
424 infof(data, "%s\n", "Connected for transmit");
425 #endif
426 state->state = TFTP_STATE_TX;
427 result = tftp_set_timeouts(state);
428 if(result)
429 return result;
430 return tftp_tx(state, event);
431 }
432
tftp_connect_for_rx(struct tftp_state_data * state,tftp_event_t event)433 static CURLcode tftp_connect_for_rx(struct tftp_state_data *state,
434 tftp_event_t event)
435 {
436 CURLcode result;
437 #ifndef CURL_DISABLE_VERBOSE_STRINGS
438 struct Curl_easy *data = state->conn->data;
439
440 infof(data, "%s\n", "Connected for receive");
441 #endif
442 state->state = TFTP_STATE_RX;
443 result = tftp_set_timeouts(state);
444 if(result)
445 return result;
446 return tftp_rx(state, event);
447 }
448
tftp_send_first(struct tftp_state_data * state,tftp_event_t event)449 static CURLcode tftp_send_first(struct tftp_state_data *state,
450 tftp_event_t event)
451 {
452 size_t sbytes;
453 ssize_t senddata;
454 const char *mode = "octet";
455 char *filename;
456 struct Curl_easy *data = state->conn->data;
457 CURLcode result = CURLE_OK;
458
459 /* Set ascii mode if -B flag was used */
460 if(data->set.prefer_ascii)
461 mode = "netascii";
462
463 switch(event) {
464
465 case TFTP_EVENT_INIT: /* Send the first packet out */
466 case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
467 /* Increment the retry counter, quit if over the limit */
468 state->retries++;
469 if(state->retries>state->retry_max) {
470 state->error = TFTP_ERR_NORESPONSE;
471 state->state = TFTP_STATE_FIN;
472 return result;
473 }
474
475 if(data->set.upload) {
476 /* If we are uploading, send an WRQ */
477 setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
478 state->conn->data->req.upload_fromhere =
479 (char *)state->spacket.data + 4;
480 if(data->state.infilesize != -1)
481 Curl_pgrsSetUploadSize(data, data->state.infilesize);
482 }
483 else {
484 /* If we are downloading, send an RRQ */
485 setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
486 }
487 /* As RFC3617 describes the separator slash is not actually part of the
488 file name so we skip the always-present first letter of the path
489 string. */
490 result = Curl_urldecode(data, &state->conn->data->state.up.path[1], 0,
491 &filename, NULL, REJECT_ZERO);
492 if(result)
493 return result;
494
495 if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
496 failf(data, "TFTP file name too long\n");
497 free(filename);
498 return CURLE_TFTP_ILLEGAL; /* too long file name field */
499 }
500
501 msnprintf((char *)state->spacket.data + 2,
502 state->blksize,
503 "%s%c%s%c", filename, '\0', mode, '\0');
504 sbytes = 4 + strlen(filename) + strlen(mode);
505
506 /* optional addition of TFTP options */
507 if(!data->set.tftp_no_options) {
508 char buf[64];
509 /* add tsize option */
510 if(data->set.upload && (data->state.infilesize != -1))
511 msnprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
512 data->state.infilesize);
513 else
514 strcpy(buf, "0"); /* the destination is large enough */
515
516 result = tftp_option_add(state, &sbytes,
517 (char *)state->spacket.data + sbytes,
518 TFTP_OPTION_TSIZE);
519 if(result == CURLE_OK)
520 result = tftp_option_add(state, &sbytes,
521 (char *)state->spacket.data + sbytes, buf);
522
523 /* add blksize option */
524 msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
525 if(result == CURLE_OK)
526 result = tftp_option_add(state, &sbytes,
527 (char *)state->spacket.data + sbytes,
528 TFTP_OPTION_BLKSIZE);
529 if(result == CURLE_OK)
530 result = tftp_option_add(state, &sbytes,
531 (char *)state->spacket.data + sbytes, buf);
532
533 /* add timeout option */
534 msnprintf(buf, sizeof(buf), "%d", state->retry_time);
535 if(result == CURLE_OK)
536 result = tftp_option_add(state, &sbytes,
537 (char *)state->spacket.data + sbytes,
538 TFTP_OPTION_INTERVAL);
539 if(result == CURLE_OK)
540 result = tftp_option_add(state, &sbytes,
541 (char *)state->spacket.data + sbytes, buf);
542
543 if(result != CURLE_OK) {
544 failf(data, "TFTP buffer too small for options");
545 free(filename);
546 return CURLE_TFTP_ILLEGAL;
547 }
548 }
549
550 /* the typecase for the 3rd argument is mostly for systems that do
551 not have a size_t argument, like older unixes that want an 'int' */
552 senddata = sendto(state->sockfd, (void *)state->spacket.data,
553 (SEND_TYPE_ARG3)sbytes, 0,
554 state->conn->ip_addr->ai_addr,
555 state->conn->ip_addr->ai_addrlen);
556 if(senddata != (ssize_t)sbytes) {
557 char buffer[STRERROR_LEN];
558 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
559 }
560 free(filename);
561 break;
562
563 case TFTP_EVENT_OACK:
564 if(data->set.upload) {
565 result = tftp_connect_for_tx(state, event);
566 }
567 else {
568 result = tftp_connect_for_rx(state, event);
569 }
570 break;
571
572 case TFTP_EVENT_ACK: /* Connected for transmit */
573 result = tftp_connect_for_tx(state, event);
574 break;
575
576 case TFTP_EVENT_DATA: /* Connected for receive */
577 result = tftp_connect_for_rx(state, event);
578 break;
579
580 case TFTP_EVENT_ERROR:
581 state->state = TFTP_STATE_FIN;
582 break;
583
584 default:
585 failf(state->conn->data, "tftp_send_first: internal error");
586 break;
587 }
588
589 return result;
590 }
591
592 /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
593 boundary */
594 #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
595
596 /**********************************************************
597 *
598 * tftp_rx
599 *
600 * Event handler for the RX state
601 *
602 **********************************************************/
tftp_rx(struct tftp_state_data * state,tftp_event_t event)603 static CURLcode tftp_rx(struct tftp_state_data *state,
604 tftp_event_t event)
605 {
606 ssize_t sbytes;
607 int rblock;
608 struct Curl_easy *data = state->conn->data;
609 char buffer[STRERROR_LEN];
610
611 switch(event) {
612
613 case TFTP_EVENT_DATA:
614 /* Is this the block we expect? */
615 rblock = getrpacketblock(&state->rpacket);
616 if(NEXT_BLOCKNUM(state->block) == rblock) {
617 /* This is the expected block. Reset counters and ACK it. */
618 state->retries = 0;
619 }
620 else if(state->block == rblock) {
621 /* This is the last recently received block again. Log it and ACK it
622 again. */
623 infof(data, "Received last DATA packet block %d again.\n", rblock);
624 }
625 else {
626 /* totally unexpected, just log it */
627 infof(data,
628 "Received unexpected DATA packet block %d, expecting block %d\n",
629 rblock, NEXT_BLOCKNUM(state->block));
630 break;
631 }
632
633 /* ACK this block. */
634 state->block = (unsigned short)rblock;
635 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
636 setpacketblock(&state->spacket, state->block);
637 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
638 4, SEND_4TH_ARG,
639 (struct sockaddr *)&state->remote_addr,
640 state->remote_addrlen);
641 if(sbytes < 0) {
642 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
643 return CURLE_SEND_ERROR;
644 }
645
646 /* Check if completed (That is, a less than full packet is received) */
647 if(state->rbytes < (ssize_t)state->blksize + 4) {
648 state->state = TFTP_STATE_FIN;
649 }
650 else {
651 state->state = TFTP_STATE_RX;
652 }
653 time(&state->rx_time);
654 break;
655
656 case TFTP_EVENT_OACK:
657 /* ACK option acknowledgement so we can move on to data */
658 state->block = 0;
659 state->retries = 0;
660 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
661 setpacketblock(&state->spacket, state->block);
662 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
663 4, SEND_4TH_ARG,
664 (struct sockaddr *)&state->remote_addr,
665 state->remote_addrlen);
666 if(sbytes < 0) {
667 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
668 return CURLE_SEND_ERROR;
669 }
670
671 /* we're ready to RX data */
672 state->state = TFTP_STATE_RX;
673 time(&state->rx_time);
674 break;
675
676 case TFTP_EVENT_TIMEOUT:
677 /* Increment the retry count and fail if over the limit */
678 state->retries++;
679 infof(data,
680 "Timeout waiting for block %d ACK. Retries = %d\n",
681 NEXT_BLOCKNUM(state->block), state->retries);
682 if(state->retries > state->retry_max) {
683 state->error = TFTP_ERR_TIMEOUT;
684 state->state = TFTP_STATE_FIN;
685 }
686 else {
687 /* Resend the previous ACK */
688 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
689 4, SEND_4TH_ARG,
690 (struct sockaddr *)&state->remote_addr,
691 state->remote_addrlen);
692 if(sbytes<0) {
693 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
694 return CURLE_SEND_ERROR;
695 }
696 }
697 break;
698
699 case TFTP_EVENT_ERROR:
700 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
701 setpacketblock(&state->spacket, state->block);
702 (void)sendto(state->sockfd, (void *)state->spacket.data,
703 4, SEND_4TH_ARG,
704 (struct sockaddr *)&state->remote_addr,
705 state->remote_addrlen);
706 /* don't bother with the return code, but if the socket is still up we
707 * should be a good TFTP client and let the server know we're done */
708 state->state = TFTP_STATE_FIN;
709 break;
710
711 default:
712 failf(data, "%s", "tftp_rx: internal error");
713 return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
714 this */
715 }
716 return CURLE_OK;
717 }
718
719 /**********************************************************
720 *
721 * tftp_tx
722 *
723 * Event handler for the TX state
724 *
725 **********************************************************/
tftp_tx(struct tftp_state_data * state,tftp_event_t event)726 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event)
727 {
728 struct Curl_easy *data = state->conn->data;
729 ssize_t sbytes;
730 CURLcode result = CURLE_OK;
731 struct SingleRequest *k = &data->req;
732 size_t cb; /* Bytes currently read */
733 char buffer[STRERROR_LEN];
734
735 switch(event) {
736
737 case TFTP_EVENT_ACK:
738 case TFTP_EVENT_OACK:
739 if(event == TFTP_EVENT_ACK) {
740 /* Ack the packet */
741 int rblock = getrpacketblock(&state->rpacket);
742
743 if(rblock != state->block &&
744 /* There's a bug in tftpd-hpa that causes it to send us an ack for
745 * 65535 when the block number wraps to 0. So when we're expecting
746 * 0, also accept 65535. See
747 * http://syslinux.zytor.com/archives/2010-September/015253.html
748 * */
749 !(state->block == 0 && rblock == 65535)) {
750 /* This isn't the expected block. Log it and up the retry counter */
751 infof(data, "Received ACK for block %d, expecting %d\n",
752 rblock, state->block);
753 state->retries++;
754 /* Bail out if over the maximum */
755 if(state->retries>state->retry_max) {
756 failf(data, "tftp_tx: giving up waiting for block %d ack",
757 state->block);
758 result = CURLE_SEND_ERROR;
759 }
760 else {
761 /* Re-send the data packet */
762 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
763 4 + state->sbytes, SEND_4TH_ARG,
764 (struct sockaddr *)&state->remote_addr,
765 state->remote_addrlen);
766 /* Check all sbytes were sent */
767 if(sbytes<0) {
768 failf(data, "%s", Curl_strerror(SOCKERRNO,
769 buffer, sizeof(buffer)));
770 result = CURLE_SEND_ERROR;
771 }
772 }
773
774 return result;
775 }
776 /* This is the expected packet. Reset the counters and send the next
777 block */
778 time(&state->rx_time);
779 state->block++;
780 }
781 else
782 state->block = 1; /* first data block is 1 when using OACK */
783
784 state->retries = 0;
785 setpacketevent(&state->spacket, TFTP_EVENT_DATA);
786 setpacketblock(&state->spacket, state->block);
787 if(state->block > 1 && state->sbytes < state->blksize) {
788 state->state = TFTP_STATE_FIN;
789 return CURLE_OK;
790 }
791
792 /* TFTP considers data block size < 512 bytes as an end of session. So
793 * in some cases we must wait for additional data to build full (512 bytes)
794 * data block.
795 * */
796 state->sbytes = 0;
797 state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4;
798 do {
799 result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes,
800 &cb);
801 if(result)
802 return result;
803 state->sbytes += (int)cb;
804 state->conn->data->req.upload_fromhere += cb;
805 } while(state->sbytes < state->blksize && cb != 0);
806
807 sbytes = sendto(state->sockfd, (void *) state->spacket.data,
808 4 + state->sbytes, SEND_4TH_ARG,
809 (struct sockaddr *)&state->remote_addr,
810 state->remote_addrlen);
811 /* Check all sbytes were sent */
812 if(sbytes<0) {
813 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
814 return CURLE_SEND_ERROR;
815 }
816 /* Update the progress meter */
817 k->writebytecount += state->sbytes;
818 Curl_pgrsSetUploadCounter(data, k->writebytecount);
819 break;
820
821 case TFTP_EVENT_TIMEOUT:
822 /* Increment the retry counter and log the timeout */
823 state->retries++;
824 infof(data, "Timeout waiting for block %d ACK. "
825 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
826 /* Decide if we've had enough */
827 if(state->retries > state->retry_max) {
828 state->error = TFTP_ERR_TIMEOUT;
829 state->state = TFTP_STATE_FIN;
830 }
831 else {
832 /* Re-send the data packet */
833 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
834 4 + state->sbytes, SEND_4TH_ARG,
835 (struct sockaddr *)&state->remote_addr,
836 state->remote_addrlen);
837 /* Check all sbytes were sent */
838 if(sbytes<0) {
839 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
840 return CURLE_SEND_ERROR;
841 }
842 /* since this was a re-send, we remain at the still byte position */
843 Curl_pgrsSetUploadCounter(data, k->writebytecount);
844 }
845 break;
846
847 case TFTP_EVENT_ERROR:
848 state->state = TFTP_STATE_FIN;
849 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
850 setpacketblock(&state->spacket, state->block);
851 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
852 (struct sockaddr *)&state->remote_addr,
853 state->remote_addrlen);
854 /* don't bother with the return code, but if the socket is still up we
855 * should be a good TFTP client and let the server know we're done */
856 state->state = TFTP_STATE_FIN;
857 break;
858
859 default:
860 failf(data, "tftp_tx: internal error, event: %i", (int)(event));
861 break;
862 }
863
864 return result;
865 }
866
867 /**********************************************************
868 *
869 * tftp_translate_code
870 *
871 * Translate internal error codes to CURL error codes
872 *
873 **********************************************************/
tftp_translate_code(tftp_error_t error)874 static CURLcode tftp_translate_code(tftp_error_t error)
875 {
876 CURLcode result = CURLE_OK;
877
878 if(error != TFTP_ERR_NONE) {
879 switch(error) {
880 case TFTP_ERR_NOTFOUND:
881 result = CURLE_TFTP_NOTFOUND;
882 break;
883 case TFTP_ERR_PERM:
884 result = CURLE_TFTP_PERM;
885 break;
886 case TFTP_ERR_DISKFULL:
887 result = CURLE_REMOTE_DISK_FULL;
888 break;
889 case TFTP_ERR_UNDEF:
890 case TFTP_ERR_ILLEGAL:
891 result = CURLE_TFTP_ILLEGAL;
892 break;
893 case TFTP_ERR_UNKNOWNID:
894 result = CURLE_TFTP_UNKNOWNID;
895 break;
896 case TFTP_ERR_EXISTS:
897 result = CURLE_REMOTE_FILE_EXISTS;
898 break;
899 case TFTP_ERR_NOSUCHUSER:
900 result = CURLE_TFTP_NOSUCHUSER;
901 break;
902 case TFTP_ERR_TIMEOUT:
903 result = CURLE_OPERATION_TIMEDOUT;
904 break;
905 case TFTP_ERR_NORESPONSE:
906 result = CURLE_COULDNT_CONNECT;
907 break;
908 default:
909 result = CURLE_ABORTED_BY_CALLBACK;
910 break;
911 }
912 }
913 else
914 result = CURLE_OK;
915
916 return result;
917 }
918
919 /**********************************************************
920 *
921 * tftp_state_machine
922 *
923 * The tftp state machine event dispatcher
924 *
925 **********************************************************/
tftp_state_machine(struct tftp_state_data * state,tftp_event_t event)926 static CURLcode tftp_state_machine(struct tftp_state_data *state,
927 tftp_event_t event)
928 {
929 CURLcode result = CURLE_OK;
930 struct Curl_easy *data = state->conn->data;
931
932 switch(state->state) {
933 case TFTP_STATE_START:
934 DEBUGF(infof(data, "TFTP_STATE_START\n"));
935 result = tftp_send_first(state, event);
936 break;
937 case TFTP_STATE_RX:
938 DEBUGF(infof(data, "TFTP_STATE_RX\n"));
939 result = tftp_rx(state, event);
940 break;
941 case TFTP_STATE_TX:
942 DEBUGF(infof(data, "TFTP_STATE_TX\n"));
943 result = tftp_tx(state, event);
944 break;
945 case TFTP_STATE_FIN:
946 infof(data, "%s\n", "TFTP finished");
947 break;
948 default:
949 DEBUGF(infof(data, "STATE: %d\n", state->state));
950 failf(data, "%s", "Internal state machine error");
951 result = CURLE_TFTP_ILLEGAL;
952 break;
953 }
954
955 return result;
956 }
957
958 /**********************************************************
959 *
960 * tftp_disconnect
961 *
962 * The disconnect callback
963 *
964 **********************************************************/
tftp_disconnect(struct connectdata * conn,bool dead_connection)965 static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
966 {
967 struct tftp_state_data *state = conn->proto.tftpc;
968 (void) dead_connection;
969
970 /* done, free dynamically allocated pkt buffers */
971 if(state) {
972 Curl_safefree(state->rpacket.data);
973 Curl_safefree(state->spacket.data);
974 free(state);
975 }
976
977 return CURLE_OK;
978 }
979
980 /**********************************************************
981 *
982 * tftp_connect
983 *
984 * The connect callback
985 *
986 **********************************************************/
tftp_connect(struct connectdata * conn,bool * done)987 static CURLcode tftp_connect(struct connectdata *conn, bool *done)
988 {
989 struct tftp_state_data *state;
990 int blksize;
991 int need_blksize;
992
993 blksize = TFTP_BLKSIZE_DEFAULT;
994
995 state = conn->proto.tftpc = calloc(1, sizeof(struct tftp_state_data));
996 if(!state)
997 return CURLE_OUT_OF_MEMORY;
998
999 /* alloc pkt buffers based on specified blksize */
1000 if(conn->data->set.tftp_blksize) {
1001 blksize = (int)conn->data->set.tftp_blksize;
1002 if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
1003 return CURLE_TFTP_ILLEGAL;
1004 }
1005
1006 need_blksize = blksize;
1007 /* default size is the fallback when no OACK is received */
1008 if(need_blksize < TFTP_BLKSIZE_DEFAULT)
1009 need_blksize = TFTP_BLKSIZE_DEFAULT;
1010
1011 if(!state->rpacket.data) {
1012 state->rpacket.data = calloc(1, need_blksize + 2 + 2);
1013
1014 if(!state->rpacket.data)
1015 return CURLE_OUT_OF_MEMORY;
1016 }
1017
1018 if(!state->spacket.data) {
1019 state->spacket.data = calloc(1, need_blksize + 2 + 2);
1020
1021 if(!state->spacket.data)
1022 return CURLE_OUT_OF_MEMORY;
1023 }
1024
1025 /* we don't keep TFTP connections up basically because there's none or very
1026 * little gain for UDP */
1027 connclose(conn, "TFTP");
1028
1029 state->conn = conn;
1030 state->sockfd = state->conn->sock[FIRSTSOCKET];
1031 state->state = TFTP_STATE_START;
1032 state->error = TFTP_ERR_NONE;
1033 state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
1034 state->requested_blksize = blksize;
1035
1036 ((struct sockaddr *)&state->local_addr)->sa_family =
1037 (CURL_SA_FAMILY_T)(conn->ip_addr->ai_family);
1038
1039 tftp_set_timeouts(state);
1040
1041 if(!conn->bits.bound) {
1042 /* If not already bound, bind to any interface, random UDP port. If it is
1043 * reused or a custom local port was desired, this has already been done!
1044 *
1045 * We once used the size of the local_addr struct as the third argument
1046 * for bind() to better work with IPv6 or whatever size the struct could
1047 * have, but we learned that at least Tru64, AIX and IRIX *requires* the
1048 * size of that argument to match the exact size of a 'sockaddr_in' struct
1049 * when running IPv4-only.
1050 *
1051 * Therefore we use the size from the address we connected to, which we
1052 * assume uses the same IP version and thus hopefully this works for both
1053 * IPv4 and IPv6...
1054 */
1055 int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
1056 conn->ip_addr->ai_addrlen);
1057 if(rc) {
1058 char buffer[STRERROR_LEN];
1059 failf(conn->data, "bind() failed; %s",
1060 Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1061 return CURLE_COULDNT_CONNECT;
1062 }
1063 conn->bits.bound = TRUE;
1064 }
1065
1066 Curl_pgrsStartNow(conn->data);
1067
1068 *done = TRUE;
1069
1070 return CURLE_OK;
1071 }
1072
1073 /**********************************************************
1074 *
1075 * tftp_done
1076 *
1077 * The done callback
1078 *
1079 **********************************************************/
tftp_done(struct connectdata * conn,CURLcode status,bool premature)1080 static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
1081 bool premature)
1082 {
1083 CURLcode result = CURLE_OK;
1084 struct tftp_state_data *state = conn->proto.tftpc;
1085
1086 (void)status; /* unused */
1087 (void)premature; /* not used */
1088
1089 if(Curl_pgrsDone(conn))
1090 return CURLE_ABORTED_BY_CALLBACK;
1091
1092 /* If we have encountered an error */
1093 if(state)
1094 result = tftp_translate_code(state->error);
1095
1096 return result;
1097 }
1098
1099 /**********************************************************
1100 *
1101 * tftp_getsock
1102 *
1103 * The getsock callback
1104 *
1105 **********************************************************/
tftp_getsock(struct connectdata * conn,curl_socket_t * socks)1106 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks)
1107 {
1108 socks[0] = conn->sock[FIRSTSOCKET];
1109 return GETSOCK_READSOCK(0);
1110 }
1111
1112 /**********************************************************
1113 *
1114 * tftp_receive_packet
1115 *
1116 * Called once select fires and data is ready on the socket
1117 *
1118 **********************************************************/
tftp_receive_packet(struct connectdata * conn)1119 static CURLcode tftp_receive_packet(struct connectdata *conn)
1120 {
1121 struct Curl_sockaddr_storage fromaddr;
1122 curl_socklen_t fromlen;
1123 CURLcode result = CURLE_OK;
1124 struct Curl_easy *data = conn->data;
1125 struct tftp_state_data *state = conn->proto.tftpc;
1126 struct SingleRequest *k = &data->req;
1127
1128 /* Receive the packet */
1129 fromlen = sizeof(fromaddr);
1130 state->rbytes = (int)recvfrom(state->sockfd,
1131 (void *)state->rpacket.data,
1132 state->blksize + 4,
1133 0,
1134 (struct sockaddr *)&fromaddr,
1135 &fromlen);
1136 if(state->remote_addrlen == 0) {
1137 memcpy(&state->remote_addr, &fromaddr, fromlen);
1138 state->remote_addrlen = fromlen;
1139 }
1140
1141 /* Sanity check packet length */
1142 if(state->rbytes < 4) {
1143 failf(data, "Received too short packet");
1144 /* Not a timeout, but how best to handle it? */
1145 state->event = TFTP_EVENT_TIMEOUT;
1146 }
1147 else {
1148 /* The event is given by the TFTP packet time */
1149 unsigned short event = getrpacketevent(&state->rpacket);
1150 state->event = (tftp_event_t)event;
1151
1152 switch(state->event) {
1153 case TFTP_EVENT_DATA:
1154 /* Don't pass to the client empty or retransmitted packets */
1155 if(state->rbytes > 4 &&
1156 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
1157 result = Curl_client_write(conn, CLIENTWRITE_BODY,
1158 (char *)state->rpacket.data + 4,
1159 state->rbytes-4);
1160 if(result) {
1161 tftp_state_machine(state, TFTP_EVENT_ERROR);
1162 return result;
1163 }
1164 k->bytecount += state->rbytes-4;
1165 Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
1166 }
1167 break;
1168 case TFTP_EVENT_ERROR:
1169 {
1170 unsigned short error = getrpacketblock(&state->rpacket);
1171 char *str = (char *)state->rpacket.data + 4;
1172 size_t strn = state->rbytes - 4;
1173 state->error = (tftp_error_t)error;
1174 if(Curl_strnlen(str, strn) < strn)
1175 infof(data, "TFTP error: %s\n", str);
1176 break;
1177 }
1178 case TFTP_EVENT_ACK:
1179 break;
1180 case TFTP_EVENT_OACK:
1181 result = tftp_parse_option_ack(state,
1182 (const char *)state->rpacket.data + 2,
1183 state->rbytes-2);
1184 if(result)
1185 return result;
1186 break;
1187 case TFTP_EVENT_RRQ:
1188 case TFTP_EVENT_WRQ:
1189 default:
1190 failf(data, "%s", "Internal error: Unexpected packet");
1191 break;
1192 }
1193
1194 /* Update the progress meter */
1195 if(Curl_pgrsUpdate(conn)) {
1196 tftp_state_machine(state, TFTP_EVENT_ERROR);
1197 return CURLE_ABORTED_BY_CALLBACK;
1198 }
1199 }
1200 return result;
1201 }
1202
1203 /**********************************************************
1204 *
1205 * tftp_state_timeout
1206 *
1207 * Check if timeouts have been reached
1208 *
1209 **********************************************************/
tftp_state_timeout(struct connectdata * conn,tftp_event_t * event)1210 static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
1211 {
1212 time_t current;
1213 struct tftp_state_data *state = conn->proto.tftpc;
1214
1215 if(event)
1216 *event = TFTP_EVENT_NONE;
1217
1218 time(¤t);
1219 if(current > state->max_time) {
1220 DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
1221 (long)current, (long)state->max_time));
1222 state->error = TFTP_ERR_TIMEOUT;
1223 state->state = TFTP_STATE_FIN;
1224 return 0;
1225 }
1226 if(current > state->rx_time + state->retry_time) {
1227 if(event)
1228 *event = TFTP_EVENT_TIMEOUT;
1229 time(&state->rx_time); /* update even though we received nothing */
1230 }
1231
1232 /* there's a typecast below here since 'time_t' may in fact be larger than
1233 'long', but we estimate that a 'long' will still be able to hold number
1234 of seconds even if "only" 32 bit */
1235 return (long)(state->max_time - current);
1236 }
1237
1238 /**********************************************************
1239 *
1240 * tftp_multi_statemach
1241 *
1242 * Handle single RX socket event and return
1243 *
1244 **********************************************************/
tftp_multi_statemach(struct connectdata * conn,bool * done)1245 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
1246 {
1247 tftp_event_t event;
1248 CURLcode result = CURLE_OK;
1249 struct Curl_easy *data = conn->data;
1250 struct tftp_state_data *state = conn->proto.tftpc;
1251 long timeout_ms = tftp_state_timeout(conn, &event);
1252
1253 *done = FALSE;
1254
1255 if(timeout_ms <= 0) {
1256 failf(data, "TFTP response timeout");
1257 return CURLE_OPERATION_TIMEDOUT;
1258 }
1259 if(event != TFTP_EVENT_NONE) {
1260 result = tftp_state_machine(state, event);
1261 if(result)
1262 return result;
1263 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1264 if(*done)
1265 /* Tell curl we're done */
1266 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1267 }
1268 else {
1269 /* no timeouts to handle, check our socket */
1270 int rc = SOCKET_READABLE(state->sockfd, 0);
1271
1272 if(rc == -1) {
1273 /* bail out */
1274 int error = SOCKERRNO;
1275 char buffer[STRERROR_LEN];
1276 failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer)));
1277 state->event = TFTP_EVENT_ERROR;
1278 }
1279 else if(rc != 0) {
1280 result = tftp_receive_packet(conn);
1281 if(result)
1282 return result;
1283 result = tftp_state_machine(state, state->event);
1284 if(result)
1285 return result;
1286 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1287 if(*done)
1288 /* Tell curl we're done */
1289 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1290 }
1291 /* if rc == 0, then select() timed out */
1292 }
1293
1294 return result;
1295 }
1296
1297 /**********************************************************
1298 *
1299 * tftp_doing
1300 *
1301 * Called from multi.c while DOing
1302 *
1303 **********************************************************/
tftp_doing(struct connectdata * conn,bool * dophase_done)1304 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
1305 {
1306 CURLcode result;
1307 result = tftp_multi_statemach(conn, dophase_done);
1308
1309 if(*dophase_done) {
1310 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1311 }
1312 else if(!result) {
1313 /* The multi code doesn't have this logic for the DOING state so we
1314 provide it for TFTP since it may do the entire transfer in this
1315 state. */
1316 if(Curl_pgrsUpdate(conn))
1317 result = CURLE_ABORTED_BY_CALLBACK;
1318 else
1319 result = Curl_speedcheck(conn->data, Curl_now());
1320 }
1321 return result;
1322 }
1323
1324 /**********************************************************
1325 *
1326 * tftp_peform
1327 *
1328 * Entry point for transfer from tftp_do, sarts state mach
1329 *
1330 **********************************************************/
tftp_perform(struct connectdata * conn,bool * dophase_done)1331 static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
1332 {
1333 CURLcode result = CURLE_OK;
1334 struct tftp_state_data *state = conn->proto.tftpc;
1335
1336 *dophase_done = FALSE;
1337
1338 result = tftp_state_machine(state, TFTP_EVENT_INIT);
1339
1340 if((state->state == TFTP_STATE_FIN) || result)
1341 return result;
1342
1343 tftp_multi_statemach(conn, dophase_done);
1344
1345 if(*dophase_done)
1346 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1347
1348 return result;
1349 }
1350
1351
1352 /**********************************************************
1353 *
1354 * tftp_do
1355 *
1356 * The do callback
1357 *
1358 * This callback initiates the TFTP transfer
1359 *
1360 **********************************************************/
1361
tftp_do(struct connectdata * conn,bool * done)1362 static CURLcode tftp_do(struct connectdata *conn, bool *done)
1363 {
1364 struct tftp_state_data *state;
1365 CURLcode result;
1366
1367 *done = FALSE;
1368
1369 if(!conn->proto.tftpc) {
1370 result = tftp_connect(conn, done);
1371 if(result)
1372 return result;
1373 }
1374
1375 state = conn->proto.tftpc;
1376 if(!state)
1377 return CURLE_TFTP_ILLEGAL;
1378
1379 result = tftp_perform(conn, done);
1380
1381 /* If tftp_perform() returned an error, use that for return code. If it
1382 was OK, see if tftp_translate_code() has an error. */
1383 if(!result)
1384 /* If we have encountered an internal tftp error, translate it. */
1385 result = tftp_translate_code(state->error);
1386
1387 return result;
1388 }
1389
tftp_setup_connection(struct connectdata * conn)1390 static CURLcode tftp_setup_connection(struct connectdata *conn)
1391 {
1392 struct Curl_easy *data = conn->data;
1393 char *type;
1394
1395 conn->transport = TRNSPRT_UDP;
1396
1397 /* TFTP URLs support an extension like ";mode=<typecode>" that
1398 * we'll try to get now! */
1399 type = strstr(data->state.up.path, ";mode=");
1400
1401 if(!type)
1402 type = strstr(conn->host.rawalloc, ";mode=");
1403
1404 if(type) {
1405 char command;
1406 *type = 0; /* it was in the middle of the hostname */
1407 command = Curl_raw_toupper(type[6]);
1408
1409 switch(command) {
1410 case 'A': /* ASCII mode */
1411 case 'N': /* NETASCII mode */
1412 data->set.prefer_ascii = TRUE;
1413 break;
1414
1415 case 'O': /* octet mode */
1416 case 'I': /* binary mode */
1417 default:
1418 /* switch off ASCII */
1419 data->set.prefer_ascii = FALSE;
1420 break;
1421 }
1422 }
1423
1424 return CURLE_OK;
1425 }
1426 #endif
1427