1 /* 2 * Copyright (C) 2021-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 #ifndef NET_MONITOR_H 17 #define NET_MONITOR_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <memory> 22 #include <mutex> 23 24 #include "refbase.h" 25 26 #include "i_net_monitor_callback.h" 27 #include "net_conn_types.h" 28 29 namespace OHOS { 30 namespace NetManagerStandard { 31 class NetMonitor : public virtual RefBase, public std::enable_shared_from_this<NetMonitor> { 32 public: 33 /** 34 * Construct a new NetMonitor to detection a network 35 * 36 * @param netId Detection network's id 37 * @param callback Network monitor callback weak reference 38 */ 39 NetMonitor(uint32_t netId, const std::weak_ptr<INetMonitorCallback> &callback); 40 41 /** 42 * Destroy the NetMonitor 43 * 44 */ 45 virtual ~NetMonitor() = default; 46 47 /** 48 * Start detection 49 * 50 */ 51 void Start(); 52 53 /** 54 * Stop detecting 55 * 56 */ 57 void Stop(); 58 59 /** 60 * Set network socket parameter 61 * 62 * @return Socket parameter setting result 63 */ 64 int32_t SetSocketParameter(int32_t sockFd); 65 66 /** 67 * Is network monitor detecting 68 * 69 * @return Status value of whether the network is detecting 70 */ 71 bool IsDetecting(); 72 73 /** 74 * Network monitor detection 75 * 76 */ 77 void Detection(); 78 79 private: 80 NetDetectionStatus SendParallelHttpProbes(); 81 NetDetectionStatus SendHttpProbe(const std::string &defaultDomain, const std::string &defaultUrl, 82 const uint16_t defaultPort); 83 int32_t GetStatusCodeFromResponse(const std::string &strResponse); 84 int32_t GetUrlRedirectFromResponse(const std::string &strResponse, std::string &urlRedirect); 85 NetDetectionStatus dealRecvResult(const std::string &strResponse); 86 int32_t GetDefaultNetDetectionUrlFromCfg(std::string &strUrl); 87 int32_t ParseUrl(const std::string &url, std::string &domain, std::string &urlPath); 88 int32_t GetIpAddr(const std::string &domain, std::string &ip_addr, int &socketType); 89 90 /** 91 * Non blocking socket connection 92 * 93 * @param sockFd Socket fd to connect 94 * @param port Transport layer default port 95 * @param ipAddr IP address 96 * 97 * @return Socket connect result 98 */ 99 int32_t Connect(int32_t sockFd, int socketType, const uint16_t port, const std::string &ipAddr); 100 101 /** 102 * Connect ipv4 ip address socket 103 * 104 * @param sockFd Socket fd to connect 105 * @param port Transport layer default port 106 * @param ipAddr IP address 107 * 108 * @return Socket connect result 109 */ 110 int32_t ConnectIpv4(int32_t sockFd, const uint16_t port, const std::string &ipAddr); 111 112 /** 113 * Connect ipv6 ip address socket 114 * 115 * @param sockFd Socket fd to connect 116 * @param port Transport layer default port 117 * @param ipAddr IP address 118 * 119 * @return Socket connect result 120 */ 121 int32_t ConnectIpv6(int32_t sockFd, const uint16_t port, const std::string &ipAddr); 122 123 /** 124 * Ready for socket reading or writing 125 * 126 * @param sockFd Socket fd to connect 127 * @param canRead Store readable status 128 * @param canWrite Store writable status 129 * 130 * @return Socket preparation result 131 */ 132 int32_t Wait(int32_t sockFd, uint8_t *canRead, uint8_t *canWrite); 133 134 /** 135 * Send data to socket 136 * 137 * @param sockFd Socket fd to send 138 * @param domain Probe link 139 * @param url Probe url 140 * 141 * @return Result of send data to socket 142 */ 143 int32_t Send(int32_t sockFd, const std::string &domain, const std::string &url); 144 145 /** 146 * Receive data from socket 147 * 148 * @param sockFd Socket fd to receive 149 * @param probResult Store received data 150 * 151 * @return Result of receive data from socket 152 */ 153 int32_t Receive(int32_t sockFd, std::string &probResult); 154 155 private: 156 uint32_t netId_ = 0; 157 std::atomic<bool> isDetecting_ = false; 158 int32_t detectionSteps_ = 0; 159 std::mutex detectionMtx_; 160 std::condition_variable detectionCond_; 161 uint32_t detectionDelay_ = 0; 162 std::string portalUrlRedirect_; 163 std::weak_ptr<INetMonitorCallback> netMonitorCallback_; 164 }; 165 } // namespace NetManagerStandard 166 } // namespace OHOS 167 #endif // NET_MONITOR_H 168