• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <string.h>
39 #include <assert.h>
40 #include "tftpc.h"
41 
42 #ifdef LOSCFG_NET_LWIP_SACK_TFTP
43 static int tcpip_init_finish = 1;
44 static char *TftpError[] = {
45     "TFTP transfer finish\n",
46     "Error while creating UDP socket\n",
47     "Error while binding to the UDP socket\n",
48     "Error returned by select() system call\n",
49     "Error while receiving data from the peer\n",
50     "Error while sending data to the peer\n",
51     "Requested file is not found\n",
52     "This is the error sent by the server when hostname cannot be resolved\n",
53     "Input parameters passed to TFTP interfaces are invalid\n",
54     "Error detected in TFTP packet or the error received from the TFTP server\n",
55     "Error during packet synhronization while sending or unexpected packet is received\n",
56     "File size limit crossed, Max block can be 0xFFFF, each block containing 512 bytes\n",
57     "File name length greater than 256\n",
58     "Hostname IP is not valid\n",
59     "TFTP server returned file access error\n",
60     "TFTP server returned error signifying that the DISK is full to write\n",
61     "TFTP server returned error signifying that the file exist\n",
62     "The source file name do not exisits\n",
63     "Memory allocaion failed in TFTP client\n",
64     "File open failed\n",
65     "File read error\n",
66     "File create error\n",
67     "File write error\n",
68     "Max time expired while waiting for file to be recived\n",
69     "Error when the received packet is less than 4bytes(error length) or greater than 512bytes\n",
70     "Returned by TFTP server for protocol user error\n",
71     "The destination file path length greater than 256\n",
72     "Returned by TFTP server for undefined transfer ID\n",
73     "IOCTL function failed at TFTP client while setting the socket to non-block\n",
74 };
75 
76 #ifndef ARRAY_SIZE
77 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
78 #endif
79 
osShellTftp(int argc,const char ** argv)80 u32_t osShellTftp(int argc, const char **argv)
81 {
82     u32_t ulRemoteAddr = IPADDR_NONE;
83     const u16_t usTftpServPort = 69;
84     u8_t ucTftpGet = 0;
85     s8_t *szLocalFileName = NULL;
86     s8_t *szRemoteFileName = NULL;
87     u32_t ret;
88 
89     int i = 1;
90     if (argc < 1 || argv == NULL) {
91         goto usage;
92     }
93 
94     if (!tcpip_init_finish) {
95         PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__);
96         return LOS_NOK;
97     }
98 
99     while (i < argc) {
100         if (strcmp(argv[i], "-p") == 0) {
101             ucTftpGet = 0;
102             i++;
103             continue;
104         }
105 
106         if (strcmp(argv[i], "-g") == 0) {
107             ucTftpGet = 1;
108             i++;
109             continue;
110         }
111 
112         if (strcmp(argv[i], "-l") == 0 && ((i + 1) < argc)) {
113             szLocalFileName = (s8_t *)argv[i + 1];
114             i += 2;
115             continue;
116         }
117 
118         if (strcmp(argv[i], "-r") == 0 && ((i + 1) < argc)) {
119             szRemoteFileName = (s8_t *)argv[i + 1];
120             i += 2;
121             continue;
122         }
123 
124         if ((i + 1) == argc) {
125             ulRemoteAddr = inet_addr(argv[i]);
126             break;
127         }
128 
129         goto usage;
130     }
131 
132     if (ulRemoteAddr == IPADDR_NONE || szLocalFileName == NULL || szRemoteFileName == NULL) {
133         goto usage;
134     }
135 
136     if (ucTftpGet) {
137         ret = lwip_tftp_get_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort,
138                                              TRANSFER_MODE_BINARY, szRemoteFileName, szLocalFileName);
139     } else {
140         ret = lwip_tftp_put_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort,
141                                              TRANSFER_MODE_BINARY, szLocalFileName, szRemoteFileName);
142     }
143 
144     LWIP_ASSERT("TFTP UNKNOW ERROR!", ret < ARRAY_SIZE(TftpError));
145     PRINTK("%s", TftpError[ret]);
146     if (ret) {
147         return LOS_NOK;
148     } else {
149         return LOS_OK;
150     }
151 usage:
152     PRINTK("usage:\nTransfer a file from/to tftp server\n");
153     PRINTK("tftp <-g/-p> -l FullPathLocalFile -r RemoteFile Host\n");
154     return LOS_NOK;
155 }
156 
157 #ifdef LOSCFG_SHELL_CMD_DEBUG
158 SHELLCMD_ENTRY(tftp_shellcmd, CMD_TYPE_EX, "tftp", XARGS, (CmdCallBackFunc)(uintptr_t)osShellTftp);
159 #endif /* LOSCFG_SHELL_CMD_DEBUG */
160 #endif /* LOSCFG_NET_LWIP_SACK_TFTP */
161