New agnostic IPv4 IPv6 functions.

Also dealing with EPROTOTYPE Yosemite seems to be throwing
at us for no reason.
This commit is contained in:
Diego Nehab 2015-08-22 19:52:01 -03:00
parent b211838648
commit 96965b179c
14 changed files with 399 additions and 352 deletions

View File

@ -160,9 +160,11 @@ Support, Manual">
<a href="socket.html#setsize">_SETSIZE</a>, <a href="socket.html#setsize">_SETSIZE</a>,
<a href="socket.html#source">source</a>, <a href="socket.html#source">source</a>,
<a href="tcp.html#socket.tcp">tcp</a>, <a href="tcp.html#socket.tcp">tcp</a>,
<a href="tcp.html#socket.tcp4">tcp4</a>,
<a href="tcp.html#socket.tcp6">tcp6</a>, <a href="tcp.html#socket.tcp6">tcp6</a>,
<a href="socket.html#try">try</a>, <a href="socket.html#try">try</a>,
<a href="udp.html#socket.udp">udp</a>, <a href="udp.html#socket.udp">udp</a>,
<a href="udp.html#socket.udp4">udp4</a>,
<a href="udp.html#socket.udp6">udp6</a>, <a href="udp.html#socket.udp6">udp6</a>,
<a href="socket.html#version">_VERSION</a>. <a href="socket.html#version">_VERSION</a>.
</blockquote> </blockquote>

View File

@ -44,6 +44,33 @@
socket.<b>tcp()</b> socket.<b>tcp()</b>
</p> </p>
<p class=description>
Creates and returns an TCP master object. A master object can
be transformed into a server object with the method
<a href=#listen><tt>listen</tt></a> (after a call to <a
href=#bind><tt>bind</tt></a>) or into a client object with
the method <a href=#connect><tt>connect</tt></a>. The only other
method supported by a master object is the
<a href=#close><tt>close</tt></a> method.</p>
<p class=return>
In case of success, a new master object is returned. In case of error,
<b><tt>nil</tt></b> is returned, followed by an error message.
</p>
<p class=note>
Note: The choice between IPv4 and IPv6 happens during a call to
<a href=#bind><tt>bind</tt></a> or <a
href=#bind><tt>connect</tt></a>, depending on the address
family obtained from the resolver.
</p>
<!-- socket.tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="socket.tcp4">
socket.<b>tcp4()</b>
</p>
<p class=description> <p class=description>
Creates and returns an IPv4 TCP master object. A master object can Creates and returns an IPv4 TCP master object. A master object can
be transformed into a server object with the method be transformed into a server object with the method

View File

@ -45,6 +45,43 @@
socket.<b>udp()</b> socket.<b>udp()</b>
</p> </p>
<p class="description">
Creates and returns an unconnected UDP object.
Unconnected objects support the
<a href="#sendto"><tt>sendto</tt></a>,
<a href="#receive"><tt>receive</tt></a>,
<a href="#receivefrom"><tt>receivefrom</tt></a>,
<a href="#getoption"><tt>getoption</tt></a>,
<a href="#getsockname"><tt>getsockname</tt></a>,
<a href="#setoption"><tt>setoption</tt></a>,
<a href="#settimeout"><tt>settimeout</tt></a>,
<a href="#setpeername"><tt>setpeername</tt></a>,
<a href="#setsockname"><tt>setsockname</tt></a>, and
<a href="#close"><tt>close</tt></a>.
The <a href="#setpeername"><tt>setpeername</tt></a>
is used to connect the object.
</p>
<p class="return">
In case of success, a new unconnected UDP object
returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
an error message.
</p>
<p class=note>
Note: The choice between IPv4 and IPv6 happens during a call to
<a href=#sendto><tt>sendto</tt></a>, <a
href=#setpeername><tt>setpeername</tt></a>, or <a
href=#setsockname><tt>sockname</tt></a>, depending on the address
family obtained from the resolver.
</p>
<!-- socket.udp4 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class="name" id="socket.udp">
socket.<b>udp4()</b>
</p>
<p class="description"> <p class="description">
Creates and returns an unconnected IPv4 UDP object. Creates and returns an unconnected IPv4 UDP object.
Unconnected objects support the Unconnected objects support the
@ -102,10 +139,6 @@ Note: The TCP object returned will have the option
"<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>. "<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>.
</p> </p>
<!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> <!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class="name" id="close"> <p class="name" id="close">

View File

@ -94,7 +94,7 @@ static int inet_global_getnameinfo(lua_State *L) {
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_family = PF_UNSPEC; hints.ai_family = AF_UNSPEC;
ret = getaddrinfo(host, serv, &hints, &resolved); ret = getaddrinfo(host, serv, &hints, &resolved);
if (ret != 0) { if (ret != 0) {
@ -146,7 +146,7 @@ static int inet_global_toip(lua_State *L)
int inet_optfamily(lua_State* L, int narg, const char* def) int inet_optfamily(lua_State* L, int narg, const char* def)
{ {
static const char* optname[] = { "unspec", "inet", "inet6", NULL }; static const char* optname[] = { "unspec", "inet", "inet6", NULL };
static int optvalue[] = { PF_UNSPEC, PF_INET, PF_INET6, 0 }; static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 };
return optvalue[luaL_checkoption(L, narg, def, optname)]; return optvalue[luaL_checkoption(L, narg, def, optname)];
} }
@ -167,7 +167,7 @@ static int inet_global_getaddrinfo(lua_State *L)
int i = 1, ret = 0; int i = 1, ret = 0;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_family = PF_UNSPEC; hints.ai_family = AF_UNSPEC;
ret = getaddrinfo(hostname, NULL, &hints, &resolved); ret = getaddrinfo(hostname, NULL, &hints, &resolved);
if (ret != 0) { if (ret != 0) {
lua_pushnil(L); lua_pushnil(L);
@ -198,6 +198,16 @@ static int inet_global_getaddrinfo(lua_State *L)
lua_pushliteral(L, "inet6"); lua_pushliteral(L, "inet6");
lua_settable(L, -3); lua_settable(L, -3);
break; break;
case AF_UNSPEC:
lua_pushliteral(L, "family");
lua_pushliteral(L, "unspec");
lua_settable(L, -3);
break;
default:
lua_pushliteral(L, "family");
lua_pushliteral(L, "unknown");
lua_settable(L, -3);
break;
} }
lua_pushliteral(L, "addr"); lua_pushliteral(L, "addr");
lua_pushstring(L, hbuf); lua_pushstring(L, hbuf);
@ -254,12 +264,11 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
} }
lua_pushstring(L, name); lua_pushstring(L, name);
lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10)); lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
if (family == PF_INET) { switch (family) {
lua_pushliteral(L, "inet"); case AF_INET: lua_pushliteral(L, "inet"); break;
} else if (family == PF_INET6) { case AF_INET6: lua_pushliteral(L, "inet6"); break;
lua_pushliteral(L, "inet6"); case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
} else { default: lua_pushliteral(L, "unknown"); break;
lua_pushliteral(L, "uknown family");
} }
return 3; return 3;
} }
@ -288,12 +297,11 @@ int inet_meth_getsockname(lua_State *L, p_socket ps, int family)
} }
lua_pushstring(L, name); lua_pushstring(L, name);
lua_pushstring(L, port); lua_pushstring(L, port);
if (family == PF_INET) { switch (family) {
lua_pushliteral(L, "inet"); case AF_INET: lua_pushliteral(L, "inet"); break;
} else if (family == PF_INET6) { case AF_INET6: lua_pushliteral(L, "inet6"); break;
lua_pushliteral(L, "inet6"); case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
} else { default: lua_pushliteral(L, "unknown"); break;
lua_pushliteral(L, "uknown family");
} }
return 3; return 3;
} }
@ -354,7 +362,7 @@ const char *inet_trycreate(p_socket ps, int family, int type) {
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
{ {
switch (family) { switch (family) {
case PF_INET: { case AF_INET: {
struct sockaddr_in sin; struct sockaddr_in sin;
memset((char *) &sin, 0, sizeof(sin)); memset((char *) &sin, 0, sizeof(sin));
sin.sin_family = AF_UNSPEC; sin.sin_family = AF_UNSPEC;
@ -362,7 +370,7 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
return socket_strerror(socket_connect(ps, (SA *) &sin, return socket_strerror(socket_connect(ps, (SA *) &sin,
sizeof(sin), tm)); sizeof(sin), tm));
} }
case PF_INET6: { case AF_INET6: {
struct sockaddr_in6 sin6; struct sockaddr_in6 sin6;
struct in6_addr addrany = IN6ADDR_ANY_INIT; struct in6_addr addrany = IN6ADDR_ANY_INIT;
memset((char *) &sin6, 0, sizeof(sin6)); memset((char *) &sin6, 0, sizeof(sin6));
@ -383,6 +391,7 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
{ {
struct addrinfo *iterator = NULL, *resolved = NULL; struct addrinfo *iterator = NULL, *resolved = NULL;
const char *err = NULL; const char *err = NULL;
int current_family = *family;
/* try resolving */ /* try resolving */
err = socket_gaistrerror(getaddrinfo(address, serv, err = socket_gaistrerror(getaddrinfo(address, serv,
connecthints, &resolved)); connecthints, &resolved));
@ -397,23 +406,23 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
* that shows up while iterating. if there was a * that shows up while iterating. if there was a
* bind, all families will be the same and we will * bind, all families will be the same and we will
* not enter this branch. */ * not enter this branch. */
if (*family != iterator->ai_family) { if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
socket_destroy(ps); socket_destroy(ps);
err = socket_strerror(socket_create(ps, iterator->ai_family, err = socket_strerror(socket_create(ps, iterator->ai_family,
iterator->ai_socktype, iterator->ai_protocol)); iterator->ai_socktype, iterator->ai_protocol));
if (err != NULL) { if (err) continue;
freeaddrinfo(resolved); current_family = iterator->ai_family;
return err; /* set non-blocking before connect */
}
*family = iterator->ai_family;
/* all sockets initially non-blocking */
socket_setnonblocking(ps); socket_setnonblocking(ps);
} }
/* try connecting to remote address */ /* try connecting to remote address */
err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr,
(socklen_t) iterator->ai_addrlen, tm)); (socklen_t) iterator->ai_addrlen, tm));
/* if success, break out of loop */ /* if success, break out of loop */
if (err == NULL) break; if (err == NULL) {
*family = current_family;
break;
}
} }
freeaddrinfo(resolved); freeaddrinfo(resolved);
/* here, if err is set, we failed */ /* here, if err is set, we failed */
@ -424,14 +433,13 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
* Tries to accept a socket * Tries to accept a socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *inet_tryaccept(p_socket server, int family, p_socket client, const char *inet_tryaccept(p_socket server, int family, p_socket client,
p_timeout tm) p_timeout tm) {
{
socklen_t len; socklen_t len;
t_sockaddr_storage addr; t_sockaddr_storage addr;
if (family == PF_INET6) { switch (family) {
len = sizeof(struct sockaddr_in6); case AF_INET6: len = sizeof(struct sockaddr_in6); break;
} else { case AF_INET: len = sizeof(struct sockaddr_in); break;
len = sizeof(struct sockaddr_in); default: len = sizeof(addr); break;
} }
return socket_strerror(socket_accept(server, client, (SA *) &addr, return socket_strerror(socket_accept(server, client, (SA *) &addr,
&len, tm)); &len, tm));
@ -440,12 +448,11 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Tries to bind socket to (address, port) * Tries to bind socket to (address, port)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
const char *inet_trybind(p_socket ps, const char *address, const char *serv, const char *inet_trybind(p_socket ps, int *family, const char *address,
struct addrinfo *bindhints) const char *serv, struct addrinfo *bindhints) {
{
struct addrinfo *iterator = NULL, *resolved = NULL; struct addrinfo *iterator = NULL, *resolved = NULL;
const char *err = NULL; const char *err = NULL;
t_socket sock = *ps; int current_family = *family;
/* translate luasocket special values to C */ /* translate luasocket special values to C */
if (strcmp(address, "*") == 0) address = NULL; if (strcmp(address, "*") == 0) address = NULL;
if (!serv) serv = "0"; if (!serv) serv = "0";
@ -457,30 +464,27 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv,
} }
/* iterate over resolved addresses until one is good */ /* iterate over resolved addresses until one is good */
for (iterator = resolved; iterator; iterator = iterator->ai_next) { for (iterator = resolved; iterator; iterator = iterator->ai_next) {
if(sock == SOCKET_INVALID) { if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
err = socket_strerror(socket_create(&sock, iterator->ai_family, socket_destroy(ps);
err = socket_strerror(socket_create(ps, iterator->ai_family,
iterator->ai_socktype, iterator->ai_protocol)); iterator->ai_socktype, iterator->ai_protocol));
if(err) if (err) continue;
continue; current_family = iterator->ai_family;
} }
/* try binding to local address */ /* try binding to local address */
err = socket_strerror(socket_bind(&sock, err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr,
(SA *) iterator->ai_addr,
(socklen_t) iterator->ai_addrlen)); (socklen_t) iterator->ai_addrlen));
/* keep trying unless bind succeeded */ /* keep trying unless bind succeeded */
if (err) { if (err == NULL) {
if(sock != *ps) *family = current_family;
socket_destroy(&sock); /* set to non-blocking after bind */
} else { socket_setnonblocking(ps);
/* remember what we connected to, particularly the family */
*bindhints = *iterator;
break; break;
} }
} }
/* cleanup and return error */ /* cleanup and return error */
freeaddrinfo(resolved); freeaddrinfo(resolved);
*ps = sock; /* here, if err is set, we failed */
return err; return err;
} }

View File

@ -27,8 +27,8 @@ int inet_open(lua_State *L);
const char *inet_trycreate(p_socket ps, int family, int type); const char *inet_trycreate(p_socket ps, int family, int type);
const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *inet_tryconnect(p_socket ps, int *family, const char *address,
const char *serv, p_timeout tm, struct addrinfo *connecthints); const char *serv, p_timeout tm, struct addrinfo *connecthints);
const char *inet_trybind(p_socket ps, const char *address, const char *serv, const char *inet_trybind(p_socket ps, int *family, const char *address,
struct addrinfo *bindhints); const char *serv, struct addrinfo *bindhints);
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm);
const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm); const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm);

View File

@ -32,7 +32,7 @@ function _M.bind(host, port, backlog)
err = "no info on address" err = "no info on address"
for i, alt in base.ipairs(addrinfo) do for i, alt in base.ipairs(addrinfo) do
if alt.family == "inet" then if alt.family == "inet" then
sock, err = socket.tcp() sock, err = socket.tcp4()
else else
sock, err = socket.tcp6() sock, err = socket.tcp6()
end end

View File

@ -18,6 +18,7 @@
* Internal function prototypes * Internal function prototypes
\*=========================================================================*/ \*=========================================================================*/
static int global_create(lua_State *L); static int global_create(lua_State *L);
static int global_create4(lua_State *L);
static int global_create6(lua_State *L); static int global_create6(lua_State *L);
static int global_connect(lua_State *L); static int global_connect(lua_State *L);
static int meth_connect(lua_State *L); static int meth_connect(lua_State *L);
@ -90,6 +91,7 @@ static t_opt optset[] = {
/* functions in library namespace */ /* functions in library namespace */
static luaL_Reg func[] = { static luaL_Reg func[] = {
{"tcp", global_create}, {"tcp", global_create},
{"tcp4", global_create4},
{"tcp6", global_create6}, {"tcp6", global_create6},
{"connect", global_connect}, {"connect", global_connect},
{NULL, NULL} {NULL, NULL}
@ -213,8 +215,7 @@ static int meth_accept(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Binds an object to an address * Binds an object to an address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_bind(lua_State *L) static int meth_bind(lua_State *L) {
{
p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1);
const char *address = luaL_checkstring(L, 2); const char *address = luaL_checkstring(L, 2);
const char *port = luaL_checkstring(L, 3); const char *port = luaL_checkstring(L, 3);
@ -224,7 +225,7 @@ static int meth_bind(lua_State *L)
bindhints.ai_socktype = SOCK_STREAM; bindhints.ai_socktype = SOCK_STREAM;
bindhints.ai_family = tcp->family; bindhints.ai_family = tcp->family;
bindhints.ai_flags = AI_PASSIVE; bindhints.ai_flags = AI_PASSIVE;
err = inet_trybind(&tcp->sock, address, port, &bindhints); err = inet_trybind(&tcp->sock, &tcp->family, address, port, &bindhints);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
@ -237,8 +238,7 @@ static int meth_bind(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Turns a master tcp object into a client object. * Turns a master tcp object into a client object.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_connect(lua_State *L) static int meth_connect(lua_State *L) {
{
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
const char *address = luaL_checkstring(L, 2); const char *address = luaL_checkstring(L, 2);
const char *port = luaL_checkstring(L, 3); const char *port = luaL_checkstring(L, 3);
@ -279,9 +279,12 @@ static int meth_close(lua_State *L)
static int meth_getfamily(lua_State *L) static int meth_getfamily(lua_State *L)
{ {
p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
if (tcp->family == PF_INET6) { if (tcp->family == AF_INET6) {
lua_pushliteral(L, "inet6"); lua_pushliteral(L, "inet6");
return 1; return 1;
} else if (tcp->family == AF_INET) {
lua_pushliteral(L, "inet4");
return 1;
} else { } else {
lua_pushliteral(L, "inet4"); lua_pushliteral(L, "inet4");
return 1; return 1;
@ -353,7 +356,12 @@ static int meth_settimeout(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int tcp_create(lua_State *L, int family) { static int tcp_create(lua_State *L, int family) {
t_socket sock; t_socket sock;
const char *err = inet_trycreate(&sock, family, SOCK_STREAM); /* if family is AF_UNSPEC, we create an AF_INET socket
* but store AF_UNSPEC into tcp-family. This will allow it
* later be replaced with an AF_INET6 socket if
* trybind or tryconnect prefer it instead. */
const char *err = inet_trycreate(&sock, family == AF_UNSPEC?
AF_INET: family, SOCK_STREAM);
/* try to allocate a system socket */ /* try to allocate a system socket */
if (!err) { if (!err) {
/* allocate tcp object */ /* allocate tcp object */
@ -363,7 +371,7 @@ static int tcp_create(lua_State *L, int family) {
auxiliar_setclass(L, "tcp{master}", -1); auxiliar_setclass(L, "tcp{master}", -1);
/* initialize remaining structure fields */ /* initialize remaining structure fields */
socket_setnonblocking(&sock); socket_setnonblocking(&sock);
if (family == PF_INET6) { if (family == AF_INET6) {
int yes = 1; int yes = 1;
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
(void *)&yes, sizeof(yes)); (void *)&yes, sizeof(yes));
@ -383,6 +391,10 @@ static int tcp_create(lua_State *L, int family) {
} }
static int global_create(lua_State *L) { static int global_create(lua_State *L) {
return tcp_create(L, AF_UNSPEC);
}
static int global_create4(lua_State *L) {
return tcp_create(L, AF_INET); return tcp_create(L, AF_INET);
} }
@ -390,53 +402,6 @@ static int global_create6(lua_State *L) {
return tcp_create(L, AF_INET6); return tcp_create(L, AF_INET6);
} }
#if 0
static const char *tryconnect6(const char *remoteaddr, const char *remoteserv,
struct addrinfo *connecthints, p_tcp tcp) {
struct addrinfo *iterator = NULL, *resolved = NULL;
const char *err = NULL;
/* try resolving */
err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv,
connecthints, &resolved));
if (err != NULL) {
if (resolved) freeaddrinfo(resolved);
return err;
}
/* iterate over all returned addresses trying to connect */
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
p_timeout tm = timeout_markstart(&tcp->tm);
/* create new socket if necessary. if there was no
* bind, we need to create one for every new family
* that shows up while iterating. if there was a
* bind, all families will be the same and we will
* not enter this branch. */
if (tcp->family != iterator->ai_family) {
socket_destroy(&tcp->sock);
err = socket_strerror(socket_create(&tcp->sock,
iterator->ai_family, iterator->ai_socktype,
iterator->ai_protocol));
if (err != NULL) {
freeaddrinfo(resolved);
return err;
}
tcp->family = iterator->ai_family;
/* all sockets initially non-blocking */
socket_setnonblocking(&tcp->sock);
}
/* finally try connecting to remote address */
err = socket_strerror(socket_connect(&tcp->sock,
(SA *) iterator->ai_addr,
(socklen_t) iterator->ai_addrlen, tm));
/* if success, break out of loop */
if (err == NULL) break;
}
freeaddrinfo(resolved);
/* here, if err is set, we failed */
return err;
}
#endif
static int global_connect(lua_State *L) { static int global_connect(lua_State *L) {
const char *remoteaddr = luaL_checkstring(L, 1); const char *remoteaddr = luaL_checkstring(L, 1);
const char *remoteserv = luaL_checkstring(L, 2); const char *remoteserv = luaL_checkstring(L, 2);
@ -453,26 +418,26 @@ static int global_connect(lua_State *L) {
timeout_init(&tcp->tm, -1, -1); timeout_init(&tcp->tm, -1, -1);
buffer_init(&tcp->buf, &tcp->io, &tcp->tm); buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
tcp->sock = SOCKET_INVALID; tcp->sock = SOCKET_INVALID;
tcp->family = PF_UNSPEC; tcp->family = AF_UNSPEC;
/* allow user to pick local address and port */ /* allow user to pick local address and port */
memset(&bindhints, 0, sizeof(bindhints)); memset(&bindhints, 0, sizeof(bindhints));
bindhints.ai_socktype = SOCK_STREAM; bindhints.ai_socktype = SOCK_STREAM;
bindhints.ai_family = family; bindhints.ai_family = family;
bindhints.ai_flags = AI_PASSIVE; bindhints.ai_flags = AI_PASSIVE;
if (localaddr) { if (localaddr) {
err = inet_trybind(&tcp->sock, localaddr, localserv, &bindhints); err = inet_trybind(&tcp->sock, &tcp->family, localaddr,
localserv, &bindhints);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
return 2; return 2;
} }
tcp->family = bindhints.ai_family;
} }
/* try to connect to remote address and port */ /* try to connect to remote address and port */
memset(&connecthints, 0, sizeof(connecthints)); memset(&connecthints, 0, sizeof(connecthints));
connecthints.ai_socktype = SOCK_STREAM; connecthints.ai_socktype = SOCK_STREAM;
/* make sure we try to connect only to the same family */ /* make sure we try to connect only to the same family */
connecthints.ai_family = bindhints.ai_family; connecthints.ai_family = tcp->family;
err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv,
&tcp->tm, &connecthints); &tcp->tm, &connecthints);
if (err) { if (err) {

View File

@ -27,6 +27,7 @@
* Internal function prototypes * Internal function prototypes
\*=========================================================================*/ \*=========================================================================*/
static int global_create(lua_State *L); static int global_create(lua_State *L);
static int global_create4(lua_State *L);
static int global_create6(lua_State *L); static int global_create6(lua_State *L);
static int meth_send(lua_State *L); static int meth_send(lua_State *L);
static int meth_sendto(lua_State *L); static int meth_sendto(lua_State *L);
@ -107,6 +108,7 @@ static t_opt optget[] = {
/* functions in library namespace */ /* functions in library namespace */
static luaL_Reg func[] = { static luaL_Reg func[] = {
{"udp", global_create}, {"udp", global_create},
{"udp4", global_create4},
{"udp6", global_create6}, {"udp6", global_create6},
{NULL, NULL} {NULL, NULL}
}; };
@ -264,7 +266,7 @@ static int meth_receivefrom(lua_State *L)
static int meth_getfamily(lua_State *L) static int meth_getfamily(lua_State *L)
{ {
p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
if (udp->family == PF_INET6) { if (udp->family == AF_INET6) {
lua_pushliteral(L, "inet6"); lua_pushliteral(L, "inet6");
return 1; return 1;
} else { } else {
@ -391,7 +393,7 @@ static int meth_setsockname(lua_State *L) {
bindhints.ai_socktype = SOCK_DGRAM; bindhints.ai_socktype = SOCK_DGRAM;
bindhints.ai_family = udp->family; bindhints.ai_family = udp->family;
bindhints.ai_flags = AI_PASSIVE; bindhints.ai_flags = AI_PASSIVE;
err = inet_trybind(&udp->sock, address, port, &bindhints); err = inet_trybind(&udp->sock, &udp->family, address, port, &bindhints);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
@ -409,7 +411,12 @@ static int meth_setsockname(lua_State *L) {
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int udp_create(lua_State *L, int family) { static int udp_create(lua_State *L, int family) {
t_socket sock; t_socket sock;
const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); /* if family is AF_UNSPEC, we create an AF_INET socket
* but store AF_UNSPEC into tcp-family. This will allow it
* later be replaced with an AF_INET6 socket if
* trybind or tryconnect prefer it instead. */
const char *err = inet_trycreate(&sock, family == AF_UNSPEC?
AF_INET: family, SOCK_DGRAM);
/* try to allocate a system socket */ /* try to allocate a system socket */
if (!err) { if (!err) {
/* allocate udp object */ /* allocate udp object */
@ -417,7 +424,7 @@ static int udp_create(lua_State *L, int family) {
auxiliar_setclass(L, "udp{unconnected}", -1); auxiliar_setclass(L, "udp{unconnected}", -1);
/* initialize remaining structure fields */ /* initialize remaining structure fields */
socket_setnonblocking(&sock); socket_setnonblocking(&sock);
if (family == PF_INET6) { if (family == AF_INET6) {
int yes = 1; int yes = 1;
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
(void *)&yes, sizeof(yes)); (void *)&yes, sizeof(yes));
@ -434,6 +441,10 @@ static int udp_create(lua_State *L, int family) {
} }
static int global_create(lua_State *L) { static int global_create(lua_State *L) {
return udp_create(L, AF_UNSPEC);
}
static int global_create4(lua_State *L) {
return udp_create(L, AF_INET); return udp_create(L, AF_INET);
} }

View File

@ -211,6 +211,8 @@ int socket_send(p_socket ps, const char *data, size_t count,
err = errno; err = errno;
/* EPIPE means the connection was closed */ /* EPIPE means the connection was closed */
if (err == EPIPE) return IO_CLOSED; if (err == EPIPE) return IO_CLOSED;
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
if (err == EPROTOTYPE) continue;
/* we call was interrupted, just try again */ /* we call was interrupted, just try again */
if (err == EINTR) continue; if (err == EINTR) continue;
/* if failed fatal reason, report error */ /* if failed fatal reason, report error */
@ -239,6 +241,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent,
} }
err = errno; err = errno;
if (err == EPIPE) return IO_CLOSED; if (err == EPIPE) return IO_CLOSED;
if (err == EPROTOTYPE) continue;
if (err == EINTR) continue; if (err == EINTR) continue;
if (err != EAGAIN) return err; if (err != EAGAIN) return err;
if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err;
@ -317,6 +320,8 @@ int socket_write(p_socket ps, const char *data, size_t count,
err = errno; err = errno;
/* EPIPE means the connection was closed */ /* EPIPE means the connection was closed */
if (err == EPIPE) return IO_CLOSED; if (err == EPIPE) return IO_CLOSED;
/* EPROTOTYPE means the connection is being closed (on Yosemite!)*/
if (err == EPROTOTYPE) continue;
/* we call was interrupted, just try again */ /* we call was interrupted, just try again */
if (err == EINTR) continue; if (err == EINTR) continue;
/* if failed fatal reason, report error */ /* if failed fatal reason, report error */
@ -410,7 +415,9 @@ const char *socket_strerror(int err) {
case ECONNABORTED: return PIE_CONNABORTED; case ECONNABORTED: return PIE_CONNABORTED;
case ECONNRESET: return PIE_CONNRESET; case ECONNRESET: return PIE_CONNRESET;
case ETIMEDOUT: return PIE_TIMEDOUT; case ETIMEDOUT: return PIE_TIMEDOUT;
default: return strerror(err); default: {
return strerror(err);
}
} }
} }

View File

@ -447,16 +447,14 @@ end
------------------------------------------------------------------------ ------------------------------------------------------------------------
function rebind_test() function rebind_test()
--local c ,c1 = socket.bind("localhost", 0)
local c ,c1 = socket.bind("127.0.0.1", 0) local c ,c1 = socket.bind("127.0.0.1", 0)
if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end
assert(c,c1) assert(c,c1)
local i, p = c:getsockname() local i, p = c:getsockname()
local s, e = socket.tcp() local s, e = socket.tcp()
assert(s, e) assert(s, e)
s:setoption("reuseaddr", false) s:setoption("reuseaddr", false)
r, e = s:bind("localhost", p) r, e = s:bind(i, p)
assert(not r, "managed to rebind!") assert(not r, "managed to rebind!")
assert(e) assert(e)
pass("ok") pass("ok")
@ -593,7 +591,7 @@ function test_writeafterclose()
while not err do while not err do
sent, err, errsent, time = data:send(str) sent, err, errsent, time = data:send(str)
end end
assert(err == "closed", "should have returned 'closed'") assert(err == "closed", "got " .. err .. " instead of 'closed'")
pass("ok") pass("ok")
end end
@ -674,6 +672,9 @@ if sock then test_methods(socket.udp6(), udp_methods)
else io.stderr:write("Warning! IPv6 does not support!\n") end else io.stderr:write("Warning! IPv6 does not support!\n") end
end end
test("closed connection detection: ")
test_closed()
test("partial receive") test("partial receive")
test_partialrecv() test_partialrecv()
@ -697,9 +698,6 @@ rebind_test()
test("active close: ") test("active close: ")
active_close() active_close()
test("closed connection detection: ")
test_closed()
test("accept function: ") test("accept function: ")
accept_timeout() accept_timeout()
accept_errors() accept_errors()

View File

@ -1,7 +1,7 @@
local socket = require"socket" local socket = require"socket"
local udp = socket.udp local udp = socket.udp
local localhost = "127.0.0.1" local localhost = "127.0.0.1"
local port = arg[1] local port = assert(arg[1], "missing port argument")
se = udp(); se:setoption("reuseaddr", true) se = udp(); se:setoption("reuseaddr", true)
se:setsockname(localhost, 5062) se:setsockname(localhost, 5062)