Coverage for nuts.channels : 63%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
""" Generic, transport-agnostic authenticated channel. Needs to be overridden by class implementing `receive` and `listen`. """ #: If the underlying transport layer has a MTU, child classes should set #: this to the same MTU
#: MACs supported by this satellite/server. Used in the SA negotiation, #: should be ordered by preference (strength). 'sha3_512', 'sha3_384', 'sha3_256', 'hmac-sha1', 'hmac-sha256', ]
""" Create a new auth channel context to keep around. """
def shared_key(self):
def shared_key(self, value):
while not self._messages: _logger.info('Listening...') data, sender = self.read_data() message = AuthenticatedMessage(sender, data) self.handle_message(message) return self._messages.pop(0)
def connect(self, address): session = ClientSession(address, self) session.connect()
# Session setup complete, let client use the session yield session
# Send terminate _logger.info('Terminating session with %s...', session.id_a) session.terminate()
# Release any resources held by the channel self.tear_down()
""" Release any resources used by the channel, if any. """ pass
""" Externally exposed interface for sending data. """ session = self.sessions[address] session.send(data)
""" Handle incoming message on the channel. """ else:
#: The maximum size of packets sent and received on this channel
super(UDPAuthChannel, self).__init__(*args, **kwargs) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) timeout = kwargs.get('timeout', 2.0) self.sock.settimeout(timeout)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.sock.bind(address) _logger.info('Bound to %s:%s' % address) except Exception as e: _logger.exception('Exception occured while binding to socket, address %s.', address) raise
_logger.debug('Sending %s to %s' % (ascii_bin(data), address)) self.sock.sendto(data, address)
data, sender = self.sock.recvfrom(self.mtu) _logger.debug('Received data: %s from %s' % (ascii_bin(data), sender)) return data, sender
self.sock.close()
""" Only return stuff locally, probably only useful for testing. """
""" Return pre-filled replies, or None. """ if self.messages_to_receive: return self.messages_to_receive.pop(0) |