From 5b4b190cb9411a35652a6fd72560330591a9818a Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 17 Apr 2021 22:32:45 -0700 Subject: [PATCH 1/2] Fix protocol being treated as family in getaddrinfo inputs/outputs Context: https://github.com/luvit/luv/pull/534#issuecomment-821902777 --- src/constants.c | 6 ++++++ src/dns.c | 12 +++++------- src/private.h | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/constants.c b/src/constants.c index 561e9f85..0aaccf00 100644 --- a/src/constants.c +++ b/src/constants.c @@ -675,3 +675,9 @@ static int luv_proto_string_to_num(const char* string) { if (!proto) return -1; return proto->p_proto; } + +static const char* luv_proto_num_to_string(int num) { + struct protoent* proto = getprotobynumber(num); + if (!proto) return NULL; + return proto->p_name; +} diff --git a/src/dns.c b/src/dns.c index df6034a8..9a3ffd35 100644 --- a/src/dns.c +++ b/src/dns.c @@ -49,7 +49,7 @@ static void luv_pushaddrinfo(lua_State* L, struct addrinfo* res) { } lua_pushstring(L, luv_sock_num_to_string(curr->ai_socktype)); lua_setfield(L, -2, "socktype"); - lua_pushstring(L, luv_af_num_to_string(curr->ai_protocol)); + lua_pushstring(L, luv_proto_num_to_string(curr->ai_protocol)); lua_setfield(L, -2, "protocol"); if (curr->ai_canonname) { lua_pushstring(L, curr->ai_canonname); @@ -134,13 +134,11 @@ static int luv_getaddrinfo(lua_State* L) { hints->ai_protocol = lua_tointeger(L, -1); } else if (lua_isstring(L, -1)) { - int protocol = luv_af_string_to_num(lua_tostring(L, -1)); - if (protocol) { - hints->ai_protocol = protocol; - } - else { - return luaL_argerror(L, 3, "Invalid protocol hint"); + int protocol = luv_proto_string_to_num(lua_tostring(L, -1)); + if (protocol < 0) { + return luaL_argerror(L, 3, lua_pushfstring(L, "invalid protocol: %s", lua_tostring(L, -1))); } + hints->ai_protocol = protocol; } else if (!lua_isnil(L, -1)) { return luaL_argerror(L, 3, "protocol hint must be string if set"); diff --git a/src/private.h b/src/private.h index dce999cb..80b305b0 100644 --- a/src/private.h +++ b/src/private.h @@ -83,6 +83,7 @@ static const char* luv_sock_num_to_string(const int num); static int luv_sig_string_to_num(const char* string); static const char* luv_sig_num_to_string(const int num); static int luv_proto_string_to_num(const char* string); +static const char* luv_proto_num_to_string(int num); /* From util.c */ // Push a Libuv error code onto the Lua stack From af4a608949713e0c75e2e5977210626515f1bc53 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sat, 17 Apr 2021 22:38:18 -0700 Subject: [PATCH 2/2] Improve the family/socktype/protocol-related docs a bit --- docs.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/docs.md b/docs.md index ceac0a08..357c484a 100644 --- a/docs.md +++ b/docs.md @@ -1939,7 +1939,7 @@ Gets the current Window width and height. Controls whether console virtual terminal sequences are processed by libuv or console. Useful in particular for enabling ConEmu support of ANSI X3.64 and Xterm 256 colors. Otherwise Windows10 consoles are usually detected -automatically. State may be a family string: `"supported"` or `"unsupported"`. +automatically. State should be one of: `"supported"` or `"unsupported"`. This function is only meaningful on Windows systems. On Unix it is silently ignored. @@ -3086,9 +3086,9 @@ called in the main loop thread. - `host`: `string` or `nil` - `service`: `string` or `nil` - `hints`: `table` or `nil` - - `family`: `integer` or `string` or `nil` - - `socktype`: `integer` or `string` or `nil` - - `protocol`: `integer` or `string` or `nil` + - `family`: `string` or `integer` or `nil` + - `socktype`: `string` or `integer` or `nil` + - `protocol`: `string` or `integer` or `nil` - `addrconfig`: `boolean` or `nil` - `v4mapped`: `boolean` or `nil` - `all`: `boolean` or `nil` @@ -3103,6 +3103,13 @@ called in the main loop thread. Equivalent to `getaddrinfo(3)`. Either `node` or `service` may be `nil` but not both. +Valid hint strings for the keys that take a string: +- `family`: `"unix"`, `"inet"`, `"inet6"`, `"ipx"`, +`"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or `"packet"` +- `socktype`: `"stream"`, `"dgram"`, `"raw"`, +`"rdm"`, or `"seqpacket"` +- `protocol`: will be looked up using the `getprotobyname(3)` function (examples: `"ip"`, `"icmp"`, `"tcp"`, `"udp"`, etc) + **Returns (sync version):** `table` or `fail` - `[1, 2, 3, ..., n]` : `table` - `addr` : `string` @@ -3128,6 +3135,9 @@ both. Equivalent to `getnameinfo(3)`. +When specified, `family` must be one of `"unix"`, `"inet"`, `"inet6"`, `"ipx"`, +`"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or `"packet"`. + **Returns (sync version):** `string, string` or `fail` **Returns (async version):** `uv_getnameinfo_t userdata` or `fail`