1 /*
2 * Copyright (C) 2022 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 #include "fillp_mgt_msg_log.h"
17 #include "net.h"
18 #include "nstackx_util.h"
19
20 #ifdef FILLP_MGT_MSG_LOG
21
22 #define FILLP_EXT_PARA_FORMAT_BUF_LEN 512
23 #define FILLP_IP_ADDR_FORMAT_BUF_LEN 64
24
25 #define FILLP_COOKIE_TAG_AND_LEN_SIZE (sizeof(FILLP_UINT16) + sizeof(FILLP_UINT16))
26
27 #define FILLP_DIRECTION_STR(_d) (((_d) == FILLP_DIRECTION_RX) ? "recv" : "send")
28
29 #define FILLP_MGT_MSG_PRINT(_sockIndex, _hdr, _direction, fmt, ...) do { \
30 FILLP_LOGMGTMSG("sock %d %s management message, version: %u, msg type: %s(0x%X)," \
31 " msg flag: reserved(0x%02X), msg length: %u, seq num: %u, pkt seq: %u. " fmt, \
32 _sockIndex, FILLP_DIRECTION_STR(_direction), FILLP_PKT_GET_PROTCOL_VERSION((_hdr)->flag), \
33 g_fillpTypeStr[FILLP_PKT_GET_TYPE((_hdr)->flag)], FILLP_PKT_GET_TYPE((_hdr)->flag), \
34 FILLP_PKT_GET_FLAG((_hdr)->flag), (_hdr)->dataLen, (_hdr)->seqNum, (_hdr)->pktNum, ##__VA_ARGS__); \
35 } while (0)
36
37 static FILLP_CONST FILLP_CHAR *g_fillpTypeStr[] = {
38 "RESERVED",
39 "DATA",
40 "CONN_REQ",
41 "NACK",
42 "RESERVED",
43 "PACK",
44 "FIN",
45 "RESERVED",
46 "RESERVED",
47 "RESERVED",
48 "CONN_REQ_ACK",
49 "CONN_CONFIRM",
50 "CONN_CONFIRM_ACK",
51 "HISTORY_ACK",
52 "CTRL_MSG",
53 };
54
FillpHeaderNtoH(struct FillpPktHead * out,FILLP_CONST struct FillpPktHead * in,FILLP_INT direction)55 static void FillpHeaderNtoH(struct FillpPktHead *out, FILLP_CONST struct FillpPktHead *in, FILLP_INT direction)
56 {
57 if (direction == FILLP_DIRECTION_RX) { /* already convert to host byte order in FillpDoInput */
58 (void)memcpy_s(out, sizeof(struct FillpPktHead), in, sizeof(struct FillpPktHead));
59 } else {
60 out->flag = FILLP_NTOHS(in->flag);
61 out->dataLen = FILLP_NTOHS(in->dataLen);
62 out->pktNum = FILLP_NTOHL(in->pktNum);
63 out->seqNum = FILLP_NTOHL(in->seqNum);
64 }
65 }
66
FillpPktSimpleLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktHead * hdrInput,FILLP_INT direction)67 void FillpPktSimpleLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktHead *hdrInput, FILLP_INT direction)
68 {
69 struct FillpPktHead hdr = {0};
70 FillpHeaderNtoH(&hdr, hdrInput, direction);
71 FILLP_UINT16 type = FILLP_PKT_GET_TYPE(hdr.flag);
72 if (type == FILLP_PKT_TYPE_CONN_REQ || type == FILLP_PKT_TYPE_CONN_REQ_ACK ||
73 type == FILLP_PKT_TYPE_CONN_CONFIRM || type == FILLP_PKT_TYPE_CONN_CONFIRM_ACK ||
74 type == FILLP_PKT_TYPE_FIN) {
75 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, direction, "");
76 }
77 }
78
FillpConnReqLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktConnReq * req,FILLP_INT direction)79 void FillpConnReqLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktConnReq *req, FILLP_INT direction)
80 {
81 struct FillpPktHead hdr = {0};
82 FillpHeaderNtoH(&hdr, (FILLP_CONST struct FillpPktHead *)req->head, direction);
83 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, direction,
84 "cookie preserve time: %u, send cache: %u, recv cache: %u, timestamp: %llu",
85 FILLP_NTOHL(req->cookiePreserveTime), FILLP_NTOHL(req->sendCache), FILLP_NTOHL(req->recvCache),
86 FILLP_NTOHLL(req->timestamp));
87 }
88
FillpExtParaRttFormat(FILLP_CONST struct FtNetconn * conn,FILLP_CHAR * buf,size_t len)89 static FILLP_INT FillpExtParaRttFormat(FILLP_CONST struct FtNetconn *conn, FILLP_CHAR *buf, size_t len)
90 {
91 return snprintf_s(buf, len, len - 1, " init rtt: %llu", conn->calcRttDuringConnect);
92 }
93
FillpExtParaPktSizeFormat(FILLP_CONST struct FtNetconn * conn,FILLP_CHAR * buf,size_t len)94 static FILLP_INT FillpExtParaPktSizeFormat(FILLP_CONST struct FtNetconn *conn, FILLP_CHAR *buf, size_t len)
95 {
96 return snprintf_s(buf, len, len - 1, " peer pkt size: %u", conn->peerPktSize);
97 }
98
FillpBitmapFormat(FILLP_CHAR * buf,size_t len,FILLP_UINT32 bitmap,FILLP_CONST FILLP_CHAR * bitmapStr[],size_t bitmapStrSize)99 static FILLP_INT FillpBitmapFormat(FILLP_CHAR *buf, size_t len, FILLP_UINT32 bitmap,
100 FILLP_CONST FILLP_CHAR *bitmapStr[], size_t bitmapStrSize)
101 {
102 size_t formatLen = 0;
103 size_t i;
104 for (i = 0; i < bitmapStrSize; i++) {
105 if (!UTILS_FLAGS_CHECK(bitmap, 1u << i)) {
106 continue;
107 }
108
109 FILLP_INT ret = snprintf_s(buf + formatLen, len - formatLen, (len - formatLen) - 1, " %s", bitmapStr[i]);
110 if (ret < 0) {
111 FILLP_LOGERR("snprintf_s failed");
112 return ret;
113 }
114 formatLen += (FILLP_UINT32)ret;
115 }
116
117 return (FILLP_INT)formatLen;
118 }
119
FillpExtParaCharacterFormat(FILLP_CONST struct FtNetconn * conn,FILLP_CHAR * buf,size_t len)120 static FILLP_INT FillpExtParaCharacterFormat(FILLP_CONST struct FtNetconn *conn, FILLP_CHAR *buf, size_t len)
121 {
122 FILLP_INT ret = snprintf_s(buf, len, len - 1, " characters: 0x%08X", conn->peerCharacters);
123 if (ret < 0) {
124 return ret;
125 }
126 size_t formatLen = (FILLP_UINT32)ret;
127
128 FILLP_CONST FILLP_CHAR *characterStr[] = { "HRBB", "PKT_IVAR" };
129 ret = FillpBitmapFormat(buf + formatLen, len - formatLen, conn->peerCharacters,
130 characterStr, UTILS_ARRAY_LEN(characterStr));
131 if (ret < 0) {
132 return ret;
133 }
134 formatLen += (FILLP_UINT32)ret;
135
136 return (FILLP_INT)formatLen;
137 }
138
FillpExtParaFcAlgFormat(FILLP_CONST struct FtNetconn * conn,FILLP_CHAR * buf,size_t len)139 static FILLP_INT FillpExtParaFcAlgFormat(FILLP_CONST struct FtNetconn *conn, FILLP_CHAR *buf, size_t len)
140 {
141 FILLP_INT ret = snprintf_s(buf, len, len - 1, " FC ALG: 0x%02X", conn->peerFcAlgs);
142 if (ret < 0) {
143 return ret;
144 }
145 size_t formatLen = (FILLP_UINT32)ret;
146
147 FILLP_CONST FILLP_CHAR *fcAlgStr[] = { "ALG_1", "ALG_2", "ALG_3", "ALG_MSG" };
148 ret = FillpBitmapFormat(buf + formatLen, len - formatLen, conn->peerFcAlgs,
149 fcAlgStr, UTILS_ARRAY_LEN(fcAlgStr));
150 if (ret < 0) {
151 return ret;
152 }
153 formatLen += (FILLP_UINT32)ret;
154
155 return (FILLP_INT)formatLen;
156 }
157
158 static FILLP_INT (*g_extParaFormatter[FILLP_PKT_EXT_BUTT])(
159 FILLP_CONST struct FtNetconn *conn, FILLP_CHAR *buf, size_t len) = {
160 FILLP_NULL_PTR,
161 FillpExtParaRttFormat,
162 FillpExtParaPktSizeFormat,
163 FillpExtParaCharacterFormat,
164 FillpExtParaFcAlgFormat,
165 };
166
FillpExtParaFormat(FILLP_CONST FILLP_UCHAR * extPara,FILLP_INT extParaLen,FILLP_CHAR * buf,size_t len)167 static FILLP_INT FillpExtParaFormat(FILLP_CONST FILLP_UCHAR *extPara, FILLP_INT extParaLen, FILLP_CHAR *buf, size_t len)
168 {
169 struct FtNetconn conn;
170 (void)memset_s(&conn, sizeof(struct FtNetconn), 0, sizeof(struct FtNetconn));
171 if (FillpDecodeExtPara(extPara, extParaLen, &conn) != ERR_OK) {
172 return -1;
173 }
174
175 size_t formatLen = 0;
176 FILLP_INT i;
177 for (i = 0; i < FILLP_PKT_EXT_BUTT; i++) {
178 if (!conn.extParameterExisted[i] || g_extParaFormatter[i] == FILLP_NULL_PTR) {
179 continue;
180 }
181
182 FILLP_INT ret = g_extParaFormatter[i](&conn, buf + formatLen, len - formatLen);
183 if (ret < 0 || (FILLP_UINT32)ret > len - formatLen) {
184 return -1;
185 }
186
187 formatLen += ret;
188 }
189
190 return (FILLP_INT)formatLen;
191 }
192
FillpConnReqAckRxLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktHead * hdr,FILLP_CONST struct FillpConnReqAckClient * ack,FILLP_CONST FILLP_UCHAR * extPara,FILLP_INT extParaLen)193 void FillpConnReqAckRxLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktHead *hdr,
194 FILLP_CONST struct FillpConnReqAckClient *ack, FILLP_CONST FILLP_UCHAR *extPara, FILLP_INT extParaLen)
195 {
196 FILLP_CHAR tmpBuf[FILLP_EXT_PARA_FORMAT_BUF_LEN] = {0};
197 if (FillpExtParaFormat(extPara, extParaLen, tmpBuf, sizeof(tmpBuf)) < 0) {
198 return;
199 }
200
201 FILLP_MGT_MSG_PRINT(sockIndex, hdr, FILLP_DIRECTION_RX,
202 "tag cookie: %hu, cookie len: %hu, cookie content: ****, timestamp: %llu, external parameter: %s",
203 ack->tagCookie, ack->cookieLength, ack->timestamp, tmpBuf);
204 }
205
FillpConnReqAckTxLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktConnReqAck * ack,FILLP_CONST FILLP_UCHAR * extPara,FILLP_INT extParaLen)206 void FillpConnReqAckTxLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktConnReqAck *ack,
207 FILLP_CONST FILLP_UCHAR *extPara, FILLP_INT extParaLen)
208 {
209 FILLP_CHAR tmpBuf[FILLP_EXT_PARA_FORMAT_BUF_LEN] = {0};
210 if (FillpExtParaFormat(extPara, extParaLen, tmpBuf, sizeof(tmpBuf)) < 0) {
211 return;
212 }
213
214 struct FillpPktHead hdr = {0};
215 FillpHeaderNtoH(&hdr, (FILLP_CONST struct FillpPktHead *)ack, FILLP_DIRECTION_TX);
216 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, FILLP_DIRECTION_TX,
217 "tag cookie: %hu, cookie len: %hu, cookie content: ****, timestamp: %llu, external parameter: %s",
218 FILLP_NTOHS(ack->tagCookie), FILLP_NTOHS(ack->cookieLength), FILLP_NTOHLL(ack->timestamp), tmpBuf);
219 }
220
FillpConnConfirmRxLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktConnConfirm * confirm,FILLP_CONST FILLP_UCHAR * extPara,FILLP_INT extParaLen)221 void FillpConnConfirmRxLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktConnConfirm *confirm,
222 FILLP_CONST FILLP_UCHAR *extPara, FILLP_INT extParaLen)
223 {
224 FILLP_CHAR tmpBuf[FILLP_EXT_PARA_FORMAT_BUF_LEN] = {0};
225 if (FillpExtParaFormat(extPara, extParaLen, tmpBuf, sizeof(tmpBuf)) < 0) {
226 return;
227 }
228
229 FILLP_CHAR ipStr[FILLP_IP_ADDR_FORMAT_BUF_LEN] = {0};
230 FILLP_INT ret = IpAddrAnonymousFormat(ipStr, sizeof(ipStr),
231 (struct sockaddr *)&confirm->remoteAddr, sizeof(confirm->remoteAddr));
232 if (ret < 0) {
233 FILLP_LOGERR("ip addr format failed");
234 return;
235 }
236
237 FILLP_MGT_MSG_PRINT(sockIndex, (FILLP_CONST struct FillpPktHead *)confirm->head, FILLP_DIRECTION_RX,
238 "tag cookie: %hu, cookie len: %hu, cookie content: ****, "
239 "remote address: [family: %s, port: %hu, IP: %s], external parameter: %s",
240 FILLP_NTOHS(confirm->tagCookie), FILLP_NTOHS(confirm->cookieLength),
241 (confirm->remoteAddr.sin6_family == AF_INET) ? "ipv4" : "ipv6",
242 FILLP_NTOHS(confirm->remoteAddr.sin6_port), ipStr, tmpBuf);
243 }
244
FillpConnConfirmTxLog(FILLP_INT sockIndex,FILLP_CONST FILLP_UCHAR * data,FILLP_INT dataLen,FILLP_INT extParaOffset)245 void FillpConnConfirmTxLog(FILLP_INT sockIndex, FILLP_CONST FILLP_UCHAR *data, FILLP_INT dataLen,
246 FILLP_INT extParaOffset)
247 {
248 if (dataLen < extParaOffset || dataLen < (FILLP_INT)(FILLP_HLEN + FILLP_COOKIE_TAG_AND_LEN_SIZE)) {
249 return;
250 }
251
252 FILLP_UINT16 tagCookie = *(FILLP_UINT16 *)(data + FILLP_HLEN);
253 tagCookie = FILLP_NTOHS(tagCookie);
254 FILLP_UINT16 cookieLen = *(FILLP_UINT16 *)(data + FILLP_HLEN + sizeof(FILLP_UINT16));
255 cookieLen = FILLP_NTOHS(cookieLen);
256 if (dataLen < (FILLP_INT)(FILLP_HLEN + FILLP_COOKIE_TAG_AND_LEN_SIZE + cookieLen + sizeof(struct sockaddr_in6))) {
257 return;
258 }
259
260 struct sockaddr_in6 remoteAddr;
261 (void)memcpy_s(&remoteAddr, sizeof(struct sockaddr_in6),
262 data + FILLP_HLEN + FILLP_COOKIE_TAG_AND_LEN_SIZE + cookieLen, sizeof(struct sockaddr_in6));
263 FILLP_CHAR ipStr[FILLP_IP_ADDR_FORMAT_BUF_LEN] = {0};
264 FILLP_INT ret = IpAddrAnonymousFormat(ipStr, sizeof(ipStr), (struct sockaddr *)&remoteAddr, sizeof(remoteAddr));
265 if (ret < 0) {
266 FILLP_LOGERR("ip addr format failed");
267 return;
268 }
269
270 FILLP_CHAR tmpBuf[FILLP_EXT_PARA_FORMAT_BUF_LEN] = {0};
271 if (FillpExtParaFormat(data + extParaOffset, dataLen - extParaOffset, tmpBuf, sizeof(tmpBuf)) < 0) {
272 return;
273 }
274
275 struct FillpPktHead hdr = {0};
276 FillpHeaderNtoH(&hdr, (FILLP_CONST struct FillpPktHead *)data, FILLP_DIRECTION_TX);
277
278 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, FILLP_DIRECTION_TX,
279 "tag cookie: %hu, cookie len: %hu, cookie content: ****, "
280 "remote address: [family: %s, port: %hu, IP: %s], external parameter: %s",
281 tagCookie, cookieLen,
282 (remoteAddr.sin6_family == AF_INET) ? "ipv4" : "ipv6",
283 FILLP_NTOHS(remoteAddr.sin6_port), ipStr, tmpBuf);
284 }
285
FillpConnConfirmAckLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktConnConfirmAck * confirmAck,FILLP_INT direction)286 void FillpConnConfirmAckLog(FILLP_INT sockIndex,
287 FILLP_CONST struct FillpPktConnConfirmAck *confirmAck, FILLP_INT direction)
288 {
289 struct FillpPktHead hdr = {0};
290 FillpHeaderNtoH(&hdr, (FILLP_CONST struct FillpPktHead *)confirmAck->head, direction);
291
292 FILLP_CHAR ipStr[FILLP_IP_ADDR_FORMAT_BUF_LEN] = {0};
293 FILLP_INT ret = IpAddrAnonymousFormat(ipStr, sizeof(ipStr),
294 (struct sockaddr *)&confirmAck->remoteAddr, sizeof(confirmAck->remoteAddr));
295 if (ret < 0) {
296 FILLP_LOGERR("ip addr format failed");
297 return;
298 }
299
300 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, direction,
301 "send cache: %u, recv cache: %u, packet size: %u, remote address: [family: %s, port: %hu, IP: %s]",
302 FILLP_NTOHL(confirmAck->sendCache), FILLP_NTOHL(confirmAck->recvCache), FILLP_NTOHL(confirmAck->pktSize),
303 (confirmAck->remoteAddr.sin6_family == AF_INET) ? "ipv4" : "ipv6",
304 FILLP_NTOHS(confirmAck->remoteAddr.sin6_port), ipStr);
305 }
306
FillpConnFinLog(FILLP_INT sockIndex,FILLP_CONST struct FillpPktFin * fin,FILLP_INT direction)307 void FillpConnFinLog(FILLP_INT sockIndex, FILLP_CONST struct FillpPktFin *fin, FILLP_INT direction)
308 {
309 FILLP_CHAR tmpBuf[FILLP_EXT_PARA_FORMAT_BUF_LEN] = {0};
310 FILLP_CONST FILLP_CHAR *flagStr[] = { "WR", "RD", "ACK", "VERSION_MISMATCH" };
311 FILLP_UINT32 flags = FILLP_NTOHS(fin->flag);
312 if (FillpBitmapFormat(tmpBuf, sizeof(tmpBuf), flags, flagStr, UTILS_ARRAY_LEN(flagStr)) < 0) {
313 return;
314 }
315
316 struct FillpPktHead hdr = {0};
317 FillpHeaderNtoH(&hdr, (FILLP_CONST struct FillpPktHead *)fin->head, direction);
318
319 FILLP_MGT_MSG_PRINT(sockIndex, &hdr, direction, "flags: %s", tmpBuf);
320 }
321 #endif
322