aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunknown <diego.nehab@gmail.com>2013-05-27 20:32:54 +0800
committerunknown <diego.nehab@gmail.com>2013-05-27 20:32:54 +0800
commit5e0b56b8d30c574c6519495ba3fcdd245d54f5a0 (patch)
treeedaf8ad1549fb1c0c4dc2de0bbdcf24b9a2e3462
parent26704061a4e28eff573f02297e6da045d166afa4 (diff)
parentbd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff)
downloadluasocket-5e0b56b8d30c574c6519495ba3fcdd245d54f5a0.tar.gz
luasocket-5e0b56b8d30c574c6519495ba3fcdd245d54f5a0.tar.bz2
luasocket-5e0b56b8d30c574c6519495ba3fcdd245d54f5a0.zip
Merge branch 'moteus' of https://github.com/moteus/luasocket into moteus
-rw-r--r--src/inet.c24
-rw-r--r--src/tcp.c1
-rw-r--r--test/test_bind.lua6
-rw-r--r--test/test_getaddrinfo.lua15
-rw-r--r--test/testclnt.lua10
5 files changed, 50 insertions, 6 deletions
diff --git a/src/inet.c b/src/inet.c
index 51e8cfe..eab325e 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -176,9 +176,24 @@ static int inet_global_getaddrinfo(lua_State *L)
176 } 176 }
177 lua_newtable(L); 177 lua_newtable(L);
178 for (iterator = resolved; iterator; iterator = iterator->ai_next) { 178 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
179 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 179 char hbuf[NI_MAXHOST]
180 getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, 180#ifndef _WINDOWS
181 (socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST); 181 ,sbuf[NI_MAXSERV]
182#endif
183 ;
184 ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf,
185 (socklen_t) sizeof(hbuf),
186#ifdef _WINDOWS
187 NULL, 0,
188#else
189 sbuf, 0,
190#endif
191 NI_NUMERICHOST);
192 if(ret){
193 lua_pushnil(L);
194 lua_pushstring(L, socket_gaistrerror(ret));
195 return 2;
196 }
182 lua_pushnumber(L, i); 197 lua_pushnumber(L, i);
183 lua_newtable(L); 198 lua_newtable(L);
184 switch (iterator->ai_family) { 199 switch (iterator->ai_family) {
@@ -463,6 +478,9 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv,
463 struct addrinfo *iterator = NULL, *resolved = NULL; 478 struct addrinfo *iterator = NULL, *resolved = NULL;
464 const char *err = NULL; 479 const char *err = NULL;
465 t_socket sock = *ps; 480 t_socket sock = *ps;
481 /* translate luasocket special values to C */
482 if (strcmp(address, "*") == 0) address = NULL;
483 if (!serv) serv = "0";
466 /* try resolving */ 484 /* try resolving */
467 err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); 485 err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved));
468 if (err) { 486 if (err) {
diff --git a/src/tcp.c b/src/tcp.c
index ca8eec2..60c1e8a 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -222,7 +222,6 @@ static int meth_bind(lua_State *L)
222 bindhints.ai_socktype = SOCK_STREAM; 222 bindhints.ai_socktype = SOCK_STREAM;
223 bindhints.ai_family = tcp->family; 223 bindhints.ai_family = tcp->family;
224 bindhints.ai_flags = AI_PASSIVE; 224 bindhints.ai_flags = AI_PASSIVE;
225 address = strcmp(address, "*")? address: NULL;
226 err = inet_trybind(&tcp->sock, address, port, &bindhints); 225 err = inet_trybind(&tcp->sock, address, port, &bindhints);
227 if (err) { 226 if (err) {
228 lua_pushnil(L); 227 lua_pushnil(L);
diff --git a/test/test_bind.lua b/test/test_bind.lua
new file mode 100644
index 0000000..93c42d7
--- /dev/null
+++ b/test/test_bind.lua
@@ -0,0 +1,6 @@
1local socket = require "socket"
2local u = socket.udp() assert(u:setsockname("*", 5088)) u:close()
3local u = socket.udp() assert(u:setsockname("*", 0)) u:close()
4local t = socket.tcp() assert(t:bind("*", 5088)) t:close()
5local t = socket.tcp() assert(t:bind("*", 0)) t:close()
6print("done!") \ No newline at end of file
diff --git a/test/test_getaddrinfo.lua b/test/test_getaddrinfo.lua
new file mode 100644
index 0000000..4b52ff9
--- /dev/null
+++ b/test/test_getaddrinfo.lua
@@ -0,0 +1,15 @@
1local socket = require "socket"
2local addresses = assert(socket.dns.getaddrinfo("localhost"))
3assert(type(addresses) == 'table')
4
5local ipv4mask = "^%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$"
6
7for i, alt in ipairs(addresses) do
8 if alt.family == 'inet' then
9 assert(type(alt.addr) == 'string')
10 assert(alt.addr:find(ipv4mask))
11 assert(alt.addr == '127.0.0.1')
12 end
13end
14
15print("done!")
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 8acb3d0..315783b 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -642,7 +642,10 @@ local tcp_methods = {
642 "shutdown", 642 "shutdown",
643} 643}
644test_methods(socket.tcp(), tcp_methods) 644test_methods(socket.tcp(), tcp_methods)
645test_methods(socket.tcp6(), tcp_methods) 645do local sock = socket.tcp6()
646if sock then test_methods(socket.tcp6(), tcp_methods)
647else io.stderr:write("Warning! IPv6 does not support!\n") end
648end
646 649
647local udp_methods = { 650local udp_methods = {
648 "close", 651 "close",
@@ -666,7 +669,10 @@ local udp_methods = {
666 669
667------------------------------------------------------------------------ 670------------------------------------------------------------------------
668test_methods(socket.udp(), udp_methods) 671test_methods(socket.udp(), udp_methods)
669test_methods(socket.udp6(), udp_methods) 672do local sock = socket.tcp6()
673if sock then test_methods(socket.udp6(), udp_methods)
674else io.stderr:write("Warning! IPv6 does not support!\n") end
675end
670 676
671test("partial receive") 677test("partial receive")
672test_partialrecv() 678test_partialrecv()