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