Lines Matching refs:state

124   tftp_state_t    state;  member
148 static CURLcode tftp_rx(struct tftp_state_data *state, tftp_event_t event);
149 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event);
204 static CURLcode tftp_set_timeouts(struct tftp_state_data *state) in tftp_set_timeouts() argument
208 bool start = (state->state == TFTP_STATE_START); in tftp_set_timeouts()
211 timeout_ms = Curl_timeleft(state->data, NULL, start); in tftp_set_timeouts()
215 failf(state->data, "Connection time-out"); in tftp_set_timeouts()
228 state->retry_max = (int)timeout/5; in tftp_set_timeouts()
231 if(state->retry_max < 3) in tftp_set_timeouts()
232 state->retry_max = 3; in tftp_set_timeouts()
234 if(state->retry_max > 50) in tftp_set_timeouts()
235 state->retry_max = 50; in tftp_set_timeouts()
238 state->retry_time = (int)(timeout/state->retry_max); in tftp_set_timeouts()
239 if(state->retry_time < 1) in tftp_set_timeouts()
240 state->retry_time = 1; in tftp_set_timeouts()
242 infof(state->data, in tftp_set_timeouts()
244 (int)state->state, timeout_ms, state->retry_time, state->retry_max); in tftp_set_timeouts()
247 state->rx_time = time(NULL); in tftp_set_timeouts()
311 static CURLcode tftp_parse_option_ack(struct tftp_state_data *state, in tftp_parse_option_ack() argument
315 struct Curl_easy *data = state->data; in tftp_parse_option_ack()
318 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_parse_option_ack()
350 else if(blksize > state->requested_blksize) { in tftp_parse_option_ack()
359 state->blksize = (int)blksize; in tftp_parse_option_ack()
361 state->blksize, "requested", state->requested_blksize); in tftp_parse_option_ack()
371 if(!data->state.upload) { in tftp_parse_option_ack()
384 static CURLcode tftp_option_add(struct tftp_state_data *state, size_t *csize, in tftp_option_add() argument
387 if(( strlen(option) + *csize + 1) > (size_t)state->blksize) in tftp_option_add()
394 static CURLcode tftp_connect_for_tx(struct tftp_state_data *state, in tftp_connect_for_tx() argument
399 struct Curl_easy *data = state->data; in tftp_connect_for_tx()
403 state->state = TFTP_STATE_TX; in tftp_connect_for_tx()
404 result = tftp_set_timeouts(state); in tftp_connect_for_tx()
407 return tftp_tx(state, event); in tftp_connect_for_tx()
410 static CURLcode tftp_connect_for_rx(struct tftp_state_data *state, in tftp_connect_for_rx() argument
415 struct Curl_easy *data = state->data; in tftp_connect_for_rx()
419 state->state = TFTP_STATE_RX; in tftp_connect_for_rx()
420 result = tftp_set_timeouts(state); in tftp_connect_for_rx()
423 return tftp_rx(state, event); in tftp_connect_for_rx()
426 static CURLcode tftp_send_first(struct tftp_state_data *state, in tftp_send_first() argument
433 struct Curl_easy *data = state->data; in tftp_send_first()
437 if(data->state.prefer_ascii) in tftp_send_first()
445 state->retries++; in tftp_send_first()
446 if(state->retries > state->retry_max) { in tftp_send_first()
447 state->error = TFTP_ERR_NORESPONSE; in tftp_send_first()
448 state->state = TFTP_STATE_FIN; in tftp_send_first()
452 if(data->state.upload) { in tftp_send_first()
454 setpacketevent(&state->spacket, TFTP_EVENT_WRQ); in tftp_send_first()
455 if(data->state.infilesize != -1) in tftp_send_first()
456 Curl_pgrsSetUploadSize(data, data->state.infilesize); in tftp_send_first()
460 setpacketevent(&state->spacket, TFTP_EVENT_RRQ); in tftp_send_first()
465 result = Curl_urldecode(&state->data->state.up.path[1], 0, in tftp_send_first()
470 if(strlen(filename) > (state->blksize - strlen(mode) - 4)) { in tftp_send_first()
476 msnprintf((char *)state->spacket.data + 2, in tftp_send_first()
477 state->blksize, in tftp_send_first()
486 data->state.upload && (data->state.infilesize != -1) ? in tftp_send_first()
487 data->state.infilesize : 0); in tftp_send_first()
489 result = tftp_option_add(state, &sbytes, in tftp_send_first()
490 (char *)state->spacket.data + sbytes, in tftp_send_first()
493 result = tftp_option_add(state, &sbytes, in tftp_send_first()
494 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
497 msnprintf(buf, sizeof(buf), "%d", state->requested_blksize); in tftp_send_first()
499 result = tftp_option_add(state, &sbytes, in tftp_send_first()
500 (char *)state->spacket.data + sbytes, in tftp_send_first()
503 result = tftp_option_add(state, &sbytes, in tftp_send_first()
504 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
507 msnprintf(buf, sizeof(buf), "%d", state->retry_time); in tftp_send_first()
509 result = tftp_option_add(state, &sbytes, in tftp_send_first()
510 (char *)state->spacket.data + sbytes, in tftp_send_first()
513 result = tftp_option_add(state, &sbytes, in tftp_send_first()
514 (char *)state->spacket.data + sbytes, buf); in tftp_send_first()
525 senddata = sendto(state->sockfd, (void *)state->spacket.data, in tftp_send_first()
537 if(data->state.upload) { in tftp_send_first()
538 result = tftp_connect_for_tx(state, event); in tftp_send_first()
541 result = tftp_connect_for_rx(state, event); in tftp_send_first()
546 result = tftp_connect_for_tx(state, event); in tftp_send_first()
550 result = tftp_connect_for_rx(state, event); in tftp_send_first()
554 state->state = TFTP_STATE_FIN; in tftp_send_first()
558 failf(state->data, "tftp_send_first: internal error"); in tftp_send_first()
576 static CURLcode tftp_rx(struct tftp_state_data *state, in tftp_rx() argument
581 struct Curl_easy *data = state->data; in tftp_rx()
588 rblock = getrpacketblock(&state->rpacket); in tftp_rx()
589 if(NEXT_BLOCKNUM(state->block) == rblock) { in tftp_rx()
591 state->retries = 0; in tftp_rx()
593 else if(state->block == rblock) { in tftp_rx()
602 rblock, NEXT_BLOCKNUM(state->block)); in tftp_rx()
607 state->block = (unsigned short)rblock; in tftp_rx()
608 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
609 setpacketblock(&state->spacket, state->block); in tftp_rx()
610 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
612 (struct sockaddr *)&state->remote_addr, in tftp_rx()
613 state->remote_addrlen); in tftp_rx()
620 if(state->rbytes < (ssize_t)state->blksize + 4) { in tftp_rx()
621 state->state = TFTP_STATE_FIN; in tftp_rx()
624 state->state = TFTP_STATE_RX; in tftp_rx()
626 state->rx_time = time(NULL); in tftp_rx()
631 state->block = 0; in tftp_rx()
632 state->retries = 0; in tftp_rx()
633 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
634 setpacketblock(&state->spacket, state->block); in tftp_rx()
635 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
637 (struct sockaddr *)&state->remote_addr, in tftp_rx()
638 state->remote_addrlen); in tftp_rx()
645 state->state = TFTP_STATE_RX; in tftp_rx()
646 state->rx_time = time(NULL); in tftp_rx()
651 state->retries++; in tftp_rx()
654 NEXT_BLOCKNUM(state->block), state->retries); in tftp_rx()
655 if(state->retries > state->retry_max) { in tftp_rx()
656 state->error = TFTP_ERR_TIMEOUT; in tftp_rx()
657 state->state = TFTP_STATE_FIN; in tftp_rx()
661 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
663 (struct sockaddr *)&state->remote_addr, in tftp_rx()
664 state->remote_addrlen); in tftp_rx()
673 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_rx()
674 setpacketblock(&state->spacket, state->block); in tftp_rx()
675 (void)sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
677 (struct sockaddr *)&state->remote_addr, in tftp_rx()
678 state->remote_addrlen); in tftp_rx()
681 state->state = TFTP_STATE_FIN; in tftp_rx()
699 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event) in tftp_tx() argument
701 struct Curl_easy *data = state->data; in tftp_tx()
716 int rblock = getrpacketblock(&state->rpacket); in tftp_tx()
718 if(rblock != state->block && in tftp_tx()
724 !(state->block == 0 && rblock == 65535)) { in tftp_tx()
727 rblock, state->block); in tftp_tx()
728 state->retries++; in tftp_tx()
730 if(state->retries > state->retry_max) { in tftp_tx()
732 state->block); in tftp_tx()
737 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
738 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG, in tftp_tx()
739 (struct sockaddr *)&state->remote_addr, in tftp_tx()
740 state->remote_addrlen); in tftp_tx()
753 state->rx_time = time(NULL); in tftp_tx()
754 state->block++; in tftp_tx()
757 state->block = 1; /* first data block is 1 when using OACK */ in tftp_tx()
759 state->retries = 0; in tftp_tx()
760 setpacketevent(&state->spacket, TFTP_EVENT_DATA); in tftp_tx()
761 setpacketblock(&state->spacket, state->block); in tftp_tx()
762 if(state->block > 1 && state->sbytes < state->blksize) { in tftp_tx()
763 state->state = TFTP_STATE_FIN; in tftp_tx()
771 state->sbytes = 0; in tftp_tx()
772 bufptr = (char *)state->spacket.data + 4; in tftp_tx()
774 result = Curl_client_read(data, bufptr, state->blksize - state->sbytes, in tftp_tx()
778 state->sbytes += (int)cb; in tftp_tx()
780 } while(state->sbytes < state->blksize && cb); in tftp_tx()
782 sbytes = sendto(state->sockfd, (void *) state->spacket.data, in tftp_tx()
783 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG, in tftp_tx()
784 (struct sockaddr *)&state->remote_addr, in tftp_tx()
785 state->remote_addrlen); in tftp_tx()
792 k->writebytecount += state->sbytes; in tftp_tx()
798 state->retries++; in tftp_tx()
800 " Retries = %d", NEXT_BLOCKNUM(state->block), state->retries); in tftp_tx()
802 if(state->retries > state->retry_max) { in tftp_tx()
803 state->error = TFTP_ERR_TIMEOUT; in tftp_tx()
804 state->state = TFTP_STATE_FIN; in tftp_tx()
808 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
809 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG, in tftp_tx()
810 (struct sockaddr *)&state->remote_addr, in tftp_tx()
811 state->remote_addrlen); in tftp_tx()
823 state->state = TFTP_STATE_FIN; in tftp_tx()
824 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_tx()
825 setpacketblock(&state->spacket, state->block); in tftp_tx()
826 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, in tftp_tx()
827 (struct sockaddr *)&state->remote_addr, in tftp_tx()
828 state->remote_addrlen); in tftp_tx()
831 state->state = TFTP_STATE_FIN; in tftp_tx()
901 static CURLcode tftp_state_machine(struct tftp_state_data *state, in tftp_state_machine() argument
905 struct Curl_easy *data = state->data; in tftp_state_machine()
907 switch(state->state) { in tftp_state_machine()
910 result = tftp_send_first(state, event); in tftp_state_machine()
914 result = tftp_rx(state, event); in tftp_state_machine()
918 result = tftp_tx(state, event); in tftp_state_machine()
924 DEBUGF(infof(data, "STATE: %d", state->state)); in tftp_state_machine()
943 struct tftp_state_data *state = conn->proto.tftpc; in tftp_disconnect() local
948 if(state) { in tftp_disconnect()
949 Curl_safefree(state->rpacket.data); in tftp_disconnect()
950 Curl_safefree(state->spacket.data); in tftp_disconnect()
951 free(state); in tftp_disconnect()
966 struct tftp_state_data *state; in tftp_connect() local
973 state = conn->proto.tftpc = calloc(1, sizeof(struct tftp_state_data)); in tftp_connect()
974 if(!state) in tftp_connect()
987 if(!state->rpacket.data) { in tftp_connect()
988 state->rpacket.data = calloc(1, need_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, need_blksize + 2 + 2); in tftp_connect()
997 if(!state->spacket.data) in tftp_connect()
1005 state->data = data; in tftp_connect()
1006 state->sockfd = 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; /* Unless updated by OACK response */ 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 int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, in tftp_connect()
1061 struct tftp_state_data *state = conn->proto.tftpc; in tftp_done() local
1070 if(state) in tftp_done()
1071 result = tftp_translate_code(state->error); in tftp_done()
1103 struct tftp_state_data *state = conn->proto.tftpc; in tftp_receive_packet() local
1106 fromlen = sizeof(state->remote_addr); in tftp_receive_packet()
1107 state->rbytes = (int)recvfrom(state->sockfd, in tftp_receive_packet()
1108 (void *)state->rpacket.data, in tftp_receive_packet()
1109 (RECV_TYPE_ARG3)state->blksize + 4, in tftp_receive_packet()
1111 (struct sockaddr *)&state->remote_addr, in tftp_receive_packet()
1113 state->remote_addrlen = fromlen; in tftp_receive_packet()
1116 if(state->rbytes < 4) { in tftp_receive_packet()
1119 state->event = TFTP_EVENT_TIMEOUT; in tftp_receive_packet()
1123 unsigned short event = getrpacketevent(&state->rpacket); in tftp_receive_packet()
1124 state->event = (tftp_event_t)event; in tftp_receive_packet()
1126 switch(state->event) { in tftp_receive_packet()
1129 if(state->rbytes > 4 && in tftp_receive_packet()
1130 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { in tftp_receive_packet()
1132 (char *)state->rpacket.data + 4, in tftp_receive_packet()
1133 state->rbytes-4); in tftp_receive_packet()
1135 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1142 unsigned short error = getrpacketblock(&state->rpacket); in tftp_receive_packet()
1143 char *str = (char *)state->rpacket.data + 4; in tftp_receive_packet()
1144 size_t strn = state->rbytes - 4; in tftp_receive_packet()
1145 state->error = (tftp_error_t)error; in tftp_receive_packet()
1153 result = tftp_parse_option_ack(state, in tftp_receive_packet()
1154 (const char *)state->rpacket.data + 2, in tftp_receive_packet()
1155 state->rbytes-2); in tftp_receive_packet()
1168 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1187 struct tftp_state_data *state = conn->proto.tftpc; in tftp_state_timeout() local
1193 timeout_ms = Curl_timeleft(state->data, NULL, in tftp_state_timeout()
1194 (state->state == TFTP_STATE_START)); in tftp_state_timeout()
1196 state->error = TFTP_ERR_TIMEOUT; in tftp_state_timeout()
1197 state->state = TFTP_STATE_FIN; in tftp_state_timeout()
1201 if(current > state->rx_time + state->retry_time) { in tftp_state_timeout()
1204 state->rx_time = time(NULL); /* update even though we received nothing */ in tftp_state_timeout()
1222 struct tftp_state_data *state = conn->proto.tftpc; in tftp_multi_statemach() local
1232 result = tftp_state_machine(state, event); in tftp_multi_statemach()
1235 *done = (state->state == TFTP_STATE_FIN); in tftp_multi_statemach()
1242 int rc = SOCKET_READABLE(state->sockfd, 0); in tftp_multi_statemach()
1249 state->event = TFTP_EVENT_ERROR; in tftp_multi_statemach()
1255 result = tftp_state_machine(state, state->event); in tftp_multi_statemach()
1258 *done = (state->state == TFTP_STATE_FIN); in tftp_multi_statemach()
1307 struct tftp_state_data *state = conn->proto.tftpc; in tftp_perform() local
1311 result = tftp_state_machine(state, TFTP_EVENT_INIT); in tftp_perform()
1313 if((state->state == TFTP_STATE_FIN) || result) in tftp_perform()
1337 struct tftp_state_data *state; in tftp_do() local
1349 state = conn->proto.tftpc; in tftp_do()
1350 if(!state) in tftp_do()
1359 result = tftp_translate_code(state->error); in tftp_do()
1373 type = strstr(data->state.up.path, ";mode="); in tftp_setup_connection()
1386 data->state.prefer_ascii = TRUE; in tftp_setup_connection()
1393 data->state.prefer_ascii = FALSE; in tftp_setup_connection()