!2499 增加UDP通道下,文件传输使用协议的协商机制

Merge pull request !2499 from zhangshen/master
This commit is contained in:
openharmony_ci 2022-10-09 13:31:26 +00:00 committed by Gitee
commit 34ed7693f3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 52 additions and 3 deletions

View File

@ -132,6 +132,7 @@ typedef struct {
int32_t encrypt;
int32_t algorithm;
int32_t crc;
bool isUdpFile;
} ChannelInfo;
#ifdef __cplusplus

View File

@ -30,6 +30,8 @@ extern "C" {
#define APP_INFO_ALGORITHM_AES_GCM_256 0
#define APP_INFO_ALGORITHM_CHACHA 1
#define APP_INFO_UDP_FILE_PROTOCAL 0x5a
typedef enum {
API_UNKNOWN = 0,
API_V1 = 1,
@ -92,6 +94,7 @@ typedef struct {
int32_t encrypt;
int32_t algorithm;
int32_t crc;
int32_t fileProtocal;
} AppInfo;
#ifdef __cplusplus

View File

@ -93,6 +93,7 @@ int32_t ClientIpcOnChannelOpened(const char *pkgName, const char *sessionName, c
WriteInt32(&io, channel->businessType);
WriteString(&io, channel->myIp);
WriteInt32(&io, channel->streamType);
WriteBool(&io, channel->isUdpFile);
if (channel->isServer) {
return OnUdpChannelOpenedAsServer(&svc, &io);
}

View File

@ -92,6 +92,7 @@ int32_t TransClientProxy::OnChannelOpened(const char *sessionName, const Channel
data.WriteInt32(channel->businessType);
data.WriteCString(channel->myIp);
data.WriteInt32(channel->streamType);
data.WriteBool(channel->isUdpFile);
if (!channel->isServer) {
data.WriteInt32(channel->peerPort);
data.WriteCString(channel->peerIp);
@ -101,6 +102,7 @@ int32_t TransClientProxy::OnChannelOpened(const char *sessionName, const Channel
data.WriteInt32(channel->encrypt);
data.WriteInt32(channel->algorithm);
data.WriteInt32(channel->crc);
MessageParcel reply;
MessageOption option;
if (remote->SendRequest(CLIENT_ON_CHANNEL_OPENED, data, reply, option) != 0) {

View File

@ -113,6 +113,7 @@ static int32_t NotifyUdpChannelOpened(const AppInfo *appInfo, bool isServerSide)
info.peerSessionName = (char*)appInfo->peerData.sessionName;
info.routeType = (int32_t)appInfo->routeType;
info.streamType = (int32_t)appInfo->streamType;
info.isUdpFile = appInfo->fileProtocal == APP_INFO_UDP_FILE_PROTOCAL ? true : false;
if (!isServerSide) {
info.peerPort = appInfo->peerData.port;
info.peerIp = (char*)appInfo->peerData.addr;

View File

@ -15,6 +15,7 @@
#include "trans_udp_negotiation_exchange.h"
#include "regex.h"
#include "softbus_message_open_channel.h"
#include "softbus_adapter_crypto.h"
#include "softbus_def.h"
@ -25,8 +26,31 @@
#define BASE64_SESSION_KEY_LEN 45
typedef enum {
CODE_EXCHANGE_UDP_INFO = 6,
CODE_FILE_TRANS_UDP = 0x602
} CodeType;
#define ISHARE_SESSION_NAME "IShare*"
static inline bool IsIShareSession(const char* sessionName)
{
regex_t regComp;
if (regcomp(&regComp, ISHARE_SESSION_NAME, REG_EXTENDED | REG_NOSUB) != 0) {
SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "regcomp failed.");
regfree(&regComp);
return false;
}
bool compare = regexec(&regComp, sessionName, 0, NULL, 0) == 0;
regfree(&regComp);
return compare;
}
static inline CodeType getCodeType(const AppInfo *appInfo)
{
return ((appInfo->udpConnType == UDP_CONN_TYPE_P2P) &&
IsIShareSession(appInfo->myData.sessionName) && (IsIShareSession(appInfo->peerData.sessionName))) ?
CODE_FILE_TRANS_UDP : CODE_EXCHANGE_UDP_INFO;
}
int32_t TransUnpackReplyErrInfo(const cJSON *msg, int32_t *errCode)
{
if ((msg == NULL) && (errCode == NULL)) {
@ -52,6 +76,12 @@ int32_t TransUnpackReplyUdpInfo(const cJSON *msg, AppInfo *appInfo)
(void)GetJsonObjectNumberItem(msg, "PID", &(appInfo->peerData.pid));
(void)GetJsonObjectNumberItem(msg, "BUSINESS_TYPE", (int*)&(appInfo->businessType));
int code = CODE_EXCHANGE_UDP_INFO;
(void)GetJsonObjectNumberItem(msg, "CODE", &code);
if ((code == CODE_FILE_TRANS_UDP) && (getCodeType(appInfo) == CODE_FILE_TRANS_UDP)) {
appInfo->fileProtocal = APP_INFO_UDP_FILE_PROTOCAL;
}
switch (appInfo->udpChannelOptType) {
case TYPE_UDP_CHANNEL_OPEN:
(void)GetJsonObjectNumber64Item(msg, "MY_CHANNEL_ID", &(appInfo->peerData.channelId));
@ -97,6 +127,12 @@ int32_t TransUnpackRequestUdpInfo(const cJSON *msg, AppInfo *appInfo)
(void)GetJsonObjectNumberItem(msg, "CHANNEL_TYPE", &(appInfo->udpChannelOptType));
(void)GetJsonObjectNumberItem(msg, "UDP_CONN_TYPE", &(appInfo->udpConnType));
int code = CODE_EXCHANGE_UDP_INFO;
(void)GetJsonObjectNumberItem(msg, "CODE", &code);
if ((code == CODE_FILE_TRANS_UDP) && (getCodeType(appInfo) == CODE_FILE_TRANS_UDP)) {
appInfo->fileProtocal = APP_INFO_UDP_FILE_PROTOCAL;
}
switch (appInfo->udpChannelOptType) {
case TYPE_UDP_CHANNEL_OPEN:
(void)GetJsonObjectNumber64Item(msg, "MY_CHANNEL_ID", &(appInfo->peerData.channelId));
@ -142,7 +178,7 @@ int32_t TransPackRequestUdpInfo(cJSON *msg, const AppInfo *appInfo)
}
(void)AddStringToJsonObject(msg, "SESSION_KEY", encodeSessionKey);
(void)AddNumberToJsonObject(msg, "CODE", CODE_EXCHANGE_UDP_INFO);
(void)AddNumberToJsonObject(msg, "CODE", getCodeType(appInfo));
(void)AddNumberToJsonObject(msg, "API_VERSION", appInfo->myData.apiVersion);
(void)AddNumberToJsonObject(msg, "UID", appInfo->myData.uid);
(void)AddNumberToJsonObject(msg, "PID", appInfo->myData.pid);
@ -180,7 +216,7 @@ int32_t TransPackReplyUdpInfo(cJSON *msg, const AppInfo *appInfo)
return SOFTBUS_ERR;
}
(void)AddNumberToJsonObject(msg, "CODE", CODE_EXCHANGE_UDP_INFO);
(void)AddNumberToJsonObject(msg, "CODE", getCodeType(appInfo));
(void)AddStringToJsonObject(msg, "PKG_NAME", appInfo->myData.pkgName);
(void)AddNumberToJsonObject(msg, "UID", appInfo->myData.uid);
(void)AddNumberToJsonObject(msg, "PID", appInfo->myData.pid);

View File

@ -53,6 +53,7 @@ int32_t ClientOnChannelOpened(IpcIo *data, IpcIo *reply)
ReadInt32(data, &(channel.businessType));
channel.myIp = (char *)ReadString(data, &size);
ReadInt32(data, &(channel.streamType));
ReadBool(data, &(channel.isUdpFile));
if (channel.isServer) {
int32_t udpPort = TransOnChannelOpened(sessionName, &channel);
WriteInt32(reply, udpPort);

View File

@ -254,6 +254,7 @@ int32_t SoftBusClientStub::OnChannelOpenedInner(MessageParcel &data, MessageParc
data.ReadInt32(channel.businessType);
channel.myIp = (char *)data.ReadCString();
data.ReadInt32(channel.streamType);
data.ReadBool(channel.isUdpFile);
if (!channel.isServer) {
data.ReadInt32(channel.peerPort);
channel.peerIp = (char *)data.ReadCString();
@ -263,6 +264,7 @@ int32_t SoftBusClientStub::OnChannelOpenedInner(MessageParcel &data, MessageParc
data.ReadInt32(channel.encrypt);
data.ReadInt32(channel.algorithm);
data.ReadInt32(channel.crc);
int ret = OnChannelOpened(sessionName, &channel);
bool res = reply.WriteInt32(ret);
if (!res) {

View File

@ -154,7 +154,9 @@ int32_t TransOnFileChannelOpened(const char *sessionName, const ChannelInfo *cha
return SOFTBUS_INVALID_PARAM;
}
int32_t fileSession;
(void)NSTACKX_DFileSetCapabilities(NSTACKX_CAPS_UDP_GSO | NSTACKX_CAPS_WLAN_CATAGORY, NSTACKX_WLAN_CAT_TCP);
uint32_t capabilityValue = channel->isUdpFile? NSTACKX_WLAN_CAT_DIRECT : NSTACKX_WLAN_CAT_TCP;
(void)NSTACKX_DFileSetCapabilities(NSTACKX_CAPS_UDP_GSO | NSTACKX_CAPS_WLAN_CATAGORY, capabilityValue);
if (channel->isServer) {
FileListener fileListener;
(void)memset_s(&fileListener, sizeof(FileListener), 0, sizeof(FileListener));