8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsctp / common / sctp.c
blob3468b2a60aaaaa91c07f3d8a7b6260304ad2229c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #define _XPG4_2
30 #define __EXTENSIONS__
32 #include <assert.h>
33 #include <sys/types.h>
34 #include <sys/uio.h>
35 #include <sys/socket.h>
36 #include <sys/stropts.h>
37 #include <sys/stream.h>
38 #include <sys/socketvar.h>
39 #include <sys/sockio.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stropts.h>
45 #include <stdio.h>
46 #include <strings.h>
47 #include <netinet/in.h>
48 #include <netinet/sctp.h>
50 /* This will hold either a v4 or a v6 sockaddr */
51 union sockaddr_storage_v6 {
52 struct sockaddr_in in;
53 struct sockaddr_in6 in6;
57 * This file implements all the libsctp calls.
61 * To bind a list of addresses to a socket. If the socket is
62 * v4, the type of the list of addresses is (struct in_addr).
63 * If the socket is v6, the type is (struct in6_addr).
65 int
66 sctp_bindx(int sock, void *addrs, int addrcnt, int flags)
68 socklen_t sz;
70 if (addrs == NULL || addrcnt == 0) {
71 errno = EINVAL;
72 return (-1);
75 /* Assume the caller uses the correct family type. */
76 switch (((struct sockaddr *)addrs)->sa_family) {
77 case AF_INET:
78 sz = sizeof (struct sockaddr_in);
79 break;
80 case AF_INET6:
81 sz = sizeof (struct sockaddr_in6);
82 break;
83 default:
84 errno = EAFNOSUPPORT;
85 return (-1);
88 switch (flags) {
89 case SCTP_BINDX_ADD_ADDR:
90 return (setsockopt(sock, IPPROTO_SCTP, SCTP_ADD_ADDR, addrs,
91 sz * addrcnt));
92 case SCTP_BINDX_REM_ADDR:
93 return (setsockopt(sock, IPPROTO_SCTP, SCTP_REM_ADDR, addrs,
94 sz * addrcnt));
95 default:
96 errno = EINVAL;
97 return (-1);
102 * XXX currently not atomic -- need a better way to do this.
105 sctp_getpaddrs(int sock, sctp_assoc_t id, void **addrs)
107 uint32_t naddrs;
108 socklen_t bufsz;
109 struct sctpopt opt;
111 if (addrs == NULL) {
112 errno = EINVAL;
113 return (-1);
116 /* First, find out how many peer addresses there are. */
117 *addrs = NULL;
119 opt.sopt_aid = id;
120 opt.sopt_name = SCTP_GET_NPADDRS;
121 opt.sopt_val = (caddr_t)&naddrs;
122 opt.sopt_len = sizeof (naddrs);
123 if (ioctl(sock, SIOCSCTPGOPT, &opt) == -1) {
124 return (-1);
126 if (naddrs == 0)
127 return (0);
130 * Now we can get all the peer addresses. This will over allocate
131 * space for v4 socket. But it should be OK and save us
132 * the job to find out if it is a v4 or v6 socket.
134 bufsz = sizeof (union sockaddr_storage_v6) * naddrs;
135 if ((*addrs = malloc(bufsz)) == NULL) {
136 return (-1);
138 opt.sopt_name = SCTP_GET_PADDRS;
139 opt.sopt_val = *addrs;
140 opt.sopt_len = bufsz;
141 if (ioctl(sock, SIOCSCTPGOPT, &opt) == -1) {
142 free(*addrs);
143 *addrs = NULL;
144 return (-1);
147 /* Calculate the number of addresses returned. */
148 switch (((struct sockaddr *)*addrs)->sa_family) {
149 case AF_INET:
150 naddrs = opt.sopt_len / sizeof (struct sockaddr_in);
151 break;
152 case AF_INET6:
153 naddrs = opt.sopt_len / sizeof (struct sockaddr_in6);
154 break;
156 return (naddrs);
159 void
160 sctp_freepaddrs(void *addrs)
162 free(addrs);
166 sctp_getladdrs(int sock, sctp_assoc_t id, void **addrs)
168 uint32_t naddrs;
169 socklen_t bufsz;
170 struct sctpopt opt;
172 if (addrs == NULL) {
173 errno = EINVAL;
174 return (-1);
177 /* First, try to find out how many bound addresses there are. */
178 *addrs = NULL;
180 opt.sopt_aid = id;
181 opt.sopt_name = SCTP_GET_NLADDRS;
182 opt.sopt_val = (caddr_t)&naddrs;
183 opt.sopt_len = sizeof (naddrs);
184 if (ioctl(sock, SIOCSCTPGOPT, &opt) == -1) {
185 return (-1);
187 if (naddrs == 0)
188 return (0);
191 * Now we can get all the bound addresses. This will over allocate
192 * space for v4 socket. But it should be OK and save us
193 * the job to find out if it is a v4 or v6 socket.
195 bufsz = sizeof (union sockaddr_storage_v6) * naddrs;
196 if ((*addrs = malloc(bufsz)) == NULL) {
197 return (-1);
199 opt.sopt_name = SCTP_GET_LADDRS;
200 opt.sopt_val = *addrs;
201 opt.sopt_len = bufsz;
202 if (ioctl(sock, SIOCSCTPGOPT, &opt) == -1) {
203 free(*addrs);
204 *addrs = NULL;
205 return (-1);
208 /* Calculate the number of addresses returned. */
209 switch (((struct sockaddr *)*addrs)->sa_family) {
210 case AF_INET:
211 naddrs = opt.sopt_len / sizeof (struct sockaddr_in);
212 break;
213 case AF_INET6:
214 naddrs = opt.sopt_len / sizeof (struct sockaddr_in6);
215 break;
217 return (naddrs);
220 void
221 sctp_freeladdrs(void *addrs)
223 free(addrs);
227 sctp_opt_info(int sock, sctp_assoc_t id, int opt, void *arg, socklen_t *len)
229 struct sctpopt sopt;
231 sopt.sopt_aid = id;
232 sopt.sopt_name = opt;
233 sopt.sopt_val = arg;
234 sopt.sopt_len = *len;
236 if (ioctl(sock, SIOCSCTPGOPT, &sopt) == -1) {
237 return (-1);
239 *len = sopt.sopt_len;
240 return (0);
244 * Branch off an association to its own socket. ioctl() allocates and
245 * returns new fd.
248 sctp_peeloff(int sock, sctp_assoc_t id)
250 int fd;
252 fd = id;
253 if (ioctl(sock, SIOCSCTPPEELOFF, &fd) == -1) {
254 return (-1);
256 return (fd);
260 ssize_t
261 sctp_recvmsg(int s, void *msg, size_t len, struct sockaddr *from,
262 socklen_t *fromlen, struct sctp_sndrcvinfo *sinfo, int *msg_flags)
264 struct msghdr hdr;
265 struct iovec iov;
266 struct cmsghdr *cmsg;
267 char cinmsg[sizeof (*sinfo) + sizeof (*cmsg) + _CMSG_HDR_ALIGNMENT];
268 int err;
270 hdr.msg_name = from;
271 hdr.msg_namelen = (fromlen != NULL) ? *fromlen : 0;
272 hdr.msg_iov = &iov;
273 hdr.msg_iovlen = 1;
274 if (sinfo != NULL) {
275 hdr.msg_control = (void *)_CMSG_HDR_ALIGN(cinmsg);
276 hdr.msg_controllen = sizeof (cinmsg) -
277 (_CMSG_HDR_ALIGN(cinmsg) - (uintptr_t)cinmsg);
278 } else {
279 hdr.msg_control = NULL;
280 hdr.msg_controllen = 0;
283 iov.iov_base = msg;
284 iov.iov_len = len;
285 err = recvmsg(s, &hdr, msg_flags == NULL ? 0 : *msg_flags);
286 if (err == -1) {
287 return (-1);
289 if (fromlen != NULL) {
290 *fromlen = hdr.msg_namelen;
292 if (msg_flags != NULL) {
293 *msg_flags = hdr.msg_flags;
295 if (sinfo != NULL) {
296 for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
297 cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
298 if (cmsg->cmsg_level == IPPROTO_SCTP &&
299 cmsg->cmsg_type == SCTP_SNDRCV) {
300 bcopy(CMSG_DATA(cmsg), sinfo, sizeof (*sinfo));
301 break;
305 return (err);
308 static ssize_t
309 sctp_send_common(int s, const void *msg, size_t len, const struct sockaddr *to,
310 socklen_t tolen, uint32_t ppid, uint32_t sinfo_flags, uint16_t stream_no,
311 uint32_t timetolive, uint32_t context, sctp_assoc_t aid, int flags)
313 struct msghdr hdr;
314 struct iovec iov;
315 struct sctp_sndrcvinfo *sinfo;
316 struct cmsghdr *cmsg;
317 char coutmsg[sizeof (*sinfo) + sizeof (*cmsg) + _CMSG_HDR_ALIGNMENT];
319 hdr.msg_name = (caddr_t)to;
320 hdr.msg_namelen = tolen;
321 hdr.msg_iov = &iov;
322 hdr.msg_iovlen = 1;
323 hdr.msg_control = (void *)_CMSG_HDR_ALIGN(coutmsg);
324 hdr.msg_controllen = sizeof (*cmsg) + sizeof (*sinfo);
326 iov.iov_len = len;
327 iov.iov_base = (caddr_t)msg;
329 cmsg = CMSG_FIRSTHDR(&hdr);
330 cmsg->cmsg_level = IPPROTO_SCTP;
331 cmsg->cmsg_type = SCTP_SNDRCV;
332 cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*sinfo);
334 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
335 sinfo->sinfo_stream = stream_no;
336 sinfo->sinfo_ssn = 0;
337 sinfo->sinfo_flags = sinfo_flags;
338 sinfo->sinfo_ppid = ppid;
339 sinfo->sinfo_context = context;
340 sinfo->sinfo_timetolive = timetolive;
341 sinfo->sinfo_tsn = 0;
342 sinfo->sinfo_cumtsn = 0;
343 sinfo->sinfo_assoc_id = aid;
345 return (sendmsg(s, &hdr, flags));
348 ssize_t
349 sctp_send(int s, const void *msg, size_t len,
350 const struct sctp_sndrcvinfo *sinfo, int flags)
352 /* Note that msg can be NULL for pure control message. */
353 if (sinfo == NULL) {
354 errno = EINVAL;
355 return (-1);
357 return (sctp_send_common(s, msg, len, NULL, 0, sinfo->sinfo_ppid,
358 sinfo->sinfo_flags, sinfo->sinfo_stream, sinfo->sinfo_timetolive,
359 sinfo->sinfo_context, sinfo->sinfo_assoc_id, flags));
362 ssize_t
363 sctp_sendmsg(int s, const void *msg, size_t len, const struct sockaddr *to,
364 socklen_t tolen, uint32_t ppid, uint32_t flags, uint16_t stream_no,
365 uint32_t timetolive, uint32_t context)
367 return (sctp_send_common(s, msg, len, to, tolen, ppid, flags,
368 stream_no, timetolive, context, 0, 0));