opensession p2p use ctrl channel to verify ip and port

Signed-off-by: breakfei1 <zhangshen6@huawei.com>
This commit is contained in:
breakfei1 2023-07-12 17:35:59 +08:00
parent 20fc7ca9b8
commit fd0571b543
5 changed files with 209 additions and 23 deletions

View File

@ -34,6 +34,7 @@ ble_connection_deps = []
if (dsoftbus_feature_conn_ble == false) {
ble_connection_src = [
"$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_direct_virtual.c",
"$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_direct_pipeline_virtual.c",
"$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_virtual.c",
]
ble_connection_inc = [
@ -68,7 +69,10 @@ if (dsoftbus_feature_conn_ble == false) {
ble_connection_inc +=
[ "$dsoftbus_root_path/dsoftbus_enhance/core/connection/ble/include" ]
} else {
ble_connection_src += [ "$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_direct_virtual.c" ]
ble_connection_src += [
"$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_direct_virtual.c",
"$dsoftbus_root_path/core/connection/ble/src/softbus_conn_ble_direct_pipeline_virtual.c",
]
}
if (enhanced_conn_coc == true) {
ble_connection_src += [

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "softbus_log.h"
#include "softbus_conn_interface.h"
#include "softbus_errcode.h"
int32_t ConnBleDirectPipelineOpen(const ConnBleDirectPipelineOption *option, const ConnectResult *result)
{
return SOFTBUS_ERR;
}
int32_t ConnBleDirectPipelineClose(int32_t channelId)
{
return SOFTBUS_ERR;
}
int32_t ConnBleDirectPipelineInit(ConnBleDirectPipelineCallback* cb)
{
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "do not support ble direct pipeline");
return SOFTBUS_OK;
}
int32_t ConnBleDirectPipelineSendMessage(int32_t channelId, const uint8_t *data, uint32_t dataLen,
PipelineMsgType type)
{
return SOFTBUS_ERR;
}
void PipelineRegisterIpPortVerifyCallBack(const OnMessageReceivedFunc cb) {
return;
}
int32_t GetPipelineIdByPeerNetworkId(const char* peerNetworkId)
{
return INVALID_CHANNEL_ID;
}

View File

@ -395,6 +395,37 @@ int32_t ConnPreventConnection(const ConnectOption *option, uint32_t time);
* @return <b>SOFTBUS_OK</b> if prevent connect other devices successfully, others if failed.
*/
int32_t ConnGetTypeByConnectionId(uint32_t connectionId, ConnectType *type);
typedef void (*OnMessageReceivedFunc)(int32_t channelId, const char *data, uint32_t len);
void PipelineRegisterIpPortVerifyCallBack(const OnMessageReceivedFunc cb);
typedef struct {
void (*onScanReceived)(const char *networkId, uint8_t *buf, size_t bufLen);
int (*onChannnelOpened)(int32_t channelId, const char *networkId, unsigned char isServer);
void (*onChannelFailed)(int32_t channelId, const char *networkId);
void (*onChannelClosed)(int32_t channelId);
void (*onMessageReceived)(int32_t channelId, const uint8_t *data, uint32_t len);
} ConnBleDirectPipelineCallback;
int32_t ConnBleDirectPipelineInit(ConnBleDirectPipelineCallback* cb);
typedef struct {
const char *networkId;
uint8_t *buf;
int bufLen;
} ConnBleDirectPipelineOption;
int32_t ConnBleDirectPipelineOpen(const ConnBleDirectPipelineOption *option, const ConnectResult *result);
int32_t ConnBleDirectPipelineClose(int32_t channelId);
typedef enum {
PIPE_LINE_MSG_TYPE_P2P_NEGO = 0xABADBEEF,
PIPE_LINE_MSG_TYPE_IP_PORT_EXCHANGE,
} PipelineMsgType;
int32_t ConnBleDirectPipelineSendMessage(int32_t channelId, const uint8_t *data, uint32_t dataLen,
PipelineMsgType type);
int32_t GetPipelineIdByPeerNetworkId(const char* peerNetworkId);
#ifdef __cplusplus
#if __cplusplus
}

View File

@ -35,6 +35,8 @@
#include "softbus_adapter_hitrace.h"
#define ID_OFFSET (1)
#define P2P_VERIFY_REQUEST 0
#define P2P_VERIFY_REPLY 1
static int32_t g_p2pSessionPort = -1;
static char g_p2pSessionIp[IP_LEN] = {0};
@ -284,20 +286,68 @@ static int32_t OpenAuthConn(const char *uuid, uint32_t reqId, bool isMeta)
}
static void SendVerifyP2pFailRsp(int64_t authId, int64_t seq,
int32_t code, int32_t errCode, const char *errDesc)
int32_t code, int32_t errCode, const char *errDesc, bool isAuthLink)
{
char *reply = VerifyP2pPackError(code, errCode, errDesc);
if (reply == NULL) {
return;
}
if (SendAuthData(authId, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SendVerifyP2pFailRsp send auth data fail");
if (isAuthLink) {
if (SendAuthData(authId, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SendVerifyP2pFailRsp send auth data fail");
}
} else {
uint32_t strLen = strlen(reply) + 1;
char *sendMsg = (char*)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
if (sendMsg == NULL) {
cJSON_free(reply);
return;
}
*(int64_t*)sendMsg = P2P_VERIFY_REPLY;
*(int64_t*)(sendMsg + sizeof(int64_t)) = seq;
if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, reply) != EOK) {
cJSON_free(reply);
SoftBusFree(sendMsg);
return;
}
ConnBleDirectPipelineSendMessage(authId, (uint8_t *)sendMsg,
strLen + sizeof(int64_t) + sizeof(int64_t), PIPE_LINE_MSG_TYPE_IP_PORT_EXCHANGE);
}
cJSON_free(reply);
return;
}
static int32_t OnVerifyP2pRequest(int64_t authId, int64_t seq, const cJSON *json)
static int32_t SendVerifyP2pRsp(int64_t authId, int32_t module, int32_t flag, int64_t seq,
const char *reply, bool isAuthLink)
{
int32_t ret = SOFTBUS_ERR;
if (isAuthLink) {
ret = SendAuthData(authId, module, flag, seq, reply);
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SendVerifyP2pFailRsp send auth data fail");
}
} else {
uint32_t strLen = strlen(reply) + 1;
char *sendMsg = (char*)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
if (sendMsg == NULL) {
return SOFTBUS_ERR;
}
*(int64_t*)sendMsg = P2P_VERIFY_REPLY;
*(int64_t*)(sendMsg + sizeof(int64_t)) = seq;
if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, reply) != EOK) {
SoftBusFree(sendMsg);
return SOFTBUS_ERR;
}
ret = ConnBleDirectPipelineSendMessage(authId, (uint8_t *)sendMsg,
strLen + sizeof(int64_t) + sizeof(int64_t), PIPE_LINE_MSG_TYPE_IP_PORT_EXCHANGE);
if (ret != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ConnBleDirectPipelineSendMessage fail");
}
}
return ret;
}
static int32_t OnVerifyP2pRequest(int64_t authId, int64_t seq, const cJSON *json, bool isAuthLink)
{
SoftBusLog(
SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OnVerifyP2pRequest: authId=%" PRId64 ", seq=%" PRId64, authId, seq);
@ -308,28 +358,27 @@ static int32_t OnVerifyP2pRequest(int64_t authId, int64_t seq, const cJSON *json
int32_t ret = VerifyP2pUnPack(json, peerIp, IP_LEN, &peerPort);
if (ret != SOFTBUS_OK) {
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "OnVerifyP2pRequest unpack fail");
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "OnVerifyP2pRequest unpack fail", isAuthLink);
return ret;
}
if (GetWifiDirectManager()->getLocalIpByRemoteIp(peerIp, myIp, sizeof(myIp)) != SOFTBUS_OK) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OnVerifyP2pRequest get p2p ip fail");
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "get p2p ip fail");
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "get p2p ip fail", isAuthLink);
return SOFTBUS_TRANS_GET_P2P_INFO_FAILED;
}
ret = StartP2pListener(myIp, &myPort);
if (ret != SOFTBUS_OK) {
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "invalid p2p port");
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "invalid p2p port", isAuthLink);
return SOFTBUS_ERR;
}
char *reply = VerifyP2pPack(myIp, myPort);
if (reply == NULL) {
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "pack reply failed");
SendVerifyP2pFailRsp(authId, seq, CODE_VERIFY_P2P, ret, "pack reply failed", isAuthLink);
return SOFTBUS_PARSE_JSON_ERR;
}
ret = SendAuthData(authId, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply);
ret = SendVerifyP2pRsp(authId, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply, isAuthLink);
cJSON_free(reply);
if (ret != SOFTBUS_OK) {
return ret;
@ -420,7 +469,7 @@ static void OnAuthMsgProc(int64_t authId, int32_t flags, int64_t seq, const cJSO
{
int32_t ret = SOFTBUS_ERR;
if (flags == MSG_FLAG_REQUEST) {
ret = OnVerifyP2pRequest(authId, seq, json);
ret = OnVerifyP2pRequest(authId, seq, json, true);
} else {
ret = OnVerifyP2pReply(authId, seq, json);
}
@ -457,7 +506,7 @@ static void OnAuthChannelClose(int64_t authId)
}
NO_SANITIZE("cfi") static int32_t OpenNewAuthConn(const AppInfo *appInfo, SessionConn *conn,
int32_t newChannelId, int32_t *channelId, uint32_t requestId)
int32_t newChannelId, uint32_t requestId)
{
int32_t ret = OpenAuthConn(appInfo->peerData.deviceId, requestId, conn->isMeta);
if (ret != SOFTBUS_OK) {
@ -465,10 +514,59 @@ NO_SANITIZE("cfi") static int32_t OpenNewAuthConn(const AppInfo *appInfo, Sessio
TransDelSessionConnById(newChannelId);
return ret;
}
*channelId = newChannelId;
return SOFTBUS_OK;
}
static void OnP2pVerifyMsgReceived(int32_t channelId, const char *data, uint32_t len)
{
TRAN_CHECK_AND_RETURN_LOG((data != NULL) && (len > sizeof(int64_t) + sizeof(int64_t)), "received data is invalid");
cJSON *json = cJSON_ParseWithLength((data + sizeof(int64_t) + sizeof(int64_t)),
len - sizeof(int64_t) - sizeof(int64_t));
TRAN_CHECK_AND_RETURN_LOG((json != NULL), "parse json failed");
int64_t msgType = *(int64_t*)data;
if (msgType == P2P_VERIFY_REQUEST) {
OnVerifyP2pRequest(channelId, *(int64_t*)(data + sizeof(int64_t)), json, false);
} else if (msgType == P2P_VERIFY_REPLY) {
OnVerifyP2pReply(channelId, *(int64_t*)(data + sizeof(int64_t)), json);
} else {
TLOGE("invalid data type: %lld", msgType);
}
}
static int32_t StartVerifyP2pInfo(const AppInfo *appInfo, SessionConn *conn)
{
int32_t ret = SOFTBUS_ERR;
int32_t newChannelId = conn->channelId;
int32_t pipeLineChannelId = GetPipelineIdByPeerNetworkId(appInfo->peerNetWorkId);
if (pipeLineChannelId == INVALID_CHANNEL_ID) {
ret = OpenNewAuthConn(appInfo, conn, newChannelId, conn->requestId);
} else {
char *msg = VerifyP2pPack(conn->appInfo.myData.addr, conn->appInfo.myData.port);
if (msg == NULL) {
return SOFTBUS_ERR;
}
uint32_t strLen = strlen(msg) + 1;
char *sendMsg = (char*)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
if (sendMsg == NULL) {
cJSON_free(msg);
return SOFTBUS_ERR;
}
*(int64_t*)sendMsg = P2P_VERIFY_REQUEST;
*(int64_t*)(sendMsg + sizeof(int64_t)) = conn->req;
if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, msg) != EOK) {
cJSON_free(msg);
SoftBusFree(sendMsg);
return SOFTBUS_ERR;
}
ret = ConnBleDirectPipelineSendMessage(pipeLineChannelId, (uint8_t *)sendMsg,
strLen + sizeof(int64_t) + sizeof(int64_t), PIPE_LINE_MSG_TYPE_IP_PORT_EXCHANGE);
cJSON_free(msg);
SoftBusFree(sendMsg);
}
return ret;
}
NO_SANITIZE("cfi") int32_t OpenP2pDirectChannel(const AppInfo *appInfo, const ConnectOption *connInfo,
int32_t *channelId)
{
@ -486,7 +584,6 @@ NO_SANITIZE("cfi") int32_t OpenP2pDirectChannel(const AppInfo *appInfo, const Co
SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(conn->channelId + ID_OFFSET));
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO,
"SoftbusHitraceChainBegin: set chainId=[%lx].", (uint64_t)(conn->channelId + ID_OFFSET));
int32_t newChannelId = conn->channelId;
(void)memcpy_s(&conn->appInfo, sizeof(AppInfo), appInfo, sizeof(AppInfo));
ret = StartP2pListener(conn->appInfo.myData.addr, &conn->appInfo.myData.port);
@ -512,9 +609,14 @@ NO_SANITIZE("cfi") int32_t OpenP2pDirectChannel(const AppInfo *appInfo, const Co
SoftBusFree(conn);
return ret;
}
ret = OpenNewAuthConn(appInfo, conn, newChannelId, channelId, requestId);
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenP2pDirectChannel end: channelId=%d", newChannelId);
ret = StartVerifyP2pInfo(appInfo, conn);
if (ret != SOFTBUS_OK) {
TransDelSessionConnById(conn->channelId);
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "StartVerifyP2pInfo fail, ret=%d", ret);
return ret;
}
*channelId = conn->channelId;
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenP2pDirectChannel end: channelId=%d", conn->channelId);
return ret;
}
@ -533,6 +635,7 @@ NO_SANITIZE("cfi") int32_t P2pDirectChannelInit(void)
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "P2pDirectChannelInit set cb fail");
return SOFTBUS_ERR;
}
PipelineRegisterIpPortVerifyCallBack(OnP2pVerifyMsgReceived);
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "P2pDirectChannelInit ok");
return SOFTBUS_OK;
}

