当前位置:   article > 正文

软总线源码分析7:认证模块_initdeviceauthservice

initdeviceauthservice

根据上一章节对发现模块的分析,我们知道当发现了外部设备后,InnerDeviceFound会被调用,InnerDeviceFound函数会根据infoNode是否为内部节点,调用回调函数。

回想第4章节我们介绍组网模块的时候,组网会调用EnableCoapDisc函数将本地服务发布后,主动发现外部服务,EnableCoapDisc的源码如下:

  1. static int32_t EnableCoapDisc(void)
  2. {
  3. LOG_INFO("EnableCoapDisc begin");
  4. int32_t ret = DiscSetDiscoverCallback(MODULE_LNN, &g_discCb);
  5. if (ret != SOFTBUS_OK) {
  6. LOG_ERR("DiscSetDiscoverCallback error!");
  7. return SOFTBUS_ERR;
  8. }
  9. PublishInnerInfo publishInfo = {
  10. .publishId = LNN_PUBLISH_ID,
  11. .medium = COAP,
  12. .freq = HIGH,
  13. .capability = LNN_DISC_CAPABILITY,
  14. .capabilityData = (unsigned char *)LNN_DISC_CAPABILITY,
  15. .dataLen = strlen(LNN_DISC_CAPABILITY) + 1,
  16. };
  17. LOG_INFO("DiscStartScan!");
  18. ret = DiscStartScan(MODULE_LNN, &publishInfo);
  19. if (ret != SOFTBUS_OK) {
  20. LOG_ERR("DiscStartScan fail!");
  21. return SOFTBUS_ERR;
  22. }
  23. SubscribeInnerInfo subscribeInfo = {
  24. .subscribeId = LNN_SUBSCRIBE_ID,
  25. .medium = COAP,
  26. .freq = HIGH,
  27. .isSameAccount = false,
  28. .isWakeRemote = false,
  29. .capability = LNN_DISC_CAPABILITY,
  30. .capabilityData = (unsigned char *)LNN_DISC_CAPABILITY,
  31. .dataLen = strlen(LNN_DISC_CAPABILITY) + 1,
  32. };
  33. LOG_INFO("DiscStartAdvertise!");
  34. ret = DiscStartAdvertise(MODULE_LNN, &subscribeInfo);
  35. if (ret != SOFTBUS_OK) {
  36. LOG_ERR("DiscStartAdvertise fail!");
  37. return SOFTBUS_ERR;
  38. }
  39. return SOFTBUS_OK;
  40. }

我们主要关心该函数的第一行,其将g_discCb挂载到本地,具体调用链不展开,总之在组网场景下,当发现外部设备后,InnerDeviceFound函数会调用g_discCb的OnDeviceFound函数指针进行处理,其函数名为DeviceFound,源码如下:

  1. static void DeviceFound(const DeviceInfo *device)
  2. {
  3. ConnectionAddr *para = NULL;
  4. if (device == NULL) {
  5. LOG_ERR("DeviceFound error!");
  6. return;
  7. }
  8. LOG_INFO("DeviceFound! type = %d", g_status.type);
  9. para = (ConnectionAddr *)SoftBusCalloc(sizeof(ConnectionAddr));
  10. if (para == NULL) {
  11. LOG_ERR("malloc init message fail");
  12. return;
  13. }
  14. para->type = g_status.type;
  15. para->info.ip.port = device->addr[0].port;
  16. if (strncpy_s(para->info.ip.ip, IP_STR_MAX_LEN, device->addr[0].addr, strlen(device->addr[0].addr)) != EOK) {
  17. LOG_ERR("STR ERROR!");
  18. SoftBusFree(para);
  19. return;
  20. }
  21. if (g_status.fsm != NULL) {
  22. LOG_INFO("PostMessageToFsm!");
  23. (void)LnnFsmRemoveMessage(g_status.fsm, FSM_MSG_TYPE_DISCOVERY_TIMEOUT);
  24. if (LnnFsmPostMessage(g_status.fsm, FSM_MSG_TYPE_JOIN_LNN, para) != SOFTBUS_OK) {
  25. LOG_ERR("LnnFsmPostMessage FSM_MSG_TYPE_JOIN_LNN error!");
  26. SoftBusFree(para);
  27. }
  28. } else {
  29. LOG_ERR("DeviceFound don't post to fsm!");
  30. SoftBusFree(para);
  31. }
  32. return;
  33. }

