1 /*
2 * Copyright (c) 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 #include "wrapper_distributor.h"
17
18 #include "netmanager_base_common_utils.h"
19 #include "netnative_log_wrapper.h"
20
21 namespace OHOS {
22 namespace nmd {
23 using namespace NetManagerStandard::CommonUtils;
24 namespace {
25 }
WrapperDistributor(int32_t socket,const int32_t format)26 WrapperDistributor::WrapperDistributor(int32_t socket, const int32_t format)
27 {
28 NETNATIVE_LOG_D("WrapperDistributor::WrapperDistributor: Socket: %{public}d, Format: %{public}d", socket, format);
29 receiver_ = std::make_unique<DataReceiver>(socket, format);
30 receiver_->RegisterCallback(
31 [this](const std::shared_ptr<NetsysEventMessage> message) { HandleDecodeSuccess(message); });
32 }
33
Start()34 int32_t WrapperDistributor::Start()
35 {
36 return receiver_->Start();
37 }
38
Stop()39 int32_t WrapperDistributor::Stop()
40 {
41 return receiver_->Stop();
42 }
43
RegisterNetlinkCallbacks(std::shared_ptr<std::vector<sptr<NetsysNative::INotifyCallback>>> netlinkCallbacks)44 int32_t WrapperDistributor::RegisterNetlinkCallbacks(
45 std::shared_ptr<std::vector<sptr<NetsysNative::INotifyCallback>>> netlinkCallbacks)
46 {
47 if (netlinkCallbacks == nullptr) {
48 NETNATIVE_LOGE("netlinkCallbacks is nullptr");
49 return NetlinkResult::ERR_NULL_PTR;
50 }
51 netlinkCallbacks_ = netlinkCallbacks;
52 return NetlinkResult::OK;
53 }
54
HandleDecodeSuccess(const std::shared_ptr<NetsysEventMessage> & message)55 void WrapperDistributor::HandleDecodeSuccess(const std::shared_ptr<NetsysEventMessage> &message)
56 {
57 if (message == nullptr) {
58 NETNATIVE_LOGE("NetlinkProcessor: OnEvent: message is nullptr");
59 return;
60 }
61 if (netlinkCallbacks_ == nullptr) {
62 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
63 return;
64 }
65 HandleStateChanged(message);
66 }
67
HandleStateChanged(const std::shared_ptr<NetsysEventMessage> & message)68 void WrapperDistributor::HandleStateChanged(const std::shared_ptr<NetsysEventMessage> &message)
69 {
70 message->DumpMessage();
71 const NetsysEventMessage::SubSys subSys = message->GetSubSys();
72 switch (subSys) {
73 case NetsysEventMessage::SubSys::NET:
74 HandleSubSysNet(message);
75 break;
76 case NetsysEventMessage::SubSys::QLOG:
77 HandleSubSysQlog(message);
78 break;
79 default:
80 break;
81 }
82 }
83
HandleSubSysNet(const std::shared_ptr<NetsysEventMessage> & message)84 void WrapperDistributor::HandleSubSysNet(const std::shared_ptr<NetsysEventMessage> &message)
85 {
86 NetsysEventMessage::Action action = message->GetAction();
87 const std::string &iface = message->GetMessage(NetsysEventMessage::Type::INTERFACE);
88
89 switch (action) {
90 case NetsysEventMessage::Action::ADD:
91 NotifyInterfaceAdd(iface);
92 break;
93 case NetsysEventMessage::Action::REMOVE:
94 NotifyInterfaceRemove(iface);
95 break;
96 case NetsysEventMessage::Action::CHANGE:
97 NotifyInterfaceChange(iface, true);
98 break;
99 case NetsysEventMessage::Action::LINKUP:
100 NotifyInterfaceLinkStateChange(iface, true);
101 break;
102 case NetsysEventMessage::Action::LINKDOWN:
103 NotifyInterfaceLinkStateChange(iface, false);
104 break;
105 case NetsysEventMessage::Action::ADDRESSUPDATE:
106 case NetsysEventMessage::Action::ADDRESSREMOVED:
107 HandleAddressChange(message);
108 break;
109 case NetsysEventMessage::Action::ROUTEUPDATED:
110 case NetsysEventMessage::Action::ROUTEREMOVED:
111 HandleRouteChange(message);
112 break;
113 default:
114 break;
115 }
116 }
117
HandleAddressChange(const std::shared_ptr<NetsysEventMessage> & message)118 void WrapperDistributor::HandleAddressChange(const std::shared_ptr<NetsysEventMessage> &message)
119 {
120 NetsysEventMessage::Action action = message->GetAction();
121 const std::string &iface = message->GetMessage(NetsysEventMessage::Type::INTERFACE);
122 const std::string &address = message->GetMessage(NetsysEventMessage::Type::ADDRESS);
123 const std::string &flags = message->GetMessage(NetsysEventMessage::Type::FLAGS);
124 const std::string &scope = message->GetMessage(NetsysEventMessage::Type::SCOPE);
125 const bool addrUpdated = (action == NetsysEventMessage::Action::ADDRESSUPDATE);
126
127 if (!iface.empty() && iface[0] && !address.empty() && !flags.empty() && !scope.empty()) {
128 if (addrUpdated) {
129 NotifyInterfaceAddressUpdate(address, iface, ConvertToInt64(flags), ConvertToInt64(scope));
130 } else {
131 NotifyInterfaceAddressRemove(address, iface, ConvertToInt64(flags), ConvertToInt64(scope));
132 }
133 }
134 }
135
HandleRouteChange(const std::shared_ptr<NetsysEventMessage> & message)136 void WrapperDistributor::HandleRouteChange(const std::shared_ptr<NetsysEventMessage> &message)
137 {
138 NetsysEventMessage::Action action = message->GetAction();
139 const std::string &route = message->GetMessage(NetsysEventMessage::Type::ROUTE);
140 const std::string &gateway = message->GetMessage(NetsysEventMessage::Type::GATEWAY);
141 const std::string &iface = message->GetMessage(NetsysEventMessage::Type::INTERFACE);
142 if (!route.empty() && (!gateway.empty() || !iface.empty())) {
143 NotifyRouteChange((action == NetsysEventMessage::Action::ROUTEUPDATED), route, gateway, iface);
144 }
145 }
146
HandleSubSysQlog(const std::shared_ptr<NetsysEventMessage> & message)147 void WrapperDistributor::HandleSubSysQlog(const std::shared_ptr<NetsysEventMessage> &message)
148 {
149 const std::string &alertName = message->GetMessage(NetsysEventMessage::Type::ALERT_NAME);
150 const std::string &iface = message->GetMessage(NetsysEventMessage::Type::INTERFACE);
151 if ((!alertName.empty()) && (!iface.empty())) {
152 NotifyQuotaLimitReache(alertName, iface);
153 }
154 }
155
NotifyInterfaceAdd(const std::string & ifName)156 void WrapperDistributor::NotifyInterfaceAdd(const std::string &ifName)
157 {
158 NETNATIVE_LOG_D("interface added: %{public}s", ifName.c_str());
159 if (netlinkCallbacks_ == nullptr) {
160 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
161 return;
162 }
163 for (auto &callback : *netlinkCallbacks_) {
164 callback->OnInterfaceAdded(ifName);
165 }
166 }
167
NotifyInterfaceRemove(const std::string & ifName)168 void WrapperDistributor::NotifyInterfaceRemove(const std::string &ifName)
169 {
170 NETNATIVE_LOG_D("interface removed: %{public}s", ifName.c_str());
171 if (netlinkCallbacks_ == nullptr) {
172 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
173 return;
174 }
175 for (auto &callback : *netlinkCallbacks_) {
176 callback->OnInterfaceRemoved(ifName);
177 }
178 }
179
NotifyInterfaceChange(const std::string & ifName,bool up)180 void WrapperDistributor::NotifyInterfaceChange(const std::string &ifName, bool up)
181 {
182 NETNATIVE_LOG_D("interface Change: %{public}s, %{public}d", ifName.c_str(), up);
183 if (netlinkCallbacks_ == nullptr) {
184 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
185 return;
186 }
187 for (auto &callback : *netlinkCallbacks_) {
188 callback->OnInterfaceChanged(ifName, up);
189 }
190 }
191
NotifyInterfaceLinkStateChange(const std::string & ifName,bool up)192 void WrapperDistributor::NotifyInterfaceLinkStateChange(const std::string &ifName, bool up)
193 {
194 NETNATIVE_LOG_D("interface link state Change: %{public}s, %{public}d", ifName.c_str(), up);
195 if (netlinkCallbacks_ == nullptr) {
196 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
197 return;
198 }
199 for (auto &callback : *netlinkCallbacks_) {
200 callback->OnInterfaceLinkStateChanged(ifName, up);
201 }
202 }
203
NotifyQuotaLimitReache(const std::string & labelName,const std::string & ifName)204 void WrapperDistributor::NotifyQuotaLimitReache(const std::string &labelName, const std::string &ifName)
205 {
206 NETNATIVE_LOG_D("NotifyQuotaLimitReache: %{public}s, %{public}s", labelName.c_str(), ifName.c_str());
207 if (netlinkCallbacks_ == nullptr) {
208 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
209 return;
210 }
211 for (auto &callback : *netlinkCallbacks_) {
212 callback->OnBandwidthReachedLimit(labelName, ifName);
213 }
214 }
215
NotifyInterfaceAddressUpdate(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)216 void WrapperDistributor::NotifyInterfaceAddressUpdate(const std::string &addr, const std::string &ifName,
217 int32_t flags, int32_t scope)
218 {
219 NETNATIVE_LOG_D("OnInterfaceAddressUpdated: %{public}s, %{public}s, %{public}d, %{public}d",
220 ToAnonymousIp(addr).c_str(), ifName.c_str(), flags, scope);
221 if (netlinkCallbacks_ == nullptr) {
222 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
223 return;
224 }
225 for (auto &callback : *netlinkCallbacks_) {
226 callback->OnInterfaceAddressUpdated(addr, ifName, flags, scope);
227 }
228 }
229
NotifyInterfaceAddressRemove(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)230 void WrapperDistributor::NotifyInterfaceAddressRemove(const std::string &addr, const std::string &ifName,
231 int32_t flags, int32_t scope)
232 {
233 NETNATIVE_LOG_D("NotifyInterfaceAddressRemove: %{public}s, %{public}s, %{public}d, %{public}d",
234 ToAnonymousIp(addr).c_str(), ifName.c_str(), flags, scope);
235 if (netlinkCallbacks_ == nullptr) {
236 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
237 return;
238 }
239 for (auto &callback : *netlinkCallbacks_) {
240 callback->OnInterfaceAddressRemoved(addr, ifName, flags, scope);
241 }
242 }
243
NotifyRouteChange(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)244 void WrapperDistributor::NotifyRouteChange(bool updated, const std::string &route, const std::string &gateway,
245 const std::string &ifName)
246 {
247 NETNATIVE_LOG_D("NotifyRouteChange: %{public}s, %{public}s, %{public}s, %{public}s",
248 updated ? "updated" : "removed", ToAnonymousIp(route).c_str(), ToAnonymousIp(gateway).c_str(),
249 ifName.c_str());
250 if (netlinkCallbacks_ == nullptr) {
251 NETNATIVE_LOGE("netlinkCallbacks_ is nullptr");
252 return;
253 }
254 for (auto &callback : *netlinkCallbacks_) {
255 callback->OnRouteChanged(updated, route, gateway, ifName);
256 }
257 }
258 } // namespace nmd
259 } // namespace OHOS
260