Initial commit: up to and including data exchange functionality
This initial commit for the PyDTLS package includes the following functionality:
* DTLS cookie exchange, using secure hmac cookies
* A platform-independent routing UDP demultiplexer
* SSL handshaking over UDP using the DTLS protocol
* Datagram exchange using the DTLS protocol
* SSL shutdown over UDP
The package is structured as follows:
* dtls: top-level package
* dtls.demux: demultiplexer package; automatically loads a
demultiplexer appropriate for the currently executing
platform
* dtls.demux.router: a routing demux for platforms whose network stacks
cannot assign incoming UDP packets to sockets based
on the sockets' connection information
* dtls.demux.osnet: a demux that uses the operating system's UDP packet
routing functionality
* dtls.err: package-wide error handling and error definitions
* dtls.sslconnection: a client and server-side connection class for
UDP network connections secured with the DTLS protocol
* dtls.openssl: a ctypes-based wrapper for the OpenSSL library
* dtls.test: test scripts, utilities, and unit tests
The following binaries are provided:
* libeay32.dll: cryptographic portion of the OpenSSL library
* ssleay32.dll: protocol portion of the OpenSSL library (depends on former)
* cygcrypto-1.0.0.dll: as libeay32.dll, but with debugging symbols
* cygssl-1.0.0.dll: as ssleay32.dll, but with debugging symbols
All binaries have been built with the MinGW tool chain, targeted for msvcr90.
The unstripped dll's can be debugged on Windows with gdb. Cygwin is not used.
This commit is contained in:
1 parent
91323b475b
commit
9bb24c5d29
17 files changed
+1430
No files matched your search
@@ -0,0 +1,26 @@
|
||||
# Demux loader: imports a demux module appropriate for this platform.
|
||||
# Written by Ray Brown.
|
||||
"""UDP Demux
|
||||
|
||||
A UDP demux is a wrapper for a datagram socket. The demux must be initialized
|
||||
with an unconnected datagram socket, referred to as the root socket. Once
|
||||
initialized, the demux will create new connections to peer endpoints upon
|
||||
arrival of datagrams from a new endpoint. Such a connection is of a
|
||||
socket-derived type, and will receive datagrams only from the peer endpoint for
|
||||
which it was created, and that are sent to the root socket.
|
||||
|
||||
Connections must be used for receiving datagrams only. Outgoing traffic should
|
||||
be sent through the root socket.
|
||||
|
||||
Varying implementations of this functionality are provided for different
|
||||
platforms.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
|
||||
from router import UDPDemux
|
||||
else:
|
||||
from osnet import UDPDemux
|
||||
|
||||
__all__ = ["UDPDemux"]
|
||||
@@ -0,0 +1,62 @@
|
||||
# OSNet demux: uses the OS network stack to demultiplex incoming datagrams
|
||||
# among sockets bound to the same ports. Written by Ray Brown.
|
||||
"""OS Network UDP Demux
|
||||
|
||||
This module implements a demux that uses the OS network stack to demultiplex
|
||||
datagrams coming from different peers among datagram sockets that are all bound
|
||||
to the port at which these datagrams are being received. The network stack is
|
||||
instructed as to which socket an incoming datagram should be sent to by
|
||||
connecting the destination socket to the peer endpoint.
|
||||
|
||||
The OSNet demux requires operating system functionality that exists in the
|
||||
Linux kernel, but not in the Windows network stack.
|
||||
|
||||
Classes:
|
||||
|
||||
UDPDemux -- a network stack configuring UDP demux
|
||||
|
||||
Exceptions:
|
||||
|
||||
KeyError -- raised for unknown peer addresses
|
||||
"""
|
||||
|
||||
|
||||
class UDPDemux(object):
|
||||
"""OS network stack configuring demux
|
||||
|
||||
This class implements a demux that creates sockets connected to peer
|
||||
network endpoints, configuring the network stack to demultiplex
|
||||
incoming datagrams from these endpoints among these sockets.
|
||||
|
||||
Methods:
|
||||
|
||||
get_connection -- create a new connection or retrieve an existing one
|
||||
remove_connection -- remove an existing connection
|
||||
service -- this method does nothing for this type of demux
|
||||
"""
|
||||
|
||||
def get_connection(self, address):
|
||||
"""Create or retrieve a muxed connection
|
||||
|
||||
Arguments:
|
||||
address -- a peer endpoint in IPv4/v6 address format; None refers
|
||||
to the connection for unknown peers
|
||||
|
||||
Return:
|
||||
a bound, connected datagram socket instance, or the root socket
|
||||
in case address was None
|
||||
"""
|
||||
|
||||
def remove_connection(self, address):
|
||||
"""Remove a muxed connection
|
||||
|
||||
Arguments:
|
||||
address -- an address for which a muxed connection was previously
|
||||
retrieved through get_connection, which has not yet
|
||||
been removed
|
||||
|
||||
Return:
|
||||
the socket object whose connection has been removed
|
||||
"""
|
||||
|
||||
return self.connections.pop(address)
|
||||
@@ -0,0 +1,172 @@
|
||||
# Routing demux: forwards datagrams from the root socket to connected
|
||||
# sockets bound to ephemeral ports. Written by Ray Brown.
|
||||
"""Routing UDP Demux
|
||||
|
||||
This module implements an explicitly routing UDP demux. New connections create
|
||||
datagram sockets bound to ephemeral ports on the loopback interface and
|
||||
connected to a forwarding socket. The demux services incoming datagrams by
|
||||
receiving them from the root socket and sending them to the socket belonging to
|
||||
the connection that is associated with the sending peer.
|
||||
|
||||
A routing UDP demux can be used on any platform.
|
||||
|
||||
Classes:
|
||||
|
||||
UDPDemux -- an explicitly routing UDP demux
|
||||
|
||||
Exceptions:
|
||||
|
||||
InvalidSocketError -- exception raised for improper socket objects
|
||||
KeyError -- raised for unknown peer addresses
|
||||
"""
|
||||
|
||||
import socket
|
||||
from logging import getLogger
|
||||
from weakref import WeakValueDictionary
|
||||
from ..err import InvalidSocketError
|
||||
|
||||
_logger = getLogger(__name__)
|
||||
|
||||
UDP_MAX_DGRAM_LENGTH = 65527
|
||||
|
||||
|
||||
class UDPDemux(object):
|
||||
"""Explicitly routing UDP demux
|
||||
|
||||
This class implements a demux that forwards packets from the root
|
||||
socket to sockets belonging to connections. It does this whenever its
|
||||
service method is invoked.
|
||||
|
||||
Methods:
|
||||
|
||||
remove_connection -- remove an existing connection
|
||||
service -- distribute datagrams from the root socket to connections
|
||||
forward -- forward a stored datagram to a connection
|
||||
"""
|
||||
|
||||
_forwarding_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
_forwarding_socket.bind(('127.0.0.1', 0))
|
||||
|
||||
def __init__(self, datagram_socket):
|
||||
"""Constructor
|
||||
|
||||
Arguments:
|
||||
datagram_socket -- the root socket; this must be a bound, unconnected
|
||||
datagram socket
|
||||
"""
|
||||
|
||||
if datagram_socket.type != socket.SOCK_DGRAM:
|
||||
raise InvalidSocketError("datagram_socket is not of " +
|
||||
"type SOCK_DGRAM")
|
||||
try:
|
||||
datagram_socket.getsockname()
|
||||
except:
|
||||
raise InvalidSocketError("datagram_socket is unbound")
|
||||
try:
|
||||
datagram_socket.getpeername()
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
raise InvalidSocketError("datagram_socket is connected")
|
||||
|
||||
self.datagram_socket = datagram_socket
|
||||
self.payload = ""
|
||||
self.payload_peer_address = None
|
||||
self.connections = WeakValueDictionary()
|
||||
|
||||
def get_connection(self, address):
|
||||
"""Create or retrieve a muxed connection
|
||||
|
||||
Arguments:
|
||||
address -- a peer endpoint in IPv4/v6 address format; None refers
|
||||
to the connection for unknown peers
|
||||
|
||||
Return:
|
||||
a bound, connected datagram socket instance
|
||||
"""
|
||||
|
||||
if self.connections.has_key(address):
|
||||
return self.connections[address]
|
||||
|
||||
# We need a new datagram socket on a dynamically assigned ephemeral port
|
||||
conn = socket.socket(self._forwarding_socket.family,
|
||||
self._forwarding_socket.type,
|
||||
self._forwarding_socket.proto)
|
||||
conn.bind((self._forwarding_socket.getsockname()[0], 0))
|
||||
conn.connect(self._forwarding_socket.getsockname())
|
||||
if not address:
|
||||
conn.setblocking(0)
|
||||
self.connections[address] = conn
|
||||
_logger.debug("Created new connection for address: %s", address)
|
||||
return conn
|
||||
|
||||
def remove_connection(self, address):
|
||||
"""Remove a muxed connection
|
||||
|
||||
Arguments:
|
||||
address -- an address that was previously returned by the service
|
||||
method and whose connection has not yet been removed
|
||||
|
||||
Return:
|
||||
the socket object whose connection has been removed
|
||||
"""
|
||||
|
||||
return self.connections.pop(address)
|
||||
|
||||
def service(self):
|
||||
"""Service the root socket
|
||||
|
||||
Read from the root socket and forward one datagram to a
|
||||
connection. The call will return without forwarding data
|
||||
if any of the following occurs:
|
||||
|
||||
* An error is encountered while reading from the root socket
|
||||
* Reading from the root socket times out
|
||||
* The root socket is non-blocking and has no data available
|
||||
* An empty payload is received
|
||||
* A non-empty payload is received from an unknown peer (a peer
|
||||
for which get_connection has not yet been called); in this case,
|
||||
the payload is held by this instance and will be forwarded when
|
||||
the forward method is called
|
||||
|
||||
Return:
|
||||
if the datagram received was from a new peer, then the peer's
|
||||
address; otherwise None
|
||||
"""
|
||||
|
||||
self.payload, self.payload_peer_address = \
|
||||
self.datagram_socket.recvfrom(UDP_MAX_DGRAM_LENGTH)
|
||||
_logger.debug("Received datagram from peer: %s",
|
||||
self.payload_peer_address)
|
||||
if not self.payload:
|
||||
self.payload_peer_address = None
|
||||
return
|
||||
if self.connections.has_key(self.payload_peer_address):
|
||||
self.forward()
|
||||
else:
|
||||
return self.payload_peer_address
|
||||
|
||||
def forward(self):
|
||||
"""Forward a stored datagram
|
||||
|
||||
When the service method returns the address of a new peer, it holds
|
||||
the datagram from that peer in this instance. In this case, this
|
||||
method will perform the forwarding step. The target connection is the
|
||||
one associated with address None if get_connection has not been called
|
||||
since the service method returned the new peer's address, and the
|
||||
connection associated with the new peer's address if it has.
|
||||
"""
|
||||
|
||||
assert self.payload
|
||||
assert self.payload_peer_address
|
||||
if self.connections.has_key(self.payload_peer_address):
|
||||
conn = self.connections[self.payload_peer_address]
|
||||
default = False
|
||||
else:
|
||||
conn = self.connections[None] # propagate exception if not created
|
||||
default = True
|
||||
_logger.debug("Forwarding datagram from peer: %s, default: %s",
|
||||
self.payload_peer_address, default)
|
||||
self._forwarding_socket.sendto(self.payload, conn.getsockname())
|
||||
self.payload = ""
|
||||
self.payload_peer_address = None
|
||||
Reference in new issue
Block a user