• 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 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #ifdef NSTACKX_WITH_LITEOS
38 #define NSTACKX_DFILE_MAX_FILE_NUM 10
39 #else
40 #define NSTACKX_DFILE_MAX_FILE_NUM 500
41 #endif
42 #define NSTACKX_MAX_FILE_NAME_LEN 256
43 #define NSTACKX_MAX_PATH_LEN 256
44 #define NSTACKX_MAX_REMOTE_PATH_LEN 1024
45 /* Maximum sending list limit. Sending more than this value will fail. */
46 #ifdef NSTACKX_WITH_LITEOS
47 #define NSTACKX_MAX_FILE_LIST_NUM 10
48 #else
49 #define NSTACKX_MAX_FILE_LIST_NUM 500
50 #endif
51 #define NSTACKX_MAX_STORAGE_PATH_NUM 500
52 #define NSTACKX_RESERVED_TRANS_ID 0
53 #define NSTACKX_RESERVED_PATH_TYPE 0
54 #define NSTACKX_MULTI_PATH_NUM 2
55 /* 10MB VTRANS */
56 #define NSTACKX_VTRANS_DEFAULT_SIZE (10 * 1024 * 1024)
57 #define NSTACKX_VTRANS_STEP_SIZE (5 * 1024 * 1024)
58 #define NSTACKX_VTRANS_MAX_SIZE (1024 * 1024 * 1024)
59 
60 #define CAPS_MULTIPATH 4
61 
62 typedef struct DFileSession DFileSession;
63 
64 /* DFile session message type list. */
65 typedef enum {
66     DFILE_ON_CONNECT_SUCCESS = 1,
67     DFILE_ON_CONNECT_FAIL,
68     DFILE_ON_FILE_LIST_RECEIVED,
69     DFILE_ON_FILE_RECEIVE_SUCCESS,
70     DFILE_ON_FILE_RECEIVE_FAIL,
71     DFILE_ON_FILE_SEND_SUCCESS,
72     DFILE_ON_FILE_SEND_FAIL,
73     DFILE_ON_FATAL_ERROR,
74     DFILE_ON_SESSION_IN_PROGRESS,
75     DFILE_ON_TRANS_IN_PROGRESS,
76     DFILE_ON_SESSION_TRANSFER_RATE,
77     DFILE_ON_BIND,
78     DFILE_ON_CLEAR_POLICY_FILE_LIST,
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     CAPS_ALG_NORATE, // NoRate algorithm
88     CAPS_RESUMABLE_TRANS,
89     CAPS_ZEROCOPY,
90     /* add more capability here */
91     CAPS_MAX,
92 };
93 
94 #define NSTACKX_WLAN_CAT_2_4G               1U
95 #define NSTACKX_WLAN_CAT_5G                 2U
96 #define NSTACKX_WLAN_CAT_DIRECT             3U
97 #define NSTACKX_WLAN_CAT_TCP                4U
98 
99 #define NSTACKX_RECV_BUFFER_LEN  1600
100 
101 #define NBITS(n)                            (1U << (n))
102 #define NSTACKX_CAPS_UDP_GSO                NBITS(CAPS_UDP_GSO)
103 #define NSTACKX_CAPS_LINK_SEQUENCE          NBITS(CAPS_LINK_SEQUENCE)
104 #define NSTACKX_CAPS_WLAN_CATAGORY          NBITS(CAPS_WLAN_CATAGORY)
105 #define NSTACKX_CAPS_MULTIPATH              NBITS(CAPS_MULTIPATH)
106 
107 #define NSTACKX_CAPS_MASK                   (NBITS(CAPS_MAX) - 1)
108 
109 /*
110  * DFile session message data. User should fetch corresponding member variable based on message type:
111  *          DFileMsgType               Member variable
112  *   DFILE_ON_CONNECT_SUCCESS            none
113  *   DFILE_ON_CONNECT_FAIL               errorCode
114  *   DFILE_ON_FILE_LIST_RECEIVED         fileList
115  *   DFILE_ON_FILE_RECEIVE_SUCCESS       fileList and transferUpdate
116  *   DFILE_ON_FILE_RECEIVE_FAIL          fileList, errorCode and transferUpdate. fileList maybe empty, as not all the
117  *                                       file names are received.
118  *   DFILE_ON_FILE_SEND_SUCCESS          fileList and transferUpdate
119  *   DFILE_ON_FILE_SEND_FAIL             fileList, errorCode and transferUpdate.
120  *   DFILE_ON_TRANS_IN_PROGRESS          transferUpdate and fileList. Transfer process update of target trans identified
121  *                                       by the transId.
122  *   DFILE_ON_SESSION_IN_PROGRESS        transferUpdate. Transfer process update of the whole session.
123  *
124  *   DFILE_ON_FATAL_ERROR                errorCode. DFileSession cannot be used any more, and should be destroyed.
125  *   DFILE_ON_SESSION_TRANSFER_RATE      rate.
126  *
127  * It's invalid when for other message types.
128  */
129 typedef enum {
130     FILE_STAT_COMPLETE,     /* whole file transfered*/
131     FILE_STAT_NOT_COMPLETE, /*file start transfered but not whole*/
132     FILE_STAT_NOT_START,    /*file not start transfered*/
133     FILE_STAT_BUTT,
134 } DFileFileStat;
135 
136 typedef struct {
137     char *file;
138     DFileFileStat stat;
139 } DFileFileInfo;
140 
141 typedef struct {
142     struct {
143         const char **files;
144         uint32_t fileNum;
145         uint16_t transId;
146         char *userData;
147     } fileList;
148     struct {
149         uint32_t fileNum;
150         const DFileFileInfo *fileInfo;
151     } clearPolicyFileList;
152     struct {
153         uint16_t transId;
154         uint64_t totalBytes;
155         uint64_t bytesTransferred;
156     } transferUpdate;
157     int32_t errorCode;
158     uint32_t rate;
159     struct sockaddr_in sockAddr[NSTACKX_MULTI_PATH_NUM];
160 } DFileMsg;
161 
162 typedef struct {
163     const char *files[NSTACKX_DFILE_MAX_FILE_NUM];
164     const char *remotePath[NSTACKX_DFILE_MAX_FILE_NUM];
165     const char *userData;
166     uint32_t fileNum;
167     uint16_t pathType;
168     uint8_t tarFlag : 1;
169     uint8_t smallFlag : 1;
170     uint8_t unuse : 6;
171 } NSTACKX_FilesInfo;
172 
173 typedef struct {
174     uint16_t rootPathType;
175     const char *initFileName;
176     char newFileName[NSTACKX_MAX_REMOTE_PATH_LEN];
177 } DFileRenamePara;
178 
179 /* Callback type for rename existing file */
180 typedef void (*OnDFileRenameFile)(DFileRenamePara *renamePara);
181 
182 /* Callback type for DFile session message receiver. */
183 typedef void (*DFileMsgReceiver)(int32_t sessionId, DFileMsgType msgType, const DFileMsg *msg);
184 
185 typedef struct {
186     struct sockaddr_in *addr;
187     socklen_t addrLen;
188     const uint8_t *key;
189     uint32_t keyLen;
190     DFileMsgReceiver msgReceiver;
191     const char *localInterfaceName;
192 } NSTACKX_SessionPara;
193 
194 typedef void (*DFileLogImpl)(const char *tag, uint32_t level, const char *format, va_list args);
195 
196 typedef struct {
197     struct sockaddr_in *addr;
198     socklen_t addrLen;
199     const char *localInterfaceName;
200 } NSTACKX_SessionParaMp;
201 
202 typedef struct {
203     struct sockaddr_in *addr;
204     socklen_t addrLen;
205 } NSTACKX_ServerParaMp;
206 
207 /* nStack HIEVENT接口设计 */
208 typedef enum {
209     DFile_EVENT_TYPE_FAULT,
210     DFile_EVENT_TYPE_STATISTIC,
211     DFile_EVENT_TYPE_SECURITY,
212     DFile_EVENT_TYPE_BEHAVIOR,
213 } DFileEventType;
214 
215 typedef enum {
216     DFile_EVENT_LEVEL_CRITICAL,
217     DFile_EVENT_LEVEL_MINOR,
218 } DFileEventLevel;
219 
220 typedef enum {
221     DFile_PARAM_TYPE_BOOL,
222     DFile_PARAM_TYPE_UINT8,
223     DFile_PARAM_TYPE_UINT16,
224     DFile_PARAM_TYPE_INT32,
225     DFile_PARAM_TYPE_UINT32,
226     DFile_PARAM_TYPE_UINT64,
227     DFile_PARAM_TYPE_FLOAT,
228     DFile_PARAM_TYPE_DOUBLE,
229     DFile_PARAM_TYPE_STRING
230 } DFileEventParamType;
231 
232 enum {
233     DFILE_LOG_LEVEL_OFF     = 0,
234     DFILE_LOG_LEVEL_FATAL   = 1,
235     DFILE_LOG_LEVEL_ERROR   = 2,
236     DFILE_LOG_LEVEL_WARNING = 3,
237     DFILE_LOG_LEVEL_INFO    = 4,
238     DFILE_LOG_LEVEL_DEBUG   = 5,
239     DFILE_LOG_LEVEL_END,
240 };
241 
242 #define DFile_EVENT_NAME_LEN 33
243 #define DFile_EVENT_TAG_LEN 16
244 
245 typedef struct {
246     DFileEventParamType type;
247     char name[DFile_EVENT_NAME_LEN];
248     union {
249         uint8_t u8v;
250         uint16_t u16v;
251         int32_t i32v;
252         uint32_t u32v;
253         uint64_t u64v;
254         float f;
255         double d;
256         char str[DFile_EVENT_NAME_LEN];
257     } value;
258 } DFileEventParam;
259 
260 typedef struct {
261     char eventName[DFile_EVENT_NAME_LEN];
262     DFileEventType type;
263     DFileEventLevel level;
264     uint32_t paramNum;
265     DFileEventParam *params;
266 } DFileEvent;
267 
268 /*
269  * Create DFile server session.
270  * param: localAddr - filled with local ip addr, port and family for socket binding
271  *                    the ip addr and port must be host order
272  * param: addrLen - localAddr length
273  * param: key - key for data encrypt or decrypt, should be a 16 bytes string.
274  * param: msgReceiver - event callback for user
275  * return positive session id on success, negative value on failure
276  */
277 NSTACKX_EXPORT int32_t NSTACKX_DFileServer(struct sockaddr_in *localAddr, socklen_t addrLen, const uint8_t *key,
278                                            uint32_t keyLen, DFileMsgReceiver msgReceiver);
279 
280 
281 /*
282  * Create DFile client session.
283  * param: srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order
284  * param: addrLen - srvAddr length
285  * param: key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to transfer
286  *              file with crypto, or NULL if transfer without crypto.
287  * param: keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with crypto, or 0
288  *                 if transfer without crypto.
289  * param: msgReceiver - event callback for user
290  * return positive session id on success, negative value on failure
291  */
292 NSTACKX_EXPORT int32_t NSTACKX_DFileClient(struct sockaddr_in *srvAddr, socklen_t addrLen, const uint8_t *key,
293                                            uint32_t keyLen, DFileMsgReceiver msgReceiver);
294 
295 /*
296  * Create DFile client session on target device.
297  * param: sessionInfo->srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order
298  * param: sessionInfo->addrLen - srvAddr length
299  * param: sessionInfo->key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to
300  *                           transfer file with crypto, or NULL if transfer without crypto.
301  * param: sessionInfo->keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with
302  *                              crypto, or 0 if transfer without crypto.
303  * param: sessionInfo->msgReceiver - event callback for user
304  * param: sessionInfo->localInterfaceName - The full name of the target device for the socket of the session to bind.
305  *                                          If this param is NULL, the session's socket will bind to the device in the
306  *                                          same LAN with the srvAddr, but it may be inaccurate under certain
307  *                                          conditions. *
308  * return positive session id on success, negative value on failure
309  */
310 NSTACKX_EXPORT int32_t NSTACKX_DFileClientWithTargetDev(NSTACKX_SessionPara *sessionPara);
311 
312 /* Close session instance. */
313 NSTACKX_EXPORT void NSTACKX_DFileClose(int32_t sessionId);
314 
315 /*
316  * Start to send files by client session.
317  * param: files - File name list.to be sent.
318  * param: fileNum - Number of elements in "files".
319  * param: userData - user context data for each Send Files.
320  * return 0 on success, negative value on failure
321  */
322 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFiles(int32_t sessionId, const char *files[], uint32_t fileNum,
323                                               const char *userData);
324 
325 /*
326  * Start to send files by client session assign the storage dir of this files for server.
327  * param: files - File name list.to be sent.
328  * param: remotePath - The remote path(including file name) list of files for the server to save them.
329  * param: fileNum - Number of elements in "files".
330  * param: userData - user context data for each Send Files.
331  * return 0 on success, negative value on failure
332  */
333 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePath(int32_t sessionId, const char *files[],
334     const char *remotePath[], uint32_t fileNum, const char *userData);
335 
336 /*
337  * Start to send files by client session and assign the storage dir of this files for server. The storage dir contains
338  * a root path and a relative dir(including filename). The root dir is set by the server, who may has set more than and
339  * will chose one who has the same type as the filesInfo->pathType. The relative path is filesInfo->remotePath.
340  * param: filesInfo->files - File name list.to be sent.
341  * param: filesInf->remotePath - The remote path list of files.
342  * param: filesInfo->fileNum - Number of elements in "files".
343  * param: filesInfo->userData - User context data for each Send Files.
344  * param: filesInfo->pathType - The type of the fileInfo->files. It determines which root path the server will storage
345  *                              these files. Whats more, transfer will be failed if the server can't find the root path
346  *                              with tha same type.
347  * param: filesInfo->tarFlag - If it is true, this files will be packaged to a tar file and sent. Otherwise, they will
348  *                             be sent separately.
349  * param: filesInfo->smallflag - If it is true, means all files of the list are small files. the files will try to send
350  *                               with big files side-by-side.
351  * return 0 on success, negative value on failure
352  */
353 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePathAndType(int32_t sessionId, NSTACKX_FilesInfo *filesInfo);
354 
355 /*
356  * Set storage path for server session. Note that it will move the files that already recevied to this new path.
357  * New incoming files will also be save to here.
358  * return 0 on success, negative value on failure
359  */
360 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePath(int32_t sessionId, const char *path);
361 
362 /*
363  * Set callback for server to rename a new file when there is an existing file with the same dir and name.
364  * If this is not called, the existing file will be overwritte by the new received file with the same dir and name.
365  */
366 NSTACKX_EXPORT int32_t NSTACKX_DFileSetRenameHook(int32_t sessionId, OnDFileRenameFile onRenameFile);
367 
368 /*
369  * Set storage paths for diffreant types of files for server session.
370  * Note that it will move the files that already recevied to this new path.
371  * For the same sessionId, only one of this interface and the NSTACKX_DFileSetStoragePath() can be used.
372  * 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
373  * one of the pathType list, these files will be refused
374  * return 0 on success, negative value on failure
375  */
376 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePathList(int32_t sessionId, const char *path[], const uint16_t *pathType,
377                                                        uint16_t pathNum);
378 
379 /*
380  * Set the log implementation
381  */
382 NSTACKX_EXPORT int32_t NSTACKX_DFileRegisterLog(DFileLogImpl logImpl);
383 
384 /*
385  * Get/Set the DFILE capabilities
386  */
387 NSTACKX_EXPORT uint32_t NSTACKX_DFileGetCapabilities(void);
388 
389 NSTACKX_EXPORT int32_t NSTACKX_DFileSetCapabilities(uint32_t capabilities, uint32_t value);
390 
391 typedef void (*DFileDumpFunc)(void *softObj, const char *data, uint32_t len);
392 
393 NSTACKX_EXPORT int32_t NSTACKX_DFileDump(uint32_t argc, const char **arg, void *softObj, DFileDumpFunc dump);
394 
395 /* 软总线提供的回调支持多线程调用,事件的触发频率要求(表格整理出来,什么时候触发,触发频率) */
396 typedef void (*DFileEventFunc)(void *softObj, const DFileEvent *info);
397 
398 NSTACKX_EXPORT void NSTACKX_DFileSetEventFunc(void *softObj, DFileEventFunc func);
399 
400 typedef void (*DFileLogCallback)(const char *moduleName, uint32_t logLevel, const char *format, ...);
401 
402 /*
403  * Set the DFile log implementation
404  */
405 NSTACKX_EXPORT int32_t NSTACKX_DFileRegisterLogCallback(DFileLogCallback userLogCallback);
406 NSTACKX_EXPORT void NSTACKX_DFileRegisterDefaultLog(void);
407 
408 /**
409  * get DFILE_ON_CLEAR_POLICY_FILE_LIST event callback.
410  * @brief Gets file list with file state
411  * @param[in] sessionId the session id of the session
412  * @return 0 on success, negative value on failure
413  */
414 NSTACKX_EXPORT int32_t NSTACKX_DFileSessionGetFileList(int32_t sessionId);
415 
416 typedef enum {
417     /* the priority of socket, value is same as IP_TOS, vallen is siezeof(uint8_t) */
418     OPT_TYPE_SOCK_PRIO,
419     OPT_TYPE_BUTT
420 } DFileOptType;
421 
422 typedef struct {
423     DFileOptType optType;
424     uint32_t valLen; /* length of value */
425     uint64_t value;  /* the option value, could be a pointer */
426 } DFileOpt;
427 
428 /*
429  * set dfile session opt
430  * @brief Sets DFile session options. for client session, Recommend to configure after DFILE_ON_CONNECT_SUCCESS.
431  * @param[in] sessionId the session id of the session
432  * @param[in] opt option tlv
433  * @return 0 on success, negative value on failure
434  */
435 NSTACKX_EXPORT int32_t NSTACKX_DFileSetSessionOpt(int32_t sessionId, const DFileOpt *opt);
436 #ifdef __cplusplus
437 }
438 #endif
439 
440 #endif  /* #ifndef NSTACKX_DFILE_H */
441