Skip to content

Commit

Permalink
Merge pull request #48 from enkiller/0828_1428
Browse files Browse the repository at this point in the history
完善客户端文件传输请求
  • Loading branch information
armink authored Aug 28, 2020
2 parents 190096a + 1870e6f commit 870a579
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
2 changes: 2 additions & 0 deletions tftp/tftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
struct tftp_client
{
int max_retry;
int err;
void *_private;
};

Expand All @@ -47,6 +48,7 @@ struct tftp_client *tftp_client_create(const char *ip_addr, int port);
void tftp_client_destroy(struct tftp_client *client);
int tftp_client_push(struct tftp_client *client, const char *local_name, const char *remote_name);
int tftp_client_pull(struct tftp_client *client, const char *remote_name, const char *local_name);
int tftp_client_err(struct tftp_client *client);
struct tftp_server *tftp_server_create(const char *root_name, int port);
void tftp_server_run(struct tftp_server *server);
void tftp_server_destroy(struct tftp_server *server);
Expand Down
43 changes: 34 additions & 9 deletions tftp/tftp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct tftp_client *tftp_client_create(const char *ip_addr, int port)
}
/* Number of Initial Retries */
client->max_retry = TFTP_MAX_RETRY;
/* Initialization error number */
client->err = TFTP_OK;
/* Binding Private Data */
client->_private = _private;
return client;
Expand Down Expand Up @@ -102,6 +104,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c

_private = client->_private;
max_retry = client->max_retry;
client->err = TFTP_OK;
while (max_retry)
{
/* Send Write Request */
Expand All @@ -110,6 +113,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
{
tftp_printf("tftp send request failed !! retry:%d. exit\n", client->max_retry - max_retry);
max_retry = 0;
client->err = res;
break;
}
/* Waiting for server response */
Expand All @@ -121,15 +125,16 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
}
else if (res == -TFTP_ETIMEOUT)
{
tftp_printf("tftp selct timeout. retry\n");
tftp_printf("tftp wait response timeout. retry\n");
max_retry --;
continue;
}
else
{
/* Waiting for Response Error */
tftp_printf("tftp selct err:%d. exit\n", res);
tftp_printf("tftp wait response err:%d. exit\n", res);
max_retry = 0;
client->err = res;
break;
}
}
Expand All @@ -143,20 +148,23 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
if (res != TFTP_OK)
{
tftp_printf("wait ack failed!! exit\n");
client->err = res;
return res;
}
/* Open file */
fp = tftp_file_open(local_name, _private->xfer->mode, 1);
if (fp == NULL)
{
tftp_printf("open file \"%s\" error.\n", local_name);
client->err = -TFTP_EFILE;
return -TFTP_EFILE;
}
pack = malloc(sizeof(struct tftp_packet));
if (pack == NULL)
{
tftp_transfer_err(_private->xfer, 0, "malloc pack failed!");
tftp_file_close(fp);
client->err = -TFTP_EMEM;
return -TFTP_EMEM;
}
while (1)
Expand All @@ -166,6 +174,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
if (r_size < 0)
{
max_retry = 0;
client->err = -TFTP_EFILE;
break;
}
max_retry = client->max_retry;
Expand All @@ -177,6 +186,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
{
tftp_transfer_err(_private->xfer, 0, "send file err!");
max_retry = 0;
client->err = -TFTP_EDATA;
break;
}
/* Wait server ACK */
Expand All @@ -188,14 +198,15 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
}
else if (res == -TFTP_ETIMEOUT)
{
tftp_printf("tftp selct timeout. retry\n");
tftp_printf("tftp wait response timeout. retry\n");
max_retry --;
continue;
}
else
{
tftp_printf("tftp selct err:%d. exit\n", res);
tftp_printf("tftp wait response err:%d. exit\n", res);
max_retry = 0;
client->err = res;
break;
}
}
Expand All @@ -208,6 +219,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
if (tftp_wait_ack(_private->xfer) != TFTP_OK)
{
tftp_printf("wait ack failed!! exit\n");
client->err = -TFTP_EACK;
break;
}
file_size += r_size;
Expand All @@ -234,6 +246,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const

_private = client->_private;
max_retry = client->max_retry;
client->err = TFTP_OK;
while (max_retry)
{
/* Send Read File Request */
Expand All @@ -242,6 +255,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
{
tftp_printf("tftp send request failed !! retry:%d. exit\n", max_retry);
max_retry = 0;
client->err = res;
break;
}
/* Waiting for the server to respond to the request */
Expand All @@ -253,16 +267,17 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
}
else if (res == -TFTP_ETIMEOUT)
{
tftp_printf("tftp selct timeout. retry\n");
tftp_printf("tftp wait response timeout. retry\n");
max_retry --;
continue;
}
else
{
tftp_printf("tftp selct err:%d. exit\n", res);
tftp_printf("tftp wait response err:%d. exit\n", res);
max_retry = 0;
client->err = res;
break;
}
}
}

/* More than the maximum number of retries. exit */
Expand All @@ -276,6 +291,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
if (fp == NULL)
{
tftp_printf("open file \"%s\" error.\n", local_name);
client->err = -TFTP_EFILE;
return -TFTP_EFILE;
}
pack = malloc(sizeof(struct tftp_packet));
Expand All @@ -284,6 +300,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
/* malloc failed. send err msg and exit */
tftp_transfer_err(_private->xfer, 0, "malloc pack failed!");
tftp_file_close(fp);
client->err = -TFTP_EMEM;
return -TFTP_EMEM;
}
while (1)
Expand All @@ -294,6 +311,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
if (recv_size < 0)
{
tftp_printf("read data err[%d]! exit\n", recv_size);
client->err = -TFTP_EDATA;
break;
}
/* Write data to file */
Expand All @@ -302,6 +320,7 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
{
tftp_printf("write file err! exit\n");
tftp_transfer_err(_private->xfer, 0, "write file err!");
client->err = -TFTP_EFILE;
break;
}
file_size += recv_size;
Expand All @@ -324,13 +343,14 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
}
else if (res == -TFTP_ETIMEOUT)
{
tftp_printf("tftp selct timeout. retry\n");
tftp_printf("tftp wait response timeout. retry\n");
max_retry --;
}
else
{
tftp_printf("tftp selct err:%d. exit\n", res);
tftp_printf("tftp wait response err:%d. exit\n", res);
max_retry = 0;
client->err = res;
break;
}
}
Expand All @@ -344,3 +364,8 @@ int tftp_client_pull(struct tftp_client *client, const char *remote_name, const
free(pack);
return file_size;
}

int tftp_client_err(struct tftp_client *client)
{
return client->err;
}
6 changes: 4 additions & 2 deletions tftp/tftp_xfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ int tftp_send_request(struct tftp_xfer *xfer, uint16_t cmd, const char *remote_f
}
/* Packing request packet header */
send_packet->cmd = htons(cmd);
size = sprintf(send_packet->info.filename, "%s%c%s%c%d%c", remote_file, 0, xfer->mode, 0, xfer->blksize, 0) + 3;
size = sprintf(send_packet->info.filename, "%s%c%s%c%s%c%d%c%s%c%d%c",
remote_file, 0, xfer->mode, 0, "blksize", 0, xfer->blksize, 0,"tsize", 0, 0, 0) + 2;
/* send data */
r_size = sendto(xfer->sock, send_packet, size, 0, (struct sockaddr *)&_private->server, sizeof(struct sockaddr_in));
r_size = sendto(xfer->sock, send_packet, size, 0,
(struct sockaddr *)&_private->server, sizeof(struct sockaddr_in));
free(send_packet);
if (size != r_size)
{
Expand Down

0 comments on commit 870a579

Please sign in to comment.