2021-01-02 13:10:00 -05:00
|
|
|
#include "socket.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "tls.h"
|
|
|
|
#include "tlscontextwrapper.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
typedef int promiseid_t;
|
|
|
|
|
|
|
|
static JSClassID _classId;
|
|
|
|
static int _count;
|
|
|
|
static int _open_count;
|
|
|
|
static tf_tls_context_t* _defaultTlsContext;
|
|
|
|
|
|
|
|
typedef enum _socket_direction_t {
|
|
|
|
kUndetermined,
|
|
|
|
kAccept,
|
|
|
|
kConnect,
|
|
|
|
} socket_direction_t;
|
|
|
|
|
|
|
|
typedef struct _socket_t {
|
|
|
|
tf_task_t* _task;
|
|
|
|
uv_tcp_t _socket;
|
|
|
|
tf_tls_session_t* _tls;
|
|
|
|
promiseid_t _startTlsPromise;
|
|
|
|
promiseid_t _closePromise;
|
|
|
|
bool _connected;
|
|
|
|
bool _noDelay;
|
|
|
|
char _peerName[256];
|
|
|
|
socket_direction_t _direction;
|
|
|
|
JSValue _object;
|
|
|
|
JSValue _onConnect;
|
|
|
|
JSValue _onRead;
|
|
|
|
JSValue _onError;
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
class Socket {
|
|
|
|
private:
|
|
|
|
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue tf_socket_init(JSContext* context)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_NewClassID(&_classId);
|
|
|
|
JSClassDef def = {
|
|
|
|
.class_name = "Socket",
|
|
|
|
.finalizer = &_socket_finalizer,
|
|
|
|
};
|
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");
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = malloc(sizeof(socket_t));
|
|
|
|
memset(socket, 0, sizeof(*socket));
|
|
|
|
|
|
|
|
socket->_onRead = JS_UNDEFINED;
|
|
|
|
socket->_onError = JS_UNDEFINED;
|
|
|
|
socket->_onConnect = JS_UNDEFINED;
|
|
|
|
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);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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_finalizer(JSRuntime *runtime, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket_t* socket = JS_GetOpaque(value, _classId);
|
|
|
|
--_count;
|
|
|
|
free(socket);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_close_internal(socket_t* socket)
|
|
|
|
{
|
|
|
|
if (!uv_is_closing((uv_handle_t*)&socket->_socket))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), socket->_onRead);
|
|
|
|
socket->_onRead = JS_UNDEFINED;
|
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), socket->_onError);
|
|
|
|
socket->_onError = JS_UNDEFINED;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_tls)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_destroy(socket->_tls);
|
|
|
|
socket->_tls = NULL;
|
|
|
|
}
|
|
|
|
uv_close((uv_handle_t*)&socket->_socket, _socket_onClose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_reportError(socket_t* socket, const char* error)
|
|
|
|
{
|
|
|
|
if (JS_IsFunction(tf_task_get_context(socket->_task),socket-> _onError))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue exception = JS_ThrowInternalError(tf_task_get_context(socket->_task), "%s", error);
|
|
|
|
JSValue result = JS_Call(tf_task_get_context(socket->_task), socket->_onError, socket->_object, 1, &exception);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Socket error.\n");
|
|
|
|
js_std_dump_error(tf_task_get_context(socket->_task));
|
|
|
|
}
|
|
|
|
tf_task_run_jobs(socket->_task);
|
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), exception);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-02 13:10:00 -05:00
|
|
|
context = tf_tls_context_wrapper_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);
|
|
|
|
}
|
|
|
|
socket->_startTlsPromise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue result = tf_task_get_promise(socket->_task, socket->_startTlsPromise);
|
|
|
|
_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)
|
|
|
|
{
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char* request_buffer = malloc(sizeof(uv_write_t) + result);
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
int writeResult = uv_write(request, (uv_stream_t*)&socket->_socket, &writeBuffer, 1, callback);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (writeResult != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
free(request_buffer);
|
|
|
|
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]);
|
|
|
|
|
|
|
|
socket_resolve_data_t* data = malloc(sizeof(socket_resolve_data_t));
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = PF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
.ai_flags = 0,
|
|
|
|
};
|
|
|
|
data->resolver.data = data;
|
|
|
|
data->socket = socket;
|
|
|
|
data->promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
|
|
|
|
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), error));
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
return tf_task_get_promise(socket->_task, data->promise);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
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), error));
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_tcp_bind: %s", uv_strerror(bindResult));
|
|
|
|
tf_task_reject_promise(data->socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(data->socket->_task), error));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(data->socket->_task, data->promise, JS_UNDEFINED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
strncpy(socket->_peerName, node, sizeof(socket->_peerName) - 1);
|
|
|
|
|
|
|
|
promiseid_t promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
|
|
|
|
socket_resolve_data_t* data = malloc(sizeof(socket_resolve_data_t));
|
|
|
|
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;
|
|
|
|
data->promise = promise;
|
|
|
|
|
|
|
|
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));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "%s", error));
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FreeCString(context, node);
|
|
|
|
JS_FreeCString(context, port);
|
|
|
|
return tf_task_get_promise(socket->_task, promise);
|
|
|
|
}
|
|
|
|
|
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));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_connect_t* request = malloc(sizeof(uv_connect_t));
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uv_freeaddrinfo(result);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
int backlog = 1;
|
|
|
|
JS_ToInt32(context, &backlog, argv[0]);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsUndefined(socket->_onConnect))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket->_onConnect = JS_DupValue(context, argv[1]);
|
|
|
|
int result = uv_listen((uv_stream_t*)&socket->_socket, backlog, _socket_onNewConnection);
|
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_listen: %s", uv_strerror(result));
|
|
|
|
return JS_ThrowInternalError(context, error);
|
|
|
|
}
|
|
|
|
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;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(socket->_onConnect))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_Call(tf_task_get_context(socket->_task), socket->_onConnect, socket->_object, 0, NULL);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Socket error on connection.\n");
|
|
|
|
js_std_dump_error(tf_task_get_context(socket->_task));
|
|
|
|
}
|
|
|
|
tf_task_run_jobs(socket->_task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
promiseid_t promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue result = tf_task_get_promise(socket->_task, promise);
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
client->_connected = true;
|
|
|
|
tf_task_resolve_promise(socket->_task, promise, client->_object);
|
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_accept: %s", uv_strerror(status));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, error));
|
|
|
|
}
|
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (socket->_closePromise == -1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
socket->_closePromise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue result = tf_task_get_promise(socket->_task, socket->_closePromise);
|
|
|
|
_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);
|
|
|
|
promiseid_t promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue result = tf_task_get_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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_shutdown_t* request = malloc(sizeof(uv_shutdown_t));
|
|
|
|
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));
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_processTlsShutdown(socket_t* socket, promiseid_t promise)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_session_shutdown(socket->_tls);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!_socket_processSomeOutgoingTls(socket, promise, _socket_onTlsShutdown))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_socket_shutdownInternal(socket, promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
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);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(socket->_onError))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, socket->_onError);
|
|
|
|
socket->_onError = JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
socket->_onError = JS_DupValue(context, argv[0]);
|
|
|
|
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);
|
|
|
|
socket->_onRead = JS_DupValue(context, argv[0]);
|
|
|
|
int result = uv_read_start((uv_stream_t*)&socket->_socket, _socket_allocateBuffer, _socket_onRead);
|
|
|
|
promiseid_t promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue read_result = tf_task_get_promise(socket->_task, promise);
|
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_read_start: %s", uv_strerror(result));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, error));
|
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);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
*buf = uv_buf_init(malloc(suggestedSize), suggestedSize);
|
|
|
|
}
|
|
|
|
|
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;
|
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 };
|
|
|
|
JSValue result = JS_Call(tf_task_get_context(socket->_task), socket->_onRead, socket->_object, 1, args);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Socket error on read.\n");
|
|
|
|
js_std_dump_error(tf_task_get_context(socket->_task));
|
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), result);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_run_jobs(socket->_task);
|
|
|
|
}
|
|
|
|
_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)))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
while (true)
|
|
|
|
{
|
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 };
|
|
|
|
JSValue result = JS_Call(tf_task_get_context(socket->_task), socket->_onRead, socket->_object, 1, args);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Socket error on read plain.\n");
|
|
|
|
js_std_dump_error(tf_task_get_context(socket->_task));
|
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), result);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_run_jobs(socket->_task);
|
|
|
|
}
|
|
|
|
break;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buffer->base);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _newUint8Array(JSContext* context, const void* data, size_t length)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)data, length);
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
|
|
|
|
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
|
|
|
|
JS_FreeValue(context, constructor);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
JS_FreeValue(context, arrayBuffer);
|
|
|
|
return typedArray;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _socket_notifyDataRead(socket_t* socket, const char* data, size_t length)
|
|
|
|
{
|
|
|
|
if (!JS_IsUndefined(socket->_onRead))
|
|
|
|
{
|
|
|
|
if (data && length > 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue typedArray = _newUint8Array(tf_task_get_context(socket->_task), data, length);
|
|
|
|
JSValue args[] = { typedArray };
|
|
|
|
JSValue result = JS_Call(tf_task_get_context(socket->_task), socket->_onRead, socket->_object, 1, args);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Socket error on data read.\n");
|
|
|
|
js_std_dump_error(tf_task_get_context(socket->_task));
|
|
|
|
}
|
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), typedArray);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(tf_task_get_context(socket->_task), result);
|
|
|
|
tf_task_run_jobs(socket->_task);
|
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
|
|
|
}
|
|
|
|
else if ((array = tf_try_get_array_buffer(context, &length, value)) != 0)
|
|
|
|
{
|
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;
|
|
|
|
JSValue buffer = tf_try_get_typed_array_buffer(context, value, &offset, &length, &element_size);
|
|
|
|
size_t size;
|
2021-10-10 17:51:38 -04:00
|
|
|
if ((array = tf_try_get_array_buffer(context, &size, buffer)) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = callback(socket, promise, (const char*)array, length);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char* rawBuffer = malloc(sizeof(uv_write_t) + length);
|
|
|
|
uv_write_t* request = (uv_write_t*)rawBuffer;
|
|
|
|
memcpy(rawBuffer + sizeof(uv_write_t), data, length);
|
|
|
|
|
|
|
|
uv_buf_t buffer = {
|
|
|
|
.base = rawBuffer + sizeof(uv_write_t),
|
|
|
|
.len = length,
|
|
|
|
};
|
|
|
|
|
|
|
|
request->data = (void*)(intptr_t)promise;
|
|
|
|
return uv_write(request, (uv_stream_t*)&socket->_socket, &buffer, 1, _socket_onWrite);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
promiseid_t promise = tf_task_allocate_promise(socket->_task);
|
|
|
|
JSValue write_result = tf_task_get_promise(socket->_task, promise);
|
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)))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, 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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_write: %s", uv_strerror(result));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, error));
|
|
|
|
}
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char error[256];
|
|
|
|
snprintf(error, sizeof(error), "uv_write: %s", uv_strerror(status));
|
|
|
|
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
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;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return _newUint8Array(tf_task_get_context(socket->_task), buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|