aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2012-04-11 13:21:25 -0700
committerSam Roberts <vieuxtech@gmail.com>2012-04-11 13:25:11 -0700
commit2778766d678b147fc079d67dee036346381b4764 (patch)
treeda44507f62fb9c8cd078cf25f6dc24107e56af34 /src
parent3a8ba90dfb0c2eb224f317dd692ede426691e72a (diff)
downloadluasocket-2778766d678b147fc079d67dee036346381b4764.tar.gz
luasocket-2778766d678b147fc079d67dee036346381b4764.tar.bz2
luasocket-2778766d678b147fc079d67dee036346381b4764.zip
Preliminary IPv6 support for v2.1
Diffstat (limited to 'src')
-rw-r--r--src/auxiliar.c2
-rw-r--r--src/buffer.c2
-rw-r--r--src/except.c2
-rw-r--r--src/ftp.lua2
-rw-r--r--src/headers.lua100
-rw-r--r--src/http.lua2
-rw-r--r--src/inet.c52
-rw-r--r--src/io.c2
-rw-r--r--src/ltn12.lua2
-rw-r--r--src/luasocket.h4
-rw-r--r--src/mime.c2
-rw-r--r--src/mime.lua2
-rw-r--r--src/options.c2
-rw-r--r--src/select.c13
-rw-r--r--src/smtp.lua2
-rw-r--r--src/socket.h1
-rw-r--r--src/socket.lua2
-rw-r--r--src/tcp.c181
-rw-r--r--src/timeout.c2
-rw-r--r--src/tp.lua2
-rw-r--r--src/udp.c2
-rw-r--r--src/unix.c2
-rw-r--r--src/url.lua12
-rw-r--r--src/usocket.c23
-rw-r--r--src/wsocket.c2
25 files changed, 381 insertions, 39 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c
index fcc7e23..9514970 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -2,7 +2,7 @@
2* Auxiliar routines for class hierarchy manipulation 2* Auxiliar routines for class hierarchy manipulation
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: auxiliar.c,v 1.14 2005/10/07 04:40:59 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8#include <stdio.h> 8#include <stdio.h>
diff --git a/src/buffer.c b/src/buffer.c
index 5be0faf..fbe00eb 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2,7 +2,7 @@
2* Input/Output interface for Lua programs 2* Input/Output interface for Lua programs
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: buffer.c,v 1.29 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include "lua.h" 7#include "lua.h"
8#include "lauxlib.h" 8#include "lauxlib.h"
diff --git a/src/except.c b/src/except.c
index fc4c038..5faa5be 100644
--- a/src/except.c
+++ b/src/except.c
@@ -2,7 +2,7 @@
2* Simple exception support 2* Simple exception support
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: except.c,v 1.8 2005/09/29 06:11:41 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8 8
diff --git a/src/ftp.lua b/src/ftp.lua
index c90a65c..1d5ff77 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -2,7 +2,7 @@
2-- FTP support for the Lua language 2-- FTP support for the Lua language
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: ftp.lua,v 1.45 2007/07/11 19:25:47 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/headers.lua b/src/headers.lua
new file mode 100644
index 0000000..f92ee7a
--- /dev/null
+++ b/src/headers.lua
@@ -0,0 +1,100 @@
1-----------------------------------------------------------------------------
2-- Canonic header field capitalization
3-- LuaSocket toolkit.
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
7module("socket.headers")
8
9canonic = {
10 ["accept"] = "Accept",
11 ["accept-charset"] = "Accept-Charset",
12 ["accept-encoding"] = "Accept-Encoding",
13 ["accept-language"] = "Accept-Language",
14 ["accept-ranges"] = "Accept-Ranges",
15 ["action"] = "Action",
16 ["alternate-recipient"] = "Alternate-Recipient",
17 ["age"] = "Age",
18 ["allow"] = "Allow",
19 ["arrival-date"] = "Arrival-Date",
20 ["authorization"] = "Authorization",
21 ["bcc"] = "Bcc",
22 ["cache-control"] = "Cache-Control",
23 ["cc"] = "Cc",
24 ["comments"] = "Comments",
25 ["connection"] = "Connection",
26 ["content-description"] = "Content-Description",
27 ["content-disposition"] = "Content-Disposition",
28 ["content-encoding"] = "Content-Encoding",
29 ["content-id"] = "Content-ID",
30 ["content-language"] = "Content-Language",
31 ["content-length"] = "Content-Length",
32 ["content-location"] = "Content-Location",
33 ["content-md5"] = "Content-MD5",
34 ["content-range"] = "Content-Range",
35 ["content-transfer-encoding"] = "Content-Transfer-Encoding",
36 ["content-type"] = "Content-Type",
37 ["date"] = "Date",
38 ["diagnostic-code"] = "Diagnostic-Code",
39 ["dsn-gateway"] = "DSN-Gateway",
40 ["etag"] = "ETag",
41 ["expect"] = "Expect",
42 ["expires"] = "Expires",
43 ["final-log-id"] = "Final-Log-ID",
44 ["final-recipient"] = "Final-Recipient",
45 ["from"] = "From",
46 ["host"] = "Host",
47 ["if-match"] = "If-Match",
48 ["if-modified-since"] = "If-Modified-Since",
49 ["if-none-match"] = "If-None-Match",
50 ["if-range"] = "If-Range",
51 ["if-unmodified-since"] = "If-Unmodified-Since",
52 ["in-reply-to"] = "In-Reply-To",
53 ["keywords"] = "Keywords",
54 ["last-attempt-date"] = "Last-Attempt-Date",
55 ["last-modified"] = "Last-Modified",
56 ["location"] = "Location",
57 ["max-forwards"] = "Max-Forwards",
58 ["message-id"] = "Message-ID",
59 ["mime-version"] = "MIME-Version",
60 ["original-envelope-id"] = "Original-Envelope-ID",
61 ["original-recipient"] = "Original-Recipient",
62 ["pragma"] = "Pragma",
63 ["proxy-authenticate"] = "Proxy-Authenticate",
64 ["proxy-authorization"] = "Proxy-Authorization",
65 ["range"] = "Range",
66 ["received"] = "Received",
67 ["received-from-mta"] = "Received-From-MTA",
68 ["references"] = "References",
69 ["referer"] = "Referer",
70 ["remote-mta"] = "Remote-MTA",
71 ["reply-to"] = "Reply-To",
72 ["reporting-mta"] = "Reporting-MTA",
73 ["resent-bcc"] = "Resent-Bcc",
74 ["resent-cc"] = "Resent-Cc",
75 ["resent-date"] = "Resent-Date",
76 ["resent-from"] = "Resent-From",
77 ["resent-message-id"] = "Resent-Message-ID",
78 ["resent-reply-to"] = "Resent-Reply-To",
79 ["resent-sender"] = "Resent-Sender",
80 ["resent-to"] = "Resent-To",
81 ["retry-after"] = "Retry-After",
82 ["return-path"] = "Return-Path",
83 ["sender"] = "Sender",
84 ["server"] = "Server",
85 ["smtp-remote-recipient"] = "SMTP-Remote-Recipient",
86 ["status"] = "Status",
87 ["subject"] = "Subject",
88 ["te"] = "TE",
89 ["to"] = "To",
90 ["trailer"] = "Trailer",
91 ["transfer-encoding"] = "Transfer-Encoding",
92 ["upgrade"] = "Upgrade",
93 ["user-agent"] = "User-Agent",
94 ["vary"] = "Vary",
95 ["via"] = "Via",
96 ["warning"] = "Warning",
97 ["will-retry-until"] = "Will-Retry-Until",
98 ["www-authenticate"] = "WWW-Authenticate",
99 ["x-mailer"] = "X-Mailer",
100}
diff --git a/src/http.lua b/src/http.lua
index 4c27149..029a367 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -2,7 +2,7 @@
2-- HTTP/1.1 client support for the Lua language. 2-- HTTP/1.1 client support for the Lua language.
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: http.lua,v 1.72 2009/05/27 09:31:35 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/inet.c b/src/inet.c
index 862288c..21a97c9 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -2,7 +2,7 @@
2* Internet domain functions 2* Internet domain functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: inet.c,v 1.28 2005/10/07 04:40:59 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8#include <string.h> 8#include <string.h>
@@ -16,14 +16,16 @@
16* Internal function prototypes. 16* Internal function prototypes.
17\*=========================================================================*/ 17\*=========================================================================*/
18static int inet_global_toip(lua_State *L); 18static int inet_global_toip(lua_State *L);
19static int inet_global_toip6(lua_State *L);
19static int inet_global_tohostname(lua_State *L); 20static int inet_global_tohostname(lua_State *L);
20static void inet_pushresolved(lua_State *L, struct hostent *hp); 21static void inet_pushresolved(lua_State *L, struct hostent *hp);
21static int inet_global_gethostname(lua_State *L); 22static int inet_global_gethostname(lua_State *L);
22 23
23/* DNS functions */ 24/* DNS functions */
24static luaL_reg func[] = { 25static luaL_reg func[] = {
25 { "toip", inet_global_toip }, 26 { "toip", inet_global_toip},
26 { "tohostname", inet_global_tohostname }, 27 { "toip6", inet_global_toip6},
28 { "tohostname", inet_global_tohostname},
27 { "gethostname", inet_global_gethostname}, 29 { "gethostname", inet_global_gethostname},
28 { NULL, NULL} 30 { NULL, NULL}
29}; 31};
@@ -95,6 +97,50 @@ static int inet_global_toip(lua_State *L)
95 return 2; 97 return 2;
96} 98}
97 99
100static int inet_global_toip6(lua_State *L)
101{
102 const char *hostname = luaL_checkstring(L, 1);
103 struct addrinfo *iterator = NULL, *resolved = NULL;
104 struct addrinfo hints;
105 int i = 1, ret = 0;
106 memset(&hints, 0, sizeof(hints));
107 hints.ai_socktype = SOCK_STREAM;
108 hints.ai_family = PF_UNSPEC;
109 ret = getaddrinfo(hostname, NULL, &hints, &resolved);
110 if (ret != 0) {
111 lua_pushnil(L);
112 lua_pushstring(L, "getaddrinfo returned error");
113 return 2;
114 }
115 lua_newtable(L);
116 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
117 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
118 getnameinfo(iterator->ai_addr, iterator->ai_addrlen, hbuf, sizeof(hbuf),
119 sbuf, 0, NI_NUMERICHOST);
120 lua_pushnumber(L, i);
121 lua_newtable(L);
122 switch (iterator->ai_family) {
123 case AF_INET:
124 lua_pushliteral(L, "family");
125 lua_pushliteral(L, "inet");
126 lua_settable(L, -3);
127 break;
128 case AF_INET6:
129 lua_pushliteral(L, "family");
130 lua_pushliteral(L, "inet6");
131 lua_settable(L, -3);
132 break;;
133 }
134 lua_pushliteral(L, "addr");
135 lua_pushstring(L, hbuf);
136 lua_settable(L, -3);
137 lua_settable(L, -3);
138 i++;
139 }
140 freeaddrinfo(resolved);
141 return 1;
142}
143
98 144
99/*-------------------------------------------------------------------------*\ 145/*-------------------------------------------------------------------------*\
100* Gets the host name 146* Gets the host name
diff --git a/src/io.c b/src/io.c
index 317cfd4..06dc50e 100644
--- a/src/io.c
+++ b/src/io.c
@@ -2,7 +2,7 @@
2* Input/Output abstraction 2* Input/Output abstraction
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: io.c,v 1.6 2005/09/29 06:11:41 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include "io.h" 7#include "io.h"
8 8
diff --git a/src/ltn12.lua b/src/ltn12.lua
index f10ad18..b42689a 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -2,7 +2,7 @@
2-- LTN12 - Filters, sources, sinks and pumps. 2-- LTN12 - Filters, sources, sinks and pumps.
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: ltn12.lua,v 1.31 2006/04/03 04:45:42 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/luasocket.h b/src/luasocket.h
index d7a78bb..3949421 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -11,8 +11,8 @@
11/*-------------------------------------------------------------------------*\ 11/*-------------------------------------------------------------------------*\
12* Current socket library version 12* Current socket library version
13\*-------------------------------------------------------------------------*/ 13\*-------------------------------------------------------------------------*/
14#define LUASOCKET_VERSION "LuaSocket 2.0.3" 14#define LUASOCKET_VERSION "LuaSocket 2.1.0"
15#define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2009 Diego Nehab" 15#define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2011 Diego Nehab"
16#define LUASOCKET_AUTHORS "Diego Nehab" 16#define LUASOCKET_AUTHORS "Diego Nehab"
17 17
18/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
diff --git a/src/mime.c b/src/mime.c
index 1e8fded..218e6f7 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -2,7 +2,7 @@
2* MIME support functions 2* MIME support functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: mime.c,v 1.29 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
diff --git a/src/mime.lua b/src/mime.lua
index eb75db2..169eda2 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -2,7 +2,7 @@
2-- MIME support for the Lua language. 2-- MIME support for the Lua language.
3-- Author: Diego Nehab 3-- Author: Diego Nehab
4-- Conforming to RFCs 2045-2049 4-- Conforming to RFCs 2045-2049
5-- RCS ID: $Id$ 5-- RCS ID: $Id: mime.lua,v 1.29 2007/06/11 23:44:54 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/options.c b/src/options.c
index 1d4c950..1f5afa8 100644
--- a/src/options.c
+++ b/src/options.c
@@ -2,7 +2,7 @@
2* Common option interface 2* Common option interface
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: options.c,v 1.7 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
diff --git a/src/select.c b/src/select.c
index 8e47d0e..0931b73 100644
--- a/src/select.c
+++ b/src/select.c
@@ -2,7 +2,7 @@
2* Select implementation 2* Select implementation
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: select.c,v 1.23 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
@@ -95,8 +95,10 @@ static t_socket getfd(lua_State *L) {
95 if (!lua_isnil(L, -1)) { 95 if (!lua_isnil(L, -1)) {
96 lua_pushvalue(L, -2); 96 lua_pushvalue(L, -2);
97 lua_call(L, 1, 1); 97 lua_call(L, 1, 1);
98 if (lua_isnumber(L, -1)) 98 if (lua_isnumber(L, -1)) {
99 fd = (t_socket) lua_tonumber(L, -1); 99 double numfd = lua_tonumber(L, -1);
100 fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID;
101 }
100 } 102 }
101 lua_pop(L, 1); 103 lua_pop(L, 1);
102 return fd; 104 return fd;
@@ -134,8 +136,13 @@ static void collect_fd(lua_State *L, int tab, int itab,
134 fd = getfd(L); 136 fd = getfd(L);
135 if (fd != SOCKET_INVALID) { 137 if (fd != SOCKET_INVALID) {
136 /* make sure we don't overflow the fd_set */ 138 /* make sure we don't overflow the fd_set */
139#ifdef _WIN32
137 if (n >= FD_SETSIZE) 140 if (n >= FD_SETSIZE)
138 luaL_argerror(L, tab, "too many sockets"); 141 luaL_argerror(L, tab, "too many sockets");
142#else
143 if (fd >= FD_SETSIZE)
144 luaL_argerror(L, tab, "descriptor too large for set size");
145#endif
139 FD_SET(fd, set); 146 FD_SET(fd, set);
140 n++; 147 n++;
141 /* keep track of the largest descriptor so far */ 148 /* keep track of the largest descriptor so far */
diff --git a/src/smtp.lua b/src/smtp.lua
index b39f5d6..e258d62 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -2,7 +2,7 @@
2-- SMTP client support for the Lua language. 2-- SMTP client support for the Lua language.
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: smtp.lua,v 1.47 2009/05/27 09:31:35 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/socket.h b/src/socket.h
index 425628d..e325952 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -59,6 +59,7 @@ int socket_accept(p_socket ps, p_socket pa, SA *addr,
59 socklen_t *addr_len, p_timeout tm); 59 socklen_t *addr_len, p_timeout tm);
60 60
61const char *socket_hoststrerror(int err); 61const char *socket_hoststrerror(int err);
62const char *socket_gaistrerror(int err);
62const char *socket_strerror(int err); 63const char *socket_strerror(int err);
63 64
64/* these are perfect to use with the io abstraction module 65/* these are perfect to use with the io abstraction module
diff --git a/src/socket.lua b/src/socket.lua
index 458418e..211adcd 100644
--- a/src/socket.lua
+++ b/src/socket.lua
@@ -1,7 +1,7 @@
1----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
2-- LuaSocket helper module 2-- LuaSocket helper module
3-- Author: Diego Nehab 3-- Author: Diego Nehab
4-- RCS ID: $Id$ 4-- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $
5----------------------------------------------------------------------------- 5-----------------------------------------------------------------------------
6 6
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
diff --git a/src/tcp.c b/src/tcp.c
index 06d687b..3204b40 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -2,7 +2,7 @@
2* TCP object 2* TCP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: tcp.c,v 1.42 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
@@ -19,6 +19,8 @@
19* Internal function prototypes 19* Internal function prototypes
20\*=========================================================================*/ 20\*=========================================================================*/
21static int global_create(lua_State *L); 21static int global_create(lua_State *L);
22static int global_connect6(lua_State *L);
23static int global_bind6(lua_State *L);
22static int meth_connect(lua_State *L); 24static int meth_connect(lua_State *L);
23static int meth_listen(lua_State *L); 25static int meth_listen(lua_State *L);
24static int meth_bind(lua_State *L); 26static int meth_bind(lua_State *L);
@@ -75,6 +77,8 @@ static t_opt optset[] = {
75/* functions in library namespace */ 77/* functions in library namespace */
76static luaL_reg func[] = { 78static luaL_reg func[] = {
77 {"tcp", global_create}, 79 {"tcp", global_create},
80 {"connect6", global_connect6},
81 {"bind6", global_bind6},
78 {NULL, NULL} 82 {NULL, NULL}
79}; 83};
80 84
@@ -208,6 +212,7 @@ static int meth_bind(lua_State *L)
208\*-------------------------------------------------------------------------*/ 212\*-------------------------------------------------------------------------*/
209static int meth_connect(lua_State *L) 213static int meth_connect(lua_State *L)
210{ 214{
215
211 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); 216 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
212 const char *address = luaL_checkstring(L, 2); 217 const char *address = luaL_checkstring(L, 2);
213 unsigned short port = (unsigned short) luaL_checknumber(L, 3); 218 unsigned short port = (unsigned short) luaL_checknumber(L, 3);
@@ -220,7 +225,6 @@ static int meth_connect(lua_State *L)
220 lua_pushstring(L, err); 225 lua_pushstring(L, err);
221 return 2; 226 return 2;
222 } 227 }
223 /* turn master object into a client object */
224 lua_pushnumber(L, 1); 228 lua_pushnumber(L, 1);
225 return 1; 229 return 1;
226} 230}
@@ -313,8 +317,7 @@ static int meth_settimeout(lua_State *L)
313/*-------------------------------------------------------------------------*\ 317/*-------------------------------------------------------------------------*\
314* Creates a master tcp object 318* Creates a master tcp object
315\*-------------------------------------------------------------------------*/ 319\*-------------------------------------------------------------------------*/
316static int global_create(lua_State *L) 320static int global_create(lua_State *L) {
317{
318 t_socket sock; 321 t_socket sock;
319 const char *err = inet_trycreate(&sock, SOCK_STREAM); 322 const char *err = inet_trycreate(&sock, SOCK_STREAM);
320 /* try to allocate a system socket */ 323 /* try to allocate a system socket */
@@ -337,3 +340,173 @@ static int global_create(lua_State *L)
337 return 2; 340 return 2;
338 } 341 }
339} 342}
343
344static const char *trybind6(const char *localaddr, const char *localserv,
345 struct addrinfo *bindhints, p_tcp tcp) {
346 struct addrinfo *iterator = NULL, *resolved = NULL;
347 const char *err = NULL;
348 /* translate luasocket special values to C */
349 if (strcmp(localaddr, "*") == 0) localaddr = NULL;
350 if (!localserv) localserv = "0";
351 /* try resolving */
352 err = socket_gaistrerror(getaddrinfo(localaddr, localserv,
353 bindhints, &resolved));
354 if (err) {
355 if (resolved) freeaddrinfo(resolved);
356 return err;
357 }
358 /* iterate over resolved addresses until one is good */
359 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
360 /* create a new socket each time because parameters
361 * may have changed */
362 const char *err = socket_strerror(socket_create(&tcp->sock,
363 iterator->ai_family, iterator->ai_socktype,
364 iterator->ai_protocol));
365 /* if failed to create socket, bail out */
366 if (err != NULL) {
367 freeaddrinfo(resolved);
368 return err;
369 }
370 /* all sockets are set as non-blocking initially */
371 socket_setnonblocking(&tcp->sock);
372 /* try binding to local address */
373 err = socket_strerror(socket_bind(&tcp->sock,
374 (SA *) iterator->ai_addr,
375 iterator->ai_addrlen));
376 /* if faiiled, we try the next one */
377 if (err != NULL) socket_destroy(&tcp->sock);
378 /* if success, we abort loop */
379 else break;
380 }
381 /* at this point, if err is not set, se succeeded */
382 if (err == NULL) {
383 /* save family of chosen local address */
384 bindhints->ai_family = iterator->ai_family;
385 }
386 /* cleanup and return error */
387 freeaddrinfo(resolved);
388 return err;
389}
390
391static int global_bind6(lua_State *L) {
392 const char *localaddr = luaL_checkstring(L, 1);
393 const char *localserv = luaL_checkstring(L, 2);
394 int backlog = luaL_checkint(L, 3);
395 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
396 struct addrinfo bindhints;
397 const char *err = NULL;
398 /* initialize tcp structure */
399 io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
400 (p_error) socket_ioerror, &tcp->sock);
401 timeout_init(&tcp->tm, -1, -1);
402 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
403 tcp->sock = SOCKET_INVALID;
404 /* try binding to local address */
405 memset(&bindhints, 0, sizeof(bindhints));
406 bindhints.ai_socktype = SOCK_STREAM;
407 bindhints.ai_family = PF_UNSPEC;
408 bindhints.ai_flags = AI_PASSIVE;
409 err = trybind6(localaddr, localserv, &bindhints, tcp);
410 if (err == NULL) {
411 /* all server sockets initially with reuseaddr set */
412 int val = 1;
413 setsockopt(tcp->sock, SOL_SOCKET, SO_REUSEADDR,
414 (char *) &val, sizeof(val));
415 /* set the backlog and listen */
416 err = socket_strerror(socket_listen(&tcp->sock, backlog));
417 if (err) {
418 socket_destroy(&tcp->sock);
419 lua_pushnil(L);
420 lua_pushstring(L, err);
421 return 2;
422 }
423 auxiliar_setclass(L, "tcp{server}", -1);
424 return 1;
425 } else {
426 lua_pushnil(L);
427 lua_pushstring(L, err);
428 return 2;
429 }
430}
431
432static const char *tryconnect6(const char *remoteaddr, const char *remoteserv,
433 struct addrinfo *connecthints, p_tcp tcp) {
434 struct addrinfo *iterator = NULL, *resolved = NULL;
435 const char *err = NULL;
436 /* try resolving */
437 err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv,
438 connecthints, &resolved));
439 if (err != NULL) {
440 if (resolved) freeaddrinfo(resolved);
441 return err;
442 }
443 /* iterate over all returned addresses trying to connect */
444 for (iterator = resolved; iterator; iterator = iterator->ai_next) {
445 p_timeout tm = timeout_markstart(&tcp->tm);
446 /* create new socket if one wasn't created by the bind stage */
447 if (tcp->sock == SOCKET_INVALID) {
448 const char *err = socket_strerror(socket_create(&tcp->sock,
449 iterator->ai_family, iterator->ai_socktype,
450 iterator->ai_protocol));
451 if (err != NULL) {
452 freeaddrinfo(resolved);
453 return err;
454 }
455 /* all sockets initially non-blocking */
456 socket_setnonblocking(&tcp->sock);
457 }
458 /* finally try connecting to remote address */
459 err = socket_strerror(socket_connect(&tcp->sock,
460 (SA *) iterator->ai_addr,
461 iterator->ai_addrlen, tm));
462 /* if success, break out of loop */
463 if (err == NULL) break;
464 }
465
466 freeaddrinfo(resolved);
467 /* here, if err is set, we failed */
468 return err;
469}
470
471static int global_connect6(lua_State *L) {
472 const char *remoteaddr = luaL_checkstring(L, 1);
473 const char *remoteserv = luaL_checkstring(L, 2);
474 const char *localaddr = luaL_optstring(L, 3, NULL);
475 const char *localserv = luaL_optstring(L, 4, "0");
476 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
477 struct addrinfo bindhints, connecthints;
478 const char *err = NULL;
479 /* initialize tcp structure */
480 io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv,
481 (p_error) socket_ioerror, &tcp->sock);
482 timeout_init(&tcp->tm, -1, -1);
483 buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
484 tcp->sock = SOCKET_INVALID;
485 /* allow user to pick local address and port */
486 memset(&bindhints, 0, sizeof(bindhints));
487 bindhints.ai_socktype = SOCK_STREAM;
488 bindhints.ai_family = PF_UNSPEC;
489 bindhints.ai_flags = AI_PASSIVE;
490 if (localaddr) {
491 err = trybind6(localaddr, localserv, &bindhints, tcp);
492 if (err) {
493 lua_pushnil(L);
494 lua_pushstring(L, err);
495 return 2;
496 }
497 }
498 /* try to connect to remote address and port */
499 memset(&connecthints, 0, sizeof(connecthints));
500 connecthints.ai_socktype = SOCK_STREAM;
501 /* make sure we try to connect only to the same family */
502 connecthints.ai_family = bindhints.ai_family;
503 err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp);
504 if (err) {
505 socket_destroy(&tcp->sock);
506 lua_pushnil(L);
507 lua_pushstring(L, err);
508 return 2;
509 }
510 auxiliar_setclass(L, "tcp{client}", -1);
511 return 1;
512}
diff --git a/src/timeout.c b/src/timeout.c
index fb015f9..cc7309c 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -2,7 +2,7 @@
2* Timeout management functions 2* Timeout management functions
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: timeout.c,v 1.31 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8#include <limits.h> 8#include <limits.h>
diff --git a/src/tp.lua b/src/tp.lua
index 46f7f64..adeb4b0 100644
--- a/src/tp.lua
+++ b/src/tp.lua
@@ -2,7 +2,7 @@
2-- Unified SMTP/FTP subsystem 2-- Unified SMTP/FTP subsystem
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: tp.lua,v 1.23 2009/05/27 09:31:35 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
diff --git a/src/udp.c b/src/udp.c
index db519ca..e3c86e7 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -2,7 +2,7 @@
2* UDP object 2* UDP object
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: udp.c,v 1.30 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
diff --git a/src/unix.c b/src/unix.c
index a309823..6962517 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -2,7 +2,7 @@
2* Unix domain socket 2* Unix domain socket
3* LuaSocket toolkit 3* LuaSocket toolkit
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id: unix.c,v 1.14 2009/05/27 09:31:35 diego Exp $
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
diff --git a/src/url.lua b/src/url.lua
index 7623557..2158796 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -2,7 +2,7 @@
2-- URI parsing, composition and relative URL resolution 2-- URI parsing, composition and relative URL resolution
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id: url.lua,v 1.38 2006/04/03 04:45:42 diego Exp $
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
@@ -195,13 +195,7 @@ function build(parsed)
195 end 195 end
196 if userinfo then authority = userinfo .. "@" .. authority end 196 if userinfo then authority = userinfo .. "@" .. authority end
197 end 197 end
198 if authority then 198 if authority then url = "//" .. authority .. url end
199 if string.sub(url, 1, 1) == "/" then
200 url = "//" .. authority .. url
201 else
202 url = "//" .. authority .. "/" .. url
203 end
204 end
205 if parsed.scheme then url = parsed.scheme .. ":" .. url end 199 if parsed.scheme then url = parsed.scheme .. ":" .. url end
206 if parsed.fragment then url = url .. "#" .. parsed.fragment end 200 if parsed.fragment then url = url .. "#" .. parsed.fragment end
207 -- url = string.gsub(url, "%s", "") 201 -- url = string.gsub(url, "%s", "")
@@ -217,8 +211,8 @@ end
217-- corresponding absolute url 211-- corresponding absolute url
218----------------------------------------------------------------------------- 212-----------------------------------------------------------------------------
219function absolute(base_url, relative_url) 213function absolute(base_url, relative_url)
220 local base_parsed = base_url
221 if base.type(base_url) == "table" then 214 if base.type(base_url) == "table" then
215 base_parsed = base_url
222 base_url = build(base_parsed) 216 base_url = build(base_parsed)
223 else 217 else
224 base_parsed = parse(base_url) 218 base_parsed = parse(base_url)
diff --git a/src/usocket.c b/src/usocket.c
index 1ba1043..5201b7b 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -6,7 +6,7 @@
6* The penalty of calling select to avoid busy-wait is only paid when 6* The penalty of calling select to avoid busy-wait is only paid when
7* the I/O call fail in the first place. 7* the I/O call fail in the first place.
8* 8*
9* RCS ID: $Id$ 9* RCS ID: $Id: usocket.c,v 1.38 2007/10/13 23:55:20 diego Exp $
10\*=========================================================================*/ 10\*=========================================================================*/
11#include <string.h> 11#include <string.h>
12#include <signal.h> 12#include <signal.h>
@@ -368,3 +368,24 @@ const char *socket_ioerror(p_socket ps, int err) {
368 (void) ps; 368 (void) ps;
369 return socket_strerror(err); 369 return socket_strerror(err);
370} 370}
371
372const char *socket_gaistrerror(int err) {
373 if (err == 0) return NULL;
374 switch (err) {
375 case EAI_AGAIN: return "temporary failure in name resolution";
376 case EAI_BADFLAGS: return "invalid value for ai_flags";
377 case EAI_BADHINTS: return "invalid value for hints";
378 case EAI_FAIL: return "non-recoverable failure in name resolution";
379 case EAI_FAMILY: return "ai_family not supported";
380 case EAI_MEMORY: return "memory allocation failure";
381 case EAI_NONAME:
382 return "hostname or servname not provided, or not known";
383 case EAI_OVERFLOW: return "argument buffer overflow";
384 case EAI_PROTOCOL: return "resolved protocol is unknown";
385 case EAI_SERVICE: return "servname not supported for socktype";
386 case EAI_SOCKTYPE: return "ai_socktype not supported";
387 case EAI_SYSTEM: return strerror(errno);
388 default: return "unknown error";
389 }
390}
391
diff --git a/src/wsocket.c b/src/wsocket.c
index 2d07904..c82882a 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -5,7 +5,7 @@
5* The penalty of calling select to avoid busy-wait is only paid when 5* The penalty of calling select to avoid busy-wait is only paid when
6* the I/O call fail in the first place. 6* the I/O call fail in the first place.
7* 7*
8* RCS ID: $Id$ 8* RCS ID: $Id: wsocket.c,v 1.36 2007/06/11 23:44:54 diego Exp $
9\*=========================================================================*/ 9\*=========================================================================*/
10#include <string.h> 10#include <string.h>
11 11