mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-19 11:48:21 +01:00
Merge branch 'agnostic'
Seems safe to move to master.
This commit is contained in:
commit
4110e4125d
@ -160,9 +160,11 @@ Support, Manual">
|
||||
<a href="socket.html#setsize">_SETSIZE</a>,
|
||||
<a href="socket.html#source">source</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="socket.html#try">try</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="socket.html#version">_VERSION</a>.
|
||||
</blockquote>
|
||||
|
33
doc/tcp.html
33
doc/tcp.html
@ -44,6 +44,39 @@
|
||||
socket.<b>tcp()</b>
|
||||
</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>
|
||||
|
||||
<p class=note>
|
||||
Note: Before the choice between IPv4 and IPv6 happens,
|
||||
the internal socket object is invalid and therefore <a
|
||||
href=#setoption><tt>setoption</tt></a> will fail.
|
||||
</p>
|
||||
|
||||
<!-- socket.tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
<p class=name id="socket.tcp4">
|
||||
socket.<b>tcp4()</b>
|
||||
</p>
|
||||
|
||||
<p class=description>
|
||||
Creates and returns an IPv4 TCP master object. A master object can
|
||||
be transformed into a server object with the method
|
||||
|
47
doc/udp.html
47
doc/udp.html
@ -45,6 +45,49 @@
|
||||
socket.<b>udp()</b>
|
||||
</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>
|
||||
|
||||
<p class=note>
|
||||
Note: Before the choice between IPv4 and IPv6 happens,
|
||||
the internal socket object is invalid and therefore <a
|
||||
href=#setoption><tt>setoption</tt></a> will fail.
|
||||
</p>
|
||||
|
||||
<!-- socket.udp4 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
<p class="name" id="socket.udp">
|
||||
socket.<b>udp4()</b>
|
||||
</p>
|
||||
|
||||
<p class="description">
|
||||
Creates and returns an unconnected IPv4 UDP object.
|
||||
Unconnected objects support the
|
||||
@ -102,10 +145,6 @@ Note: The TCP object returned will have the option
|
||||
"<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>.
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
<!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
<!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
<p class="name" id="close">
|
||||
|
117
src/inet.c
117
src/inet.c
@ -94,7 +94,7 @@ static int inet_global_getnameinfo(lua_State *L) {
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
|
||||
ret = getaddrinfo(host, serv, &hints, &resolved);
|
||||
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)
|
||||
{
|
||||
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)];
|
||||
}
|
||||
@ -167,7 +167,7 @@ static int inet_global_getaddrinfo(lua_State *L)
|
||||
int i = 1, ret = 0;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
ret = getaddrinfo(hostname, NULL, &hints, &resolved);
|
||||
if (ret != 0) {
|
||||
lua_pushnil(L);
|
||||
@ -198,6 +198,16 @@ static int inet_global_getaddrinfo(lua_State *L)
|
||||
lua_pushliteral(L, "inet6");
|
||||
lua_settable(L, -3);
|
||||
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_pushstring(L, hbuf);
|
||||
@ -254,12 +264,11 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family)
|
||||
}
|
||||
lua_pushstring(L, name);
|
||||
lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10));
|
||||
if (family == PF_INET) {
|
||||
lua_pushliteral(L, "inet");
|
||||
} else if (family == PF_INET6) {
|
||||
lua_pushliteral(L, "inet6");
|
||||
} else {
|
||||
lua_pushliteral(L, "uknown family");
|
||||
switch (family) {
|
||||
case AF_INET: lua_pushliteral(L, "inet"); break;
|
||||
case AF_INET6: lua_pushliteral(L, "inet6"); break;
|
||||
case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
|
||||
default: lua_pushliteral(L, "unknown"); break;
|
||||
}
|
||||
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, port);
|
||||
if (family == PF_INET) {
|
||||
lua_pushliteral(L, "inet");
|
||||
} else if (family == PF_INET6) {
|
||||
lua_pushliteral(L, "inet6");
|
||||
} else {
|
||||
lua_pushliteral(L, "uknown family");
|
||||
switch (family) {
|
||||
case AF_INET: lua_pushliteral(L, "inet"); break;
|
||||
case AF_INET6: lua_pushliteral(L, "inet6"); break;
|
||||
case AF_UNSPEC: lua_pushliteral(L, "unspec"); break;
|
||||
default: lua_pushliteral(L, "unknown"); break;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
@ -344,8 +352,13 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to create a new inet socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trycreate(p_socket ps, int family, int type) {
|
||||
return socket_strerror(socket_create(ps, family, type, 0));
|
||||
const char *inet_trycreate(p_socket ps, int family, int type, int protocol) {
|
||||
const char *err = socket_strerror(socket_create(ps, family, type, protocol));
|
||||
if (err == NULL && family == AF_INET6) {
|
||||
int yes = 1;
|
||||
setsockopt(*ps, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&yes, sizeof(yes));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------*\
|
||||
@ -354,7 +367,7 @@ const char *inet_trycreate(p_socket ps, int family, int type) {
|
||||
const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
|
||||
{
|
||||
switch (family) {
|
||||
case PF_INET: {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in sin;
|
||||
memset((char *) &sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_UNSPEC;
|
||||
@ -362,7 +375,7 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm)
|
||||
return socket_strerror(socket_connect(ps, (SA *) &sin,
|
||||
sizeof(sin), tm));
|
||||
}
|
||||
case PF_INET6: {
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 sin6;
|
||||
struct in6_addr addrany = IN6ADDR_ANY_INIT;
|
||||
memset((char *) &sin6, 0, sizeof(sin6));
|
||||
@ -383,6 +396,7 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||
{
|
||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||
const char *err = NULL;
|
||||
int current_family = *family;
|
||||
/* try resolving */
|
||||
err = socket_gaistrerror(getaddrinfo(address, serv,
|
||||
connecthints, &resolved));
|
||||
@ -397,23 +411,23 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||
* that shows up while iterating. if there was a
|
||||
* bind, all families will be the same and we will
|
||||
* not enter this branch. */
|
||||
if (*family != iterator->ai_family) {
|
||||
if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
|
||||
socket_destroy(ps);
|
||||
err = socket_strerror(socket_create(ps, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol));
|
||||
if (err != NULL) {
|
||||
freeaddrinfo(resolved);
|
||||
return err;
|
||||
}
|
||||
*family = iterator->ai_family;
|
||||
/* all sockets initially non-blocking */
|
||||
err = inet_trycreate(ps, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol);
|
||||
if (err) continue;
|
||||
current_family = iterator->ai_family;
|
||||
/* set non-blocking before connect */
|
||||
socket_setnonblocking(ps);
|
||||
}
|
||||
/* try connecting to remote address */
|
||||
err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr,
|
||||
(socklen_t) iterator->ai_addrlen, tm));
|
||||
/* if success, break out of loop */
|
||||
if (err == NULL) break;
|
||||
if (err == NULL) {
|
||||
*family = current_family;
|
||||
break;
|
||||
}
|
||||
}
|
||||
freeaddrinfo(resolved);
|
||||
/* here, if err is set, we failed */
|
||||
@ -424,14 +438,13 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||
* Tries to accept a socket
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_tryaccept(p_socket server, int family, p_socket client,
|
||||
p_timeout tm)
|
||||
{
|
||||
p_timeout tm) {
|
||||
socklen_t len;
|
||||
t_sockaddr_storage addr;
|
||||
if (family == PF_INET6) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
} else {
|
||||
len = sizeof(struct sockaddr_in);
|
||||
switch (family) {
|
||||
case AF_INET6: len = sizeof(struct sockaddr_in6); break;
|
||||
case AF_INET: len = sizeof(struct sockaddr_in); break;
|
||||
default: len = sizeof(addr); break;
|
||||
}
|
||||
return socket_strerror(socket_accept(server, client, (SA *) &addr,
|
||||
&len, tm));
|
||||
@ -440,12 +453,11 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client,
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* Tries to bind socket to (address, port)
|
||||
\*-------------------------------------------------------------------------*/
|
||||
const char *inet_trybind(p_socket ps, const char *address, const char *serv,
|
||||
struct addrinfo *bindhints)
|
||||
{
|
||||
const char *inet_trybind(p_socket ps, int *family, const char *address,
|
||||
const char *serv, struct addrinfo *bindhints) {
|
||||
struct addrinfo *iterator = NULL, *resolved = NULL;
|
||||
const char *err = NULL;
|
||||
t_socket sock = *ps;
|
||||
int current_family = *family;
|
||||
/* translate luasocket special values to C */
|
||||
if (strcmp(address, "*") == 0) address = NULL;
|
||||
if (!serv) serv = "0";
|
||||
@ -457,30 +469,27 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv,
|
||||
}
|
||||
/* iterate over resolved addresses until one is good */
|
||||
for (iterator = resolved; iterator; iterator = iterator->ai_next) {
|
||||
if(sock == SOCKET_INVALID) {
|
||||
err = socket_strerror(socket_create(&sock, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol));
|
||||
if(err)
|
||||
continue;
|
||||
if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) {
|
||||
socket_destroy(ps);
|
||||
err = inet_trycreate(ps, iterator->ai_family,
|
||||
iterator->ai_socktype, iterator->ai_protocol);
|
||||
if (err) continue;
|
||||
current_family = iterator->ai_family;
|
||||
}
|
||||
/* try binding to local address */
|
||||
err = socket_strerror(socket_bind(&sock,
|
||||
(SA *) iterator->ai_addr,
|
||||
err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr,
|
||||
(socklen_t) iterator->ai_addrlen));
|
||||
|
||||
/* keep trying unless bind succeeded */
|
||||
if (err) {
|
||||
if(sock != *ps)
|
||||
socket_destroy(&sock);
|
||||
} else {
|
||||
/* remember what we connected to, particularly the family */
|
||||
*bindhints = *iterator;
|
||||
if (err == NULL) {
|
||||
*family = current_family;
|
||||
/* set to non-blocking after bind */
|
||||
socket_setnonblocking(ps);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* cleanup and return error */
|
||||
freeaddrinfo(resolved);
|
||||
*ps = sock;
|
||||
/* here, if err is set, we failed */
|
||||
return err;
|
||||
}
|
||||
|
||||
|
@ -24,11 +24,11 @@
|
||||
|
||||
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, int protocol);
|
||||
const char *inet_tryconnect(p_socket ps, int *family, const char *address,
|
||||
const char *serv, p_timeout tm, struct addrinfo *connecthints);
|
||||
const char *inet_trybind(p_socket ps, const char *address, const char *serv,
|
||||
struct addrinfo *bindhints);
|
||||
const char *inet_trybind(p_socket ps, int *family, const char *address,
|
||||
const char *serv, struct addrinfo *bindhints);
|
||||
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);
|
||||
|
||||
|
@ -32,7 +32,7 @@ function _M.bind(host, port, backlog)
|
||||
err = "no info on address"
|
||||
for i, alt in base.ipairs(addrinfo) do
|
||||
if alt.family == "inet" then
|
||||
sock, err = socket.tcp()
|
||||
sock, err = socket.tcp4()
|
||||
else
|
||||
sock, err = socket.tcp6()
|
||||
end
|
||||
|
101
src/tcp.c
101
src/tcp.c
@ -18,6 +18,7 @@
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int global_create4(lua_State *L);
|
||||
static int global_create6(lua_State *L);
|
||||
static int global_connect(lua_State *L);
|
||||
static int meth_connect(lua_State *L);
|
||||
@ -90,6 +91,7 @@ static t_opt optset[] = {
|
||||
/* functions in library namespace */
|
||||
static luaL_Reg func[] = {
|
||||
{"tcp", global_create},
|
||||
{"tcp4", global_create4},
|
||||
{"tcp6", global_create6},
|
||||
{"connect", global_connect},
|
||||
{NULL, NULL}
|
||||
@ -213,8 +215,7 @@ static int meth_accept(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* 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);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
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_family = tcp->family;
|
||||
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) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
@ -237,8 +238,7 @@ static int meth_bind(lua_State *L)
|
||||
/*-------------------------------------------------------------------------*\
|
||||
* 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);
|
||||
const char *address = luaL_checkstring(L, 2);
|
||||
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)
|
||||
{
|
||||
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");
|
||||
return 1;
|
||||
} else if (tcp->family == AF_INET) {
|
||||
lua_pushliteral(L, "inet4");
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushliteral(L, "inet4");
|
||||
return 1;
|
||||
@ -352,37 +355,36 @@ static int meth_settimeout(lua_State *L)
|
||||
* Creates a master tcp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int tcp_create(lua_State *L, int family) {
|
||||
t_socket sock;
|
||||
const char *err = inet_trycreate(&sock, family, SOCK_STREAM);
|
||||
/* try to allocate a system socket */
|
||||
if (!err) {
|
||||
/* allocate tcp object */
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
memset(tcp, 0, sizeof(t_tcp));
|
||||
/* set its type as master object */
|
||||
auxiliar_setclass(L, "tcp{master}", -1);
|
||||
/* initialize remaining structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
if (family == PF_INET6) {
|
||||
int yes = 1;
|
||||
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
(void *)&yes, sizeof(yes));
|
||||
}
|
||||
tcp->sock = sock;
|
||||
/* if family is AF_UNSPEC, we leave the socket invalid and
|
||||
* store AF_UNSPEC into family. This will allow it to later be
|
||||
* replaced with an AF_INET6 or AF_INET socket upon first use. */
|
||||
tcp->sock = SOCKET_INVALID;
|
||||
tcp->family = family;
|
||||
io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
|
||||
(p_error) socket_ioerror, &tcp->sock);
|
||||
timeout_init(&tcp->tm, -1, -1);
|
||||
buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
tcp->family = family;
|
||||
return 1;
|
||||
} else {
|
||||
if (family != AF_UNSPEC) {
|
||||
const char *err = inet_trycreate(&tcp->sock, family, SOCK_STREAM, 0);
|
||||
if (err != NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
socket_setnonblocking(&tcp->sock);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -390,53 +392,6 @@ static int global_create6(lua_State *L) {
|
||||
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) {
|
||||
const char *remoteaddr = luaL_checkstring(L, 1);
|
||||
const char *remoteserv = luaL_checkstring(L, 2);
|
||||
@ -453,26 +408,26 @@ static int global_connect(lua_State *L) {
|
||||
timeout_init(&tcp->tm, -1, -1);
|
||||
buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
|
||||
tcp->sock = SOCKET_INVALID;
|
||||
tcp->family = PF_UNSPEC;
|
||||
tcp->family = AF_UNSPEC;
|
||||
/* allow user to pick local address and port */
|
||||
memset(&bindhints, 0, sizeof(bindhints));
|
||||
bindhints.ai_socktype = SOCK_STREAM;
|
||||
bindhints.ai_family = family;
|
||||
bindhints.ai_flags = AI_PASSIVE;
|
||||
if (localaddr) {
|
||||
err = inet_trybind(&tcp->sock, localaddr, localserv, &bindhints);
|
||||
err = inet_trybind(&tcp->sock, &tcp->family, localaddr,
|
||||
localserv, &bindhints);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
tcp->family = bindhints.ai_family;
|
||||
}
|
||||
/* try to connect to remote address and port */
|
||||
memset(&connecthints, 0, sizeof(connecthints));
|
||||
connecthints.ai_socktype = SOCK_STREAM;
|
||||
/* 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,
|
||||
&tcp->tm, &connecthints);
|
||||
if (err) {
|
||||
|
35
src/udp.c
35
src/udp.c
@ -27,6 +27,7 @@
|
||||
* Internal function prototypes
|
||||
\*=========================================================================*/
|
||||
static int global_create(lua_State *L);
|
||||
static int global_create4(lua_State *L);
|
||||
static int global_create6(lua_State *L);
|
||||
static int meth_send(lua_State *L);
|
||||
static int meth_sendto(lua_State *L);
|
||||
@ -107,6 +108,7 @@ static t_opt optget[] = {
|
||||
/* functions in library namespace */
|
||||
static luaL_Reg func[] = {
|
||||
{"udp", global_create},
|
||||
{"udp4", global_create4},
|
||||
{"udp6", global_create6},
|
||||
{NULL, NULL}
|
||||
};
|
||||
@ -264,7 +266,7 @@ static int meth_receivefrom(lua_State *L)
|
||||
static int meth_getfamily(lua_State *L)
|
||||
{
|
||||
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");
|
||||
return 1;
|
||||
} else {
|
||||
@ -363,7 +365,6 @@ static int meth_setpeername(lua_State *L) {
|
||||
inet_trydisconnect(&udp->sock, udp->family, tm);
|
||||
auxiliar_setclass(L, "udp{unconnected}", 1);
|
||||
}
|
||||
/* change class to connected or unconnected depending on address */
|
||||
lua_pushnumber(L, 1);
|
||||
return 1;
|
||||
}
|
||||
@ -391,7 +392,7 @@ static int meth_setsockname(lua_State *L) {
|
||||
bindhints.ai_socktype = SOCK_DGRAM;
|
||||
bindhints.ai_family = udp->family;
|
||||
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) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
@ -408,32 +409,32 @@ static int meth_setsockname(lua_State *L) {
|
||||
* Creates a master udp object
|
||||
\*-------------------------------------------------------------------------*/
|
||||
static int udp_create(lua_State *L, int family) {
|
||||
t_socket sock;
|
||||
const char *err = inet_trycreate(&sock, family, SOCK_DGRAM);
|
||||
/* try to allocate a system socket */
|
||||
if (!err) {
|
||||
/* allocate udp object */
|
||||
p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp));
|
||||
auxiliar_setclass(L, "udp{unconnected}", -1);
|
||||
/* initialize remaining structure fields */
|
||||
socket_setnonblocking(&sock);
|
||||
if (family == PF_INET6) {
|
||||
int yes = 1;
|
||||
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
(void *)&yes, sizeof(yes));
|
||||
}
|
||||
udp->sock = sock;
|
||||
/* if family is AF_UNSPEC, we leave the socket invalid and
|
||||
* store AF_UNSPEC into family. This will allow it to later be
|
||||
* replaced with an AF_INET6 or AF_INET socket upon first use. */
|
||||
udp->sock = SOCKET_INVALID;
|
||||
timeout_init(&udp->tm, -1, -1);
|
||||
udp->family = family;
|
||||
return 1;
|
||||
} else {
|
||||
if (family != AF_UNSPEC) {
|
||||
const char *err = inet_trycreate(&udp->sock, family, SOCK_DGRAM, 0);
|
||||
if (err != NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
socket_setnonblocking(&udp->sock);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -211,6 +211,8 @@ int socket_send(p_socket ps, const char *data, size_t count,
|
||||
err = errno;
|
||||
/* EPIPE means the connection was 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 */
|
||||
if (err == EINTR) continue;
|
||||
/* 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;
|
||||
if (err == EPIPE) return IO_CLOSED;
|
||||
if (err == EPROTOTYPE) continue;
|
||||
if (err == EINTR) continue;
|
||||
if (err != EAGAIN) 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;
|
||||
/* EPIPE means the connection was 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 */
|
||||
if (err == EINTR) continue;
|
||||
/* if failed fatal reason, report error */
|
||||
@ -410,7 +415,9 @@ const char *socket_strerror(int err) {
|
||||
case ECONNABORTED: return PIE_CONNABORTED;
|
||||
case ECONNRESET: return PIE_CONNRESET;
|
||||
case ETIMEDOUT: return PIE_TIMEDOUT;
|
||||
default: return strerror(err);
|
||||
default: {
|
||||
return strerror(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -304,15 +304,20 @@ function isclosed(c)
|
||||
end
|
||||
|
||||
function active_close()
|
||||
reconnect()
|
||||
if isclosed(data) then fail("should not be closed") end
|
||||
data:close()
|
||||
if not isclosed(data) then fail("should be closed") end
|
||||
data = nil
|
||||
local udp = socket.udp()
|
||||
local tcp = socket.tcp4()
|
||||
if isclosed(tcp) then fail("should not be closed") end
|
||||
tcp:close()
|
||||
if not isclosed(tcp) then fail("should be closed") end
|
||||
tcp = socket.tcp()
|
||||
if not isclosed(tcp) then fail("should be closed") end
|
||||
tcp = nil
|
||||
local udp = socket.udp4()
|
||||
if isclosed(udp) then fail("should not be closed") end
|
||||
udp:close()
|
||||
if not isclosed(udp) then fail("should be closed") end
|
||||
udp = socket.udp()
|
||||
if not isclosed(udp) then fail("should be closed") end
|
||||
udp = nil
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
@ -368,7 +373,7 @@ function test_selectbugs()
|
||||
pass("invalid input: ok")
|
||||
local toomany = {}
|
||||
for i = 1, socket._SETSIZE+1 do
|
||||
toomany[#toomany+1] = socket.udp()
|
||||
toomany[#toomany+1] = socket.udp4()
|
||||
end
|
||||
if #toomany > socket._SETSIZE then
|
||||
local e = pcall(socket.select, toomany, nil, 0.1)
|
||||
@ -447,16 +452,14 @@ end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function rebind_test()
|
||||
--local c ,c1 = socket.bind("localhost", 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
|
||||
assert(c,c1)
|
||||
|
||||
local i, p = c:getsockname()
|
||||
local s, e = socket.tcp()
|
||||
assert(s, e)
|
||||
s:setoption("reuseaddr", false)
|
||||
r, e = s:bind("localhost", p)
|
||||
r, e = s:bind(i, p)
|
||||
assert(not r, "managed to rebind!")
|
||||
assert(e)
|
||||
pass("ok")
|
||||
@ -593,7 +596,7 @@ function test_writeafterclose()
|
||||
while not err do
|
||||
sent, err, errsent, time = data:send(str)
|
||||
end
|
||||
assert(err == "closed", "should have returned 'closed'")
|
||||
assert(err == "closed", "got " .. err .. " instead of 'closed'")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
@ -674,6 +677,9 @@ if sock then test_methods(socket.udp6(), udp_methods)
|
||||
else io.stderr:write("Warning! IPv6 does not support!\n") end
|
||||
end
|
||||
|
||||
test("closed connection detection: ")
|
||||
test_closed()
|
||||
|
||||
test("partial receive")
|
||||
test_partialrecv()
|
||||
|
||||
@ -697,9 +703,6 @@ rebind_test()
|
||||
test("active close: ")
|
||||
active_close()
|
||||
|
||||
test("closed connection detection: ")
|
||||
test_closed()
|
||||
|
||||
test("accept function: ")
|
||||
accept_timeout()
|
||||
accept_errors()
|
||||
|
@ -1,7 +1,7 @@
|
||||
local socket = require"socket"
|
||||
local udp = socket.udp
|
||||
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:setsockname(localhost, 5062)
|
||||
|
Loading…
Reference in New Issue
Block a user