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_INETorAF_INET6. Socket type must beSOCK_STREAMorSOCK_SEQPACKETand protocol number must beIPPROTO_SCTP.Raises
SCTPErrorwhen one of the arguments is invalid.Note
Newly created SCTP sockets inherit all methods and attributes from sockets in the standard Python
socketmodule.
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 abytesobject- Raise:
TypeErrorif 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 abytesobject- 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 isSOCK_STREAMorSOCK_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_ADDRorSCTP_BINDX_REM_ADDR
- Returns:
None- Raise:
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:
Noneif assoc_id isFalseor “association identifier” otherwise- Raise:
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
bytesobject receiving the data- Returns:
a 5-tuple
(data, addr, info, infotype, flags)where:datais abytesobject holding the data received (up to bufsize)addr: sender addressinfois either asctp_rcvinfoor asctp_nxtinfoor asctp_recvv_rnobjectinfotypeis either the constantSCTP_RECVV_NOINFOorSCTP_RECVV_RCVINFOorSCTP_RECVV_NXTINFOorSCTP_RECVV_RNflags: received message flags (e.g.,MSG_NOTIFICATION)
- Raise:
This method is a wrapper for function
sctp_recvv()described in RFC 6458 – section-9.13.In order to receive attribures
sctp_rcvinfoandsctp_nxtinfo, you must first activate socket optionsSCTP_RECVRCVINFOandSCTP_RECVNXTINFOrespectively 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
bytesobjectaddrs – destination addresses: an iterable of IPv4/v6 addresses or
Noneinfo – attributes that can be used to describe a message to be sent or
None. The info parameter can be either asctp_sndinfoor asctp_prinfoor asctp_authinfoor asctp_sendv_spaobjectflags – the same flags as used by the
sendmsg()call flags (e.g.,MSG_DONTROUTE). Default value is0
- Returns:
number of bytes sent
- Raise:
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)¶
-
For example, when the level is
IPPROTO_SCTPand the option isSCTP_PEER_ADDR_PARAMS, you need to pass a partially pre-filled bytes object as the option value. However, the methodgetsockopt()provided by the standard Pythonsocketmodule 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_bindx() flags (see RFC 6458 – section-9.1):
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_flagsofstruct 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:
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):
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):
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 modulesctpto calculate the possible padding required when using modulestruct(seefmtproperty). These constants are intended for internal use only.
Miscellaneous:
Exceptions¶
- exception _sctp.sctp.SCTPError¶