Coverage Report

Created: 2026-10-15 06:18

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
        if(!tsize) {
319
0
          failf(data, "invalid tsize -:%s:- value in OACK packet", value);
320
0
          return CURLE_TFTP_ILLEGAL;
321
0
        }
322
0
        infof(data, "tsize parsed from OACK (%" CURL_FORMAT_CURL_OFF_T ")",
323
0
              tsize);
324
0
        Curl_pgrsSetDownloadSize(data, tsize);
325
0
      }
326
0
    }
327
0
  }
328
329
0
  return CURLE_OK;
330
0
}
331
332
static CURLcode tftp_option_add(struct tftp_conn *state, size_t *csize,
333
                                size_t index, const char *option)
334
0
{
335
0
  char *buf = (char *)&state->spacket.data[index];
336
0
  size_t oplen = strlen(option);
337
0
  size_t blen;
338
0
  if((state->blksize <= index) ||
339
0
     (oplen + 1) > (size_t)(state->blksize - index))
340
0
    return CURLE_TFTP_ILLEGAL;
341
0
  blen = state->blksize - index;
342
0
  curlx_strcopy(buf, blen, option, oplen);
343
0
  *csize += oplen + 1;
344
0
  return CURLE_OK;
345
0
}
346
347
/* the next blocknum is x + 1 but it needs to wrap at an unsigned 16-bit
348
   boundary */
349
0
#define NEXT_BLOCKNUM(x) (((x) + 1) & 0xffff)
350
351
/**********************************************************
352
 *
353
 * tftp_tx
354
 *
355
 * Event handler for the TX state
356
 *
357
 **********************************************************/
358
static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event)
359
0
{
360
0
  struct Curl_easy *data = state->data;
361
0
  ssize_t sbytes;
362
0
  CURLcode result = CURLE_OK;
363
0
  struct SingleRequest *k = &data->req;
364
0
  size_t cb; /* Bytes currently read */
365
0
  char buffer[STRERROR_LEN];
366
0
  char *bufptr;
367
0
  bool eos;
368
369
0
  if(!data->state.upload)
370
0
    return CURLE_TFTP_ILLEGAL;
371
372
0
  switch(event) {
373
374
0
  case TFTP_EVENT_ACK:
375
0
  case TFTP_EVENT_OACK:
376
0
    if(event == TFTP_EVENT_ACK) {
377
      /* Ack the packet */
378
0
      int rblock = getrpacketblock(&state->rpacket);
379
380
0
      if(rblock != state->block &&
381
         /* There is a bug in tftpd-hpa that causes it to send us an ACK for
382
          * 65535 when the block number wraps to 0. To handle it, when we are
383
          * expecting 0, also accept 65535. See
384
          * https://www.syslinux.org/archives/2010-September/015612.html
385
          * */
386
0
         !(state->block == 0 && rblock == 65535)) {
387
        /* This is not the expected block. Log it and up the retry counter */
388
0
        infof(data, "Received ACK for block %d, expecting %d",
389
0
              rblock, state->block);
390
0
        state->retries++;
391
        /* Bail out if over the maximum */
392
0
        if(state->retries > state->retry_max) {
393
0
          failf(data, "tftp_tx: giving up waiting for block %d ack",
394
0
                state->block);
395
0
          result = CURLE_SEND_ERROR;
396
0
        }
397
0
        else {
398
          /* Re-send the data packet */
399
0
          sbytes = sendto(state->sockfd, (void *)state->spacket.data,
400
0
                          4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
401
0
                          (struct sockaddr *)&state->remote_addr,
402
0
                          state->remote_addrlen);
403
          /* Check all sbytes were sent */
404
0
          if(sbytes < 0) {
405
0
            failf(data, "%s",
406
0
                  curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
407
0
            result = CURLE_SEND_ERROR;
408
0
          }
409
0
        }
410
411
0
        return result;
412
0
      }
413
      /* This is the expected packet. Reset the counters and send the next
414
         block */
415
0
      state->rx_time = time(NULL);
416
0
      state->block++;
417
0
    }
418
0
    else
419
0
      state->block = 1; /* first data block is 1 when using OACK */
420
421
0
    state->retries = 0;
422
0
    setpacketevent(&state->spacket, TFTP_EVENT_DATA);
423
0
    setpacketblock(&state->spacket, state->block);
424
0
    if(state->block > 1 && state->sbytes < state->blksize) {
425
0
      state->state = TFTP_STATE_FIN;
426
0
      return CURLE_OK;
427
0
    }
428
429
    /* TFTP considers data block size < 512 bytes as an end of session, so
430
     * in some cases we must wait for additional data to build full (512 bytes)
431
     * data block.
432
     * */
433
0
    state->sbytes = 0;
434
0
    bufptr = (char *)state->spacket.data + 4;
435
0
    do {
436
0
      result = Curl_client_read(data, bufptr, state->blksize - state->sbytes,
437
0
                                &cb, &eos);
438
0
      if(result)
439
0
        return result;
440
0
      state->sbytes += cb;
441
0
      bufptr += cb;
442
0
    } while(state->sbytes < state->blksize && cb);
443
444
0
    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
445
0
                    4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
446
0
                    (struct sockaddr *)&state->remote_addr,
447
0
                    state->remote_addrlen);
448
    /* Check all sbytes were sent */
449
0
    if(sbytes < 0) {
450
0
      failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
451
0
      return CURLE_SEND_ERROR;
452
0
    }
453
    /* Update the progress meter */
454
0
    k->writebytecount += state->sbytes;
455
0
    Curl_pgrs_upload_inc(data, state->sbytes);
456
0
    break;
457
458
0
  case TFTP_EVENT_TIMEOUT:
459
    /* Increment the retry counter and log the timeout */
460
0
    state->retries++;
461
0
    infof(data, "Timeout waiting for block %d ACK. "
462
0
          "Retries = %d", NEXT_BLOCKNUM(state->block), state->retries);
463
    /* Decide if we have had enough */
464
0
    if(state->retries > state->retry_max) {
465
0
      state->error = TFTP_ERR_TIMEOUT;
466
0
      state->state = TFTP_STATE_FIN;
467
0
    }
468
0
    else {
469
      /* Re-send the data packet */
470
0
      sbytes = sendto(state->sockfd, (void *)state->spacket.data,
471
0
                      4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
472
0
                      (struct sockaddr *)&state->remote_addr,
473
0
                      state->remote_addrlen);
474
      /* Check all sbytes were sent */
475
0
      if(sbytes < 0) {
476
0
        failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
477
0
        return CURLE_SEND_ERROR;
478
0
      }
479
      /* since this was a re-send, we remain at the still byte position */
480
0
      Curl_pgrsSetUploadCounter(data, k->writebytecount);
481
0
    }
482
0
    break;
483
484
0
  case TFTP_EVENT_ERROR:
485
0
    state->state = TFTP_STATE_FIN;
486
0
    setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
487
0
    setpacketblock(&state->spacket, state->block);
488
0
    (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
489
0
                 (struct sockaddr *)&state->remote_addr,
490
0
                 state->remote_addrlen);
491
    /* do not bother with the return code, but if the socket is still up we
492
     * should be a good TFTP client and let the server know we are done */
493
0
    state->state = TFTP_STATE_FIN;
494
0
    break;
495
496
0
  default:
497
0
    failf(data, "tftp_tx: internal error, event: %d", (int)event);
498
0
    break;
499
0
  }
500
501
0
  return result;
502
0
}
503
504
static CURLcode tftp_connect_for_tx(struct tftp_conn *state,
505
                                    tftp_event_t event)
