Skip to content

Commit

Permalink
Merge pull request #1420 from Kaiepi/platform-socket
Browse files Browse the repository at this point in the history
Introduce platform/socket.h

Nice work @Kaiepi!
  • Loading branch information
niner committed Mar 7, 2021
2 parents 4f99ab3 + 63b213c commit 564f581
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ HEADERS = src/moar.h \
src/platform/malloc_trim.h \
src/platform/random.h \
src/platform/fork.h \
src/platform/socket.h \
src/jit/graph.h \
src/jit/label.h \
src/jit/expr.h \
Expand Down
31 changes: 9 additions & 22 deletions src/debug/debugserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@
#define false FALSE

#include "cmp.h"

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef SOCKET Socket;
#define sa_family_t unsigned int
#else
#include "unistd.h"
#include <sys/socket.h>
#include <sys/un.h>

typedef int Socket;
#define closesocket close
#endif
#include "platform/socket.h"

typedef enum {
MT_MessageTypeNotUnderstood,
Expand Down Expand Up @@ -471,7 +458,7 @@ static MVMuint16 big_endian_16(MVMuint16 number) {
#endif
}

static void send_greeting(Socket *sock) {
static void send_greeting(MVMSocket *sock) {
char buffer[24] = "MOARVM-REMOTE-DEBUG\0";
MVMuint16 version = big_endian_16(DEBUGSERVER_MAJOR_PROTOCOL_VERSION);
MVMuint16 *verptr = (MVMuint16 *)(&buffer[strlen("MOARVM-REMOTE-DEBUG") + 1]);
Expand All @@ -485,7 +472,7 @@ static void send_greeting(Socket *sock) {
send(*sock, buffer, 24, 0);
}

static int receive_greeting(Socket *sock) {
static int receive_greeting(MVMSocket *sock) {
const char *expected = "MOARVM-REMOTE-CLIENT-OK";
char buffer[24];
int received = 0;
Expand Down Expand Up @@ -2705,7 +2692,7 @@ static bool socket_reader(cmp_ctx_t *ctx, void *data, size_t limit) {
if (debugspam_network)
fprintf(stderr, "asked to read %zu bytes\n", limit);
while (total_read < limit) {
if ((read = recv(*((Socket*)ctx->buf), data, limit, 0)) == -1) {
if ((read = recv(*((MVMSocket*)ctx->buf), data, limit, 0)) == -1) {
if (debugspam_network)
fprintf(stderr, "minus one\n");
return 0;
Expand Down Expand Up @@ -2737,7 +2724,7 @@ static size_t socket_writer(cmp_ctx_t *ctx, const void *data, size_t limit) {
if (debugspam_network)
fprintf(stderr, "asked to send %3zu bytes: ", limit);
while (total_sent < limit) {
if ((sent = send(*(Socket*)ctx->buf, data, limit, 0)) == -1) {
if ((sent = send(*(MVMSocket*)ctx->buf, data, limit, 0)) == -1) {
if (debugspam_network)
fprintf(stderr, "but couldn't (socket disconnected?)\n");
return 0;
Expand Down Expand Up @@ -3203,7 +3190,7 @@ MVMint32 parse_message_map(MVMThreadContext *tc, cmp_ctx_t *ctx, request_data *d

static void debugserver_worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister *args) {
int continue_running = 1;
Socket listensocket;
MVMSocket listensocket;
MVMInstance *vm = tc->instance;
MVMuint64 port = vm->debugserver->port;

Expand Down Expand Up @@ -3260,7 +3247,7 @@ static void debugserver_worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMR
}

while(continue_running) {
Socket clientsocket;
MVMSocket clientsocket;
cmp_ctx_t ctx;

MVM_gc_mark_thread_blocked(tc);
Expand All @@ -3272,7 +3259,7 @@ static void debugserver_worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMR
if (!receive_greeting(&clientsocket)) {
if (tc->instance->debugserver->debugspam_protocol)
fprintf(stderr, "Debugserver: did not receive greeting properly\n");
close(clientsocket);
MVM_platform_close_socket(clientsocket);
continue;
}

Expand Down Expand Up @@ -3302,7 +3289,7 @@ static void debugserver_worker(MVMThreadContext *tc, MVMCallsite *callsite, MVMR

cmp_write_str(&ctx, "reason", 6);
cmp_write_str(&ctx, argument.parse_fail_message, strlen(argument.parse_fail_message));
close(clientsocket);
MVM_platform_close_socket(clientsocket);
uv_mutex_unlock(&vm->debugserver->mutex_network_send);
break;
}
Expand Down
49 changes: 15 additions & 34 deletions src/io/syncsocket.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
#include "moar.h"

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>

typedef SOCKET Socket;
#define sa_family_t unsigned int
#define isatty _isatty
#else
#include "unistd.h"
#include <sys/socket.h>
#include <sys/un.h>

typedef int Socket;
#define closesocket close
#endif
#include "platform/socket.h"

#if defined(_MSC_VER)
#define snprintf _snprintf
Expand Down Expand Up @@ -50,7 +34,7 @@ MVM_NO_RETURN static void throw_error(MVMThreadContext *tc, int r, char *operati
/* Data that we keep for a socket-based handle. */
typedef struct {
/* The socket handle (file descriptor on POSIX, SOCKET on Windows). */
Socket handle;
MVMSocket handle;

/* Buffer of the last received packet of data, and start/end pointers
* into the data. */
Expand Down Expand Up @@ -225,7 +209,7 @@ MVMint64 socket_write_bytes(MVMThreadContext *tc, MVMOSHandle *h, char *buf, MVM

static MVMint64 do_close(MVMThreadContext *tc, MVMIOSyncSocketData *data) {
if (data->handle) {
closesocket(data->handle);
MVM_platform_close_socket(data->handle);
data->handle = 0;
}
return 0;
Expand All @@ -246,7 +230,7 @@ static size_t get_struct_size_for_family(sa_family_t family) {
return sizeof(struct sockaddr_in6);
case AF_INET:
return sizeof(struct sockaddr_in);
#ifndef _WIN32
#ifdef MVM_HAS_PF_UNIX
case AF_UNIX:
return sizeof(struct sockaddr_un);
#endif
Expand Down Expand Up @@ -311,20 +295,14 @@ struct sockaddr * MVM_io_resolve_host_name(MVMThreadContext *tc,
hints.ai_family = AF_INET6;
break;
case MVM_SOCKET_FAMILY_UNIX: {
#if defined(_WIN32) || !defined(AF_UNIX)
/* TODO: UNIX socket support exists in newer versions of Windows.
* See if it's good enough for us to use. */
MVM_free(host_cstr);
MVM_exception_throw_adhoc(tc, "UNIX sockets are not supported by MoarVM on this platform");
#else
static const size_t MAX_SUN_LEN = sizeof(((struct sockaddr_un *)NULL)->sun_path);
size_t sun_len = strnlen(host_cstr, MAX_SUN_LEN);
#ifdef MVM_HAS_PF_UNIX
size_t sun_len = strnlen(host_cstr, MVM_SUN_PATH_SIZE);

if (sun_len >= MAX_SUN_LEN) {
if (sun_len >= MVM_SUN_PATH_SIZE) {
char *waste[] = { host_cstr, NULL };
MVM_exception_throw_adhoc_free(
tc, waste, "Socket path '%s' is too long (max length supported by this platform is %zu characters)",
host_cstr, MAX_SUN_LEN - 1
host_cstr, MVM_SUN_PATH_SIZE - 1
);
} else {
struct sockaddr_un *result_un = MVM_malloc(sizeof(struct sockaddr_un));
Expand All @@ -333,6 +311,9 @@ struct sockaddr * MVM_io_resolve_host_name(MVMThreadContext *tc,
MVM_free(host_cstr);
return (struct sockaddr *)result_un;
}
#else
MVM_free(host_cstr);
MVM_exception_throw_adhoc(tc, "UNIX sockets are not supported by MoarVM on this platform");
#endif
}
default:
Expand Down Expand Up @@ -413,7 +394,7 @@ static void socket_connect(MVMThreadContext *tc, MVMOSHandle *h, MVMString *host
struct sockaddr *dest = MVM_io_resolve_host_name(tc, host, port, family, MVM_SOCKET_TYPE_STREAM, MVM_SOCKET_PROTOCOL_ANY, 0);
int r;

Socket s = socket(dest->sa_family , SOCK_STREAM , 0);
MVMSocket s = socket(dest->sa_family , SOCK_STREAM , 0);
if (MVM_IS_SOCKET_ERROR(s)) {
MVM_free(dest);
MVM_telemetry_interval_stop(tc, interval_id, "syncsocket connect");
Expand Down Expand Up @@ -445,7 +426,7 @@ static void socket_bind(MVMThreadContext *tc, MVMOSHandle *h, MVMString *host, M
struct sockaddr *dest = MVM_io_resolve_host_name(tc, host, port, family, MVM_SOCKET_TYPE_STREAM, MVM_SOCKET_PROTOCOL_ANY, 1);
int r;

Socket s = socket(dest->sa_family , SOCK_STREAM , 0);
MVMSocket s = socket(dest->sa_family , SOCK_STREAM , 0);
if (MVM_IS_SOCKET_ERROR(s)) {
MVM_free(dest);
throw_error(tc, s, "create socket");
Expand Down Expand Up @@ -507,7 +488,7 @@ MVMint64 socket_getport(MVMThreadContext *tc, MVMOSHandle *h) {

static MVMint64 socket_is_tty(MVMThreadContext *tc, MVMOSHandle *h) {
MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data;
return (MVMint64)isatty(data->handle);
return (MVMint64)MVM_platform_isatty(data->handle);
}

static MVMint64 socket_handle(MVMThreadContext *tc, MVMOSHandle *h) {
Expand Down Expand Up @@ -550,7 +531,7 @@ static const MVMIOOps op_table = {

static MVMObject * socket_accept(MVMThreadContext *tc, MVMOSHandle *h) {
MVMIOSyncSocketData *data = (MVMIOSyncSocketData *)h->body.data;
Socket s;
MVMSocket s;

unsigned int interval_id = MVM_telemetry_interval_start(tc, "syncsocket accept");
do {
Expand Down
38 changes: 38 additions & 0 deletions src/platform/socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifdef _WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
# include <io.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <unistd.h>
#endif

#ifdef _WIN32
# if 0
/* TODO: Is Windows' UNIX socket support more complete nowadays...? SOCK_DGRAM
* support was missing at one point. */
# include <afunix.h>
# endif
#else
# include <sys/un.h>
# define MVM_HAS_PF_UNIX
# define MVM_SUN_PATH_SIZE sizeof(((struct sockaddr_un *)NULL)->sun_path)
#endif

#ifdef _WIN32
typedef ULONG in_addr_t;
typedef USHORT in_port_t;
typedef USHORT sa_family_t;
typedef SOCKET MVMSocket;
#else
typedef int MVMSocket;
#endif

#ifdef _WIN32
# define MVM_platform_close_socket closesocket
# define MVM_platform_isatty _isatty
#else
# define MVM_platform_close_socket close
# define MVM_platform_isatty isatty
#endif

0 comments on commit 564f581

Please sign in to comment.