diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-16 00:06:04 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-16 00:06:04 +0000 |
commit | c51d4acf1c2a8675a3bb043e799ff4390cef47d6 (patch) | |
tree | 345b71aa70b50c964a58980461e147a260fa6e0b | |
parent | 3099704affa1fda195eb8f3934f6c1f18bf1f706 (diff) | |
download | luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.tar.gz luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.tar.bz2 luasocket-c51d4acf1c2a8675a3bb043e799ff4390cef47d6.zip |
Adjusted a few inconsistencies with the manual.
-rw-r--r-- | TODO | 30 | ||||
-rw-r--r-- | etc/dict.lua | 2 | ||||
-rw-r--r-- | etc/tftp.lua | 4 | ||||
-rw-r--r-- | logo.ps | 210 | ||||
-rw-r--r-- | samples/daytimeclnt.lua | 2 | ||||
-rw-r--r-- | samples/echoclnt.lua | 12 | ||||
-rw-r--r-- | samples/echosrvr.lua | 9 | ||||
-rw-r--r-- | samples/tinyirc.lua | 6 | ||||
-rw-r--r-- | src/ftp.lua | 4 | ||||
-rw-r--r-- | src/http.lua | 2 | ||||
-rw-r--r-- | src/inet.c | 29 | ||||
-rw-r--r-- | src/smtp.lua | 12 | ||||
-rw-r--r-- | src/tcp.c | 164 | ||||
-rw-r--r-- | src/timeout.c | 2 | ||||
-rw-r--r-- | src/timeout.h | 2 | ||||
-rw-r--r-- | src/udp.c | 10 | ||||
-rw-r--r-- | src/usocket.c | 6 | ||||
-rw-r--r-- | src/wsocket.c | 6 | ||||
-rw-r--r-- | test/smtptest.lua | 16 | ||||
-rw-r--r-- | test/testclnt.lua | 22 |
20 files changed, 403 insertions, 147 deletions
@@ -1,8 +1,34 @@ | |||
1 | - Melhorar a interface de setoptions (aceitar nada como true, por exemplo) | 1 | URL e URI eh a mesma coisa? |
2 | |||
3 | Check spelling | ||
4 | Fazer uma página com os exemplos. | ||
5 | Ajeitar links pras RFCs. | ||
6 | Usar DFN no lugar de <i> pra definiccoes. | ||
7 | Usar VAR pra argumentos? | ||
8 | Usar CODE pros trechos? funciona como PRE? | ||
9 | Usar PRE { margin-botton: 1em; } pra pular linha antes do </pre> | ||
10 | Trocar todos os <a name=bla> por <... id=bla>? | ||
11 | Make sure all .html are STRICT 4.01 | ||
12 | Add license.txt. | ||
13 | Check RFC links. | ||
14 | Add lots of hyperlinks | ||
15 | Check all function names (must use . or :) | ||
16 | Make sure IPv4 goes away | ||
17 | The words function and method should follow the convention | ||
18 | Adjust dates in all files | ||
19 | Test the library on every system possible | ||
20 | Document socket.time and socket.sleep | ||
21 | Create the windows executable. | ||
22 | |||
23 | Implement time critical stuff from code module in C. | ||
24 | Add service name translation. | ||
25 | |||
26 | Ajeitar o protocolo da lua_socketlibopen()... | ||
27 | |||
28 | - testar os options! | ||
2 | - testar em várias plataformas | 29 | - testar em várias plataformas |
3 | - adicionar exemplos de expansão: pipe, local, named pipe | 30 | - adicionar exemplos de expansão: pipe, local, named pipe |
4 | 31 | ||
5 | * Como mostrar um erro em lua_socketlibopen()... | ||
6 | * O location do "redirect" pode ser relativo ao servidor atual (não pode, | 32 | * O location do "redirect" pode ser relativo ao servidor atual (não pode, |
7 | mas os servidores fazem merda...) | 33 | mas os servidores fazem merda...) |
8 | * Ajeitar para Lua 5.0 | 34 | * Ajeitar para Lua 5.0 |
diff --git a/etc/dict.lua b/etc/dict.lua index 9926538..933c1c8 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
@@ -32,7 +32,7 @@ end | |||
32 | function dict_open() | 32 | function dict_open() |
33 | local sock, err = socket.connect("dict.org", 2628) | 33 | local sock, err = socket.connect("dict.org", 2628) |
34 | if not sock then return nil, err end | 34 | if not sock then return nil, err end |
35 | sock:timeout(10) | 35 | sock:settimeout(10) |
36 | local code, par = get_status(sock, 220) | 36 | local code, par = get_status(sock, 220) |
37 | if code then return nil, code end | 37 | if code then return nil, code end |
38 | return sock | 38 | return sock |
diff --git a/etc/tftp.lua b/etc/tftp.lua index d1b5594..4113034 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua | |||
@@ -82,8 +82,8 @@ function Public.get(url) | |||
82 | local udp, err = socket.udp() | 82 | local udp, err = socket.udp() |
83 | if not udp then return nil, err end | 83 | if not udp then return nil, err end |
84 | -- convert from name to ip if needed | 84 | -- convert from name to ip if needed |
85 | parsed.host = socket.toip(parsed.host) | 85 | parsed.host = socket.dns.toip(parsed.host) |
86 | udp:timeout(1) | 86 | udp:settimeout(1) |
87 | -- first packet gives data host/port to be used for data transfers | 87 | -- first packet gives data host/port to be used for data transfers |
88 | retries = 0 | 88 | retries = 0 |
89 | repeat | 89 | repeat |
@@ -0,0 +1,210 @@ | |||
1 | %!PS-Adobe-2.0 EPSF-2.0 | ||
2 | %%Title: Lua logo | ||
3 | %%Creator: lua@tecgraf.puc-rio.br | ||
4 | %%CreationDate: Wed Nov 29 19:04:04 EDT 2000 | ||
5 | %%BoundingBox: -45 0 1035 1080 | ||
6 | %%Pages: 1 | ||
7 | %%EndComments | ||
8 | %%EndProlog | ||
9 | |||
10 | %------------------------------------------------------------------------------ | ||
11 | % | ||
12 | % Copyright (C) 1998-2000. All rights reserved. | ||
13 | % Graphic design by Alexandre Nakonechny (nako@openlink.com.br). | ||
14 | % PostScript programming by the Lua team (lua@tecgraf.puc-rio.br). | ||
15 | % | ||
16 | % Permission is hereby granted, without written agreement and without license | ||
17 | % or royalty fees, to use, copy, and distribute this logo for any purpose, | ||
18 | % including commercial applications, subject to the following conditions: | ||
19 | % | ||
20 | % * The origin of this logo must not be misrepresented; you must not | ||
21 | % claim that you drew the original logo. We recommend that you give credit | ||
22 | % to the graphics designer in all printed matter that includes the logo. | ||
23 | % | ||
24 | % * The only modification you can make is to adapt the orbiting text to | ||
25 | % your product name. | ||
26 | % | ||
27 | % * The logo can be used in any scale as long as the relative proportions | ||
28 | % of its elements are maintained. | ||
29 | % | ||
30 | %------------------------------------------------------------------------------ | ||
31 | |||
32 | /LABEL (tekcoS) def | ||
33 | |||
34 | %-- DO NOT CHANGE ANYTHING BELOW THIS LINE ------------------------------------ | ||
35 | |||
36 | /PLANETCOLOR {0 0 0.5 setrgbcolor} bind def | ||
37 | /HOLECOLOR {1.0 setgray} bind def | ||
38 | /ORBITCOLOR {0.5 setgray} bind def | ||
39 | /LOGOFONT {/Helvetica 0.90} def | ||
40 | /LABELFONT {/Helvetica 0.36} def | ||
41 | |||
42 | %------------------------------------------------------------------------------ | ||
43 | |||
44 | /MOONCOLOR {PLANETCOLOR} bind def | ||
45 | /LOGOCOLOR {HOLECOLOR} bind def | ||
46 | /LABELCOLOR {ORBITCOLOR} bind def | ||
47 | |||
48 | /LABELANGLE 325 def | ||
49 | /LOGO (Lua) def | ||
50 | |||
51 | /DASHANGLE 10 def | ||
52 | /HALFDASHANGLE DASHANGLE 2 div def | ||
53 | |||
54 | % moon radius. planet radius is 1. | ||
55 | /r 1 2 sqrt 2 div sub def | ||
56 | |||
57 | /D {0 360 arc fill} bind def | ||
58 | /F {exch findfont exch scalefont setfont} bind def | ||
59 | |||
60 | % place it nicely on the paper | ||
61 | /RESOLUTION 1024 def | ||
62 | RESOLUTION 2 div dup translate | ||
63 | RESOLUTION 2 div 2 sqrt div dup scale | ||
64 | |||
65 | %-------------------------------------------------------------------- planet -- | ||
66 | PLANETCOLOR | ||
67 | 0 0 1 D | ||
68 | |||
69 | %---------------------------------------------------------------------- hole -- | ||
70 | HOLECOLOR | ||
71 | 1 2 r mul sub dup r D | ||
72 | |||
73 | %---------------------------------------------------------------------- moon -- | ||
74 | MOONCOLOR | ||
75 | 1 1 r D | ||
76 | |||
77 | %---------------------------------------------------------------------- logo -- | ||
78 | LOGOCOLOR | ||
79 | LOGOFONT | ||
80 | F | ||
81 | LOGO stringwidth pop 2 div neg | ||
82 | -0.5 moveto | ||
83 | LOGO show | ||
84 | |||
85 | %------------------------------------------------------------------------------ | ||
86 | % based on code from Blue Book Program 10, on pages 167--169 | ||
87 | % available at ftp://ftp.adobe.com/pub/adobe/displaypostscript/bluebook.shar | ||
88 | |||
89 | % str ptsize centerangle radius outsidecircletext -- | ||
90 | /outsidecircletext { | ||
91 | circtextdict begin | ||
92 | /radius exch def | ||
93 | /centerangle exch def | ||
94 | /ptsize exch def | ||
95 | /str exch def | ||
96 | |||
97 | gsave | ||
98 | str radius ptsize findhalfangle | ||
99 | centerangle | ||
100 | add rotate | ||
101 | str | ||
102 | { /charcode exch def | ||
103 | ( ) dup 0 charcode put outsideplacechar | ||
104 | } forall | ||
105 | |||
106 | grestore | ||
107 | end | ||
108 | } def | ||
109 | |||
110 | % string radius ptsize findhalfangle halfangle | ||
111 | /findhalfangle { | ||
112 | 4 div add | ||
113 | exch | ||
114 | stringwidth pop 2 div | ||
115 | exch | ||
116 | 2 mul 3.1415926535 mul div 360 mul | ||
117 | } def | ||
118 | |||
119 | /circtextdict 16 dict def | ||
120 | circtextdict begin | ||
121 | |||
122 | /outsideplacechar { | ||
123 | /char exch def | ||
124 | /halfangle char radius ptsize findhalfangle def | ||
125 | gsave | ||
126 | halfangle neg rotate | ||
127 | 1.4 0 translate | ||
128 | 90 rotate | ||
129 | char stringwidth pop 2 div neg 0 moveto | ||
130 | char show | ||
131 | grestore | ||
132 | halfangle 2 mul neg rotate | ||
133 | } def | ||
134 | |||
135 | end | ||
136 | |||
137 | %--------------------------------------------------------------------- label -- | ||
138 | LABELFONT | ||
139 | F | ||
140 | |||
141 | /LABELSIZE LABELFONT exch pop def | ||
142 | /LABELRADIUS LABELSIZE 3 div 1 r add sub neg 1.02 mul def | ||
143 | |||
144 | |||
145 | /HALFANGLE | ||
146 | LABEL LABELRADIUS LABELSIZE findhalfangle | ||
147 | HALFDASHANGLE div ceiling HALFDASHANGLE mul | ||
148 | def | ||
149 | |||
150 | /LABELANGLE | ||
151 | 60 LABELANGLE HALFANGLE sub | ||
152 | lt | ||
153 | { | ||
154 | HALFANGLE | ||
155 | HALFANGLE DASHANGLE div floor DASHANGLE mul | ||
156 | eq | ||
157 | {LABELANGLE DASHANGLE div ceiling DASHANGLE mul} | ||
158 | {LABELANGLE HALFDASHANGLE sub DASHANGLE div round DASHANGLE mul HALFDASHANGLE add} | ||
159 | ifelse | ||
160 | } | ||
161 | {HALFANGLE 60 add} | ||
162 | ifelse | ||
163 | def | ||
164 | |||
165 | LABELCOLOR | ||
166 | LABEL | ||
167 | LABELSIZE | ||
168 | LABELANGLE | ||
169 | LABELRADIUS | ||
170 | outsidecircletext | ||
171 | |||
172 | %--------------------------------------------------------------------- orbit -- | ||
173 | ORBITCOLOR | ||
174 | 0.03 setlinewidth | ||
175 | [1 r add 3.1415926535 180 div HALFDASHANGLE mul mul] 0 setdash | ||
176 | newpath | ||
177 | 0 0 | ||
178 | 1 r add | ||
179 | 3 copy | ||
180 | 30 | ||
181 | LABELANGLE HALFANGLE add | ||
182 | arcn | ||
183 | stroke | ||
184 | 60 | ||
185 | LABELANGLE HALFANGLE sub | ||
186 | 2 copy | ||
187 | lt {arc stroke} {4 {pop} repeat} ifelse | ||
188 | |||
189 | %------------------------------------------------------------------ copyright -- | ||
190 | /COPYRIGHT | ||
191 | (Graphic design by A. Nakonechny. Copyright (c) 1998, All rights reserved.) | ||
192 | def | ||
193 | |||
194 | LABELCOLOR | ||
195 | LOGOFONT | ||
196 | 32 div | ||
197 | F | ||
198 | 2 sqrt 0.99 mul | ||
199 | dup | ||
200 | neg | ||
201 | moveto | ||
202 | COPYRIGHT | ||
203 | 90 rotate | ||
204 | %show | ||
205 | |||
206 | %---------------------------------------------------------------------- done -- | ||
207 | showpage | ||
208 | |||
209 | %%Trailer | ||
210 | %%EOF | ||
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua index 5064fff..29abe17 100644 --- a/samples/daytimeclnt.lua +++ b/samples/daytimeclnt.lua | |||
@@ -10,7 +10,7 @@ if arg then | |||
10 | host = arg[1] or host | 10 | host = arg[1] or host |
11 | port = arg[2] or port | 11 | port = arg[2] or port |
12 | end | 12 | end |
13 | host = socket.toip(host) | 13 | host = socket.dns.toip(host) |
14 | udp = socket.udp() | 14 | udp = socket.udp() |
15 | print("Using host '" ..host.. "' and port " ..port.. "...") | 15 | print("Using host '" ..host.. "' and port " ..port.. "...") |
16 | udp:setpeername(host, port) | 16 | udp:setpeername(host, port) |
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua index e028b86..56bd123 100644 --- a/samples/echoclnt.lua +++ b/samples/echoclnt.lua | |||
@@ -10,17 +10,17 @@ if arg then | |||
10 | host = arg[1] or host | 10 | host = arg[1] or host |
11 | port = arg[2] or port | 11 | port = arg[2] or port |
12 | end | 12 | end |
13 | host = socket.toip(host) | 13 | host = socket.dns.toip(host) |
14 | udp, err = socket.udp() | 14 | udp, err = socket.udp() |
15 | if not udp then print(err) exit() end | 15 | assert(udp, err) |
16 | err = udp:setpeername(host, port) | 16 | ret, err = udp:setpeername(host, port) |
17 | if err then print(err) exit() end | 17 | assert(ret, err) |
18 | print("Using host '" ..host.. "' and port " .. port .. "...") | 18 | print("Using host '" ..host.. "' and port " .. port .. "...") |
19 | while 1 do | 19 | while 1 do |
20 | line = io.read() | 20 | line = io.read() |
21 | if not line then os.exit() end | 21 | if not line then os.exit() end |
22 | err = udp:send(line) | 22 | ret, err = udp:send(line) |
23 | if err then print(err) os.exit() end | 23 | if not ret then print(err) os.exit() end |
24 | dgram, err = udp:receive() | 24 | dgram, err = udp:receive() |
25 | if not dgram then print(err) os.exit() end | 25 | if not dgram then print(err) os.exit() end |
26 | print(dgram) | 26 | print(dgram) |
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua index 127ccb8..a7ed03c 100644 --- a/samples/echosrvr.lua +++ b/samples/echosrvr.lua | |||
@@ -12,11 +12,12 @@ if arg then | |||
12 | end | 12 | end |
13 | print("Binding to host '" ..host.. "' and port " ..port.. "...") | 13 | print("Binding to host '" ..host.. "' and port " ..port.. "...") |
14 | udp, err = socket.udp() | 14 | udp, err = socket.udp() |
15 | if not udp then print(err) os.exit() end | 15 | assert(udp, err) |
16 | err = udp:setsockname(host, port) | 16 | ret, err = udp:setsockname(host, port) |
17 | if err then print(err) os.exit() end | 17 | assert(ret, err) |
18 | udp:timeout(5) | 18 | udp:settimeout(5) |
19 | ip, port = udp:getsockname() | 19 | ip, port = udp:getsockname() |
20 | assert(ip, port) | ||
20 | print("Waiting packets on " .. ip .. ":" .. port .. "...") | 21 | print("Waiting packets on " .. ip .. ":" .. port .. "...") |
21 | while 1 do | 22 | while 1 do |
22 | dgram, ip, port = udp:receivefrom() | 23 | dgram, ip, port = udp:receivefrom() |
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index d9cb896..b48b90a 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua | |||
@@ -15,10 +15,10 @@ end | |||
15 | 15 | ||
16 | server1, error = socket.bind(host, port1) | 16 | server1, error = socket.bind(host, port1) |
17 | assert(server1, error) | 17 | assert(server1, error) |
18 | server1:timeout(1) -- make sure we don't block in accept | 18 | server1:settimeout(1) -- make sure we don't block in accept |
19 | server2, error = socket.bind(host, port2) | 19 | server2, error = socket.bind(host, port2) |
20 | assert(server2, error) | 20 | assert(server2, error) |
21 | server2:timeout(1) -- make sure we don't block in accept | 21 | server2:settimeout(1) -- make sure we don't block in accept |
22 | 22 | ||
23 | io.write("Servers bound\n") | 23 | io.write("Servers bound\n") |
24 | 24 | ||
@@ -55,7 +55,7 @@ while 1 do | |||
55 | io.write("Waiting for clients\n") | 55 | io.write("Waiting for clients\n") |
56 | local new = input:accept() | 56 | local new = input:accept() |
57 | if new then | 57 | if new then |
58 | new:timeout(1) | 58 | new:settimeout(1) |
59 | io.write("Inserting client in set\n") | 59 | io.write("Inserting client in set\n") |
60 | set:insert(new) | 60 | set:insert(new) |
61 | end | 61 | end |
diff --git a/src/ftp.lua b/src/ftp.lua index 9d75d2a..25226a6 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
@@ -224,7 +224,7 @@ function Private.port(control) | |||
224 | local server, ctl_ip | 224 | local server, ctl_ip |
225 | ctl_ip, answer = control:getsockname() | 225 | ctl_ip, answer = control:getsockname() |
226 | server, answer = socket.bind(ctl_ip, 0) | 226 | server, answer = socket.bind(ctl_ip, 0) |
227 | server:timeout(Public.TIMEOUT) | 227 | server:settimeout(Public.TIMEOUT) |
228 | local ip, p, ph, pl | 228 | local ip, p, ph, pl |
229 | ip, p = server:getsockname() | 229 | ip, p = server:getsockname() |
230 | pl = math.mod(p, 256) | 230 | pl = math.mod(p, 256) |
@@ -404,7 +404,7 @@ function Private.open(parsed) | |||
404 | local control, err = socket.connect(parsed.host, parsed.port) | 404 | local control, err = socket.connect(parsed.host, parsed.port) |
405 | if not control then return nil, err end | 405 | if not control then return nil, err end |
406 | -- make sure we don't block forever | 406 | -- make sure we don't block forever |
407 | control:timeout(Public.TIMEOUT) | 407 | control:settimeout(Public.TIMEOUT) |
408 | -- check greeting | 408 | -- check greeting |
409 | local code, answer = Private.greet(control) | 409 | local code, answer = Private.greet(control) |
410 | if not code then return nil, answer end | 410 | if not code then return nil, answer end |
diff --git a/src/http.lua b/src/http.lua index 4ef2c87..212e8f6 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -553,7 +553,7 @@ function Public.request_cb(request, response) | |||
553 | sock, response.error = socket.connect(parsed.host, parsed.port) | 553 | sock, response.error = socket.connect(parsed.host, parsed.port) |
554 | if not sock then return response end | 554 | if not sock then return response end |
555 | -- set connection timeout so that we do not hang forever | 555 | -- set connection timeout so that we do not hang forever |
556 | sock:timeout(Public.TIMEOUT) | 556 | sock:settimeout(Public.TIMEOUT) |
557 | -- send request message | 557 | -- send request message |
558 | response.error = Private.send_request(sock, request.method, | 558 | response.error = Private.send_request(sock, request.method, |
559 | Private.request_uri(parsed), request.headers, request.body_cb) | 559 | Private.request_uri(parsed), request.headers, request.body_cb) |
@@ -20,6 +20,7 @@ static int inet_global_toip(lua_State *L); | |||
20 | static int inet_global_tohostname(lua_State *L); | 20 | static int inet_global_tohostname(lua_State *L); |
21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); | 21 | static void inet_pushresolved(lua_State *L, struct hostent *hp); |
22 | 22 | ||
23 | /* DNS functions */ | ||
23 | static luaL_reg func[] = { | 24 | static luaL_reg func[] = { |
24 | { "toip", inet_global_toip }, | 25 | { "toip", inet_global_toip }, |
25 | { "tohostname", inet_global_tohostname }, | 26 | { "tohostname", inet_global_tohostname }, |
@@ -34,7 +35,19 @@ static luaL_reg func[] = { | |||
34 | \*-------------------------------------------------------------------------*/ | 35 | \*-------------------------------------------------------------------------*/ |
35 | void inet_open(lua_State *L) | 36 | void inet_open(lua_State *L) |
36 | { | 37 | { |
37 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | 38 | lua_pushstring(L, LUASOCKET_LIBNAME); |
39 | lua_gettable(L, LUA_GLOBALSINDEX); | ||
40 | if (lua_isnil(L, -1)) { | ||
41 | lua_pop(L, 1); | ||
42 | lua_newtable(L); | ||
43 | lua_pushstring(L, LUASOCKET_LIBNAME); | ||
44 | lua_pushvalue(L, -2); | ||
45 | lua_settable(L, LUA_GLOBALSINDEX); | ||
46 | } | ||
47 | lua_pushstring(L, "dns"); | ||
48 | lua_newtable(L); | ||
49 | luaL_openlib(L, NULL, func, 0); | ||
50 | lua_settable(L, -3); | ||
38 | lua_pop(L, 1); | 51 | lua_pop(L, 1); |
39 | } | 52 | } |
40 | 53 | ||
@@ -100,10 +113,11 @@ int inet_meth_getpeername(lua_State *L, p_sock ps) | |||
100 | socklen_t peer_len = sizeof(peer); | 113 | socklen_t peer_len = sizeof(peer); |
101 | if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { | 114 | if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { |
102 | lua_pushnil(L); | 115 | lua_pushnil(L); |
103 | return 1; | 116 | lua_pushstring(L, "getpeername failed"); |
117 | } else { | ||
118 | lua_pushstring(L, inet_ntoa(peer.sin_addr)); | ||
119 | lua_pushnumber(L, ntohs(peer.sin_port)); | ||
104 | } | 120 | } |
105 | lua_pushstring(L, inet_ntoa(peer.sin_addr)); | ||
106 | lua_pushnumber(L, ntohs(peer.sin_port)); | ||
107 | return 2; | 121 | return 2; |
108 | } | 122 | } |
109 | 123 | ||
@@ -116,10 +130,11 @@ int inet_meth_getsockname(lua_State *L, p_sock ps) | |||
116 | socklen_t local_len = sizeof(local); | 130 | socklen_t local_len = sizeof(local); |
117 | if (getsockname(*ps, (SA *) &local, &local_len) < 0) { | 131 | if (getsockname(*ps, (SA *) &local, &local_len) < 0) { |
118 | lua_pushnil(L); | 132 | lua_pushnil(L); |
119 | return 1; | 133 | lua_pushstring(L, "getsockname failed"); |
134 | } else { | ||
135 | lua_pushstring(L, inet_ntoa(local.sin_addr)); | ||
136 | lua_pushnumber(L, ntohs(local.sin_port)); | ||
120 | } | 137 | } |
121 | lua_pushstring(L, inet_ntoa(local.sin_addr)); | ||
122 | lua_pushnumber(L, ntohs(local.sin_port)); | ||
123 | return 2; | 138 | return 2; |
124 | } | 139 | } |
125 | 140 | ||
diff --git a/src/smtp.lua b/src/smtp.lua index 209825b..5249160 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
@@ -243,7 +243,7 @@ function Private.open(server) | |||
243 | -- connect to server and make sure we won't hang | 243 | -- connect to server and make sure we won't hang |
244 | local sock, err = socket.connect(server, Public.PORT) | 244 | local sock, err = socket.connect(server, Public.PORT) |
245 | if not sock then return nil, err end | 245 | if not sock then return nil, err end |
246 | sock:timeout(Public.TIMEOUT) | 246 | sock:settimeout(Public.TIMEOUT) |
247 | -- initial server greeting | 247 | -- initial server greeting |
248 | code, answer = Private.check_answer(sock, 220) | 248 | code, answer = Private.check_answer(sock, 220) |
249 | if not code then return nil, answer end | 249 | if not code then return nil, answer end |
@@ -305,10 +305,10 @@ end | |||
305 | ----------------------------------------------------------------------------- | 305 | ----------------------------------------------------------------------------- |
306 | function Public.mail(message) | 306 | function Public.mail(message) |
307 | local sock, err = Private.open(message.server) | 307 | local sock, err = Private.open(message.server) |
308 | if not sock then return err end | 308 | if not sock then return nil, err end |
309 | local code, answer = Private.send(sock, message) | 309 | local code, answer = Private.send(sock, message) |
310 | if not code then return answer end | 310 | if not code then return nil, answer end |
311 | code, answer = Private.close(sock) | 311 | code, answer = Private.close(sock) |
312 | if code then return nil end | 312 | if code then return 1 |
313 | return answer | 313 | else return nil, answer end |
314 | end | 314 | end |
@@ -29,10 +29,10 @@ static int meth_receive(lua_State *L); | |||
29 | static int meth_accept(lua_State *L); | 29 | static int meth_accept(lua_State *L); |
30 | static int meth_close(lua_State *L); | 30 | static int meth_close(lua_State *L); |
31 | static int meth_setoption(lua_State *L); | 31 | static int meth_setoption(lua_State *L); |
32 | static int meth_timeout(lua_State *L); | 32 | static int meth_settimeout(lua_State *L); |
33 | static int meth_fd(lua_State *L); | 33 | static int meth_fd(lua_State *L); |
34 | static int meth_dirty(lua_State *L); | 34 | static int meth_dirty(lua_State *L); |
35 | static int opt_nodelay(lua_State *L); | 35 | static int opt_tcp_nodelay(lua_State *L); |
36 | static int opt_keepalive(lua_State *L); | 36 | static int opt_keepalive(lua_State *L); |
37 | static int opt_linger(lua_State *L); | 37 | static int opt_linger(lua_State *L); |
38 | 38 | ||
@@ -47,7 +47,7 @@ static luaL_reg tcp[] = { | |||
47 | {"setsockname", meth_bind}, | 47 | {"setsockname", meth_bind}, |
48 | {"getpeername", meth_getpeername}, | 48 | {"getpeername", meth_getpeername}, |
49 | {"getsockname", meth_getsockname}, | 49 | {"getsockname", meth_getsockname}, |
50 | {"timeout", meth_timeout}, | 50 | {"settimeout", meth_settimeout}, |
51 | {"close", meth_close}, | 51 | {"close", meth_close}, |
52 | {"setoption", meth_setoption}, | 52 | {"setoption", meth_setoption}, |
53 | {"__gc", meth_close}, | 53 | {"__gc", meth_close}, |
@@ -59,7 +59,7 @@ static luaL_reg tcp[] = { | |||
59 | /* socket option handlers */ | 59 | /* socket option handlers */ |
60 | static luaL_reg opt[] = { | 60 | static luaL_reg opt[] = { |
61 | {"keepalive", opt_keepalive}, | 61 | {"keepalive", opt_keepalive}, |
62 | {"nodelay", opt_nodelay}, | 62 | {"tcp-nodelay", opt_tcp_nodelay}, |
63 | {"linger", opt_linger}, | 63 | {"linger", opt_linger}, |
64 | {NULL, NULL} | 64 | {NULL, NULL} |
65 | }; | 65 | }; |
@@ -83,8 +83,8 @@ void tcp_open(lua_State *L) | |||
83 | aux_add2group(L, "tcp{master}", "tcp{any}"); | 83 | aux_add2group(L, "tcp{master}", "tcp{any}"); |
84 | aux_add2group(L, "tcp{client}", "tcp{any}"); | 84 | aux_add2group(L, "tcp{client}", "tcp{any}"); |
85 | aux_add2group(L, "tcp{server}", "tcp{any}"); | 85 | aux_add2group(L, "tcp{server}", "tcp{any}"); |
86 | aux_add2group(L, "tcp{client}", "tcp{client, server}"); | 86 | aux_add2group(L, "tcp{client}", "tcp{client,server}"); |
87 | aux_add2group(L, "tcp{server}", "tcp{client, server}"); | 87 | aux_add2group(L, "tcp{server}", "tcp{client,server}"); |
88 | /* both server and client objects are selectable */ | 88 | /* both server and client objects are selectable */ |
89 | aux_add2group(L, "tcp{client}", "select{able}"); | 89 | aux_add2group(L, "tcp{client}", "select{able}"); |
90 | aux_add2group(L, "tcp{server}", "select{able}"); | 90 | aux_add2group(L, "tcp{server}", "select{able}"); |
@@ -121,7 +121,7 @@ static int meth_setoption(lua_State *L) | |||
121 | 121 | ||
122 | static int opt_boolean(lua_State *L, int level, int name) | 122 | static int opt_boolean(lua_State *L, int level, int name) |
123 | { | 123 | { |
124 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 124 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
125 | int val = aux_checkboolean(L, 2); | 125 | int val = aux_checkboolean(L, 2); |
126 | if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { | 126 | if (setsockopt(tcp->sock, level, name, (char *) &val, sizeof(val)) < 0) { |
127 | lua_pushnil(L); | 127 | lua_pushnil(L); |
@@ -132,8 +132,8 @@ static int opt_boolean(lua_State *L, int level, int name) | |||
132 | return 1; | 132 | return 1; |
133 | } | 133 | } |
134 | 134 | ||
135 | /* disables the Nagle algorithm */ | 135 | /* disables the Naggle algorithm */ |
136 | static int opt_nodelay(lua_State *L) | 136 | static int opt_tcp_nodelay(lua_State *L) |
137 | { | 137 | { |
138 | struct protoent *pe = getprotobyname("TCP"); | 138 | struct protoent *pe = getprotobyname("TCP"); |
139 | if (!pe) { | 139 | if (!pe) { |
@@ -155,13 +155,13 @@ int opt_linger(lua_State *L) | |||
155 | struct linger li; | 155 | struct linger li; |
156 | if (!lua_istable(L, 2)) | 156 | if (!lua_istable(L, 2)) |
157 | luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE)); | 157 | luaL_typerror(L, 2, lua_typename(L, LUA_TTABLE)); |
158 | lua_pushstring(L, "onoff"); | 158 | lua_pushstring(L, "on"); |
159 | lua_gettable(L, 2); | 159 | lua_gettable(L, 2); |
160 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid onoff field"); | 160 | if (!lua_isboolean(L, -1)) luaL_argerror(L, 2, "invalid 'on' field"); |
161 | li.l_onoff = (int) lua_tonumber(L, -1); | 161 | li.l_onoff = lua_toboolean(L, -1); |
162 | lua_pushstring(L, "linger"); | 162 | lua_pushstring(L, "timeout"); |
163 | lua_gettable(L, 2); | 163 | lua_gettable(L, 2); |
164 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid linger field"); | 164 | if (!lua_isnumber(L, -1)) luaL_argerror(L, 2, "invalid 'timeout' field"); |
165 | li.l_linger = (int) lua_tonumber(L, -1); | 165 | li.l_linger = (int) lua_tonumber(L, -1); |
166 | if (setsockopt(tcp->sock, SOL_SOCKET, SO_LINGER, | 166 | if (setsockopt(tcp->sock, SOL_SOCKET, SO_LINGER, |
167 | (char *) &li, sizeof(li) < 0)) { | 167 | (char *) &li, sizeof(li) < 0)) { |
@@ -178,122 +178,122 @@ int opt_linger(lua_State *L) | |||
178 | \*-------------------------------------------------------------------------*/ | 178 | \*-------------------------------------------------------------------------*/ |
179 | static int meth_fd(lua_State *L) | 179 | static int meth_fd(lua_State *L) |
180 | { | 180 | { |
181 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | 181 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
182 | lua_pushnumber(L, tcp->sock); | 182 | lua_pushnumber(L, tcp->sock); |
183 | return 1; | 183 | return 1; |
184 | } | 184 | } |
185 | 185 | ||
186 | static int meth_dirty(lua_State *L) | 186 | static int meth_dirty(lua_State *L) |
187 | { | 187 | { |
188 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | 188 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
189 | lua_pushboolean(L, !buf_isempty(&tcp->buf)); | 189 | lua_pushboolean(L, !buf_isempty(&tcp->buf)); |
190 | return 1; | 190 | return 1; |
191 | } | 191 | } |
192 | 192 | ||
193 | /*-------------------------------------------------------------------------*\ | 193 | /*-------------------------------------------------------------------------*\ |
194 | * Just call inet methods | 194 | * Waits for and returns a client object attempting connection to the |
195 | \*-------------------------------------------------------------------------*/ | 195 | * server object |
196 | static int meth_getpeername(lua_State *L) | ||
197 | { | ||
198 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | ||
199 | return inet_meth_getpeername(L, &tcp->sock); | ||
200 | } | ||
201 | |||
202 | static int meth_getsockname(lua_State *L) | ||
203 | { | ||
204 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); | ||
205 | return inet_meth_getsockname(L, &tcp->sock); | ||
206 | } | ||
207 | |||
208 | /*-------------------------------------------------------------------------*\ | ||
209 | * Just call tm methods | ||
210 | \*-------------------------------------------------------------------------*/ | ||
211 | static int meth_timeout(lua_State *L) | ||
212 | { | ||
213 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | ||
214 | return tm_meth_timeout(L, &tcp->tm); | ||
215 | } | ||
216 | |||
217 | /*-------------------------------------------------------------------------*\ | ||
218 | * Closes socket used by object | ||
219 | \*-------------------------------------------------------------------------*/ | 196 | \*-------------------------------------------------------------------------*/ |
220 | static int meth_close(lua_State *L) | 197 | static int meth_accept(lua_State *L) |
221 | { | 198 | { |
222 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); | 199 | struct sockaddr_in addr; |
223 | sock_destroy(&tcp->sock); | 200 | socklen_t addr_len = sizeof(addr); |
224 | return 0; | 201 | p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); |
202 | p_tm tm = &server->tm; | ||
203 | p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); | ||
204 | tm_markstart(tm); | ||
205 | aux_setclass(L, "tcp{client}", -1); | ||
206 | for ( ;; ) { | ||
207 | sock_accept(&server->sock, &client->sock, | ||
208 | (SA *) &addr, &addr_len, tm_get(tm)); | ||
209 | if (client->sock == SOCK_INVALID) { | ||
210 | if (tm_get(tm) == 0) { | ||
211 | lua_pushnil(L); | ||
212 | io_pusherror(L, IO_TIMEOUT); | ||
213 | return 2; | ||
214 | } | ||
215 | } else break; | ||
216 | } | ||
217 | /* initialize remaining structure fields */ | ||
218 | io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); | ||
219 | tm_init(&client->tm, -1, -1); | ||
220 | buf_init(&client->buf, &client->io, &client->tm); | ||
221 | return 1; | ||
225 | } | 222 | } |
226 | 223 | ||
227 | /*-------------------------------------------------------------------------*\ | 224 | /*-------------------------------------------------------------------------*\ |
228 | * Turns a master tcp object into a client object. | 225 | * Turns a master object into a server object |
229 | \*-------------------------------------------------------------------------*/ | 226 | \*-------------------------------------------------------------------------*/ |
230 | static int meth_connect(lua_State *L) | 227 | static int meth_bind(lua_State *L) |
231 | { | 228 | { |
232 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 229 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); |
233 | const char *address = luaL_checkstring(L, 2); | 230 | const char *address = luaL_checkstring(L, 2); |
234 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 231 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
235 | const char *err = inet_tryconnect(&tcp->sock, address, port); | 232 | int backlog = (int) luaL_optnumber(L, 4, 1); |
233 | const char *err = inet_trybind(&tcp->sock, address, port, backlog); | ||
236 | if (err) { | 234 | if (err) { |
237 | lua_pushnil(L); | 235 | lua_pushnil(L); |
238 | lua_pushstring(L, err); | 236 | lua_pushstring(L, err); |
239 | return 2; | 237 | return 2; |
240 | } | 238 | } |
241 | /* turn master object into a client object */ | 239 | /* turn master object into a server object */ |
242 | aux_setclass(L, "tcp{client}", 1); | 240 | aux_setclass(L, "tcp{server}", 1); |
243 | lua_pushnumber(L, 1); | 241 | lua_pushnumber(L, 1); |
244 | return 1; | 242 | return 1; |
245 | } | 243 | } |
246 | 244 | ||
247 | /*-------------------------------------------------------------------------*\ | 245 | /*-------------------------------------------------------------------------*\ |
248 | * Turns a master object into a server object | 246 | * Turns a master tcp object into a client object. |
249 | \*-------------------------------------------------------------------------*/ | 247 | \*-------------------------------------------------------------------------*/ |
250 | static int meth_bind(lua_State *L) | 248 | static int meth_connect(lua_State *L) |
251 | { | 249 | { |
252 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 250 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); |
253 | const char *address = luaL_checkstring(L, 2); | 251 | const char *address = luaL_checkstring(L, 2); |
254 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 252 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
255 | int backlog = (int) luaL_optnumber(L, 4, 1); | 253 | const char *err = inet_tryconnect(&tcp->sock, address, port); |
256 | const char *err = inet_trybind(&tcp->sock, address, port, backlog); | ||
257 | if (err) { | 254 | if (err) { |
258 | lua_pushnil(L); | 255 | lua_pushnil(L); |
259 | lua_pushstring(L, err); | 256 | lua_pushstring(L, err); |
260 | return 2; | 257 | return 2; |
261 | } | 258 | } |
262 | /* turn master object into a server object */ | 259 | /* turn master object into a client object */ |
263 | aux_setclass(L, "tcp{server}", 1); | 260 | aux_setclass(L, "tcp{client}", 1); |
264 | lua_pushnumber(L, 1); | 261 | lua_pushnumber(L, 1); |
265 | return 1; | 262 | return 1; |
266 | } | 263 | } |
267 | 264 | ||
268 | /*-------------------------------------------------------------------------*\ | 265 | /*-------------------------------------------------------------------------*\ |
269 | * Waits for and returns a client object attempting connection to the | 266 | * Closes socket used by object |
270 | * server object | ||
271 | \*-------------------------------------------------------------------------*/ | 267 | \*-------------------------------------------------------------------------*/ |
272 | static int meth_accept(lua_State *L) | 268 | static int meth_close(lua_State *L) |
273 | { | 269 | { |
274 | struct sockaddr_in addr; | 270 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); |
275 | socklen_t addr_len = sizeof(addr); | 271 | sock_destroy(&tcp->sock); |
276 | p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); | 272 | return 0; |
277 | p_tm tm = &server->tm; | 273 | } |
278 | p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); | 274 | |
279 | tm_markstart(tm); | 275 | /*-------------------------------------------------------------------------*\ |
280 | aux_setclass(L, "tcp{client}", -1); | 276 | * Just call inet methods |
281 | for ( ;; ) { | 277 | \*-------------------------------------------------------------------------*/ |
282 | sock_accept(&server->sock, &client->sock, | 278 | static int meth_getpeername(lua_State *L) |
283 | (SA *) &addr, &addr_len, tm_get(tm)); | 279 | { |
284 | if (client->sock == SOCK_INVALID) { | 280 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); |
285 | if (tm_get(tm) == 0) { | 281 | return inet_meth_getpeername(L, &tcp->sock); |
286 | lua_pushnil(L); | 282 | } |
287 | io_pusherror(L, IO_TIMEOUT); | 283 | |
288 | return 2; | 284 | static int meth_getsockname(lua_State *L) |
289 | } | 285 | { |
290 | } else break; | 286 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); |
291 | } | 287 | return inet_meth_getsockname(L, &tcp->sock); |
292 | /* initialize remaining structure fields */ | 288 | } |
293 | io_init(&client->io, (p_send) sock_send, (p_recv) sock_recv, &client->sock); | 289 | |
294 | tm_init(&client->tm, -1, -1); | 290 | /*-------------------------------------------------------------------------*\ |
295 | buf_init(&client->buf, &client->io, &client->tm); | 291 | * Just call tm methods |
296 | return 1; | 292 | \*-------------------------------------------------------------------------*/ |
293 | static int meth_settimeout(lua_State *L) | ||
294 | { | ||
295 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client,server}", 1); | ||
296 | return tm_meth_settimeout(L, &tcp->tm); | ||
297 | } | 297 | } |
298 | 298 | ||
299 | /*=========================================================================*\ | 299 | /*=========================================================================*\ |
diff --git a/src/timeout.c b/src/timeout.c index 6a30e3a..38d1135 100644 --- a/src/timeout.c +++ b/src/timeout.c | |||
@@ -125,7 +125,7 @@ void tm_open(lua_State *L) | |||
125 | * time: time out value in seconds | 125 | * time: time out value in seconds |
126 | * mode: "b" for block timeout, "t" for total timeout. (default: b) | 126 | * mode: "b" for block timeout, "t" for total timeout. (default: b) |
127 | \*-------------------------------------------------------------------------*/ | 127 | \*-------------------------------------------------------------------------*/ |
128 | int tm_meth_timeout(lua_State *L, p_tm tm) | 128 | int tm_meth_settimeout(lua_State *L, p_tm tm) |
129 | { | 129 | { |
130 | int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); | 130 | int ms = lua_isnil(L, 2) ? -1 : (int) (luaL_checknumber(L, 2)*1000.0); |
131 | const char *mode = luaL_optstring(L, 3, "b"); | 131 | const char *mode = luaL_optstring(L, 3, "b"); |
diff --git a/src/timeout.h b/src/timeout.h index 32eb836..ef2f533 100644 --- a/src/timeout.h +++ b/src/timeout.h | |||
@@ -26,6 +26,6 @@ void tm_markstart(p_tm tm); | |||
26 | int tm_getstart(p_tm tm); | 26 | int tm_getstart(p_tm tm); |
27 | int tm_get(p_tm tm); | 27 | int tm_get(p_tm tm); |
28 | int tm_gettime(void); | 28 | int tm_gettime(void); |
29 | int tm_meth_timeout(lua_State *L, p_tm tm); | 29 | int tm_meth_settimeout(lua_State *L, p_tm tm); |
30 | 30 | ||
31 | #endif /* TM_H */ | 31 | #endif /* TM_H */ |
@@ -30,7 +30,7 @@ static int meth_setsockname(lua_State *L); | |||
30 | static int meth_setpeername(lua_State *L); | 30 | static int meth_setpeername(lua_State *L); |
31 | static int meth_close(lua_State *L); | 31 | static int meth_close(lua_State *L); |
32 | static int meth_setoption(lua_State *L); | 32 | static int meth_setoption(lua_State *L); |
33 | static int meth_timeout(lua_State *L); | 33 | static int meth_settimeout(lua_State *L); |
34 | static int meth_fd(lua_State *L); | 34 | static int meth_fd(lua_State *L); |
35 | static int meth_dirty(lua_State *L); | 35 | static int meth_dirty(lua_State *L); |
36 | static int opt_dontroute(lua_State *L); | 36 | static int opt_dontroute(lua_State *L); |
@@ -46,7 +46,7 @@ static luaL_reg udp[] = { | |||
46 | {"sendto", meth_sendto}, | 46 | {"sendto", meth_sendto}, |
47 | {"receive", meth_receive}, | 47 | {"receive", meth_receive}, |
48 | {"receivefrom", meth_receivefrom}, | 48 | {"receivefrom", meth_receivefrom}, |
49 | {"timeout", meth_timeout}, | 49 | {"settimeout", meth_settimeout}, |
50 | {"close", meth_close}, | 50 | {"close", meth_close}, |
51 | {"setoption", meth_setoption}, | 51 | {"setoption", meth_setoption}, |
52 | {"__gc", meth_close}, | 52 | {"__gc", meth_close}, |
@@ -252,10 +252,10 @@ static int opt_broadcast(lua_State *L) | |||
252 | /*-------------------------------------------------------------------------*\ | 252 | /*-------------------------------------------------------------------------*\ |
253 | * Just call tm methods | 253 | * Just call tm methods |
254 | \*-------------------------------------------------------------------------*/ | 254 | \*-------------------------------------------------------------------------*/ |
255 | static int meth_timeout(lua_State *L) | 255 | static int meth_settimeout(lua_State *L) |
256 | { | 256 | { |
257 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 257 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
258 | return tm_meth_timeout(L, &udp->tm); | 258 | return tm_meth_settimeout(L, &udp->tm); |
259 | } | 259 | } |
260 | 260 | ||
261 | /*-------------------------------------------------------------------------*\ | 261 | /*-------------------------------------------------------------------------*\ |
@@ -297,7 +297,7 @@ static int meth_close(lua_State *L) | |||
297 | \*-------------------------------------------------------------------------*/ | 297 | \*-------------------------------------------------------------------------*/ |
298 | static int meth_setsockname(lua_State *L) | 298 | static int meth_setsockname(lua_State *L) |
299 | { | 299 | { |
300 | p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); | 300 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); |
301 | const char *address = luaL_checkstring(L, 2); | 301 | const char *address = luaL_checkstring(L, 2); |
302 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 302 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
303 | const char *err = inet_trybind(&udp->sock, address, port, -1); | 303 | const char *err = inet_trybind(&udp->sock, address, port, -1); |
diff --git a/src/usocket.c b/src/usocket.c index cdd550c..202238b 100644 --- a/src/usocket.c +++ b/src/usocket.c | |||
@@ -26,8 +26,10 @@ int sock_open(void) | |||
26 | \*-------------------------------------------------------------------------*/ | 26 | \*-------------------------------------------------------------------------*/ |
27 | void sock_destroy(p_sock ps) | 27 | void sock_destroy(p_sock ps) |
28 | { | 28 | { |
29 | close(*ps); | 29 | if (*ps != SOCK_INVALID) { |
30 | *ps = SOCK_INVALID; | 30 | close(*ps); |
31 | *ps = SOCK_INVALID; | ||
32 | } | ||
31 | } | 33 | } |
32 | 34 | ||
33 | /*-------------------------------------------------------------------------*\ | 35 | /*-------------------------------------------------------------------------*\ |
diff --git a/src/wsocket.c b/src/wsocket.c index 2ce828e..f9e1084 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -29,8 +29,10 @@ int sock_open(void) | |||
29 | \*-------------------------------------------------------------------------*/ | 29 | \*-------------------------------------------------------------------------*/ |
30 | void sock_destroy(p_sock ps) | 30 | void sock_destroy(p_sock ps) |
31 | { | 31 | { |
32 | closesocket(*ps); | 32 | if (*ps != SOCK_INVALID) { |
33 | *ps = SOCK_INVALID; | 33 | closesocket(*ps); |
34 | *ps = SOCK_INVALID; | ||
35 | } | ||
34 | } | 36 | } |
35 | 37 | ||
36 | /*-------------------------------------------------------------------------*\ | 38 | /*-------------------------------------------------------------------------*\ |
diff --git a/test/smtptest.lua b/test/smtptest.lua index 09bf634..8468408 100644 --- a/test/smtptest.lua +++ b/test/smtptest.lua | |||
@@ -209,27 +209,27 @@ insert(sent, { | |||
209 | 209 | ||
210 | io.write("testing host not found: ") | 210 | io.write("testing host not found: ") |
211 | local c, e = socket.connect("wrong.host", 25) | 211 | local c, e = socket.connect("wrong.host", 25) |
212 | local err = socket.smtp.mail{ | 212 | local ret, err = socket.smtp.mail{ |
213 | from = from, | 213 | from = from, |
214 | rcpt = rcpt, | 214 | rcpt = rcpt, |
215 | server = "wrong.host" | 215 | server = "wrong.host" |
216 | } | 216 | } |
217 | if e ~= err then fail("wrong error message") | 217 | if ret or e ~= err then fail("wrong error message") |
218 | else print("ok") end | 218 | else print("ok") end |
219 | 219 | ||
220 | io.write("testing invalid from: ") | 220 | io.write("testing invalid from: ") |
221 | local err = socket.smtp.mail{ | 221 | local ret, err = socket.smtp.mail{ |
222 | from = ' " " (( _ * ', | 222 | from = ' " " (( _ * ', |
223 | rcpt = rcpt, | 223 | rcpt = rcpt, |
224 | } | 224 | } |
225 | if not err then fail("wrong error message") | 225 | if ret or not err then fail("wrong error message") |
226 | else print(err) end | 226 | else print(err) end |
227 | 227 | ||
228 | io.write("testing no rcpt: ") | 228 | io.write("testing no rcpt: ") |
229 | local err = socket.smtp.mail{ | 229 | local ret, err = socket.smtp.mail{ |
230 | from = from, | 230 | from = from, |
231 | } | 231 | } |
232 | if not err then fail("wrong error message") | 232 | if ret or not err then fail("wrong error message") |
233 | else print(err) end | 233 | else print(err) end |
234 | 234 | ||
235 | io.write("clearing mailbox: ") | 235 | io.write("clearing mailbox: ") |
@@ -238,8 +238,8 @@ print("ok") | |||
238 | 238 | ||
239 | io.write("sending messages: ") | 239 | io.write("sending messages: ") |
240 | for i = 1, table.getn(sent) do | 240 | for i = 1, table.getn(sent) do |
241 | err = socket.smtp.mail(sent[i]) | 241 | ret, err = socket.smtp.mail(sent[i]) |
242 | if err then fail(err) end | 242 | if not ret then fail(err) end |
243 | io.write("+") | 243 | io.write("+") |
244 | io.stdout:flush() | 244 | io.stdout:flush() |
245 | end | 245 | end |
diff --git a/test/testclnt.lua b/test/testclnt.lua index b58ca2b..3f217bd 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -121,7 +121,7 @@ test_methods(socket.tcp(), { | |||
121 | "getpeername", | 121 | "getpeername", |
122 | "getsockname", | 122 | "getsockname", |
123 | "setoption", | 123 | "setoption", |
124 | "timeout", | 124 | "settimeout", |
125 | "close", | 125 | "close", |
126 | }) | 126 | }) |
127 | 127 | ||
@@ -135,7 +135,7 @@ test_methods(socket.udp(), { | |||
135 | "receive", | 135 | "receive", |
136 | "receivefrom", | 136 | "receivefrom", |
137 | "setoption", | 137 | "setoption", |
138 | "timeout", | 138 | "settimeout", |
139 | "close", | 139 | "close", |
140 | }) | 140 | }) |
141 | 141 | ||
@@ -278,7 +278,7 @@ reconnect() | |||
278 | 278 | ||
279 | -- the value is not important, we only want | 279 | -- the value is not important, we only want |
280 | -- to test non-blockin I/O anyways | 280 | -- to test non-blockin I/O anyways |
281 | data:timeout(200) | 281 | data:settimeout(200) |
282 | test_raw(1) | 282 | test_raw(1) |
283 | test_raw(17) | 283 | test_raw(17) |
284 | test_raw(200) | 284 | test_raw(200) |
@@ -298,7 +298,7 @@ function test_totaltimeoutreceive(len, tm, sl) | |||
298 | reconnect() | 298 | reconnect() |
299 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) | 299 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) |
300 | remote (string.format ([[ | 300 | remote (string.format ([[ |
301 | data:timeout(%d) | 301 | data:settimeout(%d) |
302 | str = string.rep('a', %d) | 302 | str = string.rep('a', %d) |
303 | data:send(str) | 303 | data:send(str) |
304 | print('server: sleeping for %ds') | 304 | print('server: sleeping for %ds') |
@@ -306,7 +306,7 @@ function test_totaltimeoutreceive(len, tm, sl) | |||
306 | print('server: woke up') | 306 | print('server: woke up') |
307 | data:send(str) | 307 | data:send(str) |
308 | ]], 2*tm, len, sl, sl)) | 308 | ]], 2*tm, len, sl, sl)) |
309 | data:timeout(tm, "total") | 309 | data:settimeout(tm, "total") |
310 | str, err, elapsed = data:receive(2*len) | 310 | str, err, elapsed = data:receive(2*len) |
311 | check_timeout(tm, sl, elapsed, err, "receive", "total", | 311 | check_timeout(tm, sl, elapsed, err, "receive", "total", |
312 | string.len(str) == 2*len) | 312 | string.len(str) == 2*len) |
@@ -323,14 +323,14 @@ function test_totaltimeoutsend(len, tm, sl) | |||
323 | reconnect() | 323 | reconnect() |
324 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) | 324 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) |
325 | remote (string.format ([[ | 325 | remote (string.format ([[ |
326 | data:timeout(%d) | 326 | data:settimeout(%d) |
327 | str = data:receive(%d) | 327 | str = data:receive(%d) |
328 | print('server: sleeping for %ds') | 328 | print('server: sleeping for %ds') |
329 | socket.sleep(%d) | 329 | socket.sleep(%d) |
330 | print('server: woke up') | 330 | print('server: woke up') |
331 | str = data:receive(%d) | 331 | str = data:receive(%d) |
332 | ]], 2*tm, len, sl, sl, len)) | 332 | ]], 2*tm, len, sl, sl, len)) |
333 | data:timeout(tm, "total") | 333 | data:settimeout(tm, "total") |
334 | str = string.rep("a", 2*len) | 334 | str = string.rep("a", 2*len) |
335 | total, err, elapsed = data:send(str) | 335 | total, err, elapsed = data:send(str) |
336 | check_timeout(tm, sl, elapsed, err, "send", "total", | 336 | check_timeout(tm, sl, elapsed, err, "send", "total", |
@@ -348,7 +348,7 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
348 | reconnect() | 348 | reconnect() |
349 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | 349 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) |
350 | remote (string.format ([[ | 350 | remote (string.format ([[ |
351 | data:timeout(%d) | 351 | data:settimeout(%d) |
352 | str = string.rep('a', %d) | 352 | str = string.rep('a', %d) |
353 | data:send(str) | 353 | data:send(str) |
354 | print('server: sleeping for %ds') | 354 | print('server: sleeping for %ds') |
@@ -356,7 +356,7 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
356 | print('server: woke up') | 356 | print('server: woke up') |
357 | data:send(str) | 357 | data:send(str) |
358 | ]], 2*tm, len, sl, sl)) | 358 | ]], 2*tm, len, sl, sl)) |
359 | data:timeout(tm) | 359 | data:settimeout(tm) |
360 | str, err, elapsed = data:receive(2*len) | 360 | str, err, elapsed = data:receive(2*len) |
361 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", | 361 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", |
362 | string.len(str) == 2*len) | 362 | string.len(str) == 2*len) |
@@ -373,14 +373,14 @@ function test_blockingtimeoutsend(len, tm, sl) | |||
373 | reconnect() | 373 | reconnect() |
374 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | 374 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) |
375 | remote (string.format ([[ | 375 | remote (string.format ([[ |
376 | data:timeout(%d) | 376 | data:settimeout(%d) |
377 | str = data:receive(%d) | 377 | str = data:receive(%d) |
378 | print('server: sleeping for %ds') | 378 | print('server: sleeping for %ds') |
379 | socket.sleep(%d) | 379 | socket.sleep(%d) |
380 | print('server: woke up') | 380 | print('server: woke up') |
381 | str = data:receive(%d) | 381 | str = data:receive(%d) |
382 | ]], 2*tm, len, sl, sl, len)) | 382 | ]], 2*tm, len, sl, sl, len)) |
383 | data:timeout(tm) | 383 | data:settimeout(tm) |
384 | str = string.rep("a", 2*len) | 384 | str = string.rep("a", 2*len) |
385 | total, err, elapsed = data:send(str) | 385 | total, err, elapsed = data:send(str) |
386 | check_timeout(tm, sl, elapsed, err, "send", "blocking", | 386 | check_timeout(tm, sl, elapsed, err, "send", "blocking", |