aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2009-05-27 09:31:38 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2009-05-27 09:31:38 +0000
commitbce60be30fe8e9c1b0eb33128c23c93d7bca5303 (patch)
tree3927343c777fcb7764a0f2f89754a0ceab141c21 /src
parentd1a72435d5bd3528f3c334cd4d1da16dcead47bf (diff)
downloadluasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.tar.gz
luasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.tar.bz2
luasocket-bce60be30fe8e9c1b0eb33128c23c93d7bca5303.zip
Decent makefiles!
Diffstat (limited to 'src')
-rw-r--r--src/auxiliar.h2
-rw-r--r--src/buffer.c2
-rw-r--r--src/buffer.h2
-rw-r--r--src/except.h2
-rw-r--r--src/http.lua10
-rw-r--r--src/inet.h2
-rw-r--r--src/io.h2
-rw-r--r--src/luasocket.c2
-rw-r--r--src/luasocket.h6
-rw-r--r--src/makefile156
-rw-r--r--src/mime.c10
-rw-r--r--src/mime.h6
-rw-r--r--src/options.c86
-rw-r--r--src/options.h36
-rw-r--r--src/select.c39
-rw-r--r--src/select.h2
-rw-r--r--src/smtp.lua8
-rw-r--r--src/socket.h2
-rw-r--r--src/tcp.c12
-rw-r--r--src/tcp.h2
-rw-r--r--src/timeout.c18
-rw-r--r--src/timeout.h2
-rw-r--r--src/tp.lua1
-rw-r--r--src/udp.c41
-rw-r--r--src/udp.h2
-rw-r--r--src/unix.c10
-rw-r--r--src/unix.h4
-rw-r--r--src/usocket.h6
-rw-r--r--src/wsocket.h9
29 files changed, 334 insertions, 148 deletions
diff --git a/src/auxiliar.h b/src/auxiliar.h
index 8a18bcf..57a2ecc 100644
--- a/src/auxiliar.h
+++ b/src/auxiliar.h
@@ -27,8 +27,6 @@
27* 27*
28* The mapping from class name to the corresponding metatable and the 28* The mapping from class name to the corresponding metatable and the
29* reverse mapping are done using lauxlib. 29* reverse mapping are done using lauxlib.
30*
31* RCS ID: $Id$
32\*=========================================================================*/ 30\*=========================================================================*/
33 31
34#include "lua.h" 32#include "lua.h"
diff --git a/src/buffer.c b/src/buffer.c
index de817b2..363da3d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -166,7 +166,7 @@ static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
166 size_t total = 0; 166 size_t total = 0;
167 int err = IO_DONE; 167 int err = IO_DONE;
168 while (total < count && err == IO_DONE) { 168 while (total < count && err == IO_DONE) {
169 size_t done; 169 size_t done = 0;
170 size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE; 170 size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE;
171 err = io->send(io->ctx, data+total, step, &done, tm); 171 err = io->send(io->ctx, data+total, step, &done, tm);
172 total += done; 172 total += done;
diff --git a/src/buffer.h b/src/buffer.h
index 0a4a335..58838d1 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -14,8 +14,6 @@
14* 14*
15* The module is built on top of the I/O abstraction defined in io.h and the 15* The module is built on top of the I/O abstraction defined in io.h and the
16* timeout management is done with the timeout.h interface. 16* timeout management is done with the timeout.h interface.
17*
18* RCS ID: $Id$
19\*=========================================================================*/ 17\*=========================================================================*/
20#include "lua.h" 18#include "lua.h"
21 19
diff --git a/src/except.h b/src/except.h
index 03e417d..1e7a245 100644
--- a/src/except.h
+++ b/src/except.h
@@ -24,8 +24,6 @@
24* 24*
25* With these two function, it's easy to write functions that throw 25* With these two function, it's easy to write functions that throw
26* exceptions on error, but that don't interrupt the user script. 26* exceptions on error, but that don't interrupt the user script.
27*
28* RCS ID: $Id$
29\*=========================================================================*/ 27\*=========================================================================*/
30 28
31#include "lua.h" 29#include "lua.h"
diff --git a/src/http.lua b/src/http.lua
index 3a386a6..4c27149 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -13,6 +13,7 @@ local url = require("socket.url")
13local ltn12 = require("ltn12") 13local ltn12 = require("ltn12")
14local mime = require("mime") 14local mime = require("mime")
15local string = require("string") 15local string = require("string")
16local headers = require("socket.headers")
16local base = _G 17local base = _G
17local table = require("table") 18local table = require("table")
18module("socket.http") 19module("socket.http")
@@ -123,10 +124,11 @@ function metat.__index:sendrequestline(method, uri)
123 return self.try(self.c:send(reqline)) 124 return self.try(self.c:send(reqline))
124end 125end
125 126
126function metat.__index:sendheaders(headers) 127function metat.__index:sendheaders(tosend)
128 local canonic = headers.canonic
127 local h = "\r\n" 129 local h = "\r\n"
128 for i, v in base.pairs(headers) do 130 for f, v in base.pairs(tosend) do
129 h = i .. ": " .. v .. "\r\n" .. h 131 h = (canonic[f] or f) .. ": " .. v .. "\r\n" .. h
130 end 132 end
131 self.try(self.c:send(h)) 133 self.try(self.c:send(h))
132 return 1 134 return 1
@@ -254,7 +256,7 @@ local function shouldredirect(reqt, code, headers)
254 return headers.location and 256 return headers.location and
255 string.gsub(headers.location, "%s", "") ~= "" and 257 string.gsub(headers.location, "%s", "") ~= "" and
256 (reqt.redirect ~= false) and 258 (reqt.redirect ~= false) and
257 (code == 301 or code == 302) and 259 (code == 301 or code == 302 or code == 303 or code == 307) and
258 (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD") 260 (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
259 and (not reqt.nredirects or reqt.nredirects < 5) 261 and (not reqt.nredirects or reqt.nredirects < 5)
260end 262end
diff --git a/src/inet.h b/src/inet.h
index c6d717d..dbc160c 100644
--- a/src/inet.h
+++ b/src/inet.h
@@ -13,8 +13,6 @@
13* getpeername and getsockname functions as seen by Lua programs. 13* getpeername and getsockname functions as seen by Lua programs.
14* 14*
15* The Lua functions toip and tohostname are also implemented here. 15* The Lua functions toip and tohostname are also implemented here.
16*
17* RCS ID: $Id$
18\*=========================================================================*/ 16\*=========================================================================*/
19#include "lua.h" 17#include "lua.h"
20#include "socket.h" 18#include "socket.h"
diff --git a/src/io.h b/src/io.h
index bff48a6..8cca08a 100644
--- a/src/io.h
+++ b/src/io.h
@@ -11,8 +11,6 @@
11* 11*
12* The module socket.h implements this interface, and thus the module tcp.h 12* The module socket.h implements this interface, and thus the module tcp.h
13* is very simple. 13* is very simple.
14*
15* RCS ID: $Id$
16\*=========================================================================*/ 14\*=========================================================================*/
17#include <stdio.h> 15#include <stdio.h>
18#include "lua.h" 16#include "lua.h"
diff --git a/src/luasocket.c b/src/luasocket.c
index 142aa95..3b29e8e 100644
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -10,8 +10,6 @@
10* involved in setting up both client and server connections. The provided 10* involved in setting up both client and server connections. The provided
11* IO routines, however, follow the Lua style, being very similar to the 11* IO routines, however, follow the Lua style, being very similar to the
12* standard Lua read and write functions. 12* standard Lua read and write functions.
13*
14* RCS ID: $Id$
15\*=========================================================================*/ 13\*=========================================================================*/
16 14
17/*=========================================================================*\ 15/*=========================================================================*\
diff --git a/src/luasocket.h b/src/luasocket.h
index 13134cf..d7a78bb 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -5,16 +5,14 @@
5* Networking support for the Lua language 5* Networking support for the Lua language
6* Diego Nehab 6* Diego Nehab
7* 9/11/1999 7* 9/11/1999
8*
9* RCS ID: $Id$
10\*=========================================================================*/ 8\*=========================================================================*/
11#include "lua.h" 9#include "lua.h"
12 10
13/*-------------------------------------------------------------------------*\ 11/*-------------------------------------------------------------------------*\
14* Current socket library version 12* Current socket library version
15\*-------------------------------------------------------------------------*/ 13\*-------------------------------------------------------------------------*/
16#define LUASOCKET_VERSION "LuaSocket 2.0.2" 14#define LUASOCKET_VERSION "LuaSocket 2.0.3"
17#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2007 Diego Nehab" 15#define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2009 Diego Nehab"
18#define LUASOCKET_AUTHORS "Diego Nehab" 16#define LUASOCKET_AUTHORS "Diego Nehab"
19 17
20/*-------------------------------------------------------------------------*\ 18/*-------------------------------------------------------------------------*\
diff --git a/src/makefile b/src/makefile
index b614f77..3351997 100644
--- a/src/makefile
+++ b/src/makefile
@@ -1,39 +1,86 @@
1PLAT = none
2INSTALL_DATA=cp
3INSTALL_EXEC=cp
4INSTALL_TOP= /opt/local
5LUAINC= $(LUAINC_$(PLAT))
6
1#------ 7#------
2# Load configuration 8# Install directories
3# 9#
4include ../config 10INSTALL_TOP_SHARE=$(INSTALL_TOP)/share/lua/5.1
11INSTALL_TOP_LIB=$(INSTALL_TOP)/lib/lua/5.1
12INSTALL_SOCKET_SHARE=$(INSTALL_TOP_SHARE)/socket
13INSTALL_SOCKET_LIB=$(INSTALL_TOP_LIB)/socket
14INSTALL_MIME_SHARE=$(INSTALL_TOP_SHARE)/mime
15INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime
5 16
6#------ 17#------
7# Hopefully no need to change anything below this line 18# Output file names
8# 19#
20EXT=so
21SOCKET_V=2.0.3
22MIME_V=1.0.3
23SOCKET_SO=socket.$(EXT).$(SOCKET_V)
24MIME_SO=mime.$(EXT).$(MIME_V)
25UNIX_SO=unix.$(EXT)
9 26
10#------ 27#------
11# Modules belonging to socket-core 28# Compiler and linker settings
12# 29# for Mac OS X
30LUAINC_macosx= -I/opt/local/include
31CC_macosx=gcc
32DEF_macosx= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN \
33 -DLUASOCKET_API='__attribute__((visibility("default")))' \
34 -DMIME_API='__attribute__((visibility("default")))'
35CFLAGS_macosx= $(LUAINC) $(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common \
36 -fvisibility=hidden
37LDFLAGS_macosx= -bundle -undefined dynamic_lookup
38LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
13 39
14#$(COMPAT)/compat-5.1.o \ 40#------
41# Compiler and linker settings
42# for Linux
43LUAINC_linux= -I/usr/local/include/lua5.1
44CC_linux=gcc
45DEF_linux=-DLUASOCKET_DEBUG \
46 -DLUASOCKET_API='__attribute__((visibility("default")))' \
47 -DMIME_API='__attribute__((visibility("default")))'
48CFLAGS_linux= $(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic \
49 -fvisibility=hidden
50LDFLAGS_linux=-O -shared -fpic
51LD_linux= gcc
15 52
16SOCKET_OBJS:= \ 53#------
54# Settings selected for platform
55#
56CC=$(CC_$(PLAT))
57DEF=$(DEF_$(PLAT))
58CFLAGS=$(CFLAGS_$(PLAT))
59LDFLAGS=$(LDFLAGS_$(PLAT))
60LD=$(LD_$(PLAT))
61
62#------
63# Modules belonging to socket-core
64#
65SOCKET_OBJS= \
17 luasocket.o \ 66 luasocket.o \
18 timeout.o \ 67 timeout.o \
19 buffer.o \ 68 buffer.o \
20 io.o \ 69 io.o \
21 auxiliar.o \ 70 auxiliar.o \
22 options.o \ 71 options.o \
23 inet.o \ 72 inet.o \
24 tcp.o \ 73 usocket.o \
25 udp.o \
26 except.o \ 74 except.o \
27 select.o \ 75 select.o \
28 usocket.o 76 tcp.o \
77 udp.o
29 78
30#------ 79#------
31# Modules belonging mime-core 80# Modules belonging mime-core
32# 81#
33#$(COMPAT)/compat-5.1.o \ 82MIME_OBJS= \
34 83 mime.o
35MIME_OBJS:=\
36 mime.o
37 84
38#------ 85#------
39# Modules belonging unix (local domain sockets) 86# Modules belonging unix (local domain sockets)
@@ -47,7 +94,35 @@ UNIX_OBJS:=\
47 usocket.o \ 94 usocket.o \
48 unix.o 95 unix.o
49 96
50all: $(SOCKET_SO) $(MIME_SO) 97#------
98# Files to install
99#
100TO_SOCKET_SHARE:= \
101 http.lua \
102 url.lua \
103 tp.lua \
104 ftp.lua \
105 headers.lua \
106 smtp.lua
107
108TO_TOP_SHARE:= \
109 ltn12.lua \
110 socket.lua \
111 mime.lua
112
113default: $(PLAT)
114
115macosx:
116 $(MAKE) all PLAT=macosx
117
118linux:
119 $(MAKE) all PLAT=linux
120
121none:
122 @echo "Please choose a platform:"
123 @echo " $(PLATS)"
124
125all: $(SOCKET_SO) $(MIME_SO)
51 126
52$(SOCKET_SO): $(SOCKET_OBJS) 127$(SOCKET_SO): $(SOCKET_OBJS)
53 $(LD) $(LDFLAGS) -o $@ $(SOCKET_OBJS) 128 $(LD) $(LDFLAGS) -o $@ $(SOCKET_OBJS)
@@ -58,6 +133,25 @@ $(MIME_SO): $(MIME_OBJS)
58$(UNIX_SO): $(UNIX_OBJS) 133$(UNIX_SO): $(UNIX_OBJS)
59 $(LD) $(LDFLAGS) -o $@ $(UNIX_OBJS) 134 $(LD) $(LDFLAGS) -o $@ $(UNIX_OBJS)
60 135
136install:
137 mkdir -p $(INSTALL_TOP_SHARE)
138 $(INSTALL_DATA) $(TO_TOP_SHARE) $(INSTALL_TOP_SHARE)
139 mkdir -p $(INSTALL_SOCKET_SHARE)
140 $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE)
141 mkdir -p $(INSTALL_SOCKET_LIB)
142 $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(EXT)
143 mkdir -p $(INSTALL_MIME_LIB)
144 $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(EXT)
145
146local:
147 $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=..
148
149clean:
150 rm -f $(SOCKET_SO) $(SOCKET_OBJS)
151 rm -f $(MIME_SO) $(UNIX_SO) $(MIME_OBJS) $(UNIX_OBJS)
152
153.PHONY: all $(PLATS) default clean echo none
154
61#------ 155#------
62# List of dependencies 156# List of dependencies
63# 157#
@@ -66,25 +160,19 @@ buffer.o: buffer.c buffer.h io.h timeout.h
66except.o: except.c except.h 160except.o: except.c except.h
67inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h 161inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h
68io.o: io.c io.h timeout.h 162io.o: io.c io.h timeout.h
69luasocket.o: luasocket.c luasocket.h auxiliar.h except.h timeout.h \ 163luasocket.o: luasocket.c luasocket.h auxiliar.h except.h \
70 buffer.h io.h inet.h socket.h usocket.h tcp.h udp.h select.h 164 timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
165 udp.h select.h
71mime.o: mime.c mime.h 166mime.o: mime.c mime.h
72options.o: options.c auxiliar.h options.h socket.h io.h timeout.h \ 167options.o: options.c auxiliar.h options.h socket.h io.h \
73 usocket.h inet.h 168 timeout.h usocket.h inet.h
74select.o: select.c socket.h io.h timeout.h usocket.h select.h 169select.o: select.c socket.h io.h timeout.h usocket.h select.h
75tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \ 170tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h \
76 options.h tcp.h buffer.h 171 inet.h options.h tcp.h buffer.h
77timeout.o: timeout.c auxiliar.h timeout.h 172timeout.o: timeout.c auxiliar.h timeout.h
78udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \ 173udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h \
79 options.h udp.h 174 inet.h options.h udp.h
80unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h options.h \ 175unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h \
81 unix.h buffer.h 176 options.h unix.h buffer.h
82usocket.o: usocket.c socket.h io.h timeout.h usocket.h 177usocket.o: usocket.c socket.h io.h timeout.h usocket.h
83 178wsocket.o: wsocket.c socket.h io.h timeout.h usocket.h
84clean:
85 rm -f $(SOCKET_SO) $(SOCKET_OBJS)
86 rm -f $(MIME_SO) $(UNIX_SO) $(MIME_OBJS) $(UNIX_OBJS)
87
88#------
89# End of makefile configuration
90#
diff --git a/src/mime.c b/src/mime.c
index a41bf40..1e8fded 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -272,9 +272,12 @@ static int mime_global_b64(lua_State *L)
272 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 272 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
273 /* if second part is nil, we are done */ 273 /* if second part is nil, we are done */
274 if (!input) { 274 if (!input) {
275 size_t osize = 0;
275 asize = b64pad(atom, asize, &buffer); 276 asize = b64pad(atom, asize, &buffer);
276 luaL_pushresult(&buffer); 277 luaL_pushresult(&buffer);
277 if (!(*lua_tostring(L, -1))) lua_pushnil(L); 278 /* if the output is empty and the input is nil, return nil */
279 lua_tolstring(L, -1, &osize);
280 if (osize == 0) lua_pushnil(L);
278 lua_pushnil(L); 281 lua_pushnil(L);
279 return 2; 282 return 2;
280 } 283 }
@@ -313,8 +316,11 @@ static int mime_global_unb64(lua_State *L)
313 input = (UC *) luaL_optlstring(L, 2, NULL, &isize); 316 input = (UC *) luaL_optlstring(L, 2, NULL, &isize);
314 /* if second is nil, we are done */ 317 /* if second is nil, we are done */
315 if (!input) { 318 if (!input) {
319 size_t osize = 0;
316 luaL_pushresult(&buffer); 320 luaL_pushresult(&buffer);
317 if (!(*lua_tostring(L, -1))) lua_pushnil(L); 321 /* if the output is empty and the input is nil, return nil */
322 lua_tolstring(L, -1, &osize);
323 if (osize == 0) lua_pushnil(L);
318 lua_pushnil(L); 324 lua_pushnil(L);
319 return 2; 325 return 2;
320 } 326 }
diff --git a/src/mime.h b/src/mime.h
index 0fe8000..37f370f 100644
--- a/src/mime.h
+++ b/src/mime.h
@@ -7,16 +7,14 @@
7* This module provides functions to implement transfer content encodings 7* This module provides functions to implement transfer content encodings
8* and formatting conforming to RFC 2045. It is used by mime.lua, which 8* and formatting conforming to RFC 2045. It is used by mime.lua, which
9* provide a higher level interface to this functionality. 9* provide a higher level interface to this functionality.
10*
11* RCS ID: $Id$
12\*=========================================================================*/ 10\*=========================================================================*/
13#include "lua.h" 11#include "lua.h"
14 12
15/*-------------------------------------------------------------------------*\ 13/*-------------------------------------------------------------------------*\
16* Current MIME library version 14* Current MIME library version
17\*-------------------------------------------------------------------------*/ 15\*-------------------------------------------------------------------------*/
18#define MIME_VERSION "MIME 1.0.2" 16#define MIME_VERSION "MIME 1.0.3"
19#define MIME_COPYRIGHT "Copyright (C) 2004-2007 Diego Nehab" 17#define MIME_COPYRIGHT "Copyright (C) 2004-2009 Diego Nehab"
20#define MIME_AUTHORS "Diego Nehab" 18#define MIME_AUTHORS "Diego Nehab"
21 19
22/*-------------------------------------------------------------------------*\ 20/*-------------------------------------------------------------------------*\
diff --git a/src/options.c b/src/options.c
index a464a4b..1d4c950 100644
--- a/src/options.c
+++ b/src/options.c
@@ -12,12 +12,12 @@
12#include "options.h" 12#include "options.h"
13#include "inet.h" 13#include "inet.h"
14 14
15
16/*=========================================================================*\ 15/*=========================================================================*\
17* Internal functions prototypes 16* Internal functions prototypes
18\*=========================================================================*/ 17\*=========================================================================*/
19static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); 18static int opt_setmembership(lua_State *L, p_socket ps, int level, int name);
20static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); 19static int opt_setboolean(lua_State *L, p_socket ps, int level, int name);
20static int opt_getboolean(lua_State *L, p_socket ps, int level, int name);
21static int opt_set(lua_State *L, p_socket ps, int level, int name, 21static int opt_set(lua_State *L, p_socket ps, int level, int name,
22 void *val, int len); 22 void *val, int len);
23 23
@@ -40,39 +40,63 @@ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps)
40 return opt->func(L, ps); 40 return opt->func(L, ps);
41} 41}
42 42
43int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps)
44{
45 const char *name = luaL_checkstring(L, 2); /* obj, name, ... */
46 while (opt->name && strcmp(name, opt->name))
47 opt++;
48 if (!opt->func) {
49 char msg[45];
50 sprintf(msg, "unsupported option `%.35s'", name);
51 luaL_argerror(L, 2, msg);
52 }
53 return opt->func(L, ps);
54}
55
43/* enables reuse of local address */ 56/* enables reuse of local address */
44int opt_reuseaddr(lua_State *L, p_socket ps) 57int opt_set_reuseaddr(lua_State *L, p_socket ps)
45{ 58{
46 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); 59 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR);
47} 60}
48 61
62/* enables reuse of local port */
63int opt_set_reuseport(lua_State *L, p_socket ps)
64{
65 return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT);
66}
67
49/* disables the Naggle algorithm */ 68/* disables the Naggle algorithm */
50int opt_tcp_nodelay(lua_State *L, p_socket ps) 69int opt_set_tcp_nodelay(lua_State *L, p_socket ps)
51{ 70{
52 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); 71 return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY);
53} 72}
54 73
55int opt_keepalive(lua_State *L, p_socket ps) 74int opt_set_keepalive(lua_State *L, p_socket ps)
56{ 75{
57 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); 76 return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE);
58} 77}
59 78
60int opt_dontroute(lua_State *L, p_socket ps) 79int opt_set_dontroute(lua_State *L, p_socket ps)
61{ 80{
62 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); 81 return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE);
63} 82}
64 83
65int opt_broadcast(lua_State *L, p_socket ps) 84int opt_set_broadcast(lua_State *L, p_socket ps)
66{ 85{
67 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); 86 return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST);
68} 87}
69 88
70int opt_ip_multicast_loop(lua_State *L, p_socket ps) 89int opt_set_ip_multicast_loop(lua_State *L, p_socket ps)
71{ 90{
72 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); 91 return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
73} 92}
74 93
75int opt_linger(lua_State *L, p_socket ps) 94int opt_get_ip_multicast_loop(lua_State *L, p_socket ps)
95{
96 return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
97}
98
99int opt_set_linger(lua_State *L, p_socket ps)
76{ 100{
77 struct linger li; /* obj, name, table */ 101 struct linger li; /* obj, name, table */
78 if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); 102 if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE));
@@ -89,18 +113,43 @@ int opt_linger(lua_State *L, p_socket ps)
89 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); 113 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
90} 114}
91 115
92int opt_ip_multicast_ttl(lua_State *L, p_socket ps) 116int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps)
93{ 117{
94 int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ 118 int val = (int) luaL_checknumber(L, 3); /* obj, name, int */
95 return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val)); 119 return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_TTL,
120 (char *) &val, sizeof(val));
121}
122
123int opt_set_ip_multicast_if(lua_State *L, p_socket ps)
124{
125 const char *address = luaL_checkstring(L, 3); /* obj, name, ip */
126 struct in_addr val;
127 val.s_addr = htonl(INADDR_ANY);
128 if (strcmp(address, "*") && !inet_aton(address, &val))
129 luaL_argerror(L, 3, "ip expected");
130 return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF,
131 (char *) &val, sizeof(val));
132}
133
134int opt_get_ip_multicast_if(lua_State *L, p_socket ps)
135{
136 struct in_addr val;
137 socklen_t len = sizeof(val);
138 if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) {
139 lua_pushnil(L);
140 lua_pushstring(L, "getsockopt failed");
141 return 2;
142 }
143 lua_pushstring(L, inet_ntoa(val));
144 return 1;
96} 145}
97 146
98int opt_ip_add_membership(lua_State *L, p_socket ps) 147int opt_set_ip_add_membership(lua_State *L, p_socket ps)
99{ 148{
100 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); 149 return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
101} 150}
102 151
103int opt_ip_drop_membersip(lua_State *L, p_socket ps) 152int opt_set_ip_drop_membersip(lua_State *L, p_socket ps)
104{ 153{
105 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); 154 return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
106} 155}
@@ -141,6 +190,19 @@ int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len)
141 return 1; 190 return 1;
142} 191}
143 192
193static int opt_getboolean(lua_State *L, p_socket ps, int level, int name)
194{
195 int val = 0;
196 socklen_t len = sizeof(val);
197 if (getsockopt(*ps, level, name, (char *) &val, &len) < 0) {
198 lua_pushnil(L);
199 lua_pushstring(L, "getsockopt failed");
200 return 2;
201 }
202 lua_pushboolean(L, val);
203 return 1;
204}
205
144static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) 206static int opt_setboolean(lua_State *L, p_socket ps, int level, int name)
145{ 207{
146 int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ 208 int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */
diff --git a/src/options.h b/src/options.h
index 900761e..aa43cab 100644
--- a/src/options.h
+++ b/src/options.h
@@ -6,8 +6,6 @@
6* 6*
7* This module provides a common interface to socket options, used mainly by 7* This module provides a common interface to socket options, used mainly by
8* modules UDP and TCP. 8* modules UDP and TCP.
9*
10* RCS ID: $Id$
11\*=========================================================================*/ 9\*=========================================================================*/
12 10
13#include "lua.h" 11#include "lua.h"
@@ -20,20 +18,28 @@ typedef struct t_opt {
20} t_opt; 18} t_opt;
21typedef t_opt *p_opt; 19typedef t_opt *p_opt;
22 20
23/* supported options */ 21/* supported options for setoption */
24int opt_dontroute(lua_State *L, p_socket ps); 22int opt_set_dontroute(lua_State *L, p_socket ps);
25int opt_broadcast(lua_State *L, p_socket ps); 23int opt_set_broadcast(lua_State *L, p_socket ps);
26int opt_reuseaddr(lua_State *L, p_socket ps); 24int opt_set_reuseaddr(lua_State *L, p_socket ps);
27int opt_tcp_nodelay(lua_State *L, p_socket ps); 25int opt_set_tcp_nodelay(lua_State *L, p_socket ps);
28int opt_keepalive(lua_State *L, p_socket ps); 26int opt_set_keepalive(lua_State *L, p_socket ps);
29int opt_linger(lua_State *L, p_socket ps); 27int opt_set_linger(lua_State *L, p_socket ps);
30int opt_reuseaddr(lua_State *L, p_socket ps); 28int opt_set_reuseaddr(lua_State *L, p_socket ps);
31int opt_ip_multicast_ttl(lua_State *L, p_socket ps); 29int opt_set_reuseport(lua_State *L, p_socket ps);
32int opt_ip_multicast_loop(lua_State *L, p_socket ps); 30int opt_set_ip_multicast_if(lua_State *L, p_socket ps);
33int opt_ip_add_membership(lua_State *L, p_socket ps); 31int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps);
34int opt_ip_drop_membersip(lua_State *L, p_socket ps); 32int opt_set_ip_multicast_loop(lua_State *L, p_socket ps);
35 33int opt_set_ip_add_membership(lua_State *L, p_socket ps);
34int opt_set_ip_drop_membersip(lua_State *L, p_socket ps);
36/* invokes the appropriate option handler */ 35/* invokes the appropriate option handler */
37int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); 36int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps);
38 37
38/* supported options for getoption */
39int opt_get_ip_multicast_loop(lua_State *L, p_socket ps);
40int opt_get_ip_multicast_if(lua_State *L, p_socket ps);
41/* invokes the appropriate option handler */
42int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps);
43
44
39#endif 45#endif
diff --git a/src/select.c b/src/select.c
index 99b59f5..8e47d0e 100644
--- a/src/select.c
+++ b/src/select.c
@@ -18,8 +18,8 @@
18\*=========================================================================*/ 18\*=========================================================================*/
19static t_socket getfd(lua_State *L); 19static t_socket getfd(lua_State *L);
20static int dirty(lua_State *L); 20static int dirty(lua_State *L);
21static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd, 21static void collect_fd(lua_State *L, int tab, int itab,
22 int itab, fd_set *set); 22 fd_set *set, t_socket *max_fd);
23static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); 23static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set);
24static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, 24static void return_fd(lua_State *L, fd_set *set, t_socket max_fd,
25 int itab, int tab, int start); 25 int itab, int tab, int start);
@@ -39,6 +39,9 @@ static luaL_reg func[] = {
39* Initializes module 39* Initializes module
40\*-------------------------------------------------------------------------*/ 40\*-------------------------------------------------------------------------*/
41int select_open(lua_State *L) { 41int select_open(lua_State *L) {
42 lua_pushstring(L, "_SETSIZE");
43 lua_pushnumber(L, FD_SETSIZE);
44 lua_rawset(L, -3);
42 luaL_openlib(L, NULL, func, 0); 45 luaL_openlib(L, NULL, func, 0);
43 return 0; 46 return 0;
44} 47}
@@ -51,7 +54,7 @@ int select_open(lua_State *L) {
51\*-------------------------------------------------------------------------*/ 54\*-------------------------------------------------------------------------*/
52static int global_select(lua_State *L) { 55static int global_select(lua_State *L) {
53 int rtab, wtab, itab, ret, ndirty; 56 int rtab, wtab, itab, ret, ndirty;
54 t_socket max_fd; 57 t_socket max_fd = SOCKET_INVALID;
55 fd_set rset, wset; 58 fd_set rset, wset;
56 t_timeout tm; 59 t_timeout tm;
57 double t = luaL_optnumber(L, 3, -1); 60 double t = luaL_optnumber(L, 3, -1);
@@ -60,12 +63,12 @@ static int global_select(lua_State *L) {
60 lua_newtable(L); itab = lua_gettop(L); 63 lua_newtable(L); itab = lua_gettop(L);
61 lua_newtable(L); rtab = lua_gettop(L); 64 lua_newtable(L); rtab = lua_gettop(L);
62 lua_newtable(L); wtab = lua_gettop(L); 65 lua_newtable(L); wtab = lua_gettop(L);
63 max_fd = collect_fd(L, 1, SOCKET_INVALID, itab, &rset); 66 collect_fd(L, 1, itab, &rset, &max_fd);
67 collect_fd(L, 2, itab, &wset, &max_fd);
64 ndirty = check_dirty(L, 1, rtab, &rset); 68 ndirty = check_dirty(L, 1, rtab, &rset);
65 t = ndirty > 0? 0.0: t; 69 t = ndirty > 0? 0.0: t;
66 timeout_init(&tm, t, -1); 70 timeout_init(&tm, t, -1);
67 timeout_markstart(&tm); 71 timeout_markstart(&tm);
68 max_fd = collect_fd(L, 2, max_fd, itab, &wset);
69 ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm); 72 ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm);
70 if (ret > 0 || ndirty > 0) { 73 if (ret > 0 || ndirty > 0) {
71 return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); 74 return_fd(L, &rset, max_fd+1, itab, rtab, ndirty);
@@ -77,7 +80,7 @@ static int global_select(lua_State *L) {
77 lua_pushstring(L, "timeout"); 80 lua_pushstring(L, "timeout");
78 return 3; 81 return 3;
79 } else { 82 } else {
80 lua_pushstring(L, "error"); 83 luaL_error(L, "select failed");
81 return 3; 84 return 3;
82 } 85 }
83} 86}
@@ -112,11 +115,13 @@ static int dirty(lua_State *L) {
112 return is; 115 return is;
113} 116}
114 117
115static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd, 118static void collect_fd(lua_State *L, int tab, int itab,
116 int itab, fd_set *set) { 119 fd_set *set, t_socket *max_fd) {
117 int i = 1; 120 int i = 1, n = 0;
118 if (lua_isnil(L, tab)) 121 /* nil is the same as an empty table */
119 return max_fd; 122 if (lua_isnil(L, tab)) return;
123 /* otherwise we need it to be a table */
124 luaL_checktype(L, tab, LUA_TTABLE);
120 while (1) { 125 while (1) {
121 t_socket fd; 126 t_socket fd;
122 lua_pushnumber(L, i); 127 lua_pushnumber(L, i);
@@ -125,11 +130,18 @@ static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
125 lua_pop(L, 1); 130 lua_pop(L, 1);
126 break; 131 break;
127 } 132 }
133 /* getfd figures out if this is a socket */
128 fd = getfd(L); 134 fd = getfd(L);
129 if (fd != SOCKET_INVALID) { 135 if (fd != SOCKET_INVALID) {
136 /* make sure we don't overflow the fd_set */
137 if (n >= FD_SETSIZE)
138 luaL_argerror(L, tab, "too many sockets");
130 FD_SET(fd, set); 139 FD_SET(fd, set);
131 if (max_fd == SOCKET_INVALID || max_fd < fd) 140 n++;
132 max_fd = fd; 141 /* keep track of the largest descriptor so far */
142 if (*max_fd == SOCKET_INVALID || *max_fd < fd)
143 *max_fd = fd;
144 /* make sure we can map back from descriptor to the object */
133 lua_pushnumber(L, fd); 145 lua_pushnumber(L, fd);
134 lua_pushvalue(L, -2); 146 lua_pushvalue(L, -2);
135 lua_settable(L, itab); 147 lua_settable(L, itab);
@@ -137,7 +149,6 @@ static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
137 lua_pop(L, 1); 149 lua_pop(L, 1);
138 i = i + 1; 150 i = i + 1;
139 } 151 }
140 return max_fd;
141} 152}
142 153
143static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { 154static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) {
diff --git a/src/select.h b/src/select.h
index f2d2cc2..8750200 100644
--- a/src/select.h
+++ b/src/select.h
@@ -8,8 +8,6 @@
8* method getfd() which returns the descriptor to be passed to the 8* method getfd() which returns the descriptor to be passed to the
9* underlying select function. Another method, dirty(), should return 9* underlying select function. Another method, dirty(), should return
10* true if there is data ready for reading (required for buffered input). 10* true if there is data ready for reading (required for buffered input).
11*
12* RCS ID: $Id$
13\*=========================================================================*/ 11\*=========================================================================*/
14 12
15int select_open(lua_State *L); 13int select_open(lua_State *L);
diff --git a/src/smtp.lua b/src/smtp.lua
index 9a204d8..b39f5d6 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -16,6 +16,7 @@ local os = require("os")
16local socket = require("socket") 16local socket = require("socket")
17local tp = require("socket.tp") 17local tp = require("socket.tp")
18local ltn12 = require("ltn12") 18local ltn12 = require("ltn12")
19local headers = require("socket.headers")
19local mime = require("mime") 20local mime = require("mime")
20module("socket.smtp") 21module("socket.smtp")
21 22
@@ -146,10 +147,11 @@ end
146local send_message 147local send_message
147 148
148-- yield the headers all at once, it's faster 149-- yield the headers all at once, it's faster
149local function send_headers(headers) 150local function send_headers(tosend)
151 local canonic = headers.canonic
150 local h = "\r\n" 152 local h = "\r\n"
151 for i,v in base.pairs(headers) do 153 for f,v in base.pairs(tosend) do
152 h = i .. ': ' .. v .. "\r\n" .. h 154 h = (canonic[f] or f) .. ': ' .. v .. "\r\n" .. h
153 end 155 end
154 coroutine.yield(h) 156 coroutine.yield(h)
155end 157end
diff --git a/src/socket.h b/src/socket.h
index de5d79f..425628d 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -8,8 +8,6 @@
8* differences. Also, not all *nix platforms behave the same. This module 8* differences. Also, not all *nix platforms behave the same. This module
9* (and the associated usocket.h and wsocket.h) factor these differences and 9* (and the associated usocket.h and wsocket.h) factor these differences and
10* creates a interface compatible with the io.h module. 10* creates a interface compatible with the io.h module.
11*
12* RCS ID: $Id$
13\*=========================================================================*/ 11\*=========================================================================*/
14#include "io.h" 12#include "io.h"
15 13
diff --git a/src/tcp.c b/src/tcp.c
index 539ad5e..06d687b 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -64,11 +64,11 @@ static luaL_reg tcp[] = {
64}; 64};
65 65
66/* socket option handlers */ 66/* socket option handlers */
67static t_opt opt[] = { 67static t_opt optset[] = {
68 {"keepalive", opt_keepalive}, 68 {"keepalive", opt_set_keepalive},
69 {"reuseaddr", opt_reuseaddr}, 69 {"reuseaddr", opt_set_reuseaddr},
70 {"tcp-nodelay", opt_tcp_nodelay}, 70 {"tcp-nodelay", opt_set_tcp_nodelay},
71 {"linger", opt_linger}, 71 {"linger", opt_set_linger},
72 {NULL, NULL} 72 {NULL, NULL}
73}; 73};
74 74
@@ -128,7 +128,7 @@ static int meth_setstats(lua_State *L) {
128static int meth_setoption(lua_State *L) 128static int meth_setoption(lua_State *L)
129{ 129{
130 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); 130 p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1);
131 return opt_meth_setoption(L, opt, &tcp->sock); 131 return opt_meth_setoption(L, optset, &tcp->sock);
132} 132}
133 133
134/*-------------------------------------------------------------------------*\ 134/*-------------------------------------------------------------------------*\
diff --git a/src/tcp.h b/src/tcp.h
index 7e98ab6..a536b49 100644
--- a/src/tcp.h
+++ b/src/tcp.h
@@ -13,8 +13,6 @@
13* objects are tcp objects bound to some local address. Client objects are 13* objects are tcp objects bound to some local address. Client objects are
14* tcp objects either connected to some address or returned by the accept 14* tcp objects either connected to some address or returned by the accept
15* method of a server object. 15* method of a server object.
16*
17* RCS ID: $Id$
18\*=========================================================================*/ 16\*=========================================================================*/
19#include "lua.h" 17#include "lua.h"
20 18
diff --git a/src/timeout.c b/src/timeout.c
index 863546e..fb015f9 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -5,6 +5,8 @@
5* RCS ID: $Id$ 5* RCS ID: $Id$
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8#include <limits.h>
9#include <float.h>
8 10
9#include "lua.h" 11#include "lua.h"
10#include "lauxlib.h" 12#include "lauxlib.h"
@@ -187,13 +189,23 @@ static int timeout_lua_gettime(lua_State *L)
187/*-------------------------------------------------------------------------*\ 189/*-------------------------------------------------------------------------*\
188* Sleep for n seconds. 190* Sleep for n seconds.
189\*-------------------------------------------------------------------------*/ 191\*-------------------------------------------------------------------------*/
192#ifdef _WIN32
190int timeout_lua_sleep(lua_State *L) 193int timeout_lua_sleep(lua_State *L)
191{ 194{
192 double n = luaL_checknumber(L, 1); 195 double n = luaL_checknumber(L, 1);
193#ifdef _WIN32 196 if (n < 0.0) n = 0.0;
194 Sleep((int)(n*1000)); 197 if (n < DBL_MAX/1000.0) n *= 1000.0;
198 if (n > INT_MAX) n = INT_MAX;
199 Sleep((int)n);
200 return 0;
201}
195#else 202#else
203int timeout_lua_sleep(lua_State *L)
204{
205 double n = luaL_checknumber(L, 1);
196 struct timespec t, r; 206 struct timespec t, r;
207 if (n < 0.0) n = 0.0;
208 if (n > INT_MAX) n = INT_MAX;
197 t.tv_sec = (int) n; 209 t.tv_sec = (int) n;
198 n -= t.tv_sec; 210 n -= t.tv_sec;
199 t.tv_nsec = (int) (n * 1000000000); 211 t.tv_nsec = (int) (n * 1000000000);
@@ -202,6 +214,6 @@ int timeout_lua_sleep(lua_State *L)
202 t.tv_sec = r.tv_sec; 214 t.tv_sec = r.tv_sec;
203 t.tv_nsec = r.tv_nsec; 215 t.tv_nsec = r.tv_nsec;
204 } 216 }
205#endif
206 return 0; 217 return 0;
207} 218}
219#endif
diff --git a/src/timeout.h b/src/timeout.h
index a328bcc..6715ca7 100644
--- a/src/timeout.h
+++ b/src/timeout.h
@@ -3,8 +3,6 @@
3/*=========================================================================*\ 3/*=========================================================================*\
4* Timeout management functions 4* Timeout management functions
5* LuaSocket toolkit 5* LuaSocket toolkit
6*
7* RCS ID: $Id$
8\*=========================================================================*/ 6\*=========================================================================*/
9#include "lua.h" 7#include "lua.h"
10 8
diff --git a/src/tp.lua b/src/tp.lua
index 2eacdc4..5fd8d22 100644
--- a/src/tp.lua
+++ b/src/tp.lua
@@ -64,6 +64,7 @@ function metat.__index:check(ok)
64end 64end
65 65
66function metat.__index:command(cmd, arg) 66function metat.__index:command(cmd, arg)
67 cmd = string.upper(cmd)
67 if arg then 68 if arg then
68 return self.c:send(cmd .. " " .. arg.. "\r\n") 69 return self.c:send(cmd .. " " .. arg.. "\r\n")
69 else 70 else
diff --git a/src/udp.c b/src/udp.c
index e604bea..db519ca 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -37,6 +37,7 @@ static int meth_setsockname(lua_State *L);
37static int meth_setpeername(lua_State *L); 37static int meth_setpeername(lua_State *L);
38static int meth_close(lua_State *L); 38static int meth_close(lua_State *L);
39static int meth_setoption(lua_State *L); 39static int meth_setoption(lua_State *L);
40static int meth_getoption(lua_State *L);
40static int meth_settimeout(lua_State *L); 41static int meth_settimeout(lua_State *L);
41static int meth_getfd(lua_State *L); 42static int meth_getfd(lua_State *L);
42static int meth_setfd(lua_State *L); 43static int meth_setfd(lua_State *L);
@@ -57,22 +58,32 @@ static luaL_reg udp[] = {
57 {"sendto", meth_sendto}, 58 {"sendto", meth_sendto},
58 {"setfd", meth_setfd}, 59 {"setfd", meth_setfd},
59 {"setoption", meth_setoption}, 60 {"setoption", meth_setoption},
61 {"getoption", meth_getoption},
60 {"setpeername", meth_setpeername}, 62 {"setpeername", meth_setpeername},
61 {"setsockname", meth_setsockname}, 63 {"setsockname", meth_setsockname},
62 {"settimeout", meth_settimeout}, 64 {"settimeout", meth_settimeout},
63 {NULL, NULL} 65 {NULL, NULL}
64}; 66};
65 67
66/* socket options */ 68/* socket options for setoption */
67static t_opt opt[] = { 69static t_opt optset[] = {
68 {"dontroute", opt_dontroute}, 70 {"dontroute", opt_set_dontroute},
69 {"broadcast", opt_broadcast}, 71 {"broadcast", opt_set_broadcast},
70 {"reuseaddr", opt_reuseaddr}, 72 {"reuseaddr", opt_set_reuseaddr},
71 {"ip-multicast-ttl", opt_ip_multicast_ttl}, 73 {"reuseport", opt_set_reuseport},
72 {"ip-multicast-loop", opt_ip_multicast_loop}, 74 {"ip-multicast-if", opt_set_ip_multicast_if},
73 {"ip-add-membership", opt_ip_add_membership}, 75 {"ip-multicast-ttl", opt_set_ip_multicast_ttl},
74 {"ip-drop-membership", opt_ip_drop_membersip}, 76 {"ip-multicast-loop", opt_set_ip_multicast_loop},
75 {NULL, NULL} 77 {"ip-add-membership", opt_set_ip_add_membership},
78 {"ip-drop-membership", opt_set_ip_drop_membersip},
79 {NULL, NULL}
80};
81
82/* socket options for getoption */
83static t_opt optget[] = {
84 {"ip-multicast-if", opt_get_ip_multicast_if},
85 {"ip-multicast-loop", opt_get_ip_multicast_loop},
86 {NULL, NULL}
76}; 87};
77 88
78/* functions in library namespace */ 89/* functions in library namespace */
@@ -247,7 +258,15 @@ static int meth_getsockname(lua_State *L) {
247\*-------------------------------------------------------------------------*/ 258\*-------------------------------------------------------------------------*/
248static int meth_setoption(lua_State *L) { 259static int meth_setoption(lua_State *L) {
249 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); 260 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
250 return opt_meth_setoption(L, opt, &udp->sock); 261 return opt_meth_setoption(L, optset, &udp->sock);
262}
263
264/*-------------------------------------------------------------------------*\
265* Just call option handler
266\*-------------------------------------------------------------------------*/
267static int meth_getoption(lua_State *L) {
268 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
269 return opt_meth_getoption(L, optget, &udp->sock);
251} 270}
252 271
253/*-------------------------------------------------------------------------*\ 272/*-------------------------------------------------------------------------*\
diff --git a/src/udp.h b/src/udp.h
index d29f833..358d9e1 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -11,8 +11,6 @@
11* originally unconnected. They can be "connected" to a given address 11* originally unconnected. They can be "connected" to a given address
12* with a call to the setpeername function. The same function can be used to 12* with a call to the setpeername function. The same function can be used to
13* break the connection. 13* break the connection.
14*
15* RCS ID: $Id$
16\*=========================================================================*/ 14\*=========================================================================*/
17#include "lua.h" 15#include "lua.h"
18 16
diff --git a/src/unix.c b/src/unix.c
index 0430086..a309823 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -63,10 +63,10 @@ static luaL_reg un[] = {
63}; 63};
64 64
65/* socket option handlers */ 65/* socket option handlers */
66static t_opt opt[] = { 66static t_opt optset[] = {
67 {"keepalive", opt_keepalive}, 67 {"keepalive", opt_set_keepalive},
68 {"reuseaddr", opt_reuseaddr}, 68 {"reuseaddr", opt_set_reuseaddr},
69 {"linger", opt_linger}, 69 {"linger", opt_set_linger},
70 {NULL, NULL} 70 {NULL, NULL}
71}; 71};
72 72
@@ -128,7 +128,7 @@ static int meth_setstats(lua_State *L) {
128\*-------------------------------------------------------------------------*/ 128\*-------------------------------------------------------------------------*/
129static int meth_setoption(lua_State *L) { 129static int meth_setoption(lua_State *L) {
130 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); 130 p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1);
131 return opt_meth_setoption(L, opt, &un->sock); 131 return opt_meth_setoption(L, optset, &un->sock);
132} 132}
133 133
134/*-------------------------------------------------------------------------*\ 134/*-------------------------------------------------------------------------*\
diff --git a/src/unix.h b/src/unix.h
index 055b02b..39fa719 100644
--- a/src/unix.h
+++ b/src/unix.h
@@ -6,8 +6,6 @@
6* 6*
7* This module is just an example of how to extend LuaSocket with a new 7* This module is just an example of how to extend LuaSocket with a new
8* domain. 8* domain.
9*
10* RCS ID: $Id$
11\*=========================================================================*/ 9\*=========================================================================*/
12#include "lua.h" 10#include "lua.h"
13 11
@@ -23,6 +21,6 @@ typedef struct t_unix_ {
23} t_unix; 21} t_unix;
24typedef t_unix *p_unix; 22typedef t_unix *p_unix;
25 23
26int luaopen_socketunix(lua_State *L); 24int luaopen_socket_unix(lua_State *L);
27 25
28#endif /* UNIX_H */ 26#endif /* UNIX_H */
diff --git a/src/usocket.h b/src/usocket.h
index e1f7498..75bfe82 100644
--- a/src/usocket.h
+++ b/src/usocket.h
@@ -3,8 +3,6 @@
3/*=========================================================================*\ 3/*=========================================================================*\
4* Socket compatibilization module for Unix 4* Socket compatibilization module for Unix
5* LuaSocket toolkit 5* LuaSocket toolkit
6*
7* RCS ID: $Id$
8\*=========================================================================*/ 6\*=========================================================================*/
9 7
10/*=========================================================================*\ 8/*=========================================================================*\
@@ -32,6 +30,10 @@
32/* TCP options (nagle algorithm disable) */ 30/* TCP options (nagle algorithm disable) */
33#include <netinet/tcp.h> 31#include <netinet/tcp.h>
34 32
33#ifndef SO_REUSEPORT
34#define SO_REUSEPORT SO_REUSEADDR
35#endif
36
35typedef int t_socket; 37typedef int t_socket;
36typedef t_socket *p_socket; 38typedef t_socket *p_socket;
37 39
diff --git a/src/wsocket.h b/src/wsocket.h
index e4f0e92..8e0f114 100644
--- a/src/wsocket.h
+++ b/src/wsocket.h
@@ -3,14 +3,13 @@
3/*=========================================================================*\ 3/*=========================================================================*\
4* Socket compatibilization module for Win32 4* Socket compatibilization module for Win32
5* LuaSocket toolkit 5* LuaSocket toolkit
6*
7* RCS ID: $Id$
8\*=========================================================================*/ 6\*=========================================================================*/
9 7
10/*=========================================================================*\ 8/*=========================================================================*\
11* WinSock include files 9* WinSock include files
12\*=========================================================================*/ 10\*=========================================================================*/
13#include <winsock.h> 11#include <winsock2.h>
12#include <ws2tcpip.h>
14 13
15typedef int socklen_t; 14typedef int socklen_t;
16typedef SOCKET t_socket; 15typedef SOCKET t_socket;
@@ -18,4 +17,8 @@ typedef t_socket *p_socket;
18 17
19#define SOCKET_INVALID (INVALID_SOCKET) 18#define SOCKET_INVALID (INVALID_SOCKET)
20 19
20#ifndef SO_REUSEPORT
21#define SO_REUSEPORT SO_REUSEADDR
22#endif
23
21#endif /* WSOCKET_H */ 24#endif /* WSOCKET_H */