#define countof(x) ( sizeof(x) / sizeof(x[0]) )
#define libc_error(ctx) ( JS_ThrowInternalError(ctx, "%s", strerror(errno)) )
+#define gai_error(ctx) ( JS_ThrowInternalError(ctx, "%s", gai_strerror(gai_err)) )
+
+static int gai_err;
static JSValue js_os_fork(
JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv
struct sockaddr_storage * addr
) {
struct addrinfo hints = { .ai_family = family, .ai_socktype = type };
- struct addrinfo * info = NULL; int gai_error = 0;
+ struct addrinfo * info = NULL;
- if (!(gai_error = getaddrinfo( host, port, &hints, &info ))) {
+ if (!(gai_err = getaddrinfo( host, port, &hints, &info ))) {
memcpy(addr, info->ai_addr, info->ai_addrlen);
}
freeaddrinfo(info);
- return gai_error;
+ return gai_err;
}
static int net_ip_listen(SocketData *so, int type) {
SocketData * data = JS_GetOpaque2(ctx, this, socket_cid);
// XXX: how portable is MSG_NOSIGNAL ? (POSIX.1-2008)
uint8_t *buf; size_t len = 0; int flags = MSG_NOSIGNAL;
- const char *host, *port; int gai_err = 0;
+ const char *host, *port;
struct sockaddr * to;
// destHost and destPort may be given for UDP sockets
data->bind.ss_family, data->type, host, port, &(data->peer)
);
JS_FreeCString(ctx, host); JS_FreeCString(ctx, port);
- if (gai_err) return libc_error(ctx);
+ if (gai_err) return gai_error(ctx);
to = (struct sockaddr *) &(data->peer);
// otherwise use default peer address
JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv
) {
SocketData *data = JS_GetOpaque2(ctx, this, socket_cid);
- const char *host = NULL, *port = NULL; int gai_err = 0;
+ const char *host = NULL, *port = NULL;
const size_t l_sun_path = sizeof( ((struct sockaddr_un){}).sun_path );
size_t plen; JSValue ret = JS_UNDEFINED;
if ( (gai_err = net_addrinfo(
data->bind.ss_family, data->type, host, port, &(data->bind)
)) )
- ret = JS_ThrowInternalError(ctx, "%s", gai_strerror(gai_err));
+ ret = gai_error(ctx);
else if ( net_ip_listen(data, data->type) )
ret = libc_error(ctx);
}
JSContext *ctx, JSValueConst this, int argc, JSValueConst *argv
) {
SocketData *data = JS_GetOpaque2(ctx, this, socket_cid);
- const char *host, *port; int gai_err = 0;
+ const char *host, *port;
const size_t l_sun_path = sizeof( ((struct sockaddr_un){}).sun_path );
size_t plen; JSValue ret = JS_UNDEFINED;
if ( (gai_err = net_addrinfo(
data->bind.ss_family, data->type, host, port, &(data->peer)
)) )
- ret = JS_ThrowInternalError(ctx, "%s", gai_strerror(gai_err));
+ ret = gai_error(ctx);
else if ( net_ip_connect(data, data->type) )
ret = libc_error(ctx);
}