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 } DFileMsgType; 79 80 enum { 81 CAPS_UDP_GSO = 0, 82 CAPS_LINK_SEQUENCE, 83 CAPS_WLAN_CATAGORY, 84 CAPS_NO_RTT, 85 CAPS_RESERVED, /* for multipath check of old version */ 86 CAPS_ALG_NORATE, // NoRate algorithm 87 CAPS_RESUMABLE_TRANS, 88 CAPS_ZEROCOPY, 89 CAPS_CHACHA_CIPHRE, 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 #define NSTACKX_CAPS_CHACHA NBITS(CAPS_CHACHA_CIPHRE) 107 108 #define NSTACKX_CAPS_MASK (NBITS(CAPS_MAX) - 1) 109 110 /* 111 * DFile session message data. User should fetch corresponding member variable based on message type: 112 * DFileMsgType Member variable 113 * DFILE_ON_CONNECT_SUCCESS none 114 * DFILE_ON_CONNECT_FAIL errorCode 115 * DFILE_ON_FILE_LIST_RECEIVED fileList 116 * DFILE_ON_FILE_RECEIVE_SUCCESS fileList and transferUpdate 117 * DFILE_ON_FILE_RECEIVE_FAIL fileList, errorCode and transferUpdate. fileList maybe empty, as not all the 118 * file names are received. 119 * DFILE_ON_FILE_SEND_SUCCESS fileList and transferUpdate 120 * DFILE_ON_FILE_SEND_FAIL fileList, errorCode and transferUpdate. 121 * DFILE_ON_TRANS_IN_PROGRESS transferUpdate and fileList. Transfer process update of target trans identified 122 * by the transId. 123 * DFILE_ON_SESSION_IN_PROGRESS transferUpdate. Transfer process update of the whole session. 124 * 125 * DFILE_ON_FATAL_ERROR errorCode. DFileSession cannot be used any more, and should be destroyed. 126 * DFILE_ON_SESSION_TRANSFER_RATE rate. 127 * 128 * It's invalid when for other message types. 129 */ 130 typedef struct { 131 struct { 132 const char **files; 133 uint32_t fileNum; 134 uint16_t transId; 135 char *userData; 136 } fileList; 137 int32_t errorCode; 138 struct { 139 uint16_t transId; 140 uint64_t totalBytes; 141 uint64_t bytesTransferred; 142 } transferUpdate; 143 uint32_t rate; 144 struct sockaddr_in sockAddr[NSTACKX_MULTI_PATH_NUM]; 145 } DFileMsg; 146 147 typedef struct { 148 const char *files[NSTACKX_DFILE_MAX_FILE_NUM]; 149 const char *remotePath[NSTACKX_DFILE_MAX_FILE_NUM]; 150 uint32_t fileNum; 151 const char *userData; 152 uint16_t pathType; 153 uint8_t tarFlag : 1; 154 uint8_t smallFlag : 1; 155 uint8_t unuse : 6; 156 } NSTACKX_FilesInfo; 157 158 typedef struct { 159 uint16_t rootPathType; 160 const char *initFileName; 161 char newFileName[NSTACKX_MAX_REMOTE_PATH_LEN]; 162 } DFileRenamePara; 163 164 /* Callback type for rename existing file */ 165 typedef void (*OnDFileRenameFile)(DFileRenamePara *renamePara); 166 167 /* Callback type for DFile session message receiver. */ 168 typedef void (*DFileMsgReceiver)(int32_t sessionId, DFileMsgType msgType, const DFileMsg *msg); 169 170 typedef struct { 171 struct sockaddr_in *addr; 172 socklen_t addrLen; 173 const uint8_t *key; 174 uint32_t keyLen; 175 DFileMsgReceiver msgReceiver; 176 const char *localInterfaceName; 177 } NSTACKX_SessionPara; 178 179 typedef void (*DFileLogImpl)(const char *tag, uint32_t level, const char *format, va_list args); 180 181 typedef struct { 182 struct sockaddr_in *addr; 183 socklen_t addrLen; 184 const char *localInterfaceName; 185 } NSTACKX_SessionParaMp; 186 187 typedef struct { 188 struct sockaddr_in *addr; 189 socklen_t addrLen; 190 } NSTACKX_ServerParaMp; 191 192 /* nStack HIEVENT接口设计 */ 193 typedef enum { 194 DFile_EVENT_TYPE_FAULT, 195 DFile_EVENT_TYPE_STATISTIC, 196 DFile_EVENT_TYPE_SECURITY, 197 DFile_EVENT_TYPE_BEHAVIOR, 198 } DFileEventType; 199 200 typedef enum { 201 DFile_EVENT_LEVEL_CRITICAL, 202 DFile_EVENT_LEVEL_MINOR, 203 } DFileEventLevel; 204 205 typedef enum { 206 DFile_PARAM_TYPE_BOOL, 207 DFile_PARAM_TYPE_UINT8, 208 DFile_PARAM_TYPE_UINT16, 209 DFile_PARAM_TYPE_INT32, 210 DFile_PARAM_TYPE_UINT32, 211 DFile_PARAM_TYPE_UINT64, 212 DFile_PARAM_TYPE_FLOAT, 213 DFile_PARAM_TYPE_DOUBLE, 214 DFile_PARAM_TYPE_STRING 215 } DFileEventParamType; 216 217 enum { 218 DFILE_LOG_LEVEL_OFF = 0, 219 DFILE_LOG_LEVEL_FATAL = 1, 220 DFILE_LOG_LEVEL_ERROR = 2, 221 DFILE_LOG_LEVEL_WARNING = 3, 222 DFILE_LOG_LEVEL_INFO = 4, 223 DFILE_LOG_LEVEL_DEBUG = 5, 224 DFILE_LOG_LEVEL_END, 225 }; 226 227 #define DFile_EVENT_NAME_LEN 33 228 #define DFile_EVENT_TAG_LEN 16 229 230 typedef struct { 231 DFileEventParamType type; 232 char name[DFile_EVENT_NAME_LEN]; 233 union { 234 uint8_t u8v; 235 uint16_t u16v; 236 int32_t i32v; 237 uint32_t u32v; 238 uint64_t u64v; 239 float f; 240 double d; 241 char str[DFile_EVENT_NAME_LEN]; 242 } value; 243 } DFileEventParam; 244 245 typedef struct { 246 char eventName[DFile_EVENT_NAME_LEN]; 247 DFileEventType type; 248 DFileEventLevel level; 249 uint32_t paramNum; 250 DFileEventParam *params; 251 } DFileEvent; 252 253 /* 254 * Create DFile server session. 255 * param: localAddr - filled with local ip addr, port and family for socket binding 256 * the ip addr and port must be host order 257 * param: addrLen - localAddr length 258 * param: key - key for data encrypt or decrypt, should be a 16 bytes string. 259 * param: msgReceiver - event callback for user 260 * return positive session id on success, negative value on failure 261 */ 262 NSTACKX_EXPORT int32_t NSTACKX_DFileServer(struct sockaddr_in *localAddr, socklen_t addrLen, const uint8_t *key, 263 uint32_t keyLen, DFileMsgReceiver msgReceiver); 264 265 266 /* 267 * Create DFile client session. 268 * param: srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order 269 * param: addrLen - srvAddr length 270 * param: key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to transfer 271 * file with crypto, or NULL if transfer without crypto. 272 * param: keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with crypto, or 0 273 * if transfer without crypto. 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_DFileClient(struct sockaddr_in *srvAddr, socklen_t addrLen, const uint8_t *key, 278 uint32_t keyLen, DFileMsgReceiver msgReceiver); 279 280 /* 281 * Create DFile client session on target device. 282 * param: sessionInfo->srvAddr - filled with remote ip addr, port and family, the ip addr and port must be host order 283 * param: sessionInfo->addrLen - srvAddr length 284 * param: sessionInfo->key - key for data encrypt or decrypt. It should be a 16 bytes buffer if this session is used to 285 * transfer file with crypto, or NULL if transfer without crypto. 286 * param: sessionInfo->keyLen - keyLen for the key. It should be 16 if this session is used to transfer file with 287 * crypto, or 0 if transfer without crypto. 288 * param: sessionInfo->msgReceiver - event callback for user 289 * param: sessionInfo->localInterfaceName - The full name of the target device for the socket of the session to bind. 290 * If this param is NULL, the session's socket will bind to the device in the 291 * same LAN with the srvAddr, but it may be inaccurate under certain 292 * conditions. * 293 * return positive session id on success, negative value on failure 294 */ 295 NSTACKX_EXPORT int32_t NSTACKX_DFileClientWithTargetDev(NSTACKX_SessionPara *sessionPara); 296 297 /* Close session instance. */ 298 NSTACKX_EXPORT void NSTACKX_DFileClose(int32_t sessionId); 299 300 /* 301 * Start to send files by client session. 302 * param: files - File name list.to be sent. 303 * param: fileNum - Number of elements in "files". 304 * param: userData - user context data for each Send Files. 305 * return 0 on success, negative value on failure 306 */ 307 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFiles(int32_t sessionId, const char *files[], uint32_t fileNum, 308 const char *userData); 309 310 /* 311 * Start to send files by client session assign the storage dir of this files for server. 312 * param: files - File name list.to be sent. 313 * param: remotePath - The remote path(including file name) list of files for the server to save them. 314 * param: fileNum - Number of elements in "files". 315 * param: userData - user context data for each Send Files. 316 * return 0 on success, negative value on failure 317 */ 318 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePath(int32_t sessionId, const char *files[], 319 const char *remotePath[], uint32_t fileNum, const char *userData); 320 321 /* 322 * Start to send files by client session and assign the storage dir of this files for server. The storage dir contains 323 * a root path and a relative dir(including filename). The root dir is set by the server, who may has set more than and 324 * will chose one who has the same type as the filesInfo->pathType. The relative path is filesInfo->remotePath. 325 * param: filesInfo->files - File name list.to be sent. 326 * param: filesInf->remotePath - The remote path list of files. 327 * param: filesInfo->fileNum - Number of elements in "files". 328 * param: filesInfo->userData - User context data for each Send Files. 329 * param: filesInfo->pathType - The type of the fileInfo->files. It determines which root path the server will storage 330 * these files. Whats more, transfer will be failed if the server can't find the root path 331 * with tha same type. 332 * param: filesInfo->tarFlag - If it is true, this files will be packaged to a tar file and sent. Otherwise, they will 333 * be sent separately. 334 * param: filesInfo->smallflag - If it is true, means all files of the list are small files. the files will try to send 335 * with big files side-by-side. 336 * return 0 on success, negative value on failure 337 */ 338 NSTACKX_EXPORT int32_t NSTACKX_DFileSendFilesWithRemotePathAndType(int32_t sessionId, NSTACKX_FilesInfo *filesInfo); 339 340 /* 341 * Set storage path for server session. Note that it will move the files that already recevied to this new path. 342 * New incoming files will also be save to here. 343 * return 0 on success, negative value on failure 344 */ 345 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePath(int32_t sessionId, const char *path); 346 347 /* 348 * Set callback for server to rename a new file when there is an existing file with the same dir and name. 349 * If this is not called, the existing file will be overwritte by the new received file with the same dir and name. 350 */ 351 NSTACKX_EXPORT int32_t NSTACKX_DFileSetRenameHook(int32_t sessionId, OnDFileRenameFile onRenameFile); 352 353 /* 354 * Set storage paths for diffreant types of files for server session. 355 * Note that it will move the files that already recevied to this new path. 356 * For the same sessionId, only one of this interface and the NSTACKX_DFileSetStoragePath() can be used. 357 * 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 358 * one of the pathType list, these files will be refused 359 * return 0 on success, negative value on failure 360 */ 361 NSTACKX_EXPORT int32_t NSTACKX_DFileSetStoragePathList(int32_t sessionId, const char *path[], const uint16_t *pathType, 362 uint16_t pathNum); 363 364 /* 365 * Set the log implementation 366 */ 367 NSTACKX_EXPORT int32_t NSTACKX_DFileRegisterLog(DFileLogImpl logImpl); 368 369 /* 370 * Get/Set the DFILE capabilities 371 */ 372 NSTACKX_EXPORT uint32_t NSTACKX_DFileGetCapabilities(void); 373 374 NSTACKX_EXPORT int32_t NSTACKX_DFileSetCapabilities(uint32_t capabilities, uint32_t value); 375 376 typedef void (*DFileDumpFunc)(void *softObj, const char *data, uint32_t len); 377 378 NSTACKX_EXPORT int32_t NSTACKX_DFileDump(uint32_t argc, const char **arg, void *softObj, DFileDumpFunc dump); 379 380 /* 软总线提供的回调支持多线程调用,事件的触发频率要求(表格整理出来,什么时候触发,触发频率) */ 381 typedef void (*DFileEventFunc)(void *softObj, const DFileEvent *info); 382 383 NSTACKX_EXPORT void NSTACKX_DFileSetEventFunc(void *softObj, DFileEventFunc func); 384 385 typedef void (*DFileLogCallback)(const char *moduleName, uint32_t logLevel, const char *format, ...); 386 387 /* 388 * Set the DFile log implementation 389 */ 390 NSTACKX_EXPORT int32_t NSTACKX_DFileRegisterLogCallback(DFileLogCallback userLogCallback); 391 NSTACKX_EXPORT void NSTACKX_DFileRegisterDefaultLog(void); 392 393 #ifdef __cplusplus 394 } 395 #endif 396 397 #endif /* #ifndef NSTACKX_DFILE_H */ 398