ObjFW  Check-in [49f5cd5ec5]

Overview
Comment:Add support for MPTCP
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 49f5cd5ec501357e3d66d0abe587f70cf1d1e66436aeb81493f06d6914f986ce
User & Date: js on 2024-11-07 21:17:39
Other Links: manifest | tags
Context
2024-11-08
20:56
Make ObjC++ exceptions work with libc++ on Windows check-in: c97a357529 user: js tags: trunk
2024-11-07
21:17
Add support for MPTCP check-in: 49f5cd5ec5 user: js tags: trunk
21:15
Document -[usesMPTCP] may incorrectly return false Closed-Leaf check-in: 9a6b5d6cc4 user: js tags: mptcp
2024-11-06
21:28
objfw-compile: Remove handling of .bundle check-in: c5f91347e7 user: js tags: trunk
Changes

Modified configure.ac from [b60b759470] to [5d1179e386].

1486
1487
1488
1489
1490
1491
1492

1493
1494
1495
1496
1497
1498
1499
		AC_DEFINE(OF_HAVE_NETINET_IN_H, 1,
			[Whether we have netinet/in.h])
	])
	AC_CHECK_HEADER(netinet/tcp.h, [
		AC_DEFINE(OF_HAVE_NETINET_TCP_H, 1,
			[Whether we have netinet/tcp.h])
	])

	AC_CHECK_HEADER(netinet/sctp.h, [
		AC_DEFINE(OF_HAVE_SCTP, 1, [Whether we have SCTP])
		AC_DEFINE(OF_HAVE_NETINET_SCTP_H, 1,
			[Whether we have netinet/sctp.h])
		AC_SUBST(USE_SRCS_SCTP, '${SRCS_SCTP}')
	], [], [
		#ifdef OF_HAVE_SYS_SOCKET_H







>







1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
		AC_DEFINE(OF_HAVE_NETINET_IN_H, 1,
			[Whether we have netinet/in.h])
	])
	AC_CHECK_HEADER(netinet/tcp.h, [
		AC_DEFINE(OF_HAVE_NETINET_TCP_H, 1,
			[Whether we have netinet/tcp.h])
	])
	AC_CHECK_HEADERS(linux/mptcp.h)
	AC_CHECK_HEADER(netinet/sctp.h, [
		AC_DEFINE(OF_HAVE_SCTP, 1, [Whether we have SCTP])
		AC_DEFINE(OF_HAVE_NETINET_SCTP_H, 1,
			[Whether we have netinet/sctp.h])
		AC_SUBST(USE_SRCS_SCTP, '${SRCS_SCTP}')
	], [], [
		#ifdef OF_HAVE_SYS_SOCKET_H

Modified src/OFHTTPClient.m from [8f9fbbe9f5] to [78cfc99be7].

728
729
730
731
732
733
734

735
736
737
738
739
740
741
		OFTCPSocket *sock;
		uint16_t port;
		OFNumber *IRIPort;

		[_client close];

		sock = [OFTCPSocket socket];


		if ([IRI.scheme caseInsensitiveCompare: @"https"] ==
		    OFOrderedSame)
			port = 443;
		else
			port = 80;








>







728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
		OFTCPSocket *sock;
		uint16_t port;
		OFNumber *IRIPort;

		[_client close];

		sock = [OFTCPSocket socket];
		sock.usesMPTCP = true;

		if ([IRI.scheme caseInsensitiveCompare: @"https"] ==
		    OFOrderedSame)
			port = 443;
		else
			port = 80;

Modified src/OFHTTPServer.m from [db59500413] to [0f92d8dbbf].

862
863
864
865
866
867
868

869
870
871
872
873
874
875
	if (_host == nil)
		@throw [OFInvalidArgumentException exception];

	if (_listeningSocket != nil)
		@throw [OFAlreadyOpenException exceptionWithObject: self];

	_listeningSocket = [[OFTCPSocket alloc] init];

	address = [_listeningSocket bindToHost: _host port: _port];
	_port = OFSocketAddressIPPort(&address);
	[_listeningSocket listen];

#ifdef OF_HAVE_THREADS
	if (_numberOfThreads > 1) {
		OFMutableArray *threads =







>







862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
	if (_host == nil)
		@throw [OFInvalidArgumentException exception];

	if (_listeningSocket != nil)
		@throw [OFAlreadyOpenException exceptionWithObject: self];

	_listeningSocket = [[OFTCPSocket alloc] init];
	_listeningSocket.usesMPTCP = true;
	address = [_listeningSocket bindToHost: _host port: _port];
	_port = OFSocketAddressIPPort(&address);
	[_listeningSocket listen];

#ifdef OF_HAVE_THREADS
	if (_numberOfThreads > 1) {
		OFMutableArray *threads =

Modified src/OFTCPSocket.h from [c7a38bf638] to [0b84296c9c].

85
86
87
88
89
90
91

92
93
94
95
96
97
98
99
@interface OFTCPSocket: OFStreamSocket
{
	OFString *_Nullable _SOCKS5Host;
	uint16_t _SOCKS5Port;
#ifdef OF_WII
	uint16_t _port;
#endif

	OF_RESERVE_IVARS(OFTCPSocket, 4)
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, nullable, copy, nonatomic) OFString *SOCKS5Host;
@property (class, nonatomic) uint16_t SOCKS5Port;
#endif








>
|







85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
@interface OFTCPSocket: OFStreamSocket
{
	OFString *_Nullable _SOCKS5Host;
	uint16_t _SOCKS5Port;
#ifdef OF_WII
	uint16_t _port;
#endif
	uintptr_t _flags;	/* Change to a smaller type on ABI bump */
	OF_RESERVE_IVARS(OFTCPSocket, 3)
}

#ifdef OF_HAVE_CLASS_PROPERTIES
@property (class, nullable, copy, nonatomic) OFString *SOCKS5Host;
@property (class, nonatomic) uint16_t SOCKS5Port;
#endif

118
119
120
121
122
123
124











125
126
127
128
129
130
131
 *
 * @throw OFGetOptionFailedException The option could not be retrieved
 * @throw OFSetOptionFailedException The option could not be set
 */
@property (nonatomic) bool canDelaySendingSegments;
#endif












/**
 * @brief The host to use as a SOCKS5 proxy.
 */
@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *SOCKS5Host;

/**
 * @brief The port to use on the SOCKS5 proxy.







>
>
>
>
>
>
>
>
>
>
>







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
 *
 * @throw OFGetOptionFailedException The option could not be retrieved
 * @throw OFSetOptionFailedException The option could not be set
 */
@property (nonatomic) bool canDelaySendingSegments;
#endif

/**
 * @brief Whether the socket uses MPTCP.
 *
 * If you want to use MPTCP, set this to true before connecting or binding.
 * After connecting or binding, this returns whether MPTCP was used.
 *
 * @note After connecting, this method may return `false` even when MPTCP was
 *	 used. This is an OS limitation.
 */
@property (nonatomic) bool usesMPTCP;

/**
 * @brief The host to use as a SOCKS5 proxy.
 */
@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *SOCKS5Host;

/**
 * @brief The port to use on the SOCKS5 proxy.

Modified src/OFTCPSocket.m from [5c97e81fe5] to [7912261615].

48
49
50
51
52
53
54
















55
56
57
58
59
60




















61
62
63
64
65
66
67

#import "OFAlreadyOpenException.h"
#import "OFBindIPSocketFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"

















static const OFRunLoopMode connectRunLoopMode =
    @"OFTCPSocketConnectRunLoopMode";

static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;





















@interface OFTCPSocket () <OFAsyncIPSocketConnecting>
@end

@interface OFTCPSocketConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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

#import "OFAlreadyOpenException.h"
#import "OFBindIPSocketFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"

#ifdef HAVE_LINUX_MPTCP_H
# include <linux/mptcp.h>
#endif

#if defined(OF_MACOS) || defined(OF_IOS)
# ifndef AF_MULTIPATH
#  define AF_MULTIPATH 39
# endif
#endif

enum {
	flagUseMPTCP = 1,
	flagMapIPv4 = 2,
	flagUseConnectX = 4
};

static const OFRunLoopMode connectRunLoopMode =
    @"OFTCPSocketConnectRunLoopMode";

static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;

#if defined(OF_LINUX) && defined(IPPROTO_MPTCP)
static OFSocketAddress
mapIPv4(const OFSocketAddress *IPv4Address)
{
	OFSocketAddress IPv6Address = {
		.family = OFSocketAddressFamilyIPv6,
		.length = sizeof(struct sockaddr_in6)
	};

	IPv6Address.sockaddr.in6.sin6_family = AF_INET6;
	IPv6Address.sockaddr.in6.sin6_port = IPv4Address->sockaddr.in.sin_port;
	memcpy(&IPv6Address.sockaddr.in6.sin6_addr.s6_addr[12],
	    &IPv4Address->sockaddr.in.sin_addr.s_addr, 4);
	IPv6Address.sockaddr.in6.sin6_addr.s6_addr[10] = 0xFF;
	IPv6Address.sockaddr.in6.sin6_addr.s6_addr[11] = 0xFF;

	return IPv6Address;
}
#endif

@interface OFTCPSocket () <OFAsyncIPSocketConnecting>
@end

@interface OFTCPSocketConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
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
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	int flags;
#endif

	if (_socket != OFInvalidSocketHandle)
		@throw [OFAlreadyOpenException exceptionWithObject: self];





























	if ((_socket = socket(
	    ((struct sockaddr *)&address->sockaddr)->sa_family,
	    SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle) {
		*errNo = _OFSocketErrNo();
		return false;

	}

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
		fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

	return true;
}

- (bool)of_connectSocketToAddress: (const OFSocketAddress *)address
			    errNo: (int *)errNo
{




	if (_socket == OFInvalidSocketHandle)
		@throw [OFNotOpenException exceptionWithObject: self];




























	/* Cast needed for AmigaOS, where the argument is declared non-const */


	if (connect(_socket, (struct sockaddr *)&address->sockaddr,
	    address->length) != 0) {
		*errNo = _OFSocketErrNo();
		return false;
	}

	return true;
}

- (void)of_closeSocket
{
	closesocket(_socket);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
>













>
>
>
>
|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
|
|
|







178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	int flags;
#endif

	if (_socket != OFInvalidSocketHandle)
		@throw [OFAlreadyOpenException exceptionWithObject: self];

#if defined(OF_LINUX) && defined(IPPROTO_MPTCP)
	if (_flags & flagUseMPTCP) {
		/*
		 * For MPTCP sockets, we always use AF_INET6, so that IPv4 and
		 * IPv6 can both be used for a single connection.
		 */
		_socket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC,
		    IPPROTO_MPTCP);

		if (_socket != OFInvalidSocketHandle &&
		    address->family == OFSocketAddressFamilyIPv4)
			_flags |= flagMapIPv4;
		else
			_flags &= ~flagMapIPv4;
	}
#elif (defined(OF_MACOS) || defined(OF_IOS)) && defined(SAE_ASSOCID_ANY)
	if (_flags & flagUseMPTCP) {
		_socket = socket(AF_MULTIPATH, SOCK_STREAM | SOCK_CLOEXEC,
		    IPPROTO_TCP);

		if (_socket != OFInvalidSocketHandle)
			_flags |= flagUseConnectX;
		else
			_flags &= ~flagUseConnectX;
	}
#endif

	if (_socket == OFInvalidSocketHandle) {
		if ((_socket = socket(
		    ((struct sockaddr *)&address->sockaddr)->sa_family,
		    SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle) {
			*errNo = _OFSocketErrNo();
			return false;
		}
	}

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
		fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

	return true;
}

- (bool)of_connectSocketToAddress: (const OFSocketAddress *)address
			    errNo: (int *)errNo
{
#if defined(OF_LINUX) && defined(IPPROTO_MPTCP)
	OFSocketAddress mappedIPv4;
#endif

	if (_socket == OFInvalidSocketHandle) {
		@throw [OFNotOpenException exceptionWithObject: self];
	}

#if defined(OF_LINUX) && defined(IPPROTO_MPTCP)
	if (_flags & flagMapIPv4) {
		/*
		 * For MPTCP sockets, we always use AF_INET6, so that IPv4 and
		 * IPv6 can both be used for a single connection.
		 */
		mappedIPv4 = mapIPv4(address);
		address = &mappedIPv4;
	}
#endif

#if (defined(OF_MACOS) || defined(OF_IOS)) && defined(SAE_ASSOCID_ANY)
	if (_flags & flagUseConnectX) {
		sa_endpoints_t endpoints = {
			.sae_dstaddr = (struct sockaddr *)&address->sockaddr,
			.sae_dstaddrlen = address->length
		};

		if (connectx(_socket, &endpoints, SAE_ASSOCID_ANY, 0, NULL, 0,
		    NULL, NULL) != 0) {
			*errNo = _OFSocketErrNo();
			return false;
		}
	} else
#endif
		/*
		 * Cast needed for AmigaOS, where the argument is declared
		 * non-const.
		 */
		if (connect(_socket, (struct sockaddr *)&address->sockaddr,
		    address->length) != 0) {
			*errNo = _OFSocketErrNo();
			return false;
		}

	return true;
}

- (void)of_closeSocket
{
	closesocket(_socket);
344
345
346
347
348
349
350
















351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	socketAddresses = [[OFThread DNSResolver]
	    resolveAddressesForHost: host
		      addressFamily: OFSocketAddressFamilyAny];

	address = *(OFSocketAddress *)[socketAddresses itemAtIndex: 0];
	OFSocketAddressSetIPPort(&address, port);

















	if ((_socket = socket(
	    ((struct sockaddr *)&address.sockaddr)->sa_family,
	    SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle)
		@throw [OFBindIPSocketFailedException
		    exceptionWithHost: host
				 port: port
			       socket: self
				errNo: _OFSocketErrNo()];

	_canBlock = true;

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
		fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|







442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
	socketAddresses = [[OFThread DNSResolver]
	    resolveAddressesForHost: host
		      addressFamily: OFSocketAddressFamilyAny];

	address = *(OFSocketAddress *)[socketAddresses itemAtIndex: 0];
	OFSocketAddressSetIPPort(&address, port);

#if defined(OF_LINUX) && defined(IPPROTO_MPTCP)
	if (_flags & flagUseMPTCP) {
		/*
		 * For MPTCP sockets, we always use AF_INET6, so that IPv4 and
		 * IPv6 can both be used for a single connection.
		 */
		_socket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC,
		    IPPROTO_MPTCP);

		if (_socket != OFInvalidSocketHandle &&
		    address.family == OFSocketAddressFamilyIPv4)
			address = mapIPv4(&address);
	}
#endif

	if (_socket == OFInvalidSocketHandle)
		if ((_socket = socket(
		    ((struct sockaddr *)&address.sockaddr)->sa_family,
		    SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle)
			@throw [OFBindIPSocketFailedException
			    exceptionWithHost: host
					 port: port
				       socket: self
					errNo: _OFSocketErrNo()];

	_canBlock = true;

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
		fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif
507
508
509
510
511
512
513





















514
515
516
517
518
519
520
521
522
523
		@throw [OFGetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];

	return !v;
}
#endif






















- (void)close
{
#ifdef OF_WII
	_port = 0;
#endif

	[super close];
}
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
		@throw [OFGetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];

	return !v;
}
#endif

- (void)setUsesMPTCP: (bool)usesMPTCP
{
	if (usesMPTCP)
		_flags |= flagUseMPTCP;
	else
		_flags &= ~flagUseMPTCP;
}

- (bool)usesMPTCP
{
#if defined(OF_LINUX) && defined(SOL_MPTCP) && defined(MPTCP_INFO)
	struct mptcp_info info;
	socklen_t infoLen = (socklen_t)sizeof(info);

	if (getsockopt(_socket, SOL_MPTCP, MPTCP_INFO, &info, &infoLen) != -1)
		return true;
#endif

	return false;
}

- (void)close
{
#ifdef OF_WII
	_port = 0;
#endif

	[super close];
}
@end

Modified tests/OFHTTPClientTests.m from [e9b6e1eab7] to [962f5ae8ec].

146
147
148
149
150
151
152

153
154
155
156
157
158
159
	bool sawHost = false, sawContentLength = false, sawContentType = false;
	bool sawUserAgent = false;
	char buffer[5];

	[_condition lock];

	listener = [OFTCPSocket socket];

	address = [listener bindToHost: @"127.0.0.1" port: 0];
	_port = OFSocketAddressIPPort(&address);
	[listener listen];

	[_condition signal];
	[_condition unlock];
	client = [listener accept];







>







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	bool sawHost = false, sawContentLength = false, sawContentType = false;
	bool sawUserAgent = false;
	char buffer[5];

	[_condition lock];

	listener = [OFTCPSocket socket];
	listener.usesMPTCP = true;
	address = [listener bindToHost: @"127.0.0.1" port: 0];
	_port = OFSocketAddressIPPort(&address);
	[listener listen];

	[_condition signal];
	[_condition unlock];
	client = [listener accept];