View File

@ -304,13 +304,13 @@ HWTEST_F(TransTcpDirectP2pTest, OnVerifyP2pRequestTest001, TestSize.Level1)
string msg = TestGetMsgPack();
cJSON *json = cJSON_Parse(msg.c_str());
EXPECT_TRUE(json != nullptr);
SendVerifyP2pFailRsp(authId, seq, code, errCode, nullptr);
SendVerifyP2pFailRsp(authId, seq, code, errCode, nullptr, true);
SendVerifyP2pFailRsp(authId, seq, code, errCode, errDesc);
int32_t ret = OnVerifyP2pRequest(authId, seq, nullptr);
SendVerifyP2pFailRsp(authId, seq, code, errCode, errDesc, true);
int32_t ret = OnVerifyP2pRequest(authId, seq, nullptr, true);
EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = OnVerifyP2pRequest(authId, seq, json);
ret = OnVerifyP2pRequest(authId, seq, json, true);
EXPECT_EQ(ret, SOFTBUS_PARSE_JSON_ERR);
OnAuthChannelClose(authId);
@ -389,7 +389,7 @@ HWTEST_F(TransTcpDirectP2pTest, OnAuthDataRecvTest001, TestSize.Level1)
data->seq = 1;
data->data = (const uint8_t *)str;
data->len = AUTH_TRANS_DATA_LEN;
int32_t ret = OnVerifyP2pRequest(authId, seq, nullptr);
int32_t ret = OnVerifyP2pRequest(authId, seq, nullptr, true);
EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
OnAuthMsgProc(authId, flags, seq, nullptr);