• 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 #ifndef TFTPC_H
33 #define TFTPC_H
34 
35 #include "types_adapt.h"
36 
37 #if LWIP_TFTP /* don't build if not configured for use in lwipopts.h */
38 
39 #if defined (__cplusplus) && __cplusplus
40 extern "C" {
41 #endif
42 
43 #define TFTP_NULL_UINT32        ((u32_t)0xffffffffUL)
44 
45 #define TFTP_NULL_INT32         (-1)
46 
47 /** @cond liteos
48 * @defgroup TFTP_Interfaces
49 * @ingroup Enums
50 * * This section contains the TFTP enums.
51 */
52 /**
53 *
54 * This Enum is used to specify the transfer mode of the file to be handled by TFTP client.
55 */
56 typedef enum tagTFTPC_TransferMode {
57     TRANSFER_MODE_ASCII = 0,  /**< Indicates that the mode of transfer is ASCII. */
58     TRANSFER_MODE_BINARY,     /**< Indicates that the mode of transfer is Binary */
59     TRANSFER_MODE_BUTT        /**< Indicates invalid transfer mode.*/
60 } TFTPC_TRANSFER_MODE_E;
61 
62 /**
63 * This Enum is used to specify the transfer mode to be handled by TFTP client
64 * This Enum indicates the TFTP client transfer mode of the file
65 
66 */
67 typedef enum tagTFTPC_ErrCode {
68     TFTPC_SOCKET_FAILURE = 1, /**< Error while creating UDP socket. */
69     TFTPC_BIND_FAILURE = 2, /**< Error while binding to the UDP socket. */
70     TFTPC_SELECT_ERROR = 3, /**< Error returned by select() system call. */
71     TFTPC_RECVFROM_ERROR = 4, /**< Error while receiving data from the peer. */
72     TFTPC_SENDTO_ERROR = 5, /**< Error while sending data to the peer. */
73     TFTPC_FILE_NOT_FOUND = 6, /**< Requested file is not found. */
74 
75     /**< This is the error sent by the server when host name cannot be resolved. */
76     TFTPC_CANNOT_RESOLVE_HOSTNAME = 7,
77     TFTPC_INVALID_PARAVALUE = 8, /**< Input parameters passed to TFTP interfaces are invalid. */
78 
79     /**< Error detected in TFTP packet or the error received from the TFTP server. */
80     TFTPC_PROTO_ERROR = 9,
81     /**< Error during packet synchronization while sending or unexpected packet is received. */
82     TFTPC_SYNC_FAILURE = 10,
83     /**< File size limit crossed, Max block can be 0xFFFF, each block containing 512 bytes. */
84     TFTPC_FILE_TOO_BIG = 11,
85     TFTPC_SRC_FILENAME_LENGTH_ERROR = 12, /**< File name length greater than 256. */
86     TFTPC_IP_NOT_WITHIN_RANGE = 13, /**< Host name IP is not valid. */
87     TFTPC_ACCESS_ERROR = 14, /**< TFTP server returned file access error. */
88 
89     /**< TFTP server returned error signifying that the DISK is full to write. */
90     TFTPC_DISK_FULL = 15,
91     TFTPC_FILE_EXISTS = 16, /**< TFTP server returned error signifying that the file exists. */
92 
93     /**< tftp_put_file_by_filename returned error signifying that the source file name do not exist. */
94     TFTPC_FILE_NOT_EXIST = 17,
95     TFTPC_MEMALLOC_ERROR = 18, /**< Memory allocation failed in TFTP client. */
96     TFTPC_FILEOPEN_ERROR = 19, /**< File open failed. */
97     TFTPC_FILEREAD_ERROR = 20, /**< File read error. */
98     TFTPC_FILECREATE_ERROR = 21, /**< File create error. */
99     TFTPC_FILEWRITE_ERROR = 22, /**< File write error. */
100     TFTPC_TIMEOUT_ERROR = 23, /**< Max time expired while waiting for file to be received. */
101 
102     /**< Error when the received packet is less than 4 bytes (error length) or greater than 512 bytes. */
103     TFTPC_PKT_SIZE_ERROR = 24,
104     TFTPC_ERROR_NOT_DEFINED = 25, /**< Returned by TFTP server for protocol user error. */
105     TFTPC_DEST_PATH_LENGTH_ERROR = 26, /**< If the destination file path length is greater than 256. */
106     TFTPC_UNKNOWN_TRANSFER_ID = 27, /**< Returned by TFTP server for undefined transfer ID. */
107 
108     /**<  IOCTL function failed at TFTP client while setting the socket to non-block. */
109     TFTPC_IOCTLSOCKET_FAILURE = 28,
110     TFTPC_MEMCPY_FAILURE = 29 /**< TFTP memcpy failure. */
111 } TFTPC_ERR_CODE_E;
112 
113 typedef enum tagTFTPC_OpCode {
114     TFTPC_OP_RRQ = 1,         /* read request */
115     TFTPC_OP_WRQ,             /* write request */
116     TFTPC_OP_DATA,            /* data packet */
117     TFTPC_OP_ACK,             /* acknowledgment */
118     TFTPC_OP_ERROR,           /* error code */
119     TFTPC_OP_OPT              /* option code */
120 } TFTPC_OPCODE_E;
121 
122 typedef enum tagTFTPC_PROTOCOL_ErrCode {
123     TFTPC_PROTOCOL_USER_DEFINED = 0,
124     TFTPC_PROTOCOL_FILE_NOT_FOUND,
125     TFTPC_PROTOCOL_ACCESS_ERROR,
126     TFTPC_PROTOCOL_DISK_FULL,
127     TFTPC_PROTOCOL_PROTO_ERROR,
128     TFTPC_PROTOCOL_UNKNOWN_TRANSFER_ID,
129     TFTPC_PROTOCOL_FILE_EXISTS,
130     TFTPC_PROTOCOL_CANNOT_RESOLVE_HOSTNAME
131 } TFTPC_PROT_ERRCODE_E;
132 
133 
134 #ifndef TFTPC_MAX_SEND_REQ_ATTEMPTS
135 #define TFTPC_MAX_SEND_REQ_ATTEMPTS         5 /* tftp max attempts */
136 #endif
137 
138 #ifndef TFTPC_TIMEOUT_PERIOD
139 #define TFTPC_TIMEOUT_PERIOD         5 /* tftp timeout period,unit :s */
140 #endif
141 
142 #define TFTPC_SERVER_PORT          69 /* tftp server well known port no. */
143 
144 /*  MAX file size in TFTP is 32 MB.
145     Reason for keeping 75 here , is ((75*512=38400bytes)/1024) =  37MB. So the recv/Send Loop can
146     receive the complete MAX message from the network
147 */
148 #define TFTPC_MAX_WAIT_IN_LOOP         75
149 
150 #define TFTP_BLKSIZE               512     /* data block size (IEN-133) */
151 #define TFTP_HDRSIZE               4       /* TFTP header size */
152 #define TFTP_PKTSIZE              (TFTP_BLKSIZE + TFTP_HDRSIZE) /* Packet size */
153 #define TFTP_MAX_MODE_SIZE         9  /* max size of mode string */
154 #define TFTP_MAXERRSTRSIZE         100 /* max size of error message string */
155 #define TFTP_MAX_PATH_LENGTH       256 /* Max path or filename length */
156 #define TFTP_MAX_BLK_NUM          (0xFFFFL) /* MAximum block number */
157 
158 /* IP address not including reserved IPs(0 and 127) and multicast addresses(Class D) */
159 #define TFTPC_IP_ADDR_MIN         0x01000000
160 #define TFTPC_IP_ADDR_EX_RESV     0x7effffff
161 #define TFTPC_IP_ADDR_CLASS_B     0x80000000
162 #define TFTPC_IP_ADDR_EX_CLASS_DE 0xdfffffff
163 
164 #define TFTPC_FOUR 4  /* minimum packet size */
165 
166 /****************************************************************************/
167 /*                            Structure definitions                         */
168 /****************************************************************************/
169 /* Tftp data packet */
170 typedef struct tagTFTPC_DATA {
171     u16_t usBlknum;                      /* block number */
172     u8_t ucDataBuf[TFTP_BLKSIZE];       /* Actual data */
173 } TFTPC_DATA_S;
174 
175 
176 /* TFTP error packet */
177 typedef struct tagTFTPC_ERROR {
178     u16_t usErrNum;                       /* error number */
179     u8_t ucErrMesg[TFTP_MAXERRSTRSIZE]; /* error message */
180 } TFTPC_ERROR_S;
181 
182 
183 /* TFTP packet format */
184 typedef struct tagTFTPC_PACKET {
185     u16_t usOpcode; /* Opcode value */
186     union {
187         /* it contains mode and filename */
188         s8_t ucName_Mode[TFTP_MAX_PATH_LENGTH + TFTP_MAX_MODE_SIZE];
189         u16_t usBlknum; /* Block Number */
190         TFTPC_DATA_S stTFTP_Data; /* Data Packet */
191         TFTPC_ERROR_S stTFTP_Err; /* Error Packet */
192     } u;
193 } TFTPC_PACKET_S;
194 
195 
196 /** @defgroup TFTP_Interfaces
197 * This section contains the TFTP Interfaces
198 */
199 /*
200 Func Name:  lwip_tftp_get_file_by_filename
201 */
202 /**
203 * @ingroup TFTP_Interfaces
204 * @brief
205 * This API gets the source file from the server. It then stores the received file in the destination path
206 * on the client system.
207 *
208 * @param[in]    ulHostAddr          IP address of Host. This is the TFTP server IP. [NA]
209 * @param[in]    usTftpServPort    TFTP server port. If the value is passed as 0 then the default TFTP
210 *                                                   PORT 69 is used. [NA]
211 * @param[in]    ucTftpTransMode File transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII. [NA]
212 * @param[in]    szSrcFileName     Source file in the tftp server. [NA]
213 * @param[in]    szDestDirPath     Destination file path in the in the client. [NA]
214 * @param[out]  [N/A]
215 *
216 * @return
217 *  ERR_OK: On success \n
218 *  TFTPC_ERR_CODE_E: On failure
219 *
220 * @note
221 * \n
222 * The behavior of this API is such that if the destination file already exists, it will be overwritten.
223 */
224 u32_t lwip_tftp_get_file_by_filename(u32_t ulHostAddr,
225                                      u16_t usTftpServPort,
226                                      u8_t ucTftpTransMode,
227                                      s8_t *szSrcFileName,
228                                      s8_t *szDestDirPath);
229 
230 
231 /* @defgroup TFTP_Interfaces
232 * This section contains the TFTP Interfaces
233 */
234 /*
235 Func Name:  lwip_tftp_put_file_by_filename
236 */
237 /**
238 * @ingroup TFTP_Interfaces
239 
240 * @brief
241 * This API reads the contents of the source file on the client system and sends it to the server and
242 * server then receives the data and stores it in the specified destination path.
243 *
244 * @param[in]    ulHostAddr         Indicates the IP address of Host. This is the TFTP server IP.
245 * @param[in]    usTftpServPort    Indicates the TFTP server port. If the value is passed as 0 then the default TFTP
246 *                                                   PORT 69 is used.
247 * @param[in]    ucTftpTransMode  Indicates the file transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII.
248 * @param[in]    szSrcFileName     Indicates the source file in the client.
249 * @param[in]    szDestDirPath     Indicates the destination file path on the tftp server.
250 *
251 * @return
252 *  ERR_OK: On success \n
253 *  TFTPC_ERR_CODE_E: On failure
254 *
255 */
256 u32_t lwip_tftp_put_file_by_filename(u32_t ulHostAddr,
257                                      u16_t usTftpServPort,
258                                      u8_t cTftpTransMode,
259                                      s8_t *szSrcFileName,
260                                      s8_t *szDestDirPath);
261 
262 #ifdef TFTP_TO_RAWMEM
263 /* @defgroup TFTP_Interfaces
264 * This section contains the TFTP Interfaces
265 */
266 /*
267 Func Name:  lwip_tftp_get_file_by_filename_to_rawmem
268 */
269 /**
270 * @ingroup TFTP_Interfaces
271 
272 * @brief
273 * This API gets the source file from the server. It then stores the received file in the target memory
274 * on the client system.
275 *
276 * @param[in]    ulHostAddr         Indicates the IP address of the Host. This is the TFTP server IP.
277 * @param[in]    usTftpServPort     Indicates the TFTP server port. If the value is passed as 0 then the default TFTP
278 *                                                   PORT 69 is used.
279 * @param[in]    ucTftpTransMode  Indicates the File transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII.
280 * @param[in]    szSrcFileName      Indicates the Source file in the TFTP server.
281 * @param[in]    szDestMemAddr      Indicates the target memory address in the client.
282 * @param[in/out]    ulFileLength       Indicates the target memory address can cache the size of the content,
283                                        and The real size of the Source file.
284 *
285 * @return
286 *  ERR_OK: On success \n
287 *  TFTPC_ERR_CODE_E: On failure
288 * @note
289 
290 * 1.You must define TFTP_TO_RAWMEM when using this API. \n
291 * 2.The behavior of this API is such that if the destination file already exists, it will be overwritten.
292 * @endcond
293 */
294 
295 u32_t lwip_tftp_get_file_by_filename_to_rawmem(u32_t ulHostAddr,
296                                                u16_t usTftpServPort,
297                                                u8_t ucTftpTransMode,
298                                                s8_t *szSrcFileName,
299                                                s8_t *szDestMemAddr,
300                                                u32_t *ulFileLength);
301 #endif
302 
303 #if defined (__cplusplus) && __cplusplus
304 }
305 #endif
306 
307 #endif /* LWIP_TFTP */
308 
309 #endif /* TFTPC_H */
310