Basic sockets (0.21.0)
This commit is contained in:
parent
94602f0855
commit
d546632cd4
19 changed files with 667 additions and 4 deletions
33
src/vm/os/socket_close.c
Normal file
33
src/vm/os/socket_close.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#ifdef __unix__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_CLOSE: {
|
||||
/* Pops fd; returns 1/0 */
|
||||
Value fdv = pop_value(vm);
|
||||
int ok = 0;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: sock_close expects (int fd)\n");
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
int fd = (int)fdv.i;
|
||||
ok = (close(fd) == 0) ? 1 : 0;
|
||||
#endif
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(ok));
|
||||
break;
|
||||
}
|
||||
53
src/vm/os/socket_recv.c
Normal file
53
src/vm/os/socket_recv.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_RECV: {
|
||||
/* Pops maxlen, fd; pushes data string ("" on EOF/error) */
|
||||
Value maxv = pop_value(vm);
|
||||
Value fdv = pop_value(vm);
|
||||
char *out = NULL;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT || maxv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: sock_recv expects (int fd, int maxlen)\n");
|
||||
free_value(maxv);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_string(""));
|
||||
break;
|
||||
}
|
||||
int fd = (int)fdv.i;
|
||||
int maxlen = (int)maxv.i;
|
||||
if (maxlen <= 0) maxlen = 4096;
|
||||
if (maxlen > 1<<20) maxlen = 1<<20; /* cap at 1MB */
|
||||
out = (char*)malloc((size_t)maxlen + 1);
|
||||
if (out) {
|
||||
ssize_t n = recv(fd, out, (size_t)maxlen, 0);
|
||||
if (n <= 0) {
|
||||
free(out); out = NULL;
|
||||
} else {
|
||||
out[n] = '\0';
|
||||
}
|
||||
}
|
||||
#endif
|
||||
free_value(maxv);
|
||||
free_value(fdv);
|
||||
Value s = make_string(out ? out : "");
|
||||
if (out) free(out);
|
||||
push_value(vm, s);
|
||||
break;
|
||||
}
|
||||
43
src/vm/os/socket_send.c
Normal file
43
src/vm/os/socket_send.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_SEND: {
|
||||
/* Pops data string, fd; pushes bytes sent (>=0) or -1 */
|
||||
Value datav = pop_value(vm);
|
||||
Value fdv = pop_value(vm);
|
||||
int sent = -1;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT || datav.type != VAL_STRING) {
|
||||
fprintf(stderr, "Runtime type error: sock_send expects (int fd, string data)\n");
|
||||
free_value(datav);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(-1));
|
||||
break;
|
||||
}
|
||||
int fd = (int)fdv.i;
|
||||
const char *buf = datav.s ? datav.s : "";
|
||||
size_t len = strlen(buf);
|
||||
ssize_t n = send(fd, buf, len, 0);
|
||||
if (n >= 0) sent = (int)n; else sent = -1;
|
||||
#endif
|
||||
free_value(datav);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(sent));
|
||||
break;
|
||||
}
|
||||
36
src/vm/os/socket_tcp_accept.c
Normal file
36
src/vm/os/socket_tcp_accept.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_TCP_ACCEPT: {
|
||||
/* Pops listen fd; pushes client fd (>0) or 0 */
|
||||
Value fdv = pop_value(vm);
|
||||
int client = 0;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: tcp_accept expects (int listen_fd)\n");
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
int s = (int)fdv.i;
|
||||
int c = accept(s, NULL, NULL);
|
||||
if (c >= 0) client = c;
|
||||
#endif
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(client > 0 ? client : 0));
|
||||
break;
|
||||
}
|
||||
60
src/vm/os/socket_tcp_connect.c
Normal file
60
src/vm/os/socket_tcp_connect.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_TCP_CONNECT: {
|
||||
/* Pops port, host; pushes fd (>0) or 0 */
|
||||
Value portv = pop_value(vm);
|
||||
Value hostv = pop_value(vm);
|
||||
int fd = 0;
|
||||
#ifdef __unix__
|
||||
if (hostv.type != VAL_STRING || portv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: tcp_connect expects (string host, int port)\n");
|
||||
free_value(portv);
|
||||
free_value(hostv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
char *host = value_to_string_alloc(&hostv);
|
||||
int port = (int)portv.i;
|
||||
if (host) {
|
||||
char portstr[16];
|
||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||
struct addrinfo hints, *res = NULL, *rp;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
if (getaddrinfo(host, portstr, &hints, &res) == 0) {
|
||||
for (rp = res; rp != NULL; rp = rp->ai_next) {
|
||||
int s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if (s < 0) continue;
|
||||
if (connect(s, rp->ai_addr, rp->ai_addrlen) == 0) { fd = s; break; }
|
||||
close(s);
|
||||
}
|
||||
if (res) freeaddrinfo(res);
|
||||
}
|
||||
free(host);
|
||||
}
|
||||
#endif
|
||||
free_value(portv);
|
||||
free_value(hostv);
|
||||
push_value(vm, make_int(fd > 0 ? fd : 0));
|
||||
break;
|
||||
}
|
||||
64
src/vm/os/socket_tcp_listen.c
Normal file
64
src/vm/os/socket_tcp_listen.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_TCP_LISTEN: {
|
||||
/* Pops backlog, port; pushes listen fd (>0) or 0 */
|
||||
Value backlogv = pop_value(vm);
|
||||
Value portv = pop_value(vm);
|
||||
int fd = 0;
|
||||
#ifdef __unix__
|
||||
if (portv.type != VAL_INT || backlogv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: tcp_listen expects (int port, int backlog)\n");
|
||||
free_value(backlogv);
|
||||
free_value(portv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
int port = (int)portv.i;
|
||||
int backlog = (int)backlogv.i;
|
||||
int s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (s >= 0) {
|
||||
int yes = 1;
|
||||
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
addr.sin_port = htons((uint16_t)port);
|
||||
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
|
||||
if (listen(s, backlog > 0 ? backlog : 1) == 0) {
|
||||
fd = s;
|
||||
} else {
|
||||
close(s);
|
||||
}
|
||||
} else {
|
||||
close(s);
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)fd;
|
||||
#endif
|
||||
free_value(backlogv);
|
||||
free_value(portv);
|
||||
push_value(vm, make_int(fd > 0 ? fd : 0));
|
||||
break;
|
||||
}
|
||||
53
src/vm/os/socket_unix_connect.c
Normal file
53
src/vm/os/socket_unix_connect.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_UNIX_CONNECT: {
|
||||
/* Pops path; returns fd (>0) or 0 */
|
||||
Value pathv = pop_value(vm);
|
||||
int fd = 0;
|
||||
#ifdef __unix__
|
||||
if (pathv.type != VAL_STRING) {
|
||||
fprintf(stderr, "Runtime type error: unix_connect expects (string path)\n");
|
||||
free_value(pathv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
char *path = value_to_string_alloc(&pathv);
|
||||
if (path) {
|
||||
int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
size_t maxlen = sizeof(addr.sun_path) - 1;
|
||||
strncpy(addr.sun_path, path, maxlen);
|
||||
addr.sun_path[maxlen] = '\0';
|
||||
if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
|
||||
fd = s;
|
||||
} else {
|
||||
close(s);
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
#endif
|
||||
free_value(pathv);
|
||||
push_value(vm, make_int(fd > 0 ? fd : 0));
|
||||
break;
|
||||
}
|
||||
62
src/vm/os/socket_unix_listen.c
Normal file
62
src/vm/os/socket_unix_listen.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#ifdef __unix__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
case OP_SOCK_UNIX_LISTEN: {
|
||||
/* Pops backlog, path; returns listen fd (>0) or 0 */
|
||||
Value backlogv = pop_value(vm);
|
||||
Value pathv = pop_value(vm);
|
||||
int fd = 0;
|
||||
#ifdef __unix__
|
||||
if (pathv.type != VAL_STRING || backlogv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: unix_listen expects (string path, int backlog)\n");
|
||||
free_value(backlogv);
|
||||
free_value(pathv);
|
||||
push_value(vm, make_int(0));
|
||||
break;
|
||||
}
|
||||
char *path = value_to_string_alloc(&pathv);
|
||||
int backlog = (int)backlogv.i;
|
||||
if (path) {
|
||||
int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
size_t maxlen = sizeof(addr.sun_path) - 1;
|
||||
strncpy(addr.sun_path, path, maxlen);
|
||||
addr.sun_path[maxlen] = '\0';
|
||||
unlink(addr.sun_path); /* best effort */
|
||||
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
|
||||
if (listen(s, backlog > 0 ? backlog : 1) == 0) {
|
||||
fd = s;
|
||||
} else {
|
||||
close(s);
|
||||
}
|
||||
} else {
|
||||
close(s);
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
}
|
||||
#endif
|
||||
free_value(backlogv);
|
||||
free_value(pathv);
|
||||
push_value(vm, make_int(fd > 0 ? fd : 0));
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue