SCTP socket programming basic functions

    SCTP servers can use the sctp_bindx function to bind a subset of IP addresses associated with the host system, whereas TCP and UDP servers bind either an address on the host or all addresses, but not a subset of these addresses.
#include <netinet/sctp.h>
int sctp_bindx(int sockfd, const struct sockaddr *addrs, int addrcnt, int flags);
                                       /* Return value: if successful, return 0; otherwise, return -1 */

    Among them, sockfd is the socket descriptor returned by the socket function, addrs is a pointer to a compact address list, and the number of addresses is specified by the addrcnt parameter. Each socket address structure follows the previous one with no padding bytes in between, as shown in the figure below.

    The flags parameter directs the sctp_bindx call to perform one of the two behaviors shown in the following table.

    The sctp_bindx call can be used on both bound and unbound sockets. For unbound sockets, sctp_bindx will bind the given set of addresses to it. For a bound socket, if SCTP_BINDX_ADD_ADDR is specified, the extra address is added to the socket descriptor, and if SCTP_BINDX_REM_ADDR is specified, the given address is removed from the added addresses of the socket descriptor. If this function is called on a listening socket, the new address configuration will only affect newly created associations. The port number of all socket address structures must be the same and match the one already bound, otherwise the call will generate an EINVAL error code.
    If an endpoint supports the dynamic address feature, then sctp_bindx will cause the endpoint to send an appropriate message to the peer to modify the peer's address list. Since adding or subtracting addresses from a connected association is an optional feature, implementations that do not support this feature will return EOPNOTSUPP. This feature may be useful for systems that support dynamic interface provisioning, eg if a new Ethernet interface is brought up, the application process can specify the SCTP_BINDX_ADD_ADDR flag to start using this interface on an existing connection.
    There is also the following set of commonly used functions in SCTP.
#include <netinet/sctp.h>
int sctp_connectx(int sockfd, const struct sockaddr *addrs, int addrcnt);
                                       /* Return value: if successful, return 0; otherwise, return -1 */
int sctp_getpaddrs(int sockfd, sctp_assoc_t id, struct sockaddr **addrs);
            /* Return value: If successful, return the number of peer addresses stored in addrs; otherwise, return -1 */
void sctp_freepaddrs(struct sockaddr *addrs);

int sctp_getladdrs(int sockfd, sctp_assoc_t id, struct sockaddr **addrs);
            /* Return value: If successful, return the number of local addresses stored in addrs; otherwise, return -1 */
void sctp_freeladdrs(struct sockaddr *addrs);

ssize_t sctp_sendmsg(int sockfd, const void *msg, size_t msgsz,
                     const struct sockaddr *to, socklen_t tolen,
                     uint32_t ppid, uint32_t flags, uint16_t stream,
                     uint32_t timetolive, uint32_t context);
ssize_t sctp_recvmsg(int sockfd, void *msg, size_t msgsz,
                     struct sockaddr *from, socklen_t *fromlen,
                     struct sctp_sndrcvinfo *sinfo, int *msg_flags);
             /* Return value of both functions: if successful, the number of bytes written or read; otherwise, -1 */

int sctp_opt_info(int sockfd, sctp_assoc_t assoc_id, int opt, void *arg, socklen_t *siz);
                                    /* Return value: if successful, return 0; otherwise, return -1 */
int sctp_peeloff(int sockfd, sctp_assoc_t id);
                   /* Return value: If successful, return a new socket descriptor; otherwise, return -1 */

    The sctp_connectx function is used to connect to a multihomed peer host. This function specifies addrcnt addresses in the addrs parameter that all belong to the same peer. All addresses in addrs are considered valid and verified addresses, and the SCTP stack uses one or more of these addresses to establish an association.
    Since the getpeername function only returns the main destination address when used in SCTP, if you need to know all the addresses of the peer, you should use the sctp_getpaddrs function. The id parameter is the associated identifier of the one-to-many socket, which is ignored by the one-to-one socket. The address content of the addrs parameter is a compact address list dynamically allocated and filled by this function, and the allocated resources should be released using the sctp_freepaddrs function after use.
    The usage of the sctp_getladdrs and sctp_freeladdrs functions is the same as that of sctp_getpaddrs and sctp_freepaddrs, but they are for the local end.
    Compared with the sendmsg function, the sctp_sendmsg function simplifies the sending method at the cost of specifying more parameters. It sends the msg content of the msgsz byte buffer to the peer to, and the tolen parameter specifies the length of the address stored in to. The ppid parameter specifies the payload protocol identifier that will be passed with the data block. The flags parameter will be passed to the SCTP stack to identify any SCTP options (if the implementation maps sctp_sendmsg to the sendmsg function, the flags parameter of sendmsg will be set to 0), and its valid values ​​are the same as those mentioned in the SCTP socket options section. to the sinfo_flags field in the sctp_sndrcvinfo structure.
    The stream parameter specifies an SCTP stream number. The timetolive parameter specifies the lifetime of the message in milliseconds, where 0 means infinite lifetime. The context parameter is used to specify a possible user context that associates a failure message sent via the message notification mechanism with an application-specific local context.
    Similar to sctp_sendmsg, the sctp_recvmsg function also provides a more convenient interface to the advanced features of SCTP. Using this function can not only get the address of the peer, but also get the msg_flags parameter usually returned with the recvmsg function call, which stores the possible message flags. Additionally, if sctp_data_io_event is subscribed with the SCTP_EVENTS socket option (on by default), then the sctp_sndrcvinfo structure is populated with message-related details.
    The sctp_opt_info function is provided for those implementations that cannot use the getsockopt function for SCTP, because some SCTP socket options (such as SCTP_STATUS) require an in_out variable to pass the association identifier. For systems that cannot provide input and output variables for the getsockopt function, only the sctp_opt_info function can be used. The assoc_id parameter here gives the possible association identifier, the opt parameter is the socket option of SCTP, arg gives the socket option parameter, and siz is used to store the size of the parameter.
    The sctp_peeloff function can be used to extract an association from a one-to-many socket to form a one-to-one socket by itself. Its semantics are like the accept function with one extra parameter. The caller passes the sockfd of a one-to-many socket and the associated identifier id to be extracted to the function call, and a new socket description will be returned at the end of the call. , which is a one-to-one socket descriptor corresponding to the requested association.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326026461&siteId=291194637