2021-10-24 11:46:30 -04:00
|
|
|
#include "socket.js.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
|
2024-01-27 12:11:24 -05:00
|
|
|
#include "log.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "task.h"
|
|
|
|
#include "tls.h"
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "tlscontext.js.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2023-05-21 17:36:51 -04:00
|
|
|
#include "uv.h"
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef int promiseid_t;
|
2022-06-08 22:45:34 -04:00
|
|
|
typedef struct _socket_t socket_t;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
static JSClassID _classId;
|
|
|
|
static int _count;
|
|
|
|
static int _open_count;
|
|
|
|
static tf_tls_context_t* _defaultTlsContext;
|
|
|
|
|
2022-06-08 22:45:34 -04:00
|
|
|
static socket_t** _sockets;
|
|
|
|
static int _sockets_count;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef enum _socket_direction_t {
|
|
|
|
kUndetermined,
|
|
|
|
kAccept,
|
|
|
|
kConnect,
|
|
|
|
} socket_direction_t;
|
|
|
|
|
|
|
|
typedef struct _socket_t {
|
|
|
|
tf_task_t* _task;
|
|
|
|
uv_tcp_t _socket;
|
2023-09-20 19:30:29 -04:00
|
|
|
uv_timer_t _timer;
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_t* _tls;
|
|
|
|
promiseid_t _startTlsPromise;
|
|
|
|
promiseid_t _closePromise;
|
|
|
|
bool _connected;
|
|
|
|
bool _noDelay;
|
2022-06-02 06:58:22 -04:00
|
|
|
bool _reading;
|
|
|
|
bool _listening;
|
2023-09-20 19:30:29 -04:00
|
|
|
int _active;
|
2021-01-02 13:10:00 -05:00
|
|
|
char _peerName[256];
|
|
|
|
socket_direction_t _direction;
|
|
|
|
JSValue _object;
|
|
|
|
JSValue _onConnect;
|
|
|
|
JSValue _onRead;
|
|
|
|
JSValue _onError;
|
2022-06-08 22:45:34 -04:00
|
|
|
uint64_t created_ms;
|
2023-09-20 19:30:29 -04:00
|
|
|
uint64_t timeout_ms;
|
2021-01-02 13:10:00 -05:00
|
|
|
} socket_t;
|
|
|
|
|
|
|
|
static JSValue _socket_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static void _socket_finalizer(JSRuntime *runtime, JSValue value);
|
|
|
|
|
|
|
|
static JSValue _socket_startTls(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_stopTls(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_listen(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_close(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_shutdown(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_read(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_onError(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_write(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_isConnected(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_getPeerName(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_getPeerCertificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_getNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _socket_setNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2022-06-08 22:45:34 -04:00
|
|
|
static JSValue _sockets_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2023-09-20 19:30:29 -04:00
|
|
|
static JSValue _socket_setActivityTimeout(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
static void _socket_onClose(uv_handle_t* handle);
|
|
|
|
static void _socket_onShutdown(uv_shutdown_t* request, int status);
|
|
|
|
static void _socket_onResolvedForBind(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result);
|
|
|
|
static void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result);
|
|
|
|
static void _socket_onConnect(uv_connect_t* request, int status);
|
|
|
|
static void _socket_onNewConnection(uv_stream_t* server, int status);
|
|
|
|
static void _socket_allocateBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buffer);
|
|
|
|
static void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffer);
|
|
|
|
static void _socket_onWrite(uv_write_t* request, int status);
|
|
|
|
static void _socket_onTlsShutdown(uv_write_t* request, int status);
|
2023-09-20 19:30:29 -04:00
|
|
|
static void _socket_resetTimeout(socket_t* socket);
|
|
|
|
static void _socket_pauseTimeout(socket_t* socket);
|
|
|
|
static void _socket_resumeTimeout(socket_t* socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
static void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length);
|
|
|
|
static int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(socket_t* socket, promiseid_t promise, const char*, size_t), JSValue value, int* outLength);
|
|
|
|
static int _socket_writeInternal(socket_t* socket, promiseid_t promise, const char* data, size_t length);
|
|
|
|
static void _socket_processTlsShutdown(socket_t* socket, promiseid_t promise);
|
|
|
|
static void _socket_shutdownInternal(socket_t* socket, promiseid_t promise);
|
|
|
|
static bool _socket_processSomeOutgoingTls(socket_t* socket, promiseid_t promise, uv_write_cb callback);
|
|
|
|
static void _socket_processOutgoingTls(socket_t* socket);
|
|
|
|
static void _socket_reportTlsErrors(socket_t* socket);
|
|
|
|
static void _socket_reportError(socket_t* socket, const char* error);
|
|
|
|
|
2022-06-02 06:58:22 -04:00
|
|
|
static void _socket_set_handler(socket_t* socket, JSValue* handler, JSValue new_value)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
2022-09-21 20:38:26 -04:00
|
|
|
|
|
|
|
JSValue old_handler = *handler;
|
|
|
|
|
|
|
|
if (JS_IsUndefined(old_handler) && !JS_IsUndefined(new_value))
|
2022-06-02 06:58:22 -04:00
|
|
|
{
|
|
|
|
JS_DupValue(context, socket->_object);
|
|
|
|
}
|
2022-09-21 20:38:26 -04:00
|
|
|
|
|
|
|
*handler = JS_DupValue(context, new_value);
|
|
|
|
|
|
|
|
JS_FreeValue(context, old_handler);
|
|
|
|
|
|
|
|
if (!JS_IsUndefined(old_handler) && JS_IsUndefined(new_value))
|
2022-06-02 06:58:22 -04:00
|
|
|
{
|
|
|
|
JS_FreeValue(context, socket->_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 15:32:54 -04:00
|
|
|
static void _socket_gc_mark(JSRuntime* runtime, JSValueConst value, JS_MarkFunc mark_func)
|
|
|
|
{
|
|
|
|
socket_t* socket = JS_GetOpaque(value, _classId);
|
|
|
|
if (socket)
|
|
|
|
{
|
|
|
|
JS_MarkValue(runtime, socket->_onConnect, mark_func);
|
|
|
|
JS_MarkValue(runtime, socket->_onRead, mark_func);
|
|
|
|
JS_MarkValue(runtime, socket->_onError, mark_func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
JSValue tf_socket_register(JSContext* context)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_NewClassID(&_classId);
|
2023-12-20 20:27:57 -05:00
|
|
|
JSClassDef def =
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.class_name = "Socket",
|
|
|
|
.finalizer = &_socket_finalizer,
|
2022-05-30 15:32:54 -04:00
|
|
|
.gc_mark = _socket_gc_mark,
|
2021-01-02 13:10:00 -05:00
|
|
|
};
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _classId, &def) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "Failed to register Socket.\n");
|
|
|
|
}
|
2022-06-08 22:45:34 -04:00
|
|
|
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JS_SetPropertyStr(context, global, "getSockets", JS_NewCFunction(context, _sockets_get, "getSockets", 0));
|
|
|
|
JS_FreeValue(context, global);
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_NewCFunction2(context, _socket_create, "Socket", 0, JS_CFUNC_constructor, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_socket_get_count()
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _count;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_socket_get_open_count()
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _open_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _socket_resolve_data_t {
|
|
|
|
uv_getaddrinfo_t resolver;
|
|
|
|
socket_t* socket;
|
|
|
|
promiseid_t promise;
|
|
|
|
} socket_resolve_data_t;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
socket_t* _socket_create_internal(JSContext* context)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
socket_t* socket = tf_malloc(sizeof(socket_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(socket, 0, sizeof(*socket));
|
2022-06-21 19:09:13 -04:00
|
|
|
_sockets = tf_realloc(_sockets, sizeof(socket_t*) * (_sockets_count + 1));
|
2022-06-08 22:45:34 -04:00
|
|
|
_sockets[_sockets_count++] = socket;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
socket->_closePromise = -1;
|
|
|
|
socket->_startTlsPromise = -1;
|
|
|
|
|
|
|
|
++_count;
|
|
|
|
JSValue object = JS_NewObjectClass(context, _classId);
|
|
|
|
socket->_task = tf_task_get(context);
|
|
|
|
socket->_object = object;
|
|
|
|
JS_SetOpaque(object, socket);
|
|
|
|
|
2022-06-08 22:45:34 -04:00
|
|
|
socket->created_ms = uv_now(tf_task_get_loop(socket->_task));
|
|
|
|
|
2022-06-02 06:58:22 -04:00
|
|
|
socket->_onRead = JS_UNDEFINED;
|
|
|
|
socket->_onError = JS_UNDEFINED;
|
|
|
|
socket->_onConnect = JS_UNDEFINED;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "bind", JS_NewCFunction(context, _socket_bind, "bind", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _socket_connect, "connect", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "listen", JS_NewCFunction(context, _socket_listen, "listen", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "accept", JS_NewCFunction(context, _socket_accept, "accept", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "startTls", JS_NewCFunction(context, _socket_startTls, "startTls", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "stopTls", JS_NewCFunction(context, _socket_stopTls, "stopTls", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "shutdown", JS_NewCFunction(context, _socket_shutdown, "shutdown", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "close", JS_NewCFunction(context, _socket_close, "close", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "read", JS_NewCFunction(context, _socket_read, "read", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "onError", JS_NewCFunction(context, _socket_onError, "onError", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "write", JS_NewCFunction(context, _socket_write, "write", 1));
|
2023-09-20 19:30:29 -04:00
|
|
|
JS_SetPropertyStr(context, object, "setActivityTimeout", JS_NewCFunction(context, _socket_setActivityTimeout, "setActivityTimeout", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
JSAtom atom = JS_NewAtom(context, "isConnected");
|
|
|
|
JS_DefinePropertyGetSet(context, object, atom, JS_NewCFunction(context, _socket_isConnected, "isConnected", 0), JS_NULL, 0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
atom = JS_NewAtom(context, "peerName");
|
|
|
|
JS_DefinePropertyGetSet(context, object, atom, JS_NewCFunction(context, _socket_getPeerName, "peerName", 0), JS_NULL, 0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
atom = JS_NewAtom(context, "peerCertificate");
|
|
|
|
JS_DefinePropertyGetSet(context, object, atom, JS_NewCFunction(context, _socket_getPeerCertificate, "peerCertificate", 0), JS_NULL, 0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
atom = JS_NewAtom(context, "noDelay");
|
|
|
|
JSValue get_no_delay = JS_NewCFunction(context, _socket_getNoDelay, "getNoDelay", 0);
|
|
|
|
JSValue set_no_delay = JS_NewCFunction(context, _socket_setNoDelay, "setNoDelay", 1);
|
|
|
|
JS_DefinePropertyGetSet(context, object, atom, get_no_delay, set_no_delay, 0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
++_open_count;
|
|
|
|
uv_tcp_init(tf_task_get_loop(socket->_task), &socket->_socket);
|
|
|
|
socket->_socket.data = socket;
|
2023-09-20 19:30:29 -04:00
|
|
|
uv_timer_init(tf_task_get_loop(socket->_task), &socket->_timer);
|
|
|
|
socket->_timer.data = socket;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _socket_create_internal(context)->_object;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_close_internal(socket_t* socket)
|
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
_socket_set_handler(socket, &socket->_onRead, JS_UNDEFINED);
|
|
|
|
_socket_set_handler(socket, &socket->_onError, JS_UNDEFINED);
|
|
|
|
_socket_set_handler(socket, &socket->_onConnect, JS_UNDEFINED);
|
2022-02-05 12:06:51 -05:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
|
|
|
tf_tls_session_destroy(socket->_tls);
|
|
|
|
socket->_tls = NULL;
|
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
if (socket->_socket.data &&
|
|
|
|
!uv_is_closing((uv_handle_t*)&socket->_socket))
|
2022-02-05 12:06:51 -05:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_close((uv_handle_t*)&socket->_socket, _socket_onClose);
|
|
|
|
}
|
2023-09-20 19:30:29 -04:00
|
|
|
if (socket->_timer.data &&
|
|
|
|
!uv_is_closing((uv_handle_t*)&socket->_timer))
|
|
|
|
{
|
|
|
|
uv_close((uv_handle_t*)&socket->_timer, _socket_onClose);
|
|
|
|
}
|
2022-02-05 12:06:51 -05:00
|
|
|
|
|
|
|
if (!socket->_socket.data &&
|
2023-09-20 19:30:29 -04:00
|
|
|
!socket->_timer.data &&
|
2022-02-05 12:06:51 -05:00
|
|
|
JS_IsUndefined(socket->_object))
|
|
|
|
{
|
|
|
|
--_count;
|
2022-06-08 22:45:34 -04:00
|
|
|
for (int i = 0; i < _sockets_count; i++)
|
|
|
|
{
|
|
|
|
if (_sockets[i] == socket)
|
|
|
|
{
|
|
|
|
_sockets[i] = _sockets[_sockets_count - 1];
|
|
|
|
--_sockets_count;
|
2022-06-26 14:25:31 -04:00
|
|
|
_sockets = tf_realloc(_sockets, sizeof(socket_t*) * _sockets_count);
|
2022-06-08 22:45:34 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(socket);
|
2022-02-05 12:06:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 20:27:57 -05:00
|
|
|
void _socket_finalizer(JSRuntime* runtime, JSValue value)
|
2022-02-05 12:06:51 -05:00
|
|
|
{
|
|
|
|
socket_t* socket = JS_GetOpaque(value, _classId);
|
|
|
|
socket->_object = JS_UNDEFINED;
|
|
|
|
_socket_close_internal(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_reportError(socket_t* socket, const char* error)
|
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
|
|
|
if (JS_IsFunction(context, socket-> _onError))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue exception = JS_ThrowInternalError(context, "%s", error);
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue cb_ref = JS_DupValue(context, socket->_onError);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue result = JS_Call(context, socket->_onError, socket->_object, 1, &exception);
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, cb_ref);
|
2022-06-02 06:58:22 -04:00
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, exception);
|
|
|
|
JS_FreeValue(context, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "Socket::reportError: %s\n", error);
|
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_reportTlsErrors(socket_t* socket)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char buffer[4096];
|
2021-10-10 17:51:38 -04:00
|
|
|
while (socket->_tls && tf_tls_session_get_error(socket->_tls, buffer, sizeof(buffer)))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_reportError(socket, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_startTls(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_context_t* context = 0;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (argc > 0 && JS_IsObject(argv[0]))
|
|
|
|
{
|
2021-10-24 11:46:30 -04:00
|
|
|
context = tf_tls_context_get(argv[0]);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!_defaultTlsContext)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_defaultTlsContext = tf_tls_context_create();
|
|
|
|
}
|
|
|
|
context = _defaultTlsContext;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (context)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket->_tls = tf_tls_context_create_session(context);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_set_hostname(socket->_tls, socket->_peerName);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_direction == kAccept)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_start_accept(socket->_tls);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (socket->_direction == kConnect)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_start_connect(socket->_tls);
|
|
|
|
}
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue result = tf_task_allocate_promise(socket->_task, &socket->_startTlsPromise);
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_processOutgoingTls(socket);
|
|
|
|
return result;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_ThrowInternalError(tf_task_get_context(socket->_task), "Failed to get TLS context");
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_ThrowInternalError(tf_task_get_context(socket->_task), "startTls with TLS already started");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_stopTls(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_processOutgoingTls(socket);
|
|
|
|
tf_tls_session_destroy(socket->_tls);
|
|
|
|
socket->_tls = NULL;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_ThrowInternalError(tf_task_get_context(socket->_task), "stopTls with TLS already stopped");
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
bool _socket_processSomeOutgoingTls(socket_t* socket, promiseid_t promise, uv_write_cb callback)
|
|
|
|
{
|
2023-01-18 18:03:17 -05:00
|
|
|
if (!socket->_socket.data)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
char buffer[65536];
|
|
|
|
int result = tf_tls_session_read_encrypted(socket->_tls, buffer, sizeof(buffer));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result > 0)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
char* request_buffer = tf_malloc(sizeof(uv_write_t) + result);
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_write_t* request = (uv_write_t*)request_buffer;
|
|
|
|
memset(request, 0, sizeof(*request));
|
|
|
|
request->data = (void*)(intptr_t)(promise);
|
|
|
|
char* rawBuffer = request_buffer + sizeof(uv_write_t);
|
|
|
|
memcpy(rawBuffer, buffer, result);
|
|
|
|
|
|
|
|
uv_buf_t writeBuffer =
|
|
|
|
{
|
|
|
|
.base = rawBuffer,
|
|
|
|
.len = result,
|
|
|
|
};
|
|
|
|
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_pauseTimeout(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
int writeResult = uv_write(request, (uv_stream_t*)&socket->_socket, &writeBuffer, 1, callback);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (writeResult != 0)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request_buffer);
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_write: %s", uv_strerror(writeResult));
|
|
|
|
_socket_reportError(socket, error);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_reportTlsErrors(socket);
|
|
|
|
}
|
|
|
|
return result > 0;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_processOutgoingTls(socket_t* socket)
|
|
|
|
{
|
|
|
|
while (_socket_processSomeOutgoingTls(socket, -1, _socket_onWrite))
|
|
|
|
{
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
const char* node = JS_ToCString(tf_task_get_context(socket->_task), argv[0]);
|
|
|
|
const char* port = JS_ToCString(tf_task_get_context(socket->_task), argv[1]);
|
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
socket_resolve_data_t* data = tf_malloc(sizeof(socket_resolve_data_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(data, 0, sizeof(*data));
|
2023-07-16 10:04:45 -04:00
|
|
|
struct addrinfo hints =
|
|
|
|
{
|
|
|
|
.ai_family = AF_UNSPEC,
|
2021-01-02 13:10:00 -05:00
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
.ai_flags = 0,
|
|
|
|
};
|
|
|
|
data->resolver.data = data;
|
|
|
|
data->socket = socket;
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue promise = tf_task_allocate_promise(socket->_task, &data->promise);
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = uv_getaddrinfo(tf_task_get_loop(socket->_task), &data->resolver, _socket_onResolvedForBind, node, port, &hints);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "uv_getaddrinfo: %s", uv_strerror(result)));
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-01-13 22:05:37 -05:00
|
|
|
return promise;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onResolvedForBind(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_resolve_data_t* data = (socket_resolve_data_t*)resolver->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "uv_getaddrinfo: %s", uv_strerror(status)));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int bindResult = uv_tcp_bind(&data->socket->_socket, result->ai_addr, 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (bindResult != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "uv_tcp_bind: %s", uv_strerror(bindResult)));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-18 08:28:48 -04:00
|
|
|
struct sockaddr_storage addr = { 0 };
|
|
|
|
int port = 0;
|
|
|
|
int size = (int)sizeof(addr);
|
|
|
|
if (uv_tcp_getsockname(&data->socket->_socket, (struct sockaddr*)&addr, &size) == 0)
|
|
|
|
{
|
|
|
|
if (addr.ss_family == AF_INET)
|
|
|
|
{
|
|
|
|
port = ntohs(((struct sockaddr_in*)&addr)->sin_port);
|
|
|
|
}
|
|
|
|
else if (addr.ss_family == AF_INET6)
|
|
|
|
{
|
|
|
|
port = ntohs(((struct sockaddr_in6*)&addr)->sin6_port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tf_task_resolve_promise(data->socket->_task, data->promise, JS_NewInt32(tf_task_get_context(data->socket->_task), port));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
socket->_direction = kConnect;
|
|
|
|
const char* node = JS_ToCString(context, argv[0]);
|
|
|
|
const char* port = JS_ToCString(context, argv[1]);
|
|
|
|
|
2022-06-08 22:45:34 -04:00
|
|
|
snprintf(socket->_peerName, sizeof(socket->_peerName), "%s", node);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
socket_resolve_data_t* data = tf_malloc(sizeof(socket_resolve_data_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = PF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
};
|
|
|
|
data->resolver.data = data;
|
|
|
|
data->socket = socket;
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue promise = tf_task_allocate_promise(socket->_task, &data->promise);
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = uv_getaddrinfo(tf_task_get_loop(socket->_task), &data->resolver, _socket_onResolvedForConnect, node, port, &hints);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
|
2022-01-13 22:05:37 -05:00
|
|
|
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error));
|
2022-07-09 10:14:48 -04:00
|
|
|
_socket_close_internal(socket);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_FreeCString(context, node);
|
|
|
|
JS_FreeCString(context, port);
|
2022-01-13 22:05:37 -05:00
|
|
|
return promise;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_resolve_data_t* data = resolver->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(status));
|
|
|
|
tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "%s", error));
|
2022-07-09 10:14:48 -04:00
|
|
|
_socket_close_internal(data->socket);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
uv_connect_t* request = tf_malloc(sizeof(uv_connect_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(request, 0, sizeof(*request));
|
|
|
|
request->data = (void*)(intptr_t)data->promise;
|
|
|
|
int connectResult = uv_tcp_connect(request, &data->socket->_socket, result->ai_addr, _socket_onConnect);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (connectResult != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(connectResult));
|
|
|
|
tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), "%s", error));
|
2022-07-09 10:14:48 -04:00
|
|
|
_socket_close_internal(data->socket);
|
2022-06-20 10:41:08 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
uv_freeaddrinfo(result);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onConnect(uv_connect_t* request, int status)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = (intptr_t)request->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (promise != -1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = request->handle->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (status == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket->_connected = true;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_NewInt32(tf_task_get_context(socket->_task), status));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_tcp_connect: %s", uv_strerror(status));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
|
2022-07-09 10:14:48 -04:00
|
|
|
_socket_close_internal(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_listen(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-06-02 06:58:22 -04:00
|
|
|
socket->_listening = true;
|
2022-09-21 20:38:26 -04:00
|
|
|
int backlog = 16;
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_ToInt32(context, &backlog, argv[0]);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsUndefined(socket->_onConnect))
|
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
_socket_set_handler(socket, &socket->_onConnect, argv[1]);
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = uv_listen((uv_stream_t*)&socket->_socket, backlog, _socket_onNewConnection);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
return JS_ThrowInternalError(context, "uv_listen: %s", uv_strerror(result));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
return JS_NewInt32(context, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_ThrowInternalError(context, "listen: Already listening.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onNewConnection(uv_stream_t* server, int status)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = server->data;
|
2022-06-02 06:58:22 -04:00
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(socket->_onConnect))
|
|
|
|
{
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue cb_ref = JS_DupValue(context, socket->_onConnect);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue result = JS_Call(context, socket->_onConnect, socket->_object, 0, NULL);
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, cb_ref);
|
2022-06-02 06:58:22 -04:00
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
|
|
|
|
socket_t* client = _socket_create_internal(context);
|
|
|
|
client->_direction = kAccept;
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise;
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue ref = JS_DupValue(context, client->_object);
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue result = tf_task_allocate_promise(socket->_task, &promise);
|
2021-01-02 13:10:00 -05:00
|
|
|
int status = uv_accept((uv_stream_t*)&socket->_socket, (uv_stream_t*)&client->_socket);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (status == 0)
|
|
|
|
{
|
2023-07-16 10:04:45 -04:00
|
|
|
struct sockaddr_storage name = { 0 };
|
2022-06-08 22:45:34 -04:00
|
|
|
int namelen = (int)sizeof(name);
|
|
|
|
if (uv_tcp_getpeername(&client->_socket, (struct sockaddr*)&name, &namelen) == 0)
|
|
|
|
{
|
|
|
|
uv_ip_name((const struct sockaddr*)&name, client->_peerName, sizeof(client->_peerName));
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
client->_connected = true;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, client->_object);
|
2023-01-21 15:12:41 -05:00
|
|
|
JS_FreeValue(context, client->_object);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "uv_accept: %s", uv_strerror(status)));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_close(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-06-06 22:41:44 -04:00
|
|
|
if (socket->_closePromise == -1 &&
|
|
|
|
socket->_socket.data &&
|
|
|
|
!uv_is_closing((uv_handle_t*)&socket->_socket))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue result = tf_task_allocate_promise(socket->_task, &socket->_closePromise);
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_close_internal(socket);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_shutdown(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue result = tf_task_allocate_promise(socket->_task, &promise);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_processTlsShutdown(socket, promise);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_shutdownInternal(socket, promise);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_shutdownInternal(socket_t* socket, promiseid_t promise)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
uv_shutdown_t* request = tf_malloc(sizeof(uv_shutdown_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(request, 0, sizeof(*request));
|
|
|
|
request->data = (void*)(intptr_t)promise;
|
|
|
|
int result = uv_shutdown(request, (uv_stream_t*)&socket->_socket, _socket_onShutdown);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_shutdown: %s", uv_strerror(result));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_processTlsShutdown(socket_t* socket, promiseid_t promise)
|
|
|
|
{
|
2022-05-20 20:06:01 -04:00
|
|
|
if (!socket->_tls)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_shutdownInternal(socket, promise);
|
|
|
|
}
|
2022-05-20 20:06:01 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tf_tls_session_shutdown(socket->_tls);
|
|
|
|
if (!_socket_processSomeOutgoingTls(socket, promise, _socket_onTlsShutdown))
|
|
|
|
{
|
|
|
|
_socket_shutdownInternal(socket, promise);
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onTlsShutdown(uv_write_t* request, int status)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = request->handle->data;
|
|
|
|
promiseid_t promise = (intptr_t)request->data;
|
|
|
|
_socket_processTlsShutdown(socket, promise);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_onError(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-06-02 06:58:22 -04:00
|
|
|
_socket_set_handler(socket, &socket->_onError, argv[0]);
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_read(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
|
|
|
_socket_set_handler(socket, &socket->_onRead, argv[0]);
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue read_result = tf_task_allocate_promise(socket->_task, &promise);
|
2023-01-18 18:03:17 -05:00
|
|
|
if (!socket->_reading && socket->_socket.data)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_resetTimeout(socket);
|
2022-06-02 06:58:22 -04:00
|
|
|
int result = uv_read_start((uv_stream_t*)&socket->_socket, _socket_allocateBuffer, _socket_onRead);
|
|
|
|
if (result != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "uv_read_start: %s", uv_strerror(result)));
|
2022-06-02 06:58:22 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
socket->_reading = true;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
return read_result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_allocateBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
*buf = uv_buf_init(tf_malloc(suggestedSize), suggestedSize);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onRead(uv_stream_t* stream, ssize_t readSize, const uv_buf_t* buffer)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = stream->data;
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_resetTimeout(socket);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (readSize <= 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket->_connected = false;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(socket->_onRead))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue args[] = { JS_UNDEFINED };
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, cb_ref);
|
2022-06-02 06:58:22 -04:00
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
_socket_close_internal(socket);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_reportTlsErrors(socket);
|
|
|
|
tf_tls_session_write_encrypted(socket->_tls, buffer->base, readSize);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_startTlsPromise != -1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_handshake_t result = tf_tls_session_handshake(socket->_tls);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result == k_tls_handshake_done)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = socket->_startTlsPromise;
|
|
|
|
socket->_startTlsPromise = -1;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (result == k_tls_handshake_failed)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = socket->_startTlsPromise;
|
|
|
|
socket->_startTlsPromise = -1;
|
|
|
|
char buffer[8192];
|
2021-10-10 17:51:38 -04:00
|
|
|
if (tf_tls_session_get_error(socket->_tls, buffer, sizeof(buffer)))
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "%s", buffer));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_UNDEFINED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 06:58:22 -04:00
|
|
|
while (socket->_tls)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char plain[8192];
|
|
|
|
int result = tf_tls_session_read_plain(socket->_tls, plain, sizeof(plain));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result > 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_notifyDataRead(socket, plain, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (result == k_tls_read_failed)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_reportTlsErrors(socket);
|
|
|
|
_socket_close_internal(socket);
|
|
|
|
break;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (result == k_tls_read_zero)
|
|
|
|
{
|
|
|
|
if (!JS_IsUndefined(socket->_onRead))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue args[] = { JS_UNDEFINED };
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, cb_ref);
|
2022-06-02 06:58:22 -04:00
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-06-04 11:43:35 -04:00
|
|
|
_socket_close_internal(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_processOutgoingTls(socket);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_notifyDataRead(socket, buffer->base, readSize);
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(buffer->base);
|
2022-06-02 06:58:22 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length)
|
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
if (data && length > 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-06-02 06:58:22 -04:00
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
2023-01-28 16:59:36 -05:00
|
|
|
JSValue typedArray = tf_util_new_uint8_array(context, (const uint8_t*)data, length);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue args[] = { typedArray };
|
|
|
|
if (!JS_IsUndefined(socket->_onRead))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue cb_ref = JS_DupValue(context, socket->_onRead);
|
2022-06-02 06:58:22 -04:00
|
|
|
JSValue result = JS_Call(context, socket->_onRead, socket->_object, 1, args);
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, cb_ref);
|
2022-06-02 06:58:22 -04:00
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-06-02 06:58:22 -04:00
|
|
|
JS_FreeValue(context, typedArray);
|
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int _socket_writeBytes(socket_t* socket, promiseid_t promise, int (*callback)(socket_t* socket, promiseid_t promise, const char*, size_t), JSValue value, int* outLength)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = -1;
|
|
|
|
size_t length;
|
|
|
|
uint8_t* array = NULL;
|
|
|
|
JSContext* context = tf_task_get_context(socket->_task);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsString(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* stringValue = JS_ToCStringLen(context, &length, value);
|
|
|
|
result = callback(socket, promise, stringValue, length);
|
|
|
|
JS_FreeCString(context, stringValue);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
2021-11-03 18:15:46 -04:00
|
|
|
else if ((array = tf_util_try_get_array_buffer(context, &length, value)) != 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = callback(socket, promise, (const char*)array, length);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t offset;
|
|
|
|
size_t element_size;
|
2021-11-03 18:15:46 -04:00
|
|
|
JSValue buffer = tf_util_try_get_typed_array_buffer(context, value, &offset, &length, &element_size);
|
2022-06-26 14:25:31 -04:00
|
|
|
if (!JS_IsException(buffer))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-06-26 14:25:31 -04:00
|
|
|
size_t size;
|
|
|
|
if ((array = tf_util_try_get_array_buffer(context, &size, buffer)) != 0)
|
|
|
|
{
|
|
|
|
result = callback(socket, promise, (const char*)array, length);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (outLength)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
*outLength = (int)length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int _socket_writeInternal(socket_t* socket, promiseid_t promise, const char* data, size_t length)
|
|
|
|
{
|
2023-01-18 18:03:17 -05:00
|
|
|
if (!socket->_socket.data)
|
|
|
|
{
|
2023-01-18 18:14:44 -05:00
|
|
|
return UV_ENOTCONN;
|
2023-01-18 18:03:17 -05:00
|
|
|
}
|
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
char* rawBuffer = tf_malloc(sizeof(uv_write_t) + length);
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_write_t* request = (uv_write_t*)rawBuffer;
|
|
|
|
memcpy(rawBuffer + sizeof(uv_write_t), data, length);
|
|
|
|
|
2022-06-20 10:30:00 -04:00
|
|
|
uv_buf_t buffer =
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.base = rawBuffer + sizeof(uv_write_t),
|
|
|
|
.len = length,
|
|
|
|
};
|
|
|
|
|
|
|
|
request->data = (void*)(intptr_t)promise;
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_pauseTimeout(socket);
|
2022-06-20 10:30:00 -04:00
|
|
|
int result = uv_write(request, (uv_stream_t*)&socket->_socket, &buffer, 1, _socket_onWrite);
|
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
tf_free(rawBuffer);
|
|
|
|
}
|
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static int _socket_write_tls(socket_t* socket, promiseid_t promise, const char* data, size_t size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return tf_tls_session_write_plain(socket->_tls, data, size);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_write(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue write_result = tf_task_allocate_promise(socket->_task, &promise);
|
2022-09-21 20:38:26 -04:00
|
|
|
JSValue ref = JS_DupValue(context, socket->_object);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(argv[0]))
|
|
|
|
{
|
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_reportTlsErrors(socket);
|
|
|
|
int length = 0;
|
|
|
|
int result = _socket_writeBytes(socket, -1, _socket_write_tls, argv[0], &length);
|
|
|
|
char buffer[8192];
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result <= 0 && tf_tls_session_get_error(socket->_tls, buffer, sizeof(buffer)))
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "%s", buffer));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (result < length)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_NewInt32(context, result));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_NewInt32(context, result));
|
|
|
|
}
|
|
|
|
_socket_processOutgoingTls(socket);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int length;
|
|
|
|
int result = _socket_writeBytes(socket, promise, _socket_writeInternal, argv[0], &length);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result != 0)
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "uv_write: %s", uv_strerror(result)));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_NewInt32(context, -2));
|
|
|
|
}
|
2022-09-21 20:38:26 -04:00
|
|
|
JS_FreeValue(context, ref);
|
2021-01-02 13:10:00 -05:00
|
|
|
return write_result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onWrite(uv_write_t* request, int status)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = request->handle->data;
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_resumeTimeout(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = (intptr_t)request->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (promise != -1)
|
|
|
|
{
|
|
|
|
if (status == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_NewInt32(tf_task_get_context(socket->_task), status));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-09 21:42:15 -04:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "uv_write: %s", uv_strerror(status)));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2023-09-20 19:30:29 -04:00
|
|
|
static void _socket_timeout(uv_timer_t* timer)
|
|
|
|
{
|
|
|
|
socket_t* socket = timer->data;
|
|
|
|
_socket_close_internal(socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue _socket_setActivityTimeout(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
int64_t timeout = 0;
|
|
|
|
if (JS_ToInt64(context, &timeout, argv[0]) == 0 &&
|
|
|
|
socket->timeout_ms > 0) {
|
|
|
|
socket->timeout_ms = timeout;
|
|
|
|
uv_timer_start(&socket->_timer, _socket_timeout, socket->timeout_ms, 0);
|
|
|
|
} else {
|
|
|
|
uv_timer_stop(&socket->_timer);
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_isConnected(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
return socket->_connected ? JS_TRUE : JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onClose(uv_handle_t* handle)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
--_open_count;
|
|
|
|
socket_t* socket = handle->data;
|
2022-02-05 12:06:51 -05:00
|
|
|
handle->data = NULL;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_closePromise != -1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = socket->_closePromise;
|
|
|
|
socket->_closePromise = -1;
|
|
|
|
socket->_connected = false;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
|
|
|
}
|
2022-02-05 12:06:51 -05:00
|
|
|
if (socket->_startTlsPromise != -1)
|
|
|
|
{
|
|
|
|
promiseid_t promise = socket->_startTlsPromise;
|
|
|
|
socket->_startTlsPromise = -1;
|
|
|
|
socket->_connected = false;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
|
|
|
}
|
|
|
|
_socket_close_internal(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_onShutdown(uv_shutdown_t* request, int status)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = request->handle->data;
|
2023-09-20 19:30:29 -04:00
|
|
|
_socket_resetTimeout(socket);
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promise = (intptr_t)request->data;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (status == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(socket->_task, promise, JS_UNDEFINED);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_shutdown: %s", uv_strerror(status));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error));
|
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(request);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_getPeerName(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
int nameLength = sizeof(addr);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (uv_tcp_getpeername(&socket->_socket, (struct sockaddr*)&addr, &nameLength) == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char name[1024];
|
2021-10-10 17:51:38 -04:00
|
|
|
if ((size_t)nameLength > sizeof(struct sockaddr_in))
|
|
|
|
{
|
|
|
|
if (uv_ip6_name(&addr, name, sizeof(name)) == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_NewString(context, name);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (uv_ip4_name((struct sockaddr_in*)&addr, name, sizeof(name)) == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_NewString(context, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_getPeerCertificate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char buffer[128 * 1024];
|
|
|
|
int result = tf_tls_session_get_peer_certificate(socket->_tls, buffer, sizeof(buffer));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result > 0)
|
|
|
|
{
|
2023-01-28 16:59:36 -05:00
|
|
|
return tf_util_new_uint8_array(tf_task_get_context(socket->_task), (const uint8_t*)buffer, sizeof(buffer));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_getNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
return JS_NewBool(context, socket->_noDelay);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _socket_setNoDelay(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(this_val, _classId);
|
|
|
|
int result = JS_ToBool(context, argv[0]);
|
|
|
|
socket->_noDelay = result > 0;
|
|
|
|
uv_tcp_nodelay(&socket->_socket, result > 0 ? 1 : 0);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
2022-06-08 22:45:34 -04:00
|
|
|
|
|
|
|
JSValue _sockets_get(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue array = JS_NewArray(context);
|
|
|
|
for (int i = 0; i < _sockets_count; i++)
|
|
|
|
{
|
|
|
|
socket_t* s = _sockets[i];
|
|
|
|
JSValue entry = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, entry, "peer", JS_NewString(context, s->_peerName));
|
|
|
|
JS_SetPropertyStr(context, entry, "listening", JS_NewBool(context, s->_listening));
|
|
|
|
JS_SetPropertyStr(context, entry, "connected", JS_NewBool(context, s->_connected));
|
|
|
|
JS_SetPropertyStr(context, entry, "tls", JS_NewBool(context, s->_tls != NULL));
|
2023-04-30 07:48:16 -04:00
|
|
|
JS_SetPropertyStr(context, entry, "age_seconds", JS_NewFloat64(context, (uv_now(tf_task_get_loop(s->_task)) - s->created_ms) / 1000.0));
|
2022-06-08 22:45:34 -04:00
|
|
|
JS_SetPropertyUint32(context, array, i, entry);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2023-09-20 19:30:29 -04:00
|
|
|
|
|
|
|
static void _socket_resetTimeout(socket_t* socket)
|
|
|
|
{
|
|
|
|
if (socket->timeout_ms && socket->_active == 0)
|
|
|
|
{
|
|
|
|
uv_timer_start(&socket->_timer, _socket_timeout, socket->timeout_ms, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _socket_pauseTimeout(socket_t* socket)
|
|
|
|
{
|
|
|
|
if (socket->_active++ == 1)
|
|
|
|
{
|
|
|
|
uv_timer_stop(&socket->_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _socket_resumeTimeout(socket_t* socket)
|
|
|
|
{
|
|
|
|
if (--socket->_active == 0 && socket->timeout_ms > 0)
|
|
|
|
{
|
|
|
|
uv_timer_start(&socket->_timer, _socket_timeout, socket->timeout_ms, 0);
|
|
|
|
}
|
|
|
|
}
|