Debian Codegraph @

src/lib/connectx.c

raw symbols SCIP
1 /* SCTP kernel Implementation: User API extensions.
2 *
3 * connectx.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 * (C) Copyright IBM Corp. 2001, 2005
17 *
18 * Written or modified by:
19 * Frank Filz <[email protected]>
20 */
21
22 #include <sys/socket.h> /* struct sockaddr_storage, setsockopt() */
23 #include <netinet/in.h>
24 #include <netinet/sctp.h> /* SCTP_SOCKOPT_CONNECTX_* */
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include "config.h"
30
31 #define __SYMPFX(pfx, sym) #pfx sym
32 #define _SYMPFX(pfx, sym) __SYMPFX(pfx, sym)
33 #define SYMPFX(sym) _SYMPFX(__USER_LABEL_PREFIX__, #sym)
34
35 #if HAVE_ATTRIBUTE_SYMVER
36 #define SYMVER(name, name2) __attribute__ ((symver (SYMPFX(name2))))
37 #else
38 #define SYMVER(name, name2) __asm__(".symver " SYMPFX(name) "," SYMPFX(name2));
39 #endif
40
41
42 /* Support the sctp_connectx() interface.
43 *
44 * See Sockets API Extensions for SCTP. Section 8.1.
45 *
46 * Instead of implementing through a socket call in sys_socketcall(),
47 * tunnel the request through setsockopt().
48 */
49 static int __connectx_addrsize(const struct sockaddr *addrs,
50 const int addrcnt)
51 {
52 const char *addrbuf;
53 const struct sockaddr *sa_addr;
54 int addrs_size = 0;
55 int i;
56
57 addrbuf = (char *)addrs;
58 for (i = 0; i < addrcnt; i++) {
59 sa_addr = (const struct sockaddr *)addrbuf;
60 switch (sa_addr->sa_family) {
61 case AF_INET:
62 addrs_size += sizeof(struct sockaddr_in);
63 addrbuf += sizeof(struct sockaddr_in);
64 break;
65 case AF_INET6:
66 addrs_size += sizeof(struct sockaddr_in6);
67 addrbuf += sizeof(struct sockaddr_in6);
68 break;
69 default:
70 errno = EINVAL;
71 return -1;
72 }
73 }
74
75 return addrs_size;
76 }
77
78
79 SYMVER(__sctp_connectx, sctp_connectx@)
80 int __sctp_connectx(int fd, struct sockaddr *addrs, int addrcnt)
81 {
82 int addrs_size = __connectx_addrsize(addrs, addrcnt);
83
84 if (addrs_size < 0)
85 return addrs_size;
86
87 return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD, addrs,
88 addrs_size);
89 }
90
91 SYMVER(sctp_connectx_orig, sctp_connectx@VERS_1)
92 extern int sctp_connectx_orig (int)
93 __attribute ((alias ("__sctp_connectx")));
94
95
96 static int __connectx(int fd, struct sockaddr *addrs, socklen_t addrs_size,
97 sctp_assoc_t *id)
98 {
99 int status;
100
101 if (id)
102 *id = 0;
103
104 status = setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX, addrs,
105 addrs_size);
106
107 /* Normalize status and set association id */
108 if (status > 0) {
109 if (id)
110 *id = status;
111 return 0;
112 }
113
114 /* The error is something other then "Option not supported" */
115 if (status < 0 && errno != ENOPROTOOPT)
116 return status;
117
118 /* At this point, if the application wanted the id, we can't
119 * really provide it, so we can return ENOPROTOOPT.
120 */
121 if (id) {
122 errno = ENOPROTOOPT;
123 return -1;
124 }
125
126 /* Finally, try the old API */
127 return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD,
128 addrs, addrs_size);
129 }
130
131 SYMVER(sctp_connectx2, sctp_connectx@VERS_2)
132 int sctp_connectx2(int fd, struct sockaddr *addrs, int addrcnt,
133 sctp_assoc_t *id)
134 {
135 int addrs_size = __connectx_addrsize(addrs, addrcnt);
136
137 if (addrs_size < 0)
138 return addrs_size;
139
140 return __connectx(fd, addrs, addrs_size, id);
141 }
142
143 SYMVER(sctp_connectx3, sctp_connectx@@VERS_3)
144 int sctp_connectx3(int fd, struct sockaddr *addrs, int addrcnt,
145 sctp_assoc_t *id)
146 {
147 int addrs_size = __connectx_addrsize(addrs, addrcnt);
148 int status;
149 struct sctp_getaddrs_old param;
150 socklen_t opt_len = sizeof(param);
151
152 if (addrs_size < 0)
153 return addrs_size;
154
155 /* First try the new socket api
156 * Because the id is returned in the option buffer we have prepend
157 * 32bit to it for the returned association id
158 */
159 param.assoc_id = 0;
160 param.addr_num = addrs_size;
161 param.addrs = addrs;
162 status = getsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX3,
163 &param, &opt_len);
164 if (status == 0 || errno == EINPROGRESS) {
165 /* Succeeded immediately, or initiated on non-blocking
166 * socket.
167 */
168 if (id)
169 *id = param.assoc_id;
170 }
171
172 if (errno != ENOPROTOOPT) {
173 /* No point in trying the fallbacks*/
174 return status;
175 }
176
177 /* The first incarnation of updated connectx api didn't work for
178 * non-blocking sockets. So if the application wants the association
179 * id and the socket is non-blocking, we can't really do anything.
180 */
181 if (id) {
182 /* Program wants the association-id returned. We can only do
183 * that if the socket is blocking */
184 status = fcntl(fd, F_GETFL);
185 if (status < 0)
186 return status;
187
188 if (status & O_NONBLOCK) {
189 /* Socket is non-blocking. Fail */
190 errno = ENOPROTOOPT;
191 return -1;
192 }
193 }
194
195 return __connectx(fd, addrs, addrs_size, id);
196 }
197