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
454 struct Curl_easy *data = state->conn->data; in tftp_send_first()
466 state->retries++; in tftp_send_first()
467 if(state->retries>state->retry_max) { in tftp_send_first()
468 state->error = TFTP_ERR_NORESPONSE; in tftp_send_first()
469 state->state = TFTP_STATE_FIN; in tftp_send_first()
475 setpacketevent(&state->spacket, TFTP_EVENT_WRQ); in tftp_send_first()
476 state->conn->data->req.upload_fromhere = in tftp_send_first()
477 (char *)state->spacket.data + 4; in tftp_send_first()
478 if(data->state.infilesize != -1) in tftp_send_first()
479 Curl_pgrsSetUploadSize(data, data->state.infilesize); in tftp_send_first()
483 setpacketevent(&state->spacket, TFTP_EVENT_RRQ); in tftp_send_first()
488 result = Curl_urldecode(data, &state->conn->data->state.up.path[1], 0, in tftp_send_first()
493 if(strlen(filename) > (state->blksize - strlen(mode) - 4)) { in tftp_send_first()
499 msnprintf((char *)state->spacket.data + 2, in tftp_send_first()
500 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 msnprintf(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 msnprintf(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()
551 result = tftp_connect_for_tx(state, event); in tftp_send_first()
554 result = tftp_connect_for_rx(state, event); in tftp_send_first()
559 result = tftp_connect_for_tx(state, event); in tftp_send_first()
563 result = tftp_connect_for_rx(state, event); in tftp_send_first()
567 state->state = TFTP_STATE_FIN; in tftp_send_first()
571 failf(state->conn->data, "tftp_send_first: internal error"); in tftp_send_first()
589 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) in tftp_rx() argument
593 struct Curl_easy *data = state->conn->data; in tftp_rx()
600 rblock = getrpacketblock(&state->rpacket); in tftp_rx()
601 if(NEXT_BLOCKNUM(state->block) == rblock) { in tftp_rx()
603 state->retries = 0; in tftp_rx()
605 else if(state->block == rblock) { in tftp_rx()
614 rblock, NEXT_BLOCKNUM(state->block)); in tftp_rx()
619 state->block = (unsigned short)rblock; in tftp_rx()
620 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
621 setpacketblock(&state->spacket, state->block); in tftp_rx()
622 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
624 (struct sockaddr *)&state->remote_addr, in tftp_rx()
625 state->remote_addrlen); in tftp_rx()
632 if(state->rbytes < (ssize_t)state->blksize + 4) { in tftp_rx()
633 state->state = TFTP_STATE_FIN; in tftp_rx()
636 state->state = TFTP_STATE_RX; in tftp_rx()
638 time(&state->rx_time); in tftp_rx()
643 state->block = 0; in tftp_rx()
644 state->retries = 0; in tftp_rx()
645 setpacketevent(&state->spacket, TFTP_EVENT_ACK); in tftp_rx()
646 setpacketblock(&state->spacket, state->block); in tftp_rx()
647 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
649 (struct sockaddr *)&state->remote_addr, in tftp_rx()
650 state->remote_addrlen); in tftp_rx()
657 state->state = TFTP_STATE_RX; in tftp_rx()
658 time(&state->rx_time); in tftp_rx()
663 state->retries++; in tftp_rx()
666 NEXT_BLOCKNUM(state->block), state->retries); in tftp_rx()
667 if(state->retries > state->retry_max) { in tftp_rx()
668 state->error = TFTP_ERR_TIMEOUT; in tftp_rx()
669 state->state = TFTP_STATE_FIN; in tftp_rx()
673 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
675 (struct sockaddr *)&state->remote_addr, in tftp_rx()
676 state->remote_addrlen); in tftp_rx()
685 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_rx()
686 setpacketblock(&state->spacket, state->block); in tftp_rx()
687 (void)sendto(state->sockfd, (void *)state->spacket.data, in tftp_rx()
689 (struct sockaddr *)&state->remote_addr, in tftp_rx()
690 state->remote_addrlen); in tftp_rx()
693 state->state = TFTP_STATE_FIN; in tftp_rx()
711 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) in tftp_tx() argument
713 struct Curl_easy *data = state->conn->data; in tftp_tx()
726 int rblock = getrpacketblock(&state->rpacket); in tftp_tx()
728 if(rblock != state->block && in tftp_tx()
734 !(state->block == 0 && rblock == 65535)) { in tftp_tx()
737 rblock, state->block); in tftp_tx()
738 state->retries++; in tftp_tx()
740 if(state->retries>state->retry_max) { in tftp_tx()
742 state->block); in tftp_tx()
747 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
748 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
749 (struct sockaddr *)&state->remote_addr, in tftp_tx()
750 state->remote_addrlen); in tftp_tx()
763 time(&state->rx_time); in tftp_tx()
764 state->block++; in tftp_tx()
767 state->block = 1; /* first data block is 1 when using OACK */ in tftp_tx()
769 state->retries = 0; in tftp_tx()
770 setpacketevent(&state->spacket, TFTP_EVENT_DATA); in tftp_tx()
771 setpacketblock(&state->spacket, state->block); in tftp_tx()
772 if(state->block > 1 && state->sbytes < state->blksize) { in tftp_tx()
773 state->state = TFTP_STATE_FIN; in tftp_tx()
781 state->sbytes = 0; in tftp_tx()
782 state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4; in tftp_tx()
784 result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes, in tftp_tx()
788 state->sbytes += (int)cb; in tftp_tx()
789 state->conn->data->req.upload_fromhere += cb; in tftp_tx()
790 } while(state->sbytes < state->blksize && cb != 0); 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()
802 k->writebytecount += state->sbytes; in tftp_tx()
808 state->retries++; in tftp_tx()
810 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries); in tftp_tx()
812 if(state->retries > state->retry_max) { in tftp_tx()
813 state->error = TFTP_ERR_TIMEOUT; in tftp_tx()
814 state->state = TFTP_STATE_FIN; in tftp_tx()
818 sbytes = sendto(state->sockfd, (void *)state->spacket.data, in tftp_tx()
819 4 + state->sbytes, SEND_4TH_ARG, in tftp_tx()
820 (struct sockaddr *)&state->remote_addr, in tftp_tx()
821 state->remote_addrlen); in tftp_tx()
833 state->state = TFTP_STATE_FIN; in tftp_tx()
834 setpacketevent(&state->spacket, TFTP_EVENT_ERROR); in tftp_tx()
835 setpacketblock(&state->spacket, state->block); in tftp_tx()
836 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, in tftp_tx()
837 (struct sockaddr *)&state->remote_addr, in tftp_tx()
838 state->remote_addrlen); in tftp_tx()
841 state->state = TFTP_STATE_FIN; in tftp_tx()
911 static CURLcode tftp_state_machine(tftp_state_data_t *state, in tftp_state_machine() argument
915 struct Curl_easy *data = state->conn->data; in tftp_state_machine()
917 switch(state->state) { in tftp_state_machine()
920 result = tftp_send_first(state, event); in tftp_state_machine()
924 result = tftp_rx(state, event); in tftp_state_machine()
928 result = tftp_tx(state, event); in tftp_state_machine()
934 DEBUGF(infof(data, "STATE: %d\n", state->state)); in tftp_state_machine()
952 tftp_state_data_t *state = conn->proto.tftpc; in tftp_disconnect() local
956 if(state) { in tftp_disconnect()
957 Curl_safefree(state->rpacket.data); in tftp_disconnect()
958 Curl_safefree(state->spacket.data); in tftp_disconnect()
959 free(state); in tftp_disconnect()
974 tftp_state_data_t *state; in tftp_connect() local
979 state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t)); in tftp_connect()
980 if(!state) in tftp_connect()
990 if(!state->rpacket.data) { in tftp_connect()
991 state->rpacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
993 if(!state->rpacket.data) in tftp_connect()
997 if(!state->spacket.data) { in tftp_connect()
998 state->spacket.data = calloc(1, blksize + 2 + 2); in tftp_connect()
1000 if(!state->spacket.data) in tftp_connect()
1008 state->conn = conn; in tftp_connect()
1009 state->sockfd = state->conn->sock[FIRSTSOCKET]; in tftp_connect()
1010 state->state = TFTP_STATE_START; in tftp_connect()
1011 state->error = TFTP_ERR_NONE; in tftp_connect()
1012 state->blksize = TFTP_BLKSIZE_DEFAULT; in tftp_connect()
1013 state->requested_blksize = blksize; in tftp_connect()
1015 ((struct sockaddr *)&state->local_addr)->sa_family = in tftp_connect()
1018 tftp_set_timeouts(state); in tftp_connect()
1034 int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, in tftp_connect()
1063 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_done() local
1072 if(state) in tftp_done()
1073 result = tftp_translate_code(state->error); in tftp_done()
1109 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_receive_packet() local
1114 state->rbytes = (int)recvfrom(state->sockfd, in tftp_receive_packet()
1115 (void *)state->rpacket.data, in tftp_receive_packet()
1116 state->blksize + 4, in tftp_receive_packet()
1120 if(state->remote_addrlen == 0) { in tftp_receive_packet()
1121 memcpy(&state->remote_addr, &fromaddr, fromlen); in tftp_receive_packet()
1122 state->remote_addrlen = fromlen; in tftp_receive_packet()
1126 if(state->rbytes < 4) { in tftp_receive_packet()
1129 state->event = TFTP_EVENT_TIMEOUT; in tftp_receive_packet()
1133 unsigned short event = getrpacketevent(&state->rpacket); in tftp_receive_packet()
1134 state->event = (tftp_event_t)event; in tftp_receive_packet()
1136 switch(state->event) { in tftp_receive_packet()
1139 if(state->rbytes > 4 && in tftp_receive_packet()
1140 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { in tftp_receive_packet()
1142 (char *)state->rpacket.data + 4, in tftp_receive_packet()
1143 state->rbytes-4); in tftp_receive_packet()
1145 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1148 k->bytecount += state->rbytes-4; in tftp_receive_packet()
1154 unsigned short error = getrpacketblock(&state->rpacket); in tftp_receive_packet()
1155 char *str = (char *)state->rpacket.data + 4; in tftp_receive_packet()
1156 size_t strn = state->rbytes - 4; in tftp_receive_packet()
1157 state->error = (tftp_error_t)error; in tftp_receive_packet()
1165 result = tftp_parse_option_ack(state, in tftp_receive_packet()
1166 (const char *)state->rpacket.data + 2, in tftp_receive_packet()
1167 state->rbytes-2); in tftp_receive_packet()
1180 tftp_state_machine(state, TFTP_EVENT_ERROR); in tftp_receive_packet()
1197 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_state_timeout() local
1203 if(current > state->max_time) { in tftp_state_timeout()
1205 (long)current, (long)state->max_time)); in tftp_state_timeout()
1206 state->error = TFTP_ERR_TIMEOUT; in tftp_state_timeout()
1207 state->state = TFTP_STATE_FIN; in tftp_state_timeout()
1210 if(current > state->rx_time + state->retry_time) { in tftp_state_timeout()
1213 time(&state->rx_time); /* update even though we received nothing */ in tftp_state_timeout()
1219 return (long)(state->max_time - current); in tftp_state_timeout()
1234 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_multi_statemach() local
1244 result = tftp_state_machine(state, event); in tftp_multi_statemach()
1247 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1254 int rc = SOCKET_READABLE(state->sockfd, 0); in tftp_multi_statemach()
1261 state->event = TFTP_EVENT_ERROR; in tftp_multi_statemach()
1267 result = tftp_state_machine(state, state->event); in tftp_multi_statemach()
1270 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; in tftp_multi_statemach()
1318 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_perform() local
1322 result = tftp_state_machine(state, TFTP_EVENT_INIT); in tftp_perform()
1324 if((state->state == TFTP_STATE_FIN) || result) in tftp_perform()
1348 tftp_state_data_t *state; in tftp_do() local
1359 state = (tftp_state_data_t *)conn->proto.tftpc; in tftp_do()
1360 if(!state) in tftp_do()
1369 result = tftp_translate_code(state->error); in tftp_do()
1383 type = strstr(data->state.up.path, ";mode="); in tftp_setup_connection()