1 /*
2 * Copyright (c) 2025 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 <sstream>
16 #include "i_netstack_chr_client.h"
17 #include "netstack_chr_report.h"
18 #include "netstack_log.h"
19 #include "common_event_manager.h"
20
21 using namespace OHOS::NetStack::ChrClient;
22
23 static constexpr const char* REPORT_HTTP_EVENT_NAME = "custom.event.CHR_REPORT_HTTP";
24 static constexpr const std::int32_t CHR_UID = 1201;
25 static constexpr const int REPORT_TIME_LIMIT_MINUTE = 5;
26
27 static constexpr const int REPORT_CHR_RESULT_SUCCESS = 0;
28 static constexpr const int REPORT_CHR_RESULT_TIME_LIMIT_ERROR = 1;
29 static constexpr const int REPORT_CHR_RESULT_REPORT_FAIL = 2;
30
NetStackChrReport()31 NetStackChrReport::NetStackChrReport()
32 {}
33
~NetStackChrReport()34 NetStackChrReport::~NetStackChrReport()
35 {}
36
ReportCommonEvent(DataTransChrStats chrStats)37 int NetStackChrReport::ReportCommonEvent(DataTransChrStats chrStats)
38 {
39 std::lock_guard<std::mutex> lock(report_mutex_);
40 auto currentTime = std::chrono::system_clock::now();
41 auto timeDifference = std::chrono::duration_cast<std::chrono::minutes>(currentTime - lastReceivedTime_);
42 if (timeDifference.count() < REPORT_TIME_LIMIT_MINUTE) {
43 ignoreReportTimes_ += 1;
44 return REPORT_CHR_RESULT_TIME_LIMIT_ERROR;
45 }
46 AAFwk::Want want;
47 want.SetAction(REPORT_HTTP_EVENT_NAME);
48 SetWantParam(want, chrStats);
49
50 EventFwk::CommonEventData commonEventData;
51 commonEventData.SetWant(want);
52 EventFwk::CommonEventPublishInfo publishInfo;
53 publishInfo.SetSubscriberUid({CHR_UID});
54 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, publishInfo)) {
55 NETSTACK_LOGE("Subscriber is nullptr, report to CHR failed.");
56 return REPORT_CHR_RESULT_REPORT_FAIL;
57 }
58 NETSTACK_LOGI("Report to CHR success, %{public}d reports are ignore before this.", ignoreReportTimes_);
59 lastReceivedTime_ = currentTime;
60 ignoreReportTimes_ = 0;
61
62 return REPORT_CHR_RESULT_SUCCESS;
63 }
64
SetWantParam(AAFwk::Want & want,DataTransChrStats chrStats)65 void NetStackChrReport::SetWantParam(AAFwk::Want& want, DataTransChrStats chrStats)
66 {
67 std::string httpInfoJsonStr;
68 std::string tcpInfoJsonStr;
69 SetHttpInfoJsonStr(chrStats.httpInfo, httpInfoJsonStr);
70 SetTcpInfoJsonStr(chrStats.tcpInfo, tcpInfoJsonStr);
71
72 want.SetParam("PROCESS_NAME", chrStats.processName);
73 want.SetParam("DATA_TRANS_HTTP_INFO", httpInfoJsonStr);
74 want.SetParam("DATA_TRANS_TCP_INFO", tcpInfoJsonStr);
75 }
76
SetHttpInfoJsonStr(DataTransHttpInfo httpInfo,std::string & httpInfoJsonStr)77 void NetStackChrReport::SetHttpInfoJsonStr(DataTransHttpInfo httpInfo, std::string& httpInfoJsonStr)
78 {
79 std::stringstream ss;
80 ss << "{\"uid\":" << httpInfo.uid
81 << ",{\"response_code\":" << httpInfo.responseCode
82 << ",{\"total_time\":" << httpInfo.totalTime
83 << ",{\"namelookup_time\":" << httpInfo.nameLookUpTime
84 << ",{\"connect_time\":" << httpInfo.connectTime
85 << ",{\"pretransfer_time\":" << httpInfo.preTransferTime
86 << ",{\"size_upload\":" << httpInfo.sizeUpload
87 << ",{\"size_download\":" << httpInfo.sizeDownload
88 << ",{\"speed_download\":" << httpInfo.speedDownload
89 << ",{\"speed_upload\":" << httpInfo.speedUpload
90 << ",{\"effective_method\":\"" << httpInfo.effectiveMethod
91 << "\",{\"starttransfer_time\":" << httpInfo.startTransferTime
92 << ",{\"content_type\":\"" << httpInfo.contentType
93 << "\",{\"redirect_time\":" << httpInfo.redirectTime
94 << ",{\"redirect_count\":" << httpInfo.redirectCount
95 << ",{\"os_errno\":" << httpInfo.osError
96 << ",{\"ssl_verifyresult\":" << httpInfo.sslVerifyResult
97 << ",{\"appconnect_time\":" << httpInfo.appconnectTime
98 << ",{\"retry_after\":" << httpInfo.uid
99 << ",{\"proxy_error\":" << httpInfo.proxyError
100 << ",{\"queue_time\":" << httpInfo.queueTime
101 << ",{\"curl_code\":"<< httpInfo.curlCode
102 << ",{\"request_start_time\":" << httpInfo.requestStartTime << "}";
103 httpInfoJsonStr = ss.str();
104 }
105
SetTcpInfoJsonStr(DataTransTcpInfo tcpInfo,std::string & tcpInfoJsonStr)106 void NetStackChrReport::SetTcpInfoJsonStr(DataTransTcpInfo tcpInfo, std::string& tcpInfoJsonStr)
107 {
108 std::stringstream ss;
109 ss << "{\"tcpi_unacked\":" << tcpInfo.unacked
110 << ",{\"tcpi_last_data_sent\":" << tcpInfo.lastDataSent
111 << ",{\"tcpi_last_ack_sent\":" << tcpInfo.lastAckSent
112 << ",{\"tcpi_last_data_recv\":" << tcpInfo.lastDataRecv
113 << ",{\"tcpi_last_ack_recv\":" << tcpInfo.lastAckRecv
114 << ",{\"tcpi_rtt\":" << tcpInfo.rtt
115 << ",{\"tcpi_rttvar\":" << tcpInfo.rttvar
116 << ",{\"tcpi_retransmits\":" << tcpInfo.retransmits
117 << ",{\"tcpi_total_retrans\":" << tcpInfo.totalRetrans
118 << ",{\"src_ip\":\"" << tcpInfo.srcIp
119 << "\",{\"dst_ip\":\"" << tcpInfo.dstIp
120 << "\",{\"src_port\":" << tcpInfo.srcPort
121 << ",{\"dst_port\":" << tcpInfo.dstPort << "}";
122 tcpInfoJsonStr = ss.str();
123 }