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 || netAllCap == nullptr) {
79 NETMANAGER_BASE_LOGE("NetCapabilitiesChange param is nullptr");
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 int len = static_cast<int>(netConnection->netCapabilitiesChange.size());
96 for (int i = 0; i < len; i++) {
97 auto bearTypes = netAllCap->bearerTypes_;
98 auto netCaps = netAllCap->netCaps_;
99
100 CNetCapabilities capabilities = {.bearedTypeSize = bearTypes.size(),
101 .networkCapSize = netCaps.size(),
102 .linkUpBandwidthKbps = netAllCap->linkUpBandwidthKbps_,
103 .linkDownBandwidthKbps = netAllCap->linkDownBandwidthKbps_,
104 .bearerTypes = nullptr,
105 .networkCap = nullptr};
106 if (!SetCapability(capabilities, bearTypes, netCaps)) {
107 return 0;
108 }
109
110 CNetCapabilityInfo info = {.netHandle = id, .netCap = capabilities};
111 netConnection->netCapabilitiesChange[i](info);
112 }
113 return 0;
114 }
115
SetConnectionProp(CConnectionProperties &props, const sptr<NetLinkInfo> &info)116 void SetConnectionProp(CConnectionProperties &props, const sptr<NetLinkInfo> &info)
117 {
118 if (props.linkAddressSize > 0) {
119 props.linkAddresses = static_cast<CLinkAddress *>(malloc(sizeof(CLinkAddress) * props.linkAddressSize));
120 if (props.linkAddresses == nullptr) {
121 props.linkAddressSize = 0;
122 return;
123 }
124 int i = 0;
125 for (auto it = info->netAddrList_.begin(); it != info->netAddrList_.end(); ++it, ++i) {
126 CNetAddress netAddr{.address = MallocCString(it->address_), .family = it->family_, .port = it->port_};
127 props.linkAddresses[i] = CLinkAddress{.address = netAddr, .prefixLength = it->prefixlen_};
128 }
129 }
130
131 if (props.dnsSize > 0) {
132 props.dnses = static_cast<CNetAddress *>(malloc(sizeof(CNetAddress) * props.dnsSize));
133 if (props.dnses == nullptr) {
134 return;
135 }
136 int i = 0;
137 for (auto it = info->dnsList_.begin(); it != info->dnsList_.end(); ++it, ++i) {
138 props.dnses[i] =
139 CNetAddress{.address = MallocCString(it->address_), .family = it->family_, .port = it->port_};
140 }
141 }
142
143 if (props.routeSize > 0) {
144 props.routes = static_cast<CRouteInfo *>(malloc(sizeof(CRouteInfo) * props.routeSize));
145 if (props.routes == nullptr) {
146 return;
147 }
148 int i = 0;
149 for (auto it = info->routeList_.begin(); it != info->routeList_.end(); ++it, ++i) {
150 CNetAddress destAddr = {.address = MallocCString(it->destination_.address_),
151 .family = it->destination_.family_,
152 .port = it->destination_.port_};
153 CLinkAddress dest = {.address = destAddr, .prefixLength = it->destination_.prefixlen_};
154 CNetAddress gateway = {.address = MallocCString(it->gateway_.address_),
155 .family = it->gateway_.family_,
156 .port = it->gateway_.port_};
157 props.routes[i] = CRouteInfo{.interfaceName = MallocCString(it->iface_),
158 .destination = dest,
159 .gateway = gateway,
160 .hasGateway = it->hasGateway_,
161 .isDefaultRoute = it->isDefaultRoute_};
162 }
163 }
164 }
165
NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)166 int32_t ConnectionCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
167 const sptr<NetLinkInfo> &info)
168 {
169 if (netHandle == nullptr || info == nullptr) {
170 NETMANAGER_BASE_LOGE("NetConnectionPropertiesChange param is nullptr");
171 return 0;
172 }
173 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
174 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
175 if (netConnection == nullptr) {
176 NETMANAGER_BASE_LOGE("can not find netConnection handle");
177 return 0;
178 }
179 if (netConnection->netConnectionPropertiesChange.size() == 0) {
180 return 0;
181 }
182
183 int32_t id = netHandle->GetNetId();
184 int len = static_cast<int>(netConnection->netConnectionPropertiesChange.size());
185 for (int i = 0; i < len; i++) {
186 CConnectionProperties props = {.interfaceName = MallocCString(info->ifaceName_),
187 .domains = MallocCString(info->domain_),
188 .linkAddressSize = info->netAddrList_.size(),
189 .dnsSize = info->dnsList_.size(),
190 .routeSize = info->routeList_.size(),
191 .mtu = info->mtu_,
192 .linkAddresses = nullptr,
193 .dnses = nullptr,
194 .routes = nullptr};
195 SetConnectionProp(props, info);
196 netConnection->netConnectionPropertiesChange[i](id, props);
197 }
198 return 0;
199 }
200
NetLost(sptr<NetHandle> &netHandle)201 int32_t ConnectionCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
202 {
203 if (netHandle == nullptr) {
204 return 0;
205 }
206 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
207 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
208 if (netConnection == nullptr) {
209 NETMANAGER_BASE_LOGE("can not find netConnection handle");
210 return 0;
211 }
212 if (netConnection->netLost.size() == 0) {
213 NETMANAGER_BASE_LOGE("no NetLost func registered");
214 return 0;
215 }
216 int32_t id = netHandle->GetNetId();
217 int32_t len = static_cast<int32_t>(netConnection->netLost.size());
218 for (int32_t i = 0; i < len; i++) {
219 netConnection->netLost[i](id);
220 }
221 return 0;
222 }
223
NetUnavailable()224 int32_t ConnectionCallbackObserver::NetUnavailable()
225 {
226 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
227 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
228 if (netConnection == nullptr) {
229 NETMANAGER_BASE_LOGE("can not find netConnection handle");
230 return 0;
231 }
232 if (netConnection->netUnavailable.size() == 0) {
233 NETMANAGER_BASE_LOGE("no NetUnavailable func registered");
234 return 0;
235 }
236 int len = static_cast<int>(netConnection->netUnavailable.size());
237 for (int i = 0; i < len; i++) {
238 netConnection->netUnavailable[i]();
239 }
240 return 0;
241 }
242
NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)243 int32_t ConnectionCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
244 {
245 std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
246 NetConnectionImpl *netConnection = NET_CONNECTIONS[this];
247 if (netConnection == nullptr) {
248 NETMANAGER_BASE_LOGE("can not find netConnection handle");
249 return 0;
250 }
251 if (netConnection->netBlockStatusChange.size() == 0) {
252 NETMANAGER_BASE_LOGE("no NetBlockStatusChange func registered");
253 return 0;
254 }
255 int32_t id = netHandle->GetNetId();
256 int len = static_cast<int64_t>(netConnection->netBlockStatusChange.size());
257 for (int i = 0; i < len; i++) {
258 netConnection->netBlockStatusChange[i](id, blocked);
259 }
260 return 0;
261 }
262 } // namespace OHOS::NetManagerStandard
263