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 "net_interface_callback.h"
17 #include "mdns_manager.h"
18 #include "netmgr_ext_log_wrapper.h"
19 
20 #include <algorithm>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <thread>
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 constexpr int WAITING_TIME_MS = 1000;
28 
NetInterfaceStateCallback()29 NetInterfaceStateCallback::NetInterfaceStateCallback() {}
30 
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)31 int32_t NetInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName,
32                                                              int32_t flags, int32_t scope)
33 {
34     NETMGR_EXT_LOG_I("OnInterfaceAddressUpdated, iface:[%{public}s], scope:[%{public}d]",
35                      ifName.c_str(), scope);
36 
37     std::string ifrName = ifName;
38     std::transform(ifrName.begin(), ifrName.end(), ifrName.begin(), ::tolower);
39     if (ifrName.find("p2p") != std::string::npos) {
40         NETMGR_EXT_LOG_D("mdns_log Not p2p netcard handle");
41         return NETMANAGER_SUCCESS;
42     }
43 
44     std::this_thread::sleep_for(std::chrono::milliseconds(WAITING_TIME_MS));
45     MDnsManager::GetInstance().RestartMDnsProtocolImpl();
46     return NETMANAGER_SUCCESS;
47 }
48 
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)49 int32_t NetInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName,
50                                                              int32_t flags, int32_t scope)
51 {
52     NETMGR_EXT_LOG_D("OnInterfaceAddressRemoved, iface:[%{public}s], scope:[%{public}d]",
53                      ifName.c_str(), scope);
54     return NETMANAGER_SUCCESS;
55 }
56 
OnInterfaceAdded(const std::string & ifName)57 int32_t NetInterfaceStateCallback::OnInterfaceAdded(const std::string &ifName)
58 {
59     NETMGR_EXT_LOG_D("OnInterfaceAdded, iface:[%{public}s]", ifName.c_str());
60     return NETMANAGER_SUCCESS;
61 }
62 
OnInterfaceRemoved(const std::string & ifName)63 int32_t NetInterfaceStateCallback::OnInterfaceRemoved(const std::string &ifName)
64 {
65     NETMGR_EXT_LOG_D("OnInterfaceRemoved, iface:[%{public}s]", ifName.c_str());
66     return NETMANAGER_SUCCESS;
67 }
68 
OnInterfaceChanged(const std::string & ifName,bool up)69 int32_t NetInterfaceStateCallback::OnInterfaceChanged(const std::string &ifName, bool up)
70 {
71     NETMGR_EXT_LOG_D("OnInterfaceChanged, iface:[%{public}s]->Up:[%{public}d]", ifName.c_str(), up);
72     return NETMANAGER_SUCCESS;
73 }
74 
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)75 int32_t NetInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
76 {
77     NETMGR_EXT_LOG_D("OnInterfaceLinkStateChanged, iface:[%{public}s]->Up:[%{public}d]", ifName.c_str(), up);
78     return NETMANAGER_SUCCESS;
79 }
80 } // namespace NetManagerStandard
81 } // namespace OHOS
82