Squashed 'third_party/lksctp-tools/' content from commit 200eca7f1

Change-Id: I8f7575513f114b205178cac5c6b3706f3d725cb5
git-subtree-dir: third_party/lksctp-tools
git-subtree-split: 200eca7f1419b1ae53958b51e8551f7e7f6cd467
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
new file mode 100644
index 0000000..1296caa
--- /dev/null
+++ b/src/lib/Makefile.am
@@ -0,0 +1,15 @@
+# Include these two in all the Makefile.am's!!!
+include $(top_srcdir)/Makefile.vars
+include $(top_srcdir)/Makefile.rules
+include $(top_srcdir)/Makefile.dirs
+
+# General compilation flags
+AM_CPPFLAGS = -I$(top_builddir)/src/include
+
+lib_LTLIBRARIES = libsctp.la
+
+libsctp_la_SOURCES = bindx.c connectx.c peeloff.c opt_info.c \
+		addrs.c sendmsg.c recvmsg.c Versions.map
+libsctp_la_LDFLAGS = -version-info \
+		@LIBSCTP_CURRENT@:@LIBSCTP_REVISION@:@LIBSCTP_AGE@ \
+		-Wl,--version-script=$(srcdir)/Versions.map
diff --git a/src/lib/Versions.map b/src/lib/Versions.map
new file mode 100644
index 0000000..7faad5d
--- /dev/null
+++ b/src/lib/Versions.map
@@ -0,0 +1,28 @@
+VERS_1 {
+	global:
+		sctp_getpaddrs;
+		sctp_freepaddrs;
+		sctp_getladdrs;
+		sctp_freeladdrs;
+		sctp_getaddrlen;
+		sctp_bindx;
+		sctp_connectx;
+		sctp_opt_info;
+		sctp_peeloff;
+		sctp_recvmsg;
+		sctp_recvv;
+		sctp_sendmsg;
+		sctp_sendv;
+		sctp_send;
+
+	local:
+		*;
+};
+
+VERS_2 {
+	global:	sctp_connectx;
+} VERS_1;
+
+VERS_3 {
+	global:	sctp_connectx;
+} VERS_2;
diff --git a/src/lib/addrs.c b/src/lib/addrs.c
new file mode 100644
index 0000000..65f5222
--- /dev/null
+++ b/src/lib/addrs.c
@@ -0,0 +1,155 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * addrs.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
+ *
+ * (C) Copyright IBM Corp. 2003
+ * Copyright (c) 2001-2002 Intel Corp.
+ *
+ * Written or modified by:
+ *  Ardelle Fan     <ardelle.fan@intel.com>
+ *  Sridhar Samudrala <sri@us.ibm.com>
+ *  Ivan Skytte Jørgensen <isj-sctp@i1.dk>
+ */
+
+#include <malloc.h>
+#include <netinet/in.h>
+#include <netinet/sctp.h>
+#include <string.h>
+#include <errno.h>
+
+/* 
+ * Common getsockopt() layer 
+ * If the NEW getsockopt() API fails this function will fall back to using
+ * the old API
+ */
+static int
+sctp_getaddrs(int sd, sctp_assoc_t id, int optname_new,
+	      struct sockaddr **addrs)
+{
+	int cnt, err;
+	socklen_t len;
+	size_t bufsize = 4096; /*enough for most cases*/
+
+	struct sctp_getaddrs *getaddrs = (struct sctp_getaddrs*)malloc(bufsize);
+	if(!getaddrs)
+		return -1;
+	
+	for(;;) {
+		char *new_buf;
+
+		len = bufsize;
+		getaddrs->assoc_id = id;
+		err = getsockopt(sd, SOL_SCTP, optname_new, getaddrs, &len);
+		if (err == 0) {
+			/*got it*/
+			break;
+		}
+		if (errno != ENOMEM ) {
+			/*unknown error*/
+			free(getaddrs);
+			return -1;
+		}
+		/*expand buffer*/
+		if (bufsize > 128*1024) {
+			/*this is getting ridiculous*/
+			free(getaddrs);
+			errno = ENOBUFS;
+			return -1;
+		}
+		new_buf = realloc(getaddrs, bufsize+4096);
+		if (!new_buf) {
+			free(getaddrs);
+			return -1;
+		}
+		bufsize += 4096;
+		getaddrs = (struct sctp_getaddrs*)new_buf;
+	}
+
+	/* we skip traversing the list, allocating a new buffer etc. and enjoy
+	 * a simple hack*/
+	cnt = getaddrs->addr_num;
+	memmove(getaddrs, getaddrs + 1, len);
+	*addrs = (struct sockaddr*)getaddrs;
+
+	return cnt;
+} /* sctp_getaddrs() */
+
+/* Get all peer address on a socket.  This is a new SCTP API
+ * described in the section 8.3 of the Sockets API Extensions for SCTP.
+ * This is implemented using the getsockopt() interface.
+ */
+int
+sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **addrs)
+{
+	return sctp_getaddrs(sd, id,
+			     SCTP_GET_PEER_ADDRS,
+			     addrs);
+} /* sctp_getpaddrs() */
+
+/* Frees all resources allocated by sctp_getpaddrs().  This is a new SCTP API
+ * described in the section 8.4 of the Sockets API Extensions for SCTP.
+ */
+int
+sctp_freepaddrs(struct sockaddr *addrs)
+{
+	free(addrs);
+	return 0;
+
+} /* sctp_freepaddrs() */
+
+/* Get all locally bound address on a socket.  This is a new SCTP API
+ * described in the section 8.5 of the Sockets API Extensions for SCTP.
+ * This is implemented using the getsockopt() interface.
+ */
+int
+sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **addrs)
+{
+	return sctp_getaddrs(sd, id,
+			     SCTP_GET_LOCAL_ADDRS,
+			     addrs);
+} /* sctp_getladdrs() */
+
+/* Frees all resources allocated by sctp_getladdrs().  This is a new SCTP API
+ * described in the section 8.6 of the Sockets API Extensions for SCTP.
+ */
+int
+sctp_freeladdrs(struct sockaddr *addrs)
+{
+	free(addrs);
+	return 0;
+
+} /* sctp_freeladdrs() */
+
+int
+sctp_getaddrlen(sa_family_t family)
+{
+	/* We could call into the kernel to see what it thinks the size should
+	 * be, but hardcoding the address families here is: (a) faster,
+	 * (b) easier, and (c) probably good enough for forseeable future.
+	 */
+	switch(family) {
+	case AF_INET:
+		return sizeof(struct sockaddr_in);
+	case AF_INET6:
+		return sizeof(struct sockaddr_in6);
+	default:
+		/* Currently there is no defined error handling in
+		 * draft-ietf-tsvwg-sctpsocket-13.txt.
+		 * -1 might cause the application to overwrite buffer
+		 * or misinterpret data. 0 is more likely to cause
+		 * an endless loop.
+		 */
+		return 0;
+	}
+}
diff --git a/src/lib/bindx.c b/src/lib/bindx.c
new file mode 100644
index 0000000..5f44874
--- /dev/null
+++ b/src/lib/bindx.c
@@ -0,0 +1,78 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * bindx.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
+ *
+ * (C) Copyright IBM Corp. 2001, 2003
+ * Copyright (c) 2002 Intel Corporation.
+ *
+ * Written or modified by:
+ *   La Monte H.P. Yarroll <piggy@acm.org>
+ *   Daisy Chang           <daisyc@us.ibm.com>
+ *   Inaky Perez-Gonzalez  <inaky.gonzalez@intel.com>
+ *   Sridhar Samudrala     <sri@us.ibm.com>
+ */
+
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/in.h>
+#include <netinet/sctp.h> /* SCTP_SOCKOPT_BINDX_* */
+#include <errno.h>
+
+/* Support the sctp_bindx() interface.
+ *
+ * See Sockets API Extensions for SCTP. Section 8.1.
+ *
+ * Instead of implementing through a socket call in sys_socketcall(),
+ * tunnel the request through setsockopt().
+ */
+int
+sctp_bindx(int fd, struct sockaddr *addrs, int addrcnt, int flags)
+{
+	int setsock_option = 0;
+	const char *addrbuf;
+	struct sockaddr *sa_addr;
+	socklen_t addrs_size = 0;
+	int i;
+
+	switch (flags) {
+	case SCTP_BINDX_ADD_ADDR:
+		setsock_option = SCTP_SOCKOPT_BINDX_ADD;
+		break;
+	case SCTP_BINDX_REM_ADDR:
+		setsock_option = SCTP_SOCKOPT_BINDX_REM;
+		break;
+	default:
+		errno = EINVAL;
+		return -1;
+	}
+
+	addrbuf = (char*)addrs;
+	for (i = 0; i < addrcnt; i++) {
+		sa_addr = (struct sockaddr *)addrbuf;
+		switch (sa_addr->sa_family) {
+		case AF_INET:
+			addrs_size += sizeof(struct sockaddr_in);
+			addrbuf += sizeof(struct sockaddr_in);
+			break;
+		case AF_INET6:
+			addrs_size += sizeof(struct sockaddr_in6);
+			addrbuf += sizeof(struct sockaddr_in6);
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
+		}
+	}
+			
+	return setsockopt(fd, SOL_SCTP, setsock_option, addrs, addrs_size);
+}
diff --git a/src/lib/connectx.c b/src/lib/connectx.c
new file mode 100644
index 0000000..5f4552b
--- /dev/null
+++ b/src/lib/connectx.c
@@ -0,0 +1,190 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * connectx.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ * http://www.gnu.org/copyleft/lesser.txt.
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
+ *
+ * (C) Copyright IBM Corp. 2001, 2005
+ *
+ * Written or modified by:
+ *   Frank Filz     <ffilz@us.ibm.com>
+ */
+
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/in.h>
+#include <netinet/sctp.h> /* SCTP_SOCKOPT_CONNECTX_* */
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+
+/* Support the sctp_connectx() interface.
+ *
+ * See Sockets API Extensions for SCTP. Section 8.1.
+ *
+ * Instead of implementing through a socket call in sys_socketcall(),
+ * tunnel the request through setsockopt().
+ */
+static int __connectx_addrsize(const struct sockaddr *addrs,
+				const int addrcnt)
+{
+	const char *addrbuf;
+	const struct sockaddr *sa_addr;
+	int addrs_size = 0;
+	int i;
+
+	addrbuf = (char *)addrs;
+	for (i = 0; i < addrcnt; i++) {
+		sa_addr = (const struct sockaddr *)addrbuf;
+		switch (sa_addr->sa_family) {
+		case AF_INET:
+			addrs_size += sizeof(struct sockaddr_in);
+			addrbuf += sizeof(struct sockaddr_in);
+			break;
+		case AF_INET6:
+			addrs_size += sizeof(struct sockaddr_in6);
+			addrbuf += sizeof(struct sockaddr_in6);
+			break;
+		default:
+			errno = EINVAL;
+			return -1;
+		}
+	}
+
+	return addrs_size;
+}
+			
+
+int __sctp_connectx(int fd, struct sockaddr *addrs, int addrcnt)
+{
+	int addrs_size = __connectx_addrsize(addrs, addrcnt);
+
+	if (addrs_size < 0)
+		return addrs_size;
+
+	return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD, addrs,
+			    addrs_size);
+}
+
+extern int sctp_connectx_orig (int)
+	__attribute ((alias ("__sctp_connectx")));
+
+
+static int __connectx(int fd, struct sockaddr *addrs, socklen_t addrs_size,
+			sctp_assoc_t *id)
+{
+	int status;
+
+	if (id)
+		*id = 0;
+
+	status = setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX, addrs,
+			    addrs_size);
+
+	/* Normalize status and set association id */
+	if (status > 0) {
+		if (id)
+			*id = status;
+		return 0;
+	}
+
+	/* The error is something other then "Option not supported" */
+	if (status < 0 && errno != ENOPROTOOPT)
+		return status;
+
+	/* At this point, if the application wanted the id, we can't
+	 * really provide it, so we can return ENOPROTOOPT.
+	 */
+	if (id) {
+		errno = ENOPROTOOPT;
+		return -1;
+	}
+
+	/* Finally, try the old API */
+	return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD,
+			  addrs, addrs_size);
+}
+
+int sctp_connectx2(int fd, struct sockaddr *addrs, int addrcnt,
+		      sctp_assoc_t *id)
+{
+	int addrs_size = __connectx_addrsize(addrs, addrcnt);
+
+	if (addrs_size < 0)
+		return addrs_size;
+
+	return __connectx(fd, addrs, addrs_size, id);
+}
+
+int sctp_connectx3(int fd, struct sockaddr *addrs, int addrcnt,
+		      sctp_assoc_t *id)
+{
+	int addrs_size = __connectx_addrsize(addrs, addrcnt);
+	int status;
+	struct sctp_getaddrs_old param;
+	socklen_t opt_len = sizeof(param);
+
+	if (addrs_size < 0)
+		return addrs_size;
+
+	/* First try the new socket api
+	 * Because the id is returned in the option buffer we have prepend
+	 * 32bit to it for the returned association id
+	 */
+	param.assoc_id = 0;
+	param.addr_num = addrs_size;
+	param.addrs = addrs;
+	status = getsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX3,
+		            &param, &opt_len);
+	if (status == 0 || errno == EINPROGRESS) {
+		/* Succeeded immediately, or initiated on non-blocking
+		 * socket.
+		 */
+		if (id)
+			*id = param.assoc_id;
+	}
+
+	if (errno != ENOPROTOOPT) {
+		/* No point in trying the fallbacks*/
+		return status;
+	}
+
+	/* The first incarnation of updated connectx api didn't work for
+	 * non-blocking sockets.  So if the application wants the association
+	 * id and the socket is non-blocking, we can't really do anything.
+	 */
+	if (id) {
+		/* Program wants the association-id returned. We can only do
+		 * that if the socket is blocking */
+		status = fcntl(fd, F_GETFL);
+		if (status < 0)
+			return status;
+
+		if (status & O_NONBLOCK) {
+			/* Socket is non-blocking. Fail */
+			errno = ENOPROTOOPT;
+			return -1;
+		}
+	}
+
+	return __connectx(fd, addrs, addrs_size, id);
+}
+
+#define __SYMPFX(pfx, sym) #pfx sym
+#define _SYMPFX(pfx, sym) __SYMPFX(pfx, sym)
+#define SYMPFX(sym) _SYMPFX(__USER_LABEL_PREFIX__, #sym)
+#define SYMVER(name, name2) __asm__(".symver " SYMPFX(name) "," SYMPFX(name2))
+
+SYMVER(__sctp_connectx, sctp_connectx@);
+SYMVER(sctp_connectx_orig, sctp_connectx@VERS_1);
+SYMVER(sctp_connectx2, sctp_connectx@VERS_2);
+SYMVER(sctp_connectx3, sctp_connectx@@VERS_3);
diff --git a/src/lib/opt_info.c b/src/lib/opt_info.c
new file mode 100644
index 0000000..21c8307
--- /dev/null
+++ b/src/lib/opt_info.c
@@ -0,0 +1,62 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * opt_info.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code if to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
+ *
+ * (C) Copyright IBM Corp. 2003
+ * Copyright (c) 2002 Intel Corporation.
+ *
+ * Written or modified by:
+ *   Ardelle Fan <ardelle.fan@intel.com>
+ */
+
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/sctp.h> /* SCTP_SOCKOPT_BINDX_* */
+#include <errno.h>
+
+/* Support the sctp_opt_info() interface.
+ *
+ * See Sockets API Extensions for SCTP. Section 7.
+ *
+ * Pass sctp option information pass both in to and out of the SCTP stack.
+ * This is a new SCTP API described in the section 7 of the Sockets API
+ * Extensions for SCTP. This is implemented using the getsockopt() interface.
+ */
+int
+sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t *size)
+{
+	switch (opt) {
+	case SCTP_RTOINFO:
+	case SCTP_ASSOCINFO:
+	case SCTP_INITMSG:
+	case SCTP_NODELAY:
+	case SCTP_AUTOCLOSE:
+	case SCTP_PRIMARY_ADDR:
+	case SCTP_DISABLE_FRAGMENTS:
+	case SCTP_PEER_ADDR_PARAMS:
+	case SCTP_DEFAULT_SEND_PARAM:
+	case SCTP_EVENTS:
+	case SCTP_I_WANT_MAPPED_V4_ADDR:
+	case SCTP_MAXSEG:
+	case SCTP_STATUS:
+	case SCTP_GET_PEER_ADDR_INFO:
+	case SCTP_AUTH_ACTIVE_KEY:
+	case SCTP_PEER_AUTH_CHUNKS:
+	case SCTP_LOCAL_AUTH_CHUNKS:
+		*(sctp_assoc_t *)arg = id;
+		return getsockopt(sd, IPPROTO_SCTP, opt, arg, size);
+	default:
+		return ENOTSUP;
+	}
+
+} /* sctp_opt_info() */
diff --git a/src/lib/peeloff.c b/src/lib/peeloff.c
new file mode 100644
index 0000000..a4e1ed9
--- /dev/null
+++ b/src/lib/peeloff.c
@@ -0,0 +1,72 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * peeloff.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
+ *
+ * (C) Copyright IBM Corp. 2001, 2003
+ * 
+ * Written or modified by: 
+ *  Sridhar Samudrala     <sri@us.ibm.com>
+ */
+
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/sctp.h> /* SCTP_SOCKOPT_BINDX_* */
+#include <errno.h>
+
+#ifdef HAVE_SCTP_PEELOFF_FLAGS
+int
+sctp_peeloff_flags(int fd, sctp_assoc_t associd, unsigned flags)
+{
+	socklen_t peeloff_size = sizeof(sctp_peeloff_flags_arg_t);
+	sctp_peeloff_flags_arg_t peeloff;
+	int err;
+
+	if (!flags)
+		return sctp_peeloff(fd, associd);
+
+	peeloff.p_arg.associd = associd;
+	peeloff.p_arg.sd = 0;
+	peeloff.flags = flags;
+
+	err = getsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_PEELOFF_FLAGS, &peeloff,
+			 &peeloff_size);
+
+	if (err < 0)
+		return err;
+
+	return peeloff.p_arg.sd;
+}
+#endif
+
+/* Branch off an association into a seperate socket.  This is a new SCTP API
+ * described in the section 8.2 of the Sockets API Extensions for SCTP. 
+ * This is implemented using the getsockopt() interface.
+ */
+int
+sctp_peeloff(int fd, sctp_assoc_t associd)
+{
+	socklen_t peeloff_size = sizeof(sctp_peeloff_arg_t);
+	sctp_peeloff_arg_t peeloff;
+	int err;
+
+	peeloff.associd = associd;
+	peeloff.sd = 0;
+
+	err = getsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_PEELOFF, &peeloff,
+			 &peeloff_size);
+
+	if (err < 0)
+		return err;
+
+	return peeloff.sd;
+}
diff --git a/src/lib/recvmsg.c b/src/lib/recvmsg.c
new file mode 100644
index 0000000..88fe061
--- /dev/null
+++ b/src/lib/recvmsg.c
@@ -0,0 +1,191 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * sctp_recvmsg.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
+ *
+ * Copyright (c) 2003 International Business Machines, Corp.
+ *
+ * Written or modified by:
+ *  Ryan Layer	<rmlayer@us.ibm.com>
+ *
+ * An implementation may provide a library function (or possibly system
+ * call) to assist the user with the advanced features of SCTP. Note
+ * that in order for the sctp_sndrcvinfo structure to be filled in by
+ * sctp_recvmsg() the caller must enable the sctp_data_io_events with
+ * the SCTP_EVENTS option.
+ * 
+ * sctp_recvmsg(). Its syntax is,
+ * 
+ * int sctp_recvmsg(int s,
+ *		    void *msg,
+ *		    size_t len,
+ *		    struct sockaddr *from,
+ *		    socklen_t *fromlen,
+ *		    struct sctp_sndrcvinfo *sinfo,
+ *		    int *msg_flags)
+ * 
+ * 
+ * s          - is the socket descriptor
+ * msg        - is a message buffer to be filled.
+ * len        - is the length of the message buffer.
+ * from       - is a pointer to a address to be filled with
+ *		the sender of this messages address.
+ * fromlen    - is the from length.
+ * sinfo      - A pointer to a sctp_sndrcvinfo structure
+ *		to be filled upon receipt of the message.
+ * msg_flags  - A pointer to a integer to be filled with
+ *		any message flags (e.g. MSG_NOTIFICATION).
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/sctp.h>
+
+int sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
+		 socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
+		 int *msg_flags)
+{
+	int error;
+	struct iovec iov;
+	struct msghdr inmsg;
+	char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
+	struct cmsghdr *cmsg = NULL;
+
+	memset(&inmsg, 0, sizeof (inmsg));
+
+	iov.iov_base = msg;
+	iov.iov_len = len;
+
+	inmsg.msg_name = from;
+	inmsg.msg_namelen = fromlen ? *fromlen : 0;
+	inmsg.msg_iov = &iov;
+	inmsg.msg_iovlen = 1;
+	inmsg.msg_control = incmsg;
+	inmsg.msg_controllen = sizeof(incmsg);
+
+	error = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0);
+	if (error < 0)
+		return error;
+
+	if (fromlen)
+		*fromlen = inmsg.msg_namelen;
+	if (msg_flags)
+		*msg_flags = inmsg.msg_flags;
+
+	if (!sinfo)
+		return error;
+
+	for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
+				 cmsg = CMSG_NXTHDR(&inmsg, cmsg)){
+		if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
+		    (SCTP_SNDRCV == cmsg->cmsg_type))
+			break;
+	}
+
+        /* Copy sinfo. */
+	if (cmsg)
+		memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
+
+	return (error);
+}
+
+#ifdef HAVE_SCTP_SENDV
+int sctp_recvv(int s, const struct iovec *iov, int iovlen,
+	       struct sockaddr *from, socklen_t *fromlen, void *info,
+	       socklen_t *infolen, unsigned int *infotype, int *flags)
+{
+	char incmsg[CMSG_SPACE(sizeof(struct sctp_rcvinfo) +
+			       sizeof(struct sctp_nxtinfo))];
+	int error, len, _infolen;
+	struct cmsghdr *cmsg;
+	struct msghdr inmsg;
+
+	memset(&inmsg, 0, sizeof(inmsg));
+
+	/* set from and iov */
+	inmsg.msg_name = from;
+	inmsg.msg_namelen = fromlen ? *fromlen : 0;
+	inmsg.msg_iov = (struct iovec *)iov;
+	inmsg.msg_iovlen = iovlen;
+	inmsg.msg_control = incmsg;
+	inmsg.msg_controllen = sizeof(incmsg);
+
+	error = recvmsg(s, &inmsg, flags ? *flags : 0);
+	if (error < 0)
+		return error;
+
+	/* set fromlen, frags */
+	if (fromlen)
+		*fromlen = inmsg.msg_namelen;
+
+	if (flags)
+		*flags = inmsg.msg_flags;
+
+	if (!info || !infotype || !infolen)
+		return error;
+
+	*infotype = SCTP_RECVV_NOINFO;
+	_infolen = *infolen;
+
+	/* set info and infotype */
+	for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
+				cmsg = CMSG_NXTHDR(&inmsg, cmsg)) {
+		if (cmsg->cmsg_level != IPPROTO_SCTP)
+			continue;
+
+		if (cmsg->cmsg_type == SCTP_RCVINFO) {
+			len = sizeof(struct sctp_rcvinfo);
+			if (*infotype == SCTP_RECVV_NOINFO) {
+				if (_infolen < len)
+					break;
+				memcpy(info, CMSG_DATA(cmsg), len);
+				*infotype = SCTP_RECVV_RCVINFO;
+				*infolen = len;
+			} else if (*infotype == SCTP_RECVV_NXTINFO) {
+				if (_infolen < len +
+					       sizeof(struct sctp_nxtinfo))
+					break;
+				memcpy(info + len, info,
+				       sizeof(struct sctp_nxtinfo));
+				memcpy(info, CMSG_DATA(cmsg), len);
+				*infotype = SCTP_RECVV_RN;
+				*infolen = len + sizeof(struct sctp_nxtinfo);
+			} else {
+				break;
+			}
+		} else if (cmsg->cmsg_type == SCTP_NXTINFO) {
+			len = sizeof(struct sctp_nxtinfo);
+			if (*infotype == SCTP_RECVV_NOINFO) {
+				if (_infolen < len)
+					break;
+				memcpy(info, CMSG_DATA(cmsg), len);
+				*infotype = SCTP_RECVV_NXTINFO;
+				*infolen = len;
+			} else if (*infotype == SCTP_RECVV_RCVINFO) {
+				if (_infolen < len +
+					       sizeof(struct sctp_rcvinfo))
+					break;
+				memcpy(info + sizeof(struct sctp_rcvinfo),
+				       CMSG_DATA(cmsg), len);
+				*infotype = SCTP_RECVV_RN;
+				*infolen = len + sizeof(struct sctp_rcvinfo);
+			} else {
+				break;
+			}
+		}
+	}
+
+	return error;
+}
+#endif
diff --git a/src/lib/sendmsg.c b/src/lib/sendmsg.c
new file mode 100644
index 0000000..4c90b07
--- /dev/null
+++ b/src/lib/sendmsg.c
@@ -0,0 +1,239 @@
+/* SCTP kernel Implementation: User API extensions.
+ *
+ * sendmsg.c
+ *
+ * Distributed under the terms of the LGPL v2.1 as described in
+ *    http://www.gnu.org/copyleft/lesser.txt 
+ *
+ * This file is part of the user library that offers support for the
+ * SCTP kernel Implementation. The main purpose of this
+ * code is to provide the SCTP Socket API mappings for user
+ * application to interface with the SCTP in kernel.
+ *
+ * This implementation is based on the Socket API Extensions for SCTP
+ * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
+ *
+ * Copyright (c) 2003 Intel Corp.
+ *
+ * Written or modified by:
+ *  Ardelle Fan     <ardelle.fan@intel.com>
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <netinet/in.h>
+#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
+#include <netinet/sctp.h>
+
+/* This library function assists the user with the advanced features
+ * of SCTP.  This is a new SCTP API described in the section 8.7 of the
+ * Sockets API Extensions for SCTP. This is implemented using the
+ * sendmsg() interface.
+ */
+int
+sctp_sendmsg(int s, const void *msg, size_t len, struct sockaddr *to,
+	     socklen_t tolen, uint32_t ppid, uint32_t flags,
+	     uint16_t stream_no, uint32_t timetolive, uint32_t context)
+{
+	struct msghdr outmsg;
+	struct iovec iov;
+	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
+	struct cmsghdr *cmsg;
+	struct sctp_sndrcvinfo *sinfo;
+
+	outmsg.msg_name = to;
+	outmsg.msg_namelen = tolen;
+	outmsg.msg_iov = &iov;
+	iov.iov_base = (void *)msg;
+	iov.iov_len = len;
+	outmsg.msg_iovlen = 1;
+
+	outmsg.msg_control = outcmsg;
+	outmsg.msg_controllen = sizeof(outcmsg);
+	outmsg.msg_flags = 0;
+
+	cmsg = CMSG_FIRSTHDR(&outmsg);
+	cmsg->cmsg_level = IPPROTO_SCTP;
+	cmsg->cmsg_type = SCTP_SNDRCV;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
+
+	outmsg.msg_controllen = cmsg->cmsg_len;
+	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
+	memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
+	sinfo->sinfo_ppid = ppid;
+	sinfo->sinfo_flags = flags;
+	sinfo->sinfo_stream = stream_no;
+	sinfo->sinfo_timetolive = timetolive;
+	sinfo->sinfo_context = context;
+
+	return sendmsg(s, &outmsg, 0);
+}
+
+/* This library function assist the user with sending a message without
+ * dealing directly with the CMSG header.
+ */
+int
+sctp_send(int s, const void *msg, size_t len,
+          const struct sctp_sndrcvinfo *sinfo, int flags)
+{
+	struct msghdr outmsg;
+	struct iovec iov;
+	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
+
+	outmsg.msg_name = NULL;
+	outmsg.msg_namelen = 0;
+	outmsg.msg_iov = &iov;
+	iov.iov_base = (void *)msg;
+	iov.iov_len = len;
+	outmsg.msg_iovlen = 1;
+	outmsg.msg_control = NULL;
+	outmsg.msg_controllen = 0;
+
+	if (sinfo) {	
+		struct cmsghdr *cmsg;
+
+		outmsg.msg_control = outcmsg;
+		outmsg.msg_controllen = sizeof(outcmsg);
+		outmsg.msg_flags = 0;
+
+		cmsg = CMSG_FIRSTHDR(&outmsg);
+		cmsg->cmsg_level = IPPROTO_SCTP;
+		cmsg->cmsg_type = SCTP_SNDRCV;
+		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
+
+		outmsg.msg_controllen = cmsg->cmsg_len;
+		memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
+	}
+
+	return sendmsg(s, &outmsg, flags);
+}
+
+#ifdef HAVE_SCTP_SENDV
+static struct cmsghdr *sctp_sendv_store_cmsg(struct cmsghdr *cmsg, int *cmsglen,
+					     int type, int len, void *data)
+{
+	cmsg->cmsg_level = IPPROTO_SCTP;
+	cmsg->cmsg_type = type;
+	cmsg->cmsg_len = CMSG_LEN(len);
+	memcpy(CMSG_DATA(cmsg), data, len);
+
+	*cmsglen += CMSG_SPACE(len);
+
+	return (struct cmsghdr *)((char *)cmsg + CMSG_SPACE(len));
+}
+
+int sctp_sendv(int s, const struct iovec *iov, int iovcnt,
+	       struct sockaddr *addrs, int addrcnt, void *info,
+	       socklen_t infolen, unsigned int infotype, int flags)
+{
+	char _cmsg[CMSG_SPACE(sizeof(struct sctp_sendv_spa))];
+	struct cmsghdr *cmsg = (struct cmsghdr *)_cmsg;
+	struct sockaddr *addr;
+	struct msghdr outmsg;
+	int len, cmsglen = 0;
+	int err, type, i;
+	char *addrbuf;
+
+	/* set msg_iov, msg_iovlen, msg_flags */
+	memset(&outmsg, 0x00, sizeof(outmsg));
+	outmsg.msg_iov = (struct iovec *)iov;
+	outmsg.msg_iovlen = iovcnt;
+	outmsg.msg_flags = flags;
+
+	/* set msg_name and msg_namelen */
+	if (addrs && addrcnt) {
+		outmsg.msg_name = addrs;
+		if (addrs->sa_family == AF_INET)
+			outmsg.msg_namelen = sizeof(struct sockaddr_in);
+		else if (addrs->sa_family == AF_INET6)
+			outmsg.msg_namelen = sizeof(struct sockaddr_in6);
+		else
+			return -EINVAL;
+		addrcnt -= 1;
+		addrbuf = (char *)addrs;
+		addrs = (struct sockaddr *)(addrbuf + outmsg.msg_namelen);
+	}
+
+	/* alloc memory only when it's multi-address */
+	if (addrcnt) {
+		len = CMSG_SPACE(sizeof(struct sockaddr_in6)) * addrcnt;
+		cmsg = malloc(sizeof(_cmsg) + len);
+		if (!cmsg)
+			return -ENOMEM;
+	}
+
+	outmsg.msg_control = cmsg;
+
+	/* add cmsg info for addr info */
+	for (i = 0, addrbuf = (char *)addrs; i < addrcnt; i++) {
+		void *ainfo;
+
+		addr = (struct sockaddr *)addrbuf;
+		if (addr->sa_family == AF_INET) {
+			struct sockaddr_in *a = (struct sockaddr_in *)addrbuf;
+
+			len = sizeof(struct in_addr);
+			type = SCTP_DSTADDRV4;
+			ainfo = &a->sin_addr;
+			addrbuf += sizeof(*a);
+		} else if (addr->sa_family == AF_INET6) {
+			struct sockaddr_in6 *a = (struct sockaddr_in6 *)addrbuf;
+
+			len = sizeof(struct in6_addr);
+			type = SCTP_DSTADDRV6;
+			ainfo = &a->sin6_addr;
+			addrbuf += sizeof(*a);
+		} else {
+			free(outmsg.msg_control);
+			return -EINVAL;
+		}
+
+		cmsg = sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len, ainfo);
+	}
+	/* add cmsg info for addr info for snd/pr/auth info */
+	if (infotype == SCTP_SENDV_SPA) {
+		struct sctp_sendv_spa *spa = info;
+
+		if (spa->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
+			type = SCTP_SNDINFO;
+			len = sizeof(struct sctp_sndinfo);
+			cmsg = sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len,
+						     &spa->sendv_sndinfo);
+		}
+		if (spa->sendv_flags & SCTP_SEND_PRINFO_VALID) {
+			type = SCTP_PRINFO;
+			len = sizeof(struct sctp_prinfo);
+			cmsg = sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len,
+						     &spa->sendv_prinfo);
+		}
+		if (spa->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
+			type = SCTP_AUTHINFO;
+			len = sizeof(struct sctp_authinfo);
+			sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len,
+					      &spa->sendv_authinfo);
+		}
+	} else if (infotype == SCTP_SENDV_SNDINFO) {
+		type = SCTP_SNDINFO;
+		len = sizeof(struct sctp_sndinfo);
+		sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len, info);
+	} else if (infotype == SCTP_SENDV_PRINFO) {
+		type = SCTP_PRINFO;
+		len = sizeof(struct sctp_prinfo);
+		sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len, info);
+	} else if (infotype == SCTP_SENDV_AUTHINFO) {
+		type = SCTP_AUTHINFO;
+		len = sizeof(struct sctp_authinfo);
+		sctp_sendv_store_cmsg(cmsg, &cmsglen, type, len, info);
+	}
+
+	outmsg.msg_controllen = cmsglen;
+
+	err = sendmsg(s, &outmsg, 0);
+
+	if (outmsg.msg_control != _cmsg)
+		free(outmsg.msg_control);
+
+	return err;
+}
+#endif