0
|
1 |
"""
|
|
2 |
This C(ython) extension module provides an interface to the libsctp library and associated socket API.
|
|
3 |
"""
|
|
4 |
|
|
5 |
from libc cimport *
|
|
6 |
|
|
7 |
# <netinet/sctp.h>
|
|
8 |
# this defines the kernel SCTP -> userspace API extensions, such as structure types etc.
|
|
9 |
cdef extern from "netinet/sctp.h" :
|
|
10 |
## types
|
|
11 |
ctypedef __s32 sctp_assoc_t
|
|
12 |
|
|
13 |
## constants
|
|
14 |
# sockapi
|
|
15 |
int SOL_SCTP
|
|
16 |
int IPPROTO_SCTP
|
|
17 |
|
|
18 |
# sockopts
|
|
19 |
int SCTP_RTOINFO
|
|
20 |
int SCTP_ASSOCINFO
|
|
21 |
int SCTP_INITMSG
|
|
22 |
int SCTP_NODELAY
|
|
23 |
int SCTP_AUTOCLOSE
|
|
24 |
int SCTP_SET_PEER_PRIMARY_ADDR
|
|
25 |
int SCTP_PRIMARY_ADDR
|
|
26 |
int SCTP_ADAPTATION_LAYER
|
|
27 |
int SCTP_DISABLE_FRAGMENTS
|
|
28 |
int SCTP_PEER_ADDR_PARAMS
|
|
29 |
int SCTP_DEFAULT_SEND_PARAM
|
|
30 |
int SCTP_EVENTS
|
|
31 |
int SCTP_I_WANT_MAPPED_V4_ADDR
|
|
32 |
int SCTP_MAXSEG
|
|
33 |
int SCTP_STATUS
|
|
34 |
int SCTP_GET_PEER_ADDR_INFO
|
|
35 |
int SCTP_DELAYED_ACK_TIME
|
|
36 |
int SCTP_CONTEXT
|
|
37 |
int SCTP_FRAGMENT_INTERLEAVE
|
|
38 |
int SCTP_PARTIAL_DELIVERY_POINT
|
|
39 |
int SCTP_MAX_BURST
|
|
40 |
|
|
41 |
## send/recv-msg cmsghdr's
|
|
42 |
struct sctp_initmsg :
|
|
43 |
__u16 sinit_num_ostreams
|
|
44 |
__u16 sinit_max_instreams
|
|
45 |
__u16 sinit_max_attempts
|
|
46 |
__u16 sinit_max_init_timeo
|
|
47 |
|
|
48 |
struct sctp_sndrcvinfo :
|
|
49 |
__u16 sinfo_stream
|
|
50 |
__u16 sinfo_ssn
|
|
51 |
__u16 sinfo_flags
|
|
52 |
__u32 sinfo_ppid
|
|
53 |
__u32 sinfo_context
|
|
54 |
__u32 sinfo_timetolive
|
|
55 |
__u32 sinfo_tsn
|
|
56 |
__u32 sinfo_cumtsn
|
|
57 |
sctp_assoc_t sinfo_assoc_id
|
|
58 |
|
|
59 |
# sctp_sndrcvinfo.sinfo_flags values
|
|
60 |
enum sctp_sinfo_flags :
|
|
61 |
SCTP_UNORDERED # Send/receive message unordered
|
|
62 |
SCTP_ADDR_OVER # Override the primary destination
|
|
63 |
SCTP_ABORT # Send an ABORT message to the peer
|
|
64 |
SCTP_EOF # Initiate graceful shutdown process
|
|
65 |
|
|
66 |
## notifications
|
|
67 |
struct sctp_assoc_change :
|
|
68 |
__u16 sac_type
|
|
69 |
__u16 sac_flags
|
|
70 |
__u32 sac_length
|
|
71 |
__u16 sac_state
|
|
72 |
__u16 sac_error
|
|
73 |
__u16 sac_outbound_streams
|
|
74 |
__u16 sac_inbound_streams
|
|
75 |
sctp_assoc_t sac_assoc_id
|
|
76 |
__u8 sac_info[0]
|
|
77 |
|
|
78 |
# sctp_assoc_change.sac_state
|
|
79 |
enum sctp_sac_state :
|
|
80 |
SCTP_COMM_UP
|
|
81 |
SCTP_COMM_LOST
|
|
82 |
SCTP_RESTART
|
|
83 |
SCTP_SHUTDOWN_COMP
|
|
84 |
SCTP_CANT_STR_ASSOC
|
|
85 |
|
|
86 |
struct sctp_paddr_change :
|
|
87 |
__u16 spc_type
|
|
88 |
__u16 spc_flags
|
|
89 |
__u32 spc_length
|
|
90 |
sockaddr_storage spc_aaddr
|
|
91 |
int spc_state
|
|
92 |
int spc_error
|
|
93 |
sctp_assoc_t spc_assoc_id
|
|
94 |
|
|
95 |
enum sctp_spc_state :
|
|
96 |
SCTP_ADDR_AVAILABLE
|
|
97 |
SCTP_ADDR_UNREACHABLE
|
|
98 |
SCTP_ADDR_REMOVED
|
|
99 |
SCTP_ADDR_ADDED
|
|
100 |
SCTP_ADDR_MADE_PRIM
|
|
101 |
SCTP_ADDR_CONFIRMED
|
|
102 |
|
|
103 |
struct sctp_remote_error :
|
|
104 |
__u16 sre_type
|
|
105 |
__u16 sre_flags
|
|
106 |
__u32 sre_length
|
|
107 |
__u16 sre_error
|
|
108 |
sctp_assoc_t sre_assoc_id
|
|
109 |
__u8 sre_data[0]
|
|
110 |
|
|
111 |
|
|
112 |
struct sctp_send_failed :
|
|
113 |
__u16 ssf_type
|
|
114 |
__u16 ssf_flags
|
|
115 |
__u32 ssf_length
|
|
116 |
__u32 ssf_error
|
|
117 |
sctp_sndrcvinfo ssf_info
|
|
118 |
sctp_assoc_t ssf_assoc_id
|
|
119 |
__u8 ssf_data[0]
|
|
120 |
|
|
121 |
enum sctp_ssf_flags :
|
|
122 |
SCTP_DATA_UNSENT
|
|
123 |
SCTP_DATA_SENT
|
|
124 |
|
|
125 |
struct sctp_shutdown_event :
|
|
126 |
__u16 sse_type
|
|
127 |
__u16 sse_flags
|
|
128 |
__u32 sse_length
|
|
129 |
sctp_assoc_t sse_assoc_id
|
|
130 |
|
|
131 |
struct sctp_adaptation_event :
|
|
132 |
__u16 sai_type
|
|
133 |
__u16 sai_flags
|
|
134 |
__u32 sai_length
|
|
135 |
__u32 sai_adaptation_ind
|
|
136 |
sctp_assoc_t sai_assoc_id
|
|
137 |
|
|
138 |
struct sctp_pdapi_event :
|
|
139 |
__u16 pdapi_type
|
|
140 |
__u16 pdapi_flags
|
|
141 |
__u32 pdapi_length
|
|
142 |
__u32 pdapi_indication
|
|
143 |
sctp_assoc_t pdapi_assoc_id
|
|
144 |
|
|
145 |
enum :
|
|
146 |
SCTP_PARTIAL_DELIVERY_ABORTED
|
|
147 |
|
|
148 |
struct sctp_event_subscribe :
|
|
149 |
__u8 sctp_data_io_event
|
|
150 |
__u8 sctp_association_event
|
|
151 |
__u8 sctp_address_event
|
|
152 |
__u8 sctp_send_failure_event
|
|
153 |
__u8 sctp_peer_error_event
|
|
154 |
__u8 sctp_shutdown_event
|
|
155 |
__u8 sctp_partial_delivery_event
|
|
156 |
__u8 sctp_adaptation_layer_event
|
|
157 |
|
|
158 |
struct sn_header :
|
|
159 |
__u16 sn_type
|
|
160 |
__u16 sn_flags
|
|
161 |
__u32 sn_length
|
|
162 |
|
|
163 |
union sctp_notification :
|
|
164 |
sn_header sn_header
|
|
165 |
|
|
166 |
sctp_assoc_change sn_assoc_change
|
|
167 |
sctp_paddr_change sn_paddr_change
|
|
168 |
sctp_remote_error sn_remote_error
|
|
169 |
sctp_send_failed sn_send_failed
|
|
170 |
sctp_shutdown_event sn_shutdown_event
|
|
171 |
sctp_adaptation_event sn_adaptation_event
|
|
172 |
sctp_pdapi_event sn_pdapi_event
|
|
173 |
|
|
174 |
enum sctp_sn_type :
|
|
175 |
SCTP_SN_TYPE_BASE
|
|
176 |
SCTP_ASSOC_CHANGE
|
|
177 |
SCTP_PEER_ADDR_CHANGE
|
|
178 |
SCTP_SEND_FAILED
|
|
179 |
SCTP_REMOTE_ERROR
|
|
180 |
SCTP_SHUTDOWN_EVENT
|
|
181 |
SCTP_PARTIAL_DELIVERY_EVENT
|
|
182 |
SCTP_ADAPTATION_INDICATION
|
|
183 |
|
|
184 |
enum sctp_sn_error :
|
|
185 |
SCTP_FAILED_THRESHOLD
|
|
186 |
SCTP_RECEIVED_SACK
|
|
187 |
SCTP_HEARTBEAT_SUCCESS
|
|
188 |
SCTP_RESPONSE_TO_USER_REQ
|
|
189 |
SCTP_INTERNAL_ERROR
|
|
190 |
SCTP_SHUTDOWN_GUARD_EXPIRES
|
|
191 |
SCTP_PEER_FAULTY
|
|
192 |
|
|
193 |
ctypedef sctp_sn_error sctp_sn_error_t
|
|
194 |
|
|
195 |
|
|
196 |
## sctp_bindx
|
|
197 |
int SCTP_BINDX_ADD_ADDR
|
|
198 |
int SCTP_BINDX_REM_ADDR
|
|
199 |
|
|
200 |
int sctp_bindx (int sd, sockaddr *addrs, int addrcnt, int flags)
|
|
201 |
int sctp_connectx (int sd, sockaddr *addrs, int addrcnt)
|
|
202 |
|
|
203 |
|
|
204 |
def bindx (int sd, object addrs, int flags) :
|
|
205 |
"""
|
|
206 |
Bind the given SOCK_SEQPACKET to the given set of local addresses.
|
|
207 |
|
|
208 |
sd the system socket FD
|
|
209 |
addresses the list of address tuples
|
|
210 |
flags one of SCTP_BINDX_ADD/REM_ADDR
|
|
211 |
"""
|
|
212 |
|
|
213 |
# automatically allocated array of sockaddr_storage's for passing given addresses to sctp_bindx
|
|
214 |
cdef sockaddr_storage *addrs_out
|
|
215 |
|
|
216 |
# used to iterate through addrs_out
|
|
217 |
cdef sockaddr_in *addr_in
|
|
218 |
|
|
219 |
# number of addresses given
|
|
220 |
cdef size_t addr_count = len(addrs)
|
|
221 |
|
|
222 |
# alloc stack storage for converting addresses
|
|
223 |
addrs_out = <sockaddr_storage *> alloca(addr_count * sizeof(sockaddr_storage))
|
|
224 |
|
|
225 |
for i, address in enumerate(addrs) :
|
|
226 |
# XXX: assume socket-style IPv4 for now...
|
|
227 |
addr, port = address
|
|
228 |
|
|
229 |
addr_in = <sockaddr_in *>(&addrs_out[i])
|
|
230 |
|
|
231 |
addr_in.sin_family = AF_INET
|
|
232 |
addr_in.sin_port = htons(port)
|
|
233 |
inet_aton(addr, &addr_in.sin_addr)
|
|
234 |
|
|
235 |
return sctp_bindx(sd, <sockaddr *>addrs_out, addr_count, flags)
|
|
236 |
|