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 NETSYSNATIVE_NET_DIAG_WRAPPER_H 17 #define NETSYSNATIVE_NET_DIAG_WRAPPER_H 18 19 #include <condition_variable> 20 #include <iostream> 21 #include <mutex> 22 #include <regex> 23 24 #include "i_net_diag_callback.h" 25 #include "netsys_net_diag_data.h" 26 #include "singleton.h" 27 28 namespace OHOS { 29 namespace nmd { 30 namespace { 31 using namespace NetsysNative; 32 } // namespace 33 class NetDiagWrapper : public std::enable_shared_from_this<NetDiagWrapper> { 34 public: 35 NetDiagWrapper(); 36 ~NetDiagWrapper(); GetInstance()37 static std::shared_ptr<NetDiagWrapper> &GetInstance() 38 { 39 static std::shared_ptr<NetDiagWrapper> instance = std::make_shared<NetDiagWrapper>(); 40 return instance; 41 } 42 43 int32_t PingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback); 44 int32_t GetRouteTable(std::list<NetDiagRouteTable> &routeTables); 45 int32_t GetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo); 46 int32_t GetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName); 47 int32_t UpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName, bool add); 48 int32_t SetInterfaceActiveState(const std::string &ifaceName, bool up); 49 50 private: 51 int32_t ExecuteCommandForResult(const std::string &command, std::string &result); 52 int32_t GeneratePingCommand(const NetDiagPingOption &pingOption, std::string &command); 53 bool IsBlankLine(const std::string &line); 54 void ExtractPingResult(const std::string &result, const sptr<INetDiagCallback> &callback); 55 void ExtractPingHeader(const std::smatch &match, NetDiagPingResult &pingResult); 56 void ExtractIcmpSeqInfo(const std::smatch &match, NetDiagPingResult &pingResult); 57 void ExtractPingStatistics(const std::smatch &match, NetDiagPingResult &pingResult); 58 void ExtractRouteTableInfo(const std::smatch &match, std::list<NetDiagRouteTable> &routeTables); 59 void ExtractNetProtoSocketsInfo(const std::smatch &match, NetDiagSocketsInfo &socketsInfo); 60 void ExtractUnixSocketsInfo(const std::smatch &match, NetDiagSocketsInfo &socketsInfo); 61 void ExtractIfaceName(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 62 void ExtractIfaceInet(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 63 void ExtractIfaceInet6(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 64 void ExtractIfaceMtu(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 65 void ExtractIfaceTxQueueLen(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 66 void ExtractIfaceTransDataBytes(const std::smatch &match, NetDiagIfaceConfig &ifaceInfo); 67 }; 68 } // namespace nmd 69 } // namespace OHOS 70 #endif // NETSYSNATIVE_NET_DIAG_WRAPPER_H 71