"""
This C(ython) extension module provides an interface to the libsctp library and associated socket API.
"""
from sctp cimport *
cimport libc
def bindx (int sd, object addrs, int flags) :
"""
Bind the given SOCK_SEQPACKET to the given set of local addresses.
sd the system socket FD
addresses the list of address tuples
flags one of SCTP_BINDX_ADD/REM_ADDR
"""
# automatically allocated array of sockaddr_storage's for passing given addresses to sctp_bindx
cdef libc.sockaddr_storage *addrs_out
# used to iterate through addrs_out
cdef libc.sockaddr_in *addr_in
# number of addresses given
cdef size_t addr_count = len(addrs)
# alloc stack storage for converting addresses
addrs_out = <libc.sockaddr_storage *> libc.alloca(addr_count * sizeof(libc.sockaddr_storage))
for i, address in enumerate(addrs) :
# XXX: assume socket-style IPv4 for now...
addr, port = address
addr_in = <libc.sockaddr_in *>(&addrs_out[i])
addr_in.sin_family = libc.AF_INET
addr_in.sin_port = libc.htons(port)
# XXX: known broken
libc.inet_aton(addr, &addr_in.sin_addr)
return sctp_bindx(sd, <libc.sockaddr *>addrs_out, addr_count, flags)