该函数首先根据发现的设备信息,构造para地址信息,然后移除掉给状态机设置的超时定时器,然后向状态机提交FSM_MSG_TYPE_JOIN_LNN消息。

此时,该设备即向对端设备请求认证,这种场景下我们成本地发送认证消息的设备为客户端,对端接收认证消息的设备为服务端。

发送认证请求

状态机的机制已经在之前的章节介绍过了,因为当前状态机还在初始AUTH_INDEX状态下,因此该消息触发状态机的OnJoinLNNInAuth的函数。其源码如下:

  1. static int32_t OnJoinLNNInAuth(ConnectionAddr *addr)
  2. {
  3. int32_t rc;
  4. ConnInfo *connInfo = &g_netBuilder.connInfo;
  5. ConnectOption option;
  6. if (addr == NULL) {
  7. return SOFTBUS_INVALID_PARAM;
  8. }
  9. if (ConvertAddrToOption(addr, &option) == false) {
  10. SoftBusFree(addr);
  11. return SOFTBUS_ERR;
  12. }
  13. if ((connInfo->flag & (CONN_INFO_FLAG_JOINING_ACTIVE |
  14. CONN_INFO_FLAG_JOINING_PASSIVE)) != 0) {
  15. if (LnnIsSameConnectionAddr(addr, &connInfo->addr) == true) {
  16. LOG_INFO("addr is same, waiting...");
  17. SoftBusFree(addr);
  18. return SOFTBUS_OK;
  19. }
  20. LOG_ERR("previous request is ongoing, reject it");
  21. LnnNotifyJoinResult(addr, NULL, SOFTBUS_ERR);
  22. SoftBusFree(addr);
  23. return SOFTBUS_OK;
  24. }
  25. LOG_INFO("begin a new join request");
  26. connInfo->addr = *addr;
  27. connInfo->flag |= CONN_INFO_FLAG_JOINING_ACTIVE;
  28. (void)AuthVerifyInit();
  29. LOG_INFO("hichain init ok....");
  30. rc = AuthVerifyDevice(LNN, &option);
  31. if (rc != SOFTBUS_OK) {
  32. CompleteJoinLNN(NULL, SOFTBUS_ERR);
  33. (void)AuthVerifyDeinit();
  34. } else {
  35. LnnFsmPostMessageDelay(&g_netBuilder.fsm, FSM_MSG_TYPE_JOIN_LNN_TIMEOUT,
  36. NULL, JOIN_LNN_TIMEOUT_LEN);
  37. }
  38. SoftBusFree(addr);
  39. LOG_INFO("rc = %d", rc);
  40. return rc;
  41. }

OnJoinLNNInAuth首先将Addr转为Option,然后判断当前系统是否正在进行组网过程,如果是则直接返回。如果不是,则首先调用AuthVerifyInit初始化认证工作,然后调用AuthVerifyDevice进行认证处理,最后调用LnnFsmPostMessageDelay启动15S超时定时器。

认证处理初始化

AuthVerifyInit的源码如下:

  1. int32_t AuthVerifyInit(void)
  2. {
  3. if (HichainServiceInit() != SOFTBUS_OK) {
  4. LOG_ERR("HichainServiceInit failed");
  5. return SOFTBUS_ERR;
  6. }
  7. return SOFTBUS_OK;
  8. }

该函数即调用了HichainServiceInit,其源码如下:

  1. static int32_t HichainServiceInit(void)
  2. {
  3. if (InitDeviceAuthService() != 0) {
  4. LOG_ERR("auth InitDeviceAuthService failed");
  5. return SOFTBUS_ERR;
  6. }
  7. g_hichainGaInstance = GetGaInstance();
  8. if (g_hichainGaInstance == NULL) {
  9. LOG_ERR("auth GetGaInstance failed");
  10. return SOFTBUS_ERR;
  11. }
  12. g_hichainGmInstance = GetGmInstance();
  13. if (g_hichainGmInstance == NULL) {
  14. LOG_ERR("auth GetGmInstance failed");
  15. return SOFTBUS_ERR;
  16. }
  17. (void)memset_s(&g_hichainCallback, sizeof(DeviceAuthCallback), 0, sizeof(DeviceAuthCallback));
  18. g_hichainCallback.onTransmit = AuthOnTransmit;
  19. g_hichainCallback.onSessionKeyReturned = AuthOnSessionKeyReturned;
  20. g_hichainCallback.onFi
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/253285
推荐阅读
相关标签
  

闽ICP备14008679号