Module _sctp.sctp

IP addresses

The format of IPv4 addresses is identical to that of the standard Python socket module. Thus, such an address is a 2-tuple (host, port).

Unlike the standard Python socket module (where IPv6 addresses can be 2-tuples), IPv6 addresses here are necessarily 4-tuples of the form (host, port, flowinfo, scope_id). For example, the address ('::1', 2000) will be interpreted as an IPv4 address, which will trigger an error.

For instance, we must be able to distinguish between ('localhost', 2000) and ('localhost', 2000, 0, 0), as shown in the following example:

>>> sock = sctp_socket(AF_INET6, SOCK_STREAM)
>>> sock.sctp_bindx((('localhost', 2000), ('localhost', 2000, 0, 0)),\
             SCTP_BINDX_ADD_ADDR)

Functions

Creating SCTP sockets

class _sctp.sctp.sctp_socket(family, type, proto=IPPROTO_SCTP)

Create a new SCTP socket object using the given address family, socket type and protocol number. Address family must be AF_INET or AF_INET6. Socket type must be SOCK_STREAM or SOCK_SEQPACKET and protocol number must be IPPROTO_SCTP.

Raises SCTPError when one of the arguments is invalid.

Note

Newly created SCTP sockets inherit all methods and attributes from sockets in the standard Python socket module.

Other functions

Two utility functions (not defined in RFC 6458):

_sctp.sctp.ipaddr_to_sockaddr_storage(ip_addr)
Parameters:

ip_addr – an IPv4 address (host, port) or an IPv6 address (host, port, flowinfo, scope_id)

Returns:

a struct sockaddr_storage (See RFC 3493 – section-3.10) as a bytes object

Raise:

TypeError if ip_addr is not a valid IPv4/v6 address.

_sctp.sctp.sockaddr_storage_to_ipaddr(bytes)
Parameters:

bytes – a struct sockaddr_storage (see RFC 3493 – section-3.10) as a bytes object

Returns:

an IPv4 address (host, port) or an IPv6 address (host, port, flowinfo, scope_id)

If bytes cannnot be interpreted as struct sockaddr_storage, function sockaddr_storage_to_ipaddr() returns ('???', 0) or ('???', 0, 0, 0) (no exception raised).

SCTP Socket Objects

Attributes

sctp_socket.sctp_type

Is either 'one-to-one' or 'one-to-many' depending on whether the socket type is SOCK_STREAM or SOCK_SEQPACKET.

sctp_socket.sock

The underlying socket.socket.

Note

This read-only attribute is for internal use only.

Methods

Note

In all methods that take an iterable of IPv4/IPv6 addresses as an argument, the TypeError exception is thrown if the socket is an IPv4 socket and there are one or more IPv6 addresses among the addresses.

sctp_socket.sctp_bindx(addrs, flags)
Parameters:
  • addrs – an iterable of IPv4/IPv6 addresses

  • flags – must be SCTP_BINDX_ADD_ADDR or SCTP_BINDX_REM_ADDR

Returns:

None

Raise:

TypeError, ValueError or OSError

This method is a wrapper for function sctp_bindx() described in RFC 6458 – section-9.1.

sctp_socket.sctp_connectx(addrs, *, assoc_id=False)
Parameters:
  • addrs – an iterable of IPv4/IPv6 addresses

  • assoc_id – boolean

Returns:

None if assoc_id is False or “association identifier” otherwise

Raise:

TypeError, ValueError or OSError

This method is a wrapper for function sctp_connectx() described in RFC 6458 – section-9.9.

sctp_socket.sctp_getladdrs([assoc_id])
Parameters:

assoc_id – identifier of the association to query (int32_t)

Returns:

a tuple of IPv4/IPv6 addresses or None

This method is a wrapper for function sctp_getladdrs() described in RFC 6458 – section-9.5.

sctp_socket.sctp_getpaddrs([assoc_id])
Parameters:

assoc_id – identifier of the association to query (int32_t)

Returns:

a tuple of IPv4/IPv6 addresses or None

This method is a wrapper for function sctp_getpaddrs() described in RFC 6458 – section-9.3.

