• 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_MAX
37 } MsgType;
38 
39 #define JSON_KEY_TYPE "TYPE"
40 #define JSON_KEY_IDENTITY "IDENTITY"
41 #define JSON_KEY_DEVICE_ID "DEVICE_ID"
42 #define JSON_KEY_DST_BUS_NAME "DST_BUS_NAME"
43 #define JSON_KEY_SRC_BUS_NAME "SRC_BUS_NAME"
44 #define JSON_KEY_HAS_PRIORITY "HAS_PRIORITY"
45 #define JSON_KEY_UID "UID"
46 #define JSON_KEY_PID "PID"
47 #define JSON_KEY_GROUP_ID "GROUP_ID"
48 #define JSON_KEY_PKG_NAME "PKG_NAME"
49 #define JSON_KEY_SESSION_KEY "SESSION_KEY"
50 #define JSON_KEY_REQUEST_ID "REQUEST_ID"
51 #define JSON_KEY_ENCRYPT "ENCRYPT"
52 #define JSON_KEY_ALGORITHM "ALGORITHM"
53 #define JSON_KEY_CRC "CRC"
54 #define JSON_KEY_BUSINESS_TYPE "BUSINESS_TYPE"
55 
56 typedef struct {
57     uint8_t type; // MsgType
58     uint8_t cipher;
59     int16_t myId;
60     int16_t peerId;
61     int16_t reserved;
62 } ProxyMessageHead;
63 
64 typedef struct {
65     ProxyMessageHead msgHead;
66     int32_t dateLen;
67     char *data;
68     uint32_t connId;
69     int64_t authId; /* for cipher */
70 } ProxyMessage;
71 
72 #define VERSION 1
73 #define PROXY_CHANNEL_HEAD_LEN 8
74 #define VERSION_SHIFT 4
75 #define FOUR_BIT_MASK 0xF
76 #define ENCRYPTED 0x1
77 #define AUTH_SERVER_SIDE 0x2
78 #define USE_BLE_CIPHER 0x4
79 #define PROXY_BYTES_LENGTH_MAX (4 * 1024 * 1024)
80 #define PROXY_MESSAGE_LENGTH_MAX 1024
81 
82 #define IDENTITY_LEN 32
83 typedef enum {
84     PROXY_CHANNEL_STATUS_PYH_CONNECTED,
85     PROXY_CHANNEL_STATUS_PYH_CONNECTING,
86     PROXY_CHANNEL_STATUS_HANDSHAKEING,
87     PROXY_CHANNEL_STATUS_KEEPLIVEING,
88     PROXY_CHANNEL_STATUS_TIMEOUT,
89     PROXY_CHANNEL_STATUS_HANDSHAKE_TIMEOUT,
90     PROXY_CHANNEL_STATUS_COMPLETED
91 } ProxyChannelStatus;
92 
93 #define BASE64KEY 45 // encrypt SessionKey len
94 typedef struct {
95     char sessionKeyBase64[BASE64KEY];
96     size_t len;
97 } SessionKeyBase64;
98 
99 typedef struct {
100     ListNode node;
101     int32_t channelId;
102     int32_t reqId;
103     int8_t isServer;
104     int8_t status;
105     uint16_t timeout;
106     int16_t myId;
107     int16_t peerId;
108     uint32_t connId;
109     ConnectType type;
110     int32_t seq;
111     char identity[IDENTITY_LEN + 1];
112     AppInfo appInfo;
113     int64_t authId; /* for cipher */
114 } ProxyChannelInfo;
115 
116 typedef struct {
117     int32_t active;
118     int32_t timeout;
119     int32_t sliceNumber;
120     int32_t expectedSeq;
121     int32_t dataLen;
122     int32_t bufLen;
123     char *data;
124 } SliceProcessor;
125 
126 #define PROCESSOR_MAX 3
127 typedef struct {
128     ListNode head;
129     int32_t channelId;
130     SliceProcessor processor[PROCESSOR_MAX];
131 } ChannelSliceProcessor;
132 
133 typedef struct {
134     uint8_t *inData;
135     uint32_t inLen;
136     uint8_t *outData;
137     uint32_t outLen;
138 } ProxyDataInfo;
139 
140 int32_t TransProxyUnPackHandshakeErrMsg(const char *msg, int* errCode, int32_t len);
141 int32_t TransProxyUnpackHandshakeAckMsg(const char *msg, ProxyChannelInfo *chanInfo, int32_t len);
142 char* TransProxyPackHandshakeAckMsg(ProxyChannelInfo *chan);
143 char* TransProxyPackHandshakeErrMsg(int32_t errCode);
144 int32_t TransProxyParseMessage(char *data, int32_t len, ProxyMessage *msg);
145 int32_t TransProxyPackMessage(ProxyMessageHead *msg, int64_t authId, ProxyDataInfo *dataInfo);
146 char* TransProxyPackHandshakeMsg(ProxyChannelInfo *info);
147 int32_t TransProxyUnpackHandshakeMsg(const char *msg, ProxyChannelInfo *chan, int32_t len);
148 char* TransProxyPackIdentity(const char *identity);
149 int32_t TransProxyUnpackIdentity(const char *msg, char *identity, uint32_t identitySize, int32_t len);
150 
151 #ifdef __cplusplus
152 #if __cplusplus
153 }
154 #endif /* __cplusplus */
155 #endif /* __cplusplus */
156 
157 #endif
158