Software /
code /
prosody
Annotate
net/server.lua @ 524:3f9f67f1a106
Merge from waqas
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 03 Dec 2008 15:09:58 +0000 |
parent | 519:cccd610a0ef9 |
child | 528:5fabb5aeed9d |
rev | line source |
---|---|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
1 -- Prosody IM v0.1 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
2 -- Copyright (C) 2008 Matthew Wild |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
3 -- Copyright (C) 2008 Waqas Hussain |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
4 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
5 -- This program is free software; you can redistribute it and/or |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
6 -- modify it under the terms of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
7 -- as published by the Free Software Foundation; either version 2 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
8 -- of the License, or (at your option) any later version. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
9 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
10 -- This program is distributed in the hope that it will be useful, |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
13 -- GNU General Public License for more details. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
14 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
15 -- You should have received a copy of the GNU General Public License |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
16 -- along with this program; if not, write to the Free Software |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
18 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
19 |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
374
diff
changeset
|
20 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
21 --[[ |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
22 |
64 | 23 server.lua by blastbeat of the luadch project |
24 | |
25 re-used here under the MIT/X Consortium License | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
26 |
64 | 27 - this script contains the server loop of the program |
28 - other scripts can reg a server here | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
29 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
30 ]]-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
31 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
32 ----------------------------------// DECLARATION //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
33 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
34 --// constants //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
35 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
36 local STAT_UNIT = 1 / ( 1024 * 1024 ) -- mb |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
37 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
38 --// lua functions //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
39 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
40 local function use( what ) return _G[ what ] end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
41 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
42 local type = use "type" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
43 local pairs = use "pairs" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
44 local ipairs = use "ipairs" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
45 local tostring = use "tostring" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
46 local collectgarbage = use "collectgarbage" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
47 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
48 --// lua libs //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
49 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
50 local table = use "table" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
51 local coroutine = use "coroutine" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
52 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
53 --// lua lib methods //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
54 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
55 local table_concat = table.concat |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
56 local table_remove = table.remove |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
57 local string_sub = use'string'.sub |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
58 local coroutine_wrap = coroutine.wrap |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
59 local coroutine_yield = coroutine.yield |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
60 local print = print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
61 local out_put = function () end --print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
62 local out_error = print; |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
63 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
64 --// extern libs //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
65 |
98
3a2d327c4856
server.lua should degrade gracefully when LuaSec not present
Matthew Wild <mwild1@gmail.com>
parents:
74
diff
changeset
|
66 local luasec = select(2, pcall(require, "ssl")) |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
67 local luasocket = require "socket" |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
68 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
69 --// extern lib methods //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
70 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
71 local ssl_wrap = ( luasec and luasec.wrap ) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
72 local socket_bind = luasocket.bind |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
73 local socket_select = luasocket.select |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
74 local ssl_newcontext = ( luasec and luasec.newcontext ) |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
75 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
76 --// functions //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
77 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
78 local loop |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
79 local stats |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
80 local addtimer |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
81 local closeall |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
82 local addserver |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
83 local firetimer |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
84 local closesocket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
85 local removesocket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
86 local wrapserver |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
87 local wraptcpclient |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
88 local wrapsslclient |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
89 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
90 --// tables //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
91 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
92 local listener |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
93 local readlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
94 local writelist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
95 local socketlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
96 local timelistener |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
97 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
98 --// simple data types //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
99 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
100 local _ |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
101 local readlen = 0 -- length of readlist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
102 local writelen = 0 -- lenght of writelist |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
103 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
104 local sendstat= 0 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
105 local receivestat = 0 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
106 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
107 ----------------------------------// DEFINITION //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
108 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
109 listener = { } -- key = port, value = table |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
110 readlist = { } -- array with sockets to read from |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
111 writelist = { } -- arrary with sockets to write to |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
112 socketlist = { } -- key = socket, value = wrapped socket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
113 timelistener = { } |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
114 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
115 stats = function( ) |
64 | 116 return receivestat, sendstat |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
117 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
118 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
119 wrapserver = function( listener, socket, ip, serverport, mode, sslctx ) -- this function wraps a server |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
120 |
64 | 121 local dispatch, disconnect = listener.listener, listener.disconnect -- dangerous |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
122 |
64 | 123 local wrapclient, err |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
124 |
64 | 125 if sslctx then |
126 if not ssl_newcontext then | |
127 return nil, "luasec not found" | |
128 end | |
129 if type( sslctx ) ~= "table" then | |
130 out_error "server.lua: wrong server sslctx" | |
131 return nil, "wrong server sslctx" | |
132 end | |
133 sslctx, err = ssl_newcontext( sslctx ) | |
134 if not sslctx then | |
135 err = err or "wrong sslctx parameters" | |
136 out_error( "server.lua: ", err ) | |
137 return nil, err | |
138 end | |
139 wrapclient = wrapsslclient | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
140 wrapclient = wraptlsclient |
64 | 141 else |
142 wrapclient = wraptcpclient | |
143 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
144 |
64 | 145 local accept = socket.accept |
146 local close = socket.close | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
147 |
64 | 148 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
149 |
64 | 150 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
151 |
64 | 152 handler.shutdown = function( ) end |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
153 |
64 | 154 --[[handler.listener = function( data, err ) |
155 return ondata( handler, data, err ) | |
156 end]] | |
157 handler.ssl = function( ) | |
158 return sslctx and true or false | |
159 end | |
160 handler.close = function( closed ) | |
161 _ = not closed and close( socket ) | |
162 writelen = removesocket( writelist, socket, writelen ) | |
163 readlen = removesocket( readlist, socket, readlen ) | |
164 socketlist[ socket ] = nil | |
165 handler = nil | |
166 end | |
167 handler.ip = function( ) | |
168 return ip | |
169 end | |
170 handler.serverport = function( ) | |
171 return serverport | |
172 end | |
173 handler.socket = function( ) | |
174 return socket | |
175 end | |
176 handler.receivedata = function( ) | |
177 local client, err = accept( socket ) -- try to accept | |
178 if client then | |
179 local ip, clientport = client:getpeername( ) | |
180 client:settimeout( 0 ) | |
181 local handler, client, err = wrapclient( listener, client, ip, serverport, clientport, mode, sslctx ) -- wrap new client socket | |
182 if err then -- error while wrapping ssl socket | |
183 return false | |
184 end | |
185 out_put( "server.lua: accepted new client connection from ", ip, ":", clientport ) | |
186 return dispatch( handler ) | |
187 elseif err then -- maybe timeout or something else | |
188 out_put( "server.lua: error with new client connection: ", err ) | |
189 return false | |
190 end | |
191 end | |
192 return handler | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
193 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
194 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
195 wrapsslclient = function( listener, socket, ip, serverport, clientport, mode, sslctx ) -- this function wraps a ssl cleint |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
196 |
64 | 197 local dispatch, disconnect = listener.listener, listener.disconnect |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
198 |
64 | 199 --// transform socket to ssl object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
200 |
64 | 201 local err |
202 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket | |
203 if err then | |
204 out_put( "server.lua: ssl error: ", err ) | |
205 return nil, nil, err -- fatal error | |
206 end | |
207 socket:settimeout( 0 ) | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
208 |
64 | 209 --// private closures of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
210 |
64 | 211 local writequeue = { } -- buffer for messages to send |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
212 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
213 local eol, fatal_send_error -- end of buffer |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
214 |
64 | 215 local sstat, rstat = 0, 0 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
216 |
64 | 217 --// local import of socket methods //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
218 |
64 | 219 local send = socket.send |
220 local receive = socket.receive | |
221 local close = socket.close | |
222 --local shutdown = socket.shutdown | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
223 |
64 | 224 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
225 |
64 | 226 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
227 |
64 | 228 handler.getstats = function( ) |
229 return rstat, sstat | |
230 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
231 |
64 | 232 handler.listener = function( data, err ) |
233 return listener( handler, data, err ) | |
234 end | |
235 handler.ssl = function( ) | |
236 return true | |
237 end | |
238 handler.send = function( _, data, i, j ) | |
239 return send( socket, data, i, j ) | |
240 end | |
241 handler.receive = function( pattern, prefix ) | |
242 return receive( socket, pattern, prefix ) | |
243 end | |
244 handler.shutdown = function( pattern ) | |
245 --return shutdown( socket, pattern ) | |
246 end | |
247 handler.close = function( closed ) | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
248 if eol and not fatal_send_error then handler._dispatchdata(); end |
64 | 249 close( socket ) |
250 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen | |
251 readlen = removesocket( readlist, socket, readlen ) | |
252 socketlist[ socket ] = nil | |
253 out_put "server.lua: closed handler and removed socket from list" | |
254 end | |
255 handler.ip = function( ) | |
256 return ip | |
257 end | |
258 handler.serverport = function( ) | |
259 return serverport | |
260 end | |
261 handler.clientport = function( ) | |
262 return clientport | |
263 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
264 |
64 | 265 handler.write = function( data ) |
266 if not eol then | |
267 writelen = writelen + 1 | |
268 writelist[ writelen ] = socket | |
269 eol = 0 | |
270 end | |
271 eol = eol + 1 | |
272 writequeue[ eol ] = data | |
273 end | |
274 handler.writequeue = function( ) | |
275 return writequeue | |
276 end | |
277 handler.socket = function( ) | |
278 return socket | |
279 end | |
280 handler.mode = function( ) | |
281 return mode | |
282 end | |
283 handler._receivedata = function( ) | |
284 local data, err, part = receive( socket, mode ) -- receive data in "mode" | |
285 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | |
286 local data = data or part or "" | |
287 local count = #data * STAT_UNIT | |
288 rstat = rstat + count | |
289 receivestat = receivestat + count | |
290 out_put( "server.lua: read data '", data, "', error: ", err ) | |
291 return dispatch( handler, data, err ) | |
292 else -- connections was closed or fatal error | |
293 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
294 handler.close( ) | |
295 disconnect( handler, err ) | |
296 writequeue = nil | |
297 handler = nil | |
298 return false | |
299 end | |
300 end | |
301 handler._dispatchdata = function( ) -- this function writes data to handlers | |
302 local buffer = table_concat( writequeue, "", 1, eol ) | |
303 local succ, err, byte = send( socket, buffer ) | |
304 local count = ( succ or 0 ) * STAT_UNIT | |
305 sstat = sstat + count | |
306 sendstat = sendstat + count | |
307 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) | |
308 if succ then -- sending succesful | |
309 --writequeue = { } | |
310 eol = nil | |
311 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | |
312 return true | |
313 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | |
314 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | |
315 writequeue[ 1 ] = buffer -- insert new buffer in queue | |
316 eol = 1 | |
317 return true | |
318 else -- connection was closed during sending or fatal error | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
319 fatal_send_error = true; |
64 | 320 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
321 handler.close( ) | |
322 disconnect( handler, err ) | |
323 writequeue = nil | |
324 handler = nil | |
325 return false | |
326 end | |
327 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
328 |
64 | 329 -- // COMPAT // -- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
330 |
64 | 331 handler.getIp = handler.ip |
332 handler.getPort = handler.clientport | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
333 |
64 | 334 --// handshake //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
335 |
64 | 336 local wrote |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
337 |
64 | 338 handler.handshake = coroutine_wrap( function( client ) |
339 local err | |
340 for i = 1, 10 do -- 10 handshake attemps | |
341 _, err = client:dohandshake( ) | |
342 if not err then | |
343 out_put( "server.lua: ssl handshake done" ) | |
344 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
345 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
346 handler.dispatchdata = handler._dispatchdata | |
347 return dispatch( handler ) | |
348 else | |
349 out_put( "server.lua: error during ssl handshake: ", err ) | |
350 if err == "wantwrite" then | |
351 if wrote == nil then | |
352 writelen = writelen + 1 | |
353 writelist[ writelen ] = client | |
354 wrote = true | |
355 end | |
356 end | |
357 coroutine_yield( handler, nil, err ) -- handshake not finished | |
358 end | |
359 end | |
360 _ = err ~= "closed" and close( socket ) | |
361 handler.close( ) | |
362 disconnect( handler, err ) | |
363 writequeue = nil | |
364 handler = nil | |
365 return false -- handshake failed | |
366 end | |
367 ) | |
368 handler.receivedata = handler.handshake | |
369 handler.dispatchdata = handler.handshake | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
370 |
64 | 371 handler.handshake( socket ) -- do handshake |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
372 |
64 | 373 socketlist[ socket ] = handler |
374 readlen = readlen + 1 | |
375 readlist[ readlen ] = socket | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
376 |
64 | 377 return handler, socket |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
378 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
379 |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
380 wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, sslctx ) -- this function wraps a tls cleint |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
381 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
382 local dispatch, disconnect = listener.listener, listener.disconnect |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
383 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
384 --// transform socket to ssl object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
385 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
386 local err |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
387 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
388 socket:settimeout( 0 ) |
328
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
389 out_put("setting linger on "..tostring(socket)) |
a1d25769970f
Flush write queue before closing socket
Matthew Wild <mwild1@gmail.com>
parents:
319
diff
changeset
|
390 socket:setoption("linger", { on = true, timeout = 10 }); |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
391 --// private closures of the object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
392 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
393 local writequeue = { } -- buffer for messages to send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
394 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
395 local eol, fatal_send_error -- end of buffer |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
396 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
397 local sstat, rstat = 0, 0 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
398 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
399 --// local import of socket methods //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
400 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
401 local send = socket.send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
402 local receive = socket.receive |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
403 local close = socket.close |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
404 --local shutdown = socket.shutdown |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
405 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
406 --// public methods of the object //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
407 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
408 local handler = { } |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
409 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
410 handler.getstats = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
411 return rstat, sstat |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
412 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
413 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
414 handler.listener = function( data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
415 return listener( handler, data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
416 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
417 handler.ssl = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
418 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
419 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
420 handler.send = function( _, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
421 return send( socket, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
422 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
423 handler.receive = function( pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
424 return receive( socket, pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
425 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
426 handler.shutdown = function( pattern ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
427 --return shutdown( socket, pattern ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
428 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
429 handler.close = function( closed ) |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
430 if eol and not fatal_send_error then handler._dispatchdata(); end |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
431 close( socket ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
432 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
433 readlen = removesocket( readlist, socket, readlen ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
434 socketlist[ socket ] = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
435 out_put "server.lua: closed handler and removed socket from list" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
436 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
437 handler.ip = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
438 return ip |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
439 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
440 handler.serverport = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
441 return serverport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
442 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
443 handler.clientport = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
444 return clientport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
445 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
446 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
447 handler.write = function( data ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
448 if not eol then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
449 writelen = writelen + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
450 writelist[ writelen ] = socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
451 eol = 0 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
452 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
453 eol = eol + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
454 writequeue[ eol ] = data |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
455 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
456 handler.writequeue = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
457 return writequeue |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
458 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
459 handler.socket = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
460 return socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
461 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
462 handler.mode = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
463 return mode |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
464 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
465 handler._receivedata = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
466 local data, err, part = receive( socket, mode ) -- receive data in "mode" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
467 if not err or ( err == "timeout" or err == "wantread" ) then -- received something |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
468 local data = data or part or "" |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
469 local count = #data * STAT_UNIT |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
470 rstat = rstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
471 receivestat = receivestat + count |
66 | 472 --out_put( "server.lua: read data '", data, "', error: ", err ) |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
473 return dispatch( handler, data, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
474 else -- connections was closed or fatal error |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
475 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
476 handler.close( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
477 disconnect( handler, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
478 writequeue = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
479 handler = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
480 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
481 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
482 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
483 handler._dispatchdata = function( ) -- this function writes data to handlers |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
484 local buffer = table_concat( writequeue, "", 1, eol ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
485 local succ, err, byte = send( socket, buffer ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
486 local count = ( succ or 0 ) * STAT_UNIT |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
487 sstat = sstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
488 sendstat = sendstat + count |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
489 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
490 if succ then -- sending succesful |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
491 --writequeue = { } |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
492 eol = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
493 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
494 if handler.need_tls then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
495 out_put("server.lua: connection is ready for tls handshake"); |
66 | 496 handler.starttls(true); |
497 if handler.need_tls then | |
498 out_put("server.lua: uh-oh... we still want tls, something must be wrong"); | |
499 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
500 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
501 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
502 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
503 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
504 writequeue[ 1 ] = buffer -- insert new buffer in queue |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
505 eol = 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
506 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
507 else -- connection was closed during sending or fatal error |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
508 fatal_send_error = true; -- :( |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
509 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
510 handler.close( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
511 disconnect( handler, err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
512 writequeue = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
513 handler = nil |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
514 return false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
515 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
516 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
517 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
518 handler.receivedata, handler.dispatchdata = handler._receivedata, handler._dispatchdata; |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
519 -- // COMPAT // -- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
520 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
521 handler.getIp = handler.ip |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
522 handler.getPort = handler.clientport |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
523 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
524 --// handshake //-- |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
525 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
526 local wrote, read |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
527 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
528 handler.starttls = function (now) |
66 | 529 if not now then out_put("server.lua: we need to do tls, but delaying until later"); handler.need_tls = true; return; end |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
530 out_put( "server.lua: attempting to start tls on "..tostring(socket) ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
531 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
532 out_put("sslwrapped socket is "..tostring(socket)); |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
533 if err then |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
534 out_put( "server.lua: ssl error: ", err ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
535 return nil, nil, err -- fatal error |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
536 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
537 socket:settimeout( 1 ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
538 send = socket.send |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
539 receive = socket.receive |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
540 close = socket.close |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
541 handler.ssl = function( ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
542 return true |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
543 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
544 handler.send = function( _, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
545 return send( socket, data, i, j ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
546 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
547 handler.receive = function( pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
548 return receive( socket, pattern, prefix ) |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
549 end |
66 | 550 |
319
4a8a949c3870
Fix for putting TLS in stream:features for connections already using TLS. Thanks to albert for spotting.
Matthew Wild <mwild1@gmail.com>
parents:
127
diff
changeset
|
551 handler.starttls = nil; |
4a8a949c3870
Fix for putting TLS in stream:features for connections already using TLS. Thanks to albert for spotting.
Matthew Wild <mwild1@gmail.com>
parents:
127
diff
changeset
|
552 |
66 | 553 handler.handshake = coroutine_wrap( function( client ) |
554 local err | |
555 for i = 1, 10 do -- 10 handshake attemps | |
556 _, err = client:dohandshake( ) | |
557 if not err then | |
558 out_put( "server.lua: ssl handshake done" ) | |
559 writelen = ( wrote and removesocket( writelist, socket, writelen ) ) or writelen | |
560 handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions | |
561 handler.dispatchdata = handler._dispatchdata | |
562 handler.need_tls = nil | |
563 socketlist[ client ] = handler | |
564 readlen = readlen + 1 | |
565 readlist[ readlen ] = client | |
566 return true; | |
567 else | |
568 out_put( "server.lua: error during ssl handshake: ", err ) | |
569 if err == "wantwrite" then | |
570 if wrote == nil then | |
571 writelen = writelen + 1 | |
572 writelist[ writelen ] = client | |
573 wrote = true | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
574 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
575 end |
66 | 576 coroutine_yield( handler, nil, err ) -- handshake not finished |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
577 end |
66 | 578 end |
579 _ = err ~= "closed" and close( socket ) | |
580 handler.close( ) | |
581 disconnect( handler, err ) | |
582 writequeue = nil | |
583 handler = nil | |
584 return false -- handshake failed | |
585 end | |
586 ) | |
587 handler.receivedata = handler.handshake | |
588 handler.dispatchdata = handler.handshake | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
589 |
66 | 590 handler.handshake( socket ) -- do handshake |
591 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
592 socketlist[ socket ] = handler |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
593 readlen = readlen + 1 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
594 readlist[ readlen ] = socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
595 |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
596 return handler, socket |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
597 end |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
598 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
599 wraptcpclient = function( listener, socket, ip, serverport, clientport, mode ) -- this function wraps a socket |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
600 |
64 | 601 local dispatch, disconnect = listener.listener, listener.disconnect |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
602 |
64 | 603 --// private closures of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
604 |
64 | 605 local writequeue = { } -- list for messages to send |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
606 |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
607 local eol, fatal_send_error |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
608 |
64 | 609 local rstat, sstat = 0, 0 |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
610 |
64 | 611 --// local import of socket methods //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
612 |
64 | 613 local send = socket.send |
614 local receive = socket.receive | |
615 local close = socket.close | |
616 local shutdown = socket.shutdown | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
617 |
64 | 618 --// public methods of the object //-- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
619 |
64 | 620 local handler = { } |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
621 |
64 | 622 handler.getstats = function( ) |
623 return rstat, sstat | |
624 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
625 |
64 | 626 handler.listener = function( data, err ) |
627 return listener( handler, data, err ) | |
628 end | |
629 handler.ssl = function( ) | |
630 return false | |
631 end | |
632 handler.send = function( _, data, i, j ) | |
633 return send( socket, data, i, j ) | |
634 end | |
635 handler.receive = function( pattern, prefix ) | |
636 return receive( socket, pattern, prefix ) | |
637 end | |
638 handler.shutdown = function( pattern ) | |
639 return shutdown( socket, pattern ) | |
640 end | |
641 handler.close = function( closed ) | |
374
ff502dada372
All the people who don't use TLS, this one's for you.
Matthew Wild <mwild1@gmail.com>
parents:
335
diff
changeset
|
642 if eol and not fatal_send_error then handler.dispatchdata(); end |
64 | 643 _ = not closed and shutdown( socket ) |
644 _ = not closed and close( socket ) | |
645 writelen = ( eol and removesocket( writelist, socket, writelen ) ) or writelen | |
646 readlen = removesocket( readlist, socket, readlen ) | |
647 socketlist[ socket ] = nil | |
648 out_put "server.lua: closed handler and removed socket from list" | |
649 end | |
650 handler.ip = function( ) | |
651 return ip | |
652 end | |
653 handler.serverport = function( ) | |
654 return serverport | |
655 end | |
656 handler.clientport = function( ) | |
657 return clientport | |
658 end | |
659 handler.write = function( data ) | |
660 if not eol then | |
661 writelen = writelen + 1 | |
662 writelist[ writelen ] = socket | |
663 eol = 0 | |
664 end | |
665 eol = eol + 1 | |
666 writequeue[ eol ] = data | |
667 end | |
668 handler.writequeue = function( ) | |
669 return writequeue | |
670 end | |
671 handler.socket = function( ) | |
672 return socket | |
673 end | |
674 handler.mode = function( ) | |
675 return mode | |
676 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
677 |
64 | 678 handler.receivedata = function( ) |
679 local data, err, part = receive( socket, mode ) -- receive data in "mode" | |
680 if not err or ( err == "timeout" or err == "wantread" ) then -- received something | |
681 local data = data or part or "" | |
682 local count = #data * STAT_UNIT | |
683 rstat = rstat + count | |
684 receivestat = receivestat + count | |
685 out_put( "server.lua: read data '", data, "', error: ", err ) | |
686 return dispatch( handler, data, err ) | |
687 else -- connections was closed or fatal error | |
688 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) | |
689 handler.close( ) | |
690 disconnect( handler, err ) | |
691 writequeue = nil | |
692 handler = nil | |
693 return false | |
694 end | |
695 end | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
696 |
64 | 697 handler.dispatchdata = function( ) -- this function writes data to handlers |
698 local buffer = table_concat( writequeue, "", 1, eol ) | |
699 local succ, err, byte = send( socket, buffer ) | |
700 local count = ( succ or 0 ) * STAT_UNIT | |
701 sstat = sstat + count | |
702 sendstat = sendstat + count | |
703 out_put( "server.lua: sended '", buffer, "', bytes: ", succ, ", error: ", err, ", part: ", byte, ", to: ", ip, ":", clientport ) | |
704 if succ then -- sending succesful | |
705 --writequeue = { } | |
706 eol = nil | |
707 writelen = removesocket( writelist, socket, writelen ) -- delete socket from writelist | |
708 return true | |
709 elseif byte and ( err == "timeout" or err == "wantwrite" ) then -- want write | |
710 buffer = string_sub( buffer, byte + 1, -1 ) -- new buffer | |
711 writequeue[ 1 ] = buffer -- insert new buffer in queue | |
712 eol = 1 | |
713 return true | |
714 else -- connection was closed during sending or fatal error | |
335
906311ff2170
Fix to prevent send retry when connection is already closed
Matthew Wild <mwild1@gmail.com>
parents:
328
diff
changeset
|
715 fatal_send_error = true; -- :'-( |
64 | 716 out_put( "server.lua: client ", ip, ":", clientport, " error: ", err ) |
717 handler.close( ) | |
718 disconnect( handler, err ) | |
719 writequeue = nil | |
720 handler = nil | |
721 return false | |
722 end | |
723 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
724 |
64 | 725 -- // COMPAT // -- |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
726 |
64 | 727 handler.getIp = handler.ip |
728 handler.getPort = handler.clientport | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
729 |
64 | 730 socketlist[ socket ] = handler |
731 readlen = readlen + 1 | |
732 readlist[ readlen ] = socket | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
733 |
64 | 734 return handler, socket |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
735 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
736 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
737 addtimer = function( listener ) |
64 | 738 timelistener[ #timelistener + 1 ] = listener |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
739 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
740 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
741 firetimer = function( listener ) |
64 | 742 for i, listener in ipairs( timelistener ) do |
743 listener( ) | |
744 end | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
745 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
746 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
747 addserver = function( listeners, port, addr, mode, sslctx ) -- this function provides a way for other scripts to reg a server |
64 | 748 local err |
749 if type( listeners ) ~= "table" then | |
750 err = "invalid listener table" | |
751 else | |
752 for name, func in pairs( listeners ) do | |
753 if type( func ) ~= "function" then | |
98
3a2d327c4856
server.lua should degrade gracefully when LuaSec not present
Matthew Wild <mwild1@gmail.com>
parents:
74
diff
changeset
|
754 --err = "invalid listener function" |
64 | 755 break |
756 end | |
757 end | |
758 end | |
759 if not type( port ) == "number" or not ( port >= 0 and port <= 65535 ) then | |
760 err = "invalid port" | |
761 elseif listener[ port ] then | |
762 err= "listeners on port '" .. port .. "' already exist" | |
763 elseif sslctx and not luasec then | |
764 err = "luasec not found" | |
765 end | |
766 if err then | |
767 out_error( "server.lua: ", err ) | |
768 return nil, err | |
769 end | |
770 addr = addr or "*" | |
771 local server, err = socket_bind( addr, port ) | |
772 if err then | |
773 out_error( "server.lua: ", err ) | |
774 return nil, err | |
775 end | |
776 local handler, err = wrapserver( listeners, server, addr, port, mode, sslctx ) -- wrap new server socket | |
777 if not handler then | |
778 server:close( ) | |
779 return nil, err | |
780 end | |
781 server:settimeout( 0 ) | |
782 readlen = readlen + 1 | |
783 readlist[ readlen ] = server | |
784 listener[ port ] = listeners | |
785 socketlist[ server ] = handler | |
786 out_put( "server.lua: new server listener on ", addr, ":", port ) | |
787 return true | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
788 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
789 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
790 removesocket = function( tbl, socket, len ) -- this function removes sockets from a list |
64 | 791 for i, target in ipairs( tbl ) do |
792 if target == socket then | |
793 len = len - 1 | |
794 table_remove( tbl, i ) | |
795 return len | |
796 end | |
797 end | |
798 return len | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
799 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
800 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
801 closeall = function( ) |
64 | 802 for sock, handler in pairs( socketlist ) do |
803 handler.shutdown( ) | |
804 handler.close( ) | |
805 socketlist[ sock ] = nil | |
806 end | |
807 writelist, readlist, socketlist = { }, { }, { } | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
808 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
809 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
810 closesocket = function( socket ) |
64 | 811 writelen = removesocket( writelist, socket, writelen ) |
812 readlen = removesocket( readlist, socket, readlen ) | |
813 socketlist[ socket ] = nil | |
814 socket:close( ) | |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
815 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
816 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
817 loop = function( ) -- this is the main loop of the program |
64 | 818 --signal_set( "hub", "run" ) |
819 repeat | |
66 | 820 --[[print(readlen, writelen) |
821 for _, s in ipairs(readlist) do print("R:", tostring(s)) end | |
822 for _, s in ipairs(writelist) do print("W:", tostring(s)) end | |
823 out_put("select()"..os.time())]] | |
64 | 824 local read, write, err = socket_select( readlist, writelist, 1 ) -- 1 sec timeout, nice for timers |
825 for i, socket in ipairs( write ) do -- send data waiting in writequeues | |
826 local handler = socketlist[ socket ] | |
827 if handler then | |
828 handler.dispatchdata( ) | |
829 else | |
830 closesocket( socket ) | |
831 out_put "server.lua: found no handler and closed socket (writelist)" -- this should not happen | |
832 end | |
833 end | |
834 for i, socket in ipairs( read ) do -- receive data | |
835 local handler = socketlist[ socket ] | |
836 if handler then | |
837 handler.receivedata( ) | |
838 else | |
839 closesocket( socket ) | |
840 out_put "server.lua: found no handler and closed socket (readlist)" -- this can happen | |
841 end | |
842 end | |
843 firetimer( ) | |
65
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
844 until false |
9c471840acb9
TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
845 return |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
846 end |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
847 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
848 ----------------------------------// BEGIN //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
849 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
850 ----------------------------------// PUBLIC INTERFACE //-- |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
851 |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
852 return { |
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
853 |
64 | 854 add = addserver, |
855 loop = loop, | |
856 stats = stats, | |
857 closeall = closeall, | |
858 addtimer = addtimer, | |
127 | 859 wraptlsclient = wraptlsclient, |
1
b8787e859fd2
Switched to new connection framework, courtesy of the luadch project
matthew
parents:
diff
changeset
|
860 } |