• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NSTACKX_DFILE_H
17 #define NSTACKX_DFILE_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <stdarg.h>
22 
23 #if defined(_WIN32) || defined(_WIN64)
24 #include <WS2tcpip.h>
25 #define NSTACKX_EXPORT __declspec(dllexport)
26 #else
27 #include <ifaddrs.h>
28 #include <netinet/in.h>
29 #define NSTACKX_EXPORT extern
30 #endif
31 
32 #include "nstackx_error.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef NSTACKX_WITH_LITEOS
39 #define NSTACKX_DFILE_MAX_FILE_NUM 10
40 #else
41 #define NSTACKX_DFILE_MAX_FILE_NUM 500
42 #endif
43 #define NSTACKX_MAX_FILE_NAME_LEN 256
44 #define NSTACKX_MAX_PATH_LEN 256
45 #define NSTACKX_MAX_REMOTE_PATH_LEN 1024
46 /* Maximum sending list limit. Sending more than this value will fail. */
47 #ifdef NSTACKX_WITH_LITEOS
48 #define NSTACKX_MAX_FILE_LIST_NUM 10
49 #else
50 #define NSTACKX_MAX_FILE_LIST_NUM 500
51 #endif
52 #define NSTACKX_MAX_STORAGE_PATH_NUM 500
53 #define NSTACKX_RESERVED_TRANS_ID 0
54 #define NSTACKX_RESERVED_PATH_TYPE 0
55 #define NSTACKX_MULTI_PATH_NUM 2
56 /* 10MB VTRANS */
57 #define NSTACKX_VTRANS_DEFAULT_SIZE (10 * 1024 * 1024)
58 #define NSTACKX_VTRANS_STEP_SIZE (5 * 1024 * 1024)
59 #define NSTACKX_VTRANS_MAX_SIZE (1024 * 1024 * 1024)
60 
61 #define CAPS_MULTIPATH 4
62 
63 typedef struct DFileSession DFileSession;
64 
65 /* DFile session message type list. */
66 typedef enum {
67     DFILE_ON_CONNECT_SUCCESS = 1,
68     DFILE_ON_CONNECT_FAIL,
69     DFILE_ON_FILE_LIST_RECEIVED,
70     DFILE_ON_FILE_RECEIVE_SUCCESS,
71     DFILE_ON_FILE_RECEIVE_FAIL,
72     DFILE_ON_FILE_SEND_SUCCESS,
73     DFILE_ON_FILE_SEND_FAIL,
74     DFILE_ON_FATAL_ERROR,
75     DFILE_ON_SESSION_IN_PROGRESS,
76     DFILE_ON_TRANS_IN_PROGRESS,
77     DFILE_ON_SESSION_TRANSFER_RATE,
78     DFILE_ON_BIND,
79 } DFileMsgType;
80 
81 enum {
82     CAPS_UDP_GSO = 0,
83     CAPS_LINK_SEQUENCE,
84     CAPS_WLAN_CATAGORY,
85     CAPS_NO_RTT,
86     CAPS_RESERVED, /* for multipath check of old version */
87     /* add more capability here */
88     CAPS_MAX,
89 };
90 
91 #define NSTACKX_WLAN_CAT_2_4G               1U
92 #define NSTACKX_WLAN_CAT_5G                 2U
93 #define NSTACKX_WLAN_CAT_DIRECT             3U
94 #define NSTACKX_WLAN_CAT_TCP                4U
95 
96 #define NSTACKX_RECV_BUFFER_LEN  1600
97 
98 #define NBITS(n)                            (1U << (n))
99 #define NSTACKX_CAPS_UDP_GSO                NBITS(CAPS_UDP_GSO)
100 #define NSTACKX_CAPS_LINK_SEQUENCE          NBITS(CAPS_LINK_SEQUENCE)
101 #define NSTACKX_CAPS_WLAN_CATAGORY          NBITS(CAPS_WLAN_CATAGORY)
102 #define NSTACKX_CAPS_MULTIPATH              NBITS(CAPS_MULTIPATH)
103 
104 #define NSTACKX_CAPS_MASK                   (NBITS(CAPS_MAX) - 1)
105 
106 /*
107  * DFile session message data. User should fetch corresponding member variable based on message type:
108  *          DFileMsgType               Member variable
109  *   DFILE_ON_CONNECT_SUCCESS            none
110  *   DFILE_ON_CONNECT_FAIL               errorCode
111  *   DFILE_ON_FILE_LIST_RECEIVED         fileList
112  *   DFILE_ON_FILE_RECEIVE_SUCCESS       fileList and transferUpdate
113  *   DFILE_ON_FILE_RECEIVE_FAIL          fileList, errorCode and transferUpdate. fileList maybe empty, as not all the
114  *                                       file names are received.
115  *   DFILE_ON_FILE_SEND_SUCCESS          fileList and transferUpdate
116  *   DFILE_ON_FILE_SEND_FAIL             fileList, errorCode and transferUpdate.
117  *   DFILE_ON_TRANS_IN_PROGRESS          transferUpdate and fileList. Transfer process update of target trans identified
118  *                                       by the transId.
119  *   DFILE_ON_SESSION_IN_PROGRESS        transferUpdate. Transfer process update of the whole session.
120  *
121  *   DFILE_ON_FATAL_ERROR                errorCode. DFileSession cannot be used any more, and should be destroyed.
122  *   DFILE_ON_SESSION_TRANSFER_RATE      rate.
123  *
124  * It's invalid when for other message types.
125  */
126 typedef struct {
127     struct {
128         const char **files;
129         uint32_t fileNum;
130         uint16_t transId;
131         char *userData;
132     } fileList;
133     int32_t errorCode;
134     struct {
135         uint16_t transId;
136         uint64_t totalBytes;
137         uint64_t bytesTransferred;
138     } transferUpdate;
139     uint32_t rate;
140     struct sockaddr_in sockAddr[NSTACKX_MULTI_PATH_NUM];
141 } DFileMsg;
142 
143 typedef struct {
144     const char *files[NSTACKX_DFILE_MAX_FILE_NUM];
145     const char *remotePath[NSTACKX_DFILE_MAX_FILE_NUM];
146     uint32_t fileNum;
147     const char *userData;
148     uint16_t pathType;
149     uint8_t tarFlag : 1;
150     uint8_t smallFlag : 1;
151     uint8_t unuse : 6;
152 } NSTACKX_FilesInfo;
153 
154 typedef struct {
155     uint16_t rootPathType;
156     const char *initFileName;
157     char newFileName[NSTACKX_MAX_REMOTE_PATH_LEN];
158 } DFileRenamePara;
159 
160 /* Callback type for rename existing file */
161 typedef void (*OnDFileRenameFile)(DFileRenamePara *renamePara);
162 
163 /* Callback type for DFile session message receiver. */
164 typedef void (*DFileMsgReceiver)(int32_t sessionId, DFileMsgType msgType, const DFileMsg *msg);
165 
166 typedef struct {
167     struct sockaddr_in *addr;
168     socklen_t addrLen;
169     const uint8_t *key;
170     uint32_t keyLen;
171     DFileMsgReceiver msgReceiver;
172     const char *localInterfaceName;
173 } NSTACKX_SessionPara;
174 
175 typedef void (*DFileLogImpl)(const char *tag, uint32_t level, const char *format, va_list args);
176 
177 typedef struct {
178     struct sockaddr_in *addr;
179     socklen_t addrLen;
180     const char *localInterfaceName;
181 } NSTACKX_SessionParaMp;
182 
183 typedef struct {
184     struct sockaddr_in *addr;
185     socklen_t addrLen;
186 } NSTACKX_ServerParaMp;
187 
188 /*
189  * Create DFile server session.
190  * param: localAddr - filled with local ip addr, port and family for socket binding
191  *                    the ip addr and port must be host order
192  * param: addrLen - localAddr length
193  * param: key - key for data encrypt or decrypt, should be a 16 bytes string.
194  * param: msgReceiver - event callback for user
195  * return positive session id on success, negative value on failure
196  */
197 NSTACKX_EXPORT int32_t NSTACKX_DFileServer(struct sockaddr_in *localAddr, socklen_t addrLen, const uint8_t *key,
198                                            uint32_t keyLen, DFileMsgReceiver msgReceiver);
199 
200 
201 /*
202  * Create DFile client session.
203  * param: srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order
204  * param: addrLen - srvAddr length
205  * param: key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to transfer
206  *              file with crypto, or NULL if transfer without crypto.
207  * param: keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with crypto, or 0
208  *                 if transfer without crypto.
209  * param: msgReceiver - event callback for user
210  * return positive session id on success, negative value on failure
211  */
212 NSTACKX_EXPORT int32_t NSTACKX_DFileClient(struct sockaddr_in *srvAddr, socklen_t addrLen, const uint8_t *key,
213                                            uint32_t keyLen, DFileMsgReceiver msgReceiver);
214 
215 /*
216  * Create DFile client session on target device.
217  * param: sessionInfo->srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order
218  * param: sessionInfo->addrLen - srvAddr length
219  * param: sessionInfo->key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to
220  *                           transfer file with crypto, or NULL if transfer without crypto.
221  * param: sessionInfo->keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with
222  *                              crypto, or 0 if transfer without crypto.
223  * param: sessionInfo->msgReceiver - event callback for user
224  * param: sessionInfo->localInterfaceName - The full name of the target device for the socket of the session to bind.
225  *                                          If this param is NULL, the session's socket will bind to the device in the
226  *                                          same LAN with the srvAddr, but it may be inaccurate under certain
227  *                                          conditions. *
228  * return positive session id on success, negative value on failure
229  */
230 NSTACKX_EXPORT int32_t NSTACKX_DFileClientWithTargetDev(NSTACKX_SessionPara *sessionPara);
231 
232 /* Close session instance. */
233 NSTACKX_EXPORT void NSTACKX_DFileClose(int32_t sessionId);
234 
235 /*
236  * Start to send files by client session.
237  * param: files - File name list.to be sent.
238  * param: fileNum - Number of elements in "files".
239  * param: userData - user context data for each Send Files.
240  * return 0 on success, negative value on failure
241  */
242 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFiles(int32_t sessionId, const char *files[], uint32_t fileNum,
243                                               const char *userData);
244 
245 /*
246  * Start to send files by client session assign the storage dir of this files for server.
247  * param: files - File name list.to be sent.
248  * param: remotePath - The remote path(including file name) list of files for the server to save them.
249  * param: fileNum - Number of elements in "files".
250  * param: userData - user context data for each Send Files.
251  * return 0 on success, negative value on failure
252  */
253 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePath(int32_t sessionId, const char *files[],
254     const char *remotePath[], uint32_t fileNum, const char *userData);
255 
256 /*
257  * Start to send files by client session and assign the storage dir of this files for server. The storage dir contains
258  * a root path and a relative dir(including filename). The root dir is set by the server, who may has set more than and
259  * will chose one who has the same type as the filesInfo->pathType. The relative path is filesInfo->remotePath.
260  * param: filesInfo->files - File name list.to be sent.
261  * param: filesInf->remotePath - The remote path list of files.
262  * param: filesInfo->fileNum - Number of elements in "files".
263  * param: filesInfo->userData - User context data for each Send Files.
264  * param: filesInfo->pathType - The type of the fileInfo->files. It determines which root path the server will storage
265  *                              these files. Whats more, transfer will be failed if the server can't find the root path
266  *                              with tha same type.
267  * param: filesInfo->tarFlag - If it is true, this files will be packaged to a tar file and sent. Otherwise, they will
268  *                             be sent separately.
269  * param: filesInfo->smallflag - If it is true, means all files of the list are small files. the files will try to send
270  *                               with big files side-by-side.
271  * return 0 on success, negative value on failure
272  */
273 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePathAndType(int32_t sessionId, NSTACKX_FilesInfo *filesInfo);
274 
275 /*
276  * Set storage path for server session. Note that it will move the files that already recevied to this new path.
277  * New incoming files will also be save to here.
278  * return 0 on success, negative value on failure
279  */
280 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePath(int32_t sessionId, const char *path);
281 
282 /*
283  * Set callback for server to rename a new file when there is an existing file with the same dir and name.
284  * If this is not called, the existing file will be overwritte by the new received file with the same dir and name.
285  */
286 NSTACKX_EXPORT int32_t NSTACKX_DFileSetRenameHook(int32_t sessionId, OnDFileRenameFile onRenameFile);
287 
288 /*
289  * Set storage paths for diffreant types of files for server session.
290  * Note that it will move the files that already recevied to this new path.
291  * For the same sessionId, only one of this interface and the NSTACKX_DFileSetStoragePath() can be used.
292  * New incoming files will also be save to one of these path who has the same type. If the incoming files't type isn't
293  * one of the pathType list, these files will be refused
294  * return 0 on success, negative value on failure
295  */
296 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePathList(int32_t sessionId, const char *path[], const uint16_t *pathType,
297                                                        uint16_t pathNum);
298 
299 /*
300  * Set the log implementation
301  */
302 NSTACKX_EXPORT int32_t NSTACKX_DFileRegisterLog(DFileLogImpl logImpl);
303 
304 /*
305  * Get/Set the DFILE capabilities
306  */
307 NSTACKX_EXPORT uint32_t NSTACKX_DFileGetCapabilities(void);
308 
309 NSTACKX_EXPORT int32_t NSTACKX_DFileSetCapabilities(uint32_t capabilities, uint32_t value);
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 #endif  /* #ifndef NSTACKX_DFILE_H */
316