506
0
{
507
0
  CURLcode result;
508
509
0
  infof(state->data, "%s", "Connected for transmit");
510
511
0
  state->state = TFTP_STATE_TX;
512
0
  result = tftp_set_timeouts(state);
513
0
  if(result)
514
0
    return result;
515
0
  return tftp_tx(state, event);
516
0
}
517
518
/**********************************************************
519
 *
520
 * tftp_rx
521
 *
522
 * Event handler for the RX state
523
 *
524
 **********************************************************/
525
static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event)
526
0
{
527
0
  ssize_t sbytes;
528
0
  int rblock;
529
0
  struct Curl_easy *data = state->data;
530
0
  char buffer[STRERROR_LEN];
531
532
0
  switch(event) {
533
534
0
  case TFTP_EVENT_DATA:
535
    /* Is this the block we expect? */
536
0
    rblock = getrpacketblock(&state->rpacket);
537
0
    if(NEXT_BLOCKNUM(state->block) == rblock) {
538
      /* This is the expected block. Reset counters and ACK it. */
539
0
      state->retries = 0;
540
0
    }
541
0
    else if(state->block == rblock) {
542
      /* This is the last recently received block again. Log it and ACK it
543
         again. */
544
0
      infof(data, "Received last DATA packet block %d again.", rblock);
545
0
    }
546
0
    else {
547
      /* totally unexpected, log it */
548
0
      infof(data,
549
0
            "Received unexpected DATA packet block %d, expecting block %d",
550
0
            rblock, NEXT_BLOCKNUM(state->block));
551
0
      break;
552
0
    }
553
554
    /* ACK this block. */
555
0
    state->block = (unsigned short)rblock;
556
0
    setpacketevent(&state->spacket, TFTP_EVENT_ACK);
557
0
    setpacketblock(&state->spacket, state->block);
558
0
    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
559
0
                    4, SEND_4TH_ARG,
560
0
                    (struct sockaddr *)&state->remote_addr,
561
0
                    state->remote_addrlen);
562
0
    if(sbytes < 0) {
563
0
      failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
564
0
      return CURLE_SEND_ERROR;
565
0
    }
566
567
    /* Check if completed (That is, a less than full packet is received) */
568
0
    if(state->rbytes < (ssize_t)state->blksize + 4) {
569
0
      state->state = TFTP_STATE_FIN;
570
0
    }
571
0
    else {
572
0
      state->state = TFTP_STATE_RX;
573
0
    }
574
0
    state->rx_time = time(NULL);
575
0
    break;
576
577
0
  case TFTP_EVENT_OACK:
578
    /* ACK option acknowledgement so we can move on to data */
579
0
    state->block = 0;
580
0
    state->retries = 0;
581
0
    setpacketevent(&state->spacket, TFTP_EVENT_ACK);
582
0
    setpacketblock(&state->spacket, state->block);
583
0
    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
584
0
                    4, SEND_4TH_ARG,
585
0
                    (struct sockaddr *)&state->remote_addr,
586
0
                    state->remote_addrlen);
587
0
    if(sbytes < 0) {
588
0
      failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
589
0
      return CURLE_SEND_ERROR;
590
0
    }
591
592
    /* we are ready to RX data */
593
0
    state->state = TFTP_STATE_RX;
594
0
    state->rx_time = time(NULL);
595
0
    break;
596
597
0
  case TFTP_EVENT_TIMEOUT:
598
    /* Increment the retry count and fail if over the limit */
599
0
    state->retries++;
600
0
    infof(data,
601
0
          "Timeout waiting for block %d ACK. Retries = %d",
602
0
          NEXT_BLOCKNUM(state->block), state->retries);
603
0
    if(state->retries > state->retry_max) {
604
0
      state->error = TFTP_ERR_TIMEOUT;
605
0
      state->state = TFTP_STATE_FIN;
606
0
    }
607
0
    else {
608
      /* Resend the previous ACK */
609
0
      sbytes = sendto(state->sockfd, (void *)state->spacket.data,
610
0
                      4, SEND_4TH_ARG,
611
0
                      (struct sockaddr *)&state->remote_addr,
612
0
                      state->remote_addrlen);
613
0
      if(sbytes < 0) {
614
0
        failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
615
0
        return CURLE_SEND_ERROR;
616
0
      }
617
0
    }
618
0
    break;
619
620
0
  case TFTP_EVENT_ERROR:
621
0
    setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
622
0
    setpacketblock(&state->spacket, state->block);
623
0
    (void)sendto(state->sockfd, (void *)state->spacket.data,
624
0
                 4, SEND_4TH_ARG,
625
0
                 (struct sockaddr *)&state->remote_addr,
626
0
                 state->remote_addrlen);
627
    /* do not bother with the return code, but if the socket is still up we
628
     * should be a good TFTP client and let the server know we are done */
629
0
    state->state = TFTP_STATE_FIN;
630
0
    break;
631
632
0
  default:
633
0
    failf(data, "%s", "tftp_rx: internal error");
634
0
    return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
635
                                  this */
636
0
  }
637
0
  return CURLE_OK;
638
0
}
639
640
static CURLcode tftp_connect_for_rx(struct tftp_conn *state,
641
                                    tftp_event_t event)
642
0
{
643
0
  CURLcode result;
644
645
0
  infof(state->data, "%s", "Connected for receive");
646
647
0
  state->state = TFTP_STATE_RX;
648
0
  result = tftp_set_timeouts(state);
649
0
  if(result)
650
0
    return result;
651
0
  return tftp_rx(state, event);
652
0
}
653
654
static CURLcode tftp_send_first(struct tftp_conn *state,
655
                                tftp_event_t event)
656
0
{
657
0
  size_t sbytes;
658
0
  ssize_t senddata;
659
0
  const char *mode = "octet";
660
0
  char *filename;
661
0
  struct Curl_easy *data = state->data;
662
0
  const struct Curl_sockaddr_ex *remote_addr = NULL;
663
0
  CURLcode result = CURLE_OK;
664
665
  /* Set ASCII mode if -B flag was used */
666
0
  if(data->state.prefer_ascii)
667
0
    mode = "netascii";
668
669
0
  switch(event) {
670
671
0
  case TFTP_EVENT_INIT:    /* Send the first packet out */
672
0
  case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
673
    /* Increment the retry counter, quit if over the limit */
674
0
    state->retries++;
675
0
    if(state->retries > state->retry_max) {
676
0
      state->error = TFTP_ERR_NORESPONSE;
677
0
      state->state = TFTP_STATE_FIN;
678
0
      return result;
679
0
    }
680
681
0
    if(data->state.upload) {
682
      /* If we are uploading, send a WRQ */
683
0
      setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
684
0
      if(data->state.infilesize != -1)
685
0
        Curl_pgrsSetUploadSize(data, data->state.infilesize);
686
0
    }
687
0
    else {
688
      /* If we are downloading, send an RRQ */
689
0
      setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
690
0
    }
691
    /* As RFC3617 describes the separator slash is not actually part of the
692
       filename so we skip the always-present first letter of the path
693
       string. */
694
0
    if(!state->data->state.up.path[1]) {
695
0
      failf(data, "Missing filename");
696
0
      return CURLE_TFTP_ILLEGAL;
697
0
    }
698
0
    result = Curl_urldecode(&state->data->state.up.path[1], 0,
699
0
                            &filename, NULL, REJECT_ZERO);
700
0
    if(result)
701
0
      return result;
702
703
0
    if(strlen(filename) + strlen(mode) + 4 > state->blksize) {
704
0
      failf(data, "TFTP filename too long");
705
0
      curlx_free(filename);
706
0
      return CURLE_TFTP_ILLEGAL; /* too long filename field */
707
0
    }
708
709
0
    sbytes = 2 +
710
0
      curl_msnprintf((char *)state->spacket.data + 2,
711
0
                     state->blksize,
712
0
                     "%s%c%s%c", filename, '\0', mode, '\0');
713
0
    curlx_free(filename);
714
715
    /* optional addition of TFTP options */
716
0
    if(!data->set.tftp_no_options) {
717
0
      char buf[64];
718
      /* add tsize option */
719
0
      curl_msnprintf(buf, sizeof(buf), "%" FMT_OFF_T,
720
0
                     data->state.upload && (data->state.infilesize != -1) ?
721
0
                     data->state.infilesize : 0);
722
723
0
      result = tftp_option_add(state, &sbytes, sbytes, TFTP_OPTION_TSIZE);
724
0
      if(result == CURLE_OK)
725
0
        result = tftp_option_add(state, &sbytes, sbytes, buf);
726
727
      /* add blksize option */
728
0
      curl_msnprintf(buf, sizeof(buf), "%u", state->requested_blksize);
729
0
      if(result == CURLE_OK)
730
0
        result = tftp_option_add(state, &sbytes, sbytes, TFTP_OPTION_BLKSIZE);
731
0
      if(result == CURLE_OK)
732
0
        result = tftp_option_add(state, &sbytes, sbytes, buf);
733
734
      /* add timeout option */
735
0
      curl_msnprintf(buf, sizeof(buf), "%d", state->retry_time);
736
0
      if(result == CURLE_OK)
737
0
        result = tftp_option_add(state, &sbytes, sbytes, TFTP_OPTION_INTERVAL);
738
0
      if(result == CURLE_OK)
739
0
        result = tftp_option_add(state, &sbytes, sbytes, buf);
740
741
0
      if(result != CURLE_OK) {
742
0
        failf(data, "TFTP buffer too small for options");
743
0
        return CURLE_TFTP_ILLEGAL;
744
0
      }
745
0
    }
746
747
    /* the typecast for the 3rd argument is mostly for systems that do
748
       not have a size_t argument, like older unixes that want an 'int' */
749
#ifdef __AMIGA__
750
#define CURL_SENDTO_ARG5(x) CURL_UNCONST(x)
751
#else
752
0
#define CURL_SENDTO_ARG5(x) (x)
753
0
#endif
754
0
    remote_addr = Curl_conn_get_remote_addr(data, FIRSTSOCKET);
755
0
    if(!remote_addr)
756
0
      return CURLE_FAILED_INIT;
757
758
0
    senddata = sendto(state->sockfd, (void *)state->spacket.data,
759
0
                      (SEND_TYPE_ARG3)sbytes, 0,
760
0
                      CURL_SENDTO_ARG5(&remote_addr->curl_sa_addr),
761
0
                      (curl_socklen_t)remote_addr->addrlen);
762
0
    if(senddata != (ssize_t)sbytes) {
763
0
      char buffer[STRERROR_LEN];
764
0
      failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
765
0
      return CURLE_SEND_ERROR;
766
0
    }
767
0
    break;
768
769
0
  case TFTP_EVENT_OACK:
770
0
    if(data->state.upload) {
771
0
      result = tftp_connect_for_tx(state, event);
772
0
    }
773
0
    else {
774
0
      result = tftp_connect_for_rx(state, event);
775
0
    }
776
0
    break;
777
778
0
  case TFTP_EVENT_ACK: /* Connected for transmit */
779
0
    if(!data->state.upload)
780
0
      return CURLE_TFTP_ILLEGAL;
781
0
    result = tftp_connect_for_tx(state, event);
782
0
    break;
783
784
0
  case TFTP_EVENT_DATA: /* Connected for receive */
785
0
    if(data->state.upload)
786
0
      return CURLE_TFTP_ILLEGAL;
787
0
    result = tftp_connect_for_rx(state, event);
788
0
    break;
789
790
0
  case TFTP_EVENT_ERROR:
791
0
    state->state = TFTP_STATE_FIN;
792
0
    break;
793
794
0
  default:
795
0
    failf(state->data, "tftp_send_first: internal error");
796
0
    return CURLE_TFTP_ILLEGAL;
797
0
  }
798
799
0
  return result;
800
0
}
801
802
/**********************************************************
803
 *
804
 * tftp_translate_code
805
 *
806
 * Translate internal error codes to CURL error codes
807
 *
808
 **********************************************************/
809
static CURLcode tftp_translate_code(tftp_error_t error)
810
0
{
811
0
  CURLcode result = CURLE_OK;
812
813
0
  if(error != TFTP_ERR_NONE) {
814
0
    switch(error) {
815
0
    case TFTP_ERR_NOTFOUND:
816
0
      result = CURLE_TFTP_NOTFOUND;
817
0
      break;
818
0
    case TFTP_ERR_PERM:
819
0
      result = CURLE_TFTP_PERM;
820
0
      break;
821
0
    case TFTP_ERR_DISKFULL:
822
0
      result = CURLE_REMOTE_DISK_FULL;
823
0
      break;
824
0
    case TFTP_ERR_UNDEF:
825
0
    case TFTP_ERR_ILLEGAL:
826
0
      result = CURLE_TFTP_ILLEGAL;
827
0
      break;
828
0
    case TFTP_ERR_UNKNOWNID:
829
0
      result = CURLE_TFTP_UNKNOWNID;
830
0
      break;
831
0
    case TFTP_ERR_EXISTS:
832
0
      result = CURLE_REMOTE_FILE_EXISTS;
833
0
      break;
834
0
    case TFTP_ERR_NOSUCHUSER:
835
0
      result = CURLE_TFTP_NOSUCHUSER;
836
0
      break;
837
0
    case TFTP_ERR_TIMEOUT:
838
0
      result = CURLE_OPERATION_TIMEDOUT;
839
0
      break;
840
0
    case TFTP_ERR_NORESPONSE:
841
0
      result = CURLE_COULDNT_CONNECT;
842
0
      break;
843
0
    default:
844
0
      result = CURLE_ABORTED_BY_CALLBACK;
845
0
      break;
846
0
    }
847
0
  }
848
0
  else
849
0
    result = CURLE_OK;
850
851
0
  return result;
852
0
}
853
854
/**********************************************************
855
 *
856
 * tftp_state_machine
857
 *
858
 * The tftp state machine event dispatcher
859
 *
860
 **********************************************************/
