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