Skip to content

Commit

Permalink
Fix protocol being treated as family in getaddrinfo inputs/outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Apr 18, 2021
1 parent af61b05 commit 5b4b190
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
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 5b4b190

Please sign in to comment.