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