2020-02-02 02:39:54 +00:00
|
|
|
/*
|
2021-01-12 00:00:31 +00:00
|
|
|
* Copyright (c) 2021 Calvin Rose and contributors.
|
2020-02-02 02:39:54 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef JANET_AMALG
|
|
|
|
#include "features.h"
|
|
|
|
#include <janet.h>
|
|
|
|
#include "util.h"
|
|
|
|
#endif
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
|
2020-11-12 20:52:02 +00:00
|
|
|
#include <math.h>
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#include <winsock2.h>
|
2020-05-16 17:41:26 +00:00
|
|
|
#include <windows.h>
|
2020-04-18 23:14:38 +00:00
|
|
|
#include <ws2tcpip.h>
|
2020-11-09 00:56:13 +00:00
|
|
|
#include <mswsock.h>
|
2020-04-18 23:14:38 +00:00
|
|
|
#pragma comment (lib, "Ws2_32.lib")
|
|
|
|
#pragma comment (lib, "Mswsock.lib")
|
|
|
|
#pragma comment (lib, "Advapi32.lib")
|
|
|
|
#else
|
2021-09-06 22:48:17 +00:00
|
|
|
#include <arpa/inet.h>
|
2020-02-03 15:29:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2020-06-03 05:53:17 +00:00
|
|
|
#include <sys/un.h>
|
2020-11-03 23:49:09 +00:00
|
|
|
#include <netinet/in.h>
|
2020-05-29 00:14:35 +00:00
|
|
|
#include <netinet/tcp.h>
|
2020-02-03 15:29:51 +00:00
|
|
|
#include <netdb.h>
|
2020-05-07 19:54:03 +00:00
|
|
|
#include <fcntl.h>
|
2020-04-18 23:14:38 +00:00
|
|
|
#endif
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-11-14 17:48:23 +00:00
|
|
|
const JanetAbstractType janet_address_type = {
|
2020-06-02 03:06:27 +00:00
|
|
|
"core/socket-address",
|
|
|
|
JANET_ATEND_NAME
|
|
|
|
};
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2020-10-04 17:46:15 +00:00
|
|
|
#define JSOCKCLOSE(x) closesocket((SOCKET) x)
|
2020-11-14 21:01:52 +00:00
|
|
|
#define JSOCKDEFAULT INVALID_SOCKET
|
2020-04-30 02:07:21 +00:00
|
|
|
#define JSOCKVALID(x) ((x) != INVALID_SOCKET)
|
|
|
|
#define JSock SOCKET
|
2020-05-06 23:33:25 +00:00
|
|
|
#define JSOCKFLAGS 0
|
2020-04-18 23:14:38 +00:00
|
|
|
#else
|
2020-04-30 02:07:21 +00:00
|
|
|
#define JSOCKCLOSE(x) close(x)
|
|
|
|
#define JSOCKDEFAULT 0
|
|
|
|
#define JSOCKVALID(x) ((x) >= 0)
|
|
|
|
#define JSock int
|
2020-05-07 04:44:01 +00:00
|
|
|
#ifdef SOCK_CLOEXEC
|
2020-05-06 23:33:25 +00:00
|
|
|
#define JSOCKFLAGS SOCK_CLOEXEC
|
2020-05-07 04:44:01 +00:00
|
|
|
#else
|
|
|
|
#define JSOCKFLAGS 0
|
|
|
|
#endif
|
2020-04-18 23:14:38 +00:00
|
|
|
#endif
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-11-14 21:01:52 +00:00
|
|
|
static JanetStream *make_stream(JSock handle, uint32_t flags);
|
2020-11-14 20:28:10 +00:00
|
|
|
|
2020-04-30 02:07:21 +00:00
|
|
|
/* We pass this flag to all send calls to prevent sigpipe */
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2021-01-10 17:58:47 +00:00
|
|
|
/* Make sure a socket doesn't block */
|
|
|
|
static void janet_net_socknoblock(JSock s) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
unsigned long arg = 1;
|
|
|
|
ioctlsocket(s, FIONBIO, &arg);
|
|
|
|
#else
|
|
|
|
#if !defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
|
|
|
|
int extra = O_CLOEXEC;
|
|
|
|
#else
|
|
|
|
int extra = 0;
|
|
|
|
#endif
|
|
|
|
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK | extra);
|
2020-08-10 23:59:53 +00:00
|
|
|
#ifdef SO_NOSIGPIPE
|
|
|
|
int enable = 1;
|
2021-01-10 17:58:47 +00:00
|
|
|
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int));
|
|
|
|
#endif
|
2020-08-10 23:59:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
/* State machine for accepting connections. */
|
|
|
|
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
typedef struct {
|
|
|
|
JanetListenerState head;
|
2020-11-09 00:56:13 +00:00
|
|
|
WSAOVERLAPPED overlapped;
|
2020-05-28 15:39:40 +00:00
|
|
|
JanetFunction *function;
|
2020-11-09 00:56:13 +00:00
|
|
|
JanetStream *lstream;
|
|
|
|
JanetStream *astream;
|
|
|
|
char buf[1024];
|
|
|
|
} NetStateAccept;
|
2020-05-28 15:39:40 +00:00
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
static int net_sched_accept_impl(NetStateAccept *state, Janet *err);
|
|
|
|
|
|
|
|
JanetAsyncStatus net_machine_accept(JanetListenerState *s, JanetAsyncEvent event) {
|
|
|
|
NetStateAccept *state = (NetStateAccept *)s;
|
2020-05-28 15:39:40 +00:00
|
|
|
switch (event) {
|
|
|
|
default:
|
|
|
|
break;
|
2020-11-09 00:56:13 +00:00
|
|
|
case JANET_ASYNC_EVENT_MARK: {
|
|
|
|
if (state->lstream) janet_mark(janet_wrap_abstract(state->lstream));
|
|
|
|
if (state->astream) janet_mark(janet_wrap_abstract(state->astream));
|
|
|
|
if (state->function) janet_mark(janet_wrap_abstract(state->function));
|
2020-05-28 15:39:40 +00:00
|
|
|
break;
|
2020-11-11 21:35:44 +00:00
|
|
|
}
|
2020-05-28 15:39:40 +00:00
|
|
|
case JANET_ASYNC_EVENT_CLOSE:
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_schedule(s->fiber, janet_wrap_nil());
|
2020-05-30 16:29:58 +00:00
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
2020-10-04 17:46:15 +00:00
|
|
|
case JANET_ASYNC_EVENT_COMPLETE: {
|
2020-11-09 00:56:13 +00:00
|
|
|
int seconds;
|
|
|
|
int bytes = sizeof(seconds);
|
|
|
|
if (NO_ERROR != getsockopt((SOCKET) state->astream->handle, SOL_SOCKET, SO_CONNECT_TIME,
|
2020-11-11 21:35:44 +00:00
|
|
|
(char *)&seconds, &bytes)) {
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_cancel(s->fiber, janet_cstringv("failed to accept connection"));
|
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
|
|
|
}
|
2020-11-11 21:35:44 +00:00
|
|
|
if (NO_ERROR != setsockopt((SOCKET) state->astream->handle, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
|
|
|
|
(char *) & (state->lstream->handle), sizeof(SOCKET))) {
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_cancel(s->fiber, janet_cstringv("failed to accept connection"));
|
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
|
|
|
}
|
2020-11-11 21:35:44 +00:00
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
Janet streamv = janet_wrap_abstract(state->astream);
|
|
|
|
if (state->function) {
|
|
|
|
/* Schedule worker */
|
2020-05-28 15:39:40 +00:00
|
|
|
JanetFiber *fiber = janet_fiber(state->function, 64, 1, &streamv);
|
2021-01-22 18:52:45 +00:00
|
|
|
fiber->supervisor_channel = s->fiber->supervisor_channel;
|
2020-05-28 15:39:40 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
2020-11-09 00:56:13 +00:00
|
|
|
/* Now listen again for next connection */
|
|
|
|
Janet err;
|
|
|
|
if (net_sched_accept_impl(state, &err)) {
|
|
|
|
janet_cancel(s->fiber, err);
|
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-11 21:35:44 +00:00
|
|
|
janet_schedule(s->fiber, streamv);
|
2020-11-09 00:56:13 +00:00
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 16:29:58 +00:00
|
|
|
return JANET_ASYNC_STATUS_NOT_DONE;
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
JANET_NO_RETURN static void janet_sched_accept(JanetStream *stream, JanetFunction *fun) {
|
|
|
|
Janet err;
|
|
|
|
SOCKET lsock = (SOCKET) stream->handle;
|
|
|
|
JanetListenerState *s = janet_listen(stream, net_machine_accept, JANET_ASYNC_LISTEN_READ, sizeof(NetStateAccept), NULL);
|
|
|
|
NetStateAccept *state = (NetStateAccept *)s;
|
|
|
|
memset(&state->overlapped, 0, sizeof(WSAOVERLAPPED));
|
|
|
|
memset(&state->buf, 0, 1024);
|
|
|
|
state->function = fun;
|
|
|
|
state->lstream = stream;
|
|
|
|
s->tag = &state->overlapped;
|
|
|
|
if (net_sched_accept_impl(state, &err)) janet_panicv(err);
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int net_sched_accept_impl(NetStateAccept *state, Janet *err) {
|
|
|
|
SOCKET lsock = (SOCKET) state->lstream->handle;
|
|
|
|
SOCKET asock = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
|
|
|
|
if (asock == INVALID_SOCKET) {
|
2020-11-14 21:24:13 +00:00
|
|
|
*err = janet_ev_lasterr();
|
2020-11-09 00:56:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
JanetStream *astream = make_stream(asock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
|
|
|
state->astream = astream;
|
|
|
|
int socksize = sizeof(SOCKADDR_STORAGE) + 16;
|
|
|
|
if (FALSE == AcceptEx(lsock, asock, state->buf, 0, socksize, socksize, NULL, &state->overlapped)) {
|
|
|
|
int code = WSAGetLastError();
|
|
|
|
if (code == WSA_IO_PENDING) return 0; /* indicates io is happening async */
|
2020-11-14 21:24:13 +00:00
|
|
|
*err = janet_ev_lasterr();
|
2020-11-09 00:56:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2020-05-31 20:46:01 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
JanetListenerState head;
|
2020-11-09 00:56:13 +00:00
|
|
|
JanetFunction *function;
|
2020-05-31 20:46:01 +00:00
|
|
|
} NetStateAccept;
|
|
|
|
|
|
|
|
JanetAsyncStatus net_machine_accept(JanetListenerState *s, JanetAsyncEvent event) {
|
2020-11-09 00:56:13 +00:00
|
|
|
NetStateAccept *state = (NetStateAccept *)s;
|
2020-05-31 20:46:01 +00:00
|
|
|
switch (event) {
|
|
|
|
default:
|
|
|
|
break;
|
2020-11-09 00:56:13 +00:00
|
|
|
case JANET_ASYNC_EVENT_MARK: {
|
|
|
|
if (state->function) janet_mark(janet_wrap_function(state->function));
|
|
|
|
break;
|
|
|
|
}
|
2020-05-31 20:46:01 +00:00
|
|
|
case JANET_ASYNC_EVENT_CLOSE:
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_schedule(s->fiber, janet_wrap_nil());
|
2020-05-31 20:46:01 +00:00
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
|
|
|
case JANET_ASYNC_EVENT_READ: {
|
2020-11-14 20:28:10 +00:00
|
|
|
JSock connfd = accept(s->stream->handle, NULL, NULL);
|
2020-05-31 20:46:01 +00:00
|
|
|
if (JSOCKVALID(connfd)) {
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_net_socknoblock(connfd);
|
2020-05-31 20:46:01 +00:00
|
|
|
JanetStream *stream = make_stream(connfd, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
|
|
|
Janet streamv = janet_wrap_abstract(stream);
|
2020-11-09 00:56:13 +00:00
|
|
|
if (state->function) {
|
|
|
|
JanetFiber *fiber = janet_fiber(state->function, 64, 1, &streamv);
|
2021-01-22 18:52:45 +00:00
|
|
|
fiber->supervisor_channel = s->fiber->supervisor_channel;
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
} else {
|
|
|
|
janet_schedule(s->fiber, streamv);
|
|
|
|
return JANET_ASYNC_STATUS_DONE;
|
|
|
|
}
|
2020-05-31 20:46:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JANET_ASYNC_STATUS_NOT_DONE;
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
JANET_NO_RETURN static void janet_sched_accept(JanetStream *stream, JanetFunction *fun) {
|
2020-11-09 01:48:44 +00:00
|
|
|
NetStateAccept *state = (NetStateAccept *) janet_listen(stream, net_machine_accept, JANET_ASYNC_LISTEN_READ, sizeof(NetStateAccept), NULL);
|
2020-11-09 00:56:13 +00:00
|
|
|
state->function = fun;
|
2020-05-31 20:46:01 +00:00
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:56:13 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Adress info */
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-06-02 03:06:27 +00:00
|
|
|
static int janet_get_sockettype(Janet *argv, int32_t argc, int32_t n) {
|
|
|
|
JanetKeyword stype = janet_optkeyword(argv, argc, n, NULL);
|
|
|
|
int socktype = SOCK_DGRAM;
|
|
|
|
if ((NULL == stype) || !janet_cstrcmp(stype, "stream")) {
|
|
|
|
socktype = SOCK_STREAM;
|
|
|
|
} else if (janet_cstrcmp(stype, "datagram")) {
|
|
|
|
janet_panicf("expected socket type as :stream or :datagram, got %v", argv[n]);
|
|
|
|
}
|
|
|
|
return socktype;
|
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Needs argc >= offset + 2 */
|
2020-06-03 05:53:17 +00:00
|
|
|
/* For unix paths, just rertuns a single sockaddr and sets *is_unix to 1, otherwise 0 */
|
|
|
|
static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset, int socktype, int passive, int *is_unix) {
|
2020-10-04 17:46:15 +00:00
|
|
|
/* Unix socket support - not yet supported on windows. */
|
|
|
|
#ifndef JANET_WINDOWS
|
2020-06-03 05:53:17 +00:00
|
|
|
if (janet_keyeq(argv[offset], "unix")) {
|
|
|
|
const char *path = janet_getcstring(argv, offset + 1);
|
2021-03-23 10:00:48 +00:00
|
|
|
struct sockaddr_un *saddr = janet_calloc(1, sizeof(struct sockaddr_un));
|
2020-06-03 05:53:17 +00:00
|
|
|
if (saddr == NULL) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
saddr->sun_family = AF_UNIX;
|
2021-04-15 19:57:40 +00:00
|
|
|
size_t path_size = sizeof(saddr->sun_path);
|
2020-11-02 14:16:28 +00:00
|
|
|
#ifdef JANET_LINUX
|
|
|
|
if (path[0] == '@') {
|
|
|
|
saddr->sun_path[0] = '\0';
|
2021-04-15 19:57:40 +00:00
|
|
|
snprintf(saddr->sun_path + 1, path_size - 1, "%s", path + 1);
|
2020-11-02 14:16:28 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2021-04-15 19:57:40 +00:00
|
|
|
snprintf(saddr->sun_path, path_size, "%s", path);
|
2020-11-02 14:16:28 +00:00
|
|
|
}
|
2020-06-03 05:53:17 +00:00
|
|
|
*is_unix = 1;
|
|
|
|
return (struct addrinfo *) saddr;
|
|
|
|
}
|
2020-10-04 17:46:15 +00:00
|
|
|
#endif
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Get host and port */
|
|
|
|
const char *host = janet_getcstring(argv, offset);
|
2020-05-01 04:21:26 +00:00
|
|
|
const char *port;
|
|
|
|
if (janet_checkint(argv[offset + 1])) {
|
|
|
|
port = (const char *)janet_to_string(argv[offset + 1]);
|
|
|
|
} else {
|
2020-06-03 05:53:17 +00:00
|
|
|
port = janet_optcstring(argv, offset + 2, offset + 1, NULL);
|
2020-05-01 04:21:26 +00:00
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
/* getaddrinfo */
|
2020-02-10 01:04:34 +00:00
|
|
|
struct addrinfo *ai = NULL;
|
2020-05-10 21:00:55 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2020-02-03 15:29:51 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2020-06-02 03:06:27 +00:00
|
|
|
hints.ai_socktype = socktype;
|
|
|
|
hints.ai_flags = passive ? AI_PASSIVE : 0;
|
2020-02-03 15:29:51 +00:00
|
|
|
int status = getaddrinfo(host, port, &hints, &ai);
|
|
|
|
if (status) {
|
|
|
|
janet_panicf("could not get address info: %s", gai_strerror(status));
|
|
|
|
}
|
2020-06-03 05:53:17 +00:00
|
|
|
*is_unix = 0;
|
2020-02-10 01:04:34 +00:00
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* C Funs
|
|
|
|
*/
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_net_sockaddr,
|
|
|
|
"(net/address host port &opt type)",
|
|
|
|
"Look up the connection information for a given hostname, port, and connection type. Returns "
|
|
|
|
"a handle that can be used to send datagrams over network without establishing a connection. "
|
|
|
|
"On Posix platforms, you can use :unix for host to connect to a unix domain socket, where the name is "
|
|
|
|
"given in the port argument. On Linux, abstract "
|
|
|
|
"unix domain sockets are specified with a leading '@' character in port.") {
|
2020-06-02 03:06:27 +00:00
|
|
|
janet_arity(argc, 2, 4);
|
|
|
|
int socktype = janet_get_sockettype(argv, argc, 2);
|
2020-06-03 05:53:17 +00:00
|
|
|
int is_unix = 0;
|
|
|
|
int make_arr = (argc >= 3 && janet_truthy(argv[3]));
|
|
|
|
struct addrinfo *ai = janet_get_addrinfo(argv, 0, socktype, 0, &is_unix);
|
2020-10-04 17:46:15 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
|
|
|
/* no unix domain socket support on windows yet */
|
2020-06-03 05:53:17 +00:00
|
|
|
if (is_unix) {
|
2020-11-14 17:48:23 +00:00
|
|
|
void *abst = janet_abstract(&janet_address_type, sizeof(struct sockaddr_un));
|
2020-06-03 05:53:17 +00:00
|
|
|
memcpy(abst, ai, sizeof(struct sockaddr_un));
|
|
|
|
Janet ret = janet_wrap_abstract(abst);
|
|
|
|
return make_arr ? janet_wrap_array(janet_array_n(&ret, 1)) : ret;
|
|
|
|
}
|
2020-10-04 17:46:15 +00:00
|
|
|
#endif
|
2020-06-03 05:53:17 +00:00
|
|
|
if (make_arr) {
|
2020-06-02 03:06:27 +00:00
|
|
|
/* Select all */
|
|
|
|
JanetArray *arr = janet_array(10);
|
|
|
|
struct addrinfo *iter = ai;
|
|
|
|
while (NULL != iter) {
|
2020-11-14 17:48:23 +00:00
|
|
|
void *abst = janet_abstract(&janet_address_type, iter->ai_addrlen);
|
2020-06-02 03:06:27 +00:00
|
|
|
memcpy(abst, iter->ai_addr, iter->ai_addrlen);
|
|
|
|
janet_array_push(arr, janet_wrap_abstract(abst));
|
|
|
|
iter = iter->ai_next;
|
|
|
|
}
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
return janet_wrap_array(arr);
|
|
|
|
} else {
|
|
|
|
/* Select first */
|
|
|
|
if (NULL == ai) {
|
|
|
|
janet_panic("no data for given address");
|
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
void *abst = janet_abstract(&janet_address_type, ai->ai_addrlen);
|
2020-06-02 03:06:27 +00:00
|
|
|
memcpy(abst, ai->ai_addr, ai->ai_addrlen);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
return janet_wrap_abstract(abst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_net_connect,
|
|
|
|
"(net/connect host port &opt type)",
|
|
|
|
"Open a connection to communicate with a server. Returns a duplex stream "
|
|
|
|
"that can be used to communicate with the server. Type is an optional keyword "
|
|
|
|
"to specify a connection type, either :stream or :datagram. The default is :stream. ") {
|
2020-06-02 03:06:27 +00:00
|
|
|
janet_arity(argc, 2, 3);
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-06-02 03:06:27 +00:00
|
|
|
int socktype = janet_get_sockettype(argv, argc, 2);
|
2020-06-03 05:53:17 +00:00
|
|
|
int is_unix = 0;
|
|
|
|
struct addrinfo *ai = janet_get_addrinfo(argv, 0, socktype, 0, &is_unix);
|
2020-02-10 01:04:34 +00:00
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
/* Create socket */
|
2020-06-02 03:06:27 +00:00
|
|
|
JSock sock = JSOCKDEFAULT;
|
2020-06-03 05:53:17 +00:00
|
|
|
void *addr = NULL;
|
2021-01-06 01:51:00 +00:00
|
|
|
socklen_t addrlen = 0;
|
2020-10-04 17:46:15 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2020-06-03 05:53:17 +00:00
|
|
|
if (is_unix) {
|
|
|
|
sock = socket(AF_UNIX, socktype | JSOCKFLAGS, 0);
|
|
|
|
if (!JSOCKVALID(sock)) {
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_panicf("could not create socket: %V", janet_ev_lasterr());
|
2020-06-03 05:53:17 +00:00
|
|
|
}
|
|
|
|
addr = (void *) ai;
|
|
|
|
addrlen = sizeof(struct sockaddr_un);
|
2020-10-04 17:46:15 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2020-06-03 05:53:17 +00:00
|
|
|
struct addrinfo *rp = NULL;
|
|
|
|
for (rp = ai; rp != NULL; rp = rp->ai_next) {
|
2020-11-09 00:56:13 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
sock = WSASocketW(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
|
|
|
|
#else
|
2020-06-03 05:53:17 +00:00
|
|
|
sock = socket(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol);
|
2020-11-09 00:56:13 +00:00
|
|
|
#endif
|
2020-06-03 05:53:17 +00:00
|
|
|
if (JSOCKVALID(sock)) {
|
|
|
|
addr = rp->ai_addr;
|
2020-10-04 17:46:15 +00:00
|
|
|
addrlen = (socklen_t) rp->ai_addrlen;
|
2020-06-03 05:53:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NULL == addr) {
|
|
|
|
freeaddrinfo(ai);
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_panicf("could not create socket: %V", janet_ev_lasterr());
|
2020-06-03 05:53:17 +00:00
|
|
|
}
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
2020-06-03 05:53:17 +00:00
|
|
|
|
|
|
|
/* Connect to socket */
|
2021-01-12 00:00:31 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
int status = WSAConnect(sock, addr, addrlen, NULL, NULL, NULL, NULL);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
#else
|
2020-06-03 05:53:17 +00:00
|
|
|
int status = connect(sock, addr, addrlen);
|
|
|
|
if (is_unix) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(ai);
|
2020-06-03 05:53:17 +00:00
|
|
|
} else {
|
2020-04-18 23:14:38 +00:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
2021-01-12 00:00:31 +00:00
|
|
|
#endif
|
2020-04-18 23:14:38 +00:00
|
|
|
|
|
|
|
if (status == -1) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sock);
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_panicf("could not connect to socket: %V", janet_ev_lasterr());
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:58:47 +00:00
|
|
|
/* Set up the socket for non-blocking IO after connect - TODO - non-blocking connect? */
|
|
|
|
janet_net_socknoblock(sock);
|
2020-08-10 23:45:44 +00:00
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
/* Wrap socket in abstract type JanetStream */
|
2020-02-11 14:57:44 +00:00
|
|
|
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
|
2020-02-10 01:04:34 +00:00
|
|
|
return janet_wrap_abstract(stream);
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:53:17 +00:00
|
|
|
static const char *serverify_socket(JSock sfd) {
|
|
|
|
/* Set various socket options */
|
|
|
|
int enable = 1;
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(int)) < 0) {
|
|
|
|
return "setsockopt(SO_REUSEADDR) failed";
|
|
|
|
}
|
|
|
|
#ifdef SO_REUSEPORT
|
|
|
|
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) {
|
|
|
|
return "setsockopt(SO_REUSEPORT) failed";
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_net_socknoblock(sfd);
|
2020-06-03 05:53:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:36:25 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#define JANET_SHUTDOWN_RW SD_BOTH
|
|
|
|
#define JANET_SHUTDOWN_R SD_RECEIVE
|
|
|
|
#define JANET_SHUTDOWN_W SD_SEND
|
|
|
|
#else
|
|
|
|
#define JANET_SHUTDOWN_RW SHUT_RDWR
|
|
|
|
#define JANET_SHUTDOWN_R SHUT_RD
|
|
|
|
#define JANET_SHUTDOWN_W SHUT_WR
|
|
|
|
#endif
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_net_shutdown,
|
|
|
|
"(net/shutdown stream &opt mode)",
|
|
|
|
"Stop communication on this socket in a graceful manner, either in both directions or just "
|
|
|
|
"reading/writing from the stream. The `mode` parameter controls which communication to stop on the socket. "
|
|
|
|
"\n\n* `:wr` is the default and prevents both reading new data from the socket and writing new data to the socket.\n"
|
|
|
|
"* `:r` disables reading new data from the socket.\n"
|
|
|
|
"* `:w` disable writing data to the socket.\n\n"
|
|
|
|
"Returns the original socket.") {
|
2021-03-26 20:36:25 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_SOCKET);
|
2021-03-31 02:14:42 +00:00
|
|
|
int shutdown_type = JANET_SHUTDOWN_RW;
|
2021-03-26 20:36:25 +00:00
|
|
|
if (argc == 2) {
|
|
|
|
const uint8_t *kw = janet_getkeyword(argv, 1);
|
|
|
|
if (0 == janet_cstrcmp(kw, "rw")) {
|
|
|
|
shutdown_type = JANET_SHUTDOWN_RW;
|
|
|
|
} else if (0 == janet_cstrcmp(kw, "r")) {
|
|
|
|
shutdown_type = JANET_SHUTDOWN_R;
|
|
|
|
} else if (0 == janet_cstrcmp(kw, "w")) {
|
|
|
|
shutdown_type = JANET_SHUTDOWN_W;
|
|
|
|
} else {
|
|
|
|
janet_panicf("unexpected keyword %v", argv[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int status;
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
status = shutdown((SOCKET) stream->handle, shutdown_type);
|
|
|
|
#else
|
|
|
|
do {
|
|
|
|
status = shutdown(stream->handle, shutdown_type);
|
|
|
|
} while (status == -1 && errno == EINTR);
|
|
|
|
#endif
|
|
|
|
if (status) {
|
|
|
|
janet_panicf("could not shutdown socket: %V", janet_ev_lasterr());
|
|
|
|
}
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_net_listen,
|
|
|
|
"(net/listen host port &opt type)",
|
|
|
|
"Creates a server. Returns a new stream that is neither readable nor "
|
|
|
|
"writeable. Use net/accept or net/accept-loop be to handle connections and start the server. "
|
|
|
|
"The type parameter specifies the type of network connection, either "
|
|
|
|
"a :stream (usually tcp), or :datagram (usually udp). If not specified, the default is "
|
|
|
|
":stream. The host and port arguments are the same as in net/address.") {
|
2020-11-09 17:18:09 +00:00
|
|
|
janet_arity(argc, 2, 3);
|
2020-02-10 01:04:34 +00:00
|
|
|
|
|
|
|
/* Get host, port, and handler*/
|
2020-11-09 17:18:09 +00:00
|
|
|
int socktype = janet_get_sockettype(argv, argc, 2);
|
2020-06-03 05:53:17 +00:00
|
|
|
int is_unix = 0;
|
|
|
|
struct addrinfo *ai = janet_get_addrinfo(argv, 0, socktype, 1, &is_unix);
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2020-04-30 02:07:21 +00:00
|
|
|
JSock sfd = JSOCKDEFAULT;
|
2020-10-04 17:46:15 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2020-06-03 05:53:17 +00:00
|
|
|
if (is_unix) {
|
|
|
|
sfd = socket(AF_UNIX, socktype | JSOCKFLAGS, 0);
|
|
|
|
if (!JSOCKVALID(sfd)) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(ai);
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_panicf("could not create socket: %V", janet_ev_lasterr());
|
2020-04-18 23:14:38 +00:00
|
|
|
}
|
2020-06-03 05:53:17 +00:00
|
|
|
const char *err = serverify_socket(sfd);
|
|
|
|
if (NULL != err || bind(sfd, (struct sockaddr *)ai, sizeof(struct sockaddr_un))) {
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sfd);
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(ai);
|
2021-01-10 17:58:47 +00:00
|
|
|
if (err) {
|
|
|
|
janet_panic(err);
|
|
|
|
} else {
|
|
|
|
janet_panicf("could not bind socket: %V", janet_ev_lasterr());
|
|
|
|
}
|
2020-04-18 17:12:27 +00:00
|
|
|
}
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(ai);
|
2020-10-04 17:46:15 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2020-06-03 05:53:17 +00:00
|
|
|
/* Check all addrinfos in a loop for the first that we can bind to. */
|
|
|
|
struct addrinfo *rp = NULL;
|
|
|
|
for (rp = ai; rp != NULL; rp = rp->ai_next) {
|
2020-11-09 00:56:13 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
sfd = WSASocketW(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
|
|
|
|
#else
|
2020-06-03 05:53:17 +00:00
|
|
|
sfd = socket(rp->ai_family, rp->ai_socktype | JSOCKFLAGS, rp->ai_protocol);
|
2020-11-09 00:56:13 +00:00
|
|
|
#endif
|
2020-06-03 05:53:17 +00:00
|
|
|
if (!JSOCKVALID(sfd)) continue;
|
|
|
|
const char *err = serverify_socket(sfd);
|
|
|
|
if (NULL != err) {
|
|
|
|
JSOCKCLOSE(sfd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Bind */
|
|
|
|
if (bind(sfd, rp->ai_addr, (int) rp->ai_addrlen) == 0) break;
|
2020-04-30 02:07:21 +00:00
|
|
|
JSOCKCLOSE(sfd);
|
2020-04-18 17:12:27 +00:00
|
|
|
}
|
2020-06-03 05:53:17 +00:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
if (NULL == rp) {
|
|
|
|
janet_panic("could not bind to any sockets");
|
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 03:06:27 +00:00
|
|
|
if (socktype == SOCK_DGRAM) {
|
|
|
|
/* Datagram server (UDP) */
|
2020-11-09 17:18:09 +00:00
|
|
|
JanetStream *stream = make_stream(sfd, JANET_STREAM_UDPSERVER | JANET_STREAM_READABLE);
|
|
|
|
return janet_wrap_abstract(stream);
|
2020-06-02 03:06:27 +00:00
|
|
|
} else {
|
|
|
|
/* Stream server (TCP) */
|
|
|
|
|
|
|
|
/* listen */
|
|
|
|
int status = listen(sfd, 1024);
|
|
|
|
if (status) {
|
|
|
|
JSOCKCLOSE(sfd);
|
2021-01-10 17:58:47 +00:00
|
|
|
janet_panicf("could not listen on file descriptor: %V", janet_ev_lasterr());
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put sfd on our loop */
|
2020-11-09 17:18:09 +00:00
|
|
|
JanetStream *stream = make_stream(sfd, JANET_STREAM_ACCEPTABLE);
|
|
|
|
return janet_wrap_abstract(stream);
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
2020-02-03 15:29:51 +00:00
|
|
|
|
2021-09-06 20:57:23 +00:00
|
|
|
#define SO_MAX(a, b) (((a) > (b))? (a) : (b))
|
|
|
|
#define SA_PORT_NONE (&(in_port_t){ 0 })
|
|
|
|
#define SO_MIN(a, b) (((a) < (b))? (a) : (b))
|
|
|
|
#define SA_ADDRSTRLEN SO_MAX(INET6_ADDRSTRLEN, (sizeof ((struct sockaddr_un *)0)->sun_path) + 1)
|
|
|
|
#define sa_ntoa(sa) sa_ntoa_((char [SA_ADDRSTRLEN]){ 0 }, SA_ADDRSTRLEN, (sa))
|
|
|
|
#define sa_family(...) sa_family(__VA_ARGS__)
|
|
|
|
#define sa_port(...) sa_port(__VA_ARGS__)
|
|
|
|
|
|
|
|
union sockaddr_arg {
|
2021-09-06 23:30:54 +00:00
|
|
|
struct sockaddr *sa;
|
|
|
|
const struct sockaddr *c_sa;
|
|
|
|
struct sockaddr_storage *ss;
|
|
|
|
struct sockaddr_storage *c_ss;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
struct sockaddr_in *c_sin;
|
|
|
|
struct sockaddr_in6 *sin6;
|
|
|
|
struct sockaddr_in6 *c_sin6;
|
2021-09-07 00:26:33 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2021-09-06 23:30:54 +00:00
|
|
|
struct sockaddr_un *sun;
|
2021-09-07 00:26:33 +00:00
|
|
|
#endif
|
2021-09-06 23:30:54 +00:00
|
|
|
struct sockaddr_un *c_sun;
|
|
|
|
union sockaddr_any *any;
|
|
|
|
union sockaddr_any *c_any;
|
|
|
|
|
|
|
|
void *ptr;
|
|
|
|
void *c_ptr;
|
2021-09-06 20:57:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
union sockaddr_any {
|
2021-09-06 23:30:54 +00:00
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
2021-09-07 00:26:33 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2021-09-06 23:30:54 +00:00
|
|
|
struct sockaddr_un sun;
|
2021-09-07 00:26:33 +00:00
|
|
|
#endif
|
2021-09-06 20:57:23 +00:00
|
|
|
};
|
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
static inline union sockaddr_arg sockaddr_ref(void *arg) {
|
|
|
|
return (union sockaddr_arg) {
|
|
|
|
arg
|
|
|
|
};
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
static inline sa_family_t *(sa_family)(void *arg) {
|
|
|
|
return &sockaddr_ref(arg).sa->sa_family;
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
static inline in_port_t *(sa_port)(void *arg, const in_port_t *def, int *error) {
|
|
|
|
switch (*sa_family(arg)) {
|
|
|
|
case AF_INET:
|
|
|
|
return &sockaddr_ref(arg).sin->sin_port;
|
|
|
|
case AF_INET6:
|
|
|
|
return &sockaddr_ref(arg).sin6->sin6_port;
|
|
|
|
default:
|
|
|
|
if (error)
|
|
|
|
*error = EAFNOSUPPORT;
|
|
|
|
|
|
|
|
return (in_port_t *)def;
|
|
|
|
}
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t janet_socket_strlcpy(char *dst, const char *src, size_t lim) {
|
2021-09-06 23:30:54 +00:00
|
|
|
char *d = dst;
|
|
|
|
char *e = &dst[lim];
|
|
|
|
const char *s = src;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
if (d < e) {
|
|
|
|
do {
|
|
|
|
if ('\0' == (*d++ = *s++))
|
|
|
|
return s - src - 1;
|
|
|
|
} while (d < e);
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
d[-1] = '\0';
|
|
|
|
}
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
while (*s++ != '\0')
|
|
|
|
;;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
return s - src - 1;
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *sa_ntop(char *dst, size_t lim, const void *src, const char *def, int *_error) {
|
2021-09-06 23:30:54 +00:00
|
|
|
union sockaddr_any *any = (void *)src;
|
|
|
|
const char *unspec = "0.0.0.0";
|
|
|
|
char text[SA_ADDRSTRLEN];
|
|
|
|
int error;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
switch (*sa_family(&any->sa)) {
|
|
|
|
case AF_INET:
|
|
|
|
unspec = "0.0.0.0";
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
if (!inet_ntop(AF_INET, &any->sin.sin_addr, text, sizeof text))
|
|
|
|
goto syerr;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
unspec = "::";
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
if (!inet_ntop(AF_INET6, &any->sin6.sin6_addr, text, sizeof text))
|
|
|
|
goto syerr;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
break;
|
2021-09-07 00:26:33 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
2021-09-06 23:30:54 +00:00
|
|
|
case AF_UNIX:
|
|
|
|
unspec = "/nonexistent";
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
memset(text, 0, sizeof text);
|
|
|
|
memcpy(text, any->sun.sun_path, SO_MIN(sizeof text - 1, sizeof any->sun.sun_path));
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
break;
|
2021-09-07 00:26:33 +00:00
|
|
|
#endif
|
2021-09-06 23:30:54 +00:00
|
|
|
default:
|
|
|
|
error = EAFNOSUPPORT;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
if (janet_socket_strlcpy(dst, text, lim) >= lim) {
|
|
|
|
error = ENOSPC;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
return dst;
|
2021-09-06 20:57:23 +00:00
|
|
|
syerr:
|
2021-09-06 23:30:54 +00:00
|
|
|
error = errno;
|
2021-09-06 20:57:23 +00:00
|
|
|
error:
|
2021-09-06 23:30:54 +00:00
|
|
|
if (_error)
|
|
|
|
*_error = error;
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
/*
|
|
|
|
* NOTE: Always write something in case caller ignores errors, such
|
|
|
|
* as when caller is using the sa_ntoa() macro.
|
|
|
|
*/
|
|
|
|
safe_memcpy(dst, (def) ? def : unspec, lim);
|
2021-09-06 20:57:23 +00:00
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
return (char *)def;
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *sa_ntoa_(char *dst, size_t lim, const void *src) {
|
2021-09-06 23:30:54 +00:00
|
|
|
return sa_ntop(dst, lim, src, NULL, &(int) {
|
|
|
|
0
|
|
|
|
}), dst;
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 22:35:49 +00:00
|
|
|
static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen) {
|
2021-09-06 20:57:23 +00:00
|
|
|
uint8_t *hn = NULL;
|
|
|
|
uint16_t hp = 0;
|
|
|
|
size_t plen = SA_ADDRSTRLEN;
|
|
|
|
|
2021-09-06 23:30:54 +00:00
|
|
|
switch (ss->ss_family) {
|
2021-09-06 20:57:23 +00:00
|
|
|
case AF_INET:
|
2021-09-06 23:30:54 +00:00
|
|
|
/* fall through */
|
2021-09-06 20:57:23 +00:00
|
|
|
case AF_INET6:
|
|
|
|
/* hn = hostname, hp = hostport */
|
|
|
|
hn = (uint8_t *)sa_ntoa(ss);
|
|
|
|
hp = ntohs(*sa_port((void *)ss, SA_PORT_NONE, NULL));
|
|
|
|
break;
|
|
|
|
case AF_UNIX:
|
|
|
|
/* support nameless sockets, linux-ism */
|
|
|
|
if (slen > offsetof(struct sockaddr_un, sun_path)) {
|
|
|
|
struct sockaddr_un *sun = (struct sockaddr_un *)ss;
|
2021-09-06 23:30:54 +00:00
|
|
|
char *pe = (char *)sun + SO_MIN(sizeof * sun, slen);
|
2021-09-06 20:57:23 +00:00
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
while (pe > sun->sun_path && pe[-1] == '\0')
|
|
|
|
--pe;
|
|
|
|
|
|
|
|
if ((plen = pe - sun->sun_path) > 0) {
|
|
|
|
hn = (uint8_t *)sun->sun_path;
|
|
|
|
} else {
|
|
|
|
hn = (uint8_t *)"@";
|
|
|
|
plen = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hn = (uint8_t *)"@";
|
|
|
|
plen = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
hn = (uint8_t *)"";
|
|
|
|
plen = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-09-06 22:35:49 +00:00
|
|
|
Janet name[2];
|
|
|
|
int32_t len = 1;
|
|
|
|
name[0] = janet_wrap_string(janet_cstring((const char *)hn));
|
|
|
|
if (hp > 0) {
|
|
|
|
len++;
|
|
|
|
name[1] = janet_wrap_integer(hp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return janet_wrap_tuple(janet_tuple_n(name, len));
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 22:01:09 +00:00
|
|
|
JANET_CORE_FN(cfun_net_getsockname,
|
|
|
|
"(net/localname stream)",
|
2021-09-06 23:30:54 +00:00
|
|
|
"Gets the local address and port in a tuple in that order.") {
|
2021-09-06 22:01:09 +00:00
|
|
|
janet_arity(argc, 1, 1);
|
|
|
|
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t slen = sizeof ss;
|
|
|
|
memset(&ss, 0, slen);
|
|
|
|
|
|
|
|
int error;
|
2021-09-06 23:30:54 +00:00
|
|
|
if (0 != (error = getsockname(js->handle, (struct sockaddr *) &ss, &slen)))
|
2021-09-06 22:01:09 +00:00
|
|
|
janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr());
|
|
|
|
|
2021-09-06 22:35:49 +00:00
|
|
|
return janet_so_getname(&ss, slen);
|
2021-09-06 22:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-06 20:57:23 +00:00
|
|
|
JANET_CORE_FN(cfun_net_getpeername,
|
|
|
|
"(net/peername stream)",
|
2021-09-06 23:30:54 +00:00
|
|
|
"Gets the remote peer's address and port in a tuple in that order.") {
|
2021-09-06 20:57:23 +00:00
|
|
|
janet_arity(argc, 1, 1);
|
|
|
|
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t slen = sizeof ss;
|
2021-09-06 23:30:54 +00:00
|
|
|
memset(&ss, 0, slen);
|
2021-09-06 20:57:23 +00:00
|
|
|
|
|
|
|
int error;
|
|
|
|
if (0 != (error = getpeername(js->handle, (struct sockaddr *)&ss, &slen))) {
|
|
|
|
janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr());
|
|
|
|
}
|
|
|
|
|
2021-09-06 22:35:49 +00:00
|
|
|
return janet_so_getname(&ss, slen);
|
2021-09-06 20:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_accept_loop,
|
|
|
|
"(net/accept-loop stream handler)",
|
|
|
|
"Shorthand for running a server stream that will continuously accept new connections. "
|
|
|
|
"Blocks the current fiber until the stream is closed, and will return the stream.") {
|
2020-11-09 17:18:09 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_ACCEPTABLE | JANET_STREAM_SOCKET);
|
2020-11-09 17:18:09 +00:00
|
|
|
JanetFunction *fun = janet_getfunction(argv, 1);
|
|
|
|
janet_sched_accept(stream, fun);
|
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_accept,
|
|
|
|
"(net/accept stream &opt timeout)",
|
|
|
|
"Get the next connection on a server stream. This would usually be called in a loop in a dedicated fiber. "
|
|
|
|
"Takes an optional timeout in seconds, after which will return nil. "
|
|
|
|
"Returns a new duplex stream which represents a connection to the client.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_ACCEPTABLE | JANET_STREAM_SOCKET);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 1, INFINITY);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-09 00:56:13 +00:00
|
|
|
janet_sched_accept(stream, NULL);
|
2020-02-03 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_read,
|
|
|
|
"(net/read stream nbytes &opt buf timeout)",
|
|
|
|
"Read up to n bytes from a stream, suspending the current fiber until the bytes are available. "
|
|
|
|
"`n` can also be the keyword `:all` to read into the buffer until end of stream. "
|
|
|
|
"If less than n bytes are available (and more than 0), will push those bytes and return early. "
|
|
|
|
"Takes an optional timeout in seconds, after which will return nil. "
|
|
|
|
"Returns a buffer with up to n more bytes in it, or raises an error if the read failed.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 2, 4);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_READABLE | JANET_STREAM_SOCKET);
|
2020-02-10 01:04:34 +00:00
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
2020-12-30 02:37:59 +00:00
|
|
|
if (janet_keyeq(argv[1], "all")) {
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2021-01-12 00:32:56 +00:00
|
|
|
janet_ev_recvchunk(stream, buffer, INT32_MAX, MSG_NOSIGNAL);
|
2020-12-30 02:37:59 +00:00
|
|
|
} else {
|
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
|
|
|
janet_ev_recv(stream, buffer, n, MSG_NOSIGNAL);
|
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_await();
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_chunk,
|
|
|
|
"(net/chunk stream nbytes &opt buf timeout)",
|
|
|
|
"Same a net/read, but will wait for all n bytes to arrive rather than return early. "
|
|
|
|
"Takes an optional timeout in seconds, after which will return nil.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 2, 4);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_READABLE | JANET_STREAM_SOCKET);
|
2020-02-10 01:04:34 +00:00
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_recvchunk(stream, buffer, n, MSG_NOSIGNAL);
|
|
|
|
janet_await();
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_recv_from,
|
|
|
|
"(net/recv-from stream nbytes buf &opt timoeut)",
|
|
|
|
"Receives data from a server stream and puts it into a buffer. Returns the socket-address the "
|
|
|
|
"packet came from. Takes an optional timeout in seconds, after which will return nil.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 3, 4);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_UDPSERVER | JANET_STREAM_SOCKET);
|
2020-06-02 03:06:27 +00:00
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
JanetBuffer *buffer = janet_getbuffer(argv, 2);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_recvfrom(stream, buffer, n, MSG_NOSIGNAL);
|
|
|
|
janet_await();
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_write,
|
|
|
|
"(net/write stream data &opt timeout)",
|
|
|
|
"Write data to a stream, suspending the current fiber until the write "
|
|
|
|
"completes. Takes an optional timeout in seconds, after which will return nil. "
|
|
|
|
"Returns nil, or raises an error if the write failed.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 2, 3);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_WRITABLE | JANET_STREAM_SOCKET);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 2, INFINITY);
|
2020-02-10 01:04:34 +00:00
|
|
|
if (janet_checktype(argv[1], JANET_BUFFER)) {
|
2020-07-20 00:41:12 +00:00
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_send_buffer(stream, janet_getbuffer(argv, 1), MSG_NOSIGNAL);
|
2020-02-10 01:04:34 +00:00
|
|
|
} else {
|
|
|
|
JanetByteView bytes = janet_getbytes(argv, 1);
|
2020-07-20 00:41:12 +00:00
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_send_string(stream, bytes.bytes, MSG_NOSIGNAL);
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_await();
|
2020-06-02 03:06:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_send_to,
|
|
|
|
"(net/send-to stream dest data &opt timeout)",
|
|
|
|
"Writes a datagram to a server stream. dest is a the destination address of the packet. "
|
|
|
|
"Takes an optional timeout in seconds, after which will return nil. "
|
|
|
|
"Returns stream.") {
|
2020-07-20 00:41:12 +00:00
|
|
|
janet_arity(argc, 3, 4);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_UDPSERVER | JANET_STREAM_SOCKET);
|
2020-11-14 17:48:23 +00:00
|
|
|
void *dest = janet_getabstract(argv, 1, &janet_address_type);
|
2020-07-20 00:41:12 +00:00
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
2020-06-02 03:06:27 +00:00
|
|
|
if (janet_checktype(argv[2], JANET_BUFFER)) {
|
2020-07-20 00:41:12 +00:00
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_sendto_buffer(stream, janet_getbuffer(argv, 2), dest, MSG_NOSIGNAL);
|
2020-06-02 03:06:27 +00:00
|
|
|
} else {
|
|
|
|
JanetByteView bytes = janet_getbytes(argv, 2);
|
2020-07-20 00:41:12 +00:00
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_sendto_string(stream, bytes.bytes, dest, MSG_NOSIGNAL);
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_await();
|
2020-02-10 01:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_CORE_FN(cfun_stream_flush,
|
|
|
|
"(net/flush stream)",
|
|
|
|
"Make sure that a stream is not buffering any data. This temporarily disables Nagle's algorithm. "
|
|
|
|
"Use this to make sure data is sent without delay. Returns stream.") {
|
2021-01-10 18:02:28 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2020-11-14 20:28:10 +00:00
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_WRITABLE | JANET_STREAM_SOCKET);
|
2020-05-29 00:14:35 +00:00
|
|
|
/* Toggle no delay flag */
|
|
|
|
int flag = 1;
|
2020-10-04 17:46:15 +00:00
|
|
|
setsockopt((JSock) stream->handle, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
2020-05-29 00:14:35 +00:00
|
|
|
flag = 0;
|
2020-10-04 17:46:15 +00:00
|
|
|
setsockopt((JSock) stream->handle, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
|
2020-05-29 00:14:35 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
static const JanetMethod net_stream_methods[] = {
|
2020-02-12 15:32:41 +00:00
|
|
|
{"chunk", cfun_stream_chunk},
|
2020-11-14 20:28:10 +00:00
|
|
|
{"close", janet_cfun_stream_close},
|
2020-02-12 15:32:41 +00:00
|
|
|
{"read", cfun_stream_read},
|
|
|
|
{"write", cfun_stream_write},
|
2020-05-29 00:14:35 +00:00
|
|
|
{"flush", cfun_stream_flush},
|
2020-05-31 20:46:01 +00:00
|
|
|
{"accept", cfun_stream_accept},
|
2020-11-09 17:18:09 +00:00
|
|
|
{"accept-loop", cfun_stream_accept_loop},
|
2020-06-02 03:06:27 +00:00
|
|
|
{"send-to", cfun_stream_send_to},
|
|
|
|
{"recv-from", cfun_stream_recv_from},
|
2020-11-14 20:28:10 +00:00
|
|
|
{"evread", janet_cfun_stream_read},
|
|
|
|
{"evchunk", janet_cfun_stream_chunk},
|
|
|
|
{"evwrite", janet_cfun_stream_write},
|
2021-03-26 20:36:25 +00:00
|
|
|
{"shutdown", cfun_net_shutdown},
|
2020-02-12 15:32:41 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2020-11-14 21:01:52 +00:00
|
|
|
static JanetStream *make_stream(JSock handle, uint32_t flags) {
|
|
|
|
return janet_stream((JanetHandle) handle, flags | JANET_STREAM_SOCKET, net_stream_methods);
|
2020-02-12 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 02:39:54 +00:00
|
|
|
|
|
|
|
void janet_lib_net(JanetTable *env) {
|
2021-07-26 09:20:06 +00:00
|
|
|
JanetRegExt net_cfuns[] = {
|
|
|
|
JANET_CORE_REG("net/address", cfun_net_sockaddr),
|
|
|
|
JANET_CORE_REG("net/listen", cfun_net_listen),
|
|
|
|
JANET_CORE_REG("net/accept", cfun_stream_accept),
|
|
|
|
JANET_CORE_REG("net/accept-loop", cfun_stream_accept_loop),
|
|
|
|
JANET_CORE_REG("net/read", cfun_stream_read),
|
|
|
|
JANET_CORE_REG("net/chunk", cfun_stream_chunk),
|
|
|
|
JANET_CORE_REG("net/write", cfun_stream_write),
|
|
|
|
JANET_CORE_REG("net/send-to", cfun_stream_send_to),
|
|
|
|
JANET_CORE_REG("net/recv-from", cfun_stream_recv_from),
|
|
|
|
JANET_CORE_REG("net/flush", cfun_stream_flush),
|
|
|
|
JANET_CORE_REG("net/connect", cfun_net_connect),
|
|
|
|
JANET_CORE_REG("net/shutdown", cfun_net_shutdown),
|
2021-09-06 20:57:23 +00:00
|
|
|
JANET_CORE_REG("net/peername", cfun_net_getpeername),
|
2021-09-06 22:01:09 +00:00
|
|
|
JANET_CORE_REG("net/localname", cfun_net_getsockname),
|
2021-07-26 09:20:06 +00:00
|
|
|
JANET_REG_END
|
|
|
|
};
|
|
|
|
janet_core_cfuns_ext(env, NULL, net_cfuns);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_net_init(void) {
|
2020-04-18 23:14:38 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
WSADATA wsaData;
|
|
|
|
janet_assert(!WSAStartup(MAKEWORD(2, 2), &wsaData), "could not start winsock");
|
|
|
|
#endif
|
2020-02-02 02:39:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:14:38 +00:00
|
|
|
void janet_net_deinit(void) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-28 15:39:40 +00:00
|
|
|
|
|
|
|
#endif
|