From b3ce92dde5664901adc2a343984153f72e235509 Mon Sep 17 00:00:00 2001 From: crc Date: Thu, 6 Jun 2019 15:17:56 +0000 Subject: [PATCH] retro/unix: bind, accept, listen functioning... FossilOrigin-Name: d20f444bec18f7bcf06972e8bf9f020371e1dbdde8d8906cf2712d7e5ee521b4 --- source/interfaces/retro-unix.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/source/interfaces/retro-unix.c b/source/interfaces/retro-unix.c index a459abd..43815cf 100644 --- a/source/interfaces/retro-unix.c +++ b/source/interfaces/retro-unix.c @@ -1340,17 +1340,45 @@ void socket_create() { } void socket_bind() { + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + int sock = stack_pop(); + int port = stack_pop(); + + getaddrinfo(NULL, string_extract(port), &hints, &res); + stack_push((CELL) bind(SocketID[sock], res->ai_addr, res->ai_addrlen)); } void socket_listen() { + int backlog = stack_pop(); + int sock = stack_pop(); + stack_push(listen(SocketID[sock], backlog)); + stack_push(errno); } void socket_accept() { + int i; + int sock = stack_pop(); + struct sockaddr_storage their_addr; + socklen_t addr_size = sizeof their_addr; + int new_fd = accept(SocketID[sock], (struct sockaddr *)&their_addr, &addr_size); + + for (i = 0; i < 16; i++) { + if (SocketID[i] == 0 && new_fd != 0) { + SocketID[i] = new_fd; + stack_push((CELL)i); + new_fd = 0; + } + } + stack_push(errno); } void socket_connect() { stack_push((CELL)connect(SocketID[stack_pop()], res->ai_addr, res->ai_addrlen)); + stack_push(errno); } void socket_send() {