From: Paul Hänsch Date: Thu, 19 Mar 2026 23:38:11 +0000 (+0100) Subject: get-address-info "gai_error" macro X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=6f859ff65ef2615072007267323643df4d79656e;p=quickjs_net get-address-info "gai_error" macro --- diff --git a/socket.c b/socket.c index 84cc20b..10e7d1f 100644 --- a/socket.c +++ b/socket.c @@ -13,6 +13,9 @@ #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 @@ -54,13 +57,13 @@ static int net_addrinfo( 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) { @@ -188,7 +191,7 @@ static JSValue js_sock_send( 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 @@ -199,7 +202,7 @@ static JSValue js_sock_send( 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 @@ -420,7 +423,7 @@ static JSValue js_sock_listen( 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; @@ -439,7 +442,7 @@ static JSValue js_sock_listen( 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); } @@ -453,7 +456,7 @@ static JSValue js_sock_connect( 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; @@ -472,7 +475,7 @@ static JSValue js_sock_connect( 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); }