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 #include "softbus_hidumper_trans.h"
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include "anonymizer.h"
21 #include "comm_log.h"
22 #include "securec.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_error_code.h"
25 #include "softbus_utils.h"
26 
27 #define MAX_ID_LEN (10)
28 #define MODULE_NAME_TRAN "trans"
29 #define SOFTBUS_TRANS_MODULE_HELP "List all the dump item of trans"
30 
31 const char* g_linkTypeList[DUMPER_LANE_LINK_TYPE_BUTT] = {
32     "BR",
33     "BLE",
34     "P2P",
35     "wlan",
36     "eth",
37 };
38 
39 const char* g_dataTypeList[BUSINESS_TYPE_BUTT] = {
40     "Message",
41     "Byte",
42     "File",
43     "Stream",
44     "NotCare",
45 };
46 
47 static LIST_HEAD(g_trans_var_list);
48 
SoftBusRegTransVarDump(const char * dumpVar,SoftBusVarDumpCb cb)49 int32_t SoftBusRegTransVarDump(const char *dumpVar, SoftBusVarDumpCb cb)
50 {
51     if (dumpVar == NULL || strlen(dumpVar) >= SOFTBUS_DUMP_VAR_NAME_LEN || cb == NULL) {
52         COMM_LOGE(COMM_DFX, "SoftBusRegTransVarDump invalid param");
53         return SOFTBUS_INVALID_PARAM;
54     }
55     return SoftBusAddDumpVarToList(dumpVar, cb, &g_trans_var_list);
56 }
57 
SoftBusTransDumpRegisterSession(int fd,const char * pkgName,const char * sessionName,int uid,int pid)58 void SoftBusTransDumpRegisterSession(int fd, const char* pkgName, const char* sessionName,
59     int uid, int pid)
60 {
61     if (fd < 0 || pkgName == NULL || sessionName == NULL || uid < 0 || pid < 0) {
62         COMM_LOGE(COMM_DFX, "param is invalid");
63         return;
64     }
65 
66     char uidArr[MAX_ID_LEN] = {0};
67     char pidArr[MAX_ID_LEN] = {0};
68     char uidStr[MAX_ID_LEN] = {0};
69     char pidStr[MAX_ID_LEN] = {0};
70     if (sprintf_s(uidArr, MAX_ID_LEN, "%d", uid) < 0 || sprintf_s(pidArr, MAX_ID_LEN, "%d", pid) < 0) {
71         COMM_LOGE(COMM_DFX, "set uidArr or pidArr failed");
72         return;
73     }
74 
75     DataMasking(uidArr, MAX_ID_LEN, ID_DELIMITER, uidStr);
76     DataMasking(pidArr, MAX_ID_LEN, ID_DELIMITER, pidStr);
77 
78     char *tmpPkgName = NULL;
79     char *tmpName = NULL;
80     Anonymize(sessionName, &tmpName);
81     Anonymize(pkgName, &tmpPkgName);
82     SOFTBUS_DPRINTF(fd, "SessionName           : %s\n", AnonymizeWrapper(tmpName));
83     SOFTBUS_DPRINTF(fd, "PkgName               : %s\n", AnonymizeWrapper(tmpPkgName));
84     SOFTBUS_DPRINTF(fd, "PID                   : %s\n", uidStr);
85     SOFTBUS_DPRINTF(fd, "UID                   : %s\n", pidStr);
86     AnonymizeFree(tmpName);
87     AnonymizeFree(tmpPkgName);
88 }
89 
SoftBusTransDumpRunningSession(int fd,TransDumpLaneLinkType type,AppInfo * appInfo)90 void SoftBusTransDumpRunningSession(int fd, TransDumpLaneLinkType type, AppInfo* appInfo)
91 {
92     if (fd < 0 || type < DUMPER_LANE_BR || type >= DUMPER_LANE_LINK_TYPE_BUTT || appInfo == NULL) {
93         COMM_LOGE(COMM_DFX, "param is invalid");
94         return;
95     }
96 
97     char deviceId[DEVICE_ID_SIZE_MAX] = {0};
98     char srcAddr[IP_LEN] = {0};
99     char dstAddr[IP_LEN] = {0};
100     char *localSessionName = NULL;
101     char *remoteSessionName = NULL;
102     DataMasking(appInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX, ID_DELIMITER, deviceId);
103     DataMasking(appInfo->myData.addr, IP_LEN, IP_DELIMITER, srcAddr);
104     DataMasking(appInfo->peerData.addr, IP_LEN, IP_DELIMITER, dstAddr);
105     Anonymize(appInfo->myData.sessionName, &localSessionName);
106     Anonymize(appInfo->peerData.sessionName, &remoteSessionName);
107     SOFTBUS_DPRINTF(fd, "LocalSessionName      : %s\n", AnonymizeWrapper(localSessionName));
108     SOFTBUS_DPRINTF(fd, "RemoteSessionName     : %s\n", AnonymizeWrapper(remoteSessionName));
109     SOFTBUS_DPRINTF(fd, "PeerDeviceId          : %s\n", deviceId);
110     SOFTBUS_DPRINTF(fd, "LinkType              : %s\n", g_linkTypeList[type]);
111     SOFTBUS_DPRINTF(fd, "SourceAddress         : %s\n", srcAddr);
112     SOFTBUS_DPRINTF(fd, "DestAddress           : %s\n", dstAddr);
113     SOFTBUS_DPRINTF(fd, "DataType              : %s\n", g_dataTypeList[appInfo->businessType]);
114     AnonymizeFree(localSessionName);
115     AnonymizeFree(remoteSessionName);
116 }
117 
SoftBusTransDumpHandler(int fd,int argc,const char ** argv)118 static int SoftBusTransDumpHandler(int fd, int argc, const char **argv)
119 {
120     if (fd < 0 || argv == NULL || argc < 0) {
121         COMM_LOGE(COMM_DFX, "param is invalid ");
122         return SOFTBUS_INVALID_PARAM;
123     }
124     if (argc == 0 || ((argc == 1) && (strcmp(argv[0], "-h") == 0)) || (argc == 1 && strcmp(argv[0], "-l") == 0)) {
125         SoftBusDumpSubModuleHelp(fd, (char *)MODULE_NAME_TRAN, &g_trans_var_list);
126         return SOFTBUS_OK;
127     }
128 
129     int32_t ret = SOFTBUS_OK;
130     int32_t isModuleExist = SOFTBUS_DUMP_NOT_EXIST;
131     if (argc > 1 && strcmp(argv[0], "-l") == 0) {
132         ListNode *item = NULL;
133         LIST_FOR_EACH(item, &g_trans_var_list) {
134             SoftBusDumpVarNode *itemNode = LIST_ENTRY(item, SoftBusDumpVarNode, node);
135             if (strcmp(itemNode->varName, argv[1]) == 0 && itemNode->dumpCallback != NULL) {
136                 ret = itemNode->dumpCallback(fd);
137                 isModuleExist = SOFTBUS_DUMP_EXIST;
138                 break;
139             }
140         }
141         if (isModuleExist == SOFTBUS_DUMP_NOT_EXIST) {
142             SoftBusDumpErrInfo(fd, argv[1]);
143             SoftBusDumpSubModuleHelp(fd, (char *)MODULE_NAME_TRAN, &g_trans_var_list);
144         }
145     }
146     return ret;
147 }
148 
SoftBusTransDumpHandlerInit(void)149 int32_t SoftBusTransDumpHandlerInit(void)
150 {
151     int32_t ret = SoftBusRegHiDumperHandler((char*)MODULE_NAME_TRAN, (char*)SOFTBUS_TRANS_MODULE_HELP,
152         &SoftBusTransDumpHandler);
153     if (ret != SOFTBUS_OK) {
154         COMM_LOGE(COMM_INIT, "SoftBusTransDumpHander regist fail");
155     }
156     return ret;
157 }
158 
SoftBusHiDumperTransDeInit(void)159 void SoftBusHiDumperTransDeInit(void)
160 {
161     SoftBusReleaseDumpVar(&g_trans_var_list);
162 }
163