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 #include "lnn_distributed_net_ledger_common.h"
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include <securec.h>
24 
25 #include "lnn_event.h"
26 #include "anonymizer.h"
27 #include "auth_common.h"
28 #include "auth_deviceprofile.h"
29 #include "lnn_connection_addr_utils.h"
30 #include "lnn_fast_offline.h"
31 #include "lnn_map.h"
32 #include "lnn_node_info.h"
33 #include "lnn_lane_def.h"
34 #include "lnn_deviceinfo_to_profile.h"
35 #include "lnn_device_info_recovery.h"
36 #include "lnn_feature_capability.h"
37 #include "lnn_local_net_ledger.h"
38 #include "lnn_log.h"
39 #include "softbus_adapter_mem.h"
40 #include "softbus_adapter_thread.h"
41 #include "softbus_adapter_crypto.h"
42 #include "softbus_bus_center.h"
43 #include "softbus_def.h"
44 #include "softbus_errcode.h"
45 #include "softbus_adapter_crypto.h"
46 #include "softbus_utils.h"
47 #include "softbus_hidumper_buscenter.h"
48 #include "bus_center_manager.h"
49 #include "softbus_hisysevt_bus_center.h"
50 #include "bus_center_event.h"
51 
52 DistributedNetLedger g_distributedNetLedger;
53 
LnnGetDistributedNetLedger(void)54 DistributedNetLedger* LnnGetDistributedNetLedger(void)
55 {
56     return &g_distributedNetLedger;
57 }
58 
UpdateNetworkInfo(const char * udid)59 static void UpdateNetworkInfo(const char *udid)
60 {
61     NodeBasicInfo basic;
62     if (memset_s(&basic, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo)) != EOK) {
63         LNN_LOGE(LNN_LEDGER, "memset_s basic fail!");
64     }
65     if (LnnGetBasicInfoByUdid(udid, &basic) != SOFTBUS_OK) {
66         LNN_LOGE(LNN_LEDGER, "GetBasicInfoByUdid fail.");
67         return;
68     }
69     LnnNotifyBasicInfoChanged(&basic, TYPE_NETWORK_INFO);
70 }
71 
UpdateDeviceNameInfo(const char * udid,const char * oldDeviceName)72 static void UpdateDeviceNameInfo(const char *udid, const char *oldDeviceName)
73 {
74     NodeBasicInfo basic;
75     if (memset_s(&basic, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo)) != EOK) {
76         LNN_LOGE(LNN_LEDGER, "memset_s basic fail!");
77     }
78     if (LnnGetBasicInfoByUdid(udid, &basic) != SOFTBUS_OK) {
79         LNN_LOGE(LNN_LEDGER, "GetBasicInfoByUdid fail.");
80         return;
81     }
82     char *anonyOldDeviceName = NULL;
83     Anonymize(oldDeviceName, &anonyOldDeviceName);
84     char *anonyDeviceName = NULL;
85     Anonymize(basic.deviceName, &anonyDeviceName);
86     LNN_LOGI(LNN_LEDGER, "report deviceName update, name:%{public}s -> %{public}s.",
87         anonyOldDeviceName, anonyDeviceName);
88     AnonymizeFree(anonyOldDeviceName);
89     AnonymizeFree(anonyDeviceName);
90     LnnNotifyBasicInfoChanged(&basic, TYPE_DEVICE_NAME);
91 }
92 
LnnSetAuthTypeValue(uint32_t * authTypeValue,AuthType type)93 int32_t LnnSetAuthTypeValue(uint32_t *authTypeValue, AuthType type)
94 {
95     if (authTypeValue == NULL || type >= AUTH_TYPE_BUTT) {
96         LNN_LOGE(LNN_LEDGER, "in para error!");
97         return SOFTBUS_INVALID_PARAM;
98     }
99     *authTypeValue = (*authTypeValue) | (1 << (uint32_t)type);
100     return SOFTBUS_OK;
101 }
102 
LnnClearAuthTypeValue(uint32_t * authTypeValue,AuthType type)103 int32_t LnnClearAuthTypeValue(uint32_t *authTypeValue, AuthType type)
104 {
105     if (authTypeValue == NULL || type >= AUTH_TYPE_BUTT) {
106         LNN_LOGE(LNN_LEDGER, "in para error!");
107         return SOFTBUS_INVALID_PARAM;
108     }
109     *authTypeValue = (*authTypeValue) & (~(1 << (uint32_t)type));
110     return SOFTBUS_OK;
111 }
112 
GetNodeInfoFromMap(const DoubleHashMap * map,const char * id)113 NodeInfo *GetNodeInfoFromMap(const DoubleHashMap *map, const char *id)
114 {
115     if (map == NULL || id == NULL) {
116         LNN_LOGE(LNN_LEDGER, "para error!");
117         return NULL;
118     }
119     NodeInfo *info = NULL;
120     if ((info = (NodeInfo *)LnnMapGet(&map->udidMap, id)) != NULL) {
121         return info;
122     }
123     if ((info = (NodeInfo *)LnnMapGet(&map->macMap, id)) != NULL) {
124         return info;
125     }
126     if ((info = (NodeInfo *)LnnMapGet(&map->ipMap, id)) != NULL) {
127         return info;
128     }
129     LNN_LOGE(LNN_LEDGER, "id not exist!");
130     return NULL;
131 }
132 
InitDistributedInfo(DoubleHashMap * map)133 static int32_t InitDistributedInfo(DoubleHashMap *map)
134 {
135     if (map == NULL) {
136         LNN_LOGE(LNN_LEDGER, "fail:para error!");
137         return SOFTBUS_INVALID_PARAM;
138     }
139     LnnMapInit(&map->udidMap);
140     LnnMapInit(&map->ipMap);
141     LnnMapInit(&map->macMap);
142     return SOFTBUS_OK;
143 }
144 
DeinitDistributedInfo(DoubleHashMap * map)145 static void DeinitDistributedInfo(DoubleHashMap *map)
146 {
147     if (map == NULL) {
148         LNN_LOGE(LNN_LEDGER, "fail: para error!");
149         return;
150     }
151     LnnMapDelete(&map->udidMap);
152     LnnMapDelete(&map->ipMap);
153     LnnMapDelete(&map->macMap);
154 }
155 
InitConnectionCode(ConnectionCode * cnnCode)156 static int32_t InitConnectionCode(ConnectionCode *cnnCode)
157 {
158     if (cnnCode == NULL) {
159         LNN_LOGE(LNN_LEDGER, "fail: para error!");
160         return SOFTBUS_INVALID_PARAM;
161     }
162     LnnMapInit(&cnnCode->connectionCode);
163     return SOFTBUS_OK;
164 }
165 
DeinitConnectionCode(ConnectionCode * cnnCode)166 static void DeinitConnectionCode(ConnectionCode *cnnCode)
167 {
168     if (cnnCode == NULL) {
169         LNN_LOGE(LNN_LEDGER, "fail: para error!");
170         return;
171     }
172     LnnMapDelete(&cnnCode->connectionCode);
173     return;
174 }
175 
LnnDeinitDistributedLedger(void)176 void LnnDeinitDistributedLedger(void)
177 {
178     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
179         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
180         return;
181     }
182     g_distributedNetLedger.status = DL_INIT_UNKNOWN;
183     DeinitDistributedInfo(&g_distributedNetLedger.distributedInfo);
184     DeinitConnectionCode(&g_distributedNetLedger.cnnCode);
185     if (SoftBusMutexUnlock(&g_distributedNetLedger.lock) != 0) {
186         LNN_LOGE(LNN_LEDGER, "unlock mutex fail!");
187     }
188     SoftBusMutexDestroy(&g_distributedNetLedger.lock);
189 }
190 
NewWifiDiscovered(const NodeInfo * oldInfo,NodeInfo * newInfo)191 static void NewWifiDiscovered(const NodeInfo *oldInfo, NodeInfo *newInfo)
192 {
193     const char *macAddr = NULL;
194     if (oldInfo == NULL || newInfo == NULL) {
195         LNN_LOGE(LNN_LEDGER, "para error!");
196         return;
197     }
198     newInfo->discoveryType = newInfo->discoveryType | oldInfo->discoveryType;
199     newInfo->stateVersion = oldInfo->stateVersion;
200     macAddr = LnnGetBtMac(newInfo);
201     if (macAddr == NULL) {
202         LNN_LOGE(LNN_LEDGER, "LnnGetBtMac Fail!");
203         return;
204     }
205     if (strcmp(macAddr, DEFAULT_MAC) == 0) {
206         LnnSetBtMac(newInfo, LnnGetBtMac(oldInfo));
207     }
208 }
209 
NewBrBleDiscovered(const NodeInfo * oldInfo,NodeInfo * newInfo)210 static void NewBrBleDiscovered(const NodeInfo *oldInfo, NodeInfo *newInfo)
211 {
212     const char *ipAddr = NULL;
213     if (oldInfo == NULL || newInfo == NULL) {
214         LNN_LOGE(LNN_LEDGER, "para error!");
215         return;
216     }
217     newInfo->discoveryType = newInfo->discoveryType | oldInfo->discoveryType;
218     ipAddr = LnnGetWiFiIp(newInfo);
219     if (ipAddr == NULL) {
220         LNN_LOGE(LNN_LEDGER, "LnnGetWiFiIp Fail!");
221         return;
222     }
223     if (strcmp(ipAddr, DEFAULT_IP) == 0 || strcmp(ipAddr, LOCAL_IP) == 0) {
224         LnnSetWiFiIp(newInfo, LnnGetWiFiIp(oldInfo));
225     }
226 
227     newInfo->connectInfo.authPort = oldInfo->connectInfo.authPort;
228     newInfo->connectInfo.proxyPort = oldInfo->connectInfo.proxyPort;
229     newInfo->connectInfo.sessionPort = oldInfo->connectInfo.sessionPort;
230 }
231 
RetainOfflineCode(const NodeInfo * oldInfo,NodeInfo * newInfo)232 static void RetainOfflineCode(const NodeInfo *oldInfo, NodeInfo *newInfo)
233 {
234     if (oldInfo == NULL || newInfo == NULL) {
235         LNN_LOGE(LNN_LEDGER, "para error!");
236         return;
237     }
238     if (memcpy_s(newInfo->offlineCode, OFFLINE_CODE_BYTE_SIZE,
239         oldInfo->offlineCode, OFFLINE_CODE_BYTE_SIZE) != SOFTBUS_OK) {
240         LNN_LOGE(LNN_LEDGER, "memcpy offlineCode error!");
241         return;
242     }
243 }
ConvertNodeInfoToBasicInfo(const NodeInfo * info,NodeBasicInfo * basic)244 static int32_t ConvertNodeInfoToBasicInfo(const NodeInfo *info, NodeBasicInfo *basic)
245 {
246     if (info == NULL || basic == NULL) {
247         LNN_LOGE(LNN_LEDGER, "para error!");
248         return SOFTBUS_INVALID_PARAM;
249     }
250     if (strcpy_s(basic->deviceName, DEVICE_NAME_BUF_LEN, info->deviceInfo.deviceName) != EOK) {
251         LNN_LOGE(LNN_LEDGER, "strcpy_s name error!");
252         return SOFTBUS_MEM_ERR;
253     }
254     if (strcpy_s(basic->networkId, NETWORK_ID_BUF_LEN, info->networkId) != EOK) {
255         LNN_LOGE(LNN_LEDGER, "strcpy_s networkID error!");
256         return SOFTBUS_MEM_ERR;
257     }
258     if (strcpy_s(basic->osVersion, OS_VERSION_BUF_LEN, info->deviceInfo.osVersion) != EOK) {
259         LNN_LOGE(LNN_LEDGER, "strcpy_s osVersion error!");
260         return SOFTBUS_MEM_ERR;
261     }
262     basic->deviceTypeId = info->deviceInfo.deviceTypeId;
263     basic->osType = info->deviceInfo.osType;
264     return SOFTBUS_OK;
265 }
266 
IsMetaNode(NodeInfo * info)267 bool IsMetaNode(NodeInfo *info)
268 {
269     if (info == NULL) {
270         return false;
271     }
272     return info->metaInfo.isMetaNode;
273 }
274 
GetDLOnlineNodeNumLocked(int32_t * infoNum,bool isNeedMeta)275 static int32_t GetDLOnlineNodeNumLocked(int32_t *infoNum, bool isNeedMeta)
276 {
277     NodeInfo *info = NULL;
278     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
279     MapIterator *it = LnnMapInitIterator(&map->udidMap);
280 
281     if (it == NULL) {
282         return SOFTBUS_ERR;
283     }
284     *infoNum = 0;
285     while (LnnMapHasNext(it)) {
286         it = LnnMapNext(it);
287         if (it == NULL) {
288             return SOFTBUS_ERR;
289         }
290         info = (NodeInfo *)it->node->value;
291         if (!isNeedMeta) {
292             if (LnnIsNodeOnline(info)) {
293                 (*infoNum)++;
294             }
295         } else {
296             if (LnnIsNodeOnline(info) || IsMetaNode(info)) {
297                 (*infoNum)++;
298             }
299         }
300     }
301     LnnMapDeinitIterator(it);
302     return SOFTBUS_OK;
303 }
304 
FillDLOnlineNodeInfoLocked(NodeBasicInfo * info,int32_t infoNum,bool isNeedMeta)305 static int32_t FillDLOnlineNodeInfoLocked(NodeBasicInfo *info, int32_t infoNum, bool isNeedMeta)
306 {
307     NodeInfo *nodeInfo = NULL;
308     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
309     MapIterator *it = LnnMapInitIterator(&map->udidMap);
310     int32_t i = 0;
311 
312     if (it == NULL) {
313         LNN_LOGE(LNN_LEDGER, "it is null");
314         return SOFTBUS_ERR;
315     }
316     while (LnnMapHasNext(it) && i < infoNum) {
317         it = LnnMapNext(it);
318         if (it == NULL) {
319             LnnMapDeinitIterator(it);
320             return SOFTBUS_ERR;
321         }
322         nodeInfo = (NodeInfo *)it->node->value;
323         if (!isNeedMeta) {
324             if (LnnIsNodeOnline(nodeInfo)) {
325                 ConvertNodeInfoToBasicInfo(nodeInfo, info + i);
326                 ++i;
327             }
328         } else {
329             if (LnnIsNodeOnline(nodeInfo) || IsMetaNode(nodeInfo)) {
330                 ConvertNodeInfoToBasicInfo(nodeInfo, info + i);
331                 ++i;
332             }
333         }
334     }
335     LnnMapDeinitIterator(it);
336     return SOFTBUS_OK;
337 }
338 
IsNetworkIdChanged(NodeInfo * newInfo,NodeInfo * oldInfo)339 static bool IsNetworkIdChanged(NodeInfo *newInfo, NodeInfo *oldInfo)
340 {
341     if (newInfo == NULL || oldInfo == NULL) {
342         LNN_LOGE(LNN_LEDGER, "para error!");
343         return false;
344     }
345     if (strcmp(newInfo->networkId, oldInfo->networkId) == 0) {
346         return false;
347     }
348     return true;
349 }
350 
PostOnlineNodesToCb(const INodeStateCb * callBack)351 void PostOnlineNodesToCb(const INodeStateCb *callBack)
352 {
353     NodeInfo *info = NULL;
354     NodeBasicInfo basic;
355     if (memset_s(&basic, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo)) != EOK) {
356         LNN_LOGE(LNN_LEDGER, "memset_s basic fail!");
357     }
358     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
359     if (callBack->onNodeOnline == NULL) {
360         LNN_LOGE(LNN_LEDGER, "onNodeOnline IS null!");
361         return;
362     }
363     MapIterator *it = LnnMapInitIterator(&map->udidMap);
364     if (it == NULL) {
365         return;
366     }
367     while (LnnMapHasNext(it)) {
368         it = LnnMapNext(it);
369         if (it == NULL) {
370             return;
371         }
372         info = (NodeInfo *)it->node->value;
373         if (LnnIsNodeOnline(info)) {
374             ConvertNodeInfoToBasicInfo(info, &basic);
375             callBack->onNodeOnline(&basic);
376         }
377     }
378     LnnMapDeinitIterator(it);
379 }
380 
LnnGetNodeInfoById(const char * id,IdCategory type)381 NodeInfo *LnnGetNodeInfoById(const char *id, IdCategory type)
382 {
383     NodeInfo *info = NULL;
384     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
385     if (id == NULL) {
386         LNN_LOGE(LNN_LEDGER, "para error");
387         return info;
388     }
389     if (type == CATEGORY_UDID) {
390         return GetNodeInfoFromMap(map, id);
391     }
392     MapIterator *it = LnnMapInitIterator(&map->udidMap);
393     LNN_CHECK_AND_RETURN_RET_LOGE(it != NULL, NULL, LNN_LEDGER, "LnnMapInitIterator is null");
394 
395     while (LnnMapHasNext(it)) {
396         it = LnnMapNext(it);
397         LNN_CHECK_AND_RETURN_RET_LOGE(it != NULL, info, LNN_LEDGER, "it next is null");
398         info = (NodeInfo *)it->node->value;
399         if (info == NULL) {
400             continue;
401         }
402         if (type == CATEGORY_NETWORK_ID) {
403             if (strcmp(info->networkId, id) == 0 ||
404                 (strlen(info->lastNetworkId) != 0 && strcmp(info->lastNetworkId, id) == 0)) {
405                 LnnMapDeinitIterator(it);
406                 return info;
407             }
408         } else if (type == CATEGORY_UUID) {
409             if (strcmp(info->uuid, id) == 0) {
410                 LnnMapDeinitIterator(it);
411                 return info;
412             }
413         } else {
414             LNN_LOGE(LNN_LEDGER, "type error");
415         }
416     }
417     LnnMapDeinitIterator(it);
418     return NULL;
419 }
420 
LnnGetNodeInfoByDeviceId(const char * id)421 static NodeInfo *LnnGetNodeInfoByDeviceId(const char *id)
422 {
423     NodeInfo *info = NULL;
424     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
425     NodeInfo *udidInfo = GetNodeInfoFromMap(map, id);
426     if (udidInfo != NULL) {
427         return udidInfo;
428     }
429     MapIterator *it = LnnMapInitIterator(&map->udidMap);
430     if (it == NULL) {
431         return info;
432     }
433     while (LnnMapHasNext(it)) {
434         it = LnnMapNext(it);
435         if (it == NULL) {
436             return info;
437         }
438         info = (NodeInfo *)it->node->value;
439         if (info == NULL) {
440             continue;
441         }
442         if (strcmp(info->networkId, id) == 0 ||
443             (strlen(info->lastNetworkId) != 0 && strcmp(info->lastNetworkId, id) == 0)) {
444             LnnMapDeinitIterator(it);
445             return info;
446         }
447         if (strcmp(info->uuid, id) == 0) {
448             LnnMapDeinitIterator(it);
449             return info;
450         }
451         if (StrCmpIgnoreCase(info->connectInfo.macAddr, id) == 0) {
452             LnnMapDeinitIterator(it);
453             return info;
454         }
455         if (strcmp(info->connectInfo.deviceIp, id) == 0) {
456             LnnMapDeinitIterator(it);
457             return info;
458         }
459         LNN_LOGE(LNN_LEDGER, "type error");
460     }
461     LnnMapDeinitIterator(it);
462     return NULL;
463 }
464 
LnnGetRemoteNodeInfoById(const char * id,IdCategory type,NodeInfo * info)465 int32_t LnnGetRemoteNodeInfoById(const char *id, IdCategory type, NodeInfo *info)
466 {
467     if (id == NULL || info == NULL) {
468         LNN_LOGE(LNN_LEDGER, "param error");
469         return SOFTBUS_INVALID_PARAM;
470     }
471     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
472         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
473         return SOFTBUS_LOCK_ERR;
474     }
475     NodeInfo *nodeInfo = LnnGetNodeInfoById(id, type);
476     if (nodeInfo == NULL) {
477         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
478         char *anonyId = NULL;
479         Anonymize(id, &anonyId);
480         LNN_LOGI(LNN_LEDGER, "can not find target node, id=%{public}s, type=%{public}d", anonyId, type);
481         AnonymizeFree(anonyId);
482         return SOFTBUS_NETWORK_GET_NODE_INFO_ERR;
483     }
484     if (memcpy_s(info, sizeof(NodeInfo), nodeInfo, sizeof(NodeInfo)) != EOK) {
485         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
486         return SOFTBUS_MEM_ERR;
487     }
488     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
489     return SOFTBUS_OK;
490 }
491 
492 /* key means networkId/udid/uuid/macAddr/ip */
LnnGetRemoteNodeInfoByKey(const char * key,NodeInfo * info)493 int32_t LnnGetRemoteNodeInfoByKey(const char *key, NodeInfo *info)
494 {
495     if (key == NULL || info == NULL) {
496         LNN_LOGE(LNN_LEDGER, "param error");
497         return SOFTBUS_INVALID_PARAM;
498     }
499     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
500         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
501         return SOFTBUS_LOCK_ERR;
502     }
503     NodeInfo *nodeInfo = LnnGetNodeInfoByDeviceId(key);
504     if (nodeInfo == NULL) {
505         LNN_LOGI(LNN_LEDGER, "can not find target node");
506         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
507         return SOFTBUS_NETWORK_GET_NODE_INFO_ERR;
508     }
509     if (memcpy_s(info, sizeof(NodeInfo), nodeInfo, sizeof(NodeInfo)) != EOK) {
510         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
511         return SOFTBUS_MEM_ERR;
512     }
513     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
514     return SOFTBUS_OK;
515 }
516 
LnnGetOnlineStateById(const char * id,IdCategory type)517 bool LnnGetOnlineStateById(const char *id, IdCategory type)
518 {
519     bool state = false;
520     if (!IsValidString(id, ID_MAX_LEN)) {
521         LNN_LOGE(LNN_LEDGER, "id is invalid");
522         return state;
523     }
524 
525     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
526         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
527         return state;
528     }
529     NodeInfo *nodeInfo = LnnGetNodeInfoById(id, type);
530     if (nodeInfo == NULL) {
531         LNN_LOGI(LNN_LEDGER, "can not find target node");
532         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
533         return state;
534     }
535     state = (nodeInfo->status == STATUS_ONLINE) ? true : false;
536     if (!state) {
537         state = nodeInfo->metaInfo.isMetaNode;
538     }
539     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
540     return state;
541 }
542 
CreateCnnCodeKey(const char * uuid,DiscoveryType type)543 static char *CreateCnnCodeKey(const char *uuid, DiscoveryType type)
544 {
545     if (uuid == NULL || strlen(uuid) >= UUID_BUF_LEN) {
546         LNN_LOGE(LNN_LEDGER, "para error!");
547         return NULL;
548     }
549     char *key = (char *)SoftBusCalloc(INT_TO_STR_SIZE + UUID_BUF_LEN);
550     if (key == NULL) {
551         LNN_LOGE(LNN_LEDGER, "SoftBusCalloc fail!");
552         return NULL;
553     }
554     if (sprintf_s(key, INT_TO_STR_SIZE + UUID_BUF_LEN, "%d%s", type, uuid) == -1) {
555         LNN_LOGE(LNN_LEDGER, "type convert char error!");
556         goto EXIT_FAIL;
557     }
558     return key;
559 EXIT_FAIL:
560     SoftBusFree(key);
561     return NULL;
562 }
563 
DestroyCnnCodeKey(char * key)564 static void DestroyCnnCodeKey(char *key)
565 {
566     if (key == NULL) {
567         return;
568     }
569     SoftBusFree(key);
570 }
571 
572 
AddCnnCode(Map * cnnCode,const char * uuid,DiscoveryType type,int64_t authSeqNum)573 static int32_t AddCnnCode(Map *cnnCode, const char *uuid, DiscoveryType type, int64_t authSeqNum)
574 {
575     short seq = (short)authSeqNum;
576     char *key = CreateCnnCodeKey(uuid, type);
577     if (key == NULL) {
578         LNN_LOGE(LNN_LEDGER, "CreateCnnCodeKey error!");
579         return SOFTBUS_ERR;
580     }
581     if (LnnMapSet(cnnCode, key, (void *)&seq, sizeof(short)) != SOFTBUS_OK) {
582         LNN_LOGE(LNN_LEDGER, "LnnMapSet error!");
583         DestroyCnnCodeKey(key);
584         return SOFTBUS_ERR;
585     }
586     DestroyCnnCodeKey(key);
587     return SOFTBUS_OK;
588 }
589 
RemoveCnnCode(Map * cnnCode,const char * uuid,DiscoveryType type)590 static void RemoveCnnCode(Map *cnnCode, const char *uuid, DiscoveryType type)
591 {
592     char *key = CreateCnnCodeKey(uuid, type);
593     if (key == NULL) {
594         LNN_LOGE(LNN_LEDGER, "CreateCnnCodeKey error!");
595         return;
596     }
597     if (LnnMapErase(cnnCode, key) != SOFTBUS_OK) {
598         LNN_LOGE(LNN_LEDGER, "LnnMapErase error!");
599     }
600     DestroyCnnCodeKey(key);
601     return;
602 }
603 
LnnGetCnnCode(const char * uuid,DiscoveryType type)604 short LnnGetCnnCode(const char *uuid, DiscoveryType type)
605 {
606     char *key = CreateCnnCodeKey(uuid, type);
607     if (key == NULL) {
608         LNN_LOGE(LNN_LEDGER, "CreateCnnCodeKey error!");
609         return INVALID_CONNECTION_CODE_VALUE;
610     }
611     short *ptr = (short *)LnnMapGet(&g_distributedNetLedger.cnnCode.connectionCode, key);
612     if (ptr == NULL) {
613         LNN_LOGE(LNN_LEDGER, " KEY not exist.");
614         DestroyCnnCodeKey(key);
615         return INVALID_CONNECTION_CODE_VALUE;
616     }
617     DestroyCnnCodeKey(key);
618     return (*ptr);
619 }
620 
MergeLnnInfo(const NodeInfo * oldInfo,NodeInfo * info)621 static void MergeLnnInfo(const NodeInfo *oldInfo, NodeInfo *info)
622 {
623     int32_t i, j;
624 
625     for (i = 0; i < CONNECTION_ADDR_MAX; ++i) {
626         info->relation[i] += oldInfo->relation[i];
627         info->relation[i] &= LNN_RELATION_MASK;
628         for (j = 0; j < AUTH_SIDE_MAX; ++j) {
629             if (oldInfo->authChannelId[i][j] != 0 && info->authChannelId[i][j] == 0) {
630                 info->authChannelId[i][j] = oldInfo->authChannelId[i][j];
631             }
632         }
633         if (oldInfo->authChannelId[i][AUTH_AS_CLIENT_SIDE] != 0 ||
634             oldInfo->authChannelId[i][AUTH_AS_SERVER_SIDE] != 0 || info->authChannelId[i][AUTH_AS_CLIENT_SIDE] != 0 ||
635             info->authChannelId[i][AUTH_AS_SERVER_SIDE] != 0) {
636             LNN_LOGD(LNN_LEDGER,
637                 "Merge authChannelId. authChannelId:%{public}d|%{public}d->%{public}d|%{public}d, addrType=%{public}d",
638                 oldInfo->authChannelId[i][AUTH_AS_CLIENT_SIDE], oldInfo->authChannelId[i][AUTH_AS_SERVER_SIDE],
639                 info->authChannelId[i][AUTH_AS_CLIENT_SIDE], info->authChannelId[i][AUTH_AS_SERVER_SIDE], i);
640         }
641     }
642     if (strcpy_s(info->lastNetworkId, NETWORK_ID_BUF_LEN, oldInfo->lastNetworkId) != EOK) {
643         LNN_LOGE(LNN_LEDGER, "cpy lastNetworkId fail");
644     }
645 }
646 
LnnUpdateNetworkId(const NodeInfo * newInfo)647 int32_t LnnUpdateNetworkId(const NodeInfo *newInfo)
648 {
649     const char *udid = NULL;
650     DoubleHashMap *map = NULL;
651     NodeInfo *oldInfo = NULL;
652 
653     udid = LnnGetDeviceUdid(newInfo);
654     map = &g_distributedNetLedger.distributedInfo;
655     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
656         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
657         return SOFTBUS_LOCK_ERR;
658     }
659     oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
660     if (oldInfo == NULL) {
661         LNN_LOGE(LNN_LEDGER, "no online node newInfo!");
662         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
663         return SOFTBUS_ERR;
664     }
665     if (strcpy_s(oldInfo->lastNetworkId, NETWORK_ID_BUF_LEN, oldInfo->networkId) != EOK) {
666         LNN_LOGE(LNN_LEDGER, "old networkId cpy fail");
667         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
668         return SOFTBUS_MEM_ERR;
669     }
670     if (strcpy_s(oldInfo->networkId, NETWORK_ID_BUF_LEN, newInfo->networkId) != EOK) {
671         LNN_LOGE(LNN_LEDGER, "networkId cpy fail");
672         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
673         return SOFTBUS_MEM_ERR;
674     }
675     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
676     return SOFTBUS_OK;
677 }
678 
UpdateNewNodeAccountHash(NodeInfo * info)679 static void UpdateNewNodeAccountHash(NodeInfo *info)
680 {
681     char accountString[LONG_TO_STRING_MAX_LEN] = {0};
682     if (sprintf_s(accountString, LONG_TO_STRING_MAX_LEN, "%" PRId64, info->accountId) == -1) {
683         LNN_LOGE(LNN_LEDGER, "long to string fail");
684         return;
685     }
686     LNN_LOGD(LNN_LEDGER, "accountString=%{public}s", accountString);
687     int ret = SoftBusGenerateStrHash((uint8_t *)accountString,
688         strlen(accountString), (unsigned char *)info->accountHash);
689     if (ret != SOFTBUS_OK) {
690         LNN_LOGE(LNN_LEDGER, "account hash fail, ret=%{public}d", ret);
691         return;
692     }
693 }
694 
LnnUpdateNodeInfo(NodeInfo * newInfo)695 int32_t LnnUpdateNodeInfo(NodeInfo *newInfo)
696 {
697     const char *udid = NULL;
698     DoubleHashMap *map = NULL;
699     NodeInfo *oldInfo = NULL;
700     char deviceName[DEVICE_NAME_BUF_LEN] = { 0 };
701 
702     UpdateNewNodeAccountHash(newInfo);
703     UpdateDpSameAccount(newInfo->accountId, newInfo->deviceInfo.deviceUdid);
704     udid = LnnGetDeviceUdid(newInfo);
705     map = &g_distributedNetLedger.distributedInfo;
706     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
707         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
708         return SOFTBUS_LOCK_ERR;
709     }
710     oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
711     if (oldInfo == NULL) {
712         LNN_LOGE(LNN_LEDGER, "no online node newInfo!");
713         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
714         return SOFTBUS_ERR;
715     }
716     if (LnnHasDiscoveryType(newInfo, DISCOVERY_TYPE_WIFI) || LnnHasDiscoveryType(newInfo, DISCOVERY_TYPE_LSA)) {
717         oldInfo->discoveryType = newInfo->discoveryType | oldInfo->discoveryType;
718         oldInfo->connectInfo.authPort = newInfo->connectInfo.authPort;
719         oldInfo->connectInfo.proxyPort = newInfo->connectInfo.proxyPort;
720         oldInfo->connectInfo.sessionPort = newInfo->connectInfo.sessionPort;
721     }
722     if (strcpy_s(deviceName, DEVICE_NAME_BUF_LEN, oldInfo->deviceInfo.deviceName) != EOK ||
723         strcpy_s(oldInfo->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN, newInfo->deviceInfo.deviceName) != EOK) {
724         LNN_LOGE(LNN_LEDGER, "strcpy_s fail");
725         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
726         return SOFTBUS_STRCPY_ERR;
727     }
728     if (memcpy_s(oldInfo->accountHash, SHA_256_HASH_LEN, newInfo->accountHash, SHA_256_HASH_LEN) != EOK) {
729         LNN_LOGE(LNN_LEDGER, "copy account hash failed");
730         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
731         return SOFTBUS_ERR;
732     }
733     LnnDumpRemotePtk(oldInfo->remotePtk, newInfo->remotePtk, "update node info");
734     if (memcpy_s(oldInfo->remotePtk, PTK_DEFAULT_LEN, newInfo->remotePtk, PTK_DEFAULT_LEN) != EOK) {
735         LNN_LOGE(LNN_LEDGER, "copy ptk failed");
736         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
737         return SOFTBUS_ERR;
738     }
739     oldInfo->accountId = newInfo->accountId;
740     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
741     if (memcmp(deviceName, newInfo->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN) != 0) {
742         UpdateDeviceNameInfo(newInfo->deviceInfo.deviceUdid, deviceName);
743     }
744     return SOFTBUS_OK;
745 }
746 
LnnAddMetaInfo(NodeInfo * info)747 int32_t LnnAddMetaInfo(NodeInfo *info)
748 {
749     if (info == NULL) {
750         LNN_LOGE(LNN_LEDGER, "param error");
751         return SOFTBUS_INVALID_PARAM;
752     }
753     const char *udid = NULL;
754     DoubleHashMap *map = NULL;
755     NodeInfo *oldInfo = NULL;
756     udid = LnnGetDeviceUdid(info);
757     map = &g_distributedNetLedger.distributedInfo;
758     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
759         LNN_LOGE(LNN_LEDGER, "LnnAddMetaInfo lock mutex fail!");
760         return SOFTBUS_LOCK_ERR;
761     }
762     oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
763     if (oldInfo != NULL && strcmp(oldInfo->networkId, info->networkId) == 0) {
764         LNN_LOGI(LNN_LEDGER, "old capa=%{public}u new capa=%{public}u", oldInfo->netCapacity, info->netCapacity);
765         oldInfo->connectInfo.sessionPort = info->connectInfo.sessionPort;
766         oldInfo->connectInfo.authPort = info->connectInfo.authPort;
767         oldInfo->connectInfo.proxyPort = info->connectInfo.proxyPort;
768         oldInfo->netCapacity = info->netCapacity;
769         if (strcpy_s(oldInfo->connectInfo.deviceIp, IP_LEN, info->connectInfo.deviceIp) != EOK) {
770             LNN_LOGE(LNN_LEDGER, "strcpy ip fail!");
771             SoftBusMutexUnlock(&g_distributedNetLedger.lock);
772             return SOFTBUS_STRCPY_ERR;
773         }
774         if (strcpy_s(oldInfo->uuid, UUID_BUF_LEN, info->uuid) != EOK) {
775             LNN_LOGE(LNN_LEDGER, "strcpy uuid fail!");
776             SoftBusMutexUnlock(&g_distributedNetLedger.lock);
777             return SOFTBUS_STRCPY_ERR;
778         }
779         MetaInfo temp = info->metaInfo;
780         if (memcpy_s(info, sizeof(NodeInfo), oldInfo, sizeof(NodeInfo)) != EOK) {
781             LNN_LOGE(LNN_LEDGER, "LnnAddMetaInfo copy fail!");
782             SoftBusMutexUnlock(&g_distributedNetLedger.lock);
783             return SOFTBUS_MEM_ERR;
784         }
785         info->metaInfo.isMetaNode = true;
786         info->metaInfo.metaDiscType = info->metaInfo.metaDiscType | temp.metaDiscType;
787     }
788     LnnSetAuthTypeValue(&info->AuthTypeValue, ONLINE_METANODE);
789     int32_t ret = LnnMapSet(&map->udidMap, udid, info, sizeof(NodeInfo));
790     if (ret != SOFTBUS_OK) {
791         LNN_LOGE(LNN_LEDGER, "lnn map set failed, ret=%{public}d", ret);
792     }
793     LNN_LOGI(LNN_LEDGER, "LnnAddMetaInfo success");
794     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
795     return SOFTBUS_OK;
796 }
797 
LnnDeleteMetaInfo(const char * udid,AuthLinkType type)798 int32_t LnnDeleteMetaInfo(const char *udid, AuthLinkType type)
799 {
800     NodeInfo *info = NULL;
801     DiscoveryType discType = ConvertToDiscoveryType(type);
802     if (discType == DISCOVERY_TYPE_COUNT) {
803         LNN_LOGE(LNN_LEDGER, "DeleteMetaInfo type error fail!");
804         return SOFTBUS_NETWORK_DELETE_INFO_ERR;
805     }
806     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
807     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
808         LNN_LOGE(LNN_LEDGER, "DeleteAddMetaInfo lock mutex fail!");
809         return SOFTBUS_LOCK_ERR;
810     }
811     info = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
812     if (info == NULL) {
813         LNN_LOGE(LNN_LEDGER, "DeleteAddMetaInfo para error!");
814         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
815         return SOFTBUS_NETWORK_DELETE_INFO_ERR;
816     }
817     info->metaInfo.metaDiscType = (uint32_t)info->metaInfo.metaDiscType & ~(1 << (uint32_t)discType);
818     if (info->metaInfo.metaDiscType == 0) {
819         info->metaInfo.isMetaNode = false;
820     }
821     LnnClearAuthTypeValue(&info->AuthTypeValue, ONLINE_METANODE);
822     (void)memset_s(info->remoteMetaPtk, PTK_DEFAULT_LEN, 0, PTK_DEFAULT_LEN);
823     int32_t ret = LnnMapSet(&map->udidMap, udid, info, sizeof(NodeInfo));
824     if (ret != SOFTBUS_OK) {
825         LNN_LOGE(LNN_LEDGER, "lnn map set failed, ret=%{public}d", ret);
826     }
827     LNN_LOGI(LNN_LEDGER, "LnnDeleteMetaInfo success, discType=%{public}d", discType);
828     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
829     return SOFTBUS_OK;
830 }
831 
OnlinePreventBrConnection(const NodeInfo * info)832 static void OnlinePreventBrConnection(const NodeInfo *info)
833 {
834     int32_t osType = 0;
835     if (LnnGetOsTypeByNetworkId(info->networkId, &osType)) {
836         LNN_LOGE(LNN_LEDGER, "get remote osType fail");
837     }
838     if (osType != HO_OS_TYPE) {
839         LNN_LOGD(LNN_LEDGER, "not pend br connection");
840         return;
841     }
842     const NodeInfo *localNodeInfo = LnnGetLocalNodeInfo();
843     if (localNodeInfo == NULL) {
844         LNN_LOGE(LNN_LEDGER, "get local node info fail");
845         return;
846     }
847     ConnectOption option;
848     option.type = CONNECT_BR;
849     if (strcpy_s(option.brOption.brMac, BT_MAC_LEN, info->connectInfo.macAddr) != EOK) {
850         LNN_LOGE(LNN_LEDGER, "copy br mac fail");
851         return;
852     }
853     bool preventFlag = false;
854     do {
855         LNN_LOGI(LNN_LEDGER, "check the ble start timestamp, local=%{public}" PRId64", peer=%{public}" PRId64"",
856             localNodeInfo->bleStartTimestamp, info->bleStartTimestamp);
857         if (localNodeInfo->bleStartTimestamp < info->bleStartTimestamp) {
858             LNN_LOGI(LNN_LEDGER, "peer later, prevent br connection");
859             preventFlag = true;
860             break;
861         }
862         if (localNodeInfo->bleStartTimestamp > info->bleStartTimestamp) {
863             LNN_LOGI(LNN_LEDGER, "local later, do not prevent br connection");
864             break;
865         }
866         if (strcmp(info->softBusVersion, SOFTBUS_VERSION_FOR_INITCONNECTFLAG) < 0) {
867             LNN_LOGI(LNN_LEDGER, "peer is old version, peerVersion=%{public}s", info->softBusVersion);
868             preventFlag = true;
869             break;
870         }
871         if (strcmp(info->networkId, localNodeInfo->networkId) <= 0) {
872             LNN_LOGI(LNN_LEDGER, "peer network id is smaller");
873             preventFlag = true;
874             break;
875         }
876     } while (false);
877     if (preventFlag) {
878         LNN_LOGI(LNN_LEDGER, "prevent br connection for a while");
879         ConnPreventConnection(&option, CONNECTION_FREEZE_TIMEOUT_MILLIS);
880     }
881 }
882 
NotifyMigrateUpgrade(NodeInfo * info)883 static void NotifyMigrateUpgrade(NodeInfo *info)
884 {
885     NodeBasicInfo basic;
886     (void)memset_s(&basic, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
887     if (LnnGetBasicInfoByUdid(info->deviceInfo.deviceUdid, &basic) == SOFTBUS_OK) {
888         LnnNotifyMigrate(true, &basic);
889     } else {
890         LNN_LOGE(LNN_LEDGER, "NotifyMigrateUpgrade, GetBasicInfoByUdid fail!");
891     }
892 }
893 
FilterWifiInfo(NodeInfo * info)894 static void FilterWifiInfo(NodeInfo *info)
895 {
896     (void)LnnClearDiscoveryType(info, DISCOVERY_TYPE_WIFI);
897     info->authChannelId[CONNECTION_ADDR_WLAN][AUTH_AS_CLIENT_SIDE] = 0;
898     info->authChannelId[CONNECTION_ADDR_WLAN][AUTH_AS_SERVER_SIDE] = 0;
899 }
900 
FilterBrInfo(NodeInfo * info)901 static void FilterBrInfo(NodeInfo *info)
902 {
903     (void)LnnClearDiscoveryType(info, DISCOVERY_TYPE_BR);
904     info->authChannelId[CONNECTION_ADDR_BR][AUTH_AS_CLIENT_SIDE] = 0;
905     info->authChannelId[CONNECTION_ADDR_BR][AUTH_AS_SERVER_SIDE] = 0;
906 }
907 
IsDeviceInfoChanged(NodeInfo * info)908 static bool IsDeviceInfoChanged(NodeInfo *info)
909 {
910     if (info == NULL) {
911         LNN_LOGI(LNN_LEDGER, "invalid param");
912         return false;
913     }
914     NodeInfo deviceInfo;
915     if (memset_s(&deviceInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)) != EOK) {
916         LNN_LOGE(LNN_LEDGER, "memset_s basic fail!");
917         return false;
918     }
919     uint8_t udidHash[SHA_256_HASH_LEN] = {0};
920     char hashStr[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
921     if (SoftBusGenerateStrHash((const unsigned char *)info->deviceInfo.deviceUdid,
922         strlen(info->deviceInfo.deviceUdid), udidHash) != SOFTBUS_OK) {
923         LNN_LOGI(LNN_LEDGER, "generate udidhash fail");
924         return false;
925     }
926     if (ConvertBytesToHexString(hashStr, SHORT_UDID_HASH_HEX_LEN + 1, udidHash,
927         SHORT_UDID_HASH_HEX_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
928         LNN_LOGI(LNN_LEDGER, "convert udidhash to hexstr fail");
929         return false;
930     }
931     int32_t ret = LnnRetrieveDeviceInfo(hashStr, &deviceInfo);
932     if (ret == SOFTBUS_NETWORK_NOT_FOUND) {
933         return true;
934     }
935     if (ret != SOFTBUS_OK) {
936         LNN_LOGI(LNN_LEDGER, "get deviceInfo by udidhash fail");
937         return false;
938     }
939     return memcmp(info, &deviceInfo, (size_t)&(((NodeInfo *)0)->relation)) != 0 ? true : false;
940 }
941 
GetAndSaveRemoteDeviceInfo(NodeInfo * deviceInfo,NodeInfo * info)942 static void GetAndSaveRemoteDeviceInfo(NodeInfo *deviceInfo, NodeInfo *info)
943 {
944     if (strcpy_s(deviceInfo->networkId, sizeof(deviceInfo->networkId), info->networkId) != EOK) {
945         LNN_LOGE(LNN_LEDGER, "strcpy_s networkId fail");
946         return;
947     }
948     if (strcpy_s(deviceInfo->uuid, sizeof(deviceInfo->uuid), info->uuid) != EOK) {
949         LNN_LOGE(LNN_LEDGER, "strcpy_s uuid fail");
950         return;
951     }
952     if (memcpy_s(deviceInfo->rpaInfo.peerIrk, sizeof(deviceInfo->rpaInfo.peerIrk), info->rpaInfo.peerIrk,
953         sizeof(info->rpaInfo.peerIrk)) != EOK) {
954         LNN_LOGE(LNN_LEDGER, "memcpy_s Irk fail");
955         return;
956     }
957     if (memcpy_s(deviceInfo->cipherInfo.key, sizeof(deviceInfo->cipherInfo.key), info->cipherInfo.key,
958         sizeof(info->cipherInfo.key)) != EOK) {
959         LNN_LOGE(LNN_LEDGER, "memcpy_s cipherInfo.key fail");
960         return;
961     }
962     if (memcpy_s(deviceInfo->cipherInfo.iv, sizeof(deviceInfo->cipherInfo.iv), info->cipherInfo.iv,
963         sizeof(info->cipherInfo.iv)) != EOK) {
964         LNN_LOGE(LNN_LEDGER, "memcpy_s cipherInfo.iv fail");
965         return;
966     }
967     LnnDumpRemotePtk(deviceInfo->remotePtk, info->remotePtk, "get and save remote device info");
968     if (memcpy_s(deviceInfo->remotePtk, PTK_DEFAULT_LEN, info->remotePtk, PTK_DEFAULT_LEN) != EOK) {
969         LNN_LOGE(LNN_LEDGER, "memcpy_s ptk fail");
970         return;
971     }
972     deviceInfo->netCapacity = info->netCapacity;
973     if (LnnSaveRemoteDeviceInfo(deviceInfo) != SOFTBUS_OK) {
974         LNN_LOGE(LNN_LEDGER, "save remote devInfo fail");
975         return;
976     }
977 }
978 
BleDirectlyOnlineProc(NodeInfo * info)979 static void BleDirectlyOnlineProc(NodeInfo *info)
980 {
981     if (!LnnHasDiscoveryType(info, DISCOVERY_TYPE_BLE)) {
982         NodeInfo deviceInfo;
983         if (memset_s(&deviceInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)) != EOK) {
984             LNN_LOGE(LNN_LEDGER, "memset_s basic fail!");
985         }
986         uint8_t udidHash[SHA_256_HASH_LEN] = {0};
987         char hashStr[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
988         if (SoftBusGenerateStrHash((const unsigned char *)info->deviceInfo.deviceUdid,
989             strlen(info->deviceInfo.deviceUdid), udidHash) != SOFTBUS_OK) {
990             LNN_LOGE(LNN_LEDGER, "generate udidhash fail");
991             return;
992         }
993         if (ConvertBytesToHexString(hashStr, SHORT_UDID_HASH_HEX_LEN + 1, udidHash,
994             SHORT_UDID_HASH_HEX_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
995             LNN_LOGE(LNN_LEDGER, "convert udidhash to hexstr fail");
996             return;
997         }
998         if (LnnRetrieveDeviceInfo(hashStr, &deviceInfo) != SOFTBUS_OK) {
999             LNN_LOGE(LNN_LEDGER, "get deviceInfo by udidhash fail");
1000             return;
1001         }
1002         char *anonyDevNetworkId = NULL;
1003         char *anonyNetworkId = NULL;
1004         Anonymize(deviceInfo.networkId, &anonyDevNetworkId);
1005         Anonymize(info->networkId, &anonyNetworkId);
1006         LNN_LOGI(LNN_LEDGER, "oldNetworkId=%{public}s, newNetworkid=%{public}s", anonyDevNetworkId, anonyNetworkId);
1007         AnonymizeFree(anonyDevNetworkId);
1008         AnonymizeFree(anonyNetworkId);
1009         GetAndSaveRemoteDeviceInfo(&deviceInfo, info);
1010         return;
1011     }
1012     if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_WIFI)) {
1013         FilterWifiInfo(info);
1014     }
1015     if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR)) {
1016         FilterBrInfo(info);
1017     }
1018     if (IsDeviceInfoChanged(info)) {
1019         if (LnnSaveRemoteDeviceInfo(info) != SOFTBUS_OK) {
1020             LNN_LOGE(LNN_LEDGER, "save remote devInfo fail");
1021         }
1022     } else {
1023         LnnUpdateRemoteDeviceInfo(info);
1024     }
1025 }
1026 
NodeOnlineProc(NodeInfo * info)1027 static void NodeOnlineProc(NodeInfo *info)
1028 {
1029     NodeInfo nodeInfo;
1030     if (memcpy_s(&nodeInfo, sizeof(nodeInfo), info, sizeof(NodeInfo)) != EOK) {
1031         return;
1032     }
1033     BleDirectlyOnlineProc(&nodeInfo);
1034 }
1035 
GetNodeInfoDiscovery(NodeInfo * oldInfo,NodeInfo * info,NodeInfoAbility * infoAbility)1036 static void GetNodeInfoDiscovery(NodeInfo *oldInfo, NodeInfo *info, NodeInfoAbility *infoAbility)
1037 {
1038     infoAbility->isOffline = true;
1039     infoAbility->oldWifiFlag = false;
1040     infoAbility->oldBrFlag = false;
1041     infoAbility->oldBleFlag = false;
1042     infoAbility->isChanged = false;
1043     infoAbility->isMigrateEvent = false;
1044     infoAbility->isNetworkChanged = false;
1045     infoAbility->newWifiFlag = LnnHasDiscoveryType(info, DISCOVERY_TYPE_WIFI);
1046     infoAbility->newBleBrFlag =
1047         LnnHasDiscoveryType(info, DISCOVERY_TYPE_BLE) || LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR);
1048     if (oldInfo != NULL) {
1049         info->metaInfo = oldInfo->metaInfo;
1050     }
1051     if (oldInfo != NULL && LnnIsNodeOnline(oldInfo)) {
1052         char *anonyUuid = NULL;
1053         Anonymize(oldInfo->uuid, &anonyUuid);
1054         LNN_LOGI(LNN_LEDGER, "addOnlineNode find online node, uuid=%{public}s", anonyUuid);
1055         AnonymizeFree(anonyUuid);
1056         infoAbility->isOffline = false;
1057         infoAbility->isChanged = IsNetworkIdChanged(info, oldInfo);
1058         infoAbility->oldWifiFlag = LnnHasDiscoveryType(oldInfo, DISCOVERY_TYPE_WIFI);
1059         infoAbility->oldBleFlag = LnnHasDiscoveryType(oldInfo, DISCOVERY_TYPE_BLE);
1060         infoAbility->oldBrFlag = LnnHasDiscoveryType(oldInfo, DISCOVERY_TYPE_BR);
1061         if ((infoAbility->oldBleFlag || infoAbility->oldBrFlag) && infoAbility->newWifiFlag) {
1062             NewWifiDiscovered(oldInfo, info);
1063             infoAbility->isNetworkChanged = true;
1064         } else if (infoAbility->oldWifiFlag && infoAbility->newBleBrFlag) {
1065             RetainOfflineCode(oldInfo, info);
1066             NewBrBleDiscovered(oldInfo, info);
1067             infoAbility->isNetworkChanged = true;
1068         } else {
1069             RetainOfflineCode(oldInfo, info);
1070             LNN_LOGE(LNN_LEDGER, "flag error, oldBleFlag=%{public}d, oldBrFlag=%{public}d, oldWifiFlag=%{public}d,"
1071                 "newWifiFlag=%{public}d, newBleBrFlag=%{public}d", infoAbility->oldBleFlag, infoAbility->oldBrFlag,
1072                 infoAbility->oldWifiFlag, infoAbility->newBleBrFlag, infoAbility->newBleBrFlag);
1073         }
1074         if ((infoAbility->oldBleFlag || infoAbility->oldBrFlag) && !infoAbility->oldWifiFlag &&
1075             infoAbility->newWifiFlag) {
1076             infoAbility->isMigrateEvent = true;
1077         }
1078         // update lnn discovery type
1079         info->discoveryType |= oldInfo->discoveryType;
1080         info->AuthTypeValue = oldInfo->AuthTypeValue;
1081         info->heartbeatTimeStamp = oldInfo->heartbeatTimeStamp;
1082         MergeLnnInfo(oldInfo, info);
1083         UpdateProfile(info);
1084     }
1085 }
1086 
DfxRecordLnnSetNodeOfflineEnd(const char * udid,int32_t onlineNum,int32_t reason)1087 static void DfxRecordLnnSetNodeOfflineEnd(const char *udid, int32_t onlineNum, int32_t reason)
1088 {
1089     LnnEventExtra extra = { 0 };
1090     LnnEventExtraInit(&extra);
1091     extra.onlineNum = onlineNum;
1092     extra.errcode = reason;
1093     extra.result = (reason == SOFTBUS_OK) ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
1094 
1095     char udidData[UDID_BUF_LEN] = { 0 };
1096     if (udid != NULL && strnlen(udid, UDID_BUF_LEN) != UDID_BUF_LEN && strncpy_s(udidData,
1097         UDID_BUF_LEN, udid, UDID_BUF_LEN - 1) == EOK) {
1098         extra.peerUdid = udidData;
1099     }
1100     LNN_EVENT(EVENT_SCENE_LEAVE_LNN, EVENT_STAGE_LEAVE_LNN, extra);
1101 }
1102 
TryUpdateDeviceSecurityLevel(NodeInfo * info)1103 static void TryUpdateDeviceSecurityLevel(NodeInfo *info)
1104 {
1105     NodeInfo deviceInfo;
1106     (void)memset_s(&deviceInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1107     uint8_t udidHash[SHA_256_HASH_LEN] = {0};
1108     char hashStr[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
1109     if (SoftBusGenerateStrHash((const unsigned char *)info->deviceInfo.deviceUdid,
1110         strlen(info->deviceInfo.deviceUdid), udidHash) != SOFTBUS_OK) {
1111         LNN_LOGE(LNN_LEDGER, "generate udidhash fail");
1112         return;
1113     }
1114     if (ConvertBytesToHexString(hashStr, SHORT_UDID_HASH_HEX_LEN + 1, udidHash,
1115         SHORT_UDID_HASH_HEX_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
1116         LNN_LOGE(LNN_LEDGER, "convert udidhash to hexstr fail");
1117         return;
1118     }
1119     if (LnnRetrieveDeviceInfo(hashStr, &deviceInfo) != SOFTBUS_OK) {
1120         LNN_LOGE(LNN_LEDGER, "get deviceInfo by udidhash fail");
1121         return;
1122     }
1123     LNN_LOGI(LNN_LEDGER, "deviceSecurityLevel new=%{public}d, old=%{public}d",
1124         info->deviceSecurityLevel, deviceInfo.deviceSecurityLevel);
1125     if (deviceInfo.deviceSecurityLevel > 0) {
1126         info->deviceSecurityLevel = deviceInfo.deviceSecurityLevel;
1127     }
1128 }
1129 
ReversionLastAuthSeq(NodeInfo * info)1130 static void ReversionLastAuthSeq(NodeInfo *info)
1131 {
1132     NodeInfo deviceInfo;
1133     (void)memset_s(&deviceInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
1134     uint8_t udidHash[SHA_256_HASH_LEN] = {0};
1135     char hashStr[SHORT_UDID_HASH_HEX_LEN + 1] = {0};
1136     if (SoftBusGenerateStrHash((const unsigned char *)info->deviceInfo.deviceUdid,
1137         strlen(info->deviceInfo.deviceUdid), udidHash) != SOFTBUS_OK) {
1138         LNN_LOGE(LNN_LEDGER, "generate udidhash fail");
1139         return;
1140     }
1141     if (ConvertBytesToHexString(hashStr, SHORT_UDID_HASH_HEX_LEN + 1, udidHash,
1142         SHORT_UDID_HASH_HEX_LEN / HEXIFY_UNIT_LEN) != SOFTBUS_OK) {
1143         LNN_LOGE(LNN_LEDGER, "convert udidhash to hexstr fail");
1144         return;
1145     }
1146     if (LnnRetrieveDeviceInfo(hashStr, &deviceInfo) != SOFTBUS_OK) {
1147         LNN_LOGE(LNN_LEDGER, "get deviceInfo by udidhash fail");
1148         return;
1149     }
1150     if (info->lastAuthSeq == 0 && deviceInfo.lastAuthSeq != 0) {
1151         info->lastAuthSeq = deviceInfo.lastAuthSeq;
1152         LNN_LOGI(LNN_LEDGER, "reversion lastAuthSeq:0->%{public}" PRId64, info->lastAuthSeq);
1153     }
1154 }
1155 
LnnAddOnlineNode(NodeInfo * info)1156 ReportCategory LnnAddOnlineNode(NodeInfo *info)
1157 {
1158     if (info == NULL) {
1159         return REPORT_NONE;
1160     }
1161     // judge map
1162     info->onlinetTimestamp = (uint64_t)LnnUpTimeMs();
1163     if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR)) {
1164         LNN_LOGI(LNN_LEDGER, "DiscoveryType = BR.");
1165         AddCnnCode(&g_distributedNetLedger.cnnCode.connectionCode, info->uuid, DISCOVERY_TYPE_BR, info->authSeqNum);
1166     }
1167 
1168     NodeInfoAbility infoAbility;
1169     const char *udid = LnnGetDeviceUdid(info);
1170     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
1171     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1172         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
1173         return REPORT_NONE;
1174     }
1175     NodeInfo *oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1176     GetNodeInfoDiscovery(oldInfo, info, &infoAbility);
1177     LnnSetNodeConnStatus(info, STATUS_ONLINE);
1178     LnnSetAuthTypeValue(&info->AuthTypeValue, ONLINE_HICHAIN);
1179     UpdateNewNodeAccountHash(info);
1180     TryUpdateDeviceSecurityLevel(info);
1181     ReversionLastAuthSeq(info);
1182     int32_t ret = LnnMapSet(&map->udidMap, udid, info, sizeof(NodeInfo));
1183     if (ret != SOFTBUS_OK) {
1184         LNN_LOGE(LNN_LEDGER, "lnn map set failed, ret=%{public}d", ret);
1185     }
1186     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1187     NodeOnlineProc(info);
1188     UpdateDpSameAccount(info->accountId, info->deviceInfo.deviceUdid);
1189     if (infoAbility.isNetworkChanged) {
1190         UpdateNetworkInfo(info->deviceInfo.deviceUdid);
1191     }
1192     if (infoAbility.isOffline) {
1193         if (!infoAbility.oldWifiFlag && !infoAbility.newWifiFlag && infoAbility.newBleBrFlag) {
1194             OnlinePreventBrConnection(info);
1195         }
1196         InsertToProfile(info);
1197         return REPORT_ONLINE;
1198     }
1199     if (infoAbility.isMigrateEvent) {
1200         NotifyMigrateUpgrade(info);
1201     }
1202     if (infoAbility.isChanged) {
1203         return REPORT_CHANGE;
1204     }
1205     return REPORT_NONE;
1206 }
1207 
LnnUpdateAccountInfo(const NodeInfo * info)1208 int32_t LnnUpdateAccountInfo(const NodeInfo *info)
1209 {
1210     if (info == NULL) {
1211         LNN_LOGE(LNN_LEDGER, "info is null");
1212         return SOFTBUS_INVALID_PARAM;
1213     }
1214     const char *udid = NULL;
1215     DoubleHashMap *map = NULL;
1216     NodeInfo *oldInfo = NULL;
1217     udid = LnnGetDeviceUdid(info);
1218 
1219     map = &g_distributedNetLedger.distributedInfo;
1220     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1221         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
1222         return REPORT_NONE;
1223     }
1224     oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1225     if (oldInfo != NULL) {
1226         oldInfo->accountId = info->accountId;
1227         UpdateNewNodeAccountHash(oldInfo);
1228     }
1229     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1230     return SOFTBUS_OK;
1231 }
1232 
LnnUpdateGroupType(const NodeInfo * info)1233 int32_t LnnUpdateGroupType(const NodeInfo *info)
1234 {
1235     if (info == NULL) {
1236         LNN_LOGE(LNN_LEDGER, "info is null");
1237         return SOFTBUS_INVALID_PARAM;
1238     }
1239     const char *udid = NULL;
1240     DoubleHashMap *map = NULL;
1241     NodeInfo *oldInfo = NULL;
1242     udid = LnnGetDeviceUdid(info);
1243     uint32_t groupType = AuthGetGroupType(udid, info->uuid);
1244     LNN_LOGI(LNN_LEDGER, "groupType=%{public}u", groupType);
1245     int32_t ret = SOFTBUS_ERR;
1246     map = &g_distributedNetLedger.distributedInfo;
1247     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1248         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
1249         return SOFTBUS_LOCK_ERR;
1250     }
1251     oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1252     if (oldInfo != NULL) {
1253         oldInfo->groupType = groupType;
1254         ret = SOFTBUS_OK;
1255     }
1256     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1257     return ret;
1258 }
1259 
NotifyMigrateDegrade(const char * udid)1260 static void NotifyMigrateDegrade(const char *udid)
1261 {
1262     NodeBasicInfo basic;
1263     (void)memset_s(&basic, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
1264     if (LnnGetBasicInfoByUdid(udid, &basic) == SOFTBUS_OK) {
1265         LnnNotifyMigrate(false, &basic);
1266     } else {
1267         LNN_LOGE(LNN_LEDGER, "NotifyMigrateDegrade, GetBasicInfoByUdid fail!");
1268     }
1269 }
1270 
ClearAuthChannelId(NodeInfo * info,ConnectionAddrType type,int32_t authId)1271 static ReportCategory ClearAuthChannelId(NodeInfo *info, ConnectionAddrType type, int32_t authId)
1272 {
1273     if ((LnnHasDiscoveryType(info, DISCOVERY_TYPE_WIFI) && LnnConvAddrTypeToDiscType(type) == DISCOVERY_TYPE_WIFI) ||
1274         (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BLE) && LnnConvAddrTypeToDiscType(type) == DISCOVERY_TYPE_BLE)) {
1275         if (info->authChannelId[type][AUTH_AS_CLIENT_SIDE] == authId) {
1276             info->authChannelId[type][AUTH_AS_CLIENT_SIDE] = 0;
1277         }
1278         if (info->authChannelId[type][AUTH_AS_SERVER_SIDE] == authId) {
1279             info->authChannelId[type][AUTH_AS_SERVER_SIDE] = 0;
1280         }
1281         if (info->authChannelId[type][AUTH_AS_CLIENT_SIDE] != 0 ||
1282             info->authChannelId[type][AUTH_AS_SERVER_SIDE] != 0) {
1283             LNN_LOGI(LNN_LEDGER,
1284                 "authChannelId not clear, not need to report offline. authChannelId=%{public}d|%{public}d",
1285                 info->authChannelId[type][AUTH_AS_CLIENT_SIDE], info->authChannelId[type][AUTH_AS_SERVER_SIDE]);
1286             return REPORT_NONE;
1287         }
1288     }
1289     info->authChannelId[type][AUTH_AS_CLIENT_SIDE] = 0;
1290     info->authChannelId[type][AUTH_AS_SERVER_SIDE] = 0;
1291     return REPORT_OFFLINE;
1292 }
1293 
LnnSetNodeOffline(const char * udid,ConnectionAddrType type,int32_t authId)1294 ReportCategory LnnSetNodeOffline(const char *udid, ConnectionAddrType type, int32_t authId)
1295 {
1296     NodeInfo *info = NULL;
1297 
1298     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
1299     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1300         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
1301         return REPORT_NONE;
1302     }
1303     info = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1304     if (info == NULL) {
1305         LNN_LOGE(LNN_LEDGER, "PARA ERROR!");
1306         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1307         return REPORT_NONE;
1308     }
1309     if (type != CONNECTION_ADDR_MAX && info->relation[type] > 0) {
1310         info->relation[type]--;
1311     }
1312     if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR) && LnnConvAddrTypeToDiscType(type) == DISCOVERY_TYPE_BR) {
1313         RemoveCnnCode(&g_distributedNetLedger.cnnCode.connectionCode, info->uuid, DISCOVERY_TYPE_BR);
1314     }
1315     if (ClearAuthChannelId(info, type, authId) == REPORT_NONE) {
1316         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1317         return REPORT_NONE;
1318     }
1319     if (LnnConvAddrTypeToDiscType(type) == DISCOVERY_TYPE_WIFI) {
1320         LnnSetWiFiIp(info, LOCAL_IP);
1321     }
1322     LnnClearDiscoveryType(info, LnnConvAddrTypeToDiscType(type));
1323     if (info->discoveryType != 0) {
1324         LNN_LOGI(LNN_LEDGER, "after clear, not need to report offline. discoveryType=%{public}u", info->discoveryType);
1325         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1326         UpdateNetworkInfo(udid);
1327         if (type == CONNECTION_ADDR_WLAN) {
1328             NotifyMigrateDegrade(udid);
1329         }
1330         return REPORT_NONE;
1331     }
1332     if (!LnnIsNodeOnline(info)) {
1333         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1334         LNN_LOGI(LNN_LEDGER, "the state is already offline, no need to report offline");
1335         return REPORT_NONE;
1336     }
1337     LnnSetNodeConnStatus(info, STATUS_OFFLINE);
1338     LnnClearAuthTypeValue(&info->AuthTypeValue, ONLINE_HICHAIN);
1339     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1340     LNN_LOGI(LNN_LEDGER, "need to report offline");
1341     DfxRecordLnnSetNodeOfflineEnd(udid, (int32_t)MapGetSize(&map->udidMap), SOFTBUS_OK);
1342     return REPORT_OFFLINE;
1343 }
1344 
LnnGetBasicInfoByUdid(const char * udid,NodeBasicInfo * basicInfo)1345 int32_t LnnGetBasicInfoByUdid(const char *udid, NodeBasicInfo *basicInfo)
1346 {
1347     if (udid == NULL || basicInfo == NULL) {
1348         LNN_LOGE(LNN_LEDGER, "PARA ERROR");
1349         return SOFTBUS_INVALID_PARAM;
1350     }
1351     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
1352     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1353         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1354         return SOFTBUS_LOCK_ERR;
1355     }
1356     NodeInfo *info = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1357     int32_t ret = ConvertNodeInfoToBasicInfo(info, basicInfo);
1358     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1359     return ret;
1360 }
1361 
LnnRemoveNode(const char * udid)1362 void LnnRemoveNode(const char *udid)
1363 {
1364     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
1365     if (udid == NULL) {
1366         return;
1367     }
1368     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1369         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1370         return;
1371     }
1372     LnnMapErase(&map->udidMap, udid);
1373     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1374 }
1375 
LnnConvertDLidToUdid(const char * id,IdCategory type)1376 const char *LnnConvertDLidToUdid(const char *id, IdCategory type)
1377 {
1378     NodeInfo *info = NULL;
1379     if (id == NULL) {
1380         return NULL;
1381     }
1382     info = LnnGetNodeInfoById(id, type);
1383     if (info == NULL) {
1384         LNN_LOGE(LNN_LEDGER, "uuid not find node info.");
1385         return NULL;
1386     }
1387     return LnnGetDeviceUdid(info);
1388 }
1389 
LnnConvertDlId(const char * srcId,IdCategory srcIdType,IdCategory dstIdType,char * dstIdBuf,uint32_t dstIdBufLen)1390 int32_t LnnConvertDlId(const char *srcId, IdCategory srcIdType, IdCategory dstIdType,
1391     char *dstIdBuf, uint32_t dstIdBufLen)
1392 {
1393     NodeInfo *info = NULL;
1394     const char *id = NULL;
1395     int32_t rc = SOFTBUS_OK;
1396 
1397     if (srcId == NULL || dstIdBuf == NULL) {
1398         return SOFTBUS_INVALID_PARAM;
1399     }
1400     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1401         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1402         return SOFTBUS_LOCK_ERR;
1403     }
1404     info = LnnGetNodeInfoById(srcId, srcIdType);
1405     if (info == NULL) {
1406         LNN_LOGE(LNN_LEDGER, "no node info srcIdType=%{public}d", srcIdType);
1407         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1408         return SOFTBUS_NOT_FIND;
1409     }
1410     switch (dstIdType) {
1411         case CATEGORY_UDID:
1412             id = info->deviceInfo.deviceUdid;
1413             break;
1414         case CATEGORY_UUID:
1415             id = info->uuid;
1416             break;
1417         case CATEGORY_NETWORK_ID:
1418             id = info->networkId;
1419             break;
1420         default:
1421             SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1422             return SOFTBUS_INVALID_PARAM;
1423     }
1424     if (strcpy_s(dstIdBuf, dstIdBufLen, id) != EOK) {
1425         LNN_LOGE(LNN_LEDGER, "copy id fail");
1426         rc = SOFTBUS_MEM_ERR;
1427     }
1428     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1429     return rc;
1430 }
1431 
LnnGetLnnRelation(const char * id,IdCategory type,uint8_t * relation,uint32_t len)1432 int32_t LnnGetLnnRelation(const char *id, IdCategory type, uint8_t *relation, uint32_t len)
1433 {
1434     NodeInfo *info = NULL;
1435 
1436     if (id == NULL || relation == NULL) {
1437         return SOFTBUS_INVALID_PARAM;
1438     }
1439     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1440         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1441         return SOFTBUS_LOCK_ERR;
1442     }
1443     info = LnnGetNodeInfoById(id, type);
1444     if (info == NULL || !LnnIsNodeOnline(info)) {
1445         LNN_LOGE(LNN_LEDGER, "node not online");
1446         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1447         return SOFTBUS_NOT_FIND;
1448     }
1449     if (memcpy_s(relation, len, info->relation, CONNECTION_ADDR_MAX) != EOK) {
1450         LNN_LOGE(LNN_LEDGER, "copy relation fail");
1451         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1452         return SOFTBUS_MEM_ERR;
1453     }
1454     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1455     return SOFTBUS_OK;
1456 }
1457 
UpdateDevBasicInfoToDLedger(NodeInfo * newInfo,NodeInfo * oldInfo)1458 static void UpdateDevBasicInfoToDLedger(NodeInfo *newInfo, NodeInfo *oldInfo)
1459 {
1460     if (strcpy_s(oldInfo->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN, newInfo->deviceInfo.deviceName) != EOK) {
1461         LNN_LOGE(LNN_LEDGER, "strcpy_s deviceName to distributed ledger fail");
1462     }
1463     if (strcpy_s(oldInfo->deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, newInfo->deviceInfo.unifiedName) != EOK) {
1464         LNN_LOGE(LNN_LEDGER, "strcpy_s unifiedName to distributed ledger fail");
1465     }
1466     if (strcpy_s(oldInfo->deviceInfo.unifiedDefaultName, DEVICE_NAME_BUF_LEN, newInfo->deviceInfo.unifiedDefaultName) !=
1467         EOK) {
1468         LNN_LOGE(LNN_LEDGER, "strcpy_s unifiedDefaultName to distributed ledger fail");
1469     }
1470     if (strcpy_s(oldInfo->deviceInfo.nickName, DEVICE_NAME_BUF_LEN, newInfo->deviceInfo.nickName) != EOK) {
1471         LNN_LOGE(LNN_LEDGER, "strcpy_s nickName to distributed ledger fail");
1472     }
1473     if (strcpy_s(oldInfo->deviceInfo.deviceUdid, UDID_BUF_LEN, newInfo->deviceInfo.deviceUdid) != EOK) {
1474         LNN_LOGE(LNN_LEDGER, "strcpy_s deviceUdid to distributed ledger fail");
1475     }
1476     if (strcpy_s(oldInfo->deviceInfo.deviceVersion, DEVICE_VERSION_SIZE_MAX, newInfo->deviceInfo.deviceVersion) !=
1477         EOK) {
1478         LNN_LOGE(LNN_LEDGER, "strcpy_s deviceVersion to distributed ledger fail");
1479     }
1480     oldInfo->deviceInfo.deviceTypeId = newInfo->deviceInfo.deviceTypeId;
1481     oldInfo->isBleP2p = newInfo->isBleP2p;
1482     oldInfo->supportedProtocols = newInfo->supportedProtocols;
1483     oldInfo->wifiVersion = newInfo->wifiVersion;
1484     oldInfo->bleVersion = newInfo->bleVersion;
1485     oldInfo->accountId = newInfo->accountId;
1486     oldInfo->feature = newInfo->feature;
1487     oldInfo->connSubFeature = newInfo->connSubFeature;
1488     oldInfo->authCapacity = newInfo->authCapacity;
1489     oldInfo->deviceInfo.osType = newInfo->deviceInfo.osType;
1490     oldInfo->stateVersion = newInfo->stateVersion;
1491     oldInfo->updateTimestamp = newInfo->updateTimestamp;
1492     oldInfo->deviceSecurityLevel = newInfo->deviceSecurityLevel;
1493 }
1494 
UpdateDistributedLedger(NodeInfo * newInfo,NodeInfo * oldInfo)1495 static void UpdateDistributedLedger(NodeInfo *newInfo, NodeInfo *oldInfo)
1496 {
1497     if (newInfo == NULL || oldInfo == NULL) {
1498         LNN_LOGE(LNN_LEDGER, "param error");
1499         return;
1500     }
1501     if (strcpy_s(oldInfo->softBusVersion, VERSION_MAX_LEN, newInfo->softBusVersion) != EOK) {
1502         LNN_LOGE(LNN_LEDGER, "strcpy_s softBusVersion to distributed ledger fail");
1503     }
1504     if (strcpy_s(oldInfo->pkgVersion, VERSION_MAX_LEN, newInfo->pkgVersion) != EOK) {
1505         LNN_LOGE(LNN_LEDGER, "strcpy_s pkgVersion to distributed ledger fail");
1506     }
1507     if (strcpy_s(oldInfo->connectInfo.macAddr, MAC_LEN, newInfo->connectInfo.macAddr) != EOK) {
1508         LNN_LOGE(LNN_LEDGER, "strcpy_s macAddr to distributed ledger fail");
1509     }
1510     if (strcpy_s(oldInfo->deviceInfo.osVersion, OS_VERSION_BUF_LEN, newInfo->deviceInfo.osVersion) != EOK) {
1511         LNN_LOGE(LNN_LEDGER, "strcpy_s osVersion to distributed ledger fail");
1512     }
1513     if (strcpy_s(oldInfo->p2pInfo.p2pMac, MAC_LEN, newInfo->p2pInfo.p2pMac) != EOK) {
1514         LNN_LOGE(LNN_LEDGER, "strcpy_s p2pMac to distributed ledger fail");
1515     }
1516     if (memcpy_s((char *)oldInfo->rpaInfo.peerIrk, LFINDER_IRK_LEN, (char *)newInfo->rpaInfo.peerIrk,
1517         LFINDER_IRK_LEN) != EOK) {
1518         LNN_LOGE(LNN_LEDGER, "memcpy_s peerIrk to distributed ledger fail");
1519     }
1520     if (memcpy_s((char *)oldInfo->rpaInfo.publicAddress, LFINDER_MAC_ADDR_LEN, (char *)newInfo->rpaInfo.publicAddress,
1521         LFINDER_MAC_ADDR_LEN) != EOK) {
1522         LNN_LOGE(LNN_LEDGER, "memcpy_s publicAddress to distributed ledger fail");
1523     }
1524     if (memcpy_s((char *)oldInfo->cipherInfo.key, SESSION_KEY_LENGTH, newInfo->cipherInfo.key, SESSION_KEY_LENGTH) !=
1525         EOK) {
1526         LNN_LOGE(LNN_LEDGER, "memcpy_s cipherInfo key to distributed ledger fail");
1527     }
1528     if (memcpy_s((char *)oldInfo->cipherInfo.iv, BROADCAST_IV_LEN, newInfo->cipherInfo.iv, BROADCAST_IV_LEN) != EOK) {
1529         LNN_LOGE(LNN_LEDGER, "memcpy_s cipherInfo iv to distributed ledger fail");
1530     }
1531     UpdateDevBasicInfoToDLedger(newInfo, oldInfo);
1532 }
1533 
IsIgnoreUpdateToLedger(int32_t oldStateVersion,uint64_t oldTimestamp,int32_t newStateVersion,uint64_t newTimestamp)1534 static bool IsIgnoreUpdateToLedger(
1535     int32_t oldStateVersion, uint64_t oldTimestamp, int32_t newStateVersion, uint64_t newTimestamp)
1536 {
1537     bool isIgnore = oldTimestamp > newTimestamp || (oldTimestamp == 0 && oldStateVersion > newStateVersion);
1538     if (isIgnore) {
1539         LNN_LOGE(LNN_BUILDER,
1540             "sync info is older, oldDLeger.stateVersion=%{public}d, oldDLegerTimestamp=%{public}" PRIu64
1541             ", newSyncInfo.stateVersion=%{public}d, newTimestamp=%{public}" PRIu64 "",
1542             oldStateVersion, oldTimestamp, newStateVersion, newTimestamp);
1543     }
1544     return isIgnore;
1545 }
1546 
LnnUpdateDistributedNodeInfo(NodeInfo * newInfo,const char * udid)1547 int32_t LnnUpdateDistributedNodeInfo(NodeInfo *newInfo, const char *udid)
1548 {
1549     if (newInfo == NULL || udid == NULL) {
1550         LNN_LOGE(LNN_LEDGER, "param error");
1551         return SOFTBUS_INVALID_PARAM;
1552     }
1553     DoubleHashMap *map = &g_distributedNetLedger.distributedInfo;
1554     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1555         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1556         return SOFTBUS_LOCK_ERR;
1557     }
1558     NodeInfo *oldInfo = (NodeInfo *)LnnMapGet(&map->udidMap, udid);
1559     if (oldInfo == NULL) {
1560         LNN_LOGI(LNN_LEDGER, "no this device info in ledger, need to insert");
1561         int32_t ret = LnnMapSet(&map->udidMap, udid, newInfo, sizeof(NodeInfo));
1562         if (ret != SOFTBUS_OK) {
1563             LNN_LOGE(LNN_LEDGER, "lnn map set failed, ret=%{public}d", ret);
1564             SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1565             return SOFTBUS_ERR;
1566         }
1567         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1568         LNN_LOGD(LNN_LEDGER, "DB data new device nodeinfo insert to distributed ledger success.");
1569         return SOFTBUS_OK;
1570     }
1571     if (IsIgnoreUpdateToLedger(oldInfo->stateVersion, oldInfo->updateTimestamp, newInfo->stateVersion,
1572         newInfo->updateTimestamp)) {
1573         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1574         return SOFTBUS_OK;
1575     }
1576     UpdateDistributedLedger(newInfo, oldInfo);
1577     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1578     LNN_LOGD(LNN_LEDGER, "DB data update to distributed ledger success.");
1579     return SOFTBUS_OK;
1580 }
1581 
GetAllOnlineAndMetaNodeInfo(NodeBasicInfo ** info,int32_t * infoNum,bool isNeedMeta)1582 static int32_t GetAllOnlineAndMetaNodeInfo(NodeBasicInfo **info, int32_t *infoNum, bool isNeedMeta)
1583 {
1584     if (info == NULL || infoNum == NULL) {
1585         LNN_LOGE(LNN_LEDGER, "key params are null");
1586         return SOFTBUS_INVALID_PARAM;
1587     }
1588     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1589         LNN_LOGE(LNN_LEDGER, "lock mutex fail!");
1590         return SOFTBUS_LOCK_ERR;
1591     }
1592     int32_t ret = SOFTBUS_ERR;
1593     do {
1594         *info = NULL;
1595         if (GetDLOnlineNodeNumLocked(infoNum, isNeedMeta) != SOFTBUS_OK) {
1596             LNN_LOGE(LNN_LEDGER, "get online node num failed");
1597             break;
1598         }
1599         if (*infoNum == 0) {
1600             ret = SOFTBUS_OK;
1601             break;
1602         }
1603         *info = (NodeBasicInfo*)SoftBusCalloc((*infoNum) * sizeof(NodeBasicInfo));
1604         if (*info == NULL) {
1605             LNN_LOGE(LNN_LEDGER, "malloc node info buffer failed");
1606             break;
1607         }
1608         if (FillDLOnlineNodeInfoLocked(*info, *infoNum, isNeedMeta) != SOFTBUS_OK) {
1609             LNN_LOGE(LNN_LEDGER, "fill online node num failed");
1610             break;
1611         }
1612         ret = SOFTBUS_OK;
1613     } while (false);
1614     if (ret != SOFTBUS_OK) {
1615         if (*info != NULL) {
1616             SoftBusFree(*info);
1617             *info = NULL;
1618         }
1619         *infoNum = 0;
1620     }
1621     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1622     return ret;
1623 }
1624 
LnnIsLSANode(const NodeBasicInfo * info)1625 bool LnnIsLSANode(const NodeBasicInfo *info)
1626 {
1627     NodeInfo *nodeInfo = LnnGetNodeInfoById(info->networkId, CATEGORY_NETWORK_ID);
1628     if (nodeInfo != NULL && LnnHasDiscoveryType(nodeInfo, DISCOVERY_TYPE_LSA)) {
1629         return true;
1630     }
1631     return false;
1632 }
1633 
LnnGetAllOnlineNodeNum(int32_t * nodeNum)1634 int32_t LnnGetAllOnlineNodeNum(int32_t *nodeNum)
1635 {
1636     if (nodeNum == NULL) {
1637         return SOFTBUS_INVALID_PARAM;
1638     }
1639     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1640         LNN_LOGE(LNN_LEDGER, "lock fail");
1641         return SOFTBUS_LOCK_ERR;
1642     }
1643     /* node num include meta node */
1644     if (GetDLOnlineNodeNumLocked(nodeNum, true) != SOFTBUS_OK) {
1645         LNN_LOGE(LNN_LEDGER, "get online node num failed");
1646         (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1647         return SOFTBUS_NOT_FIND;
1648     }
1649     (void)SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1650     return SOFTBUS_OK;
1651 }
1652 
LnnGetAllOnlineNodeInfo(NodeBasicInfo ** info,int32_t * infoNum)1653 int32_t LnnGetAllOnlineNodeInfo(NodeBasicInfo **info, int32_t *infoNum)
1654 {
1655     return GetAllOnlineAndMetaNodeInfo(info, infoNum, false);
1656 }
1657 
LnnGetAllOnlineAndMetaNodeInfo(NodeBasicInfo ** info,int32_t * infoNum)1658 int32_t LnnGetAllOnlineAndMetaNodeInfo(NodeBasicInfo **info, int32_t *infoNum)
1659 {
1660     return GetAllOnlineAndMetaNodeInfo(info, infoNum, true);
1661 }
1662 
SoftBusDumpBusCenterRemoteDeviceInfo(int32_t fd)1663 int32_t SoftBusDumpBusCenterRemoteDeviceInfo(int32_t fd)
1664 {
1665     SOFTBUS_DPRINTF(fd, "-----RemoteDeviceInfo-----\n");
1666     NodeBasicInfo *remoteNodeInfo = NULL;
1667     int32_t infoNum = 0;
1668     if (LnnGetAllOnlineNodeInfo(&remoteNodeInfo, &infoNum) != 0) {
1669         LNN_LOGE(LNN_LEDGER, "LnnGetAllOnlineNodeInfo failed");
1670         return SOFTBUS_ERR;
1671     }
1672     SOFTBUS_DPRINTF(fd, "remote device num = %d\n", infoNum);
1673     for (int32_t i = 0; i < infoNum; i++) {
1674         SOFTBUS_DPRINTF(fd, "\n[NO.%d]\n", i + 1);
1675         SoftBusDumpBusCenterPrintInfo(fd, remoteNodeInfo + i);
1676     }
1677     SoftBusFree(remoteNodeInfo);
1678     return SOFTBUS_OK;
1679 }
1680 
LnnInitDistributedLedger(void)1681 int32_t LnnInitDistributedLedger(void)
1682 {
1683     if (g_distributedNetLedger.status == DL_INIT_SUCCESS) {
1684         LNN_LOGI(LNN_LEDGER, "Distributed Ledger already init");
1685         return SOFTBUS_OK;
1686     }
1687 
1688     if (InitDistributedInfo(&g_distributedNetLedger.distributedInfo) != SOFTBUS_OK) {
1689         LNN_LOGE(LNN_LEDGER, "InitDistributedInfo ERROR");
1690         g_distributedNetLedger.status = DL_INIT_FAIL;
1691         return SOFTBUS_ERR;
1692     }
1693 
1694     if (InitConnectionCode(&g_distributedNetLedger.cnnCode) != SOFTBUS_OK) {
1695         LNN_LOGE(LNN_LEDGER, "InitConnectionCode ERROR");
1696         g_distributedNetLedger.status = DL_INIT_FAIL;
1697         return SOFTBUS_ERR;
1698     }
1699     if (SoftBusMutexInit(&g_distributedNetLedger.lock, NULL) != SOFTBUS_OK) {
1700         g_distributedNetLedger.status = DL_INIT_FAIL;
1701         return SOFTBUS_ERR;
1702     }
1703     if (SoftBusRegBusCenterVarDump((char*)SOFTBUS_BUSCENTER_DUMP_REMOTEDEVICEINFO,
1704         &SoftBusDumpBusCenterRemoteDeviceInfo) != SOFTBUS_OK) {
1705         LNN_LOGE(LNN_LEDGER, "SoftBusRegBusCenterVarDump regist fail");
1706         return SOFTBUS_ERR;
1707     }
1708     g_distributedNetLedger.status = DL_INIT_SUCCESS;
1709     return SOFTBUS_OK;
1710 }
1711 
LnnGetOnlineNodeByUdidHash(const char * recvUdidHash)1712 const NodeInfo *LnnGetOnlineNodeByUdidHash(const char *recvUdidHash)
1713 {
1714     int32_t i;
1715     int32_t infoNum = 0;
1716     NodeBasicInfo *info = NULL;
1717     unsigned char shortUdidHash[SHORT_UDID_HASH_LEN + 1] = {0};
1718 
1719     if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) {
1720         LNN_LOGE(LNN_LEDGER, "get all online node info fail");
1721         return NULL;
1722     }
1723     if (info == NULL || infoNum == 0) {
1724         if (info != NULL) {
1725             SoftBusFree(info);
1726         }
1727         return NULL;
1728     }
1729     for (i = 0; i < infoNum; ++i) {
1730         const NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID);
1731         if (nodeInfo == NULL) {
1732             LNN_LOGI(LNN_LEDGER, "nodeInfo is null");
1733             continue;
1734         }
1735         if (GenerateStrHashAndConvertToHexString((const unsigned char *)nodeInfo->deviceInfo.deviceUdid,
1736             SHORT_UDID_HASH_LEN, shortUdidHash, SHORT_UDID_HASH_LEN + 1) != SOFTBUS_OK) {
1737             continue;
1738         }
1739         if (memcmp(shortUdidHash, recvUdidHash, SHORT_UDID_HASH_LEN) == 0) {
1740             char *anoyUdid = NULL;
1741             char *anoyUdidHash = NULL;
1742             Anonymize(nodeInfo->deviceInfo.deviceUdid, &anoyUdid);
1743             Anonymize((const char *)shortUdidHash, &anoyUdidHash);
1744             LNN_LOGI(LNN_LEDGER, "node is online. nodeUdid=%{public}s, shortUdidHash=%{public}s",
1745                 anoyUdid, anoyUdidHash);
1746             AnonymizeFree(anoyUdid);
1747             AnonymizeFree(anoyUdidHash);
1748             SoftBusFree(info);
1749             return nodeInfo;
1750         }
1751     }
1752     SoftBusFree(info);
1753     return NULL;
1754 }
1755 
RefreshDeviceOnlineStateInfo(DeviceInfo * device,const InnerDeviceInfoAddtions * additions)1756 static void RefreshDeviceOnlineStateInfo(DeviceInfo *device, const InnerDeviceInfoAddtions *additions)
1757 {
1758     if (additions->medium == COAP || additions->medium == BLE) {
1759         device->isOnline = ((LnnGetOnlineNodeByUdidHash(device->devId)) != NULL) ? true : false;
1760     }
1761 }
1762 
LnnRefreshDeviceOnlineStateAndDevIdInfo(const char * pkgName,DeviceInfo * device,const InnerDeviceInfoAddtions * additions)1763 void LnnRefreshDeviceOnlineStateAndDevIdInfo(const char *pkgName, DeviceInfo *device,
1764     const InnerDeviceInfoAddtions *additions)
1765 {
1766     (void)pkgName;
1767     RefreshDeviceOnlineStateInfo(device, additions);
1768     if (device->devId[0] != '\0') {
1769         char *anoyUdidHash = NULL;
1770         Anonymize(device->devId, &anoyUdidHash);
1771         LNN_LOGI(LNN_LEDGER, "device found. medium=%{public}d, udidhash=%{public}s, onlineStatus=%{public}d",
1772             additions->medium, anoyUdidHash, device->isOnline);
1773         AnonymizeFree(anoyUdidHash);
1774     }
1775 }
1776 
LnnGetOsTypeByNetworkId(const char * networkId,int32_t * osType)1777 int32_t LnnGetOsTypeByNetworkId(const char *networkId, int32_t *osType)
1778 {
1779     if (networkId == NULL || osType == NULL) {
1780         LNN_LOGE(LNN_LEDGER, "invalid param");
1781         return SOFTBUS_INVALID_PARAM;
1782     }
1783     if (SoftBusMutexLock(&g_distributedNetLedger.lock) != 0) {
1784         LNN_LOGE(LNN_LEDGER, "lock mutex fail");
1785         return SOFTBUS_LOCK_ERR;
1786     }
1787     NodeInfo *nodeInfo = LnnGetNodeInfoById(networkId, CATEGORY_NETWORK_ID);
1788     if (nodeInfo == NULL) {
1789         SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1790         char *anonyNetworkId = NULL;
1791         Anonymize(networkId, &anonyNetworkId);
1792         LNN_LOGE(LNN_LEDGER, "get info by networkId=%{public}s failed", anonyNetworkId);
1793         AnonymizeFree(anonyNetworkId);
1794         return SOFTBUS_NOT_FIND;
1795     }
1796     *osType = nodeInfo->deviceInfo.osType;
1797     SoftBusMutexUnlock(&g_distributedNetLedger.lock);
1798     return SOFTBUS_OK;
1799 }
1800 
IsAvailableMeta(const char * peerNetWorkId)1801 bool IsAvailableMeta(const char *peerNetWorkId)
1802 {
1803     int32_t value = 0;
1804     int32_t ret = LnnGetRemoteNumInfo(peerNetWorkId, NUM_KEY_META_NODE, &value);
1805     if (ret != SOFTBUS_OK) {
1806         LNN_LOGE(LNN_LEDGER, "GetAuthType fail, ret=%{public}d", ret);
1807         return false;
1808     }
1809     return ((uint32_t)value & (1 << ONLINE_METANODE));
1810 }
1811