1 /*
2  * Copyright (c) 2023-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 "tcp_server_listen_context.h"
17 
18 #include "context_key.h"
19 #include "event_manager.h"
20 #include "napi_utils.h"
21 #include "netstack_log.h"
22 #include "socket_constant.h"
23 
24 namespace OHOS::NetStack::Socket {
TcpServerListenContext(napi_env env,EventManager * manager)25 TcpServerListenContext::TcpServerListenContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
26 
ParseParams(napi_value * params,size_t paramsCount)27 void TcpServerListenContext::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 
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 TcpServerListenContext::GetSocketFd() const
76 {
77     return manager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(manager_->GetData())) : -1;
78 }
79 
CheckParamsType(napi_value * params,size_t paramsCount)80 bool TcpServerListenContext::CheckParamsType(napi_value *params, size_t paramsCount)
81 {
82     if (paramsCount == PARAM_JUST_OPTIONS) {
83         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object) {
84             NETSTACK_LOGE("first param is not NetAddress");
85             SetNeedThrowException(true);
86             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
87             return false;
88         }
89         return true;
90     }
91 
92     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
93         if (NapiUtils::GetValueType(GetEnv(), params[0]) != napi_object ||
94             NapiUtils::GetValueType(GetEnv(), params[1]) != napi_function) {
95             NETSTACK_LOGE("invalid parameter");
96             SetNeedThrowException(true);
97             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
98             return false;
99         }
100         return true;
101     }
102     return false;
103 }
104 
GetErrorCode() const105 int32_t TcpServerListenContext::GetErrorCode() const
106 {
107     if (BaseContext::IsPermissionDenied()) {
108         return PERMISSION_DENIED_CODE;
109     }
110 
111     auto err = BaseContext::GetErrorCode();
112     if (err == PARSE_ERROR_CODE) {
113         return PARSE_ERROR_CODE;
114     }
115     return err + SOCKET_SERVER_ERROR_CODE_BASE;
116 }
117 
GetErrorMessage() const118 std::string TcpServerListenContext::GetErrorMessage() const
119 {
120     if (BaseContext::IsPermissionDenied()) {
121         return PERMISSION_DENIED_MSG;
122     }
123 
124     auto errCode = BaseContext::GetErrorCode();
125     if (errCode == PARSE_ERROR_CODE) {
126         return PARSE_ERROR_MSG;
127     }
128     char err[MAX_ERR_NUM] = {0};
129     (void)strerror_r(errCode, err, MAX_ERR_NUM);
130     return err;
131 }
132 } // namespace OHOS::NetStack::Socket
133