Coverage Report

Created: 2026-12-01 06:26

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