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): ...@@ -309,12 +309,13 @@ class ListeningConnection(BaseConnection):
def readable(self): def readable(self):
connector, addr = self.connector.accept() connector, addr = self.connector.accept()
logging.debug('accepted a connection from %s:%d', *addr) logging.debug('accepted a connection from %s:%d', *addr)
handler = self.getHandler() conn = ServerConnection(self.em, self.getHandler(), connector, addr)
new_conn = ServerConnection(self.em, handler, connector, addr)
if self._ssl: if self._ssl:
connector.ssl(self._ssl) conn.connecting = True
self.em.addWriter(new_conn) connector.ssl(self._ssl, conn._connected)
handler.connectionAccepted(new_conn) self.em.addWriter(conn)
else:
conn._connected()
def getAddress(self): def getAddress(self):
return self.connector.getAddress() return self.connector.getAddress()
...@@ -328,7 +329,7 @@ class Connection(BaseConnection): ...@@ -328,7 +329,7 @@ class Connection(BaseConnection):
# XXX: rename isPending, hasPendingMessages & pending methods # XXX: rename isPending, hasPendingMessages & pending methods
connecting = False connecting = True
client = False client = False
server = False server = False
peer_id = None peer_id = None
...@@ -576,11 +577,14 @@ class Connection(BaseConnection): ...@@ -576,11 +577,14 @@ class Connection(BaseConnection):
def idle(self): def idle(self):
self.ask(Packets.Ping()) self.ask(Packets.Ping())
def _connected(self):
self.connecting = False
self.getHandler().connectionCompleted(self)
class ClientConnection(Connection): class ClientConnection(Connection):
"""A connection from this node to a remote node.""" """A connection from this node to a remote node."""
connecting = True
client = True client = True
def __init__(self, app, handler, node): def __init__(self, app, handler, node):
...@@ -605,7 +609,7 @@ class ClientConnection(Connection): ...@@ -605,7 +609,7 @@ class ClientConnection(Connection):
else: else:
self.em.register(self) self.em.register(self)
if connected: if connected:
self._connectionCompleted() self._maybeConnected()
# A client connection usually has a pending packet to send # A client connection usually has a pending packet to send
# from the beginning. It would be too smart to detect when # from the beginning. It would be too smart to detect when
# it's not required to poll for writing. # it's not required to poll for writing.
...@@ -620,16 +624,16 @@ class ClientConnection(Connection): ...@@ -620,16 +624,16 @@ class ClientConnection(Connection):
if self.connector.getError(): if self.connector.getError():
self._closure() self._closure()
else: else:
self._connectionCompleted() self._maybeConnected()
self.writable() self.writable()
def _connectionCompleted(self): def _maybeConnected(self):
if self._ssl:
self.connector.ssl(self._ssl)
self.writable = self.lockWrapper(super(ClientConnection, self).writable) self.writable = self.lockWrapper(super(ClientConnection, self).writable)
self.connecting = False
self.updateTimeout(time()) self.updateTimeout(time())
self.getHandler().connectionCompleted(self) if self._ssl:
self.connector.ssl(self._ssl, self._connected)
else:
self._connected()
class ServerConnection(Connection): class ServerConnection(Connection):
......
...@@ -110,12 +110,13 @@ class SocketConnector(object): ...@@ -110,12 +110,13 @@ class SocketConnector(object):
self.socket.close() self.socket.close()
self._error('listen', e) self._error('listen', e)
def ssl(self, ssl): def ssl(self, ssl, on_handshake_done=None):
self.socket = ssl.wrap_socket(self.socket, self.socket = ssl.wrap_socket(self.socket,
server_side=self.is_server, server_side=self.is_server,
do_handshake_on_connect=False, do_handshake_on_connect=False,
suppress_ragged_eofs=False) suppress_ragged_eofs=False)
self.__class__ = self.SSLHandshakeConnectorClass self.__class__ = self.SSLHandshakeConnectorClass
self.on_handshake_done = on_handshake_done
self.queued or self.queued.append('') self.queued or self.queued.append('')
def getError(self): def getError(self):
...@@ -269,6 +270,9 @@ class _SSLHandshake(_SSL): ...@@ -269,6 +270,9 @@ class _SSLHandshake(_SSL):
self.__class__ = self.SSLConnectorClass self.__class__ = self.SSLConnectorClass
cipher, proto, bits = self.socket.cipher() cipher, proto, bits = self.socket.cipher()
logging.debug("SSL handshake done for %s: %s %s", self, cipher, bits) 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: if read_buf is None:
return self.send() return self.send()
self.receive(read_buf) self.receive(read_buf)
......
...@@ -129,9 +129,6 @@ class EventHandler(object): ...@@ -129,9 +129,6 @@ class EventHandler(object):
"""Called when a connection failed.""" """Called when a connection failed."""
logging.debug('connection failed for %r', conn) logging.debug('connection failed for %r', conn)
def connectionAccepted(self, conn):
"""Called when a connection is accepted."""
def connectionClosed(self, conn): def connectionClosed(self, conn):
"""Called when a connection is closed by the peer.""" """Called when a connection is closed by the peer."""
logging.debug('connection closed for %r', conn) logging.debug('connection closed for %r', conn)
...@@ -251,7 +248,6 @@ class AnswerBaseHandler(EventHandler): ...@@ -251,7 +248,6 @@ class AnswerBaseHandler(EventHandler):
connectionStarted = unexpectedInAnswerHandler connectionStarted = unexpectedInAnswerHandler
connectionCompleted = unexpectedInAnswerHandler connectionCompleted = unexpectedInAnswerHandler
connectionFailed = unexpectedInAnswerHandler connectionFailed = unexpectedInAnswerHandler
connectionAccepted = unexpectedInAnswerHandler
timeoutExpired = unexpectedInAnswerHandler timeoutExpired = unexpectedInAnswerHandler
connectionClosed = unexpectedInAnswerHandler connectionClosed = unexpectedInAnswerHandler
packetReceived = 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