Add IPv6 support to udp:sendto()

This commit is contained in:
Florian Zeitz 2012-07-17 19:02:20 +02:00
parent 05535a19f8
commit a6cf48596d

View File

@ -153,16 +153,37 @@ static int meth_sendto(lua_State *L) {
const char *ip = luaL_checkstring(L, 3); const char *ip = luaL_checkstring(L, 3);
unsigned short port = (unsigned short) luaL_checknumber(L, 4); unsigned short port = (unsigned short) luaL_checknumber(L, 4);
p_timeout tm = &udp->tm; p_timeout tm = &udp->tm;
struct sockaddr_in addr;
int err; int err;
memset(&addr, 0, sizeof(addr)); switch (udp->family) {
if (!inet_aton(ip, &addr.sin_addr)) case PF_INET: {
luaL_argerror(L, 3, "invalid ip address"); struct sockaddr_in addr;
addr.sin_family = AF_INET; memset(&addr, 0, sizeof(addr));
addr.sin_port = htons(port); if (!inet_pton(AF_INET, ip, &addr.sin_addr))
timeout_markstart(tm); luaL_argerror(L, 3, "invalid ip address");
err = socket_sendto(&udp->sock, data, count, &sent, addr.sin_family = AF_INET;
(SA *) &addr, sizeof(addr), tm); addr.sin_port = htons(port);
timeout_markstart(tm);
err = socket_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm);
break;
}
case PF_INET6: {
struct sockaddr_in6 addr;
memset(&addr, 0, sizeof(addr));
if (!inet_pton(AF_INET6, ip, &addr.sin6_addr))
luaL_argerror(L, 3, "invalid ip address");
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(port);
timeout_markstart(tm);
err = socket_sendto(&udp->sock, data, count, &sent,
(SA *) &addr, sizeof(addr), tm);
break;
}
default:
lua_pushnil(L);
lua_pushfstring(L, "unknown family %d", udp->family);
return 2;
}
if (err != IO_DONE) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, udp_strerror(err)); lua_pushstring(L, udp_strerror(err));