diff options
Diffstat (limited to 'Utilities/cmcurl/lib/tftp.c')
-rw-r--r-- | Utilities/cmcurl/lib/tftp.c | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/Utilities/cmcurl/lib/tftp.c b/Utilities/cmcurl/lib/tftp.c index b2b3efe..4e599fd 100644 --- a/Utilities/cmcurl/lib/tftp.c +++ b/Utilities/cmcurl/lib/tftp.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. + * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -76,12 +76,12 @@ #define TFTP_OPTION_INTERVAL "timeout" typedef enum { - TFTP_MODE_NETASCII=0, + TFTP_MODE_NETASCII = 0, TFTP_MODE_OCTET } tftp_mode_t; typedef enum { - TFTP_STATE_START=0, + TFTP_STATE_START = 0, TFTP_STATE_RX, TFTP_STATE_TX, TFTP_STATE_FIN @@ -100,7 +100,7 @@ typedef enum { } tftp_event_t; typedef enum { - TFTP_ERR_UNDEF=0, + TFTP_ERR_UNDEF = 0, TFTP_ERR_NOTFOUND, TFTP_ERR_PERM, TFTP_ERR_DISKFULL, @@ -181,6 +181,7 @@ const struct Curl_handler Curl_handler_tftp = { ZERO_NULL, /* perform_getsock */ tftp_disconnect, /* disconnect */ ZERO_NULL, /* readwrite */ + ZERO_NULL, /* connection_check */ PORT_TFTP, /* defport */ CURLPROTO_TFTP, /* protocol */ PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */ @@ -216,7 +217,7 @@ static CURLcode tftp_set_timeouts(tftp_state_data_t *state) if(start) { maxtime = (time_t)(timeout_ms + 500) / 1000; - state->max_time = state->start_time+maxtime; + state->max_time = state->start_time + maxtime; /* Set per-block timeout to total */ timeout = maxtime; @@ -231,7 +232,7 @@ static CURLcode tftp_set_timeouts(tftp_state_data_t *state) /* Compute the re-start interval to suit the timeout */ state->retry_time = (int)timeout/state->retry_max; if(state->retry_time<1) - state->retry_time=1; + state->retry_time = 1; } else { @@ -240,7 +241,7 @@ static CURLcode tftp_set_timeouts(tftp_state_data_t *state) else maxtime = 3600; - state->max_time = state->start_time+maxtime; + state->max_time = state->start_time + maxtime; /* Set per-block timeout to total */ timeout = maxtime; @@ -250,15 +251,15 @@ static CURLcode tftp_set_timeouts(tftp_state_data_t *state) } /* But bound the total number */ if(state->retry_max<3) - state->retry_max=3; + state->retry_max = 3; if(state->retry_max>50) - state->retry_max=50; + state->retry_max = 50; /* Compute the re-ACK interval to suit the timeout */ state->retry_time = (int)(timeout/state->retry_max); if(state->retry_time<1) - state->retry_time=1; + state->retry_time = 1; infof(state->conn->data, "set timeouts for state %d; Total %ld, retry %d maxtry %d\n", @@ -320,7 +321,7 @@ static const char *tftp_option_get(const char *buf, size_t len, return NULL; *option = buf; - loc += Curl_strnlen(buf+loc, len-loc); + loc += Curl_strnlen(buf + loc, len-loc); loc++; /* NULL term */ if(loc > len) @@ -474,7 +475,7 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) /* If we are uploading, send an WRQ */ setpacketevent(&state->spacket, TFTP_EVENT_WRQ); state->conn->data->req.upload_fromhere = - (char *)state->spacket.data+4; + (char *)state->spacket.data + 4; if(data->state.infilesize != -1) Curl_pgrsSetUploadSize(data, data->state.infilesize); } @@ -490,7 +491,13 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) if(result) return result; - snprintf((char *)state->spacket.data+2, + if(strlen(filename) > (state->blksize - strlen(mode) - 4)) { + failf(data, "TFTP file name too long\n"); + free(filename); + return CURLE_TFTP_ILLEGAL; /* too long file name field */ + } + + snprintf((char *)state->spacket.data + 2, state->blksize, "%s%c%s%c", filename, '\0', mode, '\0'); sbytes = 4 + strlen(filename) + strlen(mode); @@ -505,25 +512,25 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) strcpy(buf, "0"); /* the destination is large enough */ sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, + (char *)state->spacket.data + sbytes, TFTP_OPTION_TSIZE); sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, buf); + (char *)state->spacket.data + sbytes, buf); /* add blksize option */ snprintf(buf, sizeof(buf), "%d", state->requested_blksize); sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, + (char *)state->spacket.data + sbytes, TFTP_OPTION_BLKSIZE); sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, buf); + (char *)state->spacket.data + sbytes, buf); /* add timeout option */ snprintf(buf, sizeof(buf), "%d", state->retry_time); sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, + (char *)state->spacket.data + sbytes, TFTP_OPTION_INTERVAL); sbytes += tftp_option_add(state, sbytes, - (char *)state->spacket.data+sbytes, buf); + (char *)state->spacket.data + sbytes, buf); } /* the typecase for the 3rd argument is mostly for systems that do @@ -569,7 +576,7 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit boundary */ -#define NEXT_BLOCKNUM(x) (((x)+1)&0xffff) +#define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff) /********************************************************** * @@ -620,7 +627,7 @@ static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) } /* Check if completed (That is, a less than full packet is received) */ - if(state->rbytes < (ssize_t)state->blksize+4) { + if(state->rbytes < (ssize_t)state->blksize + 4) { state->state = TFTP_STATE_FIN; } else { @@ -736,7 +743,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) else { /* Re-send the data packet */ sbytes = sendto(state->sockfd, (void *)state->spacket.data, - 4+state->sbytes, SEND_4TH_ARG, + 4 + state->sbytes, SEND_4TH_ARG, (struct sockaddr *)&state->remote_addr, state->remote_addrlen); /* Check all sbytes were sent */ @@ -769,7 +776,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) * data block. * */ state->sbytes = 0; - state->conn->data->req.upload_fromhere = (char *)state->spacket.data+4; + state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4; do { result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes, &cb); @@ -806,7 +813,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) else { /* Re-send the data packet */ sbytes = sendto(state->sockfd, (void *)state->spacket.data, - 4+state->sbytes, SEND_4TH_ARG, + 4 + state->sbytes, SEND_4TH_ARG, (struct sockaddr *)&state->remote_addr, state->remote_addrlen); /* Check all sbytes were sent */ @@ -1102,11 +1109,11 @@ static CURLcode tftp_receive_packet(struct connectdata *conn) fromlen = sizeof(fromaddr); state->rbytes = (int)recvfrom(state->sockfd, (void *)state->rpacket.data, - state->blksize+4, + state->blksize + 4, 0, (struct sockaddr *)&fromaddr, &fromlen); - if(state->remote_addrlen==0) { + if(state->remote_addrlen == 0) { memcpy(&state->remote_addr, &fromaddr, fromlen); state->remote_addrlen = fromlen; } @@ -1128,7 +1135,7 @@ static CURLcode tftp_receive_packet(struct connectdata *conn) if(state->rbytes > 4 && (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { result = Curl_client_write(conn, CLIENTWRITE_BODY, - (char *)state->rpacket.data+4, + (char *)state->rpacket.data + 4, state->rbytes-4); if(result) { tftp_state_machine(state, TFTP_EVENT_ERROR); @@ -1142,14 +1149,14 @@ static CURLcode tftp_receive_packet(struct connectdata *conn) { unsigned short error = getrpacketblock(&state->rpacket); state->error = (tftp_error_t)error; - infof(data, "%s\n", (const char *)state->rpacket.data+4); + infof(data, "%s\n", (const char *)state->rpacket.data + 4); break; } case TFTP_EVENT_ACK: break; case TFTP_EVENT_OACK: result = tftp_parse_option_ack(state, - (const char *)state->rpacket.data+2, + (const char *)state->rpacket.data + 2, state->rbytes-2); if(result) return result; @@ -1193,7 +1200,7 @@ static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event) state->state = TFTP_STATE_FIN; return 0; } - if(current > state->rx_time+state->retry_time) { + if(current > state->rx_time + state->retry_time) { if(event) *event = TFTP_EVENT_TIMEOUT; time(&state->rx_time); /* update even though we received nothing */ @@ -1344,7 +1351,7 @@ static CURLcode tftp_do(struct connectdata *conn, bool *done) state = (tftp_state_data_t *)conn->proto.tftpc; if(!state) - return CURLE_BAD_CALLING_ORDER; + return CURLE_TFTP_ILLEGAL; result = tftp_perform(conn, done); |