File: connectx.c

package info (click to toggle)
lksctp-tools 1.0.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,708 kB
  • ctags: 1,184
  • sloc: ansic: 13,499; sh: 4,162; makefile: 251
file content (190 lines) | stat: -rw-r--r-- 4,852 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* SCTP kernel Implementation: User API extensions.
 *
 * connectx.c
 *
 * Distributed under the terms of the LGPL v2.1 as described in
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 * This file is part of the user library that offers support for the
 * SCTP kernel Implementation. The main purpose of this
 * code is to provide the SCTP Socket API mappings for user
 * application to interface with the SCTP in kernel.
 *
 * This implementation is based on the Socket API Extensions for SCTP
 * defined in <draft-ietf-tsvwg-sctpsocket-10.txt.
 *
 * (C) Copyright IBM Corp. 2001, 2005
 *
 * Written or modified by:
 *   Frank Filz     <ffilz@us.ibm.com>
 */

#include <sys/socket.h>   /* struct sockaddr_storage, setsockopt() */
#include <netinet/in.h>
#include <netinet/sctp.h> /* SCTP_SOCKOPT_CONNECTX_* */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

/* Support the sctp_connectx() interface.
 *
 * See Sockets API Extensions for SCTP. Section 8.1.
 *
 * Instead of implementing through a socket call in sys_socketcall(),
 * tunnel the request through setsockopt().
 */
static int __connectx_addrsize(const struct sockaddr *addrs,
				const int addrcnt)
{
	const void *addrbuf;
	const struct sockaddr *sa_addr;
	int addrs_size = 0;
	int i;

	addrbuf = addrs;
	for (i = 0; i < addrcnt; i++) {
		sa_addr = (const struct sockaddr *)addrbuf;
		switch (sa_addr->sa_family) {
		case AF_INET:
			addrs_size += sizeof(struct sockaddr_in);
			addrbuf += sizeof(struct sockaddr_in);
			break;
		case AF_INET6:
			addrs_size += sizeof(struct sockaddr_in6);
			addrbuf += sizeof(struct sockaddr_in6);
			break;
		default:
			errno = EINVAL;
			return -1;
		}
	}

	return addrs_size;
}
			

int __sctp_connectx(int fd, struct sockaddr *addrs, int addrcnt)
{
	socklen_t addrs_size = __connectx_addrsize(addrs, addrcnt);

	if (addrs_size < 0)
		return addrs_size;

	return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD, addrs,
			    addrs_size);
}

extern int sctp_connectx_orig (int)
	__attribute ((alias ("__sctp_connectx")));


static int __connectx(int fd, struct sockaddr *addrs, socklen_t addrs_size,
			sctp_assoc_t *id)
{
	int status;

	if (id)
		*id = 0;

	status = setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX, addrs,
			    addrs_size);

	/* Normalize status and set association id */
	if (status > 0) {
		if (id)
			*id = status;
		return 0;
	}

	/* The error is something other then "Option not supported" */
	if (status < 0 && errno != ENOPROTOOPT)
		return status;

	/* At this point, if the application wanted the id, we can't
	 * really provide it, so we can return ENOPROTOOPT.
	 */
	if (id) {
		errno = ENOPROTOOPT;
		return -1;
	}

	/* Finally, try the old API */
	return setsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX_OLD,
			  addrs, addrs_size);
}

int sctp_connectx2(int fd, struct sockaddr *addrs, int addrcnt,
		      sctp_assoc_t *id)
{
	socklen_t addrs_size = __connectx_addrsize(addrs, addrcnt);

	if (addrs_size < 0)
		return addrs_size;

	return __connectx(fd, addrs, addrs_size, id);
}

int sctp_connectx3(int fd, struct sockaddr *addrs, int addrcnt,
		      sctp_assoc_t *id)
{
	socklen_t addrs_size = __connectx_addrsize(addrs, addrcnt);
	int status;
	struct sctp_getaddrs_old param;
	socklen_t opt_len = sizeof(param);

	if (addrs_size < 0)
		return addrs_size;

	/* First try the new socket api
	 * Because the id is returned in the option buffer we have prepend
	 * 32bit to it for the returned association id
	 */
	param.assoc_id = 0;
	param.addr_num = addrs_size;
	param.addrs = addrs;
	status = getsockopt(fd, SOL_SCTP, SCTP_SOCKOPT_CONNECTX3,
		            &param, &opt_len);
	if (status == 0 || errno == EINPROGRESS) {
		/* Succeeded immediately, or initiated on non-blocking
		 * socket.
		 */
		if (id)
			*id = param.assoc_id;
	}

	if (errno != ENOPROTOOPT) {
		/* No point in trying the fallbacks*/
		return status;
	}

	/* The first incarnation of updated connectx api didn't work for
	 * non-blocking sockets.  So if the application wants the association
	 * id and the socket is non-blocking, we can't really do anything.
	 */
	if (id) {
		/* Program wants the association-id returned. We can only do
		 * that if the socket is blocking */
		status = fcntl(fd, F_GETFL);
		if (status < 0)
			return status;

		if (status & O_NONBLOCK) {
			/* Socket is non-blocking. Fail */
			errno = ENOPROTOOPT;
			return -1;
		}
	}

	return __connectx(fd, addrs, addrs_size, id);
}

#define __SYMPFX(pfx, sym) #pfx sym
#define _SYMPFX(pfx, sym) __SYMPFX(pfx, sym)
#define SYMPFX(sym) _SYMPFX(__USER_LABEL_PREFIX__, #sym)
#define SYMVER(name, name2) __asm__(".symver " SYMPFX(name) "," SYMPFX(name2))

SYMVER(__sctp_connectx, sctp_connectx@);
SYMVER(sctp_connectx_orig, sctp_connectx@VERS_1);
SYMVER(sctp_connectx2, sctp_connectx@VERS_2);
SYMVER(sctp_connectx3, sctp_connectx@@VERS_3);