sctp_socket.sctp_recvv(bufsize)
Parameters:

bufsize – size of the bytes object receiving the data

Returns:

a 5-tuple (data, addr, info, infotype, flags) where:

Raise:

TypeError, ValueError or OSError

This method is a wrapper for function sctp_recvv() described in RFC 6458 – section-9.13.

In order to receive attribures sctp_rcvinfo and sctp_nxtinfo, you must first activate socket options SCTP_RECVRCVINFO and SCTP_RECVNXTINFO respectively as shown below:

>>> sock = sctp_socket(AF_INET SOCK_STREAM)
>>> sock.setsockopt(IPPROTO_SCTP, SCTP_RECVRCVINFO, 1)
... # waiting for data
>>> sock.sctp_recvv(1024)
>>> _, _, info, infotype, _ = s.sctp_recvv(1024)
>>> print(infotype == SCTP_RECVV_RCVINFO)
True
>>> print(info)
{'rcv_sid': 0, 'rcv_ssn': 0, 'rcv_flags': 0, 'rcv_ppid': 0, 'rcv_tsn': 188475,
 'rcv_cumtsn': 0, 'rcv_context': 0, 'rcv_assoc_id': 955}

An other example with both options:

>>> sock = sctp_socket(AF_INET SOCK_STREAM)
>>> sock.setsockopt(IPPROTO_SCTP, SCTP_RECVRCVINFO, 1)
>>> sock.setsockopt(IPPROTO_SCTP, SCTP_RECVNXTINFO, 1)
... # waiting for data
>>> sock.sctp_recvv(1024)
>>> _, _, info, infotype, _ = s.sctp_recvv(1024)
>>> print(infotype == SCTP_RECVV_RN)
True
>>> print(info.nxtinfo)
{'nxt_sid': 1, 'nxt_flags': 0, 'nxt_ppid': 0, 'nxt_length': 68,
 'nxt_assoc_id': 23}

Note

An option may have been requested but not received.

sctp_socket.sctp_sendv(data[, addrs[, info[, flags]]])
Parameters:
  • data – data to send: an iterable of bytes object

  • addrs – destination addresses: an iterable of IPv4/v6 addresses or None

  • info – attributes that can be used to describe a message to be sent or None. The info parameter can be either a sctp_sndinfo or a sctp_prinfo or a sctp_authinfo or a sctp_sendv_spa object

  • flags – the same flags as used by the sendmsg() call flags (e.g., MSG_DONTROUTE). Default value is 0

Returns:

number of bytes sent

Raise:

TypeError, ValueError or OSError

This method is a wrapper for function sctp_sendv() described in RFC 6458 – section-9.12.

Two examples showing how to use the info parameter:

  • sending data on stream number 5,

    >>> sock = sctp_socket(AF_INET6 SOCK_STREAM)
    ...
    >>> info = sctp_sndinfo(snd_sid=5)
    >>> noc = sock.sctp_sendv((b'some data',), None, info)
    >>> print(noc)
    9
    
  • sending data on stream number 5 and setting lifetime to 1 second,

    >>> sock = sctp_socket(AF_INET6 SOCK_STREAM)
    ...
    >>> sndinfo = sctp_sndinfo(snd_sid=5)
    >>> prinfo = sctp_prinfo(pr_policy=SCTP_PR_SCTP_TTL, pr_value=1000)
    >>> spa = sctp_sendv_spa((sndinfo, prinfo))
    >>> noc = sock.sctp_sendv((b'some data',), None, spa)
    
sctp_socket._getsockopt(level, optname, optval)
Parameters:
  • level (int) – option level

  • optname (int) – option name

  • optval (bytes) – option value

For example, when the level is IPPROTO_SCTP and the option is SCTP_PEER_ADDR_PARAMS, you need to pass a partially pre-filled bytes object as the option value. However, the method getsockopt() provided by the standard Python socket module takes an integer (optional) as its last argument. Hence the need to introduce this auxiliary method.

Note

This method is intended for internal use only.

Constants

This module export the following constants:

