1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-22 08:08:50 -05:00

minor changes, the biggest one being using Curl_select()

This commit is contained in:
Daniel Stenberg 2005-09-15 20:21:27 +00:00
parent 209b9d4374
commit 97a6d7b1a8

View File

@ -320,6 +320,7 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
{ {
int sbytes; int sbytes;
int rblock; int rblock;
struct SessionHandle *data = state->conn->data;
switch(event) { switch(event) {
@ -329,11 +330,11 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
rblock = ntohs(state->rpacket.u.data.block); rblock = ntohs(state->rpacket.u.data.block);
if ((state->block+1) != rblock) { if ((state->block+1) != rblock) {
/* No, log it, up the retry count and fail if over the limit */ /* No, log it, up the retry count and fail if over the limit */
infof(state->conn->data, infof(data,
"Received unexpected DATA packet block %d\n", rblock); "Received unexpected DATA packet block %d\n", rblock);
state->retries++; state->retries++;
if (state->retries>state->retry_max) { if (state->retries>state->retry_max) {
failf(state->conn->data, "tftp_rx: giving up waiting for block %d\n", failf(data, "tftp_rx: giving up waiting for block %d\n",
state->block+1); state->block+1);
return; return;
} }
@ -346,7 +347,7 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
sbytes = sendto(state->sockfd, &state->spacket, 4, MSG_NOSIGNAL, sbytes = sendto(state->sockfd, &state->spacket, 4, MSG_NOSIGNAL,
&state->remote_addr, state->remote_addrlen); &state->remote_addr, state->remote_addrlen);
if(sbytes < 0) { if(sbytes < 0) {
failf(state->conn->data, "%s\n", strerror(errno)); failf(data, "%s\n", strerror(errno));
} }
/* Check if completed (That is, a less than full packet is recieved) */ /* Check if completed (That is, a less than full packet is recieved) */
@ -361,7 +362,7 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
case TFTP_EVENT_TIMEOUT: case TFTP_EVENT_TIMEOUT:
/* Increment the retry count and fail if over the limit */ /* Increment the retry count and fail if over the limit */
state->retries++; state->retries++;
infof(state->conn->data, infof(data,
"Timeout waiting for block %d ACK. Retries = %d\n", state->retries); "Timeout waiting for block %d ACK. Retries = %d\n", state->retries);
if(state->retries > state->retry_max) { if(state->retries > state->retry_max) {
state->error = TFTP_ERR_TIMEOUT; state->error = TFTP_ERR_TIMEOUT;
@ -373,7 +374,7 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
&state->remote_addr, state->remote_addrlen); &state->remote_addr, state->remote_addrlen);
/* Check all sbytes were sent */ /* Check all sbytes were sent */
if(sbytes<0) { if(sbytes<0) {
failf(state->conn->data, "%s\n", strerror(errno)); failf(data, "%s\n", strerror(errno));
} }
} }
break; break;
@ -383,10 +384,10 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
break; break;
default: default:
failf(state->conn->data, "%s\n", "tftp_rx: internal error"); failf(data, "%s\n", "tftp_rx: internal error");
break; break;
} }
Curl_pgrsSetDownloadCounter(state->conn->data, Curl_pgrsSetDownloadCounter(data,
(curl_off_t) state->block*512); (curl_off_t) state->block*512);
} }
@ -399,24 +400,24 @@ static void tftp_rx(tftp_state_data_t *state, tftp_event_t event)
**********************************************************/ **********************************************************/
static void tftp_tx(tftp_state_data_t *state, tftp_event_t event) static void tftp_tx(tftp_state_data_t *state, tftp_event_t event)
{ {
struct SessionHandle *data = state->conn->data;
int sbytes; int sbytes;
int rblock; int rblock;
switch(event) { switch(event) {
case TFTP_EVENT_ACK: case TFTP_EVENT_ACK:
/* Ack the packet */ /* Ack the packet */
rblock = ntohs(state->rpacket.u.data.block); rblock = ntohs(state->rpacket.u.data.block);
if(rblock != state->block) { if(rblock != state->block) {
/* This isn't the expected block. Log it and up the retry counter */ /* This isn't the expected block. Log it and up the retry counter */
infof(state->conn->data, "Received ACK for block %d, expecting %d\n", infof(data, "Received ACK for block %d, expecting %d\n",
rblock, state->block); rblock, state->block);
state->retries++; state->retries++;
/* Bail out if over the maximum */ /* Bail out if over the maximum */
if(state->retries>state->retry_max) { if(state->retries>state->retry_max) {
failf(state->conn->data, "%s\n", failf(data, "%s\n",
"tftp_tx: giving up waiting for block %d ack", "tftp_tx: giving up waiting for block %d ack",
state->block); state->block);
} }
@ -438,16 +439,14 @@ static void tftp_tx(tftp_state_data_t *state, tftp_event_t event)
&state->remote_addr, state->remote_addrlen); &state->remote_addr, state->remote_addrlen);
/* Check all sbytes were sent */ /* Check all sbytes were sent */
if(sbytes<0) { if(sbytes<0) {
failf(state->conn->data, "%s\n", strerror(errno)); failf(data, "%s\n", strerror(errno));
} }
break; break;
case TFTP_EVENT_TIMEOUT: case TFTP_EVENT_TIMEOUT:
/* Increment the retry counter and log the timeout */ /* Increment the retry counter and log the timeout */
state->retries++; state->retries++;
infof(state->conn->data, "Timeout waiting for block %d ACK. " infof(data, "Timeout waiting for block %d ACK. "
" Retries = %d\n", state->retries); " Retries = %d\n", state->retries);
/* Decide if we've had enough */ /* Decide if we've had enough */
if(state->retries > state->retry_max) { if(state->retries > state->retry_max) {
@ -460,26 +459,22 @@ static void tftp_tx(tftp_state_data_t *state, tftp_event_t event)
&state->remote_addr, state->remote_addrlen); &state->remote_addr, state->remote_addrlen);
/* Check all sbytes were sent */ /* Check all sbytes were sent */
if(sbytes<0) { if(sbytes<0) {
failf(state->conn->data, "%s\n", strerror(errno)); failf(data, "%s\n", strerror(errno));
} }
} }
break; break;
case TFTP_EVENT_ERROR: case TFTP_EVENT_ERROR:
state->state = TFTP_STATE_FIN; state->state = TFTP_STATE_FIN;
break; break;
default: default:
failf(state->conn->data, "%s\n", "tftp_tx: internal error"); failf(data, "%s\n", "tftp_tx: internal error");
break; break;
} }
/* Update the progress meter */ /* Update the progress meter */
Curl_pgrsSetUploadCounter(state->conn->data, (curl_off_t) state->block*512); Curl_pgrsSetUploadCounter(data, (curl_off_t) state->block*512);
} }
/********************************************************** /**********************************************************
@ -492,22 +487,26 @@ static void tftp_tx(tftp_state_data_t *state, tftp_event_t event)
static CURLcode tftp_state_machine(tftp_state_data_t *state, static CURLcode tftp_state_machine(tftp_state_data_t *state,
tftp_event_t event) tftp_event_t event)
{ {
struct SessionHandle *data = state->conn->data;
switch(state->state) { switch(state->state) {
case TFTP_STATE_START: case TFTP_STATE_START:
DEBUGF(infof(data, "TFTP_STATE_START\n"));
tftp_send_first(state, event); tftp_send_first(state, event);
break; break;
case TFTP_STATE_RX: case TFTP_STATE_RX:
DEBUGF(infof(data, "TFTP_STATE_RX\n"));
tftp_rx(state, event); tftp_rx(state, event);
break; break;
case TFTP_STATE_TX: case TFTP_STATE_TX:
DEBUGF(infof(data, "TFTP_STATE_TX\n"));
tftp_tx(state, event); tftp_tx(state, event);
break; break;
case TFTP_STATE_FIN: case TFTP_STATE_FIN:
infof(state->conn->data, "%s\n", "TFTP finished"); infof(data, "%s\n", "TFTP finished");
break; break;
default: default:
failf(state->conn->data, "%s\n", "Internal state machine error"); DEBUGF(infof(data, "STATE: %d\n", state->state));
failf(data, "%s\n", "Internal state machine error");
break; break;
} }
return CURLE_OK; return CURLE_OK;
@ -591,8 +590,6 @@ CURLcode Curl_tftp(struct connectdata *conn, bool *done)
struct SessionHandle *data = conn->data; struct SessionHandle *data = conn->data;
tftp_state_data_t *state = (tftp_state_data_t *)(conn->proto.tftp); tftp_state_data_t *state = (tftp_state_data_t *)(conn->proto.tftp);
tftp_event_t event; tftp_event_t event;
fd_set readset;
struct timeval tv;
CURLcode code; CURLcode code;
int rc; int rc;
struct sockaddr fromaddr; struct sockaddr fromaddr;
@ -606,29 +603,14 @@ CURLcode Curl_tftp(struct connectdata *conn, bool *done)
state->state != TFTP_STATE_FIN; state->state != TFTP_STATE_FIN;
tftp_state_machine(state, event) ) { tftp_state_machine(state, event) ) {
/* Update the progress meter */
Curl_pgrsUpdate(conn);
/* Waiting on event from network or OS */
FD_ZERO(&readset);
FD_SET(state->sockfd, &readset);
tv.tv_sec=state->retry_time; tv.tv_usec=0;
restart:
/* Wait until ready to read or timeout occurs */ /* Wait until ready to read or timeout occurs */
rc=select(state->sockfd+1, &readset, NULL, NULL, &tv); rc=Curl_select(state->sockfd, CURL_SOCKET_BAD, state->retry_time * 1000);
if(rc == -1) { if(rc == -1) {
/* Restart if a signal interrupt occured */ /* bail out */
if(errno == EINTR) { int error = Curl_ourerrno();
goto restart; failf(data, "%s\n", Curl_strerror(conn, error));
}
/* Otherwise, bail out */
failf(state->conn->data, "%s\n", strerror(errno));
event = TFTP_EVENT_ERROR; event = TFTP_EVENT_ERROR;
} }
else if (rc==0) { else if (rc==0) {
/* A timeout occured */ /* A timeout occured */
@ -670,6 +652,10 @@ CURLcode Curl_tftp(struct connectdata *conn, bool *done)
failf(conn->data, "%s\n", "Internal error: Unexpected packet"); failf(conn->data, "%s\n", "Internal error: Unexpected packet");
break; break;
} }
/* Update the progress meter */
Curl_pgrsUpdate(conn);
} }
/* Check for transfer timeout every 10 blocks, or after timeout */ /* Check for transfer timeout every 10 blocks, or after timeout */
@ -677,12 +663,13 @@ CURLcode Curl_tftp(struct connectdata *conn, bool *done)
time_t current; time_t current;
time(&current); time(&current);
if(current>state->max_time) { if(current>state->max_time) {
DEBUGF(infof(data, "timeout: %d > %d\n",
current, state->max_time));
state->error = TFTP_ERR_TIMEOUT; state->error = TFTP_ERR_TIMEOUT;
state->state = TFTP_STATE_FIN; state->state = TFTP_STATE_FIN;
} }
} }
} }
/* Tell curl we're done */ /* Tell curl we're done */