1 /*
2 * Copyright (c) 2022-2023 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 "connection_module.h"
17
18 #include "bindsocket_context.h"
19 #include "connection_async_work.h"
20 #include "connection_exec.h"
21 #include "constant.h"
22 #include "getaddressbyname_context.h"
23 #include "getappnet_context.h"
24 #include "getdefaultnet_context.h"
25 #include "gethttpproxy_context.h"
26 #include "napi_constant.h"
27 #include "net_all_capabilities.h"
28 #include "netconnection.h"
29 #include "netinterface.h"
30 #include "netmanager_base_log.h"
31 #include "none_params_context.h"
32 #include "module_template.h"
33 #include "parse_nethandle_context.h"
34 #include "register_context.h"
35 #include "interfaceregister_context.h"
36 #include "setappnet_context.h"
37 #include "setglobalhttpproxy_context.h"
38 #include "setcustomdnsrule_context.h"
39 #include "deletecustomdnsrule_context.h"
40 #include "deletecustomdnsrules_context.h"
41 #include "getinterfaceconfig_context.h"
42 #include "registernetsupplier_context.h"
43 #include "unregisternetsupplier_context.h"
44
45 static constexpr const char *CONNECTION_MODULE_NAME = "net.connection";
46 static thread_local uint64_t g_moduleId;
47
48 #define DECLARE_NET_CAP(cap) \
49 DECLARE_NAPI_STATIC_PROPERTY(#cap, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetCap::cap)))
50
51 #define DECLARE_NET_BEAR_TYPE(type) \
52 DECLARE_NAPI_STATIC_PROPERTY(#type, NapiUtils::CreateUint32(env, static_cast<uint32_t>(NetBearType::type)))
53
54 namespace OHOS::NetManagerStandard {
55
ParseTypesArray(napi_env env,napi_value obj,std::set<T> & typeArray)56 template <typename T> static bool ParseTypesArray(napi_env env, napi_value obj, std::set<T> &typeArray)
57 {
58 if (!NapiUtils::IsArray(env, obj)) {
59 return false;
60 }
61 uint32_t arrayLenght =
62 NapiUtils::GetArrayLength(env, obj) > MAX_ARRAY_LENGTH ? MAX_ARRAY_LENGTH : NapiUtils::GetArrayLength(env, obj);
63 for (uint32_t i = 0; i < arrayLenght; ++i) {
64 napi_value val = NapiUtils::GetArrayElement(env, obj, i);
65 if (NapiUtils::GetValueType(env, val) == napi_number) {
66 typeArray.insert(static_cast<T>(NapiUtils::GetUint32FromValue(env, val)));
67 } else {
68 NETMANAGER_BASE_LOGE("Invalid parameter type of array element!");
69 return false;
70 }
71 }
72 return true;
73 }
74
ParseCapabilities(napi_env env,napi_value obj,NetAllCapabilities & capabilities)75 static bool ParseCapabilities(napi_env env, napi_value obj, NetAllCapabilities &capabilities)
76 {
77 if (NapiUtils::GetValueType(env, obj) != napi_object) {
78 return false;
79 }
80
81 capabilities.linkUpBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_UP_BAND_WIDTH_KPS);
82 capabilities.linkDownBandwidthKbps_ = NapiUtils::GetUint32Property(env, obj, KEY_LINK_DOWN_BAND_WIDTH_KPS);
83
84 napi_value networkCap = NapiUtils::GetNamedProperty(env, obj, KEY_NETWORK_CAP);
85 (void)ParseTypesArray<NetCap>(env, networkCap, capabilities.netCaps_);
86
87 napi_value bearerTypes = NapiUtils::GetNamedProperty(env, obj, KEY_BEARER_TYPE);
88 if (!ParseTypesArray<NetBearType>(env, bearerTypes, capabilities.bearerTypes_)) {
89 return false;
90 }
91
92 return true;
93 }
94
ParseNetSpecifier(napi_env env,napi_value obj,NetSpecifier & specifier)95 static bool ParseNetSpecifier(napi_env env, napi_value obj, NetSpecifier &specifier)
96 {
97 napi_value capabilitiesObj = NapiUtils::GetNamedProperty(env, obj, KEY_NET_CAPABILITIES);
98 if (!ParseCapabilities(env, capabilitiesObj, specifier.netCapabilities_)) {
99 return false;
100 }
101 specifier.ident_ = NapiUtils::GetStringPropertyUtf8(env, obj, KEY_BEARER_PRIVATE_IDENTIFIER);
102 return true;
103 }
104
GetNetConnectionType(napi_env env,size_t argc,napi_value * argv)105 static NetConnectionType GetNetConnectionType(napi_env env, size_t argc, napi_value *argv)
106 {
107 if (argc == ARG_NUM_0) {
108 return NetConnectionType::PARAMETER_ZERO;
109 }
110 if (argc == ARG_NUM_1) {
111 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined) {
112 return NetConnectionType::PARAMETER_ZERO;
113 }
114 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object) {
115 return NetConnectionType::PARAMETER_SPECIFIER;
116 }
117 return NetConnectionType::PARAMETER_ERROR;
118 }
119 if (argc == ARG_NUM_2) {
120 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
121 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_number) {
122 return NetConnectionType::PARAMETER_TIMEOUT;
123 }
124 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_undefined &&
125 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
126 return NetConnectionType::PARAMETER_ZERO;
127 }
128 if (NapiUtils::GetValueType(env, argv[ARG_INDEX_0]) == napi_object &&
129 NapiUtils::GetValueType(env, argv[ARG_INDEX_1]) == napi_undefined) {
130 return NetConnectionType::PARAMETER_SPECIFIER;
131 }
132 }
133 return NetConnectionType::PARAMETER_ERROR;
134 }
135
ParseNetConnectionParams(napi_env env,size_t argc,napi_value * argv,EventManager * manager)136 static void *ParseNetConnectionParams(napi_env env, size_t argc, napi_value *argv, EventManager *manager)
137 {
138 std::unique_ptr<NetConnection, decltype(&NetConnection::DeleteNetConnection)> netConnection(
139 NetConnection::MakeNetConnection(manager), NetConnection::DeleteNetConnection);
140 netConnection->moduleId_ = g_moduleId;
141
142 auto netConnType = GetNetConnectionType(env, argc, argv);
143
144 switch (netConnType) {
145 case NetConnectionType::PARAMETER_ZERO: {
146 NETMANAGER_BASE_LOGI("ParseNetConnectionParams no params");
147 return netConnection.release();
148 }
149 case NetConnectionType::PARAMETER_SPECIFIER: {
150 if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
151 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed");
152 return nullptr;
153 }
154 netConnection->hasNetSpecifier_ = true;
155 return netConnection.release();
156 }
157 case NetConnectionType::PARAMETER_TIMEOUT: {
158 if (!ParseNetSpecifier(env, argv[ARG_INDEX_0], netConnection->netSpecifier_)) {
159 NETMANAGER_BASE_LOGE("ParseNetSpecifier failed, do not use params");
160 return nullptr;
161 }
162 netConnection->hasNetSpecifier_ = true;
163 netConnection->hasTimeout_ = true;
164 netConnection->timeout_ = NapiUtils::GetUint32FromValue(env, argv[ARG_INDEX_1]);
165 return netConnection.release();
166 }
167 default:
168 NETMANAGER_BASE_LOGE("constructor params invalid, should be none or specifier or specifier+timeout_");
169 return nullptr;
170 }
171 }
172
ParseNetInterfaceParams(napi_env env,size_t argc,napi_value * argv,EventManager * manager)173 static void *ParseNetInterfaceParams(napi_env env, size_t argc, napi_value *argv, EventManager *manager)
174 {
175 std::unique_ptr<NetInterface, decltype(&NetInterface::DeleteNetInterface)> netInterface(
176 NetInterface::MakeNetInterface(manager), NetInterface::DeleteNetInterface);
177 netInterface->moduleId_ = g_moduleId;
178
179 if (argc == ARG_NUM_0) {
180 return netInterface.release();
181 }
182 NETMANAGER_BASE_LOGE("constructor params invalid, should be none");
183 return nullptr;
184 }
185
AddCleanupHook(napi_env env)186 static void AddCleanupHook(napi_env env)
187 {
188 NapiUtils::SetEnvValid(env);
189 auto envWrapper = new (std::nothrow) napi_env;
190 if (envWrapper == nullptr) {
191 NETMANAGER_BASE_LOGE("EnvWrapper create fail!");
192 return;
193 }
194 *envWrapper = env;
195 napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper);
196 }
197
InitConnectionModule(napi_env env,napi_value exports)198 napi_value ConnectionModule::InitConnectionModule(napi_env env, napi_value exports)
199 {
200 g_moduleId = NapiUtils::CreateUvHandlerQueue(env);
201 std::initializer_list<napi_property_descriptor> functions = {
202 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET, GetDefaultNet),
203 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_NET_SYNC, GetDefaultNetSync),
204 DECLARE_NAPI_FUNCTION(FUNCTION_CREATE_NET_CONNECTION, CreateNetConnection),
205 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ADDRESSES_BY_NAME, GetAddressesByName),
206 DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET, HasDefaultNet),
207 DECLARE_NAPI_FUNCTION(FUNCTION_HAS_DEFAULT_NET_SYNC, HasDefaultNetSync),
208 DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED, IsDefaultNetMetered),
209 DECLARE_NAPI_FUNCTION(FUNCTION_IS_DEFAULT_NET_METERED_SYNC, IsDefaultNetMeteredSync),
210 DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES, GetNetCapabilities),
211 DECLARE_NAPI_FUNCTION(FUNCTION_GET_NET_CAPABILITIES_SYNC, GetNetCapabilitiesSync),
212 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES, GetConnectionProperties),
213 DECLARE_NAPI_FUNCTION(FUNCTION_GET_CONNECTION_PROPERTIES_SYNC, GetConnectionPropertiesSync),
214 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS, GetAllNets),
215 DECLARE_NAPI_FUNCTION(FUNCTION_GET_ALL_NETS_SYNC, GetAllNetsSync),
216 DECLARE_NAPI_FUNCTION(FUNCTION_ENABLE_AIRPLANE_MODE, EnableAirplaneMode),
217 DECLARE_NAPI_FUNCTION(FUNCTION_DISABLE_AIRPLANE_MODE, DisableAirplaneMode),
218 DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_CONNECTED, ReportNetConnected),
219 DECLARE_NAPI_FUNCTION(FUNCTION_REPORT_NET_DISCONNECTED, ReportNetDisconnected),
220 DECLARE_NAPI_FUNCTION(FUNCTION_GET_DEFAULT_HTTP_PROXY, GetDefaultHttpProxy),
221 DECLARE_NAPI_FUNCTION(FUNCTION_GET_GLOBAL_HTTP_PROXY, GetGlobalHttpProxy),
222 DECLARE_NAPI_FUNCTION(FUNCTION_SET_GLOBAL_HTTP_PROXY, SetGlobalHttpProxy),
223 DECLARE_NAPI_FUNCTION(FUNCTION_SET_CUSTOM_DNS_RULE, AddCustomDnsRule),
224 DECLARE_NAPI_FUNCTION(FUNCTION_DELETE_CUSTOM_DNS_RULE, RemoveCustomDnsRule),
225 DECLARE_NAPI_FUNCTION(FUNCTION_DELETE_CUSTOM_DNS_RULES, ClearCustomDnsRules),
226 DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_HTTP_PROXY, SetAppHttpProxy),
227 DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET, GetAppNet),
228 DECLARE_NAPI_FUNCTION(FUNCTION_GET_APP_NET_SYNC, GetAppNetSync),
229 DECLARE_NAPI_FUNCTION(FUNCTION_SET_APP_NET, SetAppNet),
230 DECLARE_NAPI_FUNCTION(FUNCTION_FACTORY_RESET_NETWORK, FactoryResetNetwork),
231 DECLARE_NAPI_FUNCTION(FUNCTION_FACTORY_RESET_NETWORK_SYNC, FactoryResetNetworkSync),
232 DECLARE_NAPI_FUNCTION(FUNCTION_SET_PAC_URL, SetPacUrl),
233 DECLARE_NAPI_FUNCTION(FUNCTION_GET_PAC_URL, GetPacUrl),
234 DECLARE_NAPI_FUNCTION(FUNCTION_SET_INTERFACE_UP, SetInterfaceUp),
235 DECLARE_NAPI_FUNCTION(FUNCTION_SET_INTERFACE_IP_ADDRESS, SetNetInterfaceIpAddress),
236 DECLARE_NAPI_FUNCTION(FUNCTION_ADD_NETWORK_ROUTE, AddNetworkRoute),
237 DECLARE_NAPI_FUNCTION(FUNCTION_CREATE_NET_INTERFACE, CreateNetInterface),
238 DECLARE_NAPI_FUNCTION(FUNCTION_GET_INTERFACE_CONFIG, GetNetInterfaceConfiguration),
239 DECLARE_NAPI_FUNCTION(FUNCTION_REGISTER_NET_SUPPLIER, RegisterNetSupplier),
240 DECLARE_NAPI_FUNCTION(FUNCTION_UNREGISTER_NET_SUPPLIER, UnregisterNetSupplier),
241 };
242 NapiUtils::DefineProperties(env, exports, functions);
243 InitClasses(env, exports);
244 InitProperties(env, exports);
245 AddCleanupHook(env);
246 return exports;
247 }
248
InitClasses(napi_env env,napi_value exports)249 void ConnectionModule::InitClasses(napi_env env, napi_value exports)
250 {
251 std::initializer_list<napi_property_descriptor> netConnectionFunctions = {
252 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_ON, NetConnectionInterface::On),
253 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_REGISTER, NetConnectionInterface::Register),
254 DECLARE_NAPI_FUNCTION(NetConnectionInterface::FUNCTION_UNREGISTER, NetConnectionInterface::Unregister),
255 };
256 ModuleTemplate::DefineClass(env, exports, netConnectionFunctions, INTERFACE_NET_CONNECTION);
257
258 std::initializer_list<napi_property_descriptor> netInterfaceFunctions = {
259 DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_ON, NetInterfaceInterface::On),
260 DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_REGISTER, NetInterfaceInterface::Register),
261 DECLARE_NAPI_FUNCTION(NetInterfaceInterface::FUNCTION_UNREGISTER, NetInterfaceInterface::Unregister),
262 };
263 ModuleTemplate::DefineClass(env, exports, netInterfaceFunctions, INTERFACE_NET_INTERFACE);
264 }
265
InitProperties(napi_env env,napi_value exports)266 void ConnectionModule::InitProperties(napi_env env, napi_value exports)
267 {
268 std::initializer_list<napi_property_descriptor> netCaps = {
269 DECLARE_NET_CAP(NET_CAPABILITY_MMS),
270 DECLARE_NET_CAP(NET_CAPABILITY_NOT_METERED),
271 DECLARE_NET_CAP(NET_CAPABILITY_INTERNET),
272 DECLARE_NET_CAP(NET_CAPABILITY_NOT_VPN),
273 DECLARE_NET_CAP(NET_CAPABILITY_VALIDATED),
274 DECLARE_NET_CAP(NET_CAPABILITY_PORTAL),
275 DECLARE_NET_CAP(NET_CAPABILITY_INTERNAL_DEFAULT),
276 DECLARE_NET_CAP(NET_CAPABILITY_CHECKING_CONNECTIVITY),
277 };
278 napi_value caps = NapiUtils::CreateObject(env);
279 NapiUtils::DefineProperties(env, caps, netCaps);
280 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_CAP, caps);
281
282 std::initializer_list<napi_property_descriptor> netBearTypes = {
283 DECLARE_NET_BEAR_TYPE(BEARER_CELLULAR), DECLARE_NET_BEAR_TYPE(BEARER_WIFI),
284 DECLARE_NET_BEAR_TYPE(BEARER_BLUETOOTH), DECLARE_NET_BEAR_TYPE(BEARER_ETHERNET),
285 DECLARE_NET_BEAR_TYPE(BEARER_VPN), DECLARE_NET_BEAR_TYPE(BEARER_WIFI_AWARE),
286 DECLARE_NET_BEAR_TYPE(BEARER_DEFAULT),
287 };
288 napi_value types = NapiUtils::CreateObject(env);
289 NapiUtils::DefineProperties(env, types, netBearTypes);
290 NapiUtils::SetNamedProperty(env, exports, INTERFACE_NET_BEAR_TYPE, types);
291 }
292
GetAddressesByName(napi_env env,napi_callback_info info)293 napi_value ConnectionModule::GetAddressesByName(napi_env env, napi_callback_info info)
294 {
295 return ModuleTemplate::Interface<GetAddressByNameContext>(env, info, FUNCTION_GET_ADDRESSES_BY_NAME, nullptr,
296 ConnectionAsyncWork::ExecGetAddressesByName,
297 ConnectionAsyncWork::GetAddressesByNameCallback);
298 }
299
HasDefaultNet(napi_env env,napi_callback_info info)300 napi_value ConnectionModule::HasDefaultNet(napi_env env, napi_callback_info info)
301 {
302 return ModuleTemplate::Interface<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
303 ConnectionAsyncWork::ExecHasDefaultNet,
304 ConnectionAsyncWork::HasDefaultNetCallback);
305 }
306
HasDefaultNetSync(napi_env env,napi_callback_info info)307 napi_value ConnectionModule::HasDefaultNetSync(napi_env env, napi_callback_info info)
308 {
309 return ModuleTemplate::InterfaceSync<HasDefaultNetContext>(env, info, FUNCTION_HAS_DEFAULT_NET, nullptr,
310 ConnectionExec::ExecHasDefaultNet,
311 ConnectionExec::HasDefaultNetCallback);
312 }
313
IsDefaultNetMetered(napi_env env,napi_callback_info info)314 napi_value ConnectionModule::IsDefaultNetMetered(napi_env env, napi_callback_info info)
315 {
316 return ModuleTemplate::Interface<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED, nullptr,
317 ConnectionAsyncWork::ExecIsDefaultNetMetered,
318 ConnectionAsyncWork::IsDefaultNetMeteredCallback);
319 }
320
IsDefaultNetMeteredSync(napi_env env,napi_callback_info info)321 napi_value ConnectionModule::IsDefaultNetMeteredSync(napi_env env, napi_callback_info info)
322 {
323 return ModuleTemplate::InterfaceSync<IsDefaultNetMeteredContext>(env, info, FUNCTION_IS_DEFAULT_NET_METERED,
324 nullptr, ConnectionExec::ExecIsDefaultNetMetered, ConnectionExec::IsDefaultNetMeteredCallback);
325 }
326
GetNetCapabilities(napi_env env,napi_callback_info info)327 napi_value ConnectionModule::GetNetCapabilities(napi_env env, napi_callback_info info)
328 {
329 return ModuleTemplate::Interface<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
330 ConnectionAsyncWork::ExecGetNetCapabilities,
331 ConnectionAsyncWork::GetNetCapabilitiesCallback);
332 }
333
GetNetCapabilitiesSync(napi_env env,napi_callback_info info)334 napi_value ConnectionModule::GetNetCapabilitiesSync(napi_env env, napi_callback_info info)
335 {
336 return ModuleTemplate::InterfaceSync<GetNetCapabilitiesContext>(env, info, FUNCTION_GET_NET_CAPABILITIES, nullptr,
337 ConnectionExec::ExecGetNetCapabilities,
338 ConnectionExec::GetNetCapabilitiesCallback);
339 }
340
GetConnectionProperties(napi_env env,napi_callback_info info)341 napi_value ConnectionModule::GetConnectionProperties(napi_env env, napi_callback_info info)
342 {
343 return ModuleTemplate::Interface<GetConnectionPropertiesContext>(
344 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionAsyncWork::ExecGetConnectionProperties,
345 ConnectionAsyncWork::GetConnectionPropertiesCallback);
346 }
347
GetConnectionPropertiesSync(napi_env env,napi_callback_info info)348 napi_value ConnectionModule::GetConnectionPropertiesSync(napi_env env, napi_callback_info info)
349 {
350 return ModuleTemplate::InterfaceSync<GetConnectionPropertiesContext>(
351 env, info, FUNCTION_GET_CONNECTION_PROPERTIES, nullptr, ConnectionExec::ExecGetConnectionProperties,
352 ConnectionExec::GetConnectionPropertiesCallback);
353 }
354
CreateNetConnection(napi_env env,napi_callback_info info)355 napi_value ConnectionModule::CreateNetConnection(napi_env env, napi_callback_info info)
356 {
357 return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_CONNECTION, ParseNetConnectionParams,
358 [](napi_env, void *data, void *) {
359 NETMANAGER_BASE_LOGI("finalize netConnection");
360 auto manager = static_cast<EventManager *>(data);
361 auto netConnection = static_cast<NetConnection *>(manager->GetData());
362 delete manager;
363 NetConnection::DeleteNetConnection(netConnection);
364 });
365 }
366
CreateNetInterface(napi_env env,napi_callback_info info)367 napi_value ConnectionModule::CreateNetInterface(napi_env env, napi_callback_info info)
368 {
369 return ModuleTemplate::NewInstance(env, info, INTERFACE_NET_INTERFACE, ParseNetInterfaceParams,
370 [](napi_env, void *data, void *) {
371 NETMANAGER_BASE_LOGI("finalize netInterface");
372 auto manager = static_cast<EventManager *>(data);
373 auto netInterface = static_cast<NetInterface *>(manager->GetData());
374 delete manager;
375 NetInterface::DeleteNetInterface(netInterface);
376 });
377 }
378
GetDefaultNet(napi_env env,napi_callback_info info)379 napi_value ConnectionModule::GetDefaultNet(napi_env env, napi_callback_info info)
380 {
381 return ModuleTemplate::Interface<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
382 ConnectionAsyncWork::ExecGetDefaultNet,
383 ConnectionAsyncWork::GetDefaultNetCallback);
384 }
385
GetDefaultNetSync(napi_env env,napi_callback_info info)386 napi_value ConnectionModule::GetDefaultNetSync(napi_env env, napi_callback_info info)
387 {
388 return ModuleTemplate::InterfaceSync<GetDefaultNetContext>(env, info, FUNCTION_GET_DEFAULT_NET, nullptr,
389 ConnectionExec::ExecGetDefaultNet,
390 ConnectionExec::GetDefaultNetCallback);
391 }
392
GetAllNets(napi_env env,napi_callback_info info)393 napi_value ConnectionModule::GetAllNets(napi_env env, napi_callback_info info)
394 {
395 return ModuleTemplate::Interface<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
396 ConnectionAsyncWork::ExecGetAllNets,
397 ConnectionAsyncWork::GetAllNetsCallback);
398 }
399
GetAllNetsSync(napi_env env,napi_callback_info info)400 napi_value ConnectionModule::GetAllNetsSync(napi_env env, napi_callback_info info)
401 {
402 return ModuleTemplate::InterfaceSync<GetAllNetsContext>(env, info, FUNCTION_GET_ALL_NETS, nullptr,
403 ConnectionExec::ExecGetAllNets,
404 ConnectionExec::GetAllNetsCallback);
405 }
406
EnableAirplaneMode(napi_env env,napi_callback_info info)407 napi_value ConnectionModule::EnableAirplaneMode(napi_env env, napi_callback_info info)
408 {
409 return ModuleTemplate::Interface<EnableAirplaneModeContext>(env, info, FUNCTION_ENABLE_AIRPLANE_MODE, nullptr,
410 ConnectionAsyncWork::ExecEnableAirplaneMode,
411 ConnectionAsyncWork::EnableAirplaneModeCallback);
412 }
413
DisableAirplaneMode(napi_env env,napi_callback_info info)414 napi_value ConnectionModule::DisableAirplaneMode(napi_env env, napi_callback_info info)
415 {
416 return ModuleTemplate::Interface<DisableAirplaneModeContext>(env, info, FUNCTION_DISABLE_AIRPLANE_MODE, nullptr,
417 ConnectionAsyncWork::ExecDisableAirplaneMode,
418 ConnectionAsyncWork::DisableAirplaneModeCallback);
419 }
420
ReportNetConnected(napi_env env,napi_callback_info info)421 napi_value ConnectionModule::ReportNetConnected(napi_env env, napi_callback_info info)
422 {
423 return ModuleTemplate::Interface<ReportNetConnectedContext>(env, info, FUNCTION_REPORT_NET_CONNECTED, nullptr,
424 ConnectionAsyncWork::ExecReportNetConnected,
425 ConnectionAsyncWork::ReportNetConnectedCallback);
426 }
427
ReportNetDisconnected(napi_env env,napi_callback_info info)428 napi_value ConnectionModule::ReportNetDisconnected(napi_env env, napi_callback_info info)
429 {
430 return ModuleTemplate::Interface<ReportNetDisconnectedContext>(env, info, FUNCTION_REPORT_NET_DISCONNECTED, nullptr,
431 ConnectionAsyncWork::ExecReportNetDisconnected,
432 ConnectionAsyncWork::ReportNetDisconnectedCallback);
433 }
434
GetDefaultHttpProxy(napi_env env,napi_callback_info info)435 napi_value ConnectionModule::GetDefaultHttpProxy(napi_env env, napi_callback_info info)
436 {
437 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_DEFAULT_HTTP_PROXY, nullptr,
438 ConnectionAsyncWork::ExecGetDefaultHttpProxy,
439 ConnectionAsyncWork::GetDefaultHttpProxyCallback);
440 }
441
GetGlobalHttpProxy(napi_env env,napi_callback_info info)442 napi_value ConnectionModule::GetGlobalHttpProxy(napi_env env, napi_callback_info info)
443 {
444 return ModuleTemplate::Interface<GetHttpProxyContext>(env, info, FUNCTION_GET_GLOBAL_HTTP_PROXY, nullptr,
445 ConnectionAsyncWork::ExecGetGlobalHttpProxy,
446 ConnectionAsyncWork::GetGlobalHttpProxyCallback);
447 }
448
SetGlobalHttpProxy(napi_env env,napi_callback_info info)449 napi_value ConnectionModule::SetGlobalHttpProxy(napi_env env, napi_callback_info info)
450 {
451 return ModuleTemplate::Interface<SetGlobalHttpProxyContext>(env, info, FUNCTION_SET_GLOBAL_HTTP_PROXY, nullptr,
452 ConnectionAsyncWork::ExecSetGlobalHttpProxy,
453 ConnectionAsyncWork::SetGlobalHttpProxyCallback);
454 }
455
SetAppHttpProxy(napi_env env,napi_callback_info info)456 napi_value ConnectionModule::SetAppHttpProxy(napi_env env, napi_callback_info info)
457 {
458 return ModuleTemplate::InterfaceSync<SetAppHttpProxyContext>(env, info, FUNCTION_SET_APP_HTTP_PROXY, nullptr,
459 ConnectionExec::ExecSetAppHttpProxy,
460 ConnectionExec::SetAppHttpProxyCallback);
461 }
462
GetAppNet(napi_env env,napi_callback_info info)463 napi_value ConnectionModule::GetAppNet(napi_env env, napi_callback_info info)
464 {
465 return ModuleTemplate::Interface<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
466 ConnectionAsyncWork::ExecGetAppNet,
467 ConnectionAsyncWork::GetAppNetCallback);
468 }
469
GetAppNetSync(napi_env env,napi_callback_info info)470 napi_value ConnectionModule::GetAppNetSync(napi_env env, napi_callback_info info)
471 {
472 return ModuleTemplate::InterfaceSync<GetAppNetContext>(env, info, FUNCTION_GET_APP_NET, nullptr,
473 ConnectionExec::ExecGetAppNet,
474 ConnectionExec::GetAppNetCallback);
475 }
476
SetAppNet(napi_env env,napi_callback_info info)477 napi_value ConnectionModule::SetAppNet(napi_env env, napi_callback_info info)
478 {
479 return ModuleTemplate::Interface<SetAppNetContext>(env, info, FUNCTION_SET_APP_NET, nullptr,
480 ConnectionAsyncWork::ExecSetAppNet,
481 ConnectionAsyncWork::SetAppNetCallback);
482 }
483
SetInterfaceUp(napi_env env,napi_callback_info info)484 napi_value ConnectionModule::SetInterfaceUp(napi_env env, napi_callback_info info)
485 {
486 return ModuleTemplate::Interface<SetInterfaceUpContext>(env, info, FUNCTION_SET_INTERFACE_UP, nullptr,
487 ConnectionAsyncWork::ExecSetInterfaceUp,
488 ConnectionAsyncWork::SetInterfaceUpCallback);
489 }
490
SetNetInterfaceIpAddress(napi_env env,napi_callback_info info)491 napi_value ConnectionModule::SetNetInterfaceIpAddress(napi_env env, napi_callback_info info)
492 {
493 return ModuleTemplate::Interface<SetInterfaceIpAddrContext>(env, info, FUNCTION_SET_INTERFACE_IP_ADDRESS, nullptr,
494 ConnectionAsyncWork::ExecSetInterfaceIpAddr,
495 ConnectionAsyncWork::SetInterfaceIpAddrCallback);
496 }
497
AddNetworkRoute(napi_env env,napi_callback_info info)498 napi_value ConnectionModule::AddNetworkRoute(napi_env env, napi_callback_info info)
499 {
500 return ModuleTemplate::Interface<AddNetworkRouteContext>(env, info, FUNCTION_ADD_NETWORK_ROUTE, nullptr,
501 ConnectionAsyncWork::ExecAddNetworkRoute,
502 ConnectionAsyncWork::AddNetworkRouteCallback);
503 }
504
GetNetInterfaceConfiguration(napi_env env,napi_callback_info info)505 napi_value ConnectionModule::GetNetInterfaceConfiguration(napi_env env, napi_callback_info info)
506 {
507 return ModuleTemplate::Interface<GetNetInterfaceConfigurationContext>(
508 env, info, FUNCTION_GET_INTERFACE_CONFIG, nullptr,
509 ConnectionAsyncWork::ExecGetNetInterfaceConfiguration,
510 ConnectionAsyncWork::GetNetInterfaceConfigurationCallback);
511 }
512
RegisterNetSupplier(napi_env env,napi_callback_info info)513 napi_value ConnectionModule::RegisterNetSupplier(napi_env env, napi_callback_info info)
514 {
515 return ModuleTemplate::Interface<RegisterNetSupplierContext>(
516 env, info, FUNCTION_REGISTER_NET_SUPPLIER, nullptr,
517 ConnectionAsyncWork::ExecRegisterNetSupplier,
518 ConnectionAsyncWork::RegisterNetSupplierCallback);
519 }
520
UnregisterNetSupplier(napi_env env,napi_callback_info info)521 napi_value ConnectionModule::UnregisterNetSupplier(napi_env env, napi_callback_info info)
522 {
523 return ModuleTemplate::Interface<UnregisterNetSupplierContext>(
524 env, info, FUNCTION_UNREGISTER_NET_SUPPLIER, nullptr,
525 ConnectionAsyncWork::ExecUnregisterNetSupplier,
526 ConnectionAsyncWork::UnregisterNetSupplierCallback);
527 }
528
AddCustomDnsRule(napi_env env,napi_callback_info info)529 napi_value ConnectionModule::AddCustomDnsRule(napi_env env, napi_callback_info info)
530 {
531 return ModuleTemplate::Interface<SetCustomDNSRuleContext>(env, info, FUNCTION_SET_CUSTOM_DNS_RULE, nullptr,
532 ConnectionAsyncWork::ExecSetCustomDNSRule,
533 ConnectionAsyncWork::SetCustomDNSRuleCallback);
534 }
535
RemoveCustomDnsRule(napi_env env,napi_callback_info info)536 napi_value ConnectionModule::RemoveCustomDnsRule(napi_env env, napi_callback_info info)
537 {
538 return ModuleTemplate::Interface<DeleteCustomDNSRuleContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULE, nullptr,
539 ConnectionAsyncWork::ExecDeleteCustomDNSRule,
540 ConnectionAsyncWork::DeleteCustomDNSRuleCallback);
541 }
542
ClearCustomDnsRules(napi_env env,napi_callback_info info)543 napi_value ConnectionModule::ClearCustomDnsRules(napi_env env, napi_callback_info info)
544 {
545 return ModuleTemplate::Interface<DeleteCustomDNSRulesContext>(env, info, FUNCTION_DELETE_CUSTOM_DNS_RULES, nullptr,
546 ConnectionAsyncWork::ExecDeleteCustomDNSRules,
547 ConnectionAsyncWork::DeleteCustomDNSRulesCallback);
548 }
549
FactoryResetNetwork(napi_env env,napi_callback_info info)550 napi_value ConnectionModule::FactoryResetNetwork(napi_env env, napi_callback_info info)
551 {
552 return ModuleTemplate::Interface<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
553 ConnectionAsyncWork::ExecFactoryResetNetwork,
554 ConnectionAsyncWork::FactoryResetNetworkCallback);
555 }
556
FactoryResetNetworkSync(napi_env env,napi_callback_info info)557 napi_value ConnectionModule::FactoryResetNetworkSync(napi_env env, napi_callback_info info)
558 {
559 return ModuleTemplate::InterfaceSync<FactoryResetNetworkContext>(env, info, FUNCTION_FACTORY_RESET_NETWORK, nullptr,
560 ConnectionExec::ExecFactoryResetNetwork,
561 ConnectionExec::FactoryResetNetworkCallback);
562 }
563
SetPacUrl(napi_env env,napi_callback_info info)564 napi_value ConnectionModule::SetPacUrl(napi_env env, napi_callback_info info)
565 {
566 return ModuleTemplate::InterfaceSync<SetPacUrlContext>(env, info, FUNCTION_SET_PAC_URL, nullptr,
567 ConnectionExec::ExecSetPacUrl,
568 ConnectionExec::SetPacUrlCallback);
569 }
570
GetPacUrl(napi_env env,napi_callback_info info)571 napi_value ConnectionModule::GetPacUrl(napi_env env, napi_callback_info info)
572 {
573 return ModuleTemplate::InterfaceSync<GetPacUrlContext>(env, info, FUNCTION_GET_PAC_URL, nullptr,
574 ConnectionExec::ExecGetPacUrl,
575 ConnectionExec::GetPacUrlCallback);
576 }
577
On(napi_env env,napi_callback_info info)578 napi_value ConnectionModule::NetConnectionInterface::On(napi_env env, napi_callback_info info)
579 {
580 std::initializer_list<std::string> events = {EVENT_NET_AVAILABLE,
581 EVENT_NET_BLOCK_STATUS_CHANGE,
582 EVENT_NET_CAPABILITIES_CHANGE,
583 EVENT_NET_CONNECTION_PROPERTIES_CHANGE,
584 EVENT_NET_LOST,
585 EVENT_NET_UNAVAILABLE};
586 return ModuleTemplate::On(env, info, events, false);
587 }
588
Register(napi_env env,napi_callback_info info)589 napi_value ConnectionModule::NetConnectionInterface::Register(napi_env env, napi_callback_info info)
590 {
591 return ModuleTemplate::Interface<RegisterContext>(
592 env, info, FUNCTION_REGISTER,
593 [](napi_env theEnv, napi_value thisVal, RegisterContext *context) -> bool {
594 if (context && context->GetManager() && !context->GetManager()->GetRef()) {
595 context->GetManager()->SetRef(NapiUtils::CreateReference(theEnv, thisVal));
596 }
597 return true;
598 },
599 ConnectionAsyncWork::NetConnectionAsyncWork::ExecRegister,
600 ConnectionAsyncWork::NetConnectionAsyncWork::RegisterCallback);
601 }
602
Unregister(napi_env env,napi_callback_info info)603 napi_value ConnectionModule::NetConnectionInterface::Unregister(napi_env env, napi_callback_info info)
604 {
605 return ModuleTemplate::Interface<UnregisterContext>(
606 env, info, FUNCTION_UNREGISTER,
607 [](napi_env theEnv, napi_value thisVal, UnregisterContext *context) -> bool {
608 if (context && context->GetManager()) {
609 if (context->GetManager()->GetRef()) {
610 NapiUtils::DeleteReference(theEnv, context->GetManager()->GetRef());
611 context->GetManager()->SetRef(nullptr);
612 }
613 context->GetManager()->DeleteAllListener();
614 }
615 return true;
616 },
617 ConnectionAsyncWork::NetConnectionAsyncWork::ExecUnregister,
618 ConnectionAsyncWork::NetConnectionAsyncWork::UnregisterCallback);
619 }
620
On(napi_env env,napi_callback_info info)621 napi_value ConnectionModule::NetInterfaceInterface::On(napi_env env, napi_callback_info info)
622 {
623 std::initializer_list<std::string> events = {EVENT_IFACE_ADDRESS_UPDATED,
624 EVENT_IFACE_ADDRESS_REMOVED,
625 EVENT_IFACE_ADDED,
626 EVENT_IFACE_REMOVED,
627 EVENT_IFACE_CHANGED,
628 EVENT_IFACE_LINK_STATE_CHANGED,
629 EVENT_IFACE_ROUTE_CHANGED};
630 return ModuleTemplate::On(env, info, events, false);
631 }
632
Register(napi_env env,napi_callback_info info)633 napi_value ConnectionModule::NetInterfaceInterface::Register(napi_env env, napi_callback_info info)
634 {
635 return ModuleTemplate::Interface<IfaceRegisterContext>(
636 env, info, FUNCTION_REGISTER,
637 [](napi_env theEnv, napi_value thisVal, IfaceRegisterContext *context) -> bool {
638 if (context && context->GetManager() && !context->GetManager()->GetRef()) {
639 context->GetManager()->SetRef(NapiUtils::CreateReference(theEnv, thisVal));
640 }
641 return true;
642 },
643 ConnectionAsyncWork::NetInterfaceAsyncWork::ExecIfaceRegister,
644 ConnectionAsyncWork::NetInterfaceAsyncWork::IfaceRegisterCallback);
645 }
646
Unregister(napi_env env,napi_callback_info info)647 napi_value ConnectionModule::NetInterfaceInterface::Unregister(napi_env env, napi_callback_info info)
648 {
649 return ModuleTemplate::Interface<IfaceUnregisterContext>(
650 env, info, FUNCTION_UNREGISTER,
651 [](napi_env theEnv, napi_value thisVal, IfaceUnregisterContext *context) -> bool {
652 if (context && context->GetManager()) {
653 if (context->GetManager()->GetRef()) {
654 NapiUtils::DeleteReference(theEnv, context->GetManager()->GetRef());
655 context->GetManager()->SetRef(nullptr);
656 }
657 context->GetManager()->DeleteAllListener();
658 }
659 return true;
660 },
661 ConnectionAsyncWork::NetInterfaceAsyncWork::ExecIfaceUnregister,
662 ConnectionAsyncWork::NetInterfaceAsyncWork::IfaceUnregisterCallback);
663 }
664
665 static napi_module g_connectionModule = {
666 .nm_version = 1,
667 .nm_flags = 0,
668 .nm_filename = nullptr,
669 .nm_register_func = ConnectionModule::InitConnectionModule,
670 .nm_modname = CONNECTION_MODULE_NAME,
671 .nm_priv = nullptr,
672 .reserved = {nullptr},
673 };
674
RegisterConnectionModule(void)675 extern "C" __attribute__((constructor)) void RegisterConnectionModule(void)
676 {
677 napi_module_register(&g_connectionModule);
678 }
679 } // namespace OHOS::NetManagerStandard
680