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 #include "net_connection_callback.h"
17 #include "net_connection_ffi.h"
18 #include "net_connection_impl.h"
19 #include "netmanager_base_log.h"
20
21 namespace OHOS::NetManagerStandard {
NetAvailable(sptr<NetHandle> & netHandle)22 int32_t ConnectionCallbackObserver::NetAvailable(sptr<NetHandle> &netHandle)
23 {
24 if (netHandle == nullptr) {
25 return 0;
26 }
27 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
28 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
29 if (netConnection == nullptr) {
30 NETMANAGER_BASE_LOGE("can not find netConnection handle");
31 return 0;
32 }
33 if (netConnection->netAvailible.size() == 0) {
34 NETMANAGER_BASE_LOGE("no NetAvailable func registered");
35 return 0;
36 }
37 int32_t id = netHandle->GetNetId();
38 int len = static_cast<int>(netConnection->netAvailible.size());
39 for (int i = 0; i < len; i++) {
40 netConnection->netAvailible[i](id);
41 }
42 return 0;
43 }
44
SetCapability(CNetCapabilities & capabilities,const std::set<NetBearType> & bearerTypes,const std::set<NetCap> & netCaps)45 bool SetCapability(CNetCapabilities &capabilities, const std::set<NetBearType> &bearerTypes,
46 const std::set<NetCap> &netCaps)
47 {
48 if (capabilities.bearedTypeSize > 0) {
49 capabilities.bearerTypes = static_cast<int32_t *>(malloc(sizeof(int32_t) * capabilities.bearedTypeSize));
50 if (capabilities.bearerTypes == nullptr) {
51 NETMANAGER_BASE_LOGE("NetCapabilitiesChange malloc bearerTypes failed");
52 return false;
53 }
54 int j = 0;
55 for (auto it = bearerTypes.begin(); it != bearerTypes.end(); ++it, ++j) {
56 capabilities.bearerTypes[j] = *it;
57 }
58 }
59
60 if (capabilities.networkCapSize > 0) {
61 capabilities.networkCap = static_cast<int32_t *>(malloc(sizeof(int32_t) * capabilities.networkCapSize));
62 if (capabilities.networkCap == nullptr) {
63 NETMANAGER_BASE_LOGE("NetCapabilitiesChange malloc networkCap failed");
64 free(capabilities.bearerTypes);
65 return false;
66 }
67 int j = 0;
68 for (auto it = netCaps.begin(); it != netCaps.end(); ++it, ++j) {
69 capabilities.networkCap[j] = *it;
70 }
71 }
72 return true;
73 }
74
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)75 int32_t ConnectionCallbackObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
76 const sptr<NetAllCapabilities> &netAllCap)
77 {
78 if (netHandle == nullptr) {
79 NETMANAGER_BASE_LOGE("NetCapabilitiesChange netHandle is null");
80 return 0;
81 }
82 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
83 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
84 if (netConnection == nullptr) {
85 NETMANAGER_BASE_LOGE("can not find netConnection handle");
86 return 0;
87 }
88 if (netConnection->netCapabilitiesChange.size() == 0) {
89 NETMANAGER_BASE_LOGE("no NetCapabilitiesChange func registered");
90 return 0;
91 }
92
93 int32_t id = netHandle->GetNetId();
94
95 if (netAllCap == nullptr) {
96 return 0;
97 }
98 int len = static_cast<int>(netConnection->netCapabilitiesChange.size());
99 for (int i = 0; i < len; i++) {
100 auto bearTypes = netAllCap->bearerTypes_;
101 auto netCaps = netAllCap->netCaps_;
102
103 CNetCapabilities capabilities = {.bearedTypeSize = bearTypes.size(),
104 .networkCapSize = netCaps.size(),
105 .linkUpBandwidthKbps = netAllCap->linkUpBandwidthKbps_,
106 .linkDownBandwidthKbps = netAllCap->linkDownBandwidthKbps_,
107 .bearerTypes = nullptr,
108 .networkCap = nullptr};
109 if (!SetCapability(capabilities, bearTypes, netCaps)) {
110 return 0;
111 }
112
113 CNetCapabilityInfo info = {.netHandle = id, .netCap = capabilities};
114 netConnection->netCapabilitiesChange[i](info);
115 }
116 return 0;
117 }
118
SetConnectionProp(CConnectionProperties & props,const sptr<NetLinkInfo> & info)119 void SetConnectionProp(CConnectionProperties &props, const sptr<NetLinkInfo> &info)
120 {
121 if (props.linkAddressSize > 0) {
122 props.linkAddresses = static_cast<CLinkAddress *>(malloc(sizeof(CLinkAddress) * props.linkAddressSize));
123 if (props.linkAddresses == nullptr) {
124 props.linkAddressSize = 0;
125 return;
126 }
127 int i = 0;
128 for (auto it = info->netAddrList_.begin(); it != info->netAddrList_.end(); ++it, ++i) {
129 CNetAddress netAddr{.address = MallocCString(it->address_), .family = it->family_, .port = it->port_};
130 props.linkAddresses[i] = CLinkAddress{.address = netAddr, .prefixLength = it->prefixlen_};
131 }
132 }
133
134 if (props.dnsSize > 0) {
135 props.dnses = static_cast<CNetAddress *>(malloc(sizeof(CNetAddress) * props.dnsSize));
136 if (props.dnses == nullptr) {
137 return;
138 }
139 int i = 0;
140 for (auto it = info->dnsList_.begin(); it != info->dnsList_.end(); ++it, ++i) {
141 props.dnses[i] =
142 CNetAddress{.address = MallocCString(it->address_), .family = it->family_, .port = it->port_};
143 }
144 }
145
146 if (props.routeSize > 0) {
147 props.routes = static_cast<CRouteInfo *>(malloc(sizeof(CRouteInfo) * props.routeSize));
148 if (props.routes == nullptr) {
149 return;
150 }
151 int i = 0;
152 for (auto it = info->routeList_.begin(); it != info->routeList_.end(); ++it, ++i) {
153 CNetAddress destAddr = {.address = MallocCString(it->destination_.address_),
154 .family = it->destination_.family_,
155 .port = it->destination_.port_};
156 CLinkAddress dest = {.address = destAddr, .prefixLength = it->destination_.prefixlen_};
157 CNetAddress gateway = {.address = MallocCString(it->gateway_.address_),
158 .family = it->gateway_.family_,
159 .port = it->gateway_.port_};
160 props.routes[i] = CRouteInfo{.interfaceName = MallocCString(it->iface_),
161 .destination = dest,
162 .gateway = gateway,
163 .hasGateway = it->hasGateway_,
164 .isDefaultRoute = it->isDefaultRoute_};
165 }
166 }
167 }
168
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)169 int32_t ConnectionCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
170 const sptr<NetLinkInfo> &info)
171 {
172 if (netHandle == nullptr) {
173 return 0;
174 }
175 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
176 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
177 if (netConnection == nullptr) {
178 NETMANAGER_BASE_LOGE("can not find netConnection handle");
179 return 0;
180 }
181 if (netConnection->netConnectionPropertiesChange.size() == 0) {
182 return 0;
183 }
184
185 int32_t id = netHandle->GetNetId();
186 int len = static_cast<int>(netConnection->netConnectionPropertiesChange.size());
187 for (int i = 0; i < len; i++) {
188 CConnectionProperties props = {.interfaceName = MallocCString(info->ifaceName_),
189 .domains = MallocCString(info->domain_),
190 .linkAddressSize = info->netAddrList_.size(),
191 .dnsSize = info->dnsList_.size(),
192 .routeSize = info->routeList_.size(),
193 .mtu = info->mtu_,
194 .linkAddresses = nullptr,
195 .dnses = nullptr,
196 .routes = nullptr};
197 SetConnectionProp(props, info);
198 netConnection->netConnectionPropertiesChange[i](id, props);
199 }
200 return 0;
201 }
202
NetLost(sptr<NetHandle> & netHandle)203 int32_t ConnectionCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
204 {
205 if (netHandle == nullptr) {
206 return 0;
207 }
208 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
209 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
210 if (netConnection == nullptr) {
211 NETMANAGER_BASE_LOGE("can not find netConnection handle");
212 return 0;
213 }
214 if (netConnection->netLost.size() == 0) {
215 NETMANAGER_BASE_LOGE("no NetLost func registered");
216 return 0;
217 }
218 int32_t id = netHandle->GetNetId();
219 int32_t len = static_cast<int32_t>(netConnection->netLost.size());
220 for (int32_t i = 0; i < len; i++) {
221 netConnection->netLost[i](id);
222 }
223 return 0;
224 }
225
NetUnavailable()226 int32_t ConnectionCallbackObserver::NetUnavailable()
227 {
228 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
229 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
230 if (netConnection == nullptr) {
231 NETMANAGER_BASE_LOGE("can not find netConnection handle");
232 return 0;
233 }
234 if (netConnection->netUnavailable.size() == 0) {
235 NETMANAGER_BASE_LOGE("no NetUnavailable func registered");
236 return 0;
237 }
238 int len = static_cast<int>(netConnection->netUnavailable.size());
239 for (int i = 0; i < len; i++) {
240 netConnection->netUnavailable[i]();
241 }
242 return 0;
243 }
244
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)245 int32_t ConnectionCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
246 {
247 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
248 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
249 if (netConnection == nullptr) {
250 NETMANAGER_BASE_LOGE("can not find netConnection handle");
251 return 0;
252 }
253 if (netConnection->netBlockStatusChange.size() == 0) {
254 NETMANAGER_BASE_LOGE("no NetBlockStatusChange func registered");
255 return 0;
256 }
257 int32_t id = netHandle->GetNetId();
258 int len = static_cast<int64_t>(netConnection->netBlockStatusChange.size());
259 for (int i = 0; i < len; i++) {
260 netConnection->netBlockStatusChange[i](id, blocked);
261 }
262 return 0;
263 }
264 } // namespace OHOS::NetManagerStandard
265