1*49cdfc7eSAndroid Build Coastguard Worker /* SCTP kernel Implementation: User API extensions.
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * sctp_recvmsg.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 International Business Machines, Corp.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * Written or modified by:
19*49cdfc7eSAndroid Build Coastguard Worker * Ryan Layer <[email protected]>
20*49cdfc7eSAndroid Build Coastguard Worker *
21*49cdfc7eSAndroid Build Coastguard Worker * An implementation may provide a library function (or possibly system
22*49cdfc7eSAndroid Build Coastguard Worker * call) to assist the user with the advanced features of SCTP. Note
23*49cdfc7eSAndroid Build Coastguard Worker * that in order for the sctp_sndrcvinfo structure to be filled in by
24*49cdfc7eSAndroid Build Coastguard Worker * sctp_recvmsg() the caller must enable the sctp_data_io_events with
25*49cdfc7eSAndroid Build Coastguard Worker * the SCTP_EVENTS option.
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * sctp_recvmsg(). Its syntax is,
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * int sctp_recvmsg(int s,
30*49cdfc7eSAndroid Build Coastguard Worker * void *msg,
31*49cdfc7eSAndroid Build Coastguard Worker * size_t len,
32*49cdfc7eSAndroid Build Coastguard Worker * struct sockaddr *from,
33*49cdfc7eSAndroid Build Coastguard Worker * socklen_t *fromlen,
34*49cdfc7eSAndroid Build Coastguard Worker * struct sctp_sndrcvinfo *sinfo,
35*49cdfc7eSAndroid Build Coastguard Worker * int *msg_flags)
36*49cdfc7eSAndroid Build Coastguard Worker *
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * s - is the socket descriptor
39*49cdfc7eSAndroid Build Coastguard Worker * msg - is a message buffer to be filled.
40*49cdfc7eSAndroid Build Coastguard Worker * len - is the length of the message buffer.
41*49cdfc7eSAndroid Build Coastguard Worker * from - is a pointer to a address to be filled with
42*49cdfc7eSAndroid Build Coastguard Worker * the sender of this messages address.
43*49cdfc7eSAndroid Build Coastguard Worker * fromlen - is the from length.
44*49cdfc7eSAndroid Build Coastguard Worker * sinfo - A pointer to a sctp_sndrcvinfo structure
45*49cdfc7eSAndroid Build Coastguard Worker * to be filled upon receipt of the message.
46*49cdfc7eSAndroid Build Coastguard Worker * msg_flags - A pointer to a integer to be filled with
47*49cdfc7eSAndroid Build Coastguard Worker * any message flags (e.g. MSG_NOTIFICATION).
48*49cdfc7eSAndroid Build Coastguard Worker */
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h> /* struct sockaddr_storage, setsockopt() */
53*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/sctp.h>
54*49cdfc7eSAndroid Build Coastguard Worker
sctp_recvmsg(int s,void * msg,size_t len,struct sockaddr * from,socklen_t * fromlen,struct sctp_sndrcvinfo * sinfo,int * msg_flags)55*49cdfc7eSAndroid Build Coastguard Worker int sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
56*49cdfc7eSAndroid Build Coastguard Worker socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo,
57*49cdfc7eSAndroid Build Coastguard Worker int *msg_flags)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker int error;
60*49cdfc7eSAndroid Build Coastguard Worker struct iovec iov;
61*49cdfc7eSAndroid Build Coastguard Worker struct msghdr inmsg;
62*49cdfc7eSAndroid Build Coastguard Worker char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
63*49cdfc7eSAndroid Build Coastguard Worker struct cmsghdr *cmsg = NULL;
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker memset(&inmsg, 0, sizeof (inmsg));
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker iov.iov_base = msg;
68*49cdfc7eSAndroid Build Coastguard Worker iov.iov_len = len;
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_name = from;
71*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_namelen = fromlen ? *fromlen : 0;
72*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_iov = &iov;
73*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_iovlen = 1;
74*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_control = incmsg;
75*49cdfc7eSAndroid Build Coastguard Worker inmsg.msg_controllen = sizeof(incmsg);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker error = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0);
78*49cdfc7eSAndroid Build Coastguard Worker if (error < 0)
79*49cdfc7eSAndroid Build Coastguard Worker return error;
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker if (fromlen)
82*49cdfc7eSAndroid Build Coastguard Worker *fromlen = inmsg.msg_namelen;
83*49cdfc7eSAndroid Build Coastguard Worker if (msg_flags)
84*49cdfc7eSAndroid Build Coastguard Worker *msg_flags = inmsg.msg_flags;
85*49cdfc7eSAndroid Build Coastguard Worker
86*49cdfc7eSAndroid Build Coastguard Worker if (!sinfo)
87*49cdfc7eSAndroid Build Coastguard Worker return error;
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL;
90*49cdfc7eSAndroid Build Coastguard Worker cmsg = CMSG_NXTHDR(&inmsg, cmsg)){
91*49cdfc7eSAndroid Build Coastguard Worker if ((IPPROTO_SCTP == cmsg->cmsg_level) &&
92*49cdfc7eSAndroid Build Coastguard Worker (SCTP_SNDRCV == cmsg->cmsg_type))
93*49cdfc7eSAndroid Build Coastguard Worker break;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker /* Copy sinfo. */
97*49cdfc7eSAndroid Build Coastguard Worker if (cmsg)
98*49cdfc7eSAndroid Build Coastguard Worker memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker return (error);
101*49cdfc7eSAndroid Build Coastguard Worker }
102