• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "tlssocketserver_exec.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include <napi/native_api.h>
22 #include <securec.h>
23 
24 #include "context_key.h"
25 #include "event_list.h"
26 #include "napi_utils.h"
27 #include "netstack_log.h"
28 #include "socket_error.h"
29 #include "tls_socket_server.h"
30 
31 namespace OHOS {
32 namespace NetStack {
33 namespace TlsSocketServer {
34 namespace {
35 constexpr const char *CERTIFICATA_DATA = "data";
36 constexpr const char *CERTIFICATA_ENCODING_FORMAT = "encodingFormat";
37 } // namespace
ExecGetCertificate(TlsSocket::GetCertificateContext * context)38 bool TLSSocketServerExec::ExecGetCertificate(TlsSocket::GetCertificateContext *context)
39 {
40     auto manager = context->GetManager();
41     if (manager == nullptr) {
42         NETSTACK_LOGE("manager is nullptr");
43         return false;
44     }
45     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
46     if (tlsSocketServer == nullptr) {
47         NETSTACK_LOGE("ExecGetCertificate tlsSocketServer is null");
48         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
49                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
50         return false;
51     }
52     tlsSocketServer->GetCertificate([&context](int32_t errorNumber, const TlsSocket::X509CertRawData &cert) {
53         context->localCert_ = cert;
54         context->errorNumber_ = errorNumber;
55         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
56             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
57         }
58     });
59     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
60 }
61 
ExecListen(TlsSocket::TLSListenContext * context)62 bool TLSSocketServerExec::ExecListen(TlsSocket::TLSListenContext *context)
63 {
64     auto manager = context->GetManager();
65     if (manager == nullptr) {
66         NETSTACK_LOGE("manager is nullptr");
67         return false;
68     }
69     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
70     if (tlsSocketServer == nullptr) {
71         tlsSocketServer = new TLSSocketServer();
72     }
73     if (manager->GetData() == nullptr) {
74         manager->SetData(tlsSocketServer);
75     }
76     tlsSocketServer->Listen(context->connectOptions_, [&context](int32_t errorNumber) {
77         context->errorNumber_ = errorNumber;
78         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
79             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
80         }
81     });
82     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
83 }
84 
ExecGetCipherSuites(ServerGetCipherSuitesContext * context)85 bool TLSSocketServerExec::ExecGetCipherSuites(ServerGetCipherSuitesContext *context)
86 {
87     auto manager = context->GetManager();
88     if (manager == nullptr) {
89         NETSTACK_LOGE("manager is nullptr");
90         return false;
91     }
92     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
93     if (tlsSocketServer == nullptr) {
94         NETSTACK_LOGE("ExecGetCipherSuites tlsSocketServer is null");
95         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
96                           MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
97         return false;
98     }
99     tlsSocketServer->GetCipherSuite(context->clientId_,
100                                     [&context](int32_t errorNumber, const std::vector<std::string> &suite) {
101                                         context->cipherSuites_ = suite;
102                                         context->errorNumber_ = errorNumber;
103                                         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
104                                             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
105                                         }
106                                     });
107     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
108 }
109 
ExecGetRemoteCertificate(ServerGetRemoteCertificateContext * context)110 bool TLSSocketServerExec::ExecGetRemoteCertificate(ServerGetRemoteCertificateContext *context)
111 {
112     auto manager = context->GetManager();
113     if (manager == nullptr) {
114         NETSTACK_LOGE("manager is nullptr");
115         return false;
116     }
117     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
118     if (tlsSocketServer == nullptr) {
119         NETSTACK_LOGE("ExecGetRemoteCertificate tlsSocketServer is null");
120         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
121                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
122         return false;
123     }
124     tlsSocketServer->GetRemoteCertificate(
125         context->clientId_, [&context](int32_t errorNumber, const TlsSocket::X509CertRawData &cert) {
126             context->remoteCert_ = cert;
127             context->errorNumber_ = errorNumber;
128             if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
129                 context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
130             }
131         });
132     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
133 }
134 
ExecGetProtocol(TlsSocket::GetProtocolContext * context)135 bool TLSSocketServerExec::ExecGetProtocol(TlsSocket::GetProtocolContext *context)
136 {
137     auto manager = context->GetManager();
138     if (manager == nullptr) {
139         NETSTACK_LOGE("manager is nullptr");
140         return false;
141     }
142     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
143     if (tlsSocketServer == nullptr) {
144         NETSTACK_LOGE("ExecGetProtocol tlsSocketServer is null");
145         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
146                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
147         return false;
148     }
149     tlsSocketServer->GetProtocol([&context](int32_t errorNumber, const std::string &protocol) {
150         context->protocol_ = protocol;
151         context->errorNumber_ = errorNumber;
152         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
153             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
154         }
155     });
156     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
157 }
158 
ExecGetSignatureAlgorithms(ServerGetSignatureAlgorithmsContext * context)159 bool TLSSocketServerExec::ExecGetSignatureAlgorithms(ServerGetSignatureAlgorithmsContext *context)
160 {
161     auto manager = context->GetManager();
162     if (manager == nullptr) {
163         NETSTACK_LOGE("manager is nullptr");
164         return false;
165     }
166     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
167     if (tlsSocketServer == nullptr) {
168         NETSTACK_LOGE("ExecGetSignatureAlgorithms tlsSocketServer is null");
169         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
170                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
171         return false;
172     }
173     tlsSocketServer->GetSignatureAlgorithms(
174         context->clientId_, [&context](int32_t errorNumber, const std::vector<std::string> &algorithms) {
175             context->signatureAlgorithms_ = algorithms;
176             context->errorNumber_ = errorNumber;
177             if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
178                 context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
179             }
180         });
181     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
182 }
183 
ExecSend(TLSServerSendContext * context)184 bool TLSSocketServerExec::ExecSend(TLSServerSendContext *context)
185 {
186     auto manager = context->GetManager();
187     if (manager == nullptr) {
188         NETSTACK_LOGE("manager is nullptr");
189         return false;
190     }
191     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
192     if (tlsSocketServer == nullptr) {
193         NETSTACK_LOGE("ExecSend tlsSocketServer is null");
194         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
195                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
196         return false;
197     }
198     TLSServerSendOptions tcpSendOptions;
199     int client_id = context->clientId_;
200     tcpSendOptions.SetSocket(client_id);
201     tcpSendOptions.SetSendData(context->m_sendData);
202     tlsSocketServer->Send(tcpSendOptions, [&context](int32_t errorNumber) {
203         context->errorNumber_ = errorNumber;
204         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
205             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
206         }
207     });
208     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
209 }
210 
ExecClose(TLSServerCloseContext * context)211 bool TLSSocketServerExec::ExecClose(TLSServerCloseContext *context)
212 {
213     auto manager = context->GetManager();
214     if (manager == nullptr) {
215         NETSTACK_LOGE("manager is nullptr");
216         return false;
217     }
218     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
219     if (tlsSocketServer == nullptr) {
220         NETSTACK_LOGE("ExecClose tlsSocketServer is null");
221         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
222                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
223         return false;
224     }
225     tlsSocketServer->Close(context->clientId_, [&context](int32_t errorNumber) {
226         context->errorNumber_ = errorNumber;
227         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
228             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
229         }
230     });
231     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
232 }
233 
ExecStop(TlsSocket::TLSNapiContext * context)234 bool TLSSocketServerExec::ExecStop(TlsSocket::TLSNapiContext *context)
235 {
236     auto manager = context->GetManager();
237     if (manager == nullptr) {
238         NETSTACK_LOGE("manager is nullptr");
239         return false;
240     }
241     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
242     if (tlsSocketServer == nullptr) {
243         NETSTACK_LOGE("ExecClose tlsSocketServer is null");
244         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
245                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
246         return false;
247     }
248     tlsSocketServer->Stop([&context](int32_t errorNumber) {
249         context->errorNumber_ = errorNumber;
250         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
251             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
252         }
253     });
254     delete tlsSocketServer;
255     manager->SetData(nullptr);
256     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
257 }
258 
ExecGetRemoteAddress(ServerTLSGetRemoteAddressContext * context)259 bool TLSSocketServerExec::ExecGetRemoteAddress(ServerTLSGetRemoteAddressContext *context)
260 {
261     auto manager = context->GetManager();
262     if (manager == nullptr) {
263         NETSTACK_LOGE("manager is nullptr");
264         return false;
265     }
266     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
267     if (tlsSocketServer == nullptr) {
268         NETSTACK_LOGE("ExecGetRemoteAddress tlsSocketServer is null");
269         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
270                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
271         return false;
272     }
273     tlsSocketServer->GetRemoteAddress(context->clientId_,
274                                       [&context](int32_t errorNumber, const Socket::NetAddress address) {
275                                           context->address_ = address;
276                                           context->errorNumber_ = errorNumber;
277                                           if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
278                                               context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
279                                           }
280                                       });
281     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
282 }
283 
ExecGetState(TlsSocket::TLSGetStateContext * context)284 bool TLSSocketServerExec::ExecGetState(TlsSocket::TLSGetStateContext *context)
285 {
286     auto manager = context->GetManager();
287     if (manager == nullptr) {
288         NETSTACK_LOGE("manager is nullptr");
289         context->state_.SetIsClose(true);
290         return true;
291     }
292     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
293     if (tlsSocketServer == nullptr) {
294         NETSTACK_LOGE("ExecGetState tlsSocketServer is null");
295         context->state_.SetIsClose(true);
296         return true;
297     }
298     tlsSocketServer->GetState([&context](int32_t errorNumber, const Socket::SocketStateBase state) {
299         context->state_ = state;
300         context->errorNumber_ = errorNumber;
301         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
302             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
303         }
304     });
305     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
306 }
307 
ExecSetExtraOptions(TlsSocket::TLSSetExtraOptionsContext * context)308 bool TLSSocketServerExec::ExecSetExtraOptions(TlsSocket::TLSSetExtraOptionsContext *context)
309 {
310     auto manager = context->GetManager();
311     if (manager == nullptr) {
312         NETSTACK_LOGE("manager is nullptr");
313         return false;
314     }
315     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
316     if (tlsSocketServer == nullptr) {
317         NETSTACK_LOGE("ExecSetExtraOptions tlsSocketServer is null");
318         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
319                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
320         return false;
321     }
322     tlsSocketServer->SetExtraOptions(context->options_, [&context](int32_t errorNumber) {
323         context->errorNumber_ = errorNumber;
324         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
325             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
326         }
327     });
328     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
329 }
330 
GetCertificateCallback(TlsSocket::GetCertificateContext * context)331 napi_value TLSSocketServerExec::GetCertificateCallback(TlsSocket::GetCertificateContext *context)
332 {
333     void *data = nullptr;
334     napi_value arrayBuffer = NapiUtils::CreateArrayBuffer(context->GetEnv(), context->localCert_.data.Length(), &data);
335     if (data != nullptr && arrayBuffer != nullptr) {
336         if (memcpy_s(data, context->localCert_.data.Length(),
337                      reinterpret_cast<const uint8_t *>(context->localCert_.data.Data()),
338                      context->localCert_.data.Length()) != EOK) {
339             NETSTACK_LOGE("memcpy_s failed!");
340             return NapiUtils::GetUndefined(context->GetEnv());
341         }
342     }
343     napi_value outData = nullptr;
344     napi_create_typedarray(context->GetEnv(), napi_uint8_array, context->localCert_.data.Length(), arrayBuffer, 0,
345                            &outData);
346     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
347     if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) {
348         return NapiUtils::GetUndefined(context->GetEnv());
349     }
350     NapiUtils::SetNamedProperty(context->GetEnv(), obj, CERTIFICATA_DATA, outData);
351     NapiUtils::SetInt32Property(context->GetEnv(), obj, CERTIFICATA_ENCODING_FORMAT,
352                                 context->localCert_.encodingFormat);
353     return obj;
354 }
355 
ListenCallback(TlsSocket::TLSListenContext * context)356 napi_value TLSSocketServerExec::ListenCallback(TlsSocket::TLSListenContext *context)
357 {
358     context->Emit(EVENT_LISTENING, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()),
359                                                   NapiUtils::GetUndefined(context->GetEnv())));
360     return NapiUtils::GetUndefined(context->GetEnv());
361 }
362 
GetCipherSuitesCallback(ServerGetCipherSuitesContext * context)363 napi_value TLSSocketServerExec::GetCipherSuitesCallback(ServerGetCipherSuitesContext *context)
364 {
365     napi_value cipherSuites = NapiUtils::CreateArray(context->GetEnv(), 0);
366     int index = 0;
367     for (const auto &cipher : context->cipherSuites_) {
368         napi_value cipherSuite = NapiUtils::CreateStringUtf8(context->GetEnv(), cipher);
369         NapiUtils::SetArrayElement(context->GetEnv(), cipherSuites, index++, cipherSuite);
370     }
371     return cipherSuites;
372 }
373 
GetRemoteCertificateCallback(ServerGetRemoteCertificateContext * context)374 napi_value TLSSocketServerExec::GetRemoteCertificateCallback(ServerGetRemoteCertificateContext *context)
375 {
376     napi_value obj = nullptr;
377     if (context->remoteCert_.data.Length() > 0 && context->remoteCert_.data.Data() != nullptr) {
378         void *data = nullptr;
379         napi_value arrayBuffer =
380             NapiUtils::CreateArrayBuffer(context->GetEnv(), context->remoteCert_.data.Length(), &data);
381         if (data != nullptr && arrayBuffer != nullptr) {
382             if (memcpy_s(data, context->remoteCert_.data.Length(),
383                          reinterpret_cast<const uint8_t *>(context->remoteCert_.data.Data()),
384                          context->remoteCert_.data.Length()) != EOK) {
385                 NETSTACK_LOGE("memcpy_s failed!");
386                 return NapiUtils::GetUndefined(context->GetEnv());
387             }
388         }
389         napi_value outData = nullptr;
390         napi_create_typedarray(context->GetEnv(), napi_uint8_array, context->remoteCert_.data.Length(), arrayBuffer, 0,
391                                &outData);
392         obj = NapiUtils::CreateObject(context->GetEnv());
393         if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) {
394             return NapiUtils::GetUndefined(context->GetEnv());
395         }
396         NapiUtils::SetNamedProperty(context->GetEnv(), obj, CERTIFICATA_DATA, outData);
397         NapiUtils::SetInt32Property(context->GetEnv(), obj, CERTIFICATA_ENCODING_FORMAT,
398                                     context->remoteCert_.encodingFormat);
399     }
400     return obj;
401 }
402 
GetProtocolCallback(TlsSocket::GetProtocolContext * context)403 napi_value TLSSocketServerExec::GetProtocolCallback(TlsSocket::GetProtocolContext *context)
404 {
405     return NapiUtils::CreateStringUtf8(context->GetEnv(), context->protocol_);
406 }
407 
GetSignatureAlgorithmsCallback(ServerGetSignatureAlgorithmsContext * context)408 napi_value TLSSocketServerExec::GetSignatureAlgorithmsCallback(ServerGetSignatureAlgorithmsContext *context)
409 {
410     napi_value signatureAlgorithms = NapiUtils::CreateArray(context->GetEnv(), 0);
411     int index = 0;
412     for (const auto &algorithm : context->signatureAlgorithms_) {
413         napi_value signatureAlgorithm = NapiUtils::CreateStringUtf8(context->GetEnv(), algorithm);
414         NapiUtils::SetArrayElement(context->GetEnv(), signatureAlgorithms, index++, signatureAlgorithm);
415     }
416     return signatureAlgorithms;
417 }
418 
SendCallback(TLSServerSendContext * context)419 napi_value TLSSocketServerExec::SendCallback(TLSServerSendContext *context)
420 {
421     context->Emit(EVENT_LISTENING, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()),
422                                                   NapiUtils::GetUndefined(context->GetEnv())));
423     return NapiUtils::GetUndefined(context->GetEnv());
424 }
425 
CloseCallback(TLSServerCloseContext * context)426 napi_value TLSSocketServerExec::CloseCallback(TLSServerCloseContext *context)
427 {
428     context->Emit(EVENT_CLOSE, std::make_pair(NapiUtils::GetUndefined(context->GetEnv()),
429                                               NapiUtils::GetUndefined(context->GetEnv())));
430     return NapiUtils::GetUndefined(context->GetEnv());
431 }
432 
StopCallback(TlsSocket::TLSNapiContext * context)433 napi_value TLSSocketServerExec::StopCallback(TlsSocket::TLSNapiContext *context)
434 {
435     return NapiUtils::GetUndefined(context->GetEnv());
436 }
437 
GetStateCallback(TlsSocket::TLSGetStateContext * context)438 napi_value TLSSocketServerExec::GetStateCallback(TlsSocket::TLSGetStateContext *context)
439 {
440     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
441     if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) {
442         return NapiUtils::GetUndefined(context->GetEnv());
443     }
444     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, KEY_IS_BOUND, context->state_.IsBound());
445     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, KEY_IS_CLOSE, context->state_.IsClose());
446     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, KEY_IS_CONNECTED, context->state_.IsConnected());
447     return obj;
448 }
449 
GetRemoteAddressCallback(ServerTLSGetRemoteAddressContext * context)450 napi_value TLSSocketServerExec::GetRemoteAddressCallback(ServerTLSGetRemoteAddressContext *context)
451 {
452     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
453     if (NapiUtils::GetValueType(context->GetEnv(), obj) != napi_object) {
454         return NapiUtils::GetUndefined(context->GetEnv());
455     }
456     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, KEY_ADDRESS, context->address_.GetAddress());
457     NapiUtils::SetUint32Property(context->GetEnv(), obj, KEY_FAMILY, context->address_.GetJsValueFamily());
458     NapiUtils::SetUint32Property(context->GetEnv(), obj, KEY_PORT, context->address_.GetPort());
459     return obj;
460 }
461 
SetExtraOptionsCallback(TlsSocket::TLSSetExtraOptionsContext * context)462 napi_value TLSSocketServerExec::SetExtraOptionsCallback(TlsSocket::TLSSetExtraOptionsContext *context)
463 {
464     return NapiUtils::GetUndefined(context->GetEnv());
465 }
466 
ExecConnectionSend(TLSServerSendContext * context)467 bool TLSSocketServerExec::ExecConnectionSend(TLSServerSendContext *context)
468 {
469     auto manager = context->GetManager();
470     if (manager == nullptr) {
471         NETSTACK_LOGE("manager is nullptr");
472         return false;
473     }
474     auto tlsSocketServer = reinterpret_cast<TLSSocketServer *>(manager->GetData());
475     if (tlsSocketServer == nullptr) {
476         NETSTACK_LOGE("ExecSend tlsSocketServer is null");
477         context->SetError(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND,
478                           TlsSocket::MakeErrorMessage(TlsSocket::TlsSocketError::TLS_ERR_NO_BIND));
479         return false;
480     }
481     TLSServerSendOptions tcpSendOptions;
482     int client_id = context->clientId_;
483 
484     tcpSendOptions.SetSocket(client_id);
485     tcpSendOptions.SetSendData(context->m_sendData);
486     tlsSocketServer->Send(tcpSendOptions, [&context](int32_t errorNumber) {
487         context->errorNumber_ = errorNumber;
488         if (errorNumber != TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS) {
489             context->SetError(errorNumber, TlsSocket::MakeErrorMessage(errorNumber));
490         }
491     });
492     return context->errorNumber_ == TlsSocket::TlsSocketError::TLSSOCKET_SUCCESS;
493 }
494 
ConnectionSendCallback(TLSServerSendContext * context)495 napi_value TLSSocketServerExec::ConnectionSendCallback(TLSServerSendContext *context)
496 {
497     return NapiUtils::GetUndefined(context->GetEnv());
498 }
499 } // namespace TlsSocketServer
500 } // namespace NetStack
501 } // namespace OHOS
502