ObjFW  Check-in [71834180cb]

Overview
Comment:Initial MPTCP implementation for Linux
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | mptcp
Files: files | file ages | folders
SHA3-256: 71834180cbb0cdcad827f5ea12e28d8fb745707f3726f3b55ba89a54b623b6d7
User & Date: js on 2024-10-28 21:35:38
Other Links: branch diff | manifest | tags
Context
2024-10-28
21:42
Enable MPTCP for OFHTTPClient and OFHTTPServer check-in: 50035101ae user: js tags: mptcp
21:35
Initial MPTCP implementation for Linux check-in: 71834180cb user: js tags: mptcp
2024-10-27
23:57
Add support for \x from JSON5 check-in: 229486deb1 user: js tags: trunk
Changes

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

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

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
 *
 * @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.
 */
@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 [064f26252a].

48
49
50
51
52
53
54




55
56
57
58
59
60
61

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








>
>
>
>







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#import "OFAlreadyOpenException.h"
#import "OFBindIPSocketFailedException.h"
#import "OFGetOptionFailedException.h"
#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;

139
140
141
142
143
144
145
146
147
148



149
150














151
152
153
154
155
156
157
158
159
160
161
- (bool)of_createSocketForAddress: (const OFSocketAddress *)address
			    errNo: (int *)errNo
{
#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;







|

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







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
- (bool)of_createSocketForAddress: (const OFSocketAddress *)address
			    errNo: (int *)errNo
{
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	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;
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







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







365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
	socketAddresses = [[OFThread DNSResolver]
	    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)) ==
			    OFInvalidSocketHandle)
				@throw [OFBindIPSocketFailedException
				    exceptionWithHost: host
						 port: port
					       socket: self
						errNo: _OFSocketErrNo()];
		}
	} else
#endif
		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







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










546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
		@throw [OFGetOptionFailedException
		    exceptionWithObject: self
				  errNo: _OFSocketErrNo()];

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

	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