861
static CURLcode tftp_state_machine(struct tftp_conn *state,
862
                                   tftp_event_t event)
863
0
{
864
0
  CURLcode result = CURLE_OK;
865
0
  struct Curl_easy *data = state->data;
866
867
0
  switch(state->state) {
868
0
  case TFTP_STATE_START:
869
0
    DEBUGF(infof(data, "TFTP_STATE_START"));
870
0
    result = tftp_send_first(state, event);
871
0
    break;
872
0
  case TFTP_STATE_RX:
873
0
    DEBUGF(infof(data, "TFTP_STATE_RX"));
874
0
    result = tftp_rx(state, event);
875
0
    break;
876
0
  case TFTP_STATE_TX:
877
0
    DEBUGF(infof(data, "TFTP_STATE_TX"));
878
0
    result = tftp_tx(state, event);
879
0
    break;
880
0
  case TFTP_STATE_FIN:
881
0
    infof(data, "%s", "TFTP finished");
882
0
    break;
883
0
  default:
884
0
    DEBUGF(infof(data, "STATE: %d", (int)state->state));
885
0
    failf(data, "%s", "Internal state machine error");
886
0
    result = CURLE_TFTP_ILLEGAL;
887
0
    break;
888
0
  }
889
890
0
  return result;
891
0
}
892
893
static void tftp_conn_dtor(const void *key, size_t klen, void *entry)
894
0
{
895
0
  struct tftp_conn *state = entry;
896
0
  (void)key;
897
0
  (void)klen;
898
0
  curlx_safefree(state->rpacket.data);
899
0
  curlx_safefree(state->spacket.data);
900
0
  curlx_free(state);
901
0
}
902
903
/**********************************************************
904
 *
905
 * tftp_connect
906
 *
907
 * The connect callback
908
 *
909
 **********************************************************/
910
static CURLcode tftp_connect(struct Curl_easy *data, bool *done)
911
0
{
912
0
  struct tftp_conn *state;
913
0
  int blksize;
914
0
  int need_blksize;
915
0
  struct connectdata *conn = data->conn;
916
0
  const struct Curl_sockaddr_ex *remote_addr = NULL;
917
0
  CURLcode result;
918
919
0
  blksize = TFTP_BLKSIZE_DEFAULT;
920
921
0
  state = curlx_calloc(1, sizeof(*state));
922
0
  if(!state ||
923
0
     Curl_conn_meta_set(conn, CURL_META_TFTP_CONN, state, tftp_conn_dtor))
924
0
    return CURLE_OUT_OF_MEMORY;
925
926
  /* alloc pkt buffers based on specified blksize */
927
0
  if(data->set.tftp_blksize)
928
    /* range checked when set */
929
0
    blksize = (int)data->set.tftp_blksize;
930
931
0
  need_blksize = blksize;
932
  /* default size is the fallback when no OACK is received */
933
0
  if(need_blksize < TFTP_BLKSIZE_DEFAULT)
934
0
    need_blksize = TFTP_BLKSIZE_DEFAULT;
935
936
0
  if(!state->rpacket.data) {
937
0
    state->rpacket.data = curlx_calloc(1, need_blksize + 2 + 2);
938
939
0
    if(!state->rpacket.data)
940
0
      return CURLE_OUT_OF_MEMORY;
941
0
  }
942
943
0
  if(!state->spacket.data) {
944
0
    state->spacket.data = curlx_calloc(1, need_blksize + 2 + 2);
945
946
0
    if(!state->spacket.data)
947
0
      return CURLE_OUT_OF_MEMORY;
948
0
  }
949
950
  /* we do not keep TFTP connections up because there is none or little gain
951
   * for UDP */
952
0
  connclose(conn);
953
954
0
  state->data = data;
955
0
  state->sockfd = conn->sock[FIRSTSOCKET];
956
0
  state->state = TFTP_STATE_START;
957
0
  state->error = TFTP_ERR_NONE;
958
0
  state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
959
0
  state->requested_blksize = blksize;
960
961
0
  remote_addr = Curl_conn_get_remote_addr(data, FIRSTSOCKET);
962
0
  DEBUGASSERT(remote_addr);
963
0
  if(!remote_addr)
964
0
    return CURLE_FAILED_INIT;
965
966
0
  ((struct sockaddr *)&state->local_addr)->sa_family =
967
0
    (CURL_SA_FAMILY_T)remote_addr->family;
968
969
0
  result = tftp_set_timeouts(state);
970
0
  if(result)
971
0
    return result;
972
973
0
  if(!conn->bits.bound) {
974
    /* If not already bound, bind to any interface, random UDP port. If it is
975
     * reused or a custom local port was desired, this has already been done!
976
     *
977
     * We once used the size of the local_addr struct as the third argument
978
     * for bind() to better work with IPv6 or whatever size the struct could
979
     * have, but we learned that at least Tru64, AIX and IRIX *requires* the
980
     * size of that argument to match the exact size of a 'sockaddr_in' struct
981
     * when running IPv4-only.
982
     *
983
     * Therefore we use the size from the address we connected to, which we
984
     * assume uses the same IP version and thus hopefully this works for both
985
     * IPv4 and IPv6...
986
     */
987
0
    int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
988
0
                  (curl_socklen_t)remote_addr->addrlen);
989
0
    if(rc) {
990
0
      char buffer[STRERROR_LEN];
991
0
      failf(data, "bind() failed; %s",
992
0
            curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
993
0
      return CURLE_COULDNT_CONNECT;
994
0
    }
995
0
    conn->bits.bound = TRUE;
996
0
  }
997
998
0
  *done = TRUE;
999
1000
0
  return CURLE_OK;
1001
0
}
1002
1003
/**********************************************************
1004
 *
1005
 * tftp_done
1006
 *
1007
 * The done callback
1008
 *
1009
 **********************************************************/
1010
static CURLcode tftp_done(struct Curl_easy *data, CURLcode status,
1011
                          bool premature)
1012
0
{
1013
0
  CURLcode result = CURLE_OK;
1014
0
  struct connectdata *conn = data->conn;
1015
0
  struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
1016
1017
0
  (void)status;
1018
0
  (void)premature;
1019
1020
0
  if(Curl_pgrsDone(data, Curl_pgrs_now(data)))
1021
0
    return CURLE_ABORTED_BY_CALLBACK;
1022
1023
  /* If we have encountered an error */
1024
0
  if(state)
1025
0
    result = tftp_translate_code(state->error);
1026
1027
0
  return result;
1028
0
}
1029
1030
static CURLcode tftp_pollset(struct Curl_easy *data,
1031
                             struct easy_pollset *ps)
1032
0
{
1033
0
  return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]);
1034
0
}
1035
1036
/**********************************************************
1037
 *
1038
 * tftp_receive_packet
1039
 *
1040
 * Called once select fires and data is ready on the socket
1041
 *
1042
 **********************************************************/
1043
static CURLcode tftp_receive_packet(struct Curl_easy *data,
1044
                                    struct tftp_conn *state)
1045
0
{
1046
0
  CURLcode result = CURLE_OK;
1047
0
  struct Curl_sockaddr_storage remote_addr;
1048
0
  curl_socklen_t fromlen = sizeof(remote_addr);
1049
1050
  /* Receive the packet */
1051
0
  state->rbytes = (int)recvfrom(state->sockfd,
1052
0
                                (void *)state->rpacket.data,
1053
0
                                (RECV_TYPE_ARG3)state->blksize + 4,
1054
0
                                0,
1055
0
                                (struct sockaddr *)&remote_addr,
1056
0
                                &fromlen);
1057
0
  if((state->rbytes >= 0) && fromlen) {
1058
0
    if(state->remote_pinned) {
1059
      /* pinned, verify that it comes from the same address */
1060
0
      if((state->remote_addrlen != fromlen) ||
1061
0
         memcmp(&remote_addr, &state->remote_addr, fromlen)) {
1062
0
        failf(data, "Data received from another address");
1063
0
        return CURLE_RECV_ERROR;
1064
0
      }
1065
0
    }
1066
0
    else {
1067
      /* pin address on first use */
1068
0
      state->remote_pinned = TRUE;
1069
0
      state->remote_addrlen = fromlen;
1070
0
      memcpy(&state->remote_addr, &remote_addr, fromlen);
1071
0
    }
1072
0
  }
1073
1074
  /* Sanity check packet length */
1075
0
  if(state->rbytes < 4) {
1076
0
    failf(data, "Received too short packet");
1077
    /* Not a timeout, but how best to handle it? */
1078
0
    state->event = TFTP_EVENT_TIMEOUT;
1079
0
  }
1080
0
  else {
1081
    /* The event is given by the TFTP packet time */
1082
0
    unsigned short event = getrpacketevent(&state->rpacket);
1083
0
    state->event = (tftp_event_t)event;
1084
1085
0
    switch(state->event) {
1086
0
    case TFTP_EVENT_DATA:
1087
0
      if(data->state.upload)
1088
0
        return CURLE_TFTP_ILLEGAL;
1089
      /* Do not pass to the client empty or retransmitted packets */
1090
0
      if(state->rbytes > 4 &&
1091
0
         (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
1092
0
        result = Curl_client_write(data, CLIENTWRITE_BODY,
1093
0
                                   (const char *)state->rpacket.data + 4,
1094
0
                                   state->rbytes - 4);
1095
0
        if(result) {
1096
0
          tftp_state_machine(state, TFTP_EVENT_ERROR);
1097
0
          return result;
1098
0
        }
1099
0
      }
1100
0
      break;
1101
0
    case TFTP_EVENT_ERROR: {
1102
0
      unsigned short error = getrpacketblock(&state->rpacket);
1103
0
      const char *str = (const char *)state->rpacket.data + 4;
1104
0
      size_t strn = state->rbytes - 4;
1105
0
      state->error = (tftp_error_t)error;
1106
0
      if(tftp_strnlen(str, strn) < strn)
1107
0
        infof(data, "TFTP error: %s", str);
1108
0
      break;
1109
0
    }
1110
0
    case TFTP_EVENT_ACK:
1111
0
      break;
1112
0
    case TFTP_EVENT_OACK:
1113
0
      result = tftp_parse_option_ack(state,
1114
0
                                     (const char *)state->rpacket.data + 2,
1115
0
                                     state->rbytes-2);
1116
0
      if(result)
1117
0
        return result;
1118
0
      break;
1119
0
    case TFTP_EVENT_RRQ:
1120
0
    case TFTP_EVENT_WRQ:
1121
0
    default:
1122
0
      failf(data, "%s", "Internal error: Unexpected packet");
1123
0
      break;
1124
0
    }
1125
1126
    /* Update the progress meter */
1127
0
    result = Curl_pgrsUpdate(data);
1128
0
    if(result) {
1129
0
      tftp_state_machine(state, TFTP_EVENT_ERROR);
1130
0
      return result;
1131
0
    }
1132
0
  }
1133
0
  return result;
1134
0
}
1135
1136
/**********************************************************
1137
 *
1138
 * tftp_state_timeout
1139
 *
1140
 * Check if timeouts have been reached
1141
 *
1142
 **********************************************************/
1143
static timediff_t tftp_state_timeout(struct tftp_conn *state,
1144
                                     tftp_event_t *event)
1145
0
{
1146
0
  time_t current;
1147
0
  timediff_t timeout_ms;
1148
1149
0
  if(event)
1150
0
    *event = TFTP_EVENT_NONE;
1151
1152
0
  timeout_ms = Curl_timeleft_ms(state->data);
1153
0
  if(timeout_ms < 0) {
1154
0
    state->error = TFTP_ERR_TIMEOUT;
1155
0
    state->state = TFTP_STATE_FIN;
1156
0
    return timeout_ms;
1157
0
  }
1158
0
  current = time(NULL);
1159
0
  if(current > state->rx_time + state->retry_time) {
1160
0
    if(event)
1161
0
      *event = TFTP_EVENT_TIMEOUT;
1162
0
    state->rx_time = time(NULL); /* update even though we received nothing */
1163
0
  }
1164
1165
0
  return timeout_ms;
1166
0
}
1167
1168
/**********************************************************
1169
 *
1170
 * tftp_multi_statemach
1171
 *
1172
 * Handle single RX socket event and return
1173
 *
1174
 **********************************************************/
1175
static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done)
1176
0
{
1177
0
  tftp_event_t event;
1178
0
  CURLcode result = CURLE_OK;
1179
0
  struct connectdata *conn = data->conn;
1180
0
  struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
1181
0
  timediff_t timeout_ms;
1182
1183
0
  *done = FALSE;
1184
0
  if(!state)
1185
0
    return CURLE_FAILED_INIT;
1186
1187
0
  timeout_ms = tftp_state_timeout(state, &event);
1188
0
  if(timeout_ms < 0) {
1189
0
    failf(data, "TFTP response timeout");
1190
0
    return CURLE_OPERATION_TIMEDOUT;
1191
0
  }
1192
0
  if(event != TFTP_EVENT_NONE) {
1193
0
    result = tftp_state_machine(state, event);
1194
0
    if(result)
1195
0
      return result;
1196
0
    *done = (state->state == TFTP_STATE_FIN);
1197
0
    if(*done)
1198
      /* Tell curl we are done */
1199
0
      Curl_xfer_setup_nop(data);
1200
0
  }
1201
0
  else {
1202
    /* no timeouts to handle, check our socket */
1203
0
    int rc = SOCKET_READABLE(state->sockfd, 0);
1204
1205
0
    if(rc == -1) {
1206
      /* bail out */
1207
0
      int sockerr = SOCKERRNO;
1208
0
      char buffer[STRERROR_LEN];
1209
0
      failf(data, "%s", curlx_strerror(sockerr, buffer, sizeof(buffer)));
1210
0
      state->event = TFTP_EVENT_ERROR;
1211
0
    }
1212
0
    else if(rc) {
1213
0
      result = tftp_receive_packet(data, state);
1214
0
      if(result)
1215
0
        return result;
1216
0
      result = tftp_state_machine(state, state->event);
1217
0
      if(result)
1218
0
        return result;
1219
0
      *done = (state->state == TFTP_STATE_FIN);
1220
0
      if(*done)
1221
        /* Tell curl we are done */
1222
0
        Curl_xfer_setup_nop(data);
1223
0
    }
1224
    /* if rc == 0, then select() timed out */
1225
0
  }
1226
1227
0
  return result;
1228
0
}
1229
1230
/**********************************************************
1231
 *
1232
 * tftp_doing
1233
 *
1234
 * Called from multi.c while DOing
1235
 *
1236
 **********************************************************/
1237
static CURLcode tftp_doing(struct Curl_easy *data, bool *dophase_done)
1238
0
{
1239
0
  CURLcode result;
1240
0
  result = tftp_multi_statemach(data, dophase_done);
1241
1242
0
  if(*dophase_done) {
1243
0
    DEBUGF(infof(data, "DO phase is complete"));
1244
0
  }
1245
0
  else if(!result) {
1246
    /* The multi code does not have this logic for the DOING state so we
1247
       provide it for TFTP since it may do the entire transfer in this
1248
       state. */
1249
0
    result = Curl_pgrsCheck(data);
1250
0
  }
1251
0
  return result;
1252
0
}
1253
1254
/**********************************************************
1255
 *
1256
 * tftp_perform
1257
 *
1258
 * Entry point for transfer from tftp_do, starts state mach
1259
 *
1260
 **********************************************************/
1261
static CURLcode tftp_perform(struct Curl_easy *data, bool *dophase_done)
1262
0
{
1263
0
  CURLcode result = CURLE_OK;
1264
0
  struct connectdata *conn = data->conn;
1265
0
  struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
1266
1267
0
  *dophase_done = FALSE;
1268
0
  if(!state)
1269
0
    return CURLE_FAILED_INIT;
1270
1271
0
  result = tftp_state_machine(state, TFTP_EVENT_INIT);
1272
1273
0
  if((state->state == TFTP_STATE_FIN) || result)
1274
0
    return result;
1275
1276
0
  result = tftp_multi_statemach(data, dophase_done);
1277
1278
0
  if(*dophase_done)
1279
0
    DEBUGF(infof(data, "DO phase is complete"));
1280
1281
0
  return result;
1282
0
}
1283
1284
/**********************************************************
1285
 *
1286
 * tftp_do
1287
 *
1288
 * The do callback
1289
 *
1290
 * This callback initiates the TFTP transfer
1291
 *
1292
 **********************************************************/
1293
1294
static CURLcode tftp_do(struct Curl_easy *data, bool *done)
1295
0
{
1296
0
  struct connectdata *conn = data->conn;
1297
0
  struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
1298
0
  CURLcode result;
1299
1300
0
  *done = FALSE;
1301
1302
0
  if(!state) {
1303
0
    result = tftp_connect(data, done);
1304
0
    if(result)
1305
0
      return result;
1306
1307
0
    state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
1308
0
    if(!state)
1309
0
      return CURLE_TFTP_ILLEGAL;
1310
0
  }
1311
1312
0
  result = tftp_perform(data, done);
1313
1314
  /* If tftp_perform() returned an error, use that for return code. If it
1315
     was OK, see if tftp_translate_code() has an error. */
1316
0
  if(!result)
1317
    /* If we have encountered an internal tftp error, translate it. */
1318
0
    result = tftp_translate_code(state->error);
1319
1320
0
  return result;
1321
0
}
1322
1323
static CURLcode tftp_setup_connection(struct Curl_easy *data,
1324
                                      struct connectdata *conn)
1325
0
{
1326
0
  char *path = data->state.up.path;
1327
0
  size_t len = strlen(path);
1328
1329
0
  conn->transport_wanted = TRNSPRT_UDP;
1330
1331
  /* TFTP URLs support a trailing ";mode=netascii" or ";mode=octet" */
1332
0
  if((len >= 14) && !memcmp(&path[len - 14], ";mode=netascii", 14)) {
1333
0
    data->state.prefer_ascii = TRUE;
1334
0
    path[len - 14] = 0; /* cut it there */
1335
0
  }
1336
0
  else if((len >= 11) && !memcmp(&path[len - 11], ";mode=octet", 11)) {
1337
0
    data->state.prefer_ascii = FALSE;
1338
0
    path[len - 11] = 0; /* cut it there */
1339
0
  }
1340
1341
0
  return CURLE_OK;
1342
0
}
1343
1344
/*
1345
 * TFTP protocol handler.
1346
 */
1347
const struct Curl_protocol Curl_protocol_tftp = {
1348
  tftp_setup_connection,                /* setup_connection */
1349
  tftp_do,                              /* do_it */
1350
  tftp_done,                            /* done */
1351
  ZERO_NULL,                            /* do_more */
1352
  tftp_connect,                         /* connect_it */
1353
  tftp_multi_statemach,                 /* connecting */
1354
  tftp_doing,                           /* doing */
1355
  tftp_pollset,                         /* proto_pollset */
1356
  tftp_pollset,                         /* doing_pollset */
1357
  ZERO_NULL,                            /* domore_pollset */
1358
  ZERO_NULL,                            /* perform_pollset */
1359
  ZERO_NULL,                            /* disconnect */
1360
  ZERO_NULL,                            /* write_resp */
1361
  ZERO_NULL,                            /* write_resp_hd */
1362
  ZERO_NULL,                            /* connection_is_dead */
1363
  ZERO_NULL,                            /* attach connection */
1364
  ZERO_NULL,                            /* follow */
1365
};
1366
1367
#endif