_sctp.sctp.IPPROTO_SCTP

sctp_sendv() – sctp_recvv() attributes:

  • _sctp.sctp.SCTP_RECVRCVINFO
  • _sctp.sctp.SCTP_RECVNXTINFO
  • _sctp.sctp.SCTP_SENDV_NOINFO
  • _sctp.sctp.SCTP_SENDV_SNDINFO
  • _sctp.sctp.SCTP_SENDV_PRINFO
  • _sctp.sctp.SCTP_SENDV_AUTHINFO
  • _sctp.sctp.SCTP_SENDV_SPA
  • _sctp.sctp.SCTP_SEND_SNDINFO_VALID
  • _sctp.sctp.SCTP_SEND_PRINFO_VALID
  • _sctp.sctp.SCTP_SEND_AUTHINFO_VALID
  • _sctp.sctp.SCTP_RECVV_NOINFO
  • _sctp.sctp.SCTP_RECVV_RCVINFO
  • _sctp.sctp.SCTP_RECVV_NXTINFO
  • _sctp.sctp.SCTP_RECVV_RN

SCTP Socket Options

Initialization Parameters (see RFC 6458 – section-8.1.3):

  • _sctp.sctp.SCTP_INIT
  • _sctp.sctp.SCTP_INITMSG

sctp_bindx() flags (see RFC 6458 – section-9.1):

  • _sctp.sctp.SCTP_BINDX_ADD_ADDR
  • _sctp.sctp.SCTP_BINDX_REM_ADDR

Retransmission Timeout Parameters (see RFC 6458 – section-8.1.1):

  • _sctp.sctp.SCTP_RTOINFO

Association Parameters (see RFC 6458 – section-8.1.2):

  • _sctp.sctp.SCTP_ASSOCINFO

Peer address parameters (see RFC 6458 – section-8.1.12):

  • _sctp.sctp.SCTP_PEER_ADDR_PARAMS
  • SPP_*

    field spp_flags of struct sctp_paddrparams

Peer address information (see RFC 6458 – section-8.2.2):

  • _sctp.sctp.SCTP_GET_PEER_ADDR_INFO
  • _sctp.sctp.SCTP_UNCONFIRMED
  • _sctp.sctp.SCTP_ACTIVE
  • _sctp.sctp.SCTP_INACTIVE

Set Primary Address (see RFC 6458 – section-8.1.9):

  • _sctp.sctp.SCTP_PRIMARY_ADDR

Set Adaptation Layer Indicator (see RFC 6458 – section-8.1.10):

  • _sctp.sctp.SCTP_ADAPTATION_LAYER

Get or Set the Maximum Fragmentation Size (see RFC 6458 – section-8.1.16):

  • _sctp.sctp.SCTP_MAXSEG

Get or Set the List of Supported HMAC Identifiers (see RFC 6458 – section-8.1.17):

  • _sctp.sctp.SCTP_HMAC_IDENT

Get or Set the Active Shared Key (see RFC 6458 – section-8.1.18):

  • _sctp.sctp.SCTP_AUTH_ACTIVE_KEY

Get or Set Delayed SACK Timer (see RFC 6458 – section-8.1.19):

  • _sctp.sctp.SCTP_DELAYED_SACK

Get or Set Fragmented Interleave (see RFC 6458 – section-8.1.20):

  • _sctp.sctp.SCTP_FRAGMENT_INTERLEAVE

Set or Get the Maximum Burst (see RFC 6458 – section-8.1.24):

  • _sctp.sctp.SCTP_MAX_BURST

Set or Get the Default Context (see RFC 6458 – section-8.1.25):

  • _sctp.sctp.SCTP_CONTEXT

Set Default Send Parameters (see RFC 6458 – section-8.1.31):

  • _sctp.sctp.SCTP_DEFAULT_SNDINFO

Set Default PR-SCTP Parameters (see RFC 6458 – section-8.1.32):

  • _sctp.sctp.SCTP_DEFAULT_PRINFO

Association Status (see RFC 6458 – section-8.2.1):

  • _sctp.sctp.SCTP_STATUS

    field sstat_state:

    • _sctp.sctp.SCTP_CLOSED
    • _sctp.sctp.SCTP_ESTABLISHED
    • _sctp.sctp.SCTP_SHUTDOWN_PENDING
    • _sctp.sctp.SCTP_SHUTDOWN_SENT
    • _sctp.sctp.SCTP_SHUTDOWN_RECEIVED
    • _sctp.sctp.SCTP_SHUTDOWN_ACK_SENT

Get the List of Chunks the Peer Requires to Be Authenticated (see RFC 6458 – section-8.2.3):

  • _sctp.sctp.SCTP_PEER_AUTH_CHUNKS

Get the List of Chunks the Local Endpoint Requires to Be Authenticated (see RFC 6458 – section-8.2.4):

  • _sctp.sctp.SCTP_LOCAL_AUTH_CHUNKS

Get the Current Number/List of Associations (see RFC 6458 – section-8.2.5/6):

  • _sctp.sctp.SCTP_GET_ASSOC_NUMBER
  • _sctp.sctp.SCTP_GET_ASSOC_ID_LIST

Set Peer Primary Address (see RFC 6458 – section-8.3.1):

  • _sctp.sctp.SCTP_SET_PEER_PRIMARY_ADDR

Add a Chunk That Must Be Authenticated (see RFC 6458 – section-8.3.2):

  • _sctp.sctp.SCTP_AUTH_CHUNK

Set a Shared Key (see RFC 6458 – section-8.3.3):

  • _sctp.sctp.SCTP_AUTH_KEY

Deactivate/Delete a Shared Key (see RFC 6458 – section-8.3.4/5):

  • _sctp.sctp.SCTP_AUTH_DEACTIVATE_KEY
  • _sctp.sctp.SCTP_AUTH_DELETE_KEY

Sizes (sizeof()) of various structures (struct sctp_*)

  • _sctp.sctp.SCTP_INITMSG_CSIZE
  • _sctp.sctp.SCTP_PADDRPARAMS_CSIZE
  • _sctp.sctp.SCTP_PADDRINFO_CSIZE
  • _sctp.sctp.SCTP_RTOINFO_CSIZE
  • _sctp.sctp.SCTP_ASSOCPARAMS_CSIZE
  • _sctp.sctp.SCTP_SETPRIM_CSIZE
  • _sctp.sctp.SCTP_SETADAPTATION_CSIZE
  • _sctp.sctp.SCTP_MAXSEG_CSIZE
  • _sctp.sctp.SCTP_DELAYED_SACK_CSIZE
  • _sctp.sctp.SCTP_SNDINFO_CSIZE
  • _sctp.sctp.SCTP_DEFAULT_PRINFO_CSIZE
  • _sctp.sctp.SCTP_STATUS_CSIZE
  • _sctp.sctp.SCTP_SET_PEER_PRIMARY_ADDR_CSIZE
  • _sctp.sctp.SOCKADDR_STORAGE_CSIZE

    Note

    The C structures defined in <netinet/sctp.h> are aligned differently depending on the implementation. For example, on FreeBSD they have attribute __attribute__((packed)), while on Linux they have attribute __attribute__((packed, aligned(4))). Knowing these sizes allows module sctp to calculate the possible padding required when using module struct (see fmt property). These constants are intended for internal use only.

Miscellaneous:

  • _sctp.sctp.SCTP_NODELAY
  • _sctp.sctp.SCTP_AUTOCLOSE
  • _sctp.sctp.SCTP_DISABLE_FRAGMENTS
  • _sctp.sctp.SCTP_I_WANT_MAPPED_V4_ADDR
  • _sctp.sctp.SCTP_PARTIAL_DELIVERY_POINT
  • _sctp.sctp.SCTP_AUTO_ASCONF
  • _sctp.sctp.SCTP_REUSE_PORT
  • _sctp.sctp.SCTP_AUTH_HMAC_ID_SHA1
  • _sctp.sctp.SCTP_AUTH_HMAC_ID_SHA256

Exceptions

exception _sctp.sctp.SCTPError