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