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 #include <arpa/inet.h>
17 #include "net_interface_callback.h"
18 #include "mdns_manager.h"
19 #include "netmgr_ext_log_wrapper.h"
20 
21 #include <algorithm>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <thread>
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 constexpr int WAITING_TIME_MS = 1000;
29 
NetInterfaceStateCallback()30 NetInterfaceStateCallback::NetInterfaceStateCallback() {}
31 
OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int32_t flags, int32_t scope)32 int32_t NetInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName,
33                                                              int32_t flags, int32_t scope)
34 {
35     NETMGR_EXT_LOG_I("OnInterfaceAddressUpdated, iface:[%{public}s], scope:[%{public}d]",
36                      ifName.c_str(), scope);
37     if (ifName.empty()) {
38         NETMGR_EXT_LOG_E("mdns_log Invalid interface name");
39         return NETMANAGER_SUCCESS;
40     }
41 
42     std::string ifrName = ifName;
43     std::transform(ifrName.begin(), ifrName.end(), ifrName.begin(), ::tolower);
44     if (ifrName.find("p2p") != std::string::npos) {
45         NETMGR_EXT_LOG_D("mdns_log Not p2p netcard handle");
46         return NETMANAGER_SUCCESS;
47     }
48 
49     size_t pos = addr.find("/");
50     if (pos == std::string::npos) {
51         pos = addr.length();
52     }
53     std::string tmpAddr = addr.substr(0, pos);
54     if (tmpAddr.empty()) {
55         NETMGR_EXT_LOG_E("mdns_log Invalid IP address");
56         return NETMANAGER_SUCCESS;
57     }
58     in6_addr ipAddr;
59     int32_t ret = inet_pton(AF_INET6, tmpAddr.c_str(), &ipAddr);
60     if (ret > 0 && !MDnsManager::GetInstance().IsSupportIpV6()) {
61         NETMGR_EXT_LOG_D("mdns_log Not support IpV6");
62         return NETMANAGER_SUCCESS;
63     }
64 
65     std::this_thread::sleep_for(std::chrono::milliseconds(WAITING_TIME_MS));
66     MDnsManager::GetInstance().RestartMDnsProtocolImpl();
67     return NETMANAGER_SUCCESS;
68 }
69 
OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int32_t flags, int32_t scope)70 int32_t NetInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName,
71                                                              int32_t flags, int32_t scope)
72 {
73     NETMGR_EXT_LOG_D("OnInterfaceAddressRemoved, iface:[%{public}s], scope:[%{public}d]",
74                      ifName.c_str(), scope);
75     return NETMANAGER_SUCCESS;
76 }
77 
OnInterfaceAdded(const std::string &ifName)78 int32_t NetInterfaceStateCallback::OnInterfaceAdded(const std::string &ifName)
79 {
80     NETMGR_EXT_LOG_D("OnInterfaceAdded, iface:[%{public}s]", ifName.c_str());
81     return NETMANAGER_SUCCESS;
82 }
83 
OnInterfaceRemoved(const std::string &ifName)84 int32_t NetInterfaceStateCallback::OnInterfaceRemoved(const std::string &ifName)
85 {
86     NETMGR_EXT_LOG_D("OnInterfaceRemoved, iface:[%{public}s]", ifName.c_str());
87     return NETMANAGER_SUCCESS;
88 }
89 
OnInterfaceChanged(const std::string &ifName, bool up)90 int32_t NetInterfaceStateCallback::OnInterfaceChanged(const std::string &ifName, bool up)
91 {
92     NETMGR_EXT_LOG_D("OnInterfaceChanged, iface:[%{public}s]->Up:[%{public}d]", ifName.c_str(), up);
93     return NETMANAGER_SUCCESS;
94 }
95 
OnInterfaceLinkStateChanged(const std::string &ifName, bool up)96 int32_t NetInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
97 {
98     NETMGR_EXT_LOG_D("OnInterfaceLinkStateChanged, iface:[%{public}s]->Up:[%{public}d]", ifName.c_str(), up);
99     return NETMANAGER_SUCCESS;
100 }
101 } // namespace NetManagerStandard
102 } // namespace OHOS
103