Austin Schuh | 8d0a285 | 2019-12-28 22:54:28 -0800 | [diff] [blame^] | 1 | /* SCTP kernel Implementation: User API extensions. |
| 2 | * |
| 3 | * sctp_recvmsg.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 International Business Machines, Corp. |
| 17 | * |
| 18 | * Written or modified by: |
| 19 | * Ryan Layer <rmlayer@us.ibm.com> |
| 20 | * |
| 21 | * An implementation may provide a library function (or possibly system |
| 22 | * call) to assist the user with the advanced features of SCTP. Note |
| 23 | * that in order for the sctp_sndrcvinfo structure to be filled in by |
| 24 | * sctp_recvmsg() the caller must enable the sctp_data_io_events with |
| 25 | * the SCTP_EVENTS option. |
| 26 | * |
| 27 | * sctp_recvmsg(). Its syntax is, |
| 28 | * |
| 29 | * int sctp_recvmsg(int s, |
| 30 | * void *msg, |
| 31 | * size_t len, |
| 32 | * struct sockaddr *from, |
| 33 | * socklen_t *fromlen, |
| 34 | * struct sctp_sndrcvinfo *sinfo, |
| 35 | * int *msg_flags) |
| 36 | * |
| 37 | * |
| 38 | * s - is the socket descriptor |
| 39 | * msg - is a message buffer to be filled. |
| 40 | * len - is the length of the message buffer. |
| 41 | * from - is a pointer to a address to be filled with |
| 42 | * the sender of this messages address. |
| 43 | * fromlen - is the from length. |
| 44 | * sinfo - A pointer to a sctp_sndrcvinfo structure |
| 45 | * to be filled upon receipt of the message. |
| 46 | * msg_flags - A pointer to a integer to be filled with |
| 47 | * any message flags (e.g. MSG_NOTIFICATION). |
| 48 | */ |
| 49 | |
| 50 | #include <string.h> |
| 51 | #include <errno.h> |
| 52 | #include <sys/socket.h> /* struct sockaddr_storage, setsockopt() */ |
| 53 | #include <netinet/sctp.h> |
| 54 | |
| 55 | int sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from, |
| 56 | socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo, |
| 57 | int *msg_flags) |
| 58 | { |
| 59 | int error; |
| 60 | struct iovec iov; |
| 61 | struct msghdr inmsg; |
| 62 | char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 63 | struct cmsghdr *cmsg = NULL; |
| 64 | |
| 65 | memset(&inmsg, 0, sizeof (inmsg)); |
| 66 | |
| 67 | iov.iov_base = msg; |
| 68 | iov.iov_len = len; |
| 69 | |
| 70 | inmsg.msg_name = from; |
| 71 | inmsg.msg_namelen = fromlen ? *fromlen : 0; |
| 72 | inmsg.msg_iov = &iov; |
| 73 | inmsg.msg_iovlen = 1; |
| 74 | inmsg.msg_control = incmsg; |
| 75 | inmsg.msg_controllen = sizeof(incmsg); |
| 76 | |
| 77 | error = recvmsg(s, &inmsg, msg_flags ? *msg_flags : 0); |
| 78 | if (error < 0) |
| 79 | return error; |
| 80 | |
| 81 | if (fromlen) |
| 82 | *fromlen = inmsg.msg_namelen; |
| 83 | if (msg_flags) |
| 84 | *msg_flags = inmsg.msg_flags; |
| 85 | |
| 86 | if (!sinfo) |
| 87 | return error; |
| 88 | |
| 89 | for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL; |
| 90 | cmsg = CMSG_NXTHDR(&inmsg, cmsg)){ |
| 91 | if ((IPPROTO_SCTP == cmsg->cmsg_level) && |
| 92 | (SCTP_SNDRCV == cmsg->cmsg_type)) |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Copy sinfo. */ |
| 97 | if (cmsg) |
| 98 | memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); |
| 99 | |
| 100 | return (error); |
| 101 | } |
| 102 | |
| 103 | #ifdef HAVE_SCTP_SENDV |
| 104 | int sctp_recvv(int s, const struct iovec *iov, int iovlen, |
| 105 | struct sockaddr *from, socklen_t *fromlen, void *info, |
| 106 | socklen_t *infolen, unsigned int *infotype, int *flags) |
| 107 | { |
| 108 | char incmsg[CMSG_SPACE(sizeof(struct sctp_rcvinfo) + |
| 109 | sizeof(struct sctp_nxtinfo))]; |
| 110 | int error, len, _infolen; |
| 111 | struct cmsghdr *cmsg; |
| 112 | struct msghdr inmsg; |
| 113 | |
| 114 | memset(&inmsg, 0, sizeof(inmsg)); |
| 115 | |
| 116 | /* set from and iov */ |
| 117 | inmsg.msg_name = from; |
| 118 | inmsg.msg_namelen = fromlen ? *fromlen : 0; |
| 119 | inmsg.msg_iov = (struct iovec *)iov; |
| 120 | inmsg.msg_iovlen = iovlen; |
| 121 | inmsg.msg_control = incmsg; |
| 122 | inmsg.msg_controllen = sizeof(incmsg); |
| 123 | |
| 124 | error = recvmsg(s, &inmsg, flags ? *flags : 0); |
| 125 | if (error < 0) |
| 126 | return error; |
| 127 | |
| 128 | /* set fromlen, frags */ |
| 129 | if (fromlen) |
| 130 | *fromlen = inmsg.msg_namelen; |
| 131 | |
| 132 | if (flags) |
| 133 | *flags = inmsg.msg_flags; |
| 134 | |
| 135 | if (!info || !infotype || !infolen) |
| 136 | return error; |
| 137 | |
| 138 | *infotype = SCTP_RECVV_NOINFO; |
| 139 | _infolen = *infolen; |
| 140 | |
| 141 | /* set info and infotype */ |
| 142 | for (cmsg = CMSG_FIRSTHDR(&inmsg); cmsg != NULL; |
| 143 | cmsg = CMSG_NXTHDR(&inmsg, cmsg)) { |
| 144 | if (cmsg->cmsg_level != IPPROTO_SCTP) |
| 145 | continue; |
| 146 | |
| 147 | if (cmsg->cmsg_type == SCTP_RCVINFO) { |
| 148 | len = sizeof(struct sctp_rcvinfo); |
| 149 | if (*infotype == SCTP_RECVV_NOINFO) { |
| 150 | if (_infolen < len) |
| 151 | break; |
| 152 | memcpy(info, CMSG_DATA(cmsg), len); |
| 153 | *infotype = SCTP_RECVV_RCVINFO; |
| 154 | *infolen = len; |
| 155 | } else if (*infotype == SCTP_RECVV_NXTINFO) { |
| 156 | if (_infolen < len + |
| 157 | sizeof(struct sctp_nxtinfo)) |
| 158 | break; |
| 159 | memcpy(info + len, info, |
| 160 | sizeof(struct sctp_nxtinfo)); |
| 161 | memcpy(info, CMSG_DATA(cmsg), len); |
| 162 | *infotype = SCTP_RECVV_RN; |
| 163 | *infolen = len + sizeof(struct sctp_nxtinfo); |
| 164 | } else { |
| 165 | break; |
| 166 | } |
| 167 | } else if (cmsg->cmsg_type == SCTP_NXTINFO) { |
| 168 | len = sizeof(struct sctp_nxtinfo); |
| 169 | if (*infotype == SCTP_RECVV_NOINFO) { |
| 170 | if (_infolen < len) |
| 171 | break; |
| 172 | memcpy(info, CMSG_DATA(cmsg), len); |
| 173 | *infotype = SCTP_RECVV_NXTINFO; |
| 174 | *infolen = len; |
| 175 | } else if (*infotype == SCTP_RECVV_RCVINFO) { |
| 176 | if (_infolen < len + |
| 177 | sizeof(struct sctp_rcvinfo)) |
| 178 | break; |
| 179 | memcpy(info + sizeof(struct sctp_rcvinfo), |
| 180 | CMSG_DATA(cmsg), len); |
| 181 | *infotype = SCTP_RECVV_RN; |
| 182 | *infolen = len + sizeof(struct sctp_rcvinfo); |
| 183 | } else { |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return error; |
| 190 | } |
| 191 | #endif |