diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/inet.c | 116 | ||||
-rw-r--r-- | src/inet.h | 11 | ||||
-rw-r--r-- | src/makefile | 139 | ||||
-rw-r--r-- | src/tcp.c | 19 | ||||
-rw-r--r-- | src/udp.c | 37 | ||||
-rw-r--r-- | src/wsocket.c | 8 | ||||
-rw-r--r-- | src/wsocket.h | 4 |
7 files changed, 234 insertions, 100 deletions
@@ -79,24 +79,22 @@ static int inet_global_tohostname(lua_State *L) { | |||
79 | } | 79 | } |
80 | 80 | ||
81 | static int inet_global_getnameinfo(lua_State *L) { | 81 | static int inet_global_getnameinfo(lua_State *L) { |
82 | char hbuf[NI_MAXHOST]; | ||
83 | char sbuf[NI_MAXSERV]; | ||
82 | int i, ret; | 84 | int i, ret; |
83 | char host[1024]; | ||
84 | char serv[32]; | ||
85 | struct addrinfo hints; | 85 | struct addrinfo hints; |
86 | struct addrinfo *resolved, *iter; | 86 | struct addrinfo *resolved, *iter; |
87 | const char *node = luaL_optstring(L, 1, NULL); | 87 | const char *host = luaL_optstring(L, 1, NULL); |
88 | const char *service = luaL_optstring(L, 2, NULL); | 88 | const char *serv = luaL_optstring(L, 2, NULL); |
89 | 89 | ||
90 | if (!(node || service)) | 90 | if (!(host || serv)) |
91 | luaL_error(L, "You have to specify a hostname, a service, or both"); | 91 | luaL_error(L, "host and serv cannot be both nil"); |
92 | 92 | ||
93 | memset(&hints, 0, sizeof(hints)); | 93 | memset(&hints, 0, sizeof(hints)); |
94 | hints.ai_socktype = SOCK_STREAM; | 94 | hints.ai_socktype = SOCK_STREAM; |
95 | hints.ai_family = PF_UNSPEC; | 95 | hints.ai_family = PF_UNSPEC; |
96 | 96 | ||
97 | /* getaddrinfo must get a node and a service argument */ | 97 | ret = getaddrinfo(host, serv, &hints, &resolved); |
98 | ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", | ||
99 | &hints, &resolved); | ||
100 | if (ret != 0) { | 98 | if (ret != 0) { |
101 | lua_pushnil(L); | 99 | lua_pushnil(L); |
102 | lua_pushstring(L, socket_gaistrerror(ret)); | 100 | lua_pushstring(L, socket_gaistrerror(ret)); |
@@ -105,19 +103,19 @@ static int inet_global_getnameinfo(lua_State *L) { | |||
105 | 103 | ||
106 | lua_newtable(L); | 104 | lua_newtable(L); |
107 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { | 105 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { |
108 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, | 106 | getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, |
109 | node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); | 107 | hbuf, host? (socklen_t) sizeof(hbuf): 0, |
110 | 108 | sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0); | |
111 | if (node) { | 109 | if (host) { |
112 | lua_pushnumber(L, i); | 110 | lua_pushnumber(L, i); |
113 | lua_pushstring(L, host); | 111 | lua_pushstring(L, hbuf); |
114 | lua_settable(L, -3); | 112 | lua_settable(L, -3); |
115 | } | 113 | } |
116 | } | 114 | } |
117 | freeaddrinfo(resolved); | 115 | freeaddrinfo(resolved); |
118 | 116 | ||
119 | if (service) { | 117 | if (serv) { |
120 | lua_pushstring(L, serv); | 118 | lua_pushstring(L, sbuf); |
121 | return 2; | 119 | return 2; |
122 | } else { | 120 | } else { |
123 | return 1; | 121 | return 1; |
@@ -176,9 +174,14 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
176 | } | 174 | } |
177 | lua_newtable(L); | 175 | lua_newtable(L); |
178 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 176 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
179 | char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; | 177 | char hbuf[NI_MAXHOST]; |
180 | getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, | 178 | ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, |
181 | (socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST); | 179 | hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST); |
180 | if (ret){ | ||
181 | lua_pushnil(L); | ||
182 | lua_pushstring(L, socket_gaistrerror(ret)); | ||
183 | return 2; | ||
184 | } | ||
182 | lua_pushnumber(L, i); | 185 | lua_pushnumber(L, i); |
183 | lua_newtable(L); | 186 | lua_newtable(L); |
184 | switch (iterator->ai_family) { | 187 | switch (iterator->ai_family) { |
@@ -203,7 +206,6 @@ static int inet_global_getaddrinfo(lua_State *L) | |||
203 | return 1; | 206 | return 1; |
204 | } | 207 | } |
205 | 208 | ||
206 | |||
207 | /*-------------------------------------------------------------------------*\ | 209 | /*-------------------------------------------------------------------------*\ |
208 | * Gets the host name | 210 | * Gets the host name |
209 | \*-------------------------------------------------------------------------*/ | 211 | \*-------------------------------------------------------------------------*/ |
@@ -222,7 +224,6 @@ static int inet_global_gethostname(lua_State *L) | |||
222 | } | 224 | } |
223 | 225 | ||
224 | 226 | ||
225 | |||
226 | /*=========================================================================*\ | 227 | /*=========================================================================*\ |
227 | * Lua methods | 228 | * Lua methods |
228 | \*=========================================================================*/ | 229 | \*=========================================================================*/ |
@@ -385,7 +386,6 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) | |||
385 | struct in6_addr addrany = IN6ADDR_ANY_INIT; | 386 | struct in6_addr addrany = IN6ADDR_ANY_INIT; |
386 | memset((char *) &sin6, 0, sizeof(sin6)); | 387 | memset((char *) &sin6, 0, sizeof(sin6)); |
387 | sin6.sin6_family = AF_UNSPEC; | 388 | sin6.sin6_family = AF_UNSPEC; |
388 | fprintf(stderr, "disconnecting\n"); | ||
389 | sin6.sin6_addr = addrany; | 389 | sin6.sin6_addr = addrany; |
390 | return socket_strerror(socket_connect(ps, (SA *) &sin6, | 390 | return socket_strerror(socket_connect(ps, (SA *) &sin6, |
391 | sizeof(sin6), tm)); | 391 | sizeof(sin6), tm)); |
@@ -397,7 +397,7 @@ fprintf(stderr, "disconnecting\n"); | |||
397 | /*-------------------------------------------------------------------------*\ | 397 | /*-------------------------------------------------------------------------*\ |
398 | * Tries to connect to remote address (address, port) | 398 | * Tries to connect to remote address (address, port) |
399 | \*-------------------------------------------------------------------------*/ | 399 | \*-------------------------------------------------------------------------*/ |
400 | const char *inet_tryconnect(p_socket ps, const char *address, | 400 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, |
401 | const char *serv, p_timeout tm, struct addrinfo *connecthints) | 401 | const char *serv, p_timeout tm, struct addrinfo *connecthints) |
402 | { | 402 | { |
403 | struct addrinfo *iterator = NULL, *resolved = NULL; | 403 | struct addrinfo *iterator = NULL, *resolved = NULL; |
@@ -411,6 +411,23 @@ const char *inet_tryconnect(p_socket ps, const char *address, | |||
411 | } | 411 | } |
412 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 412 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
413 | timeout_markstart(tm); | 413 | timeout_markstart(tm); |
414 | /* create new socket if necessary. if there was no | ||
415 | * bind, we need to create one for every new family | ||
416 | * that shows up while iterating. if there was a | ||
417 | * bind, all families will be the same and we will | ||
418 | * not enter this branch. */ | ||
419 | if (*family != iterator->ai_family) { | ||
420 | socket_destroy(ps); | ||
421 | err = socket_strerror(socket_create(ps, iterator->ai_family, | ||
422 | iterator->ai_socktype, iterator->ai_protocol)); | ||
423 | if (err != NULL) { | ||
424 | freeaddrinfo(resolved); | ||
425 | return err; | ||
426 | } | ||
427 | *family = iterator->ai_family; | ||
428 | /* all sockets initially non-blocking */ | ||
429 | socket_setnonblocking(ps); | ||
430 | } | ||
414 | /* try connecting to remote address */ | 431 | /* try connecting to remote address */ |
415 | err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, | 432 | err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, |
416 | (socklen_t) iterator->ai_addrlen, tm)); | 433 | (socklen_t) iterator->ai_addrlen, tm)); |
@@ -425,7 +442,8 @@ const char *inet_tryconnect(p_socket ps, const char *address, | |||
425 | /*-------------------------------------------------------------------------*\ | 442 | /*-------------------------------------------------------------------------*\ |
426 | * Tries to accept a socket | 443 | * Tries to accept a socket |
427 | \*-------------------------------------------------------------------------*/ | 444 | \*-------------------------------------------------------------------------*/ |
428 | const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) | 445 | const char *inet_tryaccept(p_socket server, int family, p_socket client, |
446 | p_timeout tm) | ||
429 | { | 447 | { |
430 | socklen_t len; | 448 | socklen_t len; |
431 | t_sockaddr_storage addr; | 449 | t_sockaddr_storage addr; |
@@ -446,6 +464,9 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, | |||
446 | struct addrinfo *iterator = NULL, *resolved = NULL; | 464 | struct addrinfo *iterator = NULL, *resolved = NULL; |
447 | const char *err = NULL; | 465 | const char *err = NULL; |
448 | t_socket sock = *ps; | 466 | t_socket sock = *ps; |
467 | /* translate luasocket special values to C */ | ||
468 | if (strcmp(address, "*") == 0) address = NULL; | ||
469 | if (!serv) serv = "0"; | ||
449 | /* try resolving */ | 470 | /* try resolving */ |
450 | err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); | 471 | err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); |
451 | if (err) { | 472 | if (err) { |
@@ -485,7 +506,7 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, | |||
485 | * Some systems do not provide this so that we provide our own. It's not | 506 | * Some systems do not provide this so that we provide our own. It's not |
486 | * marvelously fast, but it works just fine. | 507 | * marvelously fast, but it works just fine. |
487 | \*-------------------------------------------------------------------------*/ | 508 | \*-------------------------------------------------------------------------*/ |
488 | #ifdef INET_ATON | 509 | #ifdef LUASOCKET_INET_ATON |
489 | int inet_aton(const char *cp, struct in_addr *inp) | 510 | int inet_aton(const char *cp, struct in_addr *inp) |
490 | { | 511 | { |
491 | unsigned int a = 0, b = 0, c = 0, d = 0; | 512 | unsigned int a = 0, b = 0, c = 0, d = 0; |
@@ -507,4 +528,49 @@ int inet_aton(const char *cp, struct in_addr *inp) | |||
507 | } | 528 | } |
508 | #endif | 529 | #endif |
509 | 530 | ||
531 | /*-------------------------------------------------------------------------*\ | ||
532 | * inet_ntop/inet_pton for MinGW from | ||
533 | * http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html | ||
534 | \*-------------------------------------------------------------------------*/ | ||
535 | |||
536 | #ifdef LUASOCKET_INET_PTON | ||
537 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) | ||
538 | { | ||
539 | if (af == AF_INET) { | ||
540 | struct sockaddr_in in; | ||
541 | memset(&in, 0, sizeof(in)); | ||
542 | in.sin_family = AF_INET; | ||
543 | memcpy(&in.sin_addr, src, sizeof(struct in_addr)); | ||
544 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), | ||
545 | dst, cnt, NULL, 0, NI_NUMERICHOST); | ||
546 | return dst; | ||
547 | } else if (af == AF_INET6) { | ||
548 | struct sockaddr_in6 in; | ||
549 | memset(&in, 0, sizeof(in)); | ||
550 | in.sin6_family = AF_INET6; | ||
551 | memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); | ||
552 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), | ||
553 | dst, cnt, NULL, 0, NI_NUMERICHOST); | ||
554 | return dst; | ||
555 | } | ||
556 | return NULL; | ||
557 | } | ||
558 | |||
559 | int inet_pton(int af, const char *src, void *dst) | ||
560 | { | ||
561 | struct addrinfo hints, *res, *ressave; | ||
562 | memset(&hints, 0, sizeof(struct addrinfo)); | ||
563 | hints.ai_family = af; | ||
564 | if (getaddrinfo(src, NULL, &hints, &res) != 0) { | ||
565 | return -1; | ||
566 | } | ||
567 | ressave = res; | ||
568 | while (res) { | ||
569 | memcpy(dst, res->ai_addr, res->ai_addrlen); | ||
570 | res = res->ai_next; | ||
571 | } | ||
572 | freeaddrinfo(ressave); | ||
573 | return 0; | ||
574 | } | ||
510 | 575 | ||
576 | #endif | ||
@@ -19,13 +19,13 @@ | |||
19 | #include "timeout.h" | 19 | #include "timeout.h" |
20 | 20 | ||
21 | #ifdef _WIN32 | 21 | #ifdef _WIN32 |
22 | #define INET_ATON | 22 | #define LUASOCKET_INET_ATON |
23 | #endif | 23 | #endif |
24 | 24 | ||
25 | int inet_open(lua_State *L); | 25 | int inet_open(lua_State *L); |
26 | 26 | ||
27 | const char *inet_trycreate(p_socket ps, int family, int type); | 27 | const char *inet_trycreate(p_socket ps, int family, int type); |
28 | const char *inet_tryconnect(p_socket ps, const char *address, | 28 | const char *inet_tryconnect(p_socket ps, int *family, const char *address, |
29 | const char *serv, p_timeout tm, struct addrinfo *connecthints); | 29 | const char *serv, p_timeout tm, struct addrinfo *connecthints); |
30 | const char *inet_trybind(p_socket ps, const char *address, const char *serv, | 30 | const char *inet_trybind(p_socket ps, const char *address, const char *serv, |
31 | struct addrinfo *bindhints); | 31 | struct addrinfo *bindhints); |
@@ -38,8 +38,13 @@ int inet_meth_getsockname(lua_State *L, p_socket ps, int family); | |||
38 | int inet_optfamily(lua_State* L, int narg, const char* def); | 38 | int inet_optfamily(lua_State* L, int narg, const char* def); |
39 | int inet_optsocktype(lua_State* L, int narg, const char* def); | 39 | int inet_optsocktype(lua_State* L, int narg, const char* def); |
40 | 40 | ||
41 | #ifdef INET_ATON | 41 | #ifdef LUASOCKET_INET_ATON |
42 | int inet_aton(const char *cp, struct in_addr *inp); | 42 | int inet_aton(const char *cp, struct in_addr *inp); |
43 | #endif | 43 | #endif |
44 | 44 | ||
45 | #ifdef LUASOCKET_INET_PTON | ||
46 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); | ||
47 | int inet_pton(int af, const char *src, void *dst); | ||
48 | #endif | ||
49 | |||
45 | #endif /* INET_H */ | 50 | #endif /* INET_H */ |
diff --git a/src/makefile b/src/makefile index faf50fa..c44f4ef 100644 --- a/src/makefile +++ b/src/makefile | |||
@@ -12,7 +12,7 @@ | |||
12 | # | 12 | # |
13 | # make PLAT=linux DEBUG=DEBUG LUAV=5.2 prefix=/sw | 13 | # make PLAT=linux DEBUG=DEBUG LUAV=5.2 prefix=/sw |
14 | 14 | ||
15 | # PLAT: linux macosx win32 | 15 | # PLAT: linux macosx win32 mingw |
16 | # platform to build for | 16 | # platform to build for |
17 | PLAT?=linux | 17 | PLAT?=linux |
18 | 18 | ||
@@ -33,6 +33,9 @@ LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUAV) | |||
33 | # FIXME default should this default to fink or to macports? | 33 | # FIXME default should this default to fink or to macports? |
34 | # What happens when more than one Lua version is installed? | 34 | # What happens when more than one Lua version is installed? |
35 | LUAPREFIX_macosx?=/opt/local | 35 | LUAPREFIX_macosx?=/opt/local |
36 | CDIR_macosx?=lib/lua/$(LUAV) | ||
37 | LDIR_macosx?=share/lua/$(LUAV) | ||
38 | |||
36 | 39 | ||
37 | # LUAINC_linux: | 40 | # LUAINC_linux: |
38 | # /usr/include/lua$(LUAV) | 41 | # /usr/include/lua$(LUAV) |
@@ -40,21 +43,41 @@ LUAPREFIX_macosx?=/opt/local | |||
40 | # /usr/local/include/lua$(LUAV) | 43 | # /usr/local/include/lua$(LUAV) |
41 | # where lua headers are found for linux builds | 44 | # where lua headers are found for linux builds |
42 | LUAINC_linux_base?=/usr/include | 45 | LUAINC_linux_base?=/usr/include |
43 | LUAINC_linux?=$(LUAINC_linux_base)/lua$(LUAV) | 46 | LUAINC_linux?=$(LUAINC_linux_base)/lua/$(LUAV) |
44 | LUAPREFIX_linux?=/usr/local | 47 | LUAPREFIX_linux?=/usr/local |
48 | CDIR_linux?=lib/lua/$(LUAV) | ||
49 | LDIR_linux?=share/lua/$(LUAV) | ||
50 | |||
51 | # where lua headers are found for mingw builds | ||
52 | # LUAINC_mingw: | ||
53 | # /opt/local/include | ||
54 | LUAINC_mingw_base?=/usr/include | ||
55 | LUAINC_mingw?=$(LUAINC_mingw_base)/lua/$(LUAV) | ||
56 | LUALIB_mingw_base?=/usr/bin | ||
57 | LUALIB_mingw?=$(LUALIB_mingw_base)/lua/$(LUAV)/lua$(subst .,,$(LUAV)).dll | ||
58 | LUAPREFIX_mingw?=/usr | ||
59 | CDIR_mingw?=lua/$(LUAV) | ||
60 | LDIR_mingw?=lua/$(LUAV)/lua | ||
61 | |||
45 | 62 | ||
46 | # LUAINC_win32: | 63 | # LUAINC_win32: |
47 | # LUALIB_win32: | 64 | # LUALIB_win32: |
48 | # where lua headers and libraries are found for win32 builds | 65 | # where lua headers and libraries are found for win32 builds |
49 | LUAINC_win32?="../../lua-5.1.3/src" | 66 | LUAINC_win32_base?= |
50 | LUALIB_win32?="../../lua-5.1.3" | 67 | LUAINC_win32?=$(LUAINC_win32_base)/lua/$(LUAV) |
68 | PLATFORM_win32?=Release | ||
51 | LUAPREFIX_win32?= | 69 | LUAPREFIX_win32?= |
52 | # FIXME default should be where lua-for-windows puts lua | 70 | CDIR_win32?=lua/$(LUAV)/$(PLATFORM_win32) |
71 | LDIR_win32?=lua/$(LUAV)/$(PLATFORM_win32)/lua | ||
72 | LUALIB_win32?=$(LUAPREFIX_win32)/lua/$(LUAV)/$(PLATFORM_win32) | ||
53 | 73 | ||
54 | # prefix: /usr/local /usr /opt/local /sw | 74 | # prefix: /usr/local /usr /opt/local /sw |
55 | # the top of the default install tree | 75 | # the top of the default install tree |
56 | prefix?=$(LUAPREFIX_$(PLAT)) | 76 | prefix?=$(LUAPREFIX_$(PLAT)) |
57 | 77 | ||
78 | CDIR?=$(CDIR_$(PLAT)) | ||
79 | LDIR?=$(LDIR_$(PLAT)) | ||
80 | |||
58 | # DESTDIR: (no default) | 81 | # DESTDIR: (no default) |
59 | # used by package managers to install into a temporary destination | 82 | # used by package managers to install into a temporary destination |
60 | DESTDIR= | 83 | DESTDIR= |
@@ -63,13 +86,6 @@ DESTDIR= | |||
63 | # Definitions below can be overridden on the make command line, but | 86 | # Definitions below can be overridden on the make command line, but |
64 | # shouldn't have to be. | 87 | # shouldn't have to be. |
65 | 88 | ||
66 | print: | ||
67 | @echo PLAT=$(PLAT) | ||
68 | @echo LUAV=$(LUAV) | ||
69 | @echo DEBUG=$(DEBUG) | ||
70 | @echo prefix=$(prefix) | ||
71 | @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) | ||
72 | @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) | ||
73 | 89 | ||
74 | #------ | 90 | #------ |
75 | # Install directories | 91 | # Install directories |
@@ -80,18 +96,28 @@ INSTALL_DATA=install -m644 | |||
80 | INSTALL_EXEC=install | 96 | INSTALL_EXEC=install |
81 | INSTALL_TOP=$(DESTDIR)$(prefix) | 97 | INSTALL_TOP=$(DESTDIR)$(prefix) |
82 | 98 | ||
83 | INSTALL_TOP_SHARE=$(INSTALL_TOP)/share/lua/$(LUAV) | 99 | INSTALL_TOP_LDIR=$(INSTALL_TOP)/$(LDIR) |
84 | INSTALL_TOP_LIB=$(INSTALL_TOP)/lib/lua/$(LUAV) | 100 | INSTALL_TOP_CDIR=$(INSTALL_TOP)/$(CDIR) |
85 | 101 | ||
86 | INSTALL_SOCKET_SHARE=$(INSTALL_TOP_SHARE)/socket | 102 | INSTALL_SOCKET_LDIR=$(INSTALL_TOP_LDIR)/socket |
87 | INSTALL_SOCKET_LIB=$(INSTALL_TOP_LIB)/socket | 103 | INSTALL_SOCKET_CDIR=$(INSTALL_TOP_CDIR)/socket |
88 | INSTALL_MIME_SHARE=$(INSTALL_TOP_SHARE)/mime | 104 | INSTALL_MIME_LDIR=$(INSTALL_TOP_LDIR)/mime |
89 | INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime | 105 | INSTALL_MIME_CDIR=$(INSTALL_TOP_CDIR)/mime |
106 | |||
107 | print: | ||
108 | @echo PLAT=$(PLAT) | ||
109 | @echo LUAV=$(LUAV) | ||
110 | @echo DEBUG=$(DEBUG) | ||
111 | @echo prefix=$(prefix) | ||
112 | @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) | ||
113 | @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) | ||
114 | @echo INSTALL_TOP_CDIR=$(INSTALL_TOP_CDIR) | ||
115 | @echo INSTALL_TOP_LDIR=$(INSTALL_TOP_LDIR) | ||
90 | 116 | ||
91 | #------ | 117 | #------ |
92 | # Supported platforms | 118 | # Supported platforms |
93 | # | 119 | # |
94 | PLATS= macosx linux win32 | 120 | PLATS= macosx linux win32 mingw |
95 | 121 | ||
96 | #------ | 122 | #------ |
97 | # Compiler and linker settings | 123 | # Compiler and linker settings |
@@ -117,37 +143,53 @@ CC_linux=gcc | |||
117 | DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ | 143 | DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ |
118 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ | 144 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ |
119 | -DMIME_API='__attribute__((visibility("default")))' | 145 | -DMIME_API='__attribute__((visibility("default")))' |
120 | CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \ | 146 | CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ |
121 | -fvisibility=hidden | 147 | -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden |
122 | LDFLAGS_linux=-O -shared -fpic -o | 148 | LDFLAGS_linux=-O -shared -fpic -o |
123 | LD_linux=gcc | 149 | LD_linux=gcc |
124 | SOCKET_linux=usocket.o | 150 | SOCKET_linux=usocket.o |
125 | 151 | ||
126 | #------ | 152 | #------ |
127 | # Compiler and linker settings | 153 | # Compiler and linker settings |
154 | # for MingW | ||
155 | SO_mingw=dll | ||
156 | O_mingw=o | ||
157 | CC_mingw=gcc | ||
158 | DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ | ||
159 | -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \ | ||
160 | -DMIME_API='__declspec(dllexport)' | ||
161 | CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ | ||
162 | -fvisibility=hidden | ||
163 | LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lwsock32 -lws2_32 -o | ||
164 | LD_mingw=gcc | ||
165 | SOCKET_mingw=wsocket.o | ||
166 | |||
167 | |||
168 | #------ | ||
169 | # Compiler and linker settings | ||
128 | # for Win32 | 170 | # for Win32 |
129 | SO_win32=dll | 171 | SO_win32=dll |
130 | O_win32=obj | 172 | O_win32=obj |
131 | CC_win32=cl | 173 | CC_win32=cl |
132 | DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \ | 174 | DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ |
133 | /D "LUASOCKET_API=__declspec(dllexport)" /D "_CRT_SECURE_NO_WARNINGS" \ | 175 | //D "LUASOCKET_API=__declspec(dllexport)" //D "_CRT_SECURE_NO_WARNINGS" \ |
134 | /D "_WINDLL" /D "LUA_COMPAT_MODULE" /D "MIME_API=__declspec(dllexport)" \ | 176 | //D "_WINDLL" //D "LUA_COMPAT_MODULE" \ |
135 | /D "LUASOCKET_$(DEBUG)" | 177 | //D "MIME_API=__declspec(dllexport)" \ |
136 | CFLAGS_win32=/I "$(LUAINC)" $(DEF) /O2 /Ot /MD /W3 /nologo | 178 | //D "LUASOCKET_$(DEBUG)" |
137 | LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \ | 179 | CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo |
138 | /LIBPATH:"$(LUALIB)" \ | 180 | LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \ |
139 | /MANIFEST \ | 181 | //MANIFEST //MANIFESTFILE:"intermediate.manifest" \ |
140 | /MANIFESTFILE:"intermediate.manifest" \ | 182 | //MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ |
141 | /MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ | 183 | //SUBSYSTEM:WINDOWS //OPT:REF //OPT:ICF //DYNAMICBASE:NO \ |
142 | /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /DYNAMICBASE:NO \ | 184 | //MACHINE:X86 /LIBPATH:"$(shell cmd //c echo $(LUALIB))" lua$(subst .,,$(LUAV)).lib \ |
143 | /MACHINE:X86 ws2_32.lib lua$(LUAV).lib /OUT: | 185 | wsock32.lib ws2_32.lib //OUT: |
144 | LD_win32=cl | 186 | LD_win32=cl |
145 | SOCKET_win32=wsocket.obj | 187 | SOCKET_win32=wsocket.obj |
146 | 188 | ||
147 | .SUFFIXES: .obj | 189 | .SUFFIXES: .obj |
148 | 190 | ||
149 | .c.obj: | 191 | .c.obj: |
150 | $(CC) $(CFLAGS) /Fo"$@" /c $< | 192 | $(CC) $(CFLAGS) //Fo"$@" //c $< |
151 | 193 | ||
152 | #------ | 194 | #------ |
153 | # Output file names | 195 | # Output file names |
@@ -223,7 +265,7 @@ SERIAL_OBJS:=\ | |||
223 | #------ | 265 | #------ |
224 | # Files to install | 266 | # Files to install |
225 | # | 267 | # |
226 | TO_SOCKET_SHARE= \ | 268 | TO_SOCKET_LDIR= \ |
227 | http.lua \ | 269 | http.lua \ |
228 | url.lua \ | 270 | url.lua \ |
229 | tp.lua \ | 271 | tp.lua \ |
@@ -231,7 +273,7 @@ TO_SOCKET_SHARE= \ | |||
231 | headers.lua \ | 273 | headers.lua \ |
232 | smtp.lua | 274 | smtp.lua |
233 | 275 | ||
234 | TO_TOP_SHARE= \ | 276 | TO_TOP_LDIR= \ |
235 | ltn12.lua \ | 277 | ltn12.lua \ |
236 | socket.lua \ | 278 | socket.lua \ |
237 | mime.lua | 279 | mime.lua |
@@ -250,6 +292,9 @@ win32: | |||
250 | linux: | 292 | linux: |
251 | $(MAKE) all-unix PLAT=linux | 293 | $(MAKE) all-unix PLAT=linux |
252 | 294 | ||
295 | mingw: | ||
296 | $(MAKE) all PLAT=mingw | ||
297 | |||
253 | none: | 298 | none: |
254 | @echo "Please run" | 299 | @echo "Please run" |
255 | @echo " make PLATFORM" | 300 | @echo " make PLATFORM" |
@@ -273,21 +318,21 @@ $(SERIAL_SO): $(SERIAL_OBJS) | |||
273 | $(LD) $(SERIAL_OBJS) $(LDFLAGS)$@ | 318 | $(LD) $(SERIAL_OBJS) $(LDFLAGS)$@ |
274 | 319 | ||
275 | install: | 320 | install: |
276 | $(INSTALL_DIR) $(INSTALL_TOP_SHARE) | 321 | $(INSTALL_DIR) $(INSTALL_TOP_LDIR) |
277 | $(INSTALL_DATA) $(TO_TOP_SHARE) $(INSTALL_TOP_SHARE) | 322 | $(INSTALL_DATA) $(TO_TOP_LDIR) $(INSTALL_TOP_LDIR) |
278 | $(INSTALL_DIR) $(INSTALL_SOCKET_SHARE) | 323 | $(INSTALL_DIR) $(INSTALL_SOCKET_LDIR) |
279 | $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE) | 324 | $(INSTALL_DATA) $(TO_SOCKET_LDIR) $(INSTALL_SOCKET_LDIR) |
280 | $(INSTALL_DIR) $(INSTALL_SOCKET_LIB) | 325 | $(INSTALL_DIR) $(INSTALL_SOCKET_CDIR) |
281 | $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(SO) | 326 | $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_CDIR)/core.$(SO) |
282 | $(INSTALL_DIR) $(INSTALL_MIME_LIB) | 327 | $(INSTALL_DIR) $(INSTALL_MIME_CDIR) |
283 | $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(SO) | 328 | $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_CDIR)/core.$(SO) |
284 | 329 | ||
285 | install-unix: install | 330 | install-unix: install |
286 | $(INSTALL_EXEC) $(UNIX_SO) $(INSTALL_SOCKET_LIB)/$(UNIX_SO) | 331 | $(INSTALL_EXEC) $(UNIX_SO) $(INSTALL_SOCKET_CDIR)/$(UNIX_SO) |
287 | $(INSTALL_EXEC) $(SERIAL_SO) $(INSTALL_SOCKET_LIB)/$(SERIAL_SO) | 332 | $(INSTALL_EXEC) $(SERIAL_SO) $(INSTALL_SOCKET_CDIR)/$(SERIAL_SO) |
288 | 333 | ||
289 | local: | 334 | local: |
290 | $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=.. | 335 | $(MAKE) install INSTALL_TOP_CDIR=.. INSTALL_TOP_LDIR=.. |
291 | 336 | ||
292 | clean: | 337 | clean: |
293 | rm -f $(SOCKET_SO) $(SOCKET_OBJS) $(SERIAL_OBJS) | 338 | rm -f $(SOCKET_SO) $(SOCKET_OBJS) $(SERIAL_OBJS) |
@@ -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); |
@@ -248,7 +247,8 @@ static int meth_connect(lua_State *L) | |||
248 | /* make sure we try to connect only to the same family */ | 247 | /* make sure we try to connect only to the same family */ |
249 | connecthints.ai_family = tcp->family; | 248 | connecthints.ai_family = tcp->family; |
250 | timeout_markstart(&tcp->tm); | 249 | timeout_markstart(&tcp->tm); |
251 | err = inet_tryconnect(&tcp->sock, address, port, &tcp->tm, &connecthints); | 250 | err = inet_tryconnect(&tcp->sock, &tcp->family, address, port, |
251 | &tcp->tm, &connecthints); | ||
252 | /* have to set the class even if it failed due to non-blocking connects */ | 252 | /* have to set the class even if it failed due to non-blocking connects */ |
253 | auxiliar_setclass(L, "tcp{client}", 1); | 253 | auxiliar_setclass(L, "tcp{client}", 1); |
254 | if (err) { | 254 | if (err) { |
@@ -388,6 +388,7 @@ static int global_create6(lua_State *L) { | |||
388 | return tcp_create(L, AF_INET6); | 388 | return tcp_create(L, AF_INET6); |
389 | } | 389 | } |
390 | 390 | ||
391 | #if 0 | ||
391 | static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | 392 | static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, |
392 | struct addrinfo *connecthints, p_tcp tcp) { | 393 | struct addrinfo *connecthints, p_tcp tcp) { |
393 | struct addrinfo *iterator = NULL, *resolved = NULL; | 394 | struct addrinfo *iterator = NULL, *resolved = NULL; |
@@ -402,8 +403,13 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | |||
402 | /* iterate over all returned addresses trying to connect */ | 403 | /* iterate over all returned addresses trying to connect */ |
403 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { | 404 | for (iterator = resolved; iterator; iterator = iterator->ai_next) { |
404 | p_timeout tm = timeout_markstart(&tcp->tm); | 405 | p_timeout tm = timeout_markstart(&tcp->tm); |
405 | /* create new socket if one wasn't created by the bind stage */ | 406 | /* create new socket if necessary. if there was no |
406 | if (tcp->sock == SOCKET_INVALID) { | 407 | * bind, we need to create one for every new family |
408 | * that shows up while iterating. if there was a | ||
409 | * bind, all families will be the same and we will | ||
410 | * not enter this branch. */ | ||
411 | if (tcp->family != iterator->ai_family) { | ||
412 | socket_destroy(&tcp->sock); | ||
407 | err = socket_strerror(socket_create(&tcp->sock, | 413 | err = socket_strerror(socket_create(&tcp->sock, |
408 | iterator->ai_family, iterator->ai_socktype, | 414 | iterator->ai_family, iterator->ai_socktype, |
409 | iterator->ai_protocol)); | 415 | iterator->ai_protocol)); |
@@ -427,6 +433,7 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | |||
427 | /* here, if err is set, we failed */ | 433 | /* here, if err is set, we failed */ |
428 | return err; | 434 | return err; |
429 | } | 435 | } |
436 | #endif | ||
430 | 437 | ||
431 | static int global_connect(lua_State *L) { | 438 | static int global_connect(lua_State *L) { |
432 | const char *remoteaddr = luaL_checkstring(L, 1); | 439 | const char *remoteaddr = luaL_checkstring(L, 1); |
@@ -444,6 +451,7 @@ static int global_connect(lua_State *L) { | |||
444 | timeout_init(&tcp->tm, -1, -1); | 451 | timeout_init(&tcp->tm, -1, -1); |
445 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); | 452 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); |
446 | tcp->sock = SOCKET_INVALID; | 453 | tcp->sock = SOCKET_INVALID; |
454 | tcp->family = PF_UNSPEC; | ||
447 | /* allow user to pick local address and port */ | 455 | /* allow user to pick local address and port */ |
448 | memset(&bindhints, 0, sizeof(bindhints)); | 456 | memset(&bindhints, 0, sizeof(bindhints)); |
449 | bindhints.ai_socktype = SOCK_STREAM; | 457 | bindhints.ai_socktype = SOCK_STREAM; |
@@ -463,7 +471,8 @@ static int global_connect(lua_State *L) { | |||
463 | connecthints.ai_socktype = SOCK_STREAM; | 471 | connecthints.ai_socktype = SOCK_STREAM; |
464 | /* make sure we try to connect only to the same family */ | 472 | /* make sure we try to connect only to the same family */ |
465 | connecthints.ai_family = bindhints.ai_family; | 473 | connecthints.ai_family = bindhints.ai_family; |
466 | err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp); | 474 | err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, |
475 | &tcp->tm, &connecthints); | ||
467 | if (err) { | 476 | if (err) { |
468 | socket_destroy(&tcp->sock); | 477 | socket_destroy(&tcp->sock); |
469 | lua_pushnil(L); | 478 | lua_pushnil(L); |
@@ -159,7 +159,7 @@ static int meth_sendto(lua_State *L) { | |||
159 | struct sockaddr_in addr; | 159 | struct sockaddr_in addr; |
160 | memset(&addr, 0, sizeof(addr)); | 160 | memset(&addr, 0, sizeof(addr)); |
161 | if (!inet_pton(AF_INET, ip, &addr.sin_addr)) | 161 | if (!inet_pton(AF_INET, ip, &addr.sin_addr)) |
162 | luaL_argerror(L, 3, "invalid ip address"); | 162 | luaL_argerror(L, 3, "invalid ip address"); |
163 | addr.sin_family = AF_INET; | 163 | addr.sin_family = AF_INET; |
164 | addr.sin_port = htons(port); | 164 | addr.sin_port = htons(port); |
165 | timeout_markstart(tm); | 165 | timeout_markstart(tm); |
@@ -171,7 +171,7 @@ static int meth_sendto(lua_State *L) { | |||
171 | struct sockaddr_in6 addr; | 171 | struct sockaddr_in6 addr; |
172 | memset(&addr, 0, sizeof(addr)); | 172 | memset(&addr, 0, sizeof(addr)); |
173 | if (!inet_pton(AF_INET6, ip, &addr.sin6_addr)) | 173 | if (!inet_pton(AF_INET6, ip, &addr.sin6_addr)) |
174 | luaL_argerror(L, 3, "invalid ip address"); | 174 | luaL_argerror(L, 3, "invalid ip address"); |
175 | addr.sin6_family = AF_INET6; | 175 | addr.sin6_family = AF_INET6; |
176 | addr.sin6_port = htons(port); | 176 | addr.sin6_port = htons(port); |
177 | timeout_markstart(tm); | 177 | timeout_markstart(tm); |
@@ -180,9 +180,9 @@ static int meth_sendto(lua_State *L) { | |||
180 | break; | 180 | break; |
181 | } | 181 | } |
182 | default: | 182 | default: |
183 | lua_pushnil(L); | 183 | lua_pushnil(L); |
184 | lua_pushfstring(L, "unknown family %d", udp->family); | 184 | lua_pushfstring(L, "unknown family %d", udp->family); |
185 | return 2; | 185 | return 2; |
186 | } | 186 | } |
187 | if (err != IO_DONE) { | 187 | if (err != IO_DONE) { |
188 | lua_pushnil(L); | 188 | lua_pushnil(L); |
@@ -259,19 +259,19 @@ static int meth_receivefrom(lua_State *L) { | |||
259 | (SA *) &addr, &addr_len, tm); | 259 | (SA *) &addr, &addr_len, tm); |
260 | /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ | 260 | /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ |
261 | if (err == IO_CLOSED) | 261 | if (err == IO_CLOSED) |
262 | err = IO_DONE; | 262 | err = IO_DONE; |
263 | if (err == IO_DONE) { | 263 | if (err == IO_DONE) { |
264 | char addrstr[INET6_ADDRSTRLEN]; | 264 | char addrstr[INET6_ADDRSTRLEN]; |
265 | lua_pushlstring(L, buffer, got); | 265 | lua_pushlstring(L, buffer, got); |
266 | if (!inet_ntop(AF_INET6, &addr.sin6_addr, | 266 | if (!inet_ntop(AF_INET6, &addr.sin6_addr, |
267 | addrstr, sizeof(addrstr))) { | 267 | addrstr, sizeof(addrstr))) { |
268 | lua_pushnil(L); | 268 | lua_pushnil(L); |
269 | lua_pushstring(L, "invalid source address"); | 269 | lua_pushstring(L, "invalid source address"); |
270 | return 2; | 270 | return 2; |
271 | } | 271 | } |
272 | lua_pushstring(L, addrstr); | 272 | lua_pushstring(L, addrstr); |
273 | lua_pushnumber(L, ntohs(addr.sin6_port)); | 273 | lua_pushnumber(L, ntohs(addr.sin6_port)); |
274 | return 3; | 274 | return 3; |
275 | } | 275 | } |
276 | break; | 276 | break; |
277 | } | 277 | } |
@@ -376,7 +376,8 @@ static int meth_setpeername(lua_State *L) { | |||
376 | /* make sure we try to connect only to the same family */ | 376 | /* make sure we try to connect only to the same family */ |
377 | connecthints.ai_family = udp->family; | 377 | connecthints.ai_family = udp->family; |
378 | if (connecting) { | 378 | if (connecting) { |
379 | err = inet_tryconnect(&udp->sock, address, port, tm, &connecthints); | 379 | err = inet_tryconnect(&udp->sock, &udp->family, address, |
380 | port, tm, &connecthints); | ||
380 | if (err) { | 381 | if (err) { |
381 | lua_pushnil(L); | 382 | lua_pushnil(L); |
382 | lua_pushstring(L, err); | 383 | lua_pushstring(L, err); |
diff --git a/src/wsocket.c b/src/wsocket.c index d6dd004..65f76bc 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -400,13 +400,17 @@ const char *socket_gaistrerror(int err) { | |||
400 | case EAI_MEMORY: return "memory allocation failure"; | 400 | case EAI_MEMORY: return "memory allocation failure"; |
401 | case EAI_NONAME: | 401 | case EAI_NONAME: |
402 | return "host or service not provided, or not known"; | 402 | return "host or service not provided, or not known"; |
403 | // case EAI_OVERFLOW: return "argument buffer overflow"; | 403 | #ifdef EAI_OVERFLOW |
404 | case EAI_OVERFLOW: return "argument buffer overflow"; | ||
405 | #endif | ||
404 | #ifdef EAI_PROTOCOL | 406 | #ifdef EAI_PROTOCOL |
405 | case EAI_PROTOCOL: return "resolved protocol is unknown"; | 407 | case EAI_PROTOCOL: return "resolved protocol is unknown"; |
406 | #endif | 408 | #endif |
407 | case EAI_SERVICE: return "service not supported for socket type"; | 409 | case EAI_SERVICE: return "service not supported for socket type"; |
408 | case EAI_SOCKTYPE: return "ai_socktype not supported"; | 410 | case EAI_SOCKTYPE: return "ai_socktype not supported"; |
409 | // case EAI_SYSTEM: return strerror(errno); | 411 | #ifdef EAI_SYSTEM |
412 | case EAI_SYSTEM: return strerror(errno); | ||
413 | #endif | ||
410 | default: return gai_strerror(err); | 414 | default: return gai_strerror(err); |
411 | } | 415 | } |
412 | } | 416 | } |
diff --git a/src/wsocket.h b/src/wsocket.h index 0783b00..8fbc54d 100644 --- a/src/wsocket.h +++ b/src/wsocket.h | |||
@@ -16,6 +16,10 @@ typedef SOCKADDR_STORAGE t_sockaddr_storage; | |||
16 | typedef SOCKET t_socket; | 16 | typedef SOCKET t_socket; |
17 | typedef t_socket *p_socket; | 17 | typedef t_socket *p_socket; |
18 | 18 | ||
19 | #ifndef IPV6_V6ONLY | ||
20 | #define IPV6_V6ONLY 27 | ||
21 | #endif | ||
22 | |||
19 | #define SOCKET_INVALID (INVALID_SOCKET) | 23 | #define SOCKET_INVALID (INVALID_SOCKET) |
20 | 24 | ||
21 | #ifndef SO_REUSEPORT | 25 | #ifndef SO_REUSEPORT |