Imported Upstream version 1.0.10
[platform/upstream/lksctp-tools.git] / src / lib / sendmsg.c
1 /* SCTP kernel Implementation: User API extensions.
2  *
3  * sendmsg.c
4  *
5  * Distributed under the terms of the LGPL v2.1 as described in
6  *    http://www.gnu.org/copyleft/lesser.txt 
7  *
8  * This file is part of the user library that offers support for the
9  * SCTP kernel Implementation. The main purpose of this
10  * code is to provide the SCTP Socket API mappings for user
11  * application to interface with the SCTP in kernel.
12  *
13  * This implementation is based on the Socket API Extensions for SCTP
14  * defined in <draft-ietf-tsvwg-sctpsocket-10.txt>
15  *
16  * Copyright (c) 2003 Intel Corp.
17  *
18  * Written or modified by:
19  *  Ardelle Fan     <ardelle.fan@intel.com>
20  */
21
22 #include <string.h>
23 #include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
24 #include <netinet/sctp.h>
25
26 /* This library function assists the user with the advanced features
27  * of SCTP.  This is a new SCTP API described in the section 8.7 of the
28  * Sockets API Extensions for SCTP. This is implemented using the
29  * sendmsg() interface.
30  */
31 int
32 sctp_sendmsg(int s, const void *msg, size_t len, struct sockaddr *to,
33              socklen_t tolen, uint32_t ppid, uint32_t flags,
34              uint16_t stream_no, uint32_t timetolive, uint32_t context)
35 {
36         struct msghdr outmsg;
37         struct iovec iov;
38         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
39         struct cmsghdr *cmsg;
40         struct sctp_sndrcvinfo *sinfo;
41
42         outmsg.msg_name = to;
43         outmsg.msg_namelen = tolen;
44         outmsg.msg_iov = &iov;
45         iov.iov_base = (void *)msg;
46         iov.iov_len = len;
47         outmsg.msg_iovlen = 1;
48
49         outmsg.msg_control = outcmsg;
50         outmsg.msg_controllen = sizeof(outcmsg);
51         outmsg.msg_flags = 0;
52
53         cmsg = CMSG_FIRSTHDR(&outmsg);
54         cmsg->cmsg_level = IPPROTO_SCTP;
55         cmsg->cmsg_type = SCTP_SNDRCV;
56         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
57
58         outmsg.msg_controllen = cmsg->cmsg_len;
59         sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
60         memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
61         sinfo->sinfo_ppid = ppid;
62         sinfo->sinfo_flags = flags;
63         sinfo->sinfo_stream = stream_no;
64         sinfo->sinfo_timetolive = timetolive;
65         sinfo->sinfo_context = context;
66
67         return sendmsg(s, &outmsg, 0);
68 }
69
70 /* This library function assist the user with sending a message without
71  * dealing directly with the CMSG header.
72  */
73 int
74 sctp_send(int s, const void *msg, size_t len,
75           const struct sctp_sndrcvinfo *sinfo, int flags)
76 {
77         struct msghdr outmsg;
78         struct iovec iov;
79
80         outmsg.msg_name = NULL;
81         outmsg.msg_namelen = 0;
82         outmsg.msg_iov = &iov;
83         iov.iov_base = (void *)msg;
84         iov.iov_len = len;
85         outmsg.msg_iovlen = 1;
86         outmsg.msg_controllen = 0;
87
88         if (sinfo) {    
89                 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
90                 struct cmsghdr *cmsg;
91
92                 outmsg.msg_control = outcmsg;
93                 outmsg.msg_controllen = sizeof(outcmsg);
94                 outmsg.msg_flags = 0;
95
96                 cmsg = CMSG_FIRSTHDR(&outmsg);
97                 cmsg->cmsg_level = IPPROTO_SCTP;
98                 cmsg->cmsg_type = SCTP_SNDRCV;
99                 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
100
101                 outmsg.msg_controllen = cmsg->cmsg_len;
102                 memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
103         }
104
105         return sendmsg(s, &outmsg, flags);
106 }