zelos.network.base_socket module

class zelos.network.base_socket.BaseSocket(network_manager, domain, sock_type, protocol)

Bases: object

This socket pretends that every connection succeeds. Every return should succeed and return all zeros.

property errno

The last error that occurred (see python errno) for any method of this class that returns -1 or raises an exception.

setsockopt(level: int, name: int, value: Any) → None

Sets socket options.

Parameters
  • level – option level (as defined in python socket)

  • name – option name (as defined in python socket)

  • value – option value (type depends on option name)

getsockopt(level: int, name: int) → Optional[Any]

Gets socket options.

Parameters
  • level – option level (as defined in python socket)

  • name – option name (as defined in python socket)

Returns

The requested socket data, or None if that data does not exist.

set_nonblock(is_nonblock: bool)

Sets the socket blocking option.

Parameters

is_nonblock – if True, makes the socket non-blocking.

is_nonblock()

Gets the socket blocking status.

Returns

True if the socket is non_blocking, False otherwise.

connect(host_and_port)

Connects the socket.

Parameters

host_and_port – A tuple of the form (host: string, port: int)

Returns

0 on success, -1 on failure.

close()

Closes the socket.

bind(host_and_port)

Binds the socket to a port.

Parameters

host_and_port – A tuple of the form (host: string, port: int)

Returns

0 on success, -1 on failure.

listen(backlog: int = 0)

Socket listen.

Returns

0 on success, -1 on failure.

accept()

Accepts a new connection on the listening socket.

Returns

0 on success, -1 on failure.

peek()

Peek at readable data on the socket.

Returns

A byte array containing the observed data.

send(data: bytes, flags: int = 0)

Sends data over the socket.

Parameters
  • data – The byte array to send.

  • flags – socket send flags

Returns

The length of the data sent.

recv(bufsize: int, flags: int)

Receive data from the socket.

Parameters
  • bufsize – maximum size of data to receive.

  • flags – socket recv flags

Returns

A byte array of the data received, or None.

recvfrom(bufsize: int, flags: int = 0)

Receive data from a non-streaming (i.e. non-TCP) protocol.

Parameters
  • bufsize – maximum size of data to receive.

  • flags – socket recv flags

Returns

The tuple (data received, domain, host, port).

sendto(data: bytes, host_and_port, flags: int = 0)

Send data over non-streaming (i.e. non-TCP) protocol. If host and port are not None, send to that address. Otherwise, send to the address given during socket connect.

Parameters
  • host_and_port – A tuple of the form (host: string, port: int)

  • flags – socket recv flags

Returns

The length sent.

shutdown(how: int)

Shut down part of the socket connection.

Parameters

how – the part of the connection to shutdown

class zelos.network.base_socket.DnsSocketSimulator(domain, host=None, port=None)

Bases: object

Simulate DNS requests and responses.

send(payload, flags=0)
recv(bufsize, flags=0)
sendto(payload, host_and_port, flags=0)
recvfrom(bufsize, flags=0)
is_readable()
class zelos.network.base_socket.RawSocketSimulator(domain, host=None, port=None)

Bases: object

Simulates scans that make use of raw sockets. For instance, raw SYN scan packets will be replied to with appropriate SYN+ACK packets.

send(payload)
recv(bufsize)
sendto(payload, host_and_port, flags=0)
recvfrom(bufsize, flags=0)
is_readable()
class zelos.network.base_socket.BaseSelect(network_manager)

Bases: object

Implements select and poll for zelos SocketHandles that make use of the BaseSocket.

POLLIN = 1
POLLPRI = 2
POLLOUT = 4
POLLERR = 8
POLLHUP = 16
POLLNVAL = 32
POLLRDNORM = 64
POLLRDBAND = 128
POLLWRNORM = 256
POLLWRBAND = 512
POLLMSG = 1024
POLLREMOVE = 4096
POLLRDHUP = 8192
select(in_handles, out_handles, ex_handles, timeout=0.1)

Select file descriptors.

Given 3 File Descriptor lists, select the first one that is ready within the timeout window. For BaseSockets, always return ready for write events, and not ready for read events, as BaseSockets will never reply with data.

Parameters
  • in_handles – fds to select for read.

  • out_handles – fds to select for write.

  • ex_handles – fds to select for exceptional events.

Returns

3 lists that indicate which handles ids are ready.

poll(fds, timeout=0.1)

Poll file descriptors.

Parameters
  • fds – a list of tuples [(fd, events),..].

  • timeout – maximum time to wait for the file descriptors.

Returns

the list of tuples with fired events [(fd, revents),..].