• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SOFTBUS_PROXYCHANNEL_MESSAGE_H
17 #define SOFTBUS_PROXYCHANNEL_MESSAGE_H
18 #include "stdint.h"
19 #include "common_list.h"
20 #include "softbus_app_info.h"
21 #include "softbus_conn_interface.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 typedef enum {
30     PROXYCHANNEL_MSG_TYPE_NORMAL,
31     PROXYCHANNEL_MSG_TYPE_HANDSHAKE,
32     PROXYCHANNEL_MSG_TYPE_HANDSHAKE_ACK,
33     PROXYCHANNEL_MSG_TYPE_RESET,
34     PROXYCHANNEL_MSG_TYPE_KEEPALIVE,
35     PROXYCHANNEL_MSG_TYPE_KEEPALIVE_ACK,
36     PROXYCHANNEL_MSG_TYPE_HANDSHAKE_AUTH,
37 
38     PROXYCHANNEL_MSG_TYPE_MAX
39 } MsgType;
40 
41 #define JSON_KEY_TYPE "TYPE"
42 #define JSON_KEY_IDENTITY "IDENTITY"
43 #define JSON_KEY_DEVICE_ID "DEVICE_ID"
44 #define JSON_KEY_MTU_SIZE "MTU_SIZE"
45 #define JSON_KEY_DST_BUS_NAME "DST_BUS_NAME"
46 #define JSON_KEY_SRC_BUS_NAME "SRC_BUS_NAME"
47 #define JSON_KEY_HAS_PRIORITY "HAS_PRIORITY"
48 #define JSON_KEY_UID "UID"
49 #define JSON_KEY_PID "PID"
50 #define JSON_KEY_GROUP_ID "GROUP_ID"
51 #define JSON_KEY_PKG_NAME "PKG_NAME"
52 #define JSON_KEY_SESSION_KEY "SESSION_KEY"
53 #define JSON_KEY_REQUEST_ID "REQUEST_ID"
54 #define JSON_KEY_ENCRYPT "ENCRYPT"
55 #define JSON_KEY_ALGORITHM "ALGORITHM"
56 #define JSON_KEY_CRC "CRC"
57 #define JSON_KEY_BUSINESS_TYPE "BUSINESS_TYPE"
58 #define JSON_KEY_TRANS_FLAGS "TRANS_FLAGS"
59 #define JSON_KEY_MIGRATE_OPTION "MIGRATE_OPTION"
60 #define JSON_KEY_MY_HANDLE_ID "MY_HANDLE_ID"
61 #define JSON_KEY_PEER_HANDLE_ID "PEER_HANDLE_ID"
62 #define JSON_KEY_AUTH_SEQ "AUTH_SEQ"
63 #define JSON_KEY_ROUTE_TYPE "ROUTE_TYPE"
64 #define JSON_KEY_FIRST_DATA "FIRST_DATA"
65 #define JSON_KEY_FIRST_DATA_SIZE "FIRST_DATA_SIZE"
66 
67 typedef struct {
68     uint8_t type; // MsgType
69     uint8_t cipher;
70     int16_t myId;
71     int16_t peerId;
72     int16_t reserved;
73 } ProxyMessageHead;
74 
75 typedef struct {
76     ProxyMessageHead msgHead;
77     int32_t dateLen;
78     char *data;
79     uint32_t connId;
80     int64_t authId; /* for cipher */
81     int32_t keyIndex;
82 } ProxyMessage;
83 
84 #define VERSION 1
85 #define PROXY_CHANNEL_HEAD_LEN 8
86 #define VERSION_SHIFT 4
87 #define FOUR_BIT_MASK 0xF
88 #define ENCRYPTED 0x1
89 #define AUTH_SERVER_SIDE 0x2
90 #define USE_BLE_CIPHER 0x4
91 #define BAD_CIPHER 0x8
92 #define CS_MODE 0x10
93 #define PROXY_BYTES_LENGTH_MAX (4 * 1024 * 1024)
94 #define PROXY_MESSAGE_LENGTH_MAX 1024
95 
96 #define IDENTITY_LEN 32
97 typedef enum {
98     PROXY_CHANNEL_STATUS_PYH_CONNECTED,
99     PROXY_CHANNEL_STATUS_PYH_CONNECTING,
100     PROXY_CHANNEL_STATUS_HANDSHAKEING,
101     PROXY_CHANNEL_STATUS_KEEPLIVEING,
102     PROXY_CHANNEL_STATUS_TIMEOUT,
103     PROXY_CHANNEL_STATUS_HANDSHAKE_TIMEOUT,
104     PROXY_CHANNEL_STATUS_COMPLETED
105 } ProxyChannelStatus;
106 
107 #define BASE64KEY 45 // encrypt SessionKey len
108 typedef struct {
109     char sessionKeyBase64[BASE64KEY];
110     size_t len;
111 } SessionKeyBase64;
112 
113 typedef struct {
114     ListNode node;
115     int32_t channelId;
116     int32_t reqId;
117     int8_t isServer;
118     int8_t status;
119     uint16_t timeout;
120     int16_t myId;
121     int16_t peerId;
122     uint32_t connId;
123     ConnectType type;
124     BleProtocolType blePrototolType;
125     int32_t seq;
126     char identity[IDENTITY_LEN + 1];
127     AppInfo appInfo;
128     int64_t authId; /* for cipher */
129 } ProxyChannelInfo;
130 
131 typedef struct {
132     uint8_t *inData;
133     uint32_t inLen;
134     uint8_t *outData;
135     uint32_t outLen;
136 } ProxyDataInfo;
137 
138 typedef struct  {
139     int32_t magicNumber;
140     int32_t seq;
141     int32_t flags;
142     int32_t dataLen;
143 } PacketFastHead;
144 
145 typedef struct {
146     int32_t priority;
147     int32_t sliceNum;
148     int32_t sliceSeq;
149     int32_t reserved;
150 } SliceFastHead;
151 
152 typedef struct {
153     int seq;
154     int packetFlag;
155     int shouldAck;
156 } SessionHead;
157 
158 int32_t TransProxyUnPackHandshakeErrMsg(const char *msg, int* errCode, int32_t len);
159 int32_t TransProxyUnpackHandshakeAckMsg(const char *msg, ProxyChannelInfo *chanInfo,
160     int32_t len, uint16_t *fastDataSize);
161 char* TransProxyPackHandshakeAckMsg(ProxyChannelInfo *chan);
162 char* TransProxyPackHandshakeErrMsg(int32_t errCode);
163 int32_t TransProxyParseMessage(char *data, int32_t len, ProxyMessage *msg);
164 int32_t TransProxyPackMessage(ProxyMessageHead *msg, int64_t authId, ProxyDataInfo *dataInfo);
165 char* TransProxyPackHandshakeMsg(ProxyChannelInfo *info);
166 int32_t TransProxyUnpackHandshakeMsg(const char *msg, ProxyChannelInfo *chan, int32_t len);
167 char* TransProxyPackIdentity(const char *identity);
168 int32_t TransProxyUnpackIdentity(const char *msg, char *identity, uint32_t identitySize, int32_t len);
169 char *TransProxyPackFastData(const AppInfo *appInfo, uint32_t *outLen);
170 int32_t PackPlaintextMessage(ProxyMessageHead *msg, ProxyDataInfo *dataInfo);
171 
172 #ifdef __cplusplus
173 #if __cplusplus
174 }
175 #endif /* __cplusplus */
176 #endif /* __cplusplus */
177 
178 #endif
179