24
« Last post by agena.info on July 07, 2025, 01:55:49 pm »
Hello,
thanks for your two replies. What I am trying to implement is a simple IPv4 library for a fork of the Lua programming language. What works fine in Windows, Solaris, Linux and Mac obviously does not work with ArcaOS:
1) Have one C function that opens a socket and returns its descriptor, a positive integer, to the Lua environment.
2) Have additional, separate C functions that take the socket descriptor from 1) and that bind, connect, etc.
I guess that as soon as the scope of C function 1) in ArcaOS is left and the interpreter returns to the Lua environment, any info on the socket descriptor is lost and the socket is automatically closed so that all the other functions 2) reject it ?
It only works in ArcaOS if I combine a call to socket() and bind() in one and the same C function, see below, but this does not work for me.
Here are the essential code snippets, compiled with GCC 8.3.0 as follows:
gcc -o agena.exe -s agena.o -L. -lm -lagena -lm -lmapm -lz -lreadline -lncurses -ltinfo -lhistory -liconv -lexpat -lmpfr -lgmp -lg2 -lgd -lpcre2-posix -lpcre2-8 -lfi_lib -lsocket
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h> /* to be put before sys/socket.h */
#include <stddef.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h> /* must come before arpa/inet.h */
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <unistd.h>
... further Lua-specific includes ...
The following function, successfully returns a positive socket descriptor, mostly starting from 3:
static int net_openos2 (lua_State *L) {
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
errornosock(L, "socket could not be created", "net.openos2"); /* bails out */
}
lua_pushinteger(L, sock);
return 1;
}
The next function net_bindos2, however, rejects the socket descriptor returned by net_openos2, and quits with a `Socket operation on non-socket` error:
static int net_bindos2 (lua_State *L) {
struct in_addr addr;
struct sockaddr_in server;
int port = 1234;
const char *address = "127.0.0.1";
int sock = luaL_checkinteger(L, 1);
if (sock < 2) {
errornosock(L, "socket is invalid", "net.bindos2"); /* bails out */
}
/* initiate binding */
tools_memset(&server, 0, sizeof(struct sockaddr_in));
if ((inet_aton(address, &addr)) != 0) { /* valid address ? */
tools_memcpy((char *)&server.sin_addr, &addr, sizeof(addr));
} else {
errornosock(L, "failure", "net.bindos2"); /* bails out */
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
/* do it */
if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
agn_neterror(L); /* bails out */
}
lua_pushstring(L, inet_ntoa(server.sin_addr));
return 1;
}
This one works fine in ArcaOS:
static int net_openandbind (lua_State *L) {
int sock;
struct in_addr addr;
struct sockaddr_in server;
int port = 1234;
const char *address = "127.0.0.1";
/* open socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
errornosock(L, "socket could not be created", "net.openandbind"); /* bails out */
}
/* initiate binding */
tools_memset(&server, 0, sizeof(struct sockaddr_in));
if ((inet_aton(address, &addr)) != 0) { /* valid address ? */
tools_memcpy((char *)&server.sin_addr, &addr, sizeof(addr));
} else {
errornosock(L, "failure", "net.bindos2"); /* bails out */
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
/* do it */
if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
agn_neterror(L); /* bails out */
}
lua_pushinteger(L, sock);
return 1;
}