1 /*
2  * Copyright (c) 2024 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 WIFI_DIRECT_IP_MANAGER_H
17 #define WIFI_DIRECT_IP_MANAGER_H
18 
19 #include <bitset>
20 #include <map>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "conn_log.h"
27 #include "data/ipv4_info.h"
28 #include "wifi_direct_initiator.h"
29 
30 namespace OHOS::SoftBus {
31 class WifiDirectIpManager {
32 public:
GetInstance()33     static WifiDirectIpManager& GetInstance()
34     {
35         static WifiDirectIpManager instance;
36         {
37             std::lock_guard lock(clearMutex_);
38             if (!hasClear_) {
39                 ClearAllIpv4();
40                 hasClear_ = true;
41             }
42         }
43         return instance;
44     }
45     static void Init();
46 
47     std::string ApplyIpv6(const std::string &mac);
48     int32_t ApplyIpv4(const std::vector<Ipv4Info> &localArray, const std::vector<Ipv4Info> &remoteArray,
49                       Ipv4Info &source, Ipv4Info &sink);
50     int32_t ConfigIpv6(const std::string &interface, const std::string &ip);
51     int32_t ConfigIpv4(
52         const std::string &interface, const Ipv4Info &local, const Ipv4Info &remote, const std::string &remoteMac);
53     void ReleaseIpv4(
54         const std::string &interface, const Ipv4Info &local, const Ipv4Info &remote, const std::string &remoteMac);
55     static void ClearAllIpv4();
56 
Lock()57     void Lock()
58     {
59         CONN_LOGD(CONN_WIFI_DIRECT, "lock");
60         mutex_.lock();
61     }
Unlock()62     void Unlock()
63     {
64         CONN_LOGD(CONN_WIFI_DIRECT, "unlock");
65         mutex_.unlock();
66     }
67 
68     static constexpr int32_t EUI_64_IDENTIFIER_LEN = 64;
69     static constexpr int32_t LOCAL_NETWORK_ID = 99;
70 
71     static std::string ApplySubNet(const std::vector<Ipv4Info> &localArray, const std::vector<Ipv4Info> &remoteArray);
72 
73     static int32_t GetNetworkGateWay(const std::string &ipString, std::string &gateWay);
74     static int32_t GetNetworkDestination(const std::string &ipString, std::string &destination);
75 
76     static int32_t AddInterfaceAddress(const std::string &interface, const std::string &ipString, int32_t prefixLength);
77     static int32_t DeleteInterfaceAddress(
78         const std::string &interface, const std::string &ipString, int32_t prefixLength);
79     static int32_t AddStaticArp(
80         const std::string &interface, const std::string &ipString, const std::string &macString);
81     static int32_t DeleteStaticArp(
82         const std::string &interface, const std::string &ipString, const std::string &macString);
83 
84 private:
85     class Initiator {
86     public:
Initiator()87         Initiator()
88         {
89             WifiDirectInitiator::GetInstance().Add(WifiDirectIpManager::Init);
90         }
91     };
92 
93     std::recursive_mutex mutex_;
94     static inline Initiator initiator_;
95     static inline bool hasClear_ = false;
96     static inline std::recursive_mutex clearMutex_;
97 };
98 } // namespace OHOS::SoftBus
99 #endif /* WIFI_DIRECT_IP_MANAGER_H */
100