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