Переглянути джерело

Merge pull request #48 from enkiller/0828_1428

完善客户端文件传输请求
朱天龙 (Armink) 5 роки тому
батько
коміт
870a579801
3 змінених файлів з 40 додано та 11 видалено
  1. 2 0
      tftp/tftp.h
  2. 34 9
      tftp/tftp_client.c
  3. 4 2
      tftp/tftp_xfer.c

+ 2 - 0
tftp/tftp.h

@@ -32,6 +32,7 @@
 struct tftp_client
 {
     int max_retry;
+    int err;
     void *_private;
 };
 
@@ -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);

+ 34 - 9
tftp/tftp_client.c

@@ -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;
@@ -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 */
@@ -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 */
@@ -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;
         }
     }
@@ -143,6 +148,7 @@ 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 */
@@ -150,6 +156,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
     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));
@@ -157,6 +164,7 @@ int tftp_client_push(struct tftp_client *client, const char *local_name, const c
     {
         tftp_transfer_err(_private->xfer, 0, "malloc pack failed!");
         tftp_file_close(fp);
+        client->err = -TFTP_EMEM;
         return -TFTP_EMEM;
     }
     while (1)
@@ -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;
@@ -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 */
@@ -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;
             }
         }
@@ -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;
@@ -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 */
@@ -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 */
@@ -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 */
@@ -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));
@@ -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)
@@ -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 */
@@ -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;
@@ -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;
             }
         }
@@ -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;
+}

+ 4 - 2
tftp/tftp_xfer.c

@@ -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)
     {