diff options
-rw-r--r-- | makefile | 20 | ||||
-rw-r--r-- | src/inet.c | 48 | ||||
-rw-r--r-- | src/makefile | 78 |
3 files changed, 124 insertions, 22 deletions
@@ -1,13 +1,23 @@ | |||
1 | PLAT?= macosx | 1 | # luasocket makefile |
2 | # | ||
3 | # see src/makefile for description of how to customize the build | ||
4 | # | ||
5 | # Targets: | ||
6 | # install install system independent support | ||
7 | # install-unix also install unix-only support | ||
8 | # install-both install both lua5.1 and lua5.2 socket support | ||
9 | # print print the build settings | ||
10 | |||
11 | PLAT?= linux | ||
2 | PLATS= macosx linux win32 | 12 | PLATS= macosx linux win32 |
3 | 13 | ||
4 | #------ | ||
5 | # Hopefully no need to change anything below this line | ||
6 | # | ||
7 | all: $(PLAT) | 14 | all: $(PLAT) |
8 | 15 | ||
9 | $(PLATS) none install install-unix local clean: | 16 | $(PLATS) none install install-unix local clean: |
10 | @cd src; $(MAKE) $@ | 17 | $(MAKE) -C src $@ |
18 | |||
19 | print: | ||
20 | $(MAKE) -C src $@ | ||
11 | 21 | ||
12 | test: | 22 | test: |
13 | lua test/hello.lua | 23 | lua test/hello.lua |
@@ -16,6 +16,7 @@ | |||
16 | static int inet_global_toip(lua_State *L); | 16 | static int inet_global_toip(lua_State *L); |
17 | static int inet_global_getaddrinfo(lua_State *L); | 17 | static int inet_global_getaddrinfo(lua_State *L); |
18 | static int inet_global_tohostname(lua_State *L); | 18 | static int inet_global_tohostname(lua_State *L); |
19 | static int inet_global_getnameinfo(lua_State *L); | ||
19 | static void inet_pushresolved(lua_State *L, struct hostent *hp); | 20 | static void inet_pushresolved(lua_State *L, struct hostent *hp); |
20 | static int inet_global_gethostname(lua_State *L); | 21 | static int inet_global_gethostname(lua_State *L); |
21 | 22 | ||
@@ -24,6 +25,7 @@ static luaL_Reg func[] = { | |||
24 | { "toip", inet_global_toip}, | 25 | { "toip", inet_global_toip}, |
25 | { "getaddrinfo", inet_global_getaddrinfo}, | 26 | { "getaddrinfo", inet_global_getaddrinfo}, |
26 | { "tohostname", inet_global_tohostname}, | 27 | { "tohostname", inet_global_tohostname}, |
28 | { "getnameinfo", inet_global_getnameinfo}, | ||
27 | { "gethostname", inet_global_gethostname}, | 29 | { "gethostname", inet_global_gethostname}, |
28 | { NULL, NULL} | 30 | { NULL, NULL} |
29 | }; | 31 | }; |
@@ -76,6 +78,52 @@ static int inet_global_tohostname(lua_State *L) { | |||
76 | return 2; | 78 | return 2; |
77 | } | 79 | } |
78 | 80 | ||
81 | static int inet_global_getnameinfo(lua_State *L) { | ||
82 | int i, ret; | ||
83 | char host[1024]; | ||
84 | char serv[32]; | ||
85 | struct addrinfo hints; | ||
86 | struct addrinfo *resolved, *iter; | ||
87 | const char *node = luaL_optstring(L, 1, NULL); | ||
88 | const char *service = luaL_optstring(L, 2, NULL); | ||
89 | |||
90 | if (!(node || service)) | ||
91 | luaL_error(L, "You have to specify a hostname, a service, or both"); | ||
92 | |||
93 | memset(&hints, 0, sizeof(hints)); | ||
94 | hints.ai_socktype = SOCK_STREAM; | ||
95 | hints.ai_family = PF_UNSPEC; | ||
96 | |||
97 | /* getaddrinfo must get a node and a service argument */ | ||
98 | ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", | ||
99 | &hints, &resolved); | ||
100 | if (ret != 0) { | ||
101 | lua_pushnil(L); | ||
102 | lua_pushstring(L, socket_gaistrerror(ret)); | ||
103 | return 2; | ||
104 | } | ||
105 | |||
106 | lua_newtable(L); | ||
107 | for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { | ||
108 | getnameinfo(iter->ai_addr, iter->ai_addrlen, host, | ||
109 | node ? sizeof(host) : 0, serv, service ? sizeof(serv) : 0, 0); | ||
110 | |||
111 | if (node) { | ||
112 | lua_pushnumber(L, i); | ||
113 | lua_pushstring(L, host); | ||
114 | lua_settable(L, -3); | ||
115 | } | ||
116 | } | ||
117 | freeaddrinfo(resolved); | ||
118 | |||
119 | if (service) { | ||
120 | lua_pushstring(L, serv); | ||
121 | return 2; | ||
122 | } else { | ||
123 | return 1; | ||
124 | } | ||
125 | } | ||
126 | |||
79 | /*-------------------------------------------------------------------------*\ | 127 | /*-------------------------------------------------------------------------*\ |
80 | * Returns all information provided by the resolver given a host name | 128 | * Returns all information provided by the resolver given a host name |
81 | * or ip address | 129 | * or ip address |
diff --git a/src/makefile b/src/makefile index 6225ce4..4df62bd 100644 --- a/src/makefile +++ b/src/makefile | |||
@@ -1,21 +1,65 @@ | |||
1 | PLAT?=macosx | 1 | # luasocket src/makefile |
2 | # | ||
3 | # Definitions in this section can be overriden on the command line or in the | ||
4 | # environment. | ||
5 | # | ||
6 | # These are equivalent: | ||
7 | # | ||
8 | # export PLAT=linux DEBUG=DEBUG LUAV=5.2 prefix=/sw | ||
9 | # make | ||
10 | # | ||
11 | # and | ||
12 | # | ||
13 | # make PLAT=linux DEBUG=DEBUG LUAV=5.2 prefix=/sw | ||
14 | |||
15 | # PLAT: linux macosx win32 | ||
16 | # platform to build for | ||
17 | PLAT?=linux | ||
18 | |||
19 | # LUAV: 5.1 5.2 | ||
20 | # lua version to build against | ||
2 | LUAV?=5.1 | 21 | LUAV?=5.1 |
3 | prefix=../../../build/lua/$(LUAV) | ||
4 | #prefix=/usr/local | ||
5 | #prefix=/opt/local | ||
6 | #prefix=. | ||
7 | 22 | ||
8 | LUAINC_macosx=../../../build/lua/$(LUAV)/include | 23 | # DEBUG: NODEBUG DEBUG |
9 | #LUAINC_macosx=/opt/local/include | 24 | # debug mode causes luasocket to collect and returns timing information useful |
10 | #LUAINC_macosx=../../../../projects/lua_env/luaenv/lua_versions/lua-5.2.0-beta/src | 25 | # for testing and debugging luasocket itself |
11 | #LUAINC_macosx=../../../../projects/lua_env/luaenv/lua_versions/lua-5.1.4/src | 26 | DEBUG?=NODEBUG |
12 | 27 | ||
13 | #LUAINC_linux=/usr/local/include/lua$(LUAV) | 28 | # prefix: /usr/local /usr /opt/local /sw |
14 | LUAINC_linux=/usr/include/lua$(LUAV) | 29 | # the top of the default install tree |
15 | #LUAINC_linux=/usr/local/include | 30 | prefix?=/usr/local |
31 | |||
32 | # where lua headers are found for macosx builds | ||
33 | # LUAINC_macosx: /opt/local/include | ||
34 | LUAINC_macosx?=/opt/local/include | ||
35 | # FIXME default should be where fink puts lua | ||
36 | |||
37 | # LUAINC_linux: /usr/include/lua$(LUAV) /usr/local/include /usr/local/include/lua$(LUAV) | ||
38 | # where lua headers are found for linux builds | ||
39 | LUAINC_linux?=/usr/include/lua$(LUAV) | ||
40 | |||
41 | # LUAINC_win32: | ||
42 | # LUALIB_win32: | ||
43 | # where lua headers and libraries are found for win32 builds | ||
44 | LUAINC_win32?="../../lua-5.1.3/src" | ||
45 | LUALIB_win32?="../../lua-5.1.3" | ||
46 | # FIXME default should be where lua-for-windows puts lua | ||
47 | |||
48 | # DESTDIR: (no default) | ||
49 | # used by package managers to install into a temporary destination | ||
50 | DESTDIR= | ||
51 | |||
52 | #------ | ||
53 | # Definitions below can be overridden on the make command line, but | ||
54 | # shouldn't have to be. | ||
16 | 55 | ||
17 | LUAINC_win32="../../lua-5.1.3/src" | 56 | print: |
18 | LUALIB_win32="../../lua-5.1.3" | 57 | @echo PLAT=$(PLAT) |
58 | @echo LUAV=$(LUAV) | ||
59 | @echo DEBUG=$(DEBUG) | ||
60 | @echo prefix=$(prefix) | ||
61 | @echo LUAINC_$(PLAT)=$(LUAINC_$(PLAT)) | ||
62 | @echo LUALIB_$(PLAT)=$(LUALIB_$(PLAT)) | ||
19 | 63 | ||
20 | #------ | 64 | #------ |
21 | # Install directories | 65 | # Install directories |
@@ -44,7 +88,7 @@ PLATS= macosx linux win32 | |||
44 | SO_macosx=so | 88 | SO_macosx=so |
45 | O_macosx=o | 89 | O_macosx=o |
46 | CC_macosx=gcc | 90 | CC_macosx=gcc |
47 | DEF_macosx= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN -DLUA_COMPAT_MODULE \ | 91 | DEF_macosx= -DLUASOCKET_$(DEBUG) -DUNIX_HAS_SUN_LEN -DLUA_COMPAT_MODULE \ |
48 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ | 92 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ |
49 | -DMIME_API='__attribute__((visibility("default")))' | 93 | -DMIME_API='__attribute__((visibility("default")))' |
50 | CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ | 94 | CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ |
@@ -59,7 +103,7 @@ SOCKET_macosx=usocket.o | |||
59 | SO_linux=so | 103 | SO_linux=so |
60 | O_linux=o | 104 | O_linux=o |
61 | CC_linux=gcc | 105 | CC_linux=gcc |
62 | DEF_linux=-DLUASOCKET_DEBUG \ | 106 | DEF_linux=-DLUASOCKET_$(DEBUG) \ |
63 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ | 107 | -DLUASOCKET_API='__attribute__((visibility("default")))' \ |
64 | -DMIME_API='__attribute__((visibility("default")))' | 108 | -DMIME_API='__attribute__((visibility("default")))' |
65 | CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \ | 109 | CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -Wshadow -Wextra -Wimplicit -O2 -ggdb3 -fpic \ |
@@ -75,7 +119,7 @@ SO_win32=dll | |||
75 | O_win32=obj | 119 | O_win32=obj |
76 | CC_win32=cl | 120 | CC_win32=cl |
77 | DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \ | 121 | DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \ |
78 | /D "LUASOCKET_API=__declspec(dllexport)" /D "LUASOCKET_DEBUG" \ | 122 | /D "LUASOCKET_API=__declspec(dllexport)" /D "LUASOCKET_$(DEBUG)" \ |
79 | /D "_CRT_SECURE_NO_WARNINGS" /D "_WINDLL" | 123 | /D "_CRT_SECURE_NO_WARNINGS" /D "_WINDLL" |
80 | CFLAGS_win32=/I$(LUAINC) $(DEF) /O2 /Ot /MD /W3 /nologo | 124 | CFLAGS_win32=/I$(LUAINC) $(DEF) /O2 /Ot /MD /W3 /nologo |
81 | LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \ | 125 | LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \ |