Comparison

net/server.lua @ 1979:52ddaa2e6ab8

net.server: Allow changing the sslctx of a connection after it has been established
author Matthew Wild <mwild1@gmail.com>
date Sat, 17 Oct 2009 16:21:27 +0100
parent 1953:64538c620450
child 2057:ee2929e620bb
comparison
equal deleted inserted replaced
1978:8f9dc8a25660 1979:52ddaa2e6ab8
529 _ = handler and handler.close( ) 529 _ = handler and handler.close( )
530 return false 530 return false
531 end 531 end
532 end 532 end
533 533
534 if sslctx then -- ssl? 534 -- Set the sslctx
535 local handshake;
536 function handler.set_sslctx(new_sslctx)
535 ssl = true 537 ssl = true
538 sslctx = new_sslctx;
536 local wrote 539 local wrote
537 local read 540 local read
538 local handshake = coroutine_wrap( function( client ) -- create handshake coroutine 541 handshake = coroutine_wrap( function( client ) -- create handshake coroutine
539 local err 542 local err
540 for i = 1, _maxsslhandshake do 543 for i = 1, _maxsslhandshake do
541 _sendlistlen = ( wrote and removesocket( _sendlist, socket, _sendlistlen ) ) or _sendlistlen 544 _sendlistlen = ( wrote and removesocket( _sendlist, socket, _sendlistlen ) ) or _sendlistlen
542 _readlistlen = ( read and removesocket( _readlist, socket, _readlistlen ) ) or _readlistlen 545 _readlistlen = ( read and removesocket( _readlist, socket, _readlistlen ) ) or _readlistlen
543 read, wrote = nil, nil 546 read, wrote = nil, nil
547 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions 550 handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions
548 handler.sendbuffer = _sendbuffer 551 handler.sendbuffer = _sendbuffer
549 _ = status and status( handler, "ssl-handshake-complete" ) 552 _ = status and status( handler, "ssl-handshake-complete" )
550 return true 553 return true
551 else 554 else
552 out_put( "server.lua: error during ssl handshake: ", tostring(err) ) 555 out_put( "server.lua: error during ssl handshake: ", tostring(err) )
553 if err == "wantwrite" and not wrote then 556 if err == "wantwrite" and not wrote then
554 _sendlistlen = _sendlistlen + 1 557 _sendlistlen = _sendlistlen + 1
555 _sendlist[ _sendlistlen ] = client 558 _sendlist[ _sendlistlen ] = client
556 wrote = true 559 wrote = true
557 elseif err == "wantread" and not read then 560 elseif err == "wantread" and not read then
558 _readlistlen = _readlistlen + 1 561 _readlistlen = _readlistlen + 1
559 _readlist [ _readlistlen ] = client 562 _readlist [ _readlistlen ] = client
560 read = true 563 read = true
561 else 564 else
562 break; 565 break;
563 end 566 end
564 --coroutine_yield( handler, nil, err ) -- handshake not finished 567 --coroutine_yield( handler, nil, err ) -- handshake not finished
565 coroutine_yield( ) 568 coroutine_yield( )
566 end 569 end
567 end 570 end
568 disconnect( handler, "ssl handshake failed" ) 571 disconnect( handler, "ssl handshake failed" )
569 _ = handler and handler.close( true ) -- forced disconnect 572 _ = handler and handler.close( true ) -- forced disconnect
570 return false -- handshake failed 573 return false -- handshake failed
571 end 574 end
572 ) 575 )
576 end
577 if sslctx then -- ssl?
578 handler.set_sslctx(sslctx);
573 if startssl then -- ssl now? 579 if startssl then -- ssl now?
574 --out_put("server.lua: ", "starting ssl handshake") 580 --out_put("server.lua: ", "starting ssl handshake")
575 local err 581 local err
576 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket 582 socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
577 if err then 583 if err then