/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2013 David Hoeppner.  All rights reserved.
 */

#pragma D depends_on module unix
#pragma D depends_on provider sctp

/*
 * SCTP connection states.
 */
inline int32_t SCTP_STATE_IDLE = @SCTPS_IDLE@;
#pragma D binding "1.9.2" SCTP_STATE_IDLE
inline int32_t SCTP_STATE_BOUND = @SCTPS_BOUND@;
#pragma D binding "1.9.2" SCTP_STATE_BOUND
inline int32_t SCTP_STATE_LISTEN = @SCTPS_LISTEN@;
#pragma D binding "1.9.2" SCTP_STATE_LISTEN
inline int32_t SCTP_STATE_COOKIE_WAIT = @SCTPS_COOKIE_WAIT@;
#pragma D binding "1.9.2" SCTP_STATE_COOKIE_WAIT
inline int32_t SCTP_STATE_COOKIE_ECHOED = @SCTPS_COOKIE_ECHOED@;
#pragma D binding "1.9.2" SCTP_STATE_COOKIE_ECHOED
inline int32_t SCTP_STATE_ESTABLISHED = @SCTPS_ESTABLISHED@;
#pragma D binding "1.9.2" SCTP_STATE_ESTABLISHED
inline int32_t SCTP_STATE_SHUTDOWN_PENDING = @SCTPS_SHUTDOWN_PENDING@;
#pragma D binding "1.9.2" SCTP_STATE_SHUTDOWN_PENDING
inline int32_t SCTP_STATE_SHUTDOWN_SENT = @SCTPS_SHUTDOWN_SENT@;
#pragma D binding "1.9.2" SCTP_STATE_SHUTDOWN_SENT
inline int32_t SCTP_STATE_SHUTDOWN_RECEIVED = @SCTPS_SHUTDOWN_RECEIVED@;
#pragma D binding "1.9.2" SCTP_STATE_SHUTDOWN_RECEIVED
inline int32_t SCTP_STATE_SHUTDOWN_ACK_SENT = @SCTPS_SHUTDOWN_ACK_SENT@;
#pragma D binding "1.9.2" SCTP_STATE_SHUTDOWN_ACK_SENT

/*
 * Convert a SCTP state value to a string.
 */
inline string sctp_state_string[int32_t state] =
	state == SCTP_STATE_IDLE ? "state-idle" :
	state == SCTP_STATE_BOUND ? "state-bound" :
	state == SCTP_STATE_LISTEN ? "state-listen" :
	state == SCTP_STATE_COOKIE_WAIT ? "state-cookie-wait" :
	state == SCTP_STATE_COOKIE_ECHOED ? "state-cookie-echoed" :
	state == SCTP_STATE_ESTABLISHED ? "state-established" :
	state == SCTP_STATE_SHUTDOWN_PENDING ? "state-shutdown-pending" :
	state == SCTP_STATE_SHUTDOWN_SENT ? "state-shutdown-sent" :
	state == SCTP_STATE_SHUTDOWN_RECEIVED ? "state-shutdown-received" :
	state == SCTP_STATE_SHUTDOWN_ACK_SENT ? "state-shutdown-ack-sent" :
	"<unknown>";
#pragma D binding "1.9.2" sctp_state_string

/*
 * sctpinfo is the SCTP header fields.
 */
typedef struct sctpinfo {
	uint16_t	sctp_sport;	/* source port */
	uint16_t	sctp_dport;	/* destination port */
	uint32_t	sctp_verify;	/* verification tag */
	uint32_t	sctp_checksum;	/* headers + data checksum */
	sctp_chunk_hdr_t sctp_chunk_hdr;
	sctp_hdr_t	*sctp_hdr;	/* raw SCTP header */
} sctpinfo_t;

/*
 * sctpsinfo sctp state info.
 */
typedef struct sctpsinfo {
	string	sctps_laddr;		/* local address */
	string	sctps_raddr;		/* remote address */
	int32_t	sctps_state;		/* connection state */
} sctpsinfo_t;

#pragma D binding "1.9.2" translator
translator sctpinfo_t < sctp_hdr_t *S > {
	sctp_sport = ntohs(S->sh_sport);
	sctp_dport = ntohs(S->sh_dport);
	sctp_verify = ntohl(S->sh_verf);
	sctp_checksum = ntohl(S->sh_chksum);
	sctp_hdr = S;
};
