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_net_builder.h"
17
18 #include <securec.h>
19 #include <stdlib.h>
20 #include <inttypes.h>
21
22 #include "auth_interface.h"
23 #include "bus_center_event.h"
24 #include "bus_center_manager.h"
25 #include "common_list.h"
26 #include "lnn_async_callback_utils.h"
27 #include "lnn_connection_addr_utils.h"
28 #include "lnn_connection_fsm.h"
29 #include "lnn_devicename_info.h"
30 #include "lnn_discovery_manager.h"
31 #include "lnn_distributed_net_ledger.h"
32 #include "lnn_fast_offline.h"
33 #include "lnn_heartbeat_utils.h"
34 #include "lnn_local_net_ledger.h"
35 #include "lnn_network_id.h"
36 #include "lnn_network_info.h"
37 #include "lnn_network_manager.h"
38 #include "lnn_node_weight.h"
39 #include "lnn_p2p_info.h"
40 #include "lnn_physical_subnet_manager.h"
41 #include "lnn_sync_info_manager.h"
42 #include "lnn_topo_manager.h"
43 #include "softbus_adapter_mem.h"
44 #include "softbus_errcode.h"
45 #include "softbus_feature_config.h"
46 #include "softbus_json_utils.h"
47 #include "softbus_log.h"
48
49 #define LNN_CONN_CAPABILITY_MSG_LEN 8
50 #define DEFAULT_MAX_LNN_CONNECTION_COUNT 10
51 #define JSON_KEY_MASTER_UDID "MasterUdid"
52 #define JSON_KEY_MASTER_WEIGHT "MasterWeight"
53 #define NOT_TRUSTED_DEVICE_MSG_DELAY 5000
54
55 typedef enum {
56 LNN_MSG_ID_ELECT,
57 LNN_MSG_ID_MAX
58 } LnnMsgType;
59
60 typedef enum {
61 MSG_TYPE_JOIN_LNN = 0,
62 MSG_TYPE_DISCOVERY_DEVICE,
63 MSG_TYPE_CLEAN_CONN_FSM,
64 MSG_TYPE_VERIFY_RESULT,
65 MSG_TYPE_DEVICE_VERIFY_PASS,
66 MSG_TYPE_DEVICE_DISCONNECT = 5,
67 MSG_TYPE_DEVICE_NOT_TRUSTED,
68 MSG_TYPE_LEAVE_LNN,
69 MSG_TYPE_SYNC_OFFLINE_FINISH,
70 MSG_TYPE_NODE_STATE_CHANGED,
71 MSG_TYPE_MASTER_ELECT = 10,
72 MSG_TYPE_LEAVE_INVALID_CONN,
73 MSG_TYPE_LEAVE_BY_ADDR_TYPE,
74 MSG_TYPE_LEAVE_SPECIFIC,
75 MSG_TYPE_JOIN_METANODE,
76 MSG_TYPE_JOIN_METANODE_AUTH_PASS,
77 MSG_TYPE_LEAVE_METANODE,
78 MSG_TYPE_JOIN_METANODE_AUTH_FAIL,
79 MSG_TYPE_MAX,
80 } NetBuilderMessageType;
81
82 typedef int32_t (*NetBuilderMessageProcess)(const void *para);
83
84 typedef struct {
85 ListNode node;
86 ConnectionAddr addr;
87 bool needReportFailure;
88 } PendingJoinRequestNode;
89
90 typedef struct {
91 NodeType nodeType;
92
93 /* connection fsm list */
94 ListNode fsmList;
95 ListNode pendingList;
96 ListNode metaNodeList;
97 /* connection count */
98 int32_t connCount;
99
100 SoftBusLooper *looper;
101 SoftBusHandler handler;
102
103 int32_t maxConnCount;
104 int32_t maxConcurrentCount;
105 bool isInit;
106 } NetBuilder;
107
108 typedef struct {
109 uint32_t requestId;
110 int32_t retCode;
111 int64_t authId;
112 NodeInfo *nodeInfo;
113 } VerifyResultMsgPara;
114
115 typedef struct {
116 ConnectionAddr addr;
117 int64_t authId;
118 NodeInfo *nodeInfo;
119 } DeviceVerifyPassMsgPara;
120
121 typedef struct {
122 char networkId[NETWORK_ID_BUF_LEN];
123 char masterUdid[UDID_BUF_LEN];
124 int32_t masterWeight;
125 } ElectMsgPara;
126
127 typedef struct {
128 char oldNetworkId[NETWORK_ID_BUF_LEN];
129 ConnectionAddrType addrType;
130 char newNetworkId[NETWORK_ID_BUF_LEN];
131 } LeaveInvalidConnMsgPara;
132
133 typedef struct {
134 char networkId[NETWORK_ID_BUF_LEN];
135 ConnectionAddrType addrType;
136 } SpecificLeaveMsgPara;
137
138 typedef struct {
139 ConnectionAddr addr;
140 CustomData customData;
141 } ConnectionAddrKey;
142
143 typedef struct {
144 MetaJoinRequestNode *metaJoinNode;
145 int32_t reason;
146 } MetaReason;
147
148 typedef struct {
149 MetaJoinRequestNode *metaJoinNode;
150 int64_t authMetaId;
151 NodeInfo info;
152 } MetaAuthInfo;
153
154 static NetBuilder g_netBuilder;
155
NetBuilderConfigInit(void)156 static void NetBuilderConfigInit(void)
157 {
158 if (SoftbusGetConfig(SOFTBUS_INT_MAX_LNN_CONNECTION_CNT,
159 (unsigned char*)&g_netBuilder.maxConnCount, sizeof(g_netBuilder.maxConnCount)) != SOFTBUS_OK) {
160 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get lnn max connection count fail, use default value");
161 g_netBuilder.maxConnCount = DEFAULT_MAX_LNN_CONNECTION_COUNT;
162 }
163 if (SoftbusGetConfig(SOFTBUS_INT_LNN_MAX_CONCURRENT_NUM,
164 (unsigned char*)&g_netBuilder.maxConcurrentCount, sizeof(g_netBuilder.maxConnCount)) != SOFTBUS_OK) {
165 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get lnn max conncurent count fail, use default value");
166 g_netBuilder.maxConcurrentCount = 0;
167 }
168 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "lnn config is %d,%d",
169 g_netBuilder.maxConnCount, g_netBuilder.maxConcurrentCount);
170 }
171
CreateNetBuilderMessage(int32_t msgType,void * para)172 static SoftBusMessage *CreateNetBuilderMessage(int32_t msgType, void *para)
173 {
174 SoftBusMessage *msg = (SoftBusMessage *)SoftBusCalloc(sizeof(SoftBusMessage));
175 if (msg == NULL) {
176 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc softbus message failed");
177 return NULL;
178 }
179 msg->what = msgType;
180 msg->obj = para;
181 msg->handler = &g_netBuilder.handler;
182 return msg;
183 }
184
PostMessageToHandler(int32_t msgType,void * para)185 static int32_t PostMessageToHandler(int32_t msgType, void *para)
186 {
187 SoftBusMessage *msg = CreateNetBuilderMessage(msgType, para);
188 if (msg == NULL) {
189 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create softbus message failed");
190 return SOFTBUS_ERR;
191 }
192 g_netBuilder.looper->PostMessage(g_netBuilder.looper, msg);
193 return SOFTBUS_OK;
194 }
195
FindConnectionFsmByAddr(const ConnectionAddr * addr)196 static LnnConnectionFsm *FindConnectionFsmByAddr(const ConnectionAddr *addr)
197 {
198 LnnConnectionFsm *item = NULL;
199
200 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
201 if (LnnIsSameConnectionAddr(addr, &item->connInfo.addr)) {
202 return item;
203 }
204 }
205 return NULL;
206 }
207
FindMetaNodeByAddr(const ConnectionAddr * addr)208 static MetaJoinRequestNode *FindMetaNodeByAddr(const ConnectionAddr *addr)
209 {
210 MetaJoinRequestNode *item = NULL;
211
212 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.metaNodeList, MetaJoinRequestNode, node) {
213 if (LnnIsSameConnectionAddr(addr, &item->addr)) {
214 return item;
215 }
216 }
217 return NULL;
218 }
219
FindMetaNodeByRequestId(uint32_t requestId)220 static MetaJoinRequestNode *FindMetaNodeByRequestId(uint32_t requestId)
221 {
222 MetaJoinRequestNode *item = NULL;
223
224 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.metaNodeList, MetaJoinRequestNode, node) {
225 if (item->requestId == requestId) {
226 return item;
227 }
228 }
229 return NULL;
230 }
231
FindConnectionFsmByRequestId(uint32_t requestId)232 static LnnConnectionFsm *FindConnectionFsmByRequestId(uint32_t requestId)
233 {
234 LnnConnectionFsm *item = NULL;
235
236 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
237 if (item->connInfo.requestId == requestId) {
238 return item;
239 }
240 }
241 return NULL;
242 }
243
FindConnectionFsmByAuthId(int64_t authId)244 static LnnConnectionFsm *FindConnectionFsmByAuthId(int64_t authId)
245 {
246 LnnConnectionFsm *item = NULL;
247
248 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
249 if (item->connInfo.authId == authId) {
250 return item;
251 }
252 }
253 return NULL;
254 }
255
FindConnectionFsmByNetworkId(const char * networkId)256 static LnnConnectionFsm *FindConnectionFsmByNetworkId(const char *networkId)
257 {
258 LnnConnectionFsm *item = NULL;
259
260 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
261 if (strcmp(networkId, item->connInfo.peerNetworkId) == 0) {
262 return item;
263 }
264 }
265 return NULL;
266 }
267
FindConnectionFsmByConnFsmId(uint16_t connFsmId)268 static LnnConnectionFsm *FindConnectionFsmByConnFsmId(uint16_t connFsmId)
269 {
270 LnnConnectionFsm *item = NULL;
271
272 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
273 if (connFsmId == item->id) {
274 return item;
275 }
276 }
277 return NULL;
278 }
279
StartNewConnectionFsm(const ConnectionAddr * addr)280 static LnnConnectionFsm *StartNewConnectionFsm(const ConnectionAddr *addr)
281 {
282 LnnConnectionFsm *connFsm = NULL;
283
284 if (g_netBuilder.connCount >= g_netBuilder.maxConnCount) {
285 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "current connection is exceed max limit: %d",
286 g_netBuilder.connCount);
287 return NULL;
288 }
289 connFsm = LnnCreateConnectionFsm(addr);
290 if (connFsm == NULL) {
291 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create connection fsm failed");
292 return NULL;
293 }
294 connFsm->statisticData.beginTime = LnnUpTimeMs();
295 if (LnnStartConnectionFsm(connFsm) != SOFTBUS_OK) {
296 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "start connection fsm[id=%u] failed", connFsm->id);
297 LnnDestroyConnectionFsm(connFsm);
298 return NULL;
299 }
300 ListAdd(&g_netBuilder.fsmList, &connFsm->node);
301 ++g_netBuilder.connCount;
302 return connFsm;
303 }
304
IsNodeOnline(const char * networkId)305 static bool IsNodeOnline(const char *networkId)
306 {
307 NodeInfo *nodeInfo = LnnGetNodeInfoById(networkId, CATEGORY_NETWORK_ID);
308 if (nodeInfo != NULL && LnnIsNodeOnline(nodeInfo)) {
309 return true;
310 }
311 return false;
312 }
313
UpdateLocalMasterNode(bool isCurrentNode,const char * masterUdid,int32_t weight)314 static void UpdateLocalMasterNode(bool isCurrentNode, const char *masterUdid, int32_t weight)
315 {
316 if (LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, masterUdid) != SOFTBUS_OK) {
317 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set local master udid failed");
318 return;
319 }
320 if (LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, weight) != SOFTBUS_OK) {
321 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "set local master weight failed");
322 }
323 LnnNotifyMasterNodeChanged(isCurrentNode, masterUdid, weight);
324 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "update local master weight=%d", weight);
325 }
326
SyncElectMessage(const char * networkId)327 static int32_t SyncElectMessage(const char *networkId)
328 {
329 char masterUdid[UDID_BUF_LEN] = {0};
330 int32_t masterWeight;
331 char *data = NULL;
332 cJSON *json = NULL;
333 int32_t rc;
334
335 if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, masterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
336 LnnGetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, &masterWeight) != SOFTBUS_OK) {
337 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local master node info failed");
338 return SOFTBUS_INVALID_PARAM;
339 }
340 json = cJSON_CreateObject();
341 if (json == NULL) {
342 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create elect json object failed");
343 return SOFTBUS_ERR;
344 }
345 if (!AddStringToJsonObject(json, JSON_KEY_MASTER_UDID, masterUdid) ||
346 !AddNumberToJsonObject(json, JSON_KEY_MASTER_WEIGHT, masterWeight)) {
347 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "add elect info to json failed");
348 cJSON_Delete(json);
349 return SOFTBUS_ERR;
350 }
351 data = cJSON_PrintUnformatted(json);
352 cJSON_Delete(json);
353 if (data == NULL) {
354 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "format elect packet fail");
355 return SOFTBUS_ERR;
356 }
357 rc = LnnSendSyncInfoMsg(LNN_INFO_TYPE_MASTER_ELECT, networkId, (uint8_t *)data, strlen(data) + 1, NULL);
358 cJSON_free(data);
359 return rc;
360 }
361
SendElectMessageToAll(const char * skipNetworkId)362 static void SendElectMessageToAll(const char *skipNetworkId)
363 {
364 LnnConnectionFsm *item = NULL;
365
366 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
367 if (skipNetworkId != NULL && strcmp(item->connInfo.peerNetworkId, skipNetworkId) == 0) {
368 continue;
369 }
370 if (!IsNodeOnline(item->connInfo.peerNetworkId)) {
371 continue;
372 }
373 if (SyncElectMessage(item->connInfo.peerNetworkId) != SOFTBUS_OK) {
374 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "sync elect info to connFsm(%u) failed", item->id);
375 }
376 }
377 }
378
NeedPendingJoinRequest(void)379 static bool NeedPendingJoinRequest(void)
380 {
381 int32_t count = 0;
382 LnnConnectionFsm *item = NULL;
383
384 if (g_netBuilder.maxConcurrentCount == 0) { // do not limit concurent
385 return false;
386 }
387 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
388 if (item->isDead) {
389 continue;
390 }
391 if ((item->connInfo.flag & LNN_CONN_INFO_FLAG_ONLINE) != 0) {
392 continue;
393 }
394 ++count;
395 if (count >= g_netBuilder.maxConcurrentCount) {
396 return true;
397 }
398 }
399 return false;
400 }
401
TryPendingJoinRequest(const ConnectionAddr * addr,bool needReportFailure)402 static bool TryPendingJoinRequest(const ConnectionAddr *addr, bool needReportFailure)
403 {
404 PendingJoinRequestNode *request = NULL;
405
406 if (!NeedPendingJoinRequest()) {
407 return false;
408 }
409 request = (PendingJoinRequestNode *)SoftBusCalloc(sizeof(PendingJoinRequestNode));
410 if (request == NULL) {
411 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc pending join request fail, go on it");
412 return false;
413 }
414 ListInit(&request->node);
415 request->addr = *addr;
416 request->needReportFailure = needReportFailure;
417 ListTailInsert(&g_netBuilder.pendingList, &request->node);
418 return true;
419 }
420
TryJoinRequestMetaNode(const ConnectionAddr * addr,bool needReportFailure)421 static MetaJoinRequestNode *TryJoinRequestMetaNode(const ConnectionAddr *addr, bool needReportFailure)
422 {
423 MetaJoinRequestNode *request = NULL;
424
425 request = (MetaJoinRequestNode *)SoftBusCalloc(sizeof(MetaJoinRequestNode));
426 if (request == NULL) {
427 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc MetaNode join request fail, go on it");
428 return NULL;
429 }
430 ListInit(&request->node);
431 request->addr = *addr;
432 request->needReportFailure = needReportFailure;
433 ListTailInsert(&g_netBuilder.metaNodeList, &request->node);
434 return request;
435 }
436
PostJoinRequestToConnFsm(LnnConnectionFsm * connFsm,const ConnectionAddr * addr,bool needReportFailure)437 static int32_t PostJoinRequestToConnFsm(LnnConnectionFsm *connFsm, const ConnectionAddr *addr, bool needReportFailure)
438 {
439 int32_t rc = SOFTBUS_OK;
440 bool isCreate = false;
441
442 if (connFsm == NULL) {
443 connFsm = FindConnectionFsmByAddr(addr);
444 }
445 if (connFsm == NULL || connFsm->isDead) {
446 connFsm = StartNewConnectionFsm(addr);
447 isCreate = true;
448 }
449 if (connFsm == NULL || LnnSendJoinRequestToConnFsm(connFsm) != SOFTBUS_OK) {
450 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "process join lnn request failed");
451 if (needReportFailure) {
452 LnnNotifyJoinResult((ConnectionAddr *)addr, NULL, SOFTBUS_ERR);
453 }
454 if (connFsm != NULL && isCreate) {
455 LnnDestroyConnectionFsm(connFsm);
456 }
457 rc = SOFTBUS_ERR;
458 }
459 if (rc == SOFTBUS_OK) {
460 connFsm->connInfo.flag |=
461 (needReportFailure ? LNN_CONN_INFO_FLAG_JOIN_REQUEST : LNN_CONN_INFO_FLAG_JOIN_AUTO);
462 }
463 return rc;
464 }
465
PostJoinRequestToMetaNode(MetaJoinRequestNode * metaJoinNode,const ConnectionAddr * addr,CustomData * customData,bool needReportFailure)466 static int32_t PostJoinRequestToMetaNode(MetaJoinRequestNode *metaJoinNode, const ConnectionAddr *addr,
467 CustomData *customData, bool needReportFailure)
468 {
469 int32_t rc = SOFTBUS_OK;
470 if (OnJoinMetaNode(metaJoinNode, customData) != SOFTBUS_OK) {
471 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "PostJoin Request To MetaNode failed");
472 rc = SOFTBUS_ERR;
473 if (needReportFailure) {
474 MetaNodeNotifyJoinResult((ConnectionAddr *)addr, NULL, SOFTBUS_ERR);
475 }
476 }
477 return rc;
478 }
479
TryRemovePendingJoinRequest(void)480 static void TryRemovePendingJoinRequest(void)
481 {
482 PendingJoinRequestNode *item = NULL;
483
484 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.pendingList, PendingJoinRequestNode, node) {
485 if (NeedPendingJoinRequest()) {
486 return;
487 }
488 ListDelete(&item->node);
489 if (PostJoinRequestToConnFsm(NULL, &item->addr, item->needReportFailure) != SOFTBUS_OK) {
490 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post pending join request failed");
491 }
492 SoftBusFree(item);
493 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "remove a pending join request");
494 break;
495 }
496 }
497
TrySendJoinLNNRequest(const ConnectionAddr * addr,bool needReportFailure)498 static int32_t TrySendJoinLNNRequest(const ConnectionAddr *addr, bool needReportFailure)
499 {
500 LnnConnectionFsm *connFsm = NULL;
501 int32_t rc;
502
503 if (addr == NULL) {
504 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "addr is null");
505 return SOFTBUS_INVALID_PARAM;
506 }
507 connFsm = FindConnectionFsmByAddr(addr);
508 if (connFsm == NULL || connFsm->isDead) {
509 if (TryPendingJoinRequest(addr, needReportFailure)) {
510 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "join request is pending");
511 SoftBusFree((void *)addr);
512 return SOFTBUS_OK;
513 }
514 }
515 rc = PostJoinRequestToConnFsm(connFsm, addr, needReportFailure);
516 SoftBusFree((void *)addr);
517 return rc;
518 }
519
TrySendJoinMetaNodeRequest(const ConnectionAddrKey * addrDataKey,bool needReportFailure)520 static int32_t TrySendJoinMetaNodeRequest(const ConnectionAddrKey *addrDataKey, bool needReportFailure)
521 {
522 if (addrDataKey == NULL) {
523 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: addrDataKey is NULL");
524 return SOFTBUS_INVALID_PARAM;
525 }
526 const ConnectionAddr *addr = &addrDataKey->addr;
527 CustomData customData = addrDataKey->customData;
528 MetaJoinRequestNode *metaJoinNode = NULL;
529 int32_t rc;
530 metaJoinNode = FindMetaNodeByAddr(addr);
531 if (metaJoinNode == NULL) {
532 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "TrySendJoinMetaNodeRequest not find metaJoinNode");
533 metaJoinNode = TryJoinRequestMetaNode(addr, needReportFailure);
534 if (metaJoinNode == NULL) {
535 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "join request is pending");
536 SoftBusFree((void *)addrDataKey);
537 return SOFTBUS_ERR;
538 }
539 }
540 rc = PostJoinRequestToMetaNode(metaJoinNode, addr, &customData, needReportFailure);
541 SoftBusFree((void *)addrDataKey);
542 return rc;
543 }
544
ProcessJoinLNNRequest(const void * para)545 static int32_t ProcessJoinLNNRequest(const void *para)
546 {
547 return TrySendJoinLNNRequest((const ConnectionAddr *)para, true);
548 }
549
ProcessJoinMetaNodeRequest(const void * para)550 static int32_t ProcessJoinMetaNodeRequest(const void *para)
551 {
552 return TrySendJoinMetaNodeRequest((const ConnectionAddrKey *)para, true);
553 }
554
ProcessDevDiscoveryRequest(const void * para)555 static int32_t ProcessDevDiscoveryRequest(const void *para)
556 {
557 return TrySendJoinLNNRequest((const ConnectionAddr *)para, false);
558 }
559
InitiateNewNetworkOnline(ConnectionAddrType addrType,const char * networkId)560 static void InitiateNewNetworkOnline(ConnectionAddrType addrType, const char *networkId)
561 {
562 LnnConnectionFsm *item = NULL;
563 int32_t rc;
564
565 // find target connfsm, then notify it online
566 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
567 if (strcmp(networkId, item->connInfo.peerNetworkId) != 0) {
568 continue;
569 }
570 if (item->isDead) {
571 continue;
572 }
573 if (addrType != CONNECTION_ADDR_MAX && addrType != item->connInfo.addr.type) {
574 continue;
575 }
576 rc = LnnSendNewNetworkOnlineToConnFsm(item);
577 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
578 "initiate new network online to connection fsm[id=%u], rc=%d", item->id, rc);
579 }
580 }
581
TryInitiateNewNetworkOnline(const LnnConnectionFsm * connFsm)582 static void TryInitiateNewNetworkOnline(const LnnConnectionFsm *connFsm)
583 {
584 LnnConnectionFsm *item = NULL;
585 LnnInvalidCleanInfo *cleanInfo = connFsm->connInfo.cleanInfo;
586
587 if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_INITIATE_ONLINE) == 0) {
588 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]no need initiate new network online", connFsm->id);
589 return;
590 }
591 // let last invalid connfsm notify new network online after it clean
592 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
593 if (strcmp(connFsm->connInfo.peerNetworkId, item->connInfo.peerNetworkId) != 0) {
594 continue;
595 }
596 if ((item->connInfo.flag & LNN_CONN_INFO_FLAG_INITIATE_ONLINE) == 0) {
597 continue;
598 }
599 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
600 "[id=%u]wait last connfsm clean, then initiate new network online", connFsm->id);
601 return;
602 }
603 InitiateNewNetworkOnline(cleanInfo->addrType, cleanInfo->networkId);
604 }
605
TryDisconnectAllConnection(const LnnConnectionFsm * connFsm)606 static void TryDisconnectAllConnection(const LnnConnectionFsm *connFsm)
607 {
608 LnnConnectionFsm *item = NULL;
609 const ConnectionAddr *addr1 = &connFsm->connInfo.addr;
610 const ConnectionAddr *addr2 = NULL;
611 ConnectOption option;
612
613 // Not realy leaving lnn
614 if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_ONLINE) == 0) {
615 return;
616 }
617 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
618 addr2 = &item->connInfo.addr;
619 if (addr1->type != addr2->type) {
620 continue;
621 }
622 if (addr1->type == CONNECTION_ADDR_BR || addr1->type == CONNECTION_ADDR_BLE) {
623 if (strncmp(item->connInfo.addr.info.br.brMac, addr2->info.br.brMac, BT_MAC_LEN) == 0) {
624 return;
625 }
626 } else if (addr1->type == CONNECTION_ADDR_WLAN || addr1->type == CONNECTION_ADDR_ETH) {
627 if (strncmp(addr1->info.ip.ip, addr2->info.ip.ip, strlen(addr1->info.ip.ip)) == 0) {
628 return;
629 }
630 }
631 }
632 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]disconnect all connection for type=%d",
633 connFsm->id, addr1->type);
634 if (LnnConvertAddrToOption(addr1, &option)) {
635 ConnDisconnectDeviceAllConn(&option);
636 }
637 }
638
TryNotifyAllTypeOffline(const LnnConnectionFsm * connFsm)639 static void TryNotifyAllTypeOffline(const LnnConnectionFsm *connFsm)
640 {
641 LnnConnectionFsm *item = NULL;
642 const ConnectionAddr *addr1 = &connFsm->connInfo.addr;
643 const ConnectionAddr *addr2 = NULL;
644
645 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
646 addr2 = &item->connInfo.addr;
647 if (addr1->type == addr2->type) {
648 return;
649 }
650 }
651 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]notify all connection offline for type=%d",
652 connFsm->id, addr1->type);
653 (void)LnnNotifyAllTypeOffline(addr1->type);
654 }
655
CleanConnectionFsm(LnnConnectionFsm * connFsm)656 static void CleanConnectionFsm(LnnConnectionFsm *connFsm)
657 {
658 if (connFsm == NULL) {
659 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "connection fsm is null");
660 return;
661 }
662 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "connection fsm[id=%u] is cleaned", connFsm->id);
663 LnnDestroyConnectionFsm(connFsm);
664 }
665
StopConnectionFsm(LnnConnectionFsm * connFsm)666 static void StopConnectionFsm(LnnConnectionFsm *connFsm)
667 {
668 if (LnnStopConnectionFsm(connFsm, CleanConnectionFsm) != SOFTBUS_OK) {
669 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "stop connection fsm[id=%u] failed", connFsm->id);
670 }
671 ListDelete(&connFsm->node);
672 --g_netBuilder.connCount;
673 }
674
ProcessCleanConnectionFsm(const void * para)675 static int32_t ProcessCleanConnectionFsm(const void *para)
676 {
677 uint16_t connFsmId;
678 LnnConnectionFsm *connFsm = NULL;
679 int32_t rc = SOFTBUS_ERR;
680
681 if (para == NULL) {
682 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "connFsmId is null");
683 return SOFTBUS_INVALID_PARAM;
684 }
685 connFsmId = *(uint16_t *)para;
686 do {
687 connFsm = FindConnectionFsmByConnFsmId(connFsmId);
688 if (connFsm == NULL) {
689 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "can not find connection fsm");
690 break;
691 }
692 StopConnectionFsm(connFsm);
693 TryInitiateNewNetworkOnline(connFsm);
694 TryDisconnectAllConnection(connFsm);
695 TryNotifyAllTypeOffline(connFsm);
696 TryRemovePendingJoinRequest();
697 rc = SOFTBUS_OK;
698 } while (false);
699 SoftBusFree((void *)para);
700 return rc;
701 }
702
ProcessVerifyResult(const void * para)703 static int32_t ProcessVerifyResult(const void *para)
704 {
705 int32_t rc;
706 LnnConnectionFsm *connFsm = NULL;
707 const VerifyResultMsgPara *msgPara = (const VerifyResultMsgPara *)para;
708
709 if (msgPara == NULL) {
710 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "para is null");
711 return SOFTBUS_INVALID_PARAM;
712 }
713
714 if (msgPara->nodeInfo == NULL) {
715 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "msgPara node Info is null");
716 SoftBusFree((void *)msgPara);
717 return SOFTBUS_INVALID_PARAM;
718 }
719
720 do {
721 connFsm = FindConnectionFsmByRequestId(msgPara->requestId);
722 if (connFsm == NULL || connFsm->isDead) {
723 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
724 "can not find connection fsm by requestId: %u", msgPara->requestId);
725 rc = SOFTBUS_ERR;
726 break;
727 }
728 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]connection fsm auth done: retCode=%d",
729 connFsm->id, msgPara->retCode);
730 if (msgPara->retCode == SOFTBUS_OK) {
731 connFsm->connInfo.authId = msgPara->authId;
732 connFsm->connInfo.nodeInfo = msgPara->nodeInfo;
733 }
734 if (LnnSendAuthResultMsgToConnFsm(connFsm, msgPara->retCode) != SOFTBUS_OK) {
735 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "send auth result to connection fsm[id=%u] failed",
736 connFsm->id);
737 connFsm->connInfo.nodeInfo = NULL;
738 rc = SOFTBUS_ERR;
739 break;
740 }
741 rc = SOFTBUS_OK;
742 } while (false);
743
744 if (rc != SOFTBUS_OK && msgPara->nodeInfo != NULL) {
745 SoftBusFree((void *)msgPara->nodeInfo);
746 }
747 SoftBusFree((void *)msgPara);
748 return rc;
749 }
750
CreatePassiveConnectionFsm(const DeviceVerifyPassMsgPara * msgPara)751 static int32_t CreatePassiveConnectionFsm(const DeviceVerifyPassMsgPara *msgPara)
752 {
753 LnnConnectionFsm *connFsm = NULL;
754 connFsm = StartNewConnectionFsm(&msgPara->addr);
755 if (connFsm == NULL) {
756 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
757 "start new connection fsm fail: %" PRId64, msgPara->authId);
758 return SOFTBUS_ERR;
759 }
760 connFsm->connInfo.authId = msgPara->authId;
761 connFsm->connInfo.nodeInfo = msgPara->nodeInfo;
762 connFsm->connInfo.flag |= LNN_CONN_INFO_FLAG_JOIN_PASSIVE;
763 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
764 "[id=%u]start a passive connection fsm, authId=%" PRId64, connFsm->id, msgPara->authId);
765 if (LnnSendAuthResultMsgToConnFsm(connFsm, SOFTBUS_OK) != SOFTBUS_OK) {
766 connFsm->connInfo.nodeInfo = NULL;
767 StopConnectionFsm(connFsm);
768 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
769 "[id=%u]post auth result to connection fsm fail: %" PRId64, connFsm->id, msgPara->authId);
770 return SOFTBUS_ERR;
771 }
772 return SOFTBUS_OK;
773 }
774
ProcessDeviceVerifyPass(const void * para)775 static int32_t ProcessDeviceVerifyPass(const void *para)
776 {
777 int32_t rc;
778 LnnConnectionFsm *connFsm = NULL;
779 const DeviceVerifyPassMsgPara *msgPara = (const DeviceVerifyPassMsgPara *)para;
780
781 if (msgPara == NULL) {
782 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "para is null");
783 return SOFTBUS_INVALID_PARAM;
784 }
785
786 if (msgPara->nodeInfo == NULL) {
787 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "msgPara nodeInfo is null");
788 SoftBusFree((void *)msgPara);
789 return SOFTBUS_INVALID_PARAM;
790 }
791
792 do {
793 connFsm = FindConnectionFsmByAuthId(msgPara->authId);
794 if (connFsm == NULL || connFsm->isDead) {
795 rc = CreatePassiveConnectionFsm(msgPara);
796 break;
797 }
798 if (strcmp(connFsm->connInfo.peerNetworkId, msgPara->nodeInfo->networkId) != 0) {
799 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
800 "[id=%u]networkId changed: %" PRId64, connFsm->id, msgPara->authId);
801 rc = CreatePassiveConnectionFsm(msgPara);
802 break;
803 }
804 msgPara->nodeInfo->discoveryType = 1 << (uint32_t)LnnConvAddrTypeToDiscType(msgPara->addr.type);
805 if (LnnUpdateNodeInfo(msgPara->nodeInfo) != SOFTBUS_OK) {
806 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnUpdateNodeInfo failed!");
807 }
808 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
809 "[id=%u]connection fsm exist, ignore VerifyPass event: %" PRId64, connFsm->id, msgPara->authId);
810 rc = SOFTBUS_ERR;
811 } while (false);
812
813 if (rc != SOFTBUS_OK && msgPara->nodeInfo != NULL) {
814 SoftBusFree((void *)msgPara->nodeInfo);
815 }
816 SoftBusFree((void *)msgPara);
817 return rc;
818 }
819
ProcessDeviceDisconnect(const void * para)820 static int32_t ProcessDeviceDisconnect(const void *para)
821 {
822 int32_t rc;
823 LnnConnectionFsm *connFsm = NULL;
824 const int64_t *authId = (const int64_t *)para;
825
826 if (authId == NULL) {
827 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "authId is null");
828 return SOFTBUS_INVALID_PARAM;
829 }
830
831 do {
832 connFsm = FindConnectionFsmByAuthId(*authId);
833 if (connFsm == NULL || connFsm->isDead) {
834 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
835 "can not find connection fsm by authId: %" PRId64, *authId);
836 rc = SOFTBUS_ERR;
837 break;
838 }
839 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
840 "[id=%u]device disconnect, authId: %" PRId64, connFsm->id, *authId);
841 if (LnnSendDisconnectMsgToConnFsm(connFsm) != SOFTBUS_OK) {
842 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
843 "send disconnect to connection fsm[id=%u] failed", connFsm->id);
844 rc = SOFTBUS_ERR;
845 break;
846 }
847 rc = SOFTBUS_OK;
848 } while (false);
849 SoftBusFree((void *)authId);
850 return rc;
851 }
852
ProcessDeviceNotTrusted(const void * para)853 static int32_t ProcessDeviceNotTrusted(const void *para)
854 {
855 int32_t rc;
856 const char *udid = NULL;
857 LnnConnectionFsm *item = NULL;
858 const char *peerUdid = (const char *)para;
859
860 if (peerUdid == NULL) {
861 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "peer udid is null");
862 return SOFTBUS_INVALID_PARAM;
863 }
864
865 do {
866 char networkId[NETWORK_ID_BUF_LEN] = {0};
867 if (LnnGetNetworkIdByUdid(peerUdid, networkId, sizeof(networkId)) == SOFTBUS_OK) {
868 LnnRequestLeaveSpecific(networkId, CONNECTION_ADDR_MAX);
869 break;
870 }
871 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
872 udid = LnnGetDeviceUdid(item->connInfo.nodeInfo);
873 if (udid == NULL || strcmp(peerUdid, udid) != 0) {
874 continue;
875 }
876 rc = LnnSendNotTrustedToConnFsm(item);
877 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
878 "[id=%u]send not trusted msg to connection fsm result: %d", item->id, rc);
879 }
880 } while (false);
881 SoftBusFree((void *)peerUdid);
882 return SOFTBUS_OK;
883 }
884
ProcessLeaveLNNRequest(const void * para)885 static int32_t ProcessLeaveLNNRequest(const void *para)
886 {
887 const char *networkId = (const char *)para;
888 LnnConnectionFsm *item = NULL;
889 int rc = SOFTBUS_ERR;
890
891 if (networkId == NULL) {
892 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "leave networkId is null");
893 return SOFTBUS_INVALID_PARAM;
894 }
895
896 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
897 if (strcmp(networkId, item->connInfo.peerNetworkId) != 0 || item->isDead) {
898 continue;
899 }
900 if (LnnSendLeaveRequestToConnFsm(item) != SOFTBUS_OK) {
901 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "send leave LNN msg to connection fsm[id=%u] failed",
902 item->id);
903 } else {
904 rc = SOFTBUS_OK;
905 item->connInfo.flag |= LNN_CONN_INFO_FLAG_LEAVE_REQUEST;
906 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "send leave LNN msg to connection fsm[id=%u] success",
907 item->id);
908 }
909 }
910 if (rc != SOFTBUS_OK) {
911 LnnNotifyLeaveResult(networkId, SOFTBUS_ERR);
912 }
913 SoftBusFree((void *)networkId);
914 return rc;
915 }
916
LeaveMetaInfoToLedger(const MetaJoinRequestNode * metaInfo,const char * networkId)917 static void LeaveMetaInfoToLedger(const MetaJoinRequestNode *metaInfo, const char *networkId)
918 {
919 NodeInfo *info = NULL;
920 const char *udid = NULL;
921 info = LnnGetNodeInfoById(networkId, CATEGORY_NETWORK_ID);
922 if (info == NULL) {
923 return;
924 }
925 udid = LnnGetDeviceUdid(info);
926 if (LnnDeleteMetaInfo(udid, metaInfo->addr.type) != SOFTBUS_OK) {
927 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnDeleteMetaInfo error");
928 }
929 }
930
ProcessLeaveMetaNodeRequest(const void * para)931 static int32_t ProcessLeaveMetaNodeRequest(const void *para)
932 {
933 const char *networkId = (const char *)para;
934 MetaJoinRequestNode *item = NULL;
935 MetaJoinRequestNode *next = NULL;
936 int rc = SOFTBUS_ERR;
937 if (networkId == NULL) {
938 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "leave networkId is null");
939 return SOFTBUS_INVALID_PARAM;
940 }
941 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_netBuilder.metaNodeList, MetaJoinRequestNode, node) {
942 if (strcmp(networkId, item->networkId) != 0) {
943 continue;
944 }
945 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "ProcessLeaveMetaNodeRequest can find networkId");
946 AuthMetaReleaseVerify(item->authId);
947 LeaveMetaInfoToLedger(item, networkId);
948 ListDelete(&item->node);
949 SoftBusFree(item);
950 }
951 MetaNodeNotifyLeaveResult(networkId, SOFTBUS_OK);
952 SoftBusFree((void *)networkId);
953 return rc;
954 }
955
ProcessSyncOfflineFinish(const void * para)956 static int32_t ProcessSyncOfflineFinish(const void *para)
957 {
958 const char *networkId = (const char *)para;
959 LnnConnectionFsm *item = NULL;
960 int rc = SOFTBUS_OK;
961
962 if (networkId == NULL) {
963 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "sync offline finish networkId is null");
964 return SOFTBUS_INVALID_PARAM;
965 }
966 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
967 if (strcmp(networkId, item->connInfo.peerNetworkId) != 0 || item->isDead) {
968 continue;
969 }
970 rc = LnnSendSyncOfflineFinishToConnFsm(item);
971 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "send sync offline msg to connection fsm[id=%u] result: %d",
972 item->id, rc);
973 }
974 SoftBusFree((void *)networkId);
975 return rc;
976 }
977
IsInvalidConnectionFsm(const LnnConnectionFsm * connFsm,const LeaveInvalidConnMsgPara * msgPara)978 static bool IsInvalidConnectionFsm(const LnnConnectionFsm *connFsm, const LeaveInvalidConnMsgPara *msgPara)
979 {
980 if (strcmp(msgPara->oldNetworkId, connFsm->connInfo.peerNetworkId) != 0) {
981 return false;
982 }
983 if (connFsm->isDead) {
984 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]connection is dead", connFsm->id);
985 return false;
986 }
987 if (msgPara->addrType != CONNECTION_ADDR_MAX && msgPara->addrType != connFsm->connInfo.addr.type) {
988 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]connection type not match %d,%d",
989 connFsm->id, msgPara->addrType, connFsm->connInfo.addr.type);
990 return false;
991 }
992 if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_ONLINE) == 0) {
993 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]connection is not online", connFsm->id);
994 return false;
995 }
996 if ((connFsm->connInfo.flag & LNN_CONN_INFO_FLAG_INITIATE_ONLINE) != 0) {
997 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]connection is already in leaving", connFsm->id);
998 return false;
999 }
1000 return true;
1001 }
1002
ProcessLeaveInvalidConn(const void * para)1003 static int32_t ProcessLeaveInvalidConn(const void *para)
1004 {
1005 LnnConnectionFsm *item = NULL;
1006 int32_t rc = SOFTBUS_OK;
1007 int32_t count = 0;
1008 const LeaveInvalidConnMsgPara *msgPara = (const LeaveInvalidConnMsgPara *)para;
1009
1010 if (msgPara == NULL) {
1011 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "leave invalid connection msg para is null");
1012 return SOFTBUS_INVALID_PARAM;
1013 }
1014 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
1015 if (!IsInvalidConnectionFsm(item, msgPara)) {
1016 continue;
1017 }
1018 // The new connFsm should timeout when following errors occur
1019 ++count;
1020 item->connInfo.cleanInfo = (LnnInvalidCleanInfo *)SoftBusMalloc(sizeof(LnnInvalidCleanInfo));
1021 if (item->connInfo.cleanInfo == NULL) {
1022 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]malloc invalid clena info failed", item->id);
1023 continue;
1024 }
1025 item->connInfo.cleanInfo->addrType = msgPara->addrType;
1026 if (strncpy_s(item->connInfo.cleanInfo->networkId, NETWORK_ID_BUF_LEN,
1027 msgPara->newNetworkId, strlen(msgPara->newNetworkId)) != EOK) {
1028 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[id=%u]copy new networkId failed", item->id);
1029 rc = SOFTBUS_ERR;
1030 SoftBusFree(item->connInfo.cleanInfo);
1031 item->connInfo.cleanInfo = NULL;
1032 continue;
1033 }
1034 rc = LnnSendLeaveRequestToConnFsm(item);
1035 if (rc == SOFTBUS_OK) {
1036 item->connInfo.flag |= LNN_CONN_INFO_FLAG_INITIATE_ONLINE;
1037 item->connInfo.flag |= LNN_CONN_INFO_FLAG_LEAVE_AUTO;
1038 } else {
1039 SoftBusFree(item->connInfo.cleanInfo);
1040 item->connInfo.cleanInfo = NULL;
1041 }
1042 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1043 "send leave LNN msg to invalid connection fsm[id=%u] result: %d", item->id, rc);
1044 }
1045 if (count == 0) {
1046 InitiateNewNetworkOnline(msgPara->addrType, msgPara->newNetworkId);
1047 }
1048 SoftBusFree((void *)msgPara);
1049 return rc;
1050 }
1051
TryElectMasterNodeOnline(const LnnConnectionFsm * connFsm)1052 static int32_t TryElectMasterNodeOnline(const LnnConnectionFsm *connFsm)
1053 {
1054 char peerMasterUdid[UDID_BUF_LEN] = {0};
1055 char localMasterUdid[UDID_BUF_LEN] = {0};
1056 int32_t peerMasterWeight, localMasterWeight;
1057 int32_t rc;
1058
1059 // get local master node info
1060 if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
1061 LnnGetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, &localMasterWeight) != SOFTBUS_OK) {
1062 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local master node info from ledger failed");
1063 return SOFTBUS_ERR;
1064 }
1065 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "local master(%u) weight=%d", connFsm->id, localMasterWeight);
1066 if (LnnGetRemoteStrInfo(connFsm->connInfo.peerNetworkId, STRING_KEY_MASTER_NODE_UDID,
1067 peerMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
1068 LnnGetRemoteNumInfo(connFsm->connInfo.peerNetworkId, NUM_KEY_MASTER_NODE_WEIGHT,
1069 &peerMasterWeight) != SOFTBUS_OK) {
1070 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "peer node info(%u) is not found", connFsm->id);
1071 return SOFTBUS_ERR;
1072 }
1073 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "peer master(%u) weight=%d", connFsm->id, peerMasterWeight);
1074 rc = LnnCompareNodeWeight(localMasterWeight, localMasterUdid, peerMasterWeight, peerMasterUdid);
1075 if (rc >= 0) {
1076 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1077 "online node(%u) weight less than current(compare result: %d), no need elect again",
1078 connFsm->id, rc);
1079 return SOFTBUS_OK;
1080 }
1081 UpdateLocalMasterNode(false, peerMasterUdid, peerMasterWeight);
1082 SendElectMessageToAll(connFsm->connInfo.peerNetworkId);
1083 return SOFTBUS_OK;
1084 }
1085
TryElectMasterNodeOffline(const LnnConnectionFsm * connFsm)1086 static int32_t TryElectMasterNodeOffline(const LnnConnectionFsm *connFsm)
1087 {
1088 char localUdid[UDID_BUF_LEN] = {0};
1089 char localMasterUdid[UDID_BUF_LEN] = {0};
1090
1091 if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK) {
1092 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local master node info from ledger failed");
1093 return SOFTBUS_ERR;
1094 }
1095 LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN);
1096 if (strcmp(localMasterUdid, localUdid) == 0) {
1097 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "local is master node(%u), no need elect again", connFsm->id);
1098 } else {
1099 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "maybe master node(%u) offline, elect again", connFsm->id);
1100 UpdateLocalMasterNode(true, localUdid, LnnGetLocalWeight());
1101 SendElectMessageToAll(connFsm->connInfo.peerNetworkId);
1102 }
1103 return SOFTBUS_OK;
1104 }
1105
IsSupportMasterNodeElect(SoftBusVersion version)1106 static bool IsSupportMasterNodeElect(SoftBusVersion version)
1107 {
1108 return version >= SOFTBUS_NEW_V1;
1109 }
1110
ProcessNodeStateChanged(const void * para)1111 static int32_t ProcessNodeStateChanged(const void *para)
1112 {
1113 const ConnectionAddr *addr = (const ConnectionAddr *)para;
1114 LnnConnectionFsm *connFsm = NULL;
1115 int32_t rc = SOFTBUS_ERR;
1116 bool isOnline = false;
1117
1118 if (addr == NULL) {
1119 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "node state changed msg is null");
1120 return SOFTBUS_INVALID_PARAM;
1121 }
1122 do {
1123 connFsm = FindConnectionFsmByAddr(addr);
1124 if (connFsm == NULL) {
1125 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR,
1126 "can't find connection fsm when node online state changed");
1127 break;
1128 }
1129 isOnline = IsNodeOnline(connFsm->connInfo.peerNetworkId);
1130 if (!IsSupportMasterNodeElect(connFsm->connInfo.version)) {
1131 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]peer not support master node elect", connFsm->id);
1132 rc = SOFTBUS_OK;
1133 break;
1134 }
1135 rc = isOnline ? TryElectMasterNodeOnline(connFsm) : TryElectMasterNodeOffline(connFsm);
1136 } while (false);
1137 SoftBusFree((void *)addr);
1138 if (isOnline) {
1139 TryRemovePendingJoinRequest();
1140 }
1141 return rc;
1142 }
1143
ProcessMasterElect(const void * para)1144 static int32_t ProcessMasterElect(const void *para)
1145 {
1146 const ElectMsgPara *msgPara = (const ElectMsgPara *)para;
1147 LnnConnectionFsm *connFsm = NULL;
1148 char localMasterUdid[UDID_BUF_LEN] = {0};
1149 int32_t localMasterWeight, compareRet;
1150 int32_t rc = SOFTBUS_ERR;
1151
1152 if (msgPara == NULL) {
1153 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "elect msg para is null");
1154 return SOFTBUS_INVALID_PARAM;
1155 }
1156 do {
1157 connFsm = FindConnectionFsmByNetworkId(msgPara->networkId);
1158 if (connFsm == NULL || connFsm->isDead) {
1159 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "can't find connection fsm when receive elect node");
1160 break;
1161 }
1162 if (!IsNodeOnline(connFsm->connInfo.peerNetworkId)) {
1163 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "peer node(%u) is already offline", connFsm->id);
1164 break;
1165 }
1166 if (LnnGetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, localMasterUdid, UDID_BUF_LEN) != SOFTBUS_OK ||
1167 LnnGetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, &localMasterWeight) != SOFTBUS_OK) {
1168 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local master node(%u) info from ledger failed",
1169 connFsm->id);
1170 break;
1171 }
1172 compareRet = LnnCompareNodeWeight(localMasterWeight, localMasterUdid,
1173 msgPara->masterWeight, msgPara->masterUdid);
1174 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[id=%u]weight compare result: %d", connFsm->id, compareRet);
1175 if (compareRet != 0) {
1176 if (compareRet < 0) {
1177 UpdateLocalMasterNode(false, msgPara->masterUdid, msgPara->masterWeight);
1178 SendElectMessageToAll(connFsm->connInfo.peerNetworkId);
1179 } else {
1180 rc = SyncElectMessage(connFsm->connInfo.peerNetworkId);
1181 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "sync elect info to connFsm(%u) result:%d",
1182 connFsm->id, rc);
1183 }
1184 }
1185 rc = SOFTBUS_OK;
1186 } while (false);
1187 SoftBusFree((void *)msgPara);
1188 return rc;
1189 }
1190
ProcessLeaveByAddrType(const void * para)1191 static int32_t ProcessLeaveByAddrType(const void *para)
1192 {
1193 bool *addrType = NULL;
1194 LnnConnectionFsm *item = NULL;
1195 int32_t rc;
1196 bool notify = true;
1197
1198 if (para == NULL) {
1199 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "leave by addr type msg para is null");
1200 return SOFTBUS_INVALID_PARAM;
1201 }
1202
1203 addrType = (bool *)para;
1204 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
1205 if (!addrType[item->connInfo.addr.type]) {
1206 continue;
1207 }
1208 // if there are any same addr type, let last one send notify
1209 notify = false;
1210 if (item->isDead) {
1211 continue;
1212 }
1213 rc = LnnSendLeaveRequestToConnFsm(item);
1214 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "leave connFsm[id=%u] by addr type rc=%d", item->id, rc);
1215 if (rc == SOFTBUS_OK) {
1216 item->connInfo.flag |= LNN_CONN_INFO_FLAG_LEAVE_AUTO;
1217 }
1218 }
1219 if (notify) {
1220 (void)LnnNotifyAllTypeOffline(CONNECTION_ADDR_MAX);
1221 }
1222 SoftBusFree((void *)para);
1223 return SOFTBUS_OK;
1224 }
1225
ProcessLeaveSpecific(const void * para)1226 static int32_t ProcessLeaveSpecific(const void *para)
1227 {
1228 const SpecificLeaveMsgPara *msgPara = (const SpecificLeaveMsgPara *)para;
1229 LnnConnectionFsm *item = NULL;
1230
1231 if (msgPara == NULL) {
1232 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "leave specific msg is null");
1233 return SOFTBUS_INVALID_PARAM;
1234 }
1235
1236 int32_t rc;
1237 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
1238 if (strcmp(item->connInfo.peerNetworkId, msgPara->networkId) != 0 ||
1239 (item->connInfo.addr.type != msgPara->addrType &&
1240 msgPara->addrType != CONNECTION_ADDR_MAX)) {
1241 continue;
1242 }
1243 rc = LnnSendLeaveRequestToConnFsm(item);
1244 if (rc == SOFTBUS_OK) {
1245 item->connInfo.flag |= LNN_CONN_INFO_FLAG_LEAVE_AUTO;
1246 }
1247 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1248 "send leave LNN msg to connection fsm[id=%u] result: %d", item->id, rc);
1249 }
1250 SoftBusFree((void *)msgPara);
1251 return SOFTBUS_OK;
1252 }
1253
DupNodeInfo(const NodeInfo * nodeInfo)1254 static NodeInfo *DupNodeInfo(const NodeInfo *nodeInfo)
1255 {
1256 NodeInfo *node = (NodeInfo *)SoftBusMalloc(sizeof(NodeInfo));
1257 if (node == NULL) {
1258 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc NodeInfo fail");
1259 return NULL;
1260 }
1261 if (memcpy_s(node, sizeof(NodeInfo), nodeInfo, sizeof(NodeInfo)) != EOK) {
1262 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy NodeInfo fail");
1263 SoftBusFree(node);
1264 return NULL;
1265 }
1266 return node;
1267 }
1268
FillNodeInfo(MetaJoinRequestNode * metaNode,NodeInfo * info)1269 static int32_t FillNodeInfo(MetaJoinRequestNode *metaNode, NodeInfo *info)
1270 {
1271 if (metaNode == NULL || info ==NULL) {
1272 return SOFTBUS_ERR;
1273 }
1274 SoftBusSysTime times;
1275 (void)SoftBusGetTime(×);
1276 info->heartbeatTimeStamp = (uint64_t)times.sec * HB_TIME_FACTOR +
1277 (uint64_t)times.usec / HB_TIME_FACTOR;
1278 info->discoveryType = 1 << (uint32_t)LnnConvAddrTypeToDiscType(metaNode->addr.type);
1279 info->authSeqNum = metaNode->authId;
1280 info->authSeq[LnnConvAddrTypeToDiscType(metaNode->addr.type)] = metaNode->authId;
1281 info->authChannelId[metaNode->addr.type] = (int32_t)metaNode->authId;
1282 info->relation[metaNode->addr.type]++;
1283 if (AuthGetDeviceUuid(metaNode->authId, info->uuid, sizeof(info->uuid)) != SOFTBUS_OK ||
1284 info->uuid[0] == '\0') {
1285 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fill uuid fail");
1286 return SOFTBUS_ERR;
1287 }
1288 if (metaNode->addr.type == CONNECTION_ADDR_ETH || metaNode->addr.type == CONNECTION_ADDR_WLAN) {
1289 if (strcpy_s(info->connectInfo.deviceIp, MAX_ADDR_LEN, metaNode->addr.info.ip.ip) != EOK) {
1290 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fill deviceIp fail");
1291 return SOFTBUS_MEM_ERR;
1292 }
1293 }
1294 info->metaInfo.metaDiscType = 1 << (uint32_t)LnnConvAddrTypeToDiscType(metaNode->addr.type);
1295 info->metaInfo.isMetaNode = true;
1296 return SOFTBUS_OK;
1297 }
1298
ProcessOnAuthMetaVerifyPassed(const void * para)1299 static int32_t ProcessOnAuthMetaVerifyPassed(const void *para)
1300 {
1301 if (para == NULL) {
1302 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: para is NULL");
1303 return SOFTBUS_ERR;
1304 }
1305 MetaAuthInfo *meta = (MetaAuthInfo *)para;
1306 MetaJoinRequestNode *metaNode = meta->metaJoinNode;
1307 int64_t authMetaId = meta->authMetaId;
1308 NodeInfo *info = &meta->info;
1309 int32_t ret = SOFTBUS_ERR;
1310 do {
1311 if (strcpy_s(metaNode->networkId, sizeof(metaNode->networkId), info->networkId) != EOK) {
1312 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "ProcessOnAuthMetaVerifyPassed copy networkId error");
1313 break;
1314 }
1315 metaNode->authId = authMetaId;
1316 NodeInfo *newInfo = DupNodeInfo(info);
1317 if (newInfo == NULL) {
1318 break;
1319 }
1320 if (FillNodeInfo(metaNode, newInfo) != SOFTBUS_OK) {
1321 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "ProcessOnAuthMetaVerifyPassed FillNodeInfo error");
1322 SoftBusFree(newInfo);
1323 break;
1324 }
1325 ret = LnnAddMetaInfo(newInfo);
1326 SoftBusFree(newInfo);
1327 } while (0);
1328 if (ret == SOFTBUS_OK) {
1329 MetaNodeNotifyJoinResult(&metaNode->addr, info->networkId, SOFTBUS_OK);
1330 } else {
1331 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "ProcessOnAuthMetaVerifyPassed error");
1332 MetaNodeNotifyJoinResult(&metaNode->addr, NULL, SOFTBUS_ERR);
1333 ListDelete(&metaNode->node);
1334 SoftBusFree(metaNode);
1335 }
1336 SoftBusFree(meta);
1337 return ret;
1338 }
1339
ProcessOnAuthMetaVerifyFailed(const void * para)1340 static int32_t ProcessOnAuthMetaVerifyFailed(const void *para)
1341 {
1342 if (para == NULL) {
1343 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fail: para is NULL");
1344 return SOFTBUS_ERR;
1345 }
1346 MetaReason *mataReason = (MetaReason *)para;
1347 MetaJoinRequestNode *metaNode = mataReason->metaJoinNode;
1348 MetaNodeNotifyJoinResult(&metaNode->addr, NULL, mataReason->reason);
1349 ListDelete(&metaNode->node);
1350 SoftBusFree(metaNode);
1351 SoftBusFree(mataReason);
1352 return SOFTBUS_OK;
1353 }
1354
1355 static NetBuilderMessageProcess g_messageProcessor[MSG_TYPE_MAX] = {
1356 ProcessJoinLNNRequest,
1357 ProcessDevDiscoveryRequest,
1358 ProcessCleanConnectionFsm,
1359 ProcessVerifyResult,
1360 ProcessDeviceVerifyPass,
1361 ProcessDeviceDisconnect,
1362 ProcessDeviceNotTrusted,
1363 ProcessLeaveLNNRequest,
1364 ProcessSyncOfflineFinish,
1365 ProcessNodeStateChanged,
1366 ProcessMasterElect,
1367 ProcessLeaveInvalidConn,
1368 ProcessLeaveByAddrType,
1369 ProcessLeaveSpecific,
1370 ProcessJoinMetaNodeRequest,
1371 ProcessOnAuthMetaVerifyPassed,
1372 ProcessLeaveMetaNodeRequest,
1373 ProcessOnAuthMetaVerifyFailed,
1374 };
1375
NetBuilderMessageHandler(SoftBusMessage * msg)1376 static void NetBuilderMessageHandler(SoftBusMessage *msg)
1377 {
1378 int32_t ret;
1379
1380 if (msg == NULL) {
1381 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "msg is null in net builder handler");
1382 return;
1383 }
1384 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "net builder process msg: %d", msg->what);
1385 if (msg->what >= MSG_TYPE_MAX) {
1386 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid msg type");
1387 return;
1388 }
1389 ret = g_messageProcessor[msg->what](msg->obj);
1390 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "net builder process msg(%d) done, ret=%d", msg->what, ret);
1391 }
1392
GetCurrentConnectType(void)1393 static ConnectionAddrType GetCurrentConnectType(void)
1394 {
1395 char ifCurrentName[NET_IF_NAME_LEN] = {0};
1396 ConnectionAddrType type = CONNECTION_ADDR_MAX;
1397
1398 if (LnnGetLocalStrInfo(STRING_KEY_NET_IF_NAME, ifCurrentName, NET_IF_NAME_LEN) != SOFTBUS_OK) {
1399 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnGetLocalStrInfo getCurrentConnectType failed");
1400 return type;
1401 }
1402 if (LnnGetAddrTypeByIfName(ifCurrentName, &type) != SOFTBUS_OK) {
1403 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "getCurrentConnectType unknown connect type");
1404 }
1405 return type;
1406 }
1407
OnDeviceVerifyPass(int64_t authId,const NodeInfo * info)1408 static void OnDeviceVerifyPass(int64_t authId, const NodeInfo *info)
1409 {
1410 AuthConnInfo connInfo;
1411 DeviceVerifyPassMsgPara *para = NULL;
1412 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "verify passed passively, authId=%" PRId64, authId);
1413 if (AuthGetConnInfo(authId, &connInfo) != SOFTBUS_OK) {
1414 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get AuthConnInfo fail, authId: %" PRId64, authId);
1415 return;
1416 }
1417 para = (DeviceVerifyPassMsgPara *)SoftBusMalloc(sizeof(DeviceVerifyPassMsgPara));
1418 if (para == NULL) {
1419 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc DeviceVerifyPassMsgPara fail");
1420 return;
1421 }
1422 if (!LnnConvertAuthConnInfoToAddr(¶->addr, &connInfo, GetCurrentConnectType())) {
1423 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "convert connInfo to addr fail");
1424 SoftBusFree(para);
1425 return;
1426 }
1427 para->authId = authId;
1428 para->nodeInfo = DupNodeInfo(info);
1429 if (para->nodeInfo == NULL) {
1430 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "dup NodeInfo fail");
1431 SoftBusFree(para);
1432 return;
1433 }
1434 if (PostMessageToHandler(MSG_TYPE_DEVICE_VERIFY_PASS, para) != SOFTBUS_OK) {
1435 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post DEVICE_VERIFY_PASS msg fail");
1436 SoftBusFree(para->nodeInfo);
1437 SoftBusFree(para);
1438 }
1439 }
1440
OnDeviceDisconnect(int64_t authId)1441 static void OnDeviceDisconnect(int64_t authId)
1442 {
1443 int64_t *para = NULL;
1444 para = (int64_t *)SoftBusMalloc(sizeof(int64_t));
1445 if (para == NULL) {
1446 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc DeviceDisconnect para fail");
1447 return;
1448 }
1449 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "auth device disconnect, authId: %" PRId64, authId);
1450 *para = authId;
1451 if (PostMessageToHandler(MSG_TYPE_DEVICE_DISCONNECT, para) != SOFTBUS_OK) {
1452 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post DEVICE_DISCONNECT msg fail");
1453 SoftBusFree(para);
1454 }
1455 }
1456
OnLnnProcessNotTrustedMsgDelay(void * para)1457 static void OnLnnProcessNotTrustedMsgDelay(void *para)
1458 {
1459 if (para == NULL) {
1460 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid info para");
1461 return;
1462 }
1463 int64_t authSeq[DISCOVERY_TYPE_COUNT] = {0};
1464 NotTrustedDelayInfo *info = (NotTrustedDelayInfo *)para;
1465 if (LnnGetAllAuthSeq(info->udid, authSeq, DISCOVERY_TYPE_COUNT) != SOFTBUS_OK) {
1466 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[offline]LnnGetAllAuthSeq fail");
1467 SoftBusFree(info);
1468 return;
1469 }
1470 char networkId[NETWORK_ID_BUF_LEN] = {0};
1471 if (LnnConvertDlId(info->udid, CATEGORY_UDID, CATEGORY_NETWORK_ID, networkId, NETWORK_ID_BUF_LEN) != SOFTBUS_OK) {
1472 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[offline] convert networkId fail");
1473 SoftBusFree(info);
1474 return;
1475 }
1476 uint32_t type;
1477 for (type = DISCOVERY_TYPE_WIFI; type < DISCOVERY_TYPE_P2P; type++) {
1478 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1479 "OnLnnProcessNotTrustedMsgDelay: authSeq %" PRId64 "-> %" PRId64, info->authSeq[type], authSeq[type]);
1480
1481 if (authSeq[type] == info->authSeq[type] && authSeq[type] != 0 && info->authSeq[type] != 0) {
1482 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[offline] LnnRequestLeaveSpecific type:%d", type);
1483 LnnRequestLeaveSpecific(networkId, LnnDiscTypeToConnAddrType((DiscoveryType)type));
1484 continue;
1485 }
1486 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "after 5s authSeq=%" PRId64, authSeq[type]);
1487 }
1488 SoftBusFree(info);
1489 }
1490
OnDeviceNotTrusted(const char * peerUdid)1491 static void OnDeviceNotTrusted(const char *peerUdid)
1492 {
1493 if (peerUdid == NULL) {
1494 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "peer udid is NULL");
1495 return;
1496 }
1497 uint32_t udidLen = strlen(peerUdid) + 1;
1498 if (udidLen > UDID_BUF_LEN) {
1499 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "not trusted udid is too long");
1500 return;
1501 }
1502 if (!LnnGetOnlineStateById(peerUdid, CATEGORY_UDID)) {
1503 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "not trusted device has offline!");
1504 return;
1505 }
1506 NotTrustedDelayInfo *info = (NotTrustedDelayInfo *)SoftBusCalloc(sizeof(NotTrustedDelayInfo));
1507 if (info == NULL) {
1508 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc NotTrustedDelayInfo fail");
1509 return;
1510 }
1511 if (LnnGetAllAuthSeq(peerUdid, info->authSeq, DISCOVERY_TYPE_COUNT) != SOFTBUS_OK) {
1512 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[offline]LnnGetAllAuthSeq fail");
1513 SoftBusFree(info);
1514 return;
1515 }
1516 if (strcpy_s(info->udid, UDID_BUF_LEN, peerUdid) != EOK) {
1517 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy udid fail");
1518 SoftBusFree(info);
1519 return;
1520 }
1521 if (LnnSendNotTrustedInfo(info, DISCOVERY_TYPE_COUNT) != SOFTBUS_OK) {
1522 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[offline]LnnSendNotTrustedInfo fail");
1523 SoftBusFree(info);
1524 return;
1525 }
1526 if (LnnAsyncCallbackDelayHelper(GetLooper(LOOP_TYPE_DEFAULT), OnLnnProcessNotTrustedMsgDelay,
1527 info, NOT_TRUSTED_DEVICE_MSG_DELAY) != SOFTBUS_OK) {
1528 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "async not trusted msg delay fail");
1529 SoftBusFree(info);
1530 }
1531 }
1532
1533 static AuthVerifyListener g_verifyListener = {
1534 .onDeviceVerifyPass = OnDeviceVerifyPass,
1535 .onDeviceNotTrusted = OnDeviceNotTrusted,
1536 .onDeviceDisconnect = OnDeviceDisconnect,
1537 };
1538
PostVerifyResult(uint32_t requestId,int32_t retCode,int64_t authId,const NodeInfo * info)1539 static void PostVerifyResult(uint32_t requestId, int32_t retCode, int64_t authId, const NodeInfo *info)
1540 {
1541 VerifyResultMsgPara *para = NULL;
1542 para = (VerifyResultMsgPara *)SoftBusCalloc(sizeof(VerifyResultMsgPara));
1543 if (para == NULL) {
1544 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc verify result msg para fail");
1545 return;
1546 }
1547 para->requestId = requestId;
1548 para->retCode = retCode;
1549 if (retCode == SOFTBUS_OK) {
1550 para->nodeInfo = DupNodeInfo(info);
1551 if (para->nodeInfo == NULL) {
1552 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "dup NodeInfo fail");
1553 SoftBusFree(para);
1554 return;
1555 }
1556 para->authId = authId;
1557 }
1558 if (PostMessageToHandler(MSG_TYPE_VERIFY_RESULT, para) != SOFTBUS_OK) {
1559 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post verify result message failed");
1560 SoftBusFree(para->nodeInfo);
1561 SoftBusFree(para);
1562 }
1563 }
1564
OnVerifyPassed(uint32_t requestId,int64_t authId,const NodeInfo * info)1565 static void OnVerifyPassed(uint32_t requestId, int64_t authId, const NodeInfo *info)
1566 {
1567 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1568 "verify passed: requestId=%u, authId=%" PRId64, requestId, authId);
1569 if (info == NULL) {
1570 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post verify result message failed");
1571 return;
1572 }
1573 PostVerifyResult(requestId, SOFTBUS_OK, authId, info);
1574 }
1575
OnVerifyFailed(uint32_t requestId,int32_t reason)1576 static void OnVerifyFailed(uint32_t requestId, int32_t reason)
1577 {
1578 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
1579 "verify failed: requestId=%u, reason=%d", requestId, reason);
1580 PostVerifyResult(requestId, reason, AUTH_INVALID_ID, NULL);
1581 }
1582
1583 static AuthVerifyCallback g_verifyCallback = {
1584 .onVerifyPassed = OnVerifyPassed,
1585 .onVerifyFailed = OnVerifyFailed,
1586 };
1587
LnnGetVerifyCallback(void)1588 AuthVerifyCallback *LnnGetVerifyCallback(void)
1589 {
1590 return &g_verifyCallback;
1591 }
1592
OnAuthMetaVerifyPassed(uint32_t requestId,int64_t authMetaId,const NodeInfo * info)1593 void OnAuthMetaVerifyPassed(uint32_t requestId, int64_t authMetaId, const NodeInfo *info)
1594 {
1595 if (info == NULL) {
1596 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnAuthMetaVerifyPassed info = NULL");
1597 return;
1598 }
1599 MetaJoinRequestNode *metaNode = FindMetaNodeByRequestId(requestId);
1600 if (metaNode == NULL) {
1601 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnAuthMetaVerifyPassed not find metaNode");
1602 return;
1603 }
1604 MetaAuthInfo *meta = (MetaAuthInfo *)SoftBusMalloc(sizeof(MetaAuthInfo));
1605 if (meta == NULL) {
1606 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnAuthMetaVerifyPassed meta = NULL");
1607 return;
1608 }
1609 meta->authMetaId = authMetaId;
1610 meta->metaJoinNode = metaNode;
1611 meta->info = *info;
1612 if (PostMessageToHandler(MSG_TYPE_JOIN_METANODE_AUTH_PASS, meta) != SOFTBUS_OK) {
1613 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post join metanode authpass message failed");
1614 SoftBusFree(meta);
1615 return;
1616 }
1617 }
1618
OnAuthMetaVerifyFailed(uint32_t requestId,int32_t reason)1619 void OnAuthMetaVerifyFailed(uint32_t requestId, int32_t reason)
1620 {
1621 MetaJoinRequestNode *metaJoinNode = FindMetaNodeByRequestId(requestId);
1622 MetaReason *para = (MetaReason *)SoftBusMalloc(sizeof(MetaReason));
1623 if (para == NULL) {
1624 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnAuthMetaVerifyFailed para = NULL");
1625 return;
1626 }
1627 para->metaJoinNode = metaJoinNode;
1628 para->reason = reason;
1629 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "OnAuthMetaVerifyFailed can find metaNode");
1630 if (PostMessageToHandler(MSG_TYPE_JOIN_METANODE_AUTH_FAIL, para) != SOFTBUS_OK) {
1631 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post join metanode authfail message failed");
1632 SoftBusFree(para);
1633 return;
1634 }
1635 }
1636
1637 static AuthVerifyCallback g_metaVerifyCallback = {
1638 .onVerifyPassed = OnAuthMetaVerifyPassed,
1639 .onVerifyFailed = OnAuthMetaVerifyFailed,
1640 };
1641
LnnGetMetaVerifyCallback(void)1642 AuthVerifyCallback *LnnGetMetaVerifyCallback(void)
1643 {
1644 return &g_metaVerifyCallback;
1645 }
1646
CreateConnectionAddrMsgPara(const ConnectionAddr * addr)1647 static ConnectionAddr *CreateConnectionAddrMsgPara(const ConnectionAddr *addr)
1648 {
1649 ConnectionAddr *para = NULL;
1650
1651 if (addr == NULL) {
1652 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "addr is null");
1653 return NULL;
1654 }
1655 para = (ConnectionAddr *)SoftBusCalloc(sizeof(ConnectionAddr));
1656 if (para == NULL) {
1657 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc connecton addr message fail");
1658 return NULL;
1659 }
1660 *para = *addr;
1661 return para;
1662 }
1663
CreateConnectionAddrMsgParaKey(const ConnectionAddrKey * addrDataKey)1664 static ConnectionAddrKey *CreateConnectionAddrMsgParaKey(const ConnectionAddrKey *addrDataKey)
1665 {
1666 ConnectionAddrKey *para = NULL;
1667
1668 if (addrDataKey == NULL) {
1669 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "addrDataKey is null");
1670 return NULL;
1671 }
1672 para = (ConnectionAddrKey *)SoftBusCalloc(sizeof(ConnectionAddrKey));
1673 if (para == NULL) {
1674 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc connecton addrKey message fail");
1675 return NULL;
1676 }
1677 *para = *addrDataKey;
1678 return para;
1679 }
1680
CreateNetworkIdMsgPara(const char * networkId)1681 static char *CreateNetworkIdMsgPara(const char *networkId)
1682 {
1683 char *para = NULL;
1684
1685 if (networkId == NULL) {
1686 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "networkId is null");
1687 return NULL;
1688 }
1689 para = (char *)SoftBusMalloc(NETWORK_ID_BUF_LEN);
1690 if (para == NULL) {
1691 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc networkId message fail");
1692 return NULL;
1693 }
1694 if (strncpy_s(para, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK) {
1695 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy network id fail");
1696 SoftBusFree(para);
1697 return NULL;
1698 }
1699 return para;
1700 }
1701
ConifgLocalLedger(void)1702 static int32_t ConifgLocalLedger(void)
1703 {
1704 char uuid[UUID_BUF_LEN] = {0};
1705 char networkId[NETWORK_ID_BUF_LEN] = {0};
1706
1707 // set local uuid and networkId
1708 if (LnnGenLocalNetworkId(networkId, NETWORK_ID_BUF_LEN) != SOFTBUS_OK ||
1709 LnnGenLocalUuid(uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
1710 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local id fail");
1711 return SOFTBUS_ERR;
1712 }
1713 LnnSetLocalStrInfo(STRING_KEY_UUID, uuid);
1714 LnnSetLocalStrInfo(STRING_KEY_NETWORKID, networkId);
1715 return SOFTBUS_OK;
1716 }
1717
OnReceiveMasterElectMsg(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)1718 static void OnReceiveMasterElectMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
1719 {
1720 cJSON *json = NULL;
1721 ElectMsgPara *para = NULL;
1722
1723 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "recv master elect msg, type:%d, len: %d", type, len);
1724 if (g_netBuilder.isInit == false) {
1725 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
1726 return;
1727 }
1728 if (type != LNN_INFO_TYPE_MASTER_ELECT) {
1729 return;
1730 }
1731 if (strnlen((char *)msg, len) == len) {
1732 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "OnReceiveMasterElectMsg msg invalid");
1733 return;
1734 }
1735 json = cJSON_Parse((char *)msg);
1736 if (json == NULL) {
1737 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "parse elect msg json fail");
1738 return;
1739 }
1740 para = (ElectMsgPara *)SoftBusMalloc(sizeof(ElectMsgPara));
1741 if (para == NULL) {
1742 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc elect msg para fail");
1743 cJSON_Delete(json);
1744 return;
1745 }
1746 if (!GetJsonObjectNumberItem(json, JSON_KEY_MASTER_WEIGHT, ¶->masterWeight) ||
1747 !GetJsonObjectStringItem(json, JSON_KEY_MASTER_UDID, para->masterUdid, UDID_BUF_LEN)) {
1748 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "parse master info json fail");
1749 cJSON_Delete(json);
1750 SoftBusFree(para);
1751 return;
1752 }
1753 cJSON_Delete(json);
1754 if (strcpy_s(para->networkId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
1755 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy network id fail");
1756 SoftBusFree(para);
1757 return;
1758 }
1759 if (PostMessageToHandler(MSG_TYPE_MASTER_ELECT, para) != SOFTBUS_OK) {
1760 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post elect message fail");
1761 SoftBusFree(para);
1762 }
1763 }
1764
OnReceiveNodeAddrChangedMsg(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t size)1765 static void OnReceiveNodeAddrChangedMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t size)
1766 {
1767 (void)type;
1768 size_t addrLen = strnlen((const char *)msg, size);
1769 if (addrLen != size - 1 || addrLen == 0) {
1770 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:bad addr received!networkId=%s", __func__,
1771 AnonymizesNetworkID(networkId));
1772 return;
1773 }
1774 int ret = LnnSetDLNodeAddr(networkId, CATEGORY_NETWORK_ID, (const char *)msg);
1775 if (ret != SOFTBUS_OK) {
1776 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:update node addr failed!networkId=%s,ret=%d", __func__,
1777 AnonymizesNetworkID(networkId), ret);
1778 }
1779 }
1780
LnnUpdateNodeAddr(const char * addr)1781 int32_t LnnUpdateNodeAddr(const char *addr)
1782 {
1783 if (addr == NULL) {
1784 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:null ptr!", __func__);
1785 return SOFTBUS_INVALID_PARAM;
1786 }
1787 NodeBasicInfo *info = NULL;
1788 int32_t infoNum, i;
1789 char localNetworkId[NETWORK_ID_BUF_LEN] = {0};
1790 char addrHis[SHORT_ADDRESS_MAX_LEN];
1791
1792 if (LnnGetLocalStrInfo(STRING_KEY_NODE_ADDR, addrHis, SHORT_ADDRESS_MAX_LEN) == SOFTBUS_OK) {
1793 if (strlen(addr) == strlen(addrHis) && (strcmp(addr, addrHis) == 0)) {
1794 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "%s update the same node addr", __func__);
1795 }
1796 }
1797 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "%s start updating node addr", __func__);
1798 int32_t ret = LnnGetLocalStrInfo(STRING_KEY_NETWORKID, localNetworkId, sizeof(localNetworkId));
1799 if (ret != SOFTBUS_OK) {
1800 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:get local network id failed!", __func__);
1801 return SOFTBUS_ERR;
1802 }
1803
1804 ret = LnnSetLocalStrInfo(STRING_KEY_NODE_ADDR, addr);
1805 if (ret != SOFTBUS_OK) {
1806 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:set local node addr failed!ret=%d", __func__, ret);
1807 return ret;
1808 }
1809 ret = LnnGetAllOnlineNodeInfo(&info, &infoNum);
1810 if (ret != SOFTBUS_OK) {
1811 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "%s:get all online node info fail", __func__);
1812 } else {
1813 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "%s:online nodes count=%d", __func__, infoNum);
1814 for (i = 0; i < infoNum; ++i) {
1815 if (strcmp(localNetworkId, info[i].networkId) == 0) {
1816 continue;
1817 }
1818 SoftBusLog(
1819 SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "sync node address to %s", AnonymizesNetworkID(info[i].networkId));
1820 if (LnnSendSyncInfoMsg(LNN_INFO_TYPE_NODE_ADDR, info[i].networkId, (const uint8_t *)addr, strlen(addr) + 1,
1821 NULL) != SOFTBUS_OK) {
1822 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "sync node address to %s failed",
1823 AnonymizesNetworkID(info[i].networkId));
1824 }
1825 }
1826 SoftBusFree(info);
1827 }
1828 LnnNotifyNodeAddressChanged(addr);
1829 return SOFTBUS_OK;
1830 }
1831
NodeInfoSync(void)1832 int32_t NodeInfoSync(void)
1833 {
1834 if (LnnInitP2p() != SOFTBUS_OK) {
1835 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init lnn p2p fail");
1836 return SOFTBUS_ERR;
1837 }
1838 if (LnnInitNetworkInfo() != SOFTBUS_OK) {
1839 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnInitNetworkInfo fail");
1840 return SOFTBUS_ERR;
1841 }
1842 if (LnnInitDevicename() != SOFTBUS_OK) {
1843 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "LnnInitDeviceName fail");
1844 return SOFTBUS_ERR;
1845 }
1846 return SOFTBUS_OK;
1847 }
1848
LnnInitNetBuilder(void)1849 int32_t LnnInitNetBuilder(void)
1850 {
1851 if (g_netBuilder.isInit == true) {
1852 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "init net builder repeatly");
1853 return SOFTBUS_OK;
1854 }
1855 if (LnnInitSyncInfoManager() != SOFTBUS_OK) {
1856 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init sync info manager fail");
1857 return SOFTBUS_ERR;
1858 }
1859 LnnInitTopoManager();
1860 NodeInfoSync();
1861 NetBuilderConfigInit();
1862 if (RegAuthVerifyListener(&g_verifyListener) != SOFTBUS_OK) {
1863 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "register auth verify listener fail");
1864 return SOFTBUS_ERR;
1865 }
1866 if (LnnRegSyncInfoHandler(LNN_INFO_TYPE_MASTER_ELECT, OnReceiveMasterElectMsg) != SOFTBUS_OK) {
1867 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "register sync master elect msg fail");
1868 return SOFTBUS_ERR;
1869 }
1870 if (LnnRegSyncInfoHandler(LNN_INFO_TYPE_NODE_ADDR, OnReceiveNodeAddrChangedMsg) != SOFTBUS_OK) {
1871 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "register node addr changed msg fail");
1872 return SOFTBUS_ERR;
1873 }
1874 if (ConifgLocalLedger() != SOFTBUS_OK) {
1875 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "config local ledger fail");
1876 return SOFTBUS_ERR;
1877 }
1878 ListInit(&g_netBuilder.fsmList);
1879 ListInit(&g_netBuilder.pendingList);
1880 ListInit(&g_netBuilder.metaNodeList);
1881 g_netBuilder.nodeType = NODE_TYPE_L;
1882 g_netBuilder.looper = GetLooper(LOOP_TYPE_DEFAULT);
1883 if (g_netBuilder.looper == NULL) {
1884 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get default looper fail");
1885 return SOFTBUS_ERR;
1886 }
1887 g_netBuilder.handler.name = (char *)"NetBuilderHandler";
1888 g_netBuilder.handler.looper = g_netBuilder.looper;
1889 g_netBuilder.handler.HandleMessage = NetBuilderMessageHandler;
1890 g_netBuilder.isInit = true;
1891 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "init net builder success");
1892 return SOFTBUS_OK;
1893 }
1894
LnnInitNetBuilderDelay(void)1895 int32_t LnnInitNetBuilderDelay(void)
1896 {
1897 char udid[UDID_BUF_LEN] = {0};
1898 // set master weight and master udid
1899 if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
1900 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "get local udid error!");
1901 return SOFTBUS_ERR;
1902 }
1903 LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, udid);
1904 LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, LnnGetLocalWeight());
1905 if (LnnInitFastOffline() != SOFTBUS_OK) {
1906 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "fast offline init fail!");
1907 return SOFTBUS_ERR;
1908 }
1909 return SOFTBUS_OK;
1910 }
1911
LnnDeinitNetBuilder(void)1912 void LnnDeinitNetBuilder(void)
1913 {
1914 LnnConnectionFsm *item = NULL;
1915 LnnConnectionFsm *nextItem = NULL;
1916
1917 if (!g_netBuilder.isInit) {
1918 return;
1919 }
1920 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
1921 StopConnectionFsm(item);
1922 }
1923 LnnUnregSyncInfoHandler(LNN_INFO_TYPE_MASTER_ELECT, OnReceiveMasterElectMsg);
1924 UnregAuthVerifyListener();
1925 LnnDeinitTopoManager();
1926 LnnDeinitP2p();
1927 LnnDeinitSyncInfoManager();
1928 LnnDeinitFastOffline();
1929 g_netBuilder.isInit = false;
1930 }
1931
LnnServerJoin(ConnectionAddr * addr)1932 int32_t LnnServerJoin(ConnectionAddr *addr)
1933 {
1934 ConnectionAddr *para = NULL;
1935
1936 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnServerJoin enter!");
1937 if (g_netBuilder.isInit == false) {
1938 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
1939 return SOFTBUS_NO_INIT;
1940 }
1941 para = CreateConnectionAddrMsgPara(addr);
1942 if (para == NULL) {
1943 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare join lnn message fail");
1944 return SOFTBUS_MALLOC_ERR;
1945 }
1946 if (PostMessageToHandler(MSG_TYPE_JOIN_LNN, para) != SOFTBUS_OK) {
1947 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post join lnn message failed");
1948 SoftBusFree(para);
1949 return SOFTBUS_NETWORK_LOOPER_ERR;
1950 }
1951 return SOFTBUS_OK;
1952 }
1953
MetaNodeServerJoin(ConnectionAddr * addr,CustomData * customData)1954 int32_t MetaNodeServerJoin(ConnectionAddr *addr, CustomData *customData)
1955 {
1956 ConnectionAddrKey addrDataKey = {
1957 .addr = *addr,
1958 .customData = *customData,
1959 };
1960 ConnectionAddrKey *para = NULL;
1961 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "MetaNodeServerJoin enter!");
1962 if (g_netBuilder.isInit == false) {
1963 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
1964 return SOFTBUS_NO_INIT;
1965 }
1966 para = CreateConnectionAddrMsgParaKey(&addrDataKey);
1967 if (para == NULL) {
1968 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare join lnn message fail");
1969 return SOFTBUS_MALLOC_ERR;
1970 }
1971 if (PostMessageToHandler(MSG_TYPE_JOIN_METANODE, para) != SOFTBUS_OK) {
1972 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post join lnn message failed");
1973 SoftBusFree(para);
1974 return SOFTBUS_NETWORK_LOOPER_ERR;
1975 }
1976 return SOFTBUS_OK;
1977 }
1978
LnnServerLeave(const char * networkId)1979 int32_t LnnServerLeave(const char *networkId)
1980 {
1981 char *para = NULL;
1982
1983 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnServerLeave enter!");
1984 if (g_netBuilder.isInit == false) {
1985 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
1986 return SOFTBUS_NO_INIT;
1987 }
1988 para = CreateNetworkIdMsgPara(networkId);
1989 if (para == NULL) {
1990 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare leave lnn message fail");
1991 return SOFTBUS_MALLOC_ERR;
1992 }
1993 if (PostMessageToHandler(MSG_TYPE_LEAVE_LNN, para) != SOFTBUS_OK) {
1994 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post leave lnn message failed");
1995 SoftBusFree(para);
1996 return SOFTBUS_NETWORK_LOOPER_ERR;
1997 }
1998 return SOFTBUS_OK;
1999 }
2000
MetaNodeServerLeave(const char * networkId)2001 int32_t MetaNodeServerLeave(const char *networkId)
2002 {
2003 char *para = NULL;
2004
2005 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "MetaNodeServerLeave enter!");
2006 if (g_netBuilder.isInit == false) {
2007 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2008 return SOFTBUS_NO_INIT;
2009 }
2010 para = CreateNetworkIdMsgPara(networkId);
2011 if (para == NULL) {
2012 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare leave lnn message fail");
2013 return SOFTBUS_MALLOC_ERR;
2014 }
2015 if (PostMessageToHandler(MSG_TYPE_LEAVE_METANODE, para) != SOFTBUS_OK) {
2016 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post leave lnn message failed");
2017 SoftBusFree(para);
2018 return SOFTBUS_NETWORK_LOOPER_ERR;
2019 }
2020 return SOFTBUS_OK;
2021 }
2022
LnnNotifyDiscoveryDevice(const ConnectionAddr * addr)2023 int32_t LnnNotifyDiscoveryDevice(const ConnectionAddr *addr)
2024 {
2025 ConnectionAddr *para = NULL;
2026
2027 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnNotifyDiscoveryDevice enter!");
2028 if (g_netBuilder.isInit == false) {
2029 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2030 return SOFTBUS_ERR;
2031 }
2032 para = CreateConnectionAddrMsgPara(addr);
2033 if (para == NULL) {
2034 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc discovery device message fail");
2035 return SOFTBUS_MALLOC_ERR;
2036 }
2037 if (PostMessageToHandler(MSG_TYPE_DISCOVERY_DEVICE, para) != SOFTBUS_OK) {
2038 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post notify discovery device message failed");
2039 SoftBusFree(para);
2040 return SOFTBUS_ERR;
2041 }
2042 return SOFTBUS_OK;
2043 }
2044
LnnRequestLeaveInvalidConn(const char * oldNetworkId,ConnectionAddrType addrType,const char * newNetworkId)2045 int32_t LnnRequestLeaveInvalidConn(const char *oldNetworkId, ConnectionAddrType addrType,
2046 const char *newNetworkId)
2047 {
2048 LeaveInvalidConnMsgPara *para = NULL;
2049
2050 if (g_netBuilder.isInit == false) {
2051 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2052 return SOFTBUS_ERR;
2053 }
2054 para = (LeaveInvalidConnMsgPara *)SoftBusMalloc(sizeof(LeaveInvalidConnMsgPara));
2055 if (para == NULL) {
2056 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare leave invalid connection message fail");
2057 return SOFTBUS_MALLOC_ERR;
2058 }
2059 if (strncpy_s(para->oldNetworkId, NETWORK_ID_BUF_LEN, oldNetworkId, strlen(oldNetworkId)) != EOK ||
2060 strncpy_s(para->newNetworkId, NETWORK_ID_BUF_LEN, newNetworkId, strlen(newNetworkId)) != EOK) {
2061 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy old networkId or new networkId fail");
2062 SoftBusFree(para);
2063 return SOFTBUS_MALLOC_ERR;
2064 }
2065 para->addrType = addrType;
2066 if (PostMessageToHandler(MSG_TYPE_LEAVE_INVALID_CONN, para) != SOFTBUS_OK) {
2067 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post leave invalid connection message failed");
2068 SoftBusFree(para);
2069 return SOFTBUS_ERR;
2070 }
2071 return SOFTBUS_OK;
2072 }
2073
LnnRequestCleanConnFsm(uint16_t connFsmId)2074 int32_t LnnRequestCleanConnFsm(uint16_t connFsmId)
2075 {
2076 uint16_t *para = NULL;
2077
2078 if (g_netBuilder.isInit == false) {
2079 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2080 return SOFTBUS_ERR;
2081 }
2082 para = (uint16_t *)SoftBusMalloc(sizeof(uint16_t));
2083 if (para == NULL) {
2084 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc clean connection fsm msg failed");
2085 return SOFTBUS_MALLOC_ERR;
2086 }
2087 *para = connFsmId;
2088 if (PostMessageToHandler(MSG_TYPE_CLEAN_CONN_FSM, para) != SOFTBUS_OK) {
2089 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post request clean connectionlnn message failed");
2090 SoftBusFree(para);
2091 return SOFTBUS_ERR;
2092 }
2093 return SOFTBUS_OK;
2094 }
2095
LnnSyncOfflineComplete(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)2096 void LnnSyncOfflineComplete(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
2097 {
2098 char *para = NULL;
2099
2100 (void)type;
2101 (void)msg;
2102 (void)len;
2103 if (g_netBuilder.isInit == false) {
2104 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2105 return;
2106 }
2107 para = CreateNetworkIdMsgPara(networkId);
2108 if (para == NULL) {
2109 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "prepare notify sync offline message fail");
2110 return;
2111 }
2112 if (PostMessageToHandler(MSG_TYPE_SYNC_OFFLINE_FINISH, para) != SOFTBUS_OK) {
2113 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post sync offline finish message failed");
2114 SoftBusFree(para);
2115 }
2116 }
2117
LnnNotifyNodeStateChanged(const ConnectionAddr * addr)2118 int32_t LnnNotifyNodeStateChanged(const ConnectionAddr *addr)
2119 {
2120 ConnectionAddr *para = NULL;
2121
2122 if (g_netBuilder.isInit == false) {
2123 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2124 return SOFTBUS_ERR;
2125 }
2126 para = CreateConnectionAddrMsgPara(addr);
2127 if (para == NULL) {
2128 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "create node state changed msg failed");
2129 return SOFTBUS_MALLOC_ERR;
2130 }
2131 if (PostMessageToHandler(MSG_TYPE_NODE_STATE_CHANGED, para) != SOFTBUS_OK) {
2132 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post node state changed message failed");
2133 SoftBusFree(para);
2134 return SOFTBUS_ERR;
2135 }
2136 return SOFTBUS_OK;
2137 }
2138
LnnNotifyMasterElect(const char * networkId,const char * masterUdid,int32_t masterWeight)2139 int32_t LnnNotifyMasterElect(const char *networkId, const char *masterUdid, int32_t masterWeight)
2140 {
2141 ElectMsgPara *para = NULL;
2142
2143 if (g_netBuilder.isInit == false) {
2144 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2145 return SOFTBUS_ERR;
2146 }
2147 if (networkId == NULL || masterUdid == NULL) {
2148 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid elect msg para");
2149 return SOFTBUS_INVALID_PARAM;
2150 }
2151 para = (ElectMsgPara *)SoftBusMalloc(sizeof(ElectMsgPara));
2152 if (para == NULL) {
2153 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc elect msg para failed");
2154 return SOFTBUS_MEM_ERR;
2155 }
2156 if (strncpy_s(para->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId)) != EOK ||
2157 strncpy_s(para->masterUdid, UDID_BUF_LEN, masterUdid, strlen(masterUdid)) != EOK) {
2158 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy udid and maser udid failed");
2159 SoftBusFree(para);
2160 return SOFTBUS_ERR;
2161 }
2162 para->masterWeight = masterWeight;
2163 if (PostMessageToHandler(MSG_TYPE_MASTER_ELECT, para) != SOFTBUS_OK) {
2164 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post elect message failed");
2165 SoftBusFree(para);
2166 return SOFTBUS_ERR;
2167 }
2168 return SOFTBUS_OK;
2169 }
2170
2171 /* Note: must called in connection fsm. */
LnnNotifyAuthHandleLeaveLNN(int64_t authId)2172 int32_t LnnNotifyAuthHandleLeaveLNN(int64_t authId)
2173 {
2174 LnnConnectionFsm *item = NULL;
2175
2176 if (g_netBuilder.isInit == false) {
2177 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2178 return SOFTBUS_ERR;
2179 }
2180
2181 LIST_FOR_EACH_ENTRY(item, &g_netBuilder.fsmList, LnnConnectionFsm, node) {
2182 if (item->isDead) {
2183 continue;
2184 }
2185 if (item->connInfo.authId == authId) {
2186 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO,
2187 "[id=%u]connection fsm already use authId: %" PRId64, item->id, authId);
2188 return SOFTBUS_OK;
2189 }
2190 }
2191 AuthHandleLeaveLNN(authId);
2192 return SOFTBUS_OK;
2193 }
2194
LnnRequestLeaveByAddrType(const bool * type,uint32_t typeLen)2195 int32_t LnnRequestLeaveByAddrType(const bool *type, uint32_t typeLen)
2196 {
2197 bool *para = NULL;
2198 if (typeLen != CONNECTION_ADDR_MAX) {
2199 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid typeLen");
2200 return SOFTBUS_ERR;
2201 }
2202 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "LnnRequestLeaveByAddrType");
2203 if (g_netBuilder.isInit == false) {
2204 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2205 return SOFTBUS_ERR;
2206 }
2207 para = (bool *)SoftBusMalloc(sizeof(bool) * typeLen);
2208 if (para == NULL) {
2209 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc leave by addr type msg para failed");
2210 return SOFTBUS_MEM_ERR;
2211 }
2212 if (memcpy_s(para, sizeof(bool) * typeLen, type, sizeof(bool) * typeLen) != EOK) {
2213 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "memcopy para fail");
2214 SoftBusFree(para);
2215 return SOFTBUS_ERR;
2216 }
2217 if (PostMessageToHandler(MSG_TYPE_LEAVE_BY_ADDR_TYPE, para) != SOFTBUS_OK) {
2218 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post leave by addr type message failed");
2219 SoftBusFree(para);
2220 return SOFTBUS_ERR;
2221 }
2222 return SOFTBUS_OK;
2223 }
2224
LnnRequestLeaveSpecific(const char * networkId,ConnectionAddrType addrType)2225 int32_t LnnRequestLeaveSpecific(const char *networkId, ConnectionAddrType addrType)
2226 {
2227 SpecificLeaveMsgPara *para = NULL;
2228
2229 if (networkId == NULL) {
2230 return SOFTBUS_INVALID_PARAM;
2231 }
2232 if (g_netBuilder.isInit == false) {
2233 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no init");
2234 return SOFTBUS_NO_INIT;
2235 }
2236 para = (SpecificLeaveMsgPara *)SoftBusCalloc(sizeof(SpecificLeaveMsgPara));
2237 if (para == NULL) {
2238 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "malloc specific msg fail");
2239 return SOFTBUS_MALLOC_ERR;
2240 }
2241 if (strcpy_s(para->networkId, NETWORK_ID_BUF_LEN, networkId) != EOK) {
2242 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "copy networkId fail");
2243 SoftBusFree(para);
2244 return SOFTBUS_ERR;
2245 }
2246 para->addrType = addrType;
2247 if (PostMessageToHandler(MSG_TYPE_LEAVE_SPECIFIC, para) != SOFTBUS_OK) {
2248 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "post leave specific msg failed");
2249 SoftBusFree(para);
2250 return SOFTBUS_ERR;
2251 }
2252 return SOFTBUS_OK;
2253 }