sshmitm.multisocket module

Utility functions to create server sockets able to listen on both IPv4 and IPv6.

sshmitm.multisocket.create_server_sock(address, family=None, reuse_addr=None, transparent=False, queue_size=5, dual_stack=True)

Convenience function which creates a TCP server bound to address and return the socket object.

Internally it takes care of choosing the right address family (IPv4 or IPv6) depending on the host specified in address (a (host, port) tuple. If host is an empty string or None all interfaces are assumed and if dual stack is supported by kernel the socket will be able to listen for both IPv4 and IPv6 connections.

family can be set to either AF_INET or AF_INET6 to force the socket to use IPv4 or IPv6. If not set it will be determined from host.

reuse_addr tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not set will default to True on POSIX.

queue_size is the maximum number of queued connections passed to listen() (defaults to 5).

If dual_stack if True it will force the socket to listen on both IPv4 and IPv6 connections (defaults to True on all platforms natively supporting this functionality).

The returned socket can be used to accept() new connections as in:

>>> server = create_server_sock((None, 8000))
>>> while True:
...     sock, addr = server.accept()
...     # handle new sock connection
Parameters:
  • address (Tuple[str, int])

  • family (Optional[AddressFamily], default: None)

  • reuse_addr (Optional[bool], default: None)

  • transparent (bool, default: False)

  • queue_size (int, default: 5)

  • dual_stack (bool, default: True)

Return type:

socket

sshmitm.multisocket.has_dual_stack(sock=None)

Return True if kernel allows creating a socket which is able to listen for both IPv4 and IPv6 connections. If sock is provided the check is made against it.

Parameters:

sock (Optional[socket], default: None)

Return type:

bool