Skip to content

Commit

Permalink
Merge pull request #538 from squeek502/protocol
Browse files Browse the repository at this point in the history
Fix protocol being treated as family in getaddrinfo inputs/outputs
  • Loading branch information
zhaozg authored Apr 18, 2021
2 parents af61b05 + af4a608 commit b85b9ef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
18 changes: 14 additions & 4 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand Down
6 changes: 6 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 5 additions & 7 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b85b9ef

Please sign in to comment.