Skip to content

Commit

Permalink
Sock: Improve inet_pton()/inet_ntop() to handle IPv4 addresses as num…
Browse files Browse the repository at this point in the history
…bers.
  • Loading branch information
tnodir committed Jan 15, 2015
1 parent 502072a commit 58cbb20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/mem/sys_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ sys_buffer_write_done (lua_State *L, struct sys_buffer *sb,
}
return 1;
}
return 0;
}


Expand Down
29 changes: 22 additions & 7 deletions src/sock/sock_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,15 @@ sock_getifaddrs (lua_State *L)


/*
* Arguments: text_address (string)
* Returns: [binary_address (string)]
* Arguments: text_address (string), [ip4_tonumber (true)]
* Returns: [binary_address (string | number)]
*/
static int
sock_inet_pton (lua_State *L)
{
const char *src = luaL_checkstring(L, 1);
const int af = strchr(src, ':') ? AF_INET6 : AF_INET;
const int to_ip4 = lua_toboolean(L, 2);
const int af = (!to_ip4 && strchr(src, ':')) ? AF_INET6 : AF_INET;
struct sock_addr sa;
void *inp = sock_addr_get_inp(&sa, af);
const int in_len = sock_addr_get_inlen(af);
Expand All @@ -476,23 +477,37 @@ sock_inet_pton (lua_State *L)
&sa.u.addr, &sa.addrlen)) {
#endif
end:
lua_pushlstring(L, inp, in_len);
if (to_ip4)
lua_pushinteger(L, ntohl(*((unsigned long *) inp)));
else
lua_pushlstring(L, inp, in_len);
return 1;
}
return sys_seterror(L, 0);
}

/*
* Arguments: binary_address (string)
* Arguments: binary_address (string | number)
* Returns: [text_address (string)]
*/
static int
sock_inet_ntop (lua_State *L)
{
const int is_ip4 = (lua_type(L, 1) == LUA_TNUMBER);
unsigned long ip4;
int in_len, af;
const char *src = sock_checkladdr(L, 1, &in_len, &af);
const char *src;
char buf[48];

if (is_ip4) {
in_len = 4;
af = AF_INET;
ip4 = htonl(lua_tointeger(L, 1));
src = &ip4;
} else {
src = sock_checkladdr(L, 1, &in_len, &af);
}

#ifndef _WIN32
if (inet_ntop(af, src, buf, sizeof(buf)) == NULL)
goto err;
Expand All @@ -508,7 +523,7 @@ sock_inet_ntop (lua_State *L)
memcpy(inp, src, in_len);
sa.u.addr.sa_family = (short) af;

if (WSAAddressToString(&sa.u.addr, sl, NULL, buf, &buflen)
if (WSAAddressToStringA(&sa.u.addr, sl, NULL, buf, &buflen)
|| buflen >= sizeof(buf))
goto err;
}
Expand Down

0 comments on commit 58cbb20

Please sign in to comment.