diff --git a/.gitignore b/.gitignore index 0e0b11f..8307483 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,16 @@ *.o *.so *.so.* -macosx.cmd -win32.cmd -linux.cmd +*.obj +*.lib +*.dll* +*.user +*.sdf +Lua.props +Debug +Release +*.manifest +*.swp +*.suo +x64 diff --git a/Lua51.props b/Lua51.props new file mode 100644 index 0000000..1bd6256 --- /dev/null +++ b/Lua51.props @@ -0,0 +1,28 @@ + + + + + ..\build\vc12\bin\lua\5.1\ + ..\build\vc12\bin\lua\5.1\ + ..\build\vc12\include\lua\5.1\ + lua51.lib + + + <_PropertySheetDisplayName>Lua51 + + + + + $(LUALIB_PATH) + + + $(LUABIN_PATH) + + + $(LUAINC_PATH) + + + $(LUALIB) + + + diff --git a/Lua52.props b/Lua52.props new file mode 100644 index 0000000..01afcfa --- /dev/null +++ b/Lua52.props @@ -0,0 +1,28 @@ + + + + + ..\build\vc12\bin\lua\5.2\ + ..\build\vc12\bin\lua\5.2\ + ..\build\vc12\include\lua\5.2\ + lua52.lib + + + <_PropertySheetDisplayName>Lua52 + + + + + $(LUALIB_PATH) + + + $(LUABIN_PATH) + + + $(LUAINC_PATH) + + + $(LUALIB) + + + diff --git a/linux.cmd b/linux.cmd new file mode 100644 index 0000000..bd59adc --- /dev/null +++ b/linux.cmd @@ -0,0 +1 @@ +make PLAT=linux DEBUG=DEBUG LUAINC_linux_base=/home/diego/build/linux/include LUAPREFIX_linux=/home/diego/build/linux diff --git a/macosx.cmd b/macosx.cmd new file mode 100644 index 0000000..46a0709 --- /dev/null +++ b/macosx.cmd @@ -0,0 +1 @@ +make DEBUG=DEBUG PLAT=macosx LUAINC_macosx_base=/Users/diego/build/macosx/include LUAPREFIX_macosx=/Users/diego/build/macosx install-both diff --git a/makefile b/makefile index f9fa6fe..04cd894 100644 --- a/makefile +++ b/makefile @@ -3,13 +3,14 @@ # see src/makefile for description of how to customize the build # # Targets: -# install install system independent support -# install-unix also install unix-only support -# install-both install both lua5.1 and lua5.2 socket support -# print print the build settings +# install install system independent support +# install-unix also install unix-only support +# install-both install for both lua5.1 and lua5.2 +# install-both-unix also install unix-only +# print print the build settings PLAT?= linux -PLATS= macosx linux win32 +PLATS= macosx linux win32 mingw all: $(PLAT) @@ -23,6 +24,14 @@ test: lua test/hello.lua install-both: + $(MAKE) clean + @cd src; $(MAKE) $(PLAT) LUAV=5.1 + @cd src; $(MAKE) install LUAV=5.1 + $(MAKE) clean + @cd src; $(MAKE) $(PLAT) LUAV=5.2 + @cd src; $(MAKE) install LUAV=5.2 + +install-both-unix: $(MAKE) clean @cd src; $(MAKE) $(PLAT) LUAV=5.1 @cd src; $(MAKE) install-unix LUAV=5.1 diff --git a/mime.vcxproj b/mime.vcxproj index 95781f2..63f5452 100755 --- a/mime.vcxproj +++ b/mime.vcxproj @@ -21,6 +21,19 @@ + + + Document + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + copy %(FullPath) $(LUALIB_PATH)$(Platform)\$(Configuration) + copy %(FullPath) $(LUALIB_PATH)$(Platform)\$(Configuration) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + + {128E8BD0-174A-48F0-8771-92B1E8D18713} Win32Proj @@ -52,25 +65,29 @@ + + + + <_ProjectFileVersion>11.0.50727.1 - $(SolutionDir)\$(Configuration)\mime\ + $(LUABIN_PATH)$(Configuration)\mime\ $(Configuration)\ true core @@ -78,23 +95,23 @@ true core - $(SolutionDir)$(Platform)\$(Configuration)\mime\ + $(LUABIN_PATH)$(Platform)\$(Configuration)\mime\ - $(SolutionDir)\$(Configuration)\mime\ + $(LUABIN_PATH)$(Configuration)\mime\ $(Configuration)\ false core false - $(SolutionDir)$(Platform)\$(Configuration)\mime\ + $(LUABIN_PATH)$(Platform)\$(Configuration)\mime\ core Disabled - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;MIME_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) true EnableFastChecks @@ -102,11 +119,12 @@ Level3 EditAndContinue + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;%(AdditionalDependencies) + $(LUALIB);%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Configuration);%(AdditionalLibraryDirectories) true $(OutDir)mime.pdb Windows @@ -114,12 +132,13 @@ $(OutDir)$(TargetName).lib MachineX86 + false Disabled - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;MIME_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) EnableFastChecks MultiThreadedDebugDLL @@ -127,11 +146,12 @@ Level3 ProgramDatabase + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;%(AdditionalDependencies) + $(LUALIB);%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) true $(OutDir)mime.pdb Windows @@ -143,17 +163,18 @@ - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;MIME_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) MultiThreadedDLL Level4 + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;%(AdditionalDependencies) + $(LUALIB);%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Configuration);%(AdditionalLibraryDirectories) true Windows true @@ -166,7 +187,7 @@ - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;MIME_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) MultiThreadedDLL @@ -174,11 +195,12 @@ Level4 + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;%(AdditionalDependencies) + $(LUALIB);%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) true Windows true diff --git a/mime.vcxproj.filters b/mime.vcxproj.filters new file mode 100644 index 0000000..621215b --- /dev/null +++ b/mime.vcxproj.filters @@ -0,0 +1,16 @@ + + + + + + + + {fad87a86-297c-4881-a114-73b967bb3c92} + + + + + cdir + + + \ No newline at end of file diff --git a/mingw.cmd b/mingw.cmd new file mode 100644 index 0000000..bf2b7ed --- /dev/null +++ b/mingw.cmd @@ -0,0 +1 @@ +make PLAT=mingw LUAINC_mingw_base=/home/diego/build/mingw/include LUALIB_mingw_base=/home/diego/build/mingw/bin LUAPREFIX_mingw=/home/diego/build/mingw/bin DEBUG=DEBUG install-both diff --git a/socket.vcxproj b/socket.vcxproj index 0256c90..53c5539 100755 --- a/socket.vcxproj +++ b/socket.vcxproj @@ -32,6 +32,98 @@ + + + Document + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + + + Document + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\%(Filename)%(Extension) + + + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + + Document + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + $(LUABIN_PATH)$(Configuration)\socket\%(Filename)%(Extension) + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Platform)\$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + copy %(FullPath) $(LUABIN_PATH)$(Configuration)\socket + + {66E3CE14-884D-4AEA-9F20-15A0BEAF8C5A} Win32Proj @@ -63,25 +155,29 @@ + + + + <_ProjectFileVersion>11.0.50727.1 - $(SolutionDir)\$(Configuration)\socket\ + $(LUALIB_PATH)$(Configuration)\socket\ $(Configuration)\ true core @@ -89,23 +185,23 @@ true core - $(SolutionDir)$(Platform)\$(Configuration)\socket\ + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\ - $(SolutionDir)\$(Configuration)\socket\ + $(LUALIB_PATH)$(Configuration)\socket\ $(Configuration)\ false core false - $(SolutionDir)$(Platform)\$(Configuration)\socket\ + $(LUABIN_PATH)$(Platform)\$(Configuration)\socket\ core Disabled - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;LUASOCKET_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;LUASOCKET_DEBUG;%(PreprocessorDefinitions) true EnableFastChecks @@ -113,11 +209,12 @@ Level3 EditAndContinue + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;ws2_32.lib;%(AdditionalDependencies) + $(LUALIB);wsock32.lib;ws2_32.lib;%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Configuration);%(AdditionalLibraryDirectories) true $(OutDir)mime.pdb Windows @@ -125,12 +222,13 @@ $(OutDir)$(TargetName).lib MachineX86 + false Disabled - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;LUASOCKET_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;LUASOCKET_DEBUG;%(PreprocessorDefinitions) EnableFastChecks MultiThreadedDebugDLL @@ -138,11 +236,12 @@ Level3 ProgramDatabase + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;ws2_32.lib;%(AdditionalDependencies) + $(LUALIB);wsock32.lib;ws2_32.lib;%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) true $(OutDir)mime.pdb Windows @@ -154,17 +253,18 @@ - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;LUASOCKET_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) MultiThreadedDLL Level4 + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;ws2_32.lib;%(AdditionalDependencies) + $(LUALIB);wsock32.lib;ws2_32.lib;%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Configuration);%(AdditionalLibraryDirectories) true Windows true @@ -177,7 +277,7 @@ - ..\include;%(AdditionalIncludeDirectories) + $(LUAINC_PATH);%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;LUASOCKET_API=__declspec(dllexport);_CRT_SECURE_NO_WARNINGS;LUA_COMPAT_MODULE;%(PreprocessorDefinitions) MultiThreadedDLL @@ -185,11 +285,12 @@ Level4 + $(IntDir)$(TargetName)$(PlatformToolsetVersion).pdb - lualib.lib;ws2_32.lib;%(AdditionalDependencies) + $(LUALIB);wsock32.lib;ws2_32.lib;%(AdditionalDependencies) $(OutDir)$(TargetName).dll - ..\bin\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) + $(LUALIB_PATH)$(Platform)\$(Configuration);%(AdditionalLibraryDirectories) true Windows true diff --git a/socket.vcxproj.filters b/socket.vcxproj.filters new file mode 100644 index 0000000..38f2f07 --- /dev/null +++ b/socket.vcxproj.filters @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + cdir + + + cdir + + + ldir + + + ldir + + + ldir + + + ldir + + + ldir + + + ldir + + + + + {b053460d-5439-4e3a-a2eb-c31a95b5691f} + + + {b301b82c-37cb-4e05-9333-194e92ed7a62} + + + \ No newline at end of file diff --git a/src/inet.c b/src/inet.c index 69d32e6..fe9769b 100644 --- a/src/inet.c +++ b/src/inet.c @@ -79,24 +79,22 @@ static int inet_global_tohostname(lua_State *L) { } static int inet_global_getnameinfo(lua_State *L) { + char hbuf[NI_MAXHOST]; + char sbuf[NI_MAXSERV]; int i, ret; - char host[1024]; - char serv[32]; struct addrinfo hints; struct addrinfo *resolved, *iter; - const char *node = luaL_optstring(L, 1, NULL); - const char *service = luaL_optstring(L, 2, NULL); + const char *host = luaL_optstring(L, 1, NULL); + const char *serv = luaL_optstring(L, 2, NULL); - if (!(node || service)) - luaL_error(L, "You have to specify a hostname, a service, or both"); + if (!(host || serv)) + luaL_error(L, "host and serv cannot be both nil"); memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = PF_UNSPEC; - /* getaddrinfo must get a node and a service argument */ - ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", - &hints, &resolved); + ret = getaddrinfo(host, serv, &hints, &resolved); if (ret != 0) { lua_pushnil(L); lua_pushstring(L, socket_gaistrerror(ret)); @@ -105,19 +103,19 @@ static int inet_global_getnameinfo(lua_State *L) { lua_newtable(L); for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { - getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, - node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); - - if (node) { + getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, + hbuf, host? (socklen_t) sizeof(hbuf): 0, + sbuf, serv? (socklen_t) sizeof(sbuf): 0, 0); + if (host) { lua_pushnumber(L, i); - lua_pushstring(L, host); + lua_pushstring(L, hbuf); lua_settable(L, -3); } } freeaddrinfo(resolved); - if (service) { - lua_pushstring(L, serv); + if (serv) { + lua_pushstring(L, sbuf); return 2; } else { return 1; @@ -176,9 +174,14 @@ static int inet_global_getaddrinfo(lua_State *L) } lua_newtable(L); for (iterator = resolved; iterator; iterator = iterator->ai_next) { - char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; - getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, - (socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST); + char hbuf[NI_MAXHOST]; + ret = getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, + hbuf, (socklen_t) sizeof(hbuf), NULL, 0, NI_NUMERICHOST); + if (ret){ + lua_pushnil(L); + lua_pushstring(L, socket_gaistrerror(ret)); + return 2; + } lua_pushnumber(L, i); lua_newtable(L); switch (iterator->ai_family) { @@ -203,7 +206,6 @@ static int inet_global_getaddrinfo(lua_State *L) return 1; } - /*-------------------------------------------------------------------------*\ * Gets the host name \*-------------------------------------------------------------------------*/ @@ -222,7 +224,6 @@ static int inet_global_gethostname(lua_State *L) } - /*=========================================================================*\ * Lua methods \*=========================================================================*/ @@ -385,7 +386,6 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) struct in6_addr addrany = IN6ADDR_ANY_INIT; memset((char *) &sin6, 0, sizeof(sin6)); sin6.sin6_family = AF_UNSPEC; -fprintf(stderr, "disconnecting\n"); sin6.sin6_addr = addrany; return socket_strerror(socket_connect(ps, (SA *) &sin6, sizeof(sin6), tm)); @@ -397,7 +397,7 @@ fprintf(stderr, "disconnecting\n"); /*-------------------------------------------------------------------------*\ * Tries to connect to remote address (address, port) \*-------------------------------------------------------------------------*/ -const char *inet_tryconnect(p_socket ps, const char *address, +const char *inet_tryconnect(p_socket ps, int *family, const char *address, const char *serv, p_timeout tm, struct addrinfo *connecthints) { struct addrinfo *iterator = NULL, *resolved = NULL; @@ -411,6 +411,23 @@ const char *inet_tryconnect(p_socket ps, const char *address, } for (iterator = resolved; iterator; iterator = iterator->ai_next) { timeout_markstart(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 (*family != iterator->ai_family) { + 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 */ + socket_setnonblocking(ps); + } /* try connecting to remote address */ err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, (socklen_t) iterator->ai_addrlen, tm)); @@ -425,7 +442,8 @@ const char *inet_tryconnect(p_socket ps, const char *address, /*-------------------------------------------------------------------------*\ * Tries to accept a socket \*-------------------------------------------------------------------------*/ -const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) +const char *inet_tryaccept(p_socket server, int family, p_socket client, + p_timeout tm) { socklen_t len; t_sockaddr_storage addr; @@ -446,6 +464,9 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, struct addrinfo *iterator = NULL, *resolved = NULL; const char *err = NULL; t_socket sock = *ps; + /* translate luasocket special values to C */ + if (strcmp(address, "*") == 0) address = NULL; + if (!serv) serv = "0"; /* try resolving */ err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); if (err) { @@ -485,7 +506,7 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, * Some systems do not provide this so that we provide our own. It's not * marvelously fast, but it works just fine. \*-------------------------------------------------------------------------*/ -#ifdef INET_ATON +#ifdef LUASOCKET_INET_ATON int inet_aton(const char *cp, struct in_addr *inp) { unsigned int a = 0, b = 0, c = 0, d = 0; @@ -507,4 +528,49 @@ int inet_aton(const char *cp, struct in_addr *inp) } #endif +/*-------------------------------------------------------------------------*\ +* inet_ntop/inet_pton for MinGW from +* http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html +\*-------------------------------------------------------------------------*/ +#ifdef LUASOCKET_INET_PTON +const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) +{ + if (af == AF_INET) { + struct sockaddr_in in; + memset(&in, 0, sizeof(in)); + in.sin_family = AF_INET; + memcpy(&in.sin_addr, src, sizeof(struct in_addr)); + getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), + dst, cnt, NULL, 0, NI_NUMERICHOST); + return dst; + } else if (af == AF_INET6) { + struct sockaddr_in6 in; + memset(&in, 0, sizeof(in)); + in.sin6_family = AF_INET6; + memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); + getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), + dst, cnt, NULL, 0, NI_NUMERICHOST); + return dst; + } + return NULL; +} + +int inet_pton(int af, const char *src, void *dst) +{ + struct addrinfo hints, *res, *ressave; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = af; + if (getaddrinfo(src, NULL, &hints, &res) != 0) { + return -1; + } + ressave = res; + while (res) { + memcpy(dst, res->ai_addr, res->ai_addrlen); + res = res->ai_next; + } + freeaddrinfo(ressave); + return 0; +} + +#endif diff --git a/src/inet.h b/src/inet.h index 4678ba6..1f1a96a 100644 --- a/src/inet.h +++ b/src/inet.h @@ -19,13 +19,13 @@ #include "timeout.h" #ifdef _WIN32 -#define INET_ATON +#define LUASOCKET_INET_ATON #endif int inet_open(lua_State *L); const char *inet_trycreate(p_socket ps, int family, int type); -const char *inet_tryconnect(p_socket ps, const char *address, +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); @@ -38,8 +38,13 @@ int inet_meth_getsockname(lua_State *L, p_socket ps, int family); int inet_optfamily(lua_State* L, int narg, const char* def); int inet_optsocktype(lua_State* L, int narg, const char* def); -#ifdef INET_ATON +#ifdef LUASOCKET_INET_ATON int inet_aton(const char *cp, struct in_addr *inp); #endif +#ifdef LUASOCKET_INET_PTON +const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); +int inet_pton(int af, const char *src, void *dst); +#endif + #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 @@ # # make PLAT=linux DEBUG=DEBUG LUAV=5.2 prefix=/sw -# PLAT: linux macosx win32 +# PLAT: linux macosx win32 mingw # platform to build for PLAT?=linux @@ -33,6 +33,9 @@ LUAINC_macosx?=$(LUAINC_macosx_base)/lua/$(LUAV) # FIXME default should this default to fink or to macports? # What happens when more than one Lua version is installed? LUAPREFIX_macosx?=/opt/local +CDIR_macosx?=lib/lua/$(LUAV) +LDIR_macosx?=share/lua/$(LUAV) + # LUAINC_linux: # /usr/include/lua$(LUAV) @@ -40,21 +43,41 @@ LUAPREFIX_macosx?=/opt/local # /usr/local/include/lua$(LUAV) # where lua headers are found for linux builds LUAINC_linux_base?=/usr/include -LUAINC_linux?=$(LUAINC_linux_base)/lua$(LUAV) +LUAINC_linux?=$(LUAINC_linux_base)/lua/$(LUAV) LUAPREFIX_linux?=/usr/local +CDIR_linux?=lib/lua/$(LUAV) +LDIR_linux?=share/lua/$(LUAV) + +# where lua headers are found for mingw builds +# LUAINC_mingw: +# /opt/local/include +LUAINC_mingw_base?=/usr/include +LUAINC_mingw?=$(LUAINC_mingw_base)/lua/$(LUAV) +LUALIB_mingw_base?=/usr/bin +LUALIB_mingw?=$(LUALIB_mingw_base)/lua/$(LUAV)/lua$(subst .,,$(LUAV)).dll +LUAPREFIX_mingw?=/usr +CDIR_mingw?=lua/$(LUAV) +LDIR_mingw?=lua/$(LUAV)/lua + # LUAINC_win32: # LUALIB_win32: # where lua headers and libraries are found for win32 builds -LUAINC_win32?="../../lua-5.1.3/src" -LUALIB_win32?="../../lua-5.1.3" +LUAINC_win32_base?= +LUAINC_win32?=$(LUAINC_win32_base)/lua/$(LUAV) +PLATFORM_win32?=Release LUAPREFIX_win32?= -# FIXME default should be where lua-for-windows puts lua +CDIR_win32?=lua/$(LUAV)/$(PLATFORM_win32) +LDIR_win32?=lua/$(LUAV)/$(PLATFORM_win32)/lua +LUALIB_win32?=$(LUAPREFIX_win32)/lua/$(LUAV)/$(PLATFORM_win32) # prefix: /usr/local /usr /opt/local /sw # the top of the default install tree prefix?=$(LUAPREFIX_$(PLAT)) +CDIR?=$(CDIR_$(PLAT)) +LDIR?=$(LDIR_$(PLAT)) + # DESTDIR: (no default) # used by package managers to install into a temporary destination DESTDIR= @@ -63,13 +86,6 @@ DESTDIR= # Definitions below can be overridden on the make command line, but # shouldn't have to be. -print: - @echo PLAT=$(PLAT) - @echo LUAV=$(LUAV) - @echo DEBUG=$(DEBUG) - @echo prefix=$(prefix) - @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) - @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) #------ # Install directories @@ -80,18 +96,28 @@ INSTALL_DATA=install -m644 INSTALL_EXEC=install INSTALL_TOP=$(DESTDIR)$(prefix) -INSTALL_TOP_SHARE=$(INSTALL_TOP)/share/lua/$(LUAV) -INSTALL_TOP_LIB=$(INSTALL_TOP)/lib/lua/$(LUAV) +INSTALL_TOP_LDIR=$(INSTALL_TOP)/$(LDIR) +INSTALL_TOP_CDIR=$(INSTALL_TOP)/$(CDIR) -INSTALL_SOCKET_SHARE=$(INSTALL_TOP_SHARE)/socket -INSTALL_SOCKET_LIB=$(INSTALL_TOP_LIB)/socket -INSTALL_MIME_SHARE=$(INSTALL_TOP_SHARE)/mime -INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime +INSTALL_SOCKET_LDIR=$(INSTALL_TOP_LDIR)/socket +INSTALL_SOCKET_CDIR=$(INSTALL_TOP_CDIR)/socket +INSTALL_MIME_LDIR=$(INSTALL_TOP_LDIR)/mime +INSTALL_MIME_CDIR=$(INSTALL_TOP_CDIR)/mime + +print: + @echo PLAT=$(PLAT) + @echo LUAV=$(LUAV) + @echo DEBUG=$(DEBUG) + @echo prefix=$(prefix) + @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) + @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) + @echo INSTALL_TOP_CDIR=$(INSTALL_TOP_CDIR) + @echo INSTALL_TOP_LDIR=$(INSTALL_TOP_LDIR) #------ # Supported platforms # -PLATS= macosx linux win32 +PLATS= macosx linux win32 mingw #------ # Compiler and linker settings @@ -117,37 +143,53 @@ CC_linux=gcc DEF_linux=-DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ -DLUASOCKET_API='__attribute__((visibility("default")))' \ -DMIME_API='__attribute__((visibility("default")))' -CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \ - -fvisibility=hidden +CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra \ + -Wimplicit -O2 -ggdb3 -fpic -fvisibility=hidden LDFLAGS_linux=-O -shared -fpic -o LD_linux=gcc SOCKET_linux=usocket.o +#------ +# Compiler and linker settings +# for MingW +SO_mingw=dll +O_mingw=o +CC_mingw=gcc +DEF_mingw= -DLUASOCKET_INET_PTON -DLUASOCKET_$(DEBUG) -DLUA_COMPAT_MODULE \ + -DWINVER=0x0501 -DLUASOCKET_API='__declspec(dllexport)' \ + -DMIME_API='__declspec(dllexport)' +CFLAGS_mingw= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ + -fvisibility=hidden +LDFLAGS_mingw= $(LUALIB) -shared -Wl,-s -lwsock32 -lws2_32 -o +LD_mingw=gcc +SOCKET_mingw=wsocket.o + + #------ # Compiler and linker settings # for Win32 SO_win32=dll O_win32=obj CC_win32=cl -DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \ - /D "LUASOCKET_API=__declspec(dllexport)" /D "_CRT_SECURE_NO_WARNINGS" \ - /D "_WINDLL" /D "LUA_COMPAT_MODULE" /D "MIME_API=__declspec(dllexport)" \ - /D "LUASOCKET_$(DEBUG)" -CFLAGS_win32=/I "$(LUAINC)" $(DEF) /O2 /Ot /MD /W3 /nologo -LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \ - /LIBPATH:"$(LUALIB)" \ - /MANIFEST \ - /MANIFESTFILE:"intermediate.manifest" \ - /MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ - /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /DYNAMICBASE:NO \ - /MACHINE:X86 ws2_32.lib lua$(LUAV).lib /OUT: +DEF_win32= //D "WIN32" //D "NDEBUG" //D "_WINDOWS" //D "_USRDLL" \ + //D "LUASOCKET_API=__declspec(dllexport)" //D "_CRT_SECURE_NO_WARNINGS" \ + //D "_WINDLL" //D "LUA_COMPAT_MODULE" \ + //D "MIME_API=__declspec(dllexport)" \ + //D "LUASOCKET_$(DEBUG)" +CFLAGS_win32=//I "$(LUAINC)" $(DEF) //O2 //Ot //MD //W3 //nologo +LDFLAGS_win32= //nologo //link //NOLOGO //DLL //INCREMENTAL:NO \ + //MANIFEST //MANIFESTFILE:"intermediate.manifest" \ + //MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ + //SUBSYSTEM:WINDOWS //OPT:REF //OPT:ICF //DYNAMICBASE:NO \ + //MACHINE:X86 /LIBPATH:"$(shell cmd //c echo $(LUALIB))" lua$(subst .,,$(LUAV)).lib \ + wsock32.lib ws2_32.lib //OUT: LD_win32=cl SOCKET_win32=wsocket.obj .SUFFIXES: .obj .c.obj: - $(CC) $(CFLAGS) /Fo"$@" /c $< + $(CC) $(CFLAGS) //Fo"$@" //c $< #------ # Output file names @@ -223,7 +265,7 @@ SERIAL_OBJS:=\ #------ # Files to install # -TO_SOCKET_SHARE= \ +TO_SOCKET_LDIR= \ http.lua \ url.lua \ tp.lua \ @@ -231,7 +273,7 @@ TO_SOCKET_SHARE= \ headers.lua \ smtp.lua -TO_TOP_SHARE= \ +TO_TOP_LDIR= \ ltn12.lua \ socket.lua \ mime.lua @@ -250,6 +292,9 @@ win32: linux: $(MAKE) all-unix PLAT=linux +mingw: + $(MAKE) all PLAT=mingw + none: @echo "Please run" @echo " make PLATFORM" @@ -273,21 +318,21 @@ $(SERIAL_SO): $(SERIAL_OBJS) $(LD) $(SERIAL_OBJS) $(LDFLAGS)$@ install: - $(INSTALL_DIR) $(INSTALL_TOP_SHARE) - $(INSTALL_DATA) $(TO_TOP_SHARE) $(INSTALL_TOP_SHARE) - $(INSTALL_DIR) $(INSTALL_SOCKET_SHARE) - $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE) - $(INSTALL_DIR) $(INSTALL_SOCKET_LIB) - $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(SO) - $(INSTALL_DIR) $(INSTALL_MIME_LIB) - $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(SO) + $(INSTALL_DIR) $(INSTALL_TOP_LDIR) + $(INSTALL_DATA) $(TO_TOP_LDIR) $(INSTALL_TOP_LDIR) + $(INSTALL_DIR) $(INSTALL_SOCKET_LDIR) + $(INSTALL_DATA) $(TO_SOCKET_LDIR) $(INSTALL_SOCKET_LDIR) + $(INSTALL_DIR) $(INSTALL_SOCKET_CDIR) + $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_CDIR)/core.$(SO) + $(INSTALL_DIR) $(INSTALL_MIME_CDIR) + $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_CDIR)/core.$(SO) install-unix: install - $(INSTALL_EXEC) $(UNIX_SO) $(INSTALL_SOCKET_LIB)/$(UNIX_SO) - $(INSTALL_EXEC) $(SERIAL_SO) $(INSTALL_SOCKET_LIB)/$(SERIAL_SO) + $(INSTALL_EXEC) $(UNIX_SO) $(INSTALL_SOCKET_CDIR)/$(UNIX_SO) + $(INSTALL_EXEC) $(SERIAL_SO) $(INSTALL_SOCKET_CDIR)/$(SERIAL_SO) local: - $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=.. + $(MAKE) install INSTALL_TOP_CDIR=.. INSTALL_TOP_LDIR=.. clean: rm -f $(SOCKET_SO) $(SOCKET_OBJS) $(SERIAL_OBJS) diff --git a/src/tcp.c b/src/tcp.c index 6734dc0..60c1e8a 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -222,7 +222,6 @@ static int meth_bind(lua_State *L) bindhints.ai_socktype = SOCK_STREAM; bindhints.ai_family = tcp->family; bindhints.ai_flags = AI_PASSIVE; - address = strcmp(address, "*")? address: NULL; err = inet_trybind(&tcp->sock, address, port, &bindhints); if (err) { lua_pushnil(L); @@ -248,7 +247,8 @@ static int meth_connect(lua_State *L) /* make sure we try to connect only to the same family */ connecthints.ai_family = tcp->family; timeout_markstart(&tcp->tm); - err = inet_tryconnect(&tcp->sock, address, port, &tcp->tm, &connecthints); + err = inet_tryconnect(&tcp->sock, &tcp->family, address, port, + &tcp->tm, &connecthints); /* have to set the class even if it failed due to non-blocking connects */ auxiliar_setclass(L, "tcp{client}", 1); if (err) { @@ -388,6 +388,7 @@ 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; @@ -402,8 +403,13 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, /* 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 one wasn't created by the bind stage */ - if (tcp->sock == SOCKET_INVALID) { + /* 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)); @@ -427,6 +433,7 @@ static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, /* here, if err is set, we failed */ return err; } +#endif static int global_connect(lua_State *L) { const char *remoteaddr = luaL_checkstring(L, 1); @@ -444,6 +451,7 @@ 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; /* allow user to pick local address and port */ memset(&bindhints, 0, sizeof(bindhints)); bindhints.ai_socktype = SOCK_STREAM; @@ -463,7 +471,8 @@ static int global_connect(lua_State *L) { connecthints.ai_socktype = SOCK_STREAM; /* make sure we try to connect only to the same family */ connecthints.ai_family = bindhints.ai_family; - err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp); + err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, + &tcp->tm, &connecthints); if (err) { socket_destroy(&tcp->sock); lua_pushnil(L); diff --git a/src/udp.c b/src/udp.c index 8e14fac..2a51d1c 100644 --- a/src/udp.c +++ b/src/udp.c @@ -159,7 +159,7 @@ static int meth_sendto(lua_State *L) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); if (!inet_pton(AF_INET, ip, &addr.sin_addr)) - luaL_argerror(L, 3, "invalid ip address"); + luaL_argerror(L, 3, "invalid ip address"); addr.sin_family = AF_INET; addr.sin_port = htons(port); timeout_markstart(tm); @@ -171,7 +171,7 @@ static int meth_sendto(lua_State *L) { struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); if (!inet_pton(AF_INET6, ip, &addr.sin6_addr)) - luaL_argerror(L, 3, "invalid ip address"); + luaL_argerror(L, 3, "invalid ip address"); addr.sin6_family = AF_INET6; addr.sin6_port = htons(port); timeout_markstart(tm); @@ -180,9 +180,9 @@ static int meth_sendto(lua_State *L) { break; } default: - lua_pushnil(L); - lua_pushfstring(L, "unknown family %d", udp->family); - return 2; + lua_pushnil(L); + lua_pushfstring(L, "unknown family %d", udp->family); + return 2; } if (err != IO_DONE) { lua_pushnil(L); @@ -259,19 +259,19 @@ static int meth_receivefrom(lua_State *L) { (SA *) &addr, &addr_len, tm); /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ if (err == IO_CLOSED) - err = IO_DONE; + err = IO_DONE; if (err == IO_DONE) { - char addrstr[INET6_ADDRSTRLEN]; - lua_pushlstring(L, buffer, got); - if (!inet_ntop(AF_INET6, &addr.sin6_addr, - addrstr, sizeof(addrstr))) { - lua_pushnil(L); - lua_pushstring(L, "invalid source address"); - return 2; - } - lua_pushstring(L, addrstr); - lua_pushnumber(L, ntohs(addr.sin6_port)); - return 3; + char addrstr[INET6_ADDRSTRLEN]; + lua_pushlstring(L, buffer, got); + if (!inet_ntop(AF_INET6, &addr.sin6_addr, + addrstr, sizeof(addrstr))) { + lua_pushnil(L); + lua_pushstring(L, "invalid source address"); + return 2; + } + lua_pushstring(L, addrstr); + lua_pushnumber(L, ntohs(addr.sin6_port)); + return 3; } break; } @@ -376,7 +376,8 @@ static int meth_setpeername(lua_State *L) { /* make sure we try to connect only to the same family */ connecthints.ai_family = udp->family; if (connecting) { - err = inet_tryconnect(&udp->sock, address, port, tm, &connecthints); + err = inet_tryconnect(&udp->sock, &udp->family, address, + port, tm, &connecthints); if (err) { lua_pushnil(L); 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) { case EAI_MEMORY: return "memory allocation failure"; case EAI_NONAME: return "host or service not provided, or not known"; -// case EAI_OVERFLOW: return "argument buffer overflow"; +#ifdef EAI_OVERFLOW + case EAI_OVERFLOW: return "argument buffer overflow"; +#endif #ifdef EAI_PROTOCOL case EAI_PROTOCOL: return "resolved protocol is unknown"; #endif case EAI_SERVICE: return "service not supported for socket type"; case EAI_SOCKTYPE: return "ai_socktype not supported"; -// case EAI_SYSTEM: return strerror(errno); +#ifdef EAI_SYSTEM + case EAI_SYSTEM: return strerror(errno); +#endif default: return gai_strerror(err); } } 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; typedef SOCKET t_socket; typedef t_socket *p_socket; +#ifndef IPV6_V6ONLY +#define IPV6_V6ONLY 27 +#endif + #define SOCKET_INVALID (INVALID_SOCKET) #ifndef SO_REUSEPORT 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 @@ +local socket = require "socket" +local u = socket.udp() assert(u:setsockname("*", 5088)) u:close() +local u = socket.udp() assert(u:setsockname("*", 0)) u:close() +local t = socket.tcp() assert(t:bind("*", 5088)) t:close() +local t = socket.tcp() assert(t:bind("*", 0)) t:close() +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 @@ +local socket = require "socket" +local addresses = assert(socket.dns.getaddrinfo("localhost")) +assert(type(addresses) == 'table') + +local ipv4mask = "^%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?$" + +for i, alt in ipairs(addresses) do + if alt.family == 'inet' then + assert(type(alt.addr) == 'string') + assert(alt.addr:find(ipv4mask)) + assert(alt.addr == '127.0.0.1') + end +end + +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 = { "shutdown", } test_methods(socket.tcp(), tcp_methods) -test_methods(socket.tcp6(), tcp_methods) +do local sock = socket.tcp6() +if sock then test_methods(socket.tcp6(), tcp_methods) +else io.stderr:write("Warning! IPv6 does not support!\n") end +end local udp_methods = { "close", @@ -666,7 +669,10 @@ local udp_methods = { ------------------------------------------------------------------------ test_methods(socket.udp(), udp_methods) -test_methods(socket.udp6(), udp_methods) +do local sock = socket.tcp6() +if sock then test_methods(socket.udp6(), udp_methods) +else io.stderr:write("Warning! IPv6 does not support!\n") end +end test("partial receive") test_partialrecv() diff --git a/win32.cmd b/win32.cmd new file mode 100644 index 0000000..48522f0 --- /dev/null +++ b/win32.cmd @@ -0,0 +1,12 @@ +make PLAT=win32 LUAV=5.2 LUAINC_win32='c:\cygwin\home\diego\build\include' LUALIB_win32='c:\cygwin\home\diego\build\bin\release' + +#!/bin/sh +for p in Release Debug x64/Release x64/Debug; do + for el in mime socket; do + for e in dll lib; do + cp $p/$el/core.$e ../bin/$p/$el/ + done; + done; + cp src/ltn12.lua src/socket.lua src/mime.lua ../bin/$p/ + cp src/http.lua src/url.lua src/tp.lua src/ftp.lua src/headers.lua src/smtp.lua ../bin/$p/socket/ +done;