• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMMUNICATIONNETMANAGERBASE_CONNECTION_EXEC_H
17 #define COMMUNICATIONNETMANAGERBASE_CONNECTION_EXEC_H
18 
19 #include <netdb.h>
20 
21 #include "bindsocket_context.h"
22 #include "getaddressbyname_context.h"
23 #include "getappnet_context.h"
24 #include "getdefaultnet_context.h"
25 #include "gethttpproxy_context.h"
26 #include "napi/native_api.h"
27 #include "none_params_context.h"
28 #include "parse_nethandle_context.h"
29 #include "register_context.h"
30 #include "setappnet_context.h"
31 #include "setglobalhttpproxy_context.h"
32 #include "setcustomdnsrule_context.h"
33 #include "deletecustomdnsrule_context.h"
34 #include "deletecustomdnsrules_context.h"
35 #include "factoryresetnetwork_context.h"
36 #include "setapphttpproxy_context.h"
37 #include "pacurl_context.h"
38 #include "setinterfaceup_context.h"
39 #include "setinterfaceipaddr_context.h"
40 #include "addnetworkroute_context.h"
41 #include "interfaceregister_context.h"
42 #include "getinterfaceconfig_context.h"
43 #include "registernetsupplier_context.h"
44 #include "unregisternetsupplier_context.h"
45 #include "netextattribute_context.h"
46 
47 namespace OHOS::NetManagerStandard {
48 static constexpr size_t MAX_ARRAY_LENGTH = 64;
49 static constexpr size_t MAX_IPV4_STR_LEN = 16;
50 static constexpr size_t MAX_IPV6_STR_LEN = 64;
51 class ConnectionExec final {
52 public:
53     ConnectionExec() = delete;
54 
55     ~ConnectionExec() = delete;
56 
57     static napi_value CreateNetHandle(napi_env env, NetHandle *handle);
58 
59     static napi_value CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities);
60 
61     static napi_value CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo);
62 
63     static bool ExecGetDefaultNet(GetDefaultNetContext *context);
64 
65     static napi_value GetDefaultNetCallback(GetDefaultNetContext *context);
66 
67     static bool ExecHasDefaultNet(HasDefaultNetContext *context);
68 
69     static napi_value HasDefaultNetCallback(HasDefaultNetContext *context);
70 
71     static bool ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context);
72 
73     static napi_value IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext *context);
74 
75     static bool ExecGetNetCapabilities(GetNetCapabilitiesContext *context);
76 
77     static napi_value GetNetCapabilitiesCallback(GetNetCapabilitiesContext *context);
78 
79     static bool ExecGetConnectionProperties(GetConnectionPropertiesContext *context);
80 
81     static napi_value GetConnectionPropertiesCallback(GetConnectionPropertiesContext *context);
82 
83     static bool ExecGetAddressByName(GetAddressByNameContext *context);
84 
85     static napi_value GetAddressByNameCallback(GetAddressByNameContext *context);
86 
87     static bool ExecGetAllNets(GetAllNetsContext *context);
88 
89     static napi_value GetAllNetsCallback(GetAllNetsContext *context);
90 
91     static bool ExecEnableAirplaneMode(EnableAirplaneModeContext *context);
92 
93     static napi_value EnableAirplaneModeCallback(EnableAirplaneModeContext *context);
94 
95     static bool ExecDisableAirplaneMode(DisableAirplaneModeContext *context);
96 
97     static napi_value DisableAirplaneModeCallback(DisableAirplaneModeContext *context);
98 
99     static bool ExecReportNetConnected(ReportNetConnectedContext *context);
100 
101     static napi_value ReportNetConnectedCallback(ReportNetConnectedContext *context);
102 
103     static bool ExecReportNetDisconnected(ReportNetDisconnectedContext *context);
104 
105     static napi_value ReportNetDisconnectedCallback(ReportNetDisconnectedContext *context);
106 
107     static bool ExecGetDefaultHttpProxy(GetHttpProxyContext *context);
108 
109     static napi_value GetDefaultHttpProxyCallback(GetHttpProxyContext *context);
110 
111     static bool ExecGetGlobalHttpProxy(GetHttpProxyContext *context);
112 
113     static napi_value GetGlobalHttpProxyCallback(GetHttpProxyContext *context);
114 
115     static bool ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext *context);
116 
117     static napi_value SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext *context);
118 
119     static bool ExecSetAppHttpProxy(SetAppHttpProxyContext *context);
120 
121     static napi_value SetAppHttpProxyCallback(SetAppHttpProxyContext *context);
122 
123     static bool ExecGetAppNet(GetAppNetContext *context);
124 
125     static napi_value GetAppNetCallback(GetAppNetContext *context);
126 
127     static bool ExecSetAppNet(SetAppNetContext *context);
128 
129     static napi_value SetAppNetCallback(SetAppNetContext *context);
130 
131     static bool ExecSetInterfaceUp(SetInterfaceUpContext *context);
132 
133     static napi_value SetInterfaceUpCallback(SetInterfaceUpContext *context);
134 
135     static bool ExecSetInterfaceIpAddr(SetInterfaceIpAddrContext *context);
136 
137     static napi_value SetInterfaceIpAddrCallback(SetInterfaceIpAddrContext *context);
138 
139     static bool ExecAddNetworkRoute(AddNetworkRouteContext *context);
140 
141     static napi_value AddNetworkRouteCallback(AddNetworkRouteContext *context);
142 
143     static bool ExecGetNetInterfaceConfiguration(GetNetInterfaceConfigurationContext *context);
144 
145     static napi_value GetNetInterfaceConfigurationCallback(GetNetInterfaceConfigurationContext *context);
146 
147     static bool ExecRegisterNetSupplier(RegisterNetSupplierContext *context);
148 
149     static napi_value RegisterNetSupplierCallback(RegisterNetSupplierContext *context);
150 
151     static bool ExecUnregisterNetSupplier(UnregisterNetSupplierContext *context);
152 
153     static napi_value UnregisterNetSupplierCallback(UnregisterNetSupplierContext *context);
154 
155     static bool ExecSetCustomDNSRule(SetCustomDNSRuleContext *context);
156 
157     static napi_value SetCustomDNSRuleCallback(SetCustomDNSRuleContext *context);
158 
159     static bool ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext *context);
160 
161     static napi_value DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext *context);
162 
163     static bool ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext *context);
164 
165     static napi_value DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext *context);
166 
167     static bool ExecFactoryResetNetwork(FactoryResetNetworkContext *context);
168 
169     static napi_value FactoryResetNetworkCallback(FactoryResetNetworkContext *context);
170 
171     static bool ExecSetPacUrl(SetPacUrlContext *context);
172 
173     static napi_value SetPacUrlCallback(SetPacUrlContext *context);
174 
175     static bool ExecGetPacUrl(GetPacUrlContext *context);
176 
177     static napi_value GetPacUrlCallback(GetPacUrlContext *context);
178 
179     static bool ExecGetNetExtAttribute(GetNetExtAttributeContext *context);
180 
181     static napi_value GetNetExtAttributeCallback(GetNetExtAttributeContext *context);
182 
183     static bool ExecSetNetExtAttribute(SetNetExtAttributeContext *context);
184 
185     static napi_value SetNetExtAttributeCallback(SetNetExtAttributeContext *context);
186 
187     static bool ExecSetPacFileUrl(SetPacFileUrlContext *context);
188 
189     static bool ExecSetProxyMode(ProxyModeContext *context);
190 
191     static bool ExecGetProxyMode(ProxyModeContext *context);
192 
193     static napi_value SetProxyModeCallback(ProxyModeContext *context);
194 
195     static napi_value GetProxyModeCallback(ProxyModeContext *context);
196 
197     static napi_value SetPacFileUrlCallback(SetPacFileUrlContext *context);
198 
199     static bool ExecGetPacFileUrl(GetPacFileUrlContext *context);
200 
201     static napi_value GetPacFileUrlCallback(GetPacFileUrlContext *context);
202 
203     static bool ExecFindProxyForUrl(FindPacFileUrlContext *context);
204 
205     static napi_value FindProxyForUrlCallback(FindPacFileUrlContext *context);
206 
207     class NetHandleExec final {
208     public:
209         NetHandleExec() = delete;
210 
211         ~NetHandleExec() = delete;
212 
213         static bool ExecGetAddressByName(GetAddressByNameContext *context);
214 
215         static napi_value GetAddressByNameCallback(GetAddressByNameContext *context);
216 
217         static bool ExecGetAddressesByName(GetAddressByNameContext *context);
218 
219         static napi_value GetAddressesByNameCallback(GetAddressByNameContext *context);
220 
221         static bool ExecBindSocket(BindSocketContext *context);
222 
223         static napi_value BindSocketCallback(BindSocketContext *context);
224 
225     private:
226         static napi_value MakeNetAddressJsValue(napi_env env, const NetAddress &address);
227 
228         static void SetAddressInfo(const char *host, addrinfo *info, NetAddress &address);
229     };
230 
231     class NetConnectionExec final {
232     public:
233         NetConnectionExec() = delete;
234 
235         ~NetConnectionExec() = delete;
236 
237         static bool ExecRegister(RegisterContext *context);
238 
239         static napi_value RegisterCallback(RegisterContext *context);
240 
241         static bool ExecUnregister(UnregisterContext *context);
242 
243         static napi_value UnregisterCallback(UnregisterContext *context);
244     };
245 
246     class NetInterfaceExec final {
247     public:
248         NetInterfaceExec() = delete;
249 
250         ~NetInterfaceExec() = delete;
251 
252         static bool ExecIfaceRegister(IfaceRegisterContext *context);
253 
254         static napi_value IfaceRegisterCallback(IfaceRegisterContext *context);
255 
256         static bool ExecIfaceUnregister(IfaceUnregisterContext *context);
257 
258         static napi_value IfaceUnregisterCallback(IfaceUnregisterContext *context);
259     };
260 
261 private:
262     static void FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
263 
264     static void FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
265 
266     static void FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
267 };
268 
269 extern std::mutex g_predefinedHostMtx;
270 } // namespace OHOS::NetManagerStandard
271 
272 #endif /* COMMUNICATIONNETMANAGERBASE_CONNECTION_EXEC_H */
273