1f08c3bdfSopenharmony_ci/* SCTP kernel Implementation: User API extensions. 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * sctp_recvmsg.c 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * Distributed under the terms of the LGPL v2.1 as described in 6f08c3bdfSopenharmony_ci * http://www.gnu.org/copyleft/lesser.txt 7f08c3bdfSopenharmony_ci * 8f08c3bdfSopenharmony_ci * This file is part of the user library that offers support for the 9f08c3bdfSopenharmony_ci * SCTP kernel Implementation. The main purpose of this 10f08c3bdfSopenharmony_ci * code is to provide the SCTP Socket API mappings for user 11f08c3bdfSopenharmony_ci * application to interface with the SCTP in kernel. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * This implementation is based on the Socket API Extensions for SCTP 14f08c3bdfSopenharmony_ci * defined in <draft-ietf-tsvwg-sctpsocket-10.txt> 15f08c3bdfSopenharmony_ci * 16f08c3bdfSopenharmony_ci * Copyright (c) 2003 International Business Machines, Corp. 17f08c3bdfSopenharmony_ci * 18f08c3bdfSopenharmony_ci * Written or modified by: 19f08c3bdfSopenharmony_ci * Ryan Layer <rmlayer@us.ibm.com> 20f08c3bdfSopenharmony_ci * 21f08c3bdfSopenharmony_ci * An implementation may provide a library function (or possibly system 22f08c3bdfSopenharmony_ci * call) to assist the user with the advanced features of SCTP. Note 23f08c3bdfSopenharmony_ci * that in order for the sctp_sndrcvinfo structure to be filled in by 24f08c3bdfSopenharmony_ci * sctp_recvmsg() the caller must enable the sctp_data_io_events with 25f08c3bdfSopenharmony_ci * the SCTP_EVENTS option. 26f08c3bdfSopenharmony_ci * 27f08c3bdfSopenharmony_ci * sctp_recvmsg(). Its syntax is, 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * int sctp_recvmsg(int s, 30f08c3bdfSopenharmony_ci * void *msg, 31f08c3bdfSopenharmony_ci * size_t len, 32f08c3bdfSopenharmony_ci * struct sockaddr *from, 33f08c3bdfSopenharmony_ci * socklen_t *fromlen, 34f08c3bdfSopenharmony_ci * struct sctp_sndrcvinfo *sinfo, 35f08c3bdfSopenharmony_ci * int *msg_flags) 36f08c3bdfSopenharmony_ci * 37f08c3bdfSopenharmony_ci * 38f08c3bdfSopenharmony_ci * s - is the socket descriptor 39f08c3bdfSopenharmony_ci * msg - is a message buffer to be filled. 40f08c3bdfSopenharmony_ci * len - is the length of the message buffer. 41f08c3bdfSopenharmony_ci * from - is a pointer to a address to be filled with 42f08c3bdfSopenharmony_ci * the sender of this messages address. 43f08c3bdfSopenharmony_ci * fromlen - is the from length. 44f08c3bdfSopenharmony_ci * sinfo - A pointer to a sctp_sndrcvinfo structure 45f08c3bdfSopenharmony_ci * to be filled upon receipt of the message. 46f08c3bdfSopenharmony_ci * msg_flags - A pointer to a integer to be filled with 47f08c3bdfSopenharmony_ci * any message flags (e.g. MSG_NOTIFICATION). 48f08c3bdfSopenharmony_ci */ 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci#include <string.h> 51f08c3bdfSopenharmony_ci#include <errno.h> 52f08c3bdfSopenharmony_ci#include <sys/socket.h> /* struct sockaddr_storage, setsockopt() */ 53f08c3bdfSopenharmony_ci#include <netinet/sctp.h> 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ciint sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from, 56f08c3bdfSopenharmony_ci socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo, 57f08c3bdfSopenharmony_ci int *msg_flags) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci int error; 60f08c3bdfSopenharmony_ci struct iovec iov; 61f08c3bdfSopenharmony_ci struct msghdr inmsg; 62f08c3bdfSopenharmony_ci char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 63f08c3bdfSopenharmony_ci struct cmsghdr *cmsg = NULL; 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci memset(&inmsg, 0, sizeof (inmsg)); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci iov.iov_base = msg; 68f08c3bdfSopenharmony_ci iov.iov_len = len; 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci inmsg.msg_name = from; 71f08c3bdfSopenharmony_ci inmsg.msg_namelen = fromlen ? *fromlen : 0; 72f08c3bdfSopenharmony_ci inmsg.msg_iov = &iov; 73f08c3bdfSopenharmony_ci inmsg.msg_iovlen = 1; 74f08c3bdfSopenharmony_ci inmsg.msg_control = incmsg; 75f08c3bdfSopenharmony_ci inmsg.msg_controllen = sizeof(incmsg); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci error = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0); 78f08c3bdfSopenharmony_ci if (error < 0) 79f08c3bdfSopenharmony_ci return error; 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci if (fromlen) 82f08c3bdfSopenharmony_ci *fromlen = inmsg.msg_namelen; 83f08c3bdfSopenharmony_ci if (msg_flags) 84f08c3bdfSopenharmony_ci *msg_flags = inmsg.msg_flags; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci if (!sinfo) 87f08c3bdfSopenharmony_ci return error; 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL; 90f08c3bdfSopenharmony_ci cmsg = CMSG_NXTHDR(&inmsg, cmsg)){ 91f08c3bdfSopenharmony_ci if ((IPPROTO_SCTP == cmsg->cmsg_level) && 92f08c3bdfSopenharmony_ci (SCTP_SNDRCV == cmsg->cmsg_type)) 93f08c3bdfSopenharmony_ci break; 94f08c3bdfSopenharmony_ci } 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci /* Copy sinfo. */ 97f08c3bdfSopenharmony_ci if (cmsg) 98f08c3bdfSopenharmony_ci memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci return (error); 101f08c3bdfSopenharmony_ci} 102