Lines Matching refs:state
123 tftp_state_t state; member
149 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event);
150 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event);
200 static CURLcode tftp_set_timeouts(tftp_state_data_t *state) in tftp_set_timeouts() argument
204 bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE; in tftp_set_timeouts()
206 time(&state->start_time); in tftp_set_timeouts()
209 timeout_ms = Curl_timeleft(state->conn->data, NULL, start); in tftp_set_timeouts()
213 failf(state->conn->data, "Connection time-out"); in tftp_set_timeouts()
220 state->max_time = state->start_time + maxtime; in tftp_set_timeouts()
226 state->retry_max = (int)timeout/5; in tftp_set_timeouts()
228 if(state->retry_max < 1) in tftp_set_timeouts()
230 state->retry_max = 1; in tftp_set_timeouts()
233 state->retry_time = (int)timeout/state->retry_max; in tftp_set_timeouts()
234 if(state->retry_time<1) in tftp_set_timeouts()
235 state->retry_time = 1; in tftp_set_timeouts()
244 state->max_time = state->start_time + maxtime; in tftp_set_timeouts()
250 state->retry_max = (int)timeout/5; in tftp_set_timeouts()
253 if(state->retry_max<3) in tftp_set_timeouts()
254 state->retry_max = 3; in tftp_set_timeouts()
256 if(state->retry_max>50) in tftp_set_timeouts()
257 state->retry_max = 50; in tftp_set_timeouts()
260 state->retry_time = (int)(timeout/state->retry_max); in tftp_set_timeouts()
261 if(state->retry_time<1) in tftp_set_timeouts()
262 state->retry_time = 1; in tftp_set_timeouts()
264 infof(state->conn->data, in tftp_set_timeouts()
266 (int)state->state, (long)(state->max_time-state->start_time), in tftp_set_timeouts()
267 state->retry_time, state->retry_max); in tftp_set_timeouts()
270 time(&state->rx_time); in tftp_set_timeouts()
334 static CURLcode tftp_parse_option_ack(tftp_state_data_t *state, in tftp_parse_option_ack() argument
338 struct Curl_easy *data = state->conn->data; in tftp_parse_option_ack()
341 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_parse_option_ack()
373 else if(blksize > state->requested_blksize) { in tftp_parse_option_ack()
382 state->blksize = (int)blksize; in tftp_parse_option_ack()
384 state->blksize, "requested", state->requested_blksize); in tftp_parse_option_ack()
407 static size_t tftp_option_add(tftp_state_data_t *state, size_t csize, in tftp_option_add() argument
410 if(( strlen(option) + csize + 1) > (size_t)state->blksize) in tftp_option_add()
416 static CURLcode tftp_connect_for_tx(tftp_state_data_t *state, in tftp_connect_for_tx() argument
421 struct Curl_easy *data = state->conn->data; in tftp_connect_for_tx()
425 state->state = TFTP_STATE_TX; in tftp_connect_for_tx()
426 result = tftp_set_timeouts(state); in tftp_connect_for_tx()
429 return tftp_tx(state, event); in tftp_connect_for_tx()
432 static CURLcode tftp_connect_for_rx(tftp_state_data_t *state, in tftp_connect_for_rx() argument
437 struct Curl_easy *data = state->conn->data; in tftp_connect_for_rx()
441 state->state = TFTP_STATE_RX; in tftp_connect_for_rx()
442 result = tftp_set_timeouts(state); in tftp_connect_for_rx()
445 return tftp_rx(state, event); in tftp_connect_for_rx()
448 static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) in tftp_send_first() argument
455 struct Curl_easy *data = state->conn->data; in tftp_send_first()
467 state->retries++; in tftp_send_first()
468 if(state->retries>state->retry_max) { in tftp_send_first()
469 state->error = TFTP_ERR_NORESPONSE; in tftp_send_first()
470 state->state = TFTP_STATE_FIN; in tftp_send_first()
476 setpacketevent(&state->spacket, TFTP_EVENT_WRQ); in tftp_send_first()
477 state->conn->data->req.upload_fromhere = in tftp_send_first()
478 (char *)state->spacket.data + 4; in tftp_send_first()
479 if(data->state.infilesize != -1) in tftp_send_first()
480 Curl_pgrsSetUploadSize(data, data->state.infilesize); in tftp_send_first()
484 setpacketevent(&state->spacket, TFTP_EVENT_RRQ); in tftp_send_first()
489 result = Curl_urldecode(data, &state->conn->data->state.path[1], 0, in tftp_send_first()
494 if(strlen(filename) > (state->blksize - strlen(mode) - 4)) { in tftp_send_first()
500 snprintf((char *)state->spacket.data + 2, in tftp_send_first()
501 state->blksize, in tftp_send_first()
508 if(data->set.upload && (data->state.infilesize != -1)) in tftp_send_first()
510 data->state.infilesize); in tftp_send_first()
514 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
515 (char *)state->spacket.data + sbytes, in tftp_send_first()
517 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
518 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
520 snprintf(buf, sizeof(buf), "%d", state->requested_blksize); in tftp_send_first()
521 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
522 (char *)state->spacket.data + sbytes, in tftp_send_first()
524 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
525 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
528 snprintf(buf, sizeof(buf), "%d", state->retry_time); in tftp_send_first()
529 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
530 (char *)state->spacket.data + sbytes, in tftp_send_first()
532 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
533 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
538 senddata = sendto(state->sockfd, (void *)state->spacket.data, in tftp_send_first()
540 state->conn->ip_addr->ai_addr, in tftp_send_first()
541 state->conn->ip_addr->ai_addrlen); in tftp_send_first()
543 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_send_first()
550 result = tftp_connect_for_tx(state, event); in tftp_send_first()
553 result = tftp_connect_for_rx(state, event); in tftp_send_first()
558 result = tftp_connect_for_tx(state, event); in tftp_send_first()
562 result = tftp_connect_for_rx(state, event); in tftp_send_first()
566 state->state = TFTP_STATE_FIN; in tftp_send_first()
570 failf(state->conn->data, "tftp_send_first: internal error"); in tftp_send_first()
588 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) in tftp_rx() argument
592 struct Curl_easy *data = state->conn->data; in tftp_rx()
598 rblock = getrpacketblock(&state->rpacket); in tftp_rx()
599 if(NEXT_BLOCKNUM(state->block) == rblock) { in tftp_rx()
601 state->retries = 0; in tftp_rx()
603 else if(state->block == rblock) { in tftp_rx()
612 rblock, NEXT_BLOCKNUM(state->block)); in tftp_rx()
617 state->block = (unsigned short)rblock; in tftp_rx()
618 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
619 setpacketblock(&state->spacket, state->block); in tftp_rx()
620 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
622 (struct sockaddr *)&state->remote_addr, in tftp_rx()
623 state->remote_addrlen); in tftp_rx()
625 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
630 if(state->rbytes < (ssize_t)state->blksize + 4) { in tftp_rx()
631 state->state = TFTP_STATE_FIN; in tftp_rx()
634 state->state = TFTP_STATE_RX; in tftp_rx()
636 time(&state->rx_time); in tftp_rx()
641 state->block = 0; in tftp_rx()
642 state->retries = 0; in tftp_rx()
643 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
644 setpacketblock(&state->spacket, state->block); in tftp_rx()
645 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
647 (struct sockaddr *)&state->remote_addr, in tftp_rx()
648 state->remote_addrlen); in tftp_rx()
650 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
655 state->state = TFTP_STATE_RX; in tftp_rx()
656 time(&state->rx_time); in tftp_rx()
661 state->retries++; in tftp_rx()
664 NEXT_BLOCKNUM(state->block), state->retries); in tftp_rx()
665 if(state->retries > state->retry_max) { in tftp_rx()
666 state->error = TFTP_ERR_TIMEOUT; in tftp_rx()
667 state->state = TFTP_STATE_FIN; in tftp_rx()
671 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
673 (struct sockaddr *)&state->remote_addr, in tftp_rx()
674 state->remote_addrlen); in tftp_rx()
676 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
683 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_rx()
684 setpacketblock(&state->spacket, state->block); in tftp_rx()
685 (void)sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
687 (struct sockaddr *)&state->remote_addr, in tftp_rx()
688 state->remote_addrlen); in tftp_rx()
691 state->state = TFTP_STATE_FIN; in tftp_rx()
709 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) in tftp_tx() argument
711 struct Curl_easy *data = state->conn->data; in tftp_tx()
724 rblock = getrpacketblock(&state->rpacket); in tftp_tx()
726 if(rblock != state->block && in tftp_tx()
732 !(state->block == 0 && rblock == 65535)) { in tftp_tx()
735 rblock, state->block); in tftp_tx()
736 state->retries++; in tftp_tx()
738 if(state->retries>state->retry_max) { in tftp_tx()
740 state->block); in tftp_tx()
745 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
746 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
747 (struct sockaddr *)&state->remote_addr, in tftp_tx()
748 state->remote_addrlen); in tftp_tx()
751 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
760 time(&state->rx_time); in tftp_tx()
761 state->block++; in tftp_tx()
764 state->block = 1; /* first data block is 1 when using OACK */ in tftp_tx()
766 state->retries = 0; in tftp_tx()
767 setpacketevent(&state->spacket, TFTP_EVENT_DATA); in tftp_tx()
768 setpacketblock(&state->spacket, state->block); in tftp_tx()
769 if(state->block > 1 && state->sbytes < (int)state->blksize) { in tftp_tx()
770 state->state = TFTP_STATE_FIN; in tftp_tx()
778 state->sbytes = 0; in tftp_tx()
779 state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4; in tftp_tx()
781 result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes, in tftp_tx()
785 state->sbytes += cb; in tftp_tx()
786 state->conn->data->req.upload_fromhere += cb; in tftp_tx()
787 } while(state->sbytes < state->blksize && cb != 0); in tftp_tx()
789 sbytes = sendto(state->sockfd, (void *) state->spacket.data, in tftp_tx()
790 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
791 (struct sockaddr *)&state->remote_addr, in tftp_tx()
792 state->remote_addrlen); in tftp_tx()
795 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
799 k->writebytecount += state->sbytes; in tftp_tx()
805 state->retries++; in tftp_tx()
807 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries); in tftp_tx()
809 if(state->retries > state->retry_max) { in tftp_tx()
810 state->error = TFTP_ERR_TIMEOUT; in tftp_tx()
811 state->state = TFTP_STATE_FIN; in tftp_tx()
815 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
816 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
817 (struct sockaddr *)&state->remote_addr, in tftp_tx()
818 state->remote_addrlen); in tftp_tx()
821 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
830 state->state = TFTP_STATE_FIN; in tftp_tx()
831 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_tx()
832 setpacketblock(&state->spacket, state->block); in tftp_tx()
833 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, in tftp_tx()
834 (struct sockaddr *)&state->remote_addr, in tftp_tx()
835 state->remote_addrlen); in tftp_tx()
838 state->state = TFTP_STATE_FIN; in tftp_tx()
908 static CURLcode tftp_state_machine(tftp_state_data_t *state, in tftp_state_machine() argument
912 struct Curl_easy *data = state->conn->data; in tftp_state_machine()
914 switch(state->state) { in tftp_state_machine()
917 result = tftp_send_first(state, event); in tftp_state_machine()
921 result = tftp_rx(state, event); in tftp_state_machine()
925 result = tftp_tx(state, event); in tftp_state_machine()
931 DEBUGF(infof(data, "STATE: %d\n", state->state)); in tftp_state_machine()
949 tftp_state_data_t *state = conn->proto.tftpc; in tftp_disconnect() local
953 if(state) { in tftp_disconnect()
954 Curl_safefree(state->rpacket.data); in tftp_disconnect()
955 Curl_safefree(state->spacket.data); in tftp_disconnect()
956 free(state); in tftp_disconnect()
971 tftp_state_data_t *state; in tftp_connect() local
976 state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t)); in tftp_connect()
977 if(!state) in tftp_connect()
987 if(!state->rpacket.data) { in tftp_connect()
988 state->rpacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
990 if(!state->rpacket.data) in tftp_connect()
994 if(!state->spacket.data) { in tftp_connect()
995 state->spacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
997 if(!state->spacket.data) in tftp_connect()
1005 state->conn = conn; in tftp_connect()
1006 state->sockfd = state->conn->sock[FIRSTSOCKET]; in tftp_connect()
1007 state->state = TFTP_STATE_START; in tftp_connect()
1008 state->error = TFTP_ERR_NONE; in tftp_connect()
1009 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_connect()
1010 state->requested_blksize = blksize; in tftp_connect()
1012 ((struct sockaddr *)&state->local_addr)->sa_family = in tftp_connect()
1015 tftp_set_timeouts(state); in tftp_connect()
1031 rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, in tftp_connect()
1059 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_done() local
1068 if(state) in tftp_done()
1069 result = tftp_translate_code(state->error); in tftp_done()
1105 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_receive_packet() local
1110 state->rbytes = (int)recvfrom(state->sockfd, in tftp_receive_packet()
1111 (void *)state->rpacket.data, in tftp_receive_packet()
1112 state->blksize + 4, in tftp_receive_packet()
1116 if(state->remote_addrlen == 0) { in tftp_receive_packet()
1117 memcpy(&state->remote_addr, &fromaddr, fromlen); in tftp_receive_packet()
1118 state->remote_addrlen = fromlen; in tftp_receive_packet()
1122 if(state->rbytes < 4) { in tftp_receive_packet()
1125 state->event = TFTP_EVENT_TIMEOUT; in tftp_receive_packet()
1129 unsigned short event = getrpacketevent(&state->rpacket); in tftp_receive_packet()
1130 state->event = (tftp_event_t)event; in tftp_receive_packet()
1132 switch(state->event) { in tftp_receive_packet()
1135 if(state->rbytes > 4 && in tftp_receive_packet()
1136 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { in tftp_receive_packet()
1138 (char *)state->rpacket.data + 4, in tftp_receive_packet()
1139 state->rbytes-4); in tftp_receive_packet()
1141 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1144 k->bytecount += state->rbytes-4; in tftp_receive_packet()
1150 unsigned short error = getrpacketblock(&state->rpacket); in tftp_receive_packet()
1151 state->error = (tftp_error_t)error; in tftp_receive_packet()
1152 infof(data, "%s\n", (const char *)state->rpacket.data + 4); in tftp_receive_packet()
1158 result = tftp_parse_option_ack(state, in tftp_receive_packet()
1159 (const char *)state->rpacket.data + 2, in tftp_receive_packet()
1160 state->rbytes-2); in tftp_receive_packet()
1173 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1190 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_state_timeout() local
1196 if(current > state->max_time) { in tftp_state_timeout()
1198 (long)current, (long)state->max_time)); in tftp_state_timeout()
1199 state->error = TFTP_ERR_TIMEOUT; in tftp_state_timeout()
1200 state->state = TFTP_STATE_FIN; in tftp_state_timeout()
1203 if(current > state->rx_time + state->retry_time) { in tftp_state_timeout()
1206 time(&state->rx_time); /* update even though we received nothing */ in tftp_state_timeout()
1212 return (long)(state->max_time - current); in tftp_state_timeout()
1228 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_multi_statemach() local
1238 result = tftp_state_machine(state, event); in tftp_multi_statemach()
1241 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1248 rc = SOCKET_READABLE(state->sockfd, 0); in tftp_multi_statemach()
1254 state->event = TFTP_EVENT_ERROR; in tftp_multi_statemach()
1260 result = tftp_state_machine(state, state->event); in tftp_multi_statemach()
1263 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1311 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_perform() local
1315 result = tftp_state_machine(state, TFTP_EVENT_INIT); in tftp_perform()
1317 if((state->state == TFTP_STATE_FIN) || result) in tftp_perform()
1341 tftp_state_data_t *state; in tftp_do() local
1352 state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_do()
1353 if(!state) in tftp_do()
1362 result = tftp_translate_code(state->error); in tftp_do()
1377 type = strstr(data->state.path, ";mode="); in tftp_setup_connection()