1 /*
2  * Copyright (c) 2021-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 "bind_context.h"
17 
18 #include "context_key.h"
19 #include "socket_constant.h"
20 #include "event_manager.h"
21 #include "netstack_log.h"
22 #include "napi_utils.h"
23 
24 namespace OHOS::NetStack::Socket {
BindContext(napi_env env,EventManager * manager)25 BindContext::BindContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void BindContext::ParseParams(napi_value *params, size_t paramsCount)
28 {
29     bool valid = CheckParamsType(params, paramsCount);
30     if (!valid) {
31         if (paramsCount == PARAM_JUST_CALLBACK) {
32             if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
33                 SetCallback(params[0]);
34             }
35             return;
36         }
37         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
38             if (NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function) {
39                 SetCallback(params[1]);
40             }
41             return;
42         }
43         return;
44     }
45 
46     std::string addr = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[0], KEY_ADDRESS);
47     if (addr.empty()) {
48         NETSTACK_LOGE("address is empty");
49     }
50     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_FAMILY)) {
51         uint32_t family = NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_FAMILY);
52         address_.SetFamilyByJsValue(family);
53     }
54     address_.SetIpAddress(addr);
55     if (address_.GetAddress().empty()) {
56         if (paramsCount == PARAM_OPTIONS_AND_CALLBACK && SetCallback(params[1]) != napi_ok) {
57             NETSTACK_LOGE("failed to set callback");
58         }
59         return;
60     }
61 
62     if (NapiUtils::HasNamedProperty(GetEnv(), params[0], KEY_PORT)) {
63         uint16_t port = static_cast<uint16_t>(NapiUtils::GetUint32Property(GetEnv(), params[0], KEY_PORT));
64         address_.SetPort(port);
65     }
66 
67     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
68         SetParseOK(SetCallback(params[1]) == napi_ok);
69         return;
70     }
71     SetParseOK(true);
72 }
73 
GetSocketFd() const74 int BindContext::GetSocketFd() const
75 {
76     if (manager_ == nullptr) {
77         return -1;
78     }
79     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
80 }
81 
CheckParamsType(napi_value * params,size_t paramsCount)82 bool BindContext::CheckParamsType(napi_value *params, size_t paramsCount)
83 {
84     if (paramsCount == PARAM_JUST_OPTIONS) {
85         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
86     }
87 
88     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
89         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object &&
90                NapiUtils::GetValueType(GetEnv(), params[1]) == napi_function;
91     }
92     return false;
93 }
94 
GetErrorCode() const95 int32_t BindContext::GetErrorCode() const
96 {
97     if (BaseContext::IsPermissionDenied()) {
98         return PERMISSION_DENIED_CODE;
99     }
100 
101     auto err = BaseContext::GetErrorCode();
102     if (err == PARSE_ERROR_CODE) {
103         return PARSE_ERROR_CODE;
104     }
105 #if defined(IOS_PLATFORM)
106     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
107 #endif
108     return err + SOCKET_ERROR_CODE_BASE;
109 }
110 
GetErrorMessage() const111 std::string BindContext::GetErrorMessage() const
112 {
113     if (BaseContext::IsPermissionDenied()) {
114         return PERMISSION_DENIED_MSG;
115     }
116 
117     auto errCode = BaseContext::GetErrorCode();
118     if (errCode == PARSE_ERROR_CODE) {
119         return PARSE_ERROR_MSG;
120     }
121 #if defined(IOS_PLATFORM)
122     std::string errMessage;
123     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
124     return errMessage;
125 #else
126     char err[MAX_ERR_NUM] = {0};
127     (void)strerror_r(errCode, err, MAX_ERR_NUM);
128     return err;
129 #endif
130 }
131 } // namespace OHOS::NetStack::Socket
132