Lines Matching refs:state
122 tftp_state_t state; member
148 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event);
149 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event);
198 static CURLcode tftp_set_timeouts(tftp_state_data_t *state) in tftp_set_timeouts() argument
202 bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE; in tftp_set_timeouts()
204 time(&state->start_time); in tftp_set_timeouts()
207 timeout_ms = Curl_timeleft(state->conn->data, NULL, start); in tftp_set_timeouts()
211 failf(state->conn->data, "Connection time-out"); in tftp_set_timeouts()
218 state->max_time = state->start_time+maxtime; in tftp_set_timeouts()
224 state->retry_max = (int)timeout/5; in tftp_set_timeouts()
226 if(state->retry_max < 1) in tftp_set_timeouts()
228 state->retry_max = 1; in tftp_set_timeouts()
231 state->retry_time = (int)timeout/state->retry_max; in tftp_set_timeouts()
232 if(state->retry_time<1) in tftp_set_timeouts()
233 state->retry_time=1; in tftp_set_timeouts()
242 state->max_time = state->start_time+maxtime; in tftp_set_timeouts()
248 state->retry_max = (int)timeout/5; in tftp_set_timeouts()
251 if(state->retry_max<3) in tftp_set_timeouts()
252 state->retry_max=3; in tftp_set_timeouts()
254 if(state->retry_max>50) in tftp_set_timeouts()
255 state->retry_max=50; in tftp_set_timeouts()
258 state->retry_time = (int)(timeout/state->retry_max); in tftp_set_timeouts()
259 if(state->retry_time<1) in tftp_set_timeouts()
260 state->retry_time=1; in tftp_set_timeouts()
262 infof(state->conn->data, in tftp_set_timeouts()
264 (int)state->state, (long)(state->max_time-state->start_time), in tftp_set_timeouts()
265 state->retry_time, state->retry_max); in tftp_set_timeouts()
268 time(&state->rx_time); in tftp_set_timeouts()
332 static CURLcode tftp_parse_option_ack(tftp_state_data_t *state, in tftp_parse_option_ack() argument
336 struct SessionHandle *data = state->conn->data; in tftp_parse_option_ack()
339 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_parse_option_ack()
371 else if(blksize > state->requested_blksize) { in tftp_parse_option_ack()
380 state->blksize = (int)blksize; in tftp_parse_option_ack()
382 state->blksize, "requested", state->requested_blksize); in tftp_parse_option_ack()
405 static size_t tftp_option_add(tftp_state_data_t *state, size_t csize, in tftp_option_add() argument
408 if(( strlen(option) + csize + 1 ) > (size_t)state->blksize) in tftp_option_add()
414 static CURLcode tftp_connect_for_tx(tftp_state_data_t *state, in tftp_connect_for_tx() argument
419 struct SessionHandle *data = state->conn->data; in tftp_connect_for_tx()
423 state->state = TFTP_STATE_TX; in tftp_connect_for_tx()
424 result = tftp_set_timeouts(state); in tftp_connect_for_tx()
427 return tftp_tx(state, event); in tftp_connect_for_tx()
430 static CURLcode tftp_connect_for_rx(tftp_state_data_t *state, in tftp_connect_for_rx() argument
435 struct SessionHandle *data = state->conn->data; in tftp_connect_for_rx()
439 state->state = TFTP_STATE_RX; in tftp_connect_for_rx()
440 result = tftp_set_timeouts(state); in tftp_connect_for_rx()
443 return tftp_rx(state, event); in tftp_connect_for_rx()
446 static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) in tftp_send_first() argument
453 struct SessionHandle *data = state->conn->data; in tftp_send_first()
465 state->retries++; in tftp_send_first()
466 if(state->retries>state->retry_max) { in tftp_send_first()
467 state->error = TFTP_ERR_NORESPONSE; in tftp_send_first()
468 state->state = TFTP_STATE_FIN; in tftp_send_first()
474 setpacketevent(&state->spacket, TFTP_EVENT_WRQ); in tftp_send_first()
475 state->conn->data->req.upload_fromhere = in tftp_send_first()
476 (char *)state->spacket.data+4; in tftp_send_first()
477 if(data->state.infilesize != -1) in tftp_send_first()
478 Curl_pgrsSetUploadSize(data, data->state.infilesize); in tftp_send_first()
482 setpacketevent(&state->spacket, TFTP_EVENT_RRQ); in tftp_send_first()
487 filename = curl_easy_unescape(data, &state->conn->data->state.path[1], 0, in tftp_send_first()
492 snprintf((char *)state->spacket.data+2, in tftp_send_first()
493 state->blksize, in tftp_send_first()
498 if(data->set.upload && (data->state.infilesize != -1)) in tftp_send_first()
500 data->state.infilesize); in tftp_send_first()
504 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
505 (char *)state->spacket.data+sbytes, in tftp_send_first()
507 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
508 (char *)state->spacket.data+sbytes, buf); in tftp_send_first()
510 snprintf( buf, sizeof(buf), "%d", state->requested_blksize ); in tftp_send_first()
511 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
512 (char *)state->spacket.data+sbytes, in tftp_send_first()
514 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
515 (char *)state->spacket.data+sbytes, buf ); in tftp_send_first()
518 snprintf( buf, sizeof(buf), "%d", state->retry_time); in tftp_send_first()
519 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
520 (char *)state->spacket.data+sbytes, in tftp_send_first()
522 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
523 (char *)state->spacket.data+sbytes, buf ); in tftp_send_first()
527 senddata = sendto(state->sockfd, (void *)state->spacket.data, in tftp_send_first()
529 state->conn->ip_addr->ai_addr, in tftp_send_first()
530 state->conn->ip_addr->ai_addrlen); in tftp_send_first()
532 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_send_first()
539 result = tftp_connect_for_tx(state, event); in tftp_send_first()
542 result = tftp_connect_for_rx(state, event); in tftp_send_first()
547 result = tftp_connect_for_tx(state, event); in tftp_send_first()
551 result = tftp_connect_for_rx(state, event); in tftp_send_first()
555 state->state = TFTP_STATE_FIN; in tftp_send_first()
559 failf(state->conn->data, "tftp_send_first: internal error"); in tftp_send_first()
577 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) in tftp_rx() argument
581 struct SessionHandle *data = state->conn->data; in tftp_rx()
587 rblock = getrpacketblock(&state->rpacket); in tftp_rx()
588 if(NEXT_BLOCKNUM(state->block) == rblock) { in tftp_rx()
590 state->retries = 0; in tftp_rx()
592 else if(state->block == rblock) { in tftp_rx()
601 rblock, NEXT_BLOCKNUM(state->block)); in tftp_rx()
606 state->block = (unsigned short)rblock; in tftp_rx()
607 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
608 setpacketblock(&state->spacket, state->block); in tftp_rx()
609 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
611 (struct sockaddr *)&state->remote_addr, in tftp_rx()
612 state->remote_addrlen); in tftp_rx()
614 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
619 if(state->rbytes < (ssize_t)state->blksize+4) { in tftp_rx()
620 state->state = TFTP_STATE_FIN; in tftp_rx()
623 state->state = TFTP_STATE_RX; in tftp_rx()
625 time(&state->rx_time); in tftp_rx()
630 state->block = 0; in tftp_rx()
631 state->retries = 0; in tftp_rx()
632 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
633 setpacketblock(&state->spacket, state->block); in tftp_rx()
634 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
636 (struct sockaddr *)&state->remote_addr, in tftp_rx()
637 state->remote_addrlen); in tftp_rx()
639 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
644 state->state = TFTP_STATE_RX; in tftp_rx()
645 time(&state->rx_time); in tftp_rx()
650 state->retries++; in tftp_rx()
653 NEXT_BLOCKNUM(state->block), state->retries); in tftp_rx()
654 if(state->retries > state->retry_max) { in tftp_rx()
655 state->error = TFTP_ERR_TIMEOUT; in tftp_rx()
656 state->state = TFTP_STATE_FIN; in tftp_rx()
660 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
662 (struct sockaddr *)&state->remote_addr, in tftp_rx()
663 state->remote_addrlen); in tftp_rx()
665 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
672 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_rx()
673 setpacketblock(&state->spacket, state->block); in tftp_rx()
674 (void)sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
676 (struct sockaddr *)&state->remote_addr, in tftp_rx()
677 state->remote_addrlen); in tftp_rx()
680 state->state = TFTP_STATE_FIN; in tftp_rx()
698 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) in tftp_tx() argument
700 struct SessionHandle *data = state->conn->data; in tftp_tx()
712 rblock = getrpacketblock(&state->rpacket); in tftp_tx()
714 if(rblock != state->block && in tftp_tx()
720 !(state->block == 0 && rblock == 65535)) { in tftp_tx()
723 rblock, state->block); in tftp_tx()
724 state->retries++; in tftp_tx()
726 if(state->retries>state->retry_max) { in tftp_tx()
728 state->block); in tftp_tx()
733 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
734 4+state->sbytes, SEND_4TH_ARG, in tftp_tx()
735 (struct sockaddr *)&state->remote_addr, in tftp_tx()
736 state->remote_addrlen); in tftp_tx()
739 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
748 time(&state->rx_time); in tftp_tx()
749 state->block++; in tftp_tx()
752 state->block = 1; /* first data block is 1 when using OACK */ in tftp_tx()
754 state->retries = 0; in tftp_tx()
755 setpacketevent(&state->spacket, TFTP_EVENT_DATA); in tftp_tx()
756 setpacketblock(&state->spacket, state->block); in tftp_tx()
757 if(state->block > 1 && state->sbytes < (int)state->blksize) { in tftp_tx()
758 state->state = TFTP_STATE_FIN; in tftp_tx()
762 result = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes); in tftp_tx()
766 sbytes = sendto(state->sockfd, (void *) state->spacket.data, in tftp_tx()
767 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
768 (struct sockaddr *)&state->remote_addr, in tftp_tx()
769 state->remote_addrlen); in tftp_tx()
772 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
776 k->writebytecount += state->sbytes; in tftp_tx()
782 state->retries++; in tftp_tx()
784 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries); in tftp_tx()
786 if(state->retries > state->retry_max) { in tftp_tx()
787 state->error = TFTP_ERR_TIMEOUT; in tftp_tx()
788 state->state = TFTP_STATE_FIN; in tftp_tx()
792 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
793 4+state->sbytes, SEND_4TH_ARG, in tftp_tx()
794 (struct sockaddr *)&state->remote_addr, in tftp_tx()
795 state->remote_addrlen); in tftp_tx()
798 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
807 state->state = TFTP_STATE_FIN; in tftp_tx()
808 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_tx()
809 setpacketblock(&state->spacket, state->block); in tftp_tx()
810 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, in tftp_tx()
811 (struct sockaddr *)&state->remote_addr, in tftp_tx()
812 state->remote_addrlen); in tftp_tx()
815 state->state = TFTP_STATE_FIN; in tftp_tx()
885 static CURLcode tftp_state_machine(tftp_state_data_t *state, in tftp_state_machine() argument
889 struct SessionHandle *data = state->conn->data; in tftp_state_machine()
891 switch(state->state) { in tftp_state_machine()
894 result = tftp_send_first(state, event); in tftp_state_machine()
898 result = tftp_rx(state, event); in tftp_state_machine()
902 result = tftp_tx(state, event); in tftp_state_machine()
908 DEBUGF(infof(data, "STATE: %d\n", state->state)); in tftp_state_machine()
926 tftp_state_data_t *state = conn->proto.tftpc; in tftp_disconnect() local
930 if(state) { in tftp_disconnect()
931 Curl_safefree(state->rpacket.data); in tftp_disconnect()
932 Curl_safefree(state->spacket.data); in tftp_disconnect()
933 free(state); in tftp_disconnect()
948 tftp_state_data_t *state; in tftp_connect() local
953 state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t)); in tftp_connect()
954 if(!state) in tftp_connect()
964 if(!state->rpacket.data) { in tftp_connect()
965 state->rpacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
967 if(!state->rpacket.data) in tftp_connect()
971 if(!state->spacket.data) { in tftp_connect()
972 state->spacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
974 if(!state->spacket.data) in tftp_connect()
982 state->conn = conn; in tftp_connect()
983 state->sockfd = state->conn->sock[FIRSTSOCKET]; in tftp_connect()
984 state->state = TFTP_STATE_START; in tftp_connect()
985 state->error = TFTP_ERR_NONE; in tftp_connect()
986 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_connect()
987 state->requested_blksize = blksize; in tftp_connect()
989 ((struct sockaddr *)&state->local_addr)->sa_family = in tftp_connect()
992 tftp_set_timeouts(state); in tftp_connect()
1008 rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, in tftp_connect()
1036 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_done() local
1045 if(state) in tftp_done()
1046 result = tftp_translate_code(state->error); in tftp_done()
1082 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_receive_packet() local
1087 state->rbytes = (int)recvfrom(state->sockfd, in tftp_receive_packet()
1088 (void *)state->rpacket.data, in tftp_receive_packet()
1089 state->blksize+4, in tftp_receive_packet()
1093 if(state->remote_addrlen==0) { in tftp_receive_packet()
1094 memcpy(&state->remote_addr, &fromaddr, fromlen); in tftp_receive_packet()
1095 state->remote_addrlen = fromlen; in tftp_receive_packet()
1099 if(state->rbytes < 4) { in tftp_receive_packet()
1102 state->event = TFTP_EVENT_TIMEOUT; in tftp_receive_packet()
1106 state->event = (tftp_event_t)getrpacketevent(&state->rpacket); in tftp_receive_packet()
1108 switch(state->event) { in tftp_receive_packet()
1111 if(state->rbytes > 4 && in tftp_receive_packet()
1112 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { in tftp_receive_packet()
1114 (char *)state->rpacket.data+4, in tftp_receive_packet()
1115 state->rbytes-4); in tftp_receive_packet()
1117 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1120 k->bytecount += state->rbytes-4; in tftp_receive_packet()
1125 state->error = (tftp_error_t)getrpacketblock(&state->rpacket); in tftp_receive_packet()
1126 infof(data, "%s\n", (const char *)state->rpacket.data+4); in tftp_receive_packet()
1131 result = tftp_parse_option_ack(state, in tftp_receive_packet()
1132 (const char *)state->rpacket.data+2, in tftp_receive_packet()
1133 state->rbytes-2); in tftp_receive_packet()
1146 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1163 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_state_timeout() local
1169 if(current > state->max_time) { in tftp_state_timeout()
1171 (long)current, (long)state->max_time)); in tftp_state_timeout()
1172 state->error = TFTP_ERR_TIMEOUT; in tftp_state_timeout()
1173 state->state = TFTP_STATE_FIN; in tftp_state_timeout()
1176 else if(current > state->rx_time+state->retry_time) { in tftp_state_timeout()
1179 time(&state->rx_time); /* update even though we received nothing */ in tftp_state_timeout()
1185 return (long)(state->max_time - current); in tftp_state_timeout()
1201 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_multi_statemach() local
1211 result = tftp_state_machine(state, event); in tftp_multi_statemach()
1214 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1221 rc = Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD, 0); in tftp_multi_statemach()
1227 state->event = TFTP_EVENT_ERROR; in tftp_multi_statemach()
1233 result = tftp_state_machine(state, state->event); in tftp_multi_statemach()
1236 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1284 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_perform() local
1288 result = tftp_state_machine(state, TFTP_EVENT_INIT); in tftp_perform()
1290 if((state->state == TFTP_STATE_FIN) || result) in tftp_perform()
1314 tftp_state_data_t *state; in tftp_do() local
1325 state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_do()
1326 if(!state) in tftp_do()
1335 result = tftp_translate_code(state->error); in tftp_do()
1350 type = strstr(data->state.path, ";mode="); in tftp_setup_connection()