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