• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 NET_HTTP_PROBE_H
17 #define NET_HTTP_PROBE_H
18 
19 #include <curl/curl.h>
20 #include <curl/easy.h>
21 #include <map>
22 #include <mutex>
23 #include <string>
24 
25 #include "net_http_probe_result.h"
26 #include "net_link_info.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 enum ProbeType : uint32_t { PROBE_HTTP = 1, PROBE_HTTPS = 2, PROBE_HTTP_HTTPS = 3 };
31 
32 class NetHttpProbe {
33 public:
34     NetHttpProbe(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo);
35     ~NetHttpProbe();
36 
37     int32_t SendProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl);
38     NetHttpProbeResult GetHttpProbeResult() const;
39     NetHttpProbeResult GetHttpsProbeResult() const;
40     void UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo);
41     void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
42     bool HasProbeType(ProbeType inputProbeType, ProbeType hasProbeType);
43 
44 private:
45     static bool CurlGlobalInit();
46     static void CurlGlobalCleanup();
47 
48     bool CheckCurlGlobalInitState();
49     void CleanHttpCurl();
50     void ClearProbeResult();
51     std::string ExtractDomainFormUrl(const std::string &url);
52     std::string GetAddrInfo(const std::string &domain);
53     bool InitHttpCurl(ProbeType probeType);
54     bool SetCurlOptions(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl);
55     bool SetHttpOptions(ProbeType probeType, CURL *curl, const std::string &url);
56     bool SetProxyOption(ProbeType probeType, bool &useProxy);
57     bool SetResolveOption(ProbeType probeType, const std::string &domain, const std::string &ipAddress, int32_t port);
58     bool SendDnsProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl,
59                       const bool useProxy);
60     void SendHttpProbeRequest();
61     void RecvHttpProbeResponse();
62     int32_t LoadProxy(std::string &proxyHost, int32_t &proxyPort);
63 
64 private:
65     static std::mutex initCurlMutex_;
66     static int32_t useCurlCount_;
67 
68     std::mutex proxyMtx_;
69     bool isCurlInit_ = false;
70     uint32_t netId_ = 0;
71     NetBearType netBearType_ = BEARER_DEFAULT;
72     NetLinkInfo netLinkInfo_;
73     HttpProxy globalHttpProxy_;
74     CURLM *curlMulti_ = nullptr;
75     CURL *httpCurl_ = nullptr;
76     CURL *httpsCurl_ = nullptr;
77     curl_slist *httpResolveList_ = nullptr;
78     curl_slist *httpsResolveList_ = nullptr;
79     NetHttpProbeResult httpProbeResult_;
80     NetHttpProbeResult httpsProbeResult_;
81 };
82 } // namespace NetManagerStandard
83 } // namespace OHOS
84 #endif // NET_HTTP_PROBE_H