Commit aaefaf8b authored by Julien Muchembled's avatar Julien Muchembled

ssl: consider connections completed after the handshake

- Server connections can now be in 'connecting' state.
- connectionAccepted event (which has never been used so far) is merged into
  connectionCompleted.
parent 6b1f198f
......@@ -309,12 +309,13 @@ class ListeningConnection(BaseConnection):
def readable(self):
connector, addr = self.connector.accept()
logging.debug('accepted a connection from %s:%d', *addr)
handler = self.getHandler()
new_conn = ServerConnection(self.em, handler, connector, addr)
conn = ServerConnection(self.em, self.getHandler(), connector, addr)
if self._ssl:
connector.ssl(self._ssl)
self.em.addWriter(new_conn)
handler.connectionAccepted(new_conn)
conn.connecting = True
connector.ssl(self._ssl, conn._connected)
self.em.addWriter(conn)
else:
conn._connected()
def getAddress(self):
return self.connector.getAddress()
......@@ -328,7 +329,7 @@ class Connection(BaseConnection):
# XXX: rename isPending, hasPendingMessages & pending methods
connecting = False
connecting = True
client = False
server = False
peer_id = None
......@@ -576,11 +577,14 @@ class Connection(BaseConnection):
def idle(self):
self.ask(Packets.Ping())
def _connected(self):
self.connecting = False
self.getHandler().connectionCompleted(self)
class ClientConnection(Connection):
"""A connection from this node to a remote node."""
connecting = True
client = True
def __init__(self, app, handler, node):
......@@ -605,7 +609,7 @@ class ClientConnection(Connection):
else:
self.em.register(self)
if connected:
self._connectionCompleted()
self._maybeConnected()
# A client connection usually has a pending packet to send
# from the beginning. It would be too smart to detect when
# it's not required to poll for writing.
......@@ -620,16 +624,16 @@ class ClientConnection(Connection):
if self.connector.getError():
self._closure()
else:
self._connectionCompleted()
self._maybeConnected()
self.writable()
def _connectionCompleted(self):
if self._ssl:
self.connector.ssl(self._ssl)
def _maybeConnected(self):
self.writable = self.lockWrapper(super(ClientConnection, self).writable)
self.connecting = False
self.updateTimeout(time())
self.getHandler().connectionCompleted(self)
if self._ssl:
self.connector.ssl(self._ssl, self._connected)
else:
self._connected()
class ServerConnection(Connection):
......
......@@ -110,12 +110,13 @@ class SocketConnector(object):
self.socket.close()
self._error('listen', e)
def ssl(self, ssl):
def ssl(self, ssl, on_handshake_done=None):
self.socket = ssl.wrap_socket(self.socket,
server_side=self.is_server,
do_handshake_on_connect=False,
suppress_ragged_eofs=False)
self.__class__ = self.SSLHandshakeConnectorClass
self.on_handshake_done = on_handshake_done
self.queued or self.queued.append('')
def getError(self):
......@@ -269,6 +270,9 @@ class _SSLHandshake(_SSL):
self.__class__ = self.SSLConnectorClass
cipher, proto, bits = self.socket.cipher()
logging.debug("SSL handshake done for %s: %s %s", self, cipher, bits)
if self.on_handshake_done:
self.on_handshake_done()
del self.on_handshake_done
if read_buf is None:
return self.send()
self.receive(read_buf)
......
......@@ -129,9 +129,6 @@ class EventHandler(object):
"""Called when a connection failed."""
logging.debug('connection failed for %r', conn)
def connectionAccepted(self, conn):
"""Called when a connection is accepted."""
def connectionClosed(self, conn):
"""Called when a connection is closed by the peer."""
logging.debug('connection closed for %r', conn)
......@@ -251,7 +248,6 @@ class AnswerBaseHandler(EventHandler):
connectionStarted = unexpectedInAnswerHandler
connectionCompleted = unexpectedInAnswerHandler
connectionFailed = unexpectedInAnswerHandler
connectionAccepted = unexpectedInAnswerHandler
timeoutExpired = unexpectedInAnswerHandler
connectionClosed = unexpectedInAnswerHandler
packetReceived = unexpectedInAnswerHandler
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment