ObjFW  Check-in [4e763857b8]

Overview
Comment:Initial MPTCP implementation for macOS/iOS

Unfortunately, I haven't found a way to reliably check if a connection
is using MPTCP on macOS/iOS yet, so -[usesMPTCP] always returns false
for now. In my tests with SIOCGCONNINFO, the flags were always 0.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | mptcp
Files: files | file ages | folders
SHA3-256: 4e763857b88e948b689ff7e213e13c094d2167a283936e6b22c6a066474ba92e
User & Date: js on 2024-10-28 23:46:55
Other Links: branch diff | manifest | tags
Context
2024-10-28
23:46
Initial MPTCP implementation for macOS/iOS Leaf check-in: 4e763857b8 user: js tags: mptcp
21:42
Enable MPTCP for OFHTTPClient and OFHTTPServer check-in: 50035101ae user: js tags: mptcp
Changes

Modified src/OFTCPSocket.h from [2c25626468] to [a5332f3211].

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
	uintptr_t _usesMPTCP;	/* Change to bool 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







|







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
	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

Modified src/OFTCPSocket.m from [064f26252a] to [2efb3cbab4].

52
53
54
55
56
57
58











59
60
61
62
63
64
65
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"

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












static const OFRunLoopMode connectRunLoopMode =
    @"OFTCPSocketConnectRunLoopMode";

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








>
>
>
>
>
>
>
>
>
>
>







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
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"

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

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

enum {
	flagUseMPTCP = 1,
	flagUseConnectX = 2
};

static const OFRunLoopMode connectRunLoopMode =
    @"OFTCPSocketConnectRunLoopMode";

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

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
















191


192
193
194
195
196
197
198
199
200
201
202
203
	int flags;
#endif

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

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

















	} else
#endif
		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);







|
|












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




















|

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







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
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
	int flags;
#endif

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

#if defined(OF_LINUX)
	if (_flags & flagUseMPTCP) {
		if ((_socket = socket(
		    ((struct sockaddr *)&address->sockaddr)->sa_family,
		    SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_MPTCP)) ==
		    OFInvalidSocketHandle) {
			if ((_socket = socket(
			    ((struct sockaddr *)&address->sockaddr)->sa_family,
			    SOCK_STREAM | SOCK_CLOEXEC, 0)) ==
			    OFInvalidSocketHandle) {
				*errNo = _OFSocketErrNo();
				return false;
			}
		}
	} else
#elif defined(OF_MACOS) || defined(OF_IOS)
	if (_flags & flagUseMPTCP) {
		if ((_socket = socket(AF_MULTIPATH, SOCK_STREAM | SOCK_CLOEXEC,
		    IPPROTO_TCP)) != OFInvalidSocketHandle)
			_flags |= flagUseConnectX;
		else {
			if ((_socket = socket(
			    ((struct sockaddr *)&address->sockaddr)->sa_family,
			    SOCK_STREAM | SOCK_CLOEXEC, 0)) ==
			    OFInvalidSocketHandle) {
				*errNo = _OFSocketErrNo();
				return false;
			}

			_flags &= ~flagUseConnectX;
		}
	} else
#endif
		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];
	}

#if defined(OF_MACOS) || defined(OF_IOS)
	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);
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
	    resolveAddressesForHost: host
		      addressFamily: OFSocketAddressFamilyAny];

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

#ifdef OF_LINUX
	if (_usesMPTCP) {
		if ((_socket = socket(
		    ((struct sockaddr *)&address.sockaddr)->sa_family,
		    SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_MPTCP)) ==
		    OFInvalidSocketHandle) {
			if ((_socket = socket(
			    ((struct sockaddr *)&address.sockaddr)->sa_family,
			    SOCK_STREAM | SOCK_CLOEXEC, 0)) ==







|







412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
	    resolveAddressesForHost: host
		      addressFamily: OFSocketAddressFamilyAny];

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

#ifdef OF_LINUX
	if (_flags & flagUseMPTCP) {
		if ((_socket = socket(
		    ((struct sockaddr *)&address.sockaddr)->sa_family,
		    SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_MPTCP)) ==
		    OFInvalidSocketHandle) {
			if ((_socket = socket(
			    ((struct sockaddr *)&address.sockaddr)->sa_family,
			    SOCK_STREAM | SOCK_CLOEXEC, 0)) ==
549
550
551
552
553
554
555
556



557
558
559
560
561
562
563

	return !v;
}
#endif

- (void)setUsesMPTCP: (bool)usesMPTCP
{
	_usesMPTCP = usesMPTCP;



}

- (bool)usesMPTCP
{
#ifdef OF_LINUX
	struct mptcp_info info;
	socklen_t infoLen = (socklen_t)sizeof(info);







|
>
>
>







595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

	return !v;
}
#endif

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

- (bool)usesMPTCP
{
#ifdef OF_LINUX
	struct mptcp_info info;
	socklen_t infoLen = (socklen_t)sizeof(info);