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 
38 namespace OHOS::NetManagerStandard {
39 static constexpr size_t MAX_ARRAY_LENGTH = 64;
40 static constexpr size_t MAX_IPV4_STR_LEN = 16;
41 static constexpr size_t MAX_IPV6_STR_LEN = 64;
42 class ConnectionExec final {
43 public:
44     ConnectionExec() = delete;
45 
46     ~ConnectionExec() = delete;
47 
48     static napi_value CreateNetHandle(napi_env env, NetHandle *handle);
49 
50     static napi_value CreateNetCapabilities(napi_env env, NetAllCapabilities *capabilities);
51 
52     static napi_value CreateConnectionProperties(napi_env env, NetLinkInfo *linkInfo);
53 
54     static bool ExecGetDefaultNet(GetDefaultNetContext *context);
55 
56     static napi_value GetDefaultNetCallback(GetDefaultNetContext *context);
57 
58     static bool ExecHasDefaultNet(HasDefaultNetContext *context);
59 
60     static napi_value HasDefaultNetCallback(HasDefaultNetContext *context);
61 
62     static bool ExecIsDefaultNetMetered(IsDefaultNetMeteredContext *context);
63 
64     static napi_value IsDefaultNetMeteredCallback(IsDefaultNetMeteredContext *context);
65 
66     static bool ExecGetNetCapabilities(GetNetCapabilitiesContext *context);
67 
68     static napi_value GetNetCapabilitiesCallback(GetNetCapabilitiesContext *context);
69 
70     static bool ExecGetConnectionProperties(GetConnectionPropertiesContext *context);
71 
72     static napi_value GetConnectionPropertiesCallback(GetConnectionPropertiesContext *context);
73 
74     static bool ExecGetAddressByName(GetAddressByNameContext *context);
75 
76     static napi_value GetAddressByNameCallback(GetAddressByNameContext *context);
77 
78     static bool ExecGetAllNets(GetAllNetsContext *context);
79 
80     static napi_value GetAllNetsCallback(GetAllNetsContext *context);
81 
82     static bool ExecEnableAirplaneMode(EnableAirplaneModeContext *context);
83 
84     static napi_value EnableAirplaneModeCallback(EnableAirplaneModeContext *context);
85 
86     static bool ExecDisableAirplaneMode(DisableAirplaneModeContext *context);
87 
88     static napi_value DisableAirplaneModeCallback(DisableAirplaneModeContext *context);
89 
90     static bool ExecReportNetConnected(ReportNetConnectedContext *context);
91 
92     static napi_value ReportNetConnectedCallback(ReportNetConnectedContext *context);
93 
94     static bool ExecReportNetDisconnected(ReportNetDisconnectedContext *context);
95 
96     static napi_value ReportNetDisconnectedCallback(ReportNetDisconnectedContext *context);
97 
98     static bool ExecGetDefaultHttpProxy(GetHttpProxyContext *context);
99 
100     static napi_value GetDefaultHttpProxyCallback(GetHttpProxyContext *context);
101 
102     static bool ExecGetGlobalHttpProxy(GetHttpProxyContext *context);
103 
104     static napi_value GetGlobalHttpProxyCallback(GetHttpProxyContext *context);
105 
106     static bool ExecSetGlobalHttpProxy(SetGlobalHttpProxyContext *context);
107 
108     static napi_value SetGlobalHttpProxyCallback(SetGlobalHttpProxyContext *context);
109 
110     static bool ExecSetAppHttpProxy(SetAppHttpProxyContext *context);
111 
112     static napi_value SetAppHttpProxyCallback(SetAppHttpProxyContext *context);
113 
114     static bool ExecGetAppNet(GetAppNetContext *context);
115 
116     static napi_value GetAppNetCallback(GetAppNetContext *context);
117 
118     static bool ExecSetAppNet(SetAppNetContext *context);
119 
120     static napi_value SetAppNetCallback(SetAppNetContext *context);
121 
122     static bool ExecSetCustomDNSRule(SetCustomDNSRuleContext *context);
123 
124     static napi_value SetCustomDNSRuleCallback(SetCustomDNSRuleContext *context);
125 
126     static bool ExecDeleteCustomDNSRule(DeleteCustomDNSRuleContext *context);
127 
128     static napi_value DeleteCustomDNSRuleCallback(DeleteCustomDNSRuleContext *context);
129 
130     static bool ExecDeleteCustomDNSRules(DeleteCustomDNSRulesContext *context);
131 
132     static napi_value DeleteCustomDNSRulesCallback(DeleteCustomDNSRulesContext *context);
133 
134     static bool ExecFactoryResetNetwork(FactoryResetNetworkContext *context);
135 
136     static napi_value FactoryResetNetworkCallback(FactoryResetNetworkContext *context);
137 
138     class NetHandleExec final {
139     public:
140         NetHandleExec() = delete;
141 
142         ~NetHandleExec() = delete;
143 
144         static bool ExecGetAddressByName(GetAddressByNameContext *context);
145 
146         static napi_value GetAddressByNameCallback(GetAddressByNameContext *context);
147 
148         static bool ExecGetAddressesByName(GetAddressByNameContext *context);
149 
150         static napi_value GetAddressesByNameCallback(GetAddressByNameContext *context);
151 
152         static bool ExecBindSocket(BindSocketContext *context);
153 
154         static napi_value BindSocketCallback(BindSocketContext *context);
155 
156     private:
157         static napi_value MakeNetAddressJsValue(napi_env env, const NetAddress &address);
158 
159         static void SetAddressInfo(const char *host, addrinfo *info, NetAddress &address);
160     };
161 
162     class NetConnectionExec final {
163     public:
164         NetConnectionExec() = delete;
165 
166         ~NetConnectionExec() = delete;
167 
168         static bool ExecRegister(RegisterContext *context);
169 
170         static napi_value RegisterCallback(RegisterContext *context);
171 
172         static bool ExecUnregister(UnregisterContext *context);
173 
174         static napi_value UnregisterCallback(UnregisterContext *context);
175     };
176 
177 private:
178     static void FillLinkAddress(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
179 
180     static void FillRouoteList(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
181 
182     static void FillDns(napi_env env, napi_value connectionProperties, NetLinkInfo *linkInfo);
183 };
184 } // namespace OHOS::NetManagerStandard
185 
186 #endif /* COMMUNICATIONNETMANAGERBASE_CONNECTION_EXEC_H */
187