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 Curl_easy *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 Curl_easy *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 Curl_easy *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 Curl_easy *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()
500 if(data->set.upload && (data->state.infilesize != -1)) in tftp_send_first()
502 data->state.infilesize); in tftp_send_first()
506 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
507 (char *)state->spacket.data+sbytes, in tftp_send_first()
509 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
510 (char *)state->spacket.data+sbytes, buf); in tftp_send_first()
512 snprintf(buf, sizeof(buf), "%d", state->requested_blksize); in tftp_send_first()
513 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
514 (char *)state->spacket.data+sbytes, in tftp_send_first()
516 sbytes += tftp_option_add(state, sbytes, in tftp_send_first()
517 (char *)state->spacket.data+sbytes, buf); in tftp_send_first()
520 snprintf(buf, sizeof(buf), "%d", state->retry_time); 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()
530 senddata = sendto(state->sockfd, (void *)state->spacket.data, in tftp_send_first()
532 state->conn->ip_addr->ai_addr, in tftp_send_first()
533 state->conn->ip_addr->ai_addrlen); in tftp_send_first()
535 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_send_first()
542 result = tftp_connect_for_tx(state, event); in tftp_send_first()
545 result = tftp_connect_for_rx(state, event); in tftp_send_first()
550 result = tftp_connect_for_tx(state, event); in tftp_send_first()
554 result = tftp_connect_for_rx(state, event); in tftp_send_first()
558 state->state = TFTP_STATE_FIN; in tftp_send_first()
562 failf(state->conn->data, "tftp_send_first: internal error"); in tftp_send_first()
580 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) in tftp_rx() argument
584 struct Curl_easy *data = state->conn->data; in tftp_rx()
590 rblock = getrpacketblock(&state->rpacket); in tftp_rx()
591 if(NEXT_BLOCKNUM(state->block) == rblock) { in tftp_rx()
593 state->retries = 0; in tftp_rx()
595 else if(state->block == rblock) { in tftp_rx()
604 rblock, NEXT_BLOCKNUM(state->block)); in tftp_rx()
609 state->block = (unsigned short)rblock; in tftp_rx()
610 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
611 setpacketblock(&state->spacket, state->block); in tftp_rx()
612 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
614 (struct sockaddr *)&state->remote_addr, in tftp_rx()
615 state->remote_addrlen); in tftp_rx()
617 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
622 if(state->rbytes < (ssize_t)state->blksize+4) { in tftp_rx()
623 state->state = TFTP_STATE_FIN; in tftp_rx()
626 state->state = TFTP_STATE_RX; in tftp_rx()
628 time(&state->rx_time); in tftp_rx()
633 state->block = 0; in tftp_rx()
634 state->retries = 0; in tftp_rx()
635 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
636 setpacketblock(&state->spacket, state->block); in tftp_rx()
637 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
639 (struct sockaddr *)&state->remote_addr, in tftp_rx()
640 state->remote_addrlen); in tftp_rx()
642 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
647 state->state = TFTP_STATE_RX; in tftp_rx()
648 time(&state->rx_time); in tftp_rx()
653 state->retries++; in tftp_rx()
656 NEXT_BLOCKNUM(state->block), state->retries); in tftp_rx()
657 if(state->retries > state->retry_max) { in tftp_rx()
658 state->error = TFTP_ERR_TIMEOUT; in tftp_rx()
659 state->state = TFTP_STATE_FIN; in tftp_rx()
663 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
665 (struct sockaddr *)&state->remote_addr, in tftp_rx()
666 state->remote_addrlen); in tftp_rx()
668 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_rx()
675 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_rx()
676 setpacketblock(&state->spacket, state->block); in tftp_rx()
677 (void)sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
679 (struct sockaddr *)&state->remote_addr, in tftp_rx()
680 state->remote_addrlen); in tftp_rx()
683 state->state = TFTP_STATE_FIN; in tftp_rx()
701 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) in tftp_tx() argument
703 struct Curl_easy *data = state->conn->data; in tftp_tx()
715 rblock = getrpacketblock(&state->rpacket); in tftp_tx()
717 if(rblock != state->block && in tftp_tx()
723 !(state->block == 0 && rblock == 65535)) { in tftp_tx()
726 rblock, state->block); in tftp_tx()
727 state->retries++; in tftp_tx()
729 if(state->retries>state->retry_max) { in tftp_tx()
731 state->block); in tftp_tx()
736 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
737 4+state->sbytes, SEND_4TH_ARG, in tftp_tx()
738 (struct sockaddr *)&state->remote_addr, in tftp_tx()
739 state->remote_addrlen); in tftp_tx()
742 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
751 time(&state->rx_time); in tftp_tx()
752 state->block++; in tftp_tx()
755 state->block = 1; /* first data block is 1 when using OACK */ in tftp_tx()
757 state->retries = 0; in tftp_tx()
758 setpacketevent(&state->spacket, TFTP_EVENT_DATA); in tftp_tx()
759 setpacketblock(&state->spacket, state->block); in tftp_tx()
760 if(state->block > 1 && state->sbytes < (int)state->blksize) { in tftp_tx()
761 state->state = TFTP_STATE_FIN; in tftp_tx()
765 result = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes); in tftp_tx()
769 sbytes = sendto(state->sockfd, (void *) state->spacket.data, in tftp_tx()
770 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
771 (struct sockaddr *)&state->remote_addr, in tftp_tx()
772 state->remote_addrlen); in tftp_tx()
775 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
779 k->writebytecount += state->sbytes; in tftp_tx()
785 state->retries++; in tftp_tx()
787 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries); in tftp_tx()
789 if(state->retries > state->retry_max) { in tftp_tx()
790 state->error = TFTP_ERR_TIMEOUT; in tftp_tx()
791 state->state = TFTP_STATE_FIN; in tftp_tx()
795 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
796 4+state->sbytes, SEND_4TH_ARG, in tftp_tx()
797 (struct sockaddr *)&state->remote_addr, in tftp_tx()
798 state->remote_addrlen); in tftp_tx()
801 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); in tftp_tx()
810 state->state = TFTP_STATE_FIN; in tftp_tx()
811 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_tx()
812 setpacketblock(&state->spacket, state->block); in tftp_tx()
813 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, in tftp_tx()
814 (struct sockaddr *)&state->remote_addr, in tftp_tx()
815 state->remote_addrlen); in tftp_tx()
818 state->state = TFTP_STATE_FIN; in tftp_tx()
888 static CURLcode tftp_state_machine(tftp_state_data_t *state, in tftp_state_machine() argument
892 struct Curl_easy *data = state->conn->data; in tftp_state_machine()
894 switch(state->state) { in tftp_state_machine()
897 result = tftp_send_first(state, event); in tftp_state_machine()
901 result = tftp_rx(state, event); in tftp_state_machine()
905 result = tftp_tx(state, event); in tftp_state_machine()
911 DEBUGF(infof(data, "STATE: %d\n", state->state)); in tftp_state_machine()
929 tftp_state_data_t *state = conn->proto.tftpc; in tftp_disconnect() local
933 if(state) { in tftp_disconnect()
934 Curl_safefree(state->rpacket.data); in tftp_disconnect()
935 Curl_safefree(state->spacket.data); in tftp_disconnect()
936 free(state); in tftp_disconnect()
951 tftp_state_data_t *state; in tftp_connect() local
956 state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t)); in tftp_connect()
957 if(!state) in tftp_connect()
967 if(!state->rpacket.data) { in tftp_connect()
968 state->rpacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
970 if(!state->rpacket.data) in tftp_connect()
974 if(!state->spacket.data) { in tftp_connect()
975 state->spacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
977 if(!state->spacket.data) in tftp_connect()
985 state->conn = conn; in tftp_connect()
986 state->sockfd = state->conn->sock[FIRSTSOCKET]; in tftp_connect()
987 state->state = TFTP_STATE_START; in tftp_connect()
988 state->error = TFTP_ERR_NONE; in tftp_connect()
989 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_connect()
990 state->requested_blksize = blksize; in tftp_connect()
992 ((struct sockaddr *)&state->local_addr)->sa_family = in tftp_connect()
995 tftp_set_timeouts(state); in tftp_connect()
1011 rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, in tftp_connect()
1039 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_done() local
1048 if(state) in tftp_done()
1049 result = tftp_translate_code(state->error); in tftp_done()
1085 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_receive_packet() local
1090 state->rbytes = (int)recvfrom(state->sockfd, in tftp_receive_packet()
1091 (void *)state->rpacket.data, in tftp_receive_packet()
1092 state->blksize+4, in tftp_receive_packet()
1096 if(state->remote_addrlen==0) { in tftp_receive_packet()
1097 memcpy(&state->remote_addr, &fromaddr, fromlen); in tftp_receive_packet()
1098 state->remote_addrlen = fromlen; in tftp_receive_packet()
1102 if(state->rbytes < 4) { in tftp_receive_packet()
1105 state->event = TFTP_EVENT_TIMEOUT; in tftp_receive_packet()
1109 state->event = (tftp_event_t)getrpacketevent(&state->rpacket); in tftp_receive_packet()
1111 switch(state->event) { in tftp_receive_packet()
1114 if(state->rbytes > 4 && in tftp_receive_packet()
1115 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { in tftp_receive_packet()
1117 (char *)state->rpacket.data+4, in tftp_receive_packet()
1118 state->rbytes-4); in tftp_receive_packet()
1120 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1123 k->bytecount += state->rbytes-4; in tftp_receive_packet()
1128 state->error = (tftp_error_t)getrpacketblock(&state->rpacket); in tftp_receive_packet()
1129 infof(data, "%s\n", (const char *)state->rpacket.data+4); in tftp_receive_packet()
1134 result = tftp_parse_option_ack(state, in tftp_receive_packet()
1135 (const char *)state->rpacket.data+2, in tftp_receive_packet()
1136 state->rbytes-2); in tftp_receive_packet()
1149 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1166 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_state_timeout() local
1172 if(current > state->max_time) { in tftp_state_timeout()
1174 (long)current, (long)state->max_time)); in tftp_state_timeout()
1175 state->error = TFTP_ERR_TIMEOUT; in tftp_state_timeout()
1176 state->state = TFTP_STATE_FIN; in tftp_state_timeout()
1179 else if(current > state->rx_time+state->retry_time) { in tftp_state_timeout()
1182 time(&state->rx_time); /* update even though we received nothing */ in tftp_state_timeout()
1188 return (long)(state->max_time - current); in tftp_state_timeout()
1204 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_multi_statemach() local
1214 result = tftp_state_machine(state, event); in tftp_multi_statemach()
1217 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1224 rc = Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD, 0); in tftp_multi_statemach()
1230 state->event = TFTP_EVENT_ERROR; in tftp_multi_statemach()
1236 result = tftp_state_machine(state, state->event); in tftp_multi_statemach()
1239 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1287 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_perform() local
1291 result = tftp_state_machine(state, TFTP_EVENT_INIT); in tftp_perform()
1293 if((state->state == TFTP_STATE_FIN) || result) in tftp_perform()
1317 tftp_state_data_t *state; in tftp_do() local
1328 state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_do()
1329 if(!state) in tftp_do()
1338 result = tftp_translate_code(state->error); in tftp_do()
1353 type = strstr(data->state.path, ";mode="); in tftp_setup_connection()