diff options
author | unknown <diego.nehab@gmail.com> | 2013-05-27 20:32:54 +0800 |
---|---|---|
committer | unknown <diego.nehab@gmail.com> | 2013-05-27 20:32:54 +0800 |
commit | 5e0b56b8d30c574c6519495ba3fcdd245d54f5a0 (patch) | |
tree | edaf8ad1549fb1c0c4dc2de0bbdcf24b9a2e3462 | |
parent | 26704061a4e28eff573f02297e6da045d166afa4 (diff) | |
parent | bd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff) | |
download | luasocket-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.c | 24 | ||||
-rw-r--r-- | src/tcp.c | 1 | ||||
-rw-r--r-- | test/test_bind.lua | 6 | ||||
-rw-r--r-- | test/test_getaddrinfo.lua | 15 | ||||
-rw-r--r-- | test/testclnt.lua | 10 |
5 files changed, 50 insertions, 6 deletions
@@ -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) { |
@@ -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 @@ | |||
1 | local socket = require "socket" | ||
2 | local u = socket.udp() assert(u:setsockname("*", 5088)) u:close() | ||
3 | local u = socket.udp() assert(u:setsockname("*", 0)) u:close() | ||
4 | local t = socket.tcp() assert(t:bind("*", 5088)) t:close() | ||
5 | local t = socket.tcp() assert(t:bind("*", 0)) t:close() | ||
6 | print("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 @@ | |||
1 | local socket = require "socket" | ||
2 | local addresses = assert(socket.dns.getaddrinfo("localhost")) | ||
3 | assert(type(addresses) == 'table') | ||
4 | |||
5 | local ipv4mask = "^%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$" | ||
6 | |||
7 | for 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 | ||
13 | end | ||
14 | |||
15 | print("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 | } |
644 | test_methods(socket.tcp(), tcp_methods) | 644 | test_methods(socket.tcp(), tcp_methods) |
645 | test_methods(socket.tcp6(), tcp_methods) | 645 | do local sock = socket.tcp6() |
646 | if sock then test_methods(socket.tcp6(), tcp_methods) | ||
647 | else io.stderr:write("Warning! IPv6 does not support!\n") end | ||
648 | end | ||
646 | 649 | ||
647 | local udp_methods = { | 650 | local udp_methods = { |
648 | "close", | 651 | "close", |
@@ -666,7 +669,10 @@ local udp_methods = { | |||
666 | 669 | ||
667 | ------------------------------------------------------------------------ | 670 | ------------------------------------------------------------------------ |
668 | test_methods(socket.udp(), udp_methods) | 671 | test_methods(socket.udp(), udp_methods) |
669 | test_methods(socket.udp6(), udp_methods) | 672 | do local sock = socket.tcp6() |
673 | if sock then test_methods(socket.udp6(), udp_methods) | ||
674 | else io.stderr:write("Warning! IPv6 does not support!\n") end | ||
675 | end | ||
670 | 676 | ||
671 | test("partial receive") | 677 | test("partial receive") |
672 | test_partialrecv() | 678 | test_partialrecv() |