ObjFW  Check-in [c34be99f15]

Overview
Comment:OFSCTPSocket: Return socket address bound to
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sctp
Files: files | file ages | folders
SHA3-256: c34be99f15b180d99bc020cff3cd38610c12c92cbf4342d55fbe581dc216de5b
User & Date: js on 2024-04-28 18:25:57
Other Links: branch diff | manifest | tags
Context
2024-04-28
20:32
OFSCTPSocket: Document exceptions check-in: 7c8346a056 user: js tags: sctp
18:25
OFSCTPSocket: Return socket address bound to check-in: c34be99f15 user: js tags: sctp
18:15
Merge trunk into branch "sctp" check-in: bd2d723910 user: js tags: sctp
Changes

Modified src/OFSCTPSocket.h from [4e07124e27] to [f3fd87dead].

144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * @brief Bind the socket to the specified host and port.
 *
 * @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for
 *	       IPv6 to bind to all.
 * @param port The port to bind to. If the port is 0, an unused port will be
 *	       chosen, which can be obtained using the return value.
 * @return The port the socket was bound to
 */
- (uint16_t)bindToHost: (OFString *)host port: (uint16_t)port;
@end

OF_ASSUME_NONNULL_END







|

|



144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * @brief Bind the socket to the specified host and port.
 *
 * @param host The host to bind to. Use `@"0.0.0.0"` for IPv4 or `@"::"` for
 *	       IPv6 to bind to all.
 * @param port The port to bind to. If the port is 0, an unused port will be
 *	       chosen, which can be obtained using the return value.
 * @return The address the socket was bound to
 */
- (OFSocketAddress)bindToHost: (OFString *)host port: (uint16_t)port;
@end

OF_ASSUME_NONNULL_END

Modified src/OFSCTPSocket.m from [23fe3787d2] to [ea0f96d7d6].

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
			   block: block] autorelease]
	    startWithRunLoopMode: runLoopMode];

	objc_autoreleasePoolPop(pool);
}
#endif

- (uint16_t)bindToHost: (OFString *)host port: (uint16_t)port
{
	const int one = 1;
	void *pool = objc_autoreleasePoolPush();
	OFData *socketAddresses;
	OFSocketAddress address;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	int flags;







|







211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
			   block: block] autorelease]
	    startWithRunLoopMode: runLoopMode];

	objc_autoreleasePoolPop(pool);
}
#endif

- (OFSocketAddress)bindToHost: (OFString *)host port: (uint16_t)port
{
	const int one = 1;
	void *pool = objc_autoreleasePoolPush();
	OFData *socketAddresses;
	OFSocketAddress address;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	int flags;
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

294
295
296

297
298
299
300
301
302
303
304
305
306
307




308
309
310
311
312
313
314

		@throw [OFBindIPSocketFailedException exceptionWithHost: host
								   port: port
								 socket: self
								  errNo: errNo];
	}

	objc_autoreleasePoolPop(pool);

	if (port > 0)
		return port;

	memset(&address, 0, sizeof(address));

	address.length = (socklen_t)sizeof(address.sockaddr);
	if (OFGetSockName(_socket, (struct sockaddr *)&address.sockaddr,
	    &address.length) != 0) {
		int errNo = OFSocketErrNo();

		closesocket(_socket);
		_socket = OFInvalidSocketHandle;

		@throw [OFBindIPSocketFailedException exceptionWithHost: host
								   port: port
								 socket: self
								  errNo: errNo];
	}

	switch (((struct sockaddr *)&address.sockaddr)->sa_family) {
	case AF_INET:
		return OFFromBigEndian16(address.sockaddr.in.sin_port);

# ifdef OF_HAVE_IPV6
	case AF_INET6:
		return OFFromBigEndian16(address.sockaddr.in6.sin6_port);

# endif
	default:
		closesocket(_socket);
		_socket = OFInvalidSocketHandle;

		@throw [OFBindIPSocketFailedException
		    exceptionWithHost: host
				 port: port
			       socket: self
				errNo: EAFNOSUPPORT];
	}




}

- (void)setCanDelaySendingPackets: (bool)canDelaySendingPackets
{
	int v = !canDelaySendingPackets;

	if (setsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,







<
<
<
<
<


















|
>


|
>











>
>
>
>







263
264
265
266
267
268
269





270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

		@throw [OFBindIPSocketFailedException exceptionWithHost: host
								   port: port
								 socket: self
								  errNo: errNo];
	}






	memset(&address, 0, sizeof(address));

	address.length = (socklen_t)sizeof(address.sockaddr);
	if (OFGetSockName(_socket, (struct sockaddr *)&address.sockaddr,
	    &address.length) != 0) {
		int errNo = OFSocketErrNo();

		closesocket(_socket);
		_socket = OFInvalidSocketHandle;

		@throw [OFBindIPSocketFailedException exceptionWithHost: host
								   port: port
								 socket: self
								  errNo: errNo];
	}

	switch (((struct sockaddr *)&address.sockaddr)->sa_family) {
	case AF_INET:
		address.family = OFSocketAddressFamilyIPv4;
		break;
# ifdef OF_HAVE_IPV6
	case AF_INET6:
		address.family = OFSocketAddressFamilyIPv6;
		break;
# endif
	default:
		closesocket(_socket);
		_socket = OFInvalidSocketHandle;

		@throw [OFBindIPSocketFailedException
		    exceptionWithHost: host
				 port: port
			       socket: self
				errNo: EAFNOSUPPORT];
	}

	objc_autoreleasePoolPop(pool);

	return address;
}

- (void)setCanDelaySendingPackets: (bool)canDelaySendingPackets
{
	int v = !canDelaySendingPackets;

	if (setsockopt(_socket, IPPROTO_SCTP, SCTP_NODELAY,

Modified tests/OFSCTPSocketTests.m from [61d02ea1d1] to [b41fd34ef1].

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
@interface OFSCTPSocketTests: OTTestCase
@end

@implementation OFSCTPSocketTests
- (void)testSCTPSocket
{
	OFSCTPSocket *server, *client, *accepted;
	uint16_t port;
	char buffer[6];

	server = [OFSCTPSocket socket];
	client = [OFSCTPSocket socket];

	@try {
		port = [server bindToHost: @"127.0.0.1" port: 0];
	} @catch (OFBindSocketFailedException *e) {
		switch (e.errNo) {
		case EPROTONOSUPPORT:
			OTSkip(@"SCTP unsupported");
		default:
			@throw e;
		}
	}

	[server listen];

	[client connectToHost: @"127.0.0.1" port: port];


	accepted = [server accept];
	OTAssertEqualObjects(OFSocketAddressString(accepted.remoteAddress),
	    @"127.0.0.1");

	[client sendBuffer: "Hello!" length: 6];

	[accepted receiveIntoBuffer: buffer length: 6];
	OTAssertEqual(memcmp(buffer, "Hello!", 6), 0);
}
@end







|






|











|
>











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
@interface OFSCTPSocketTests: OTTestCase
@end

@implementation OFSCTPSocketTests
- (void)testSCTPSocket
{
	OFSCTPSocket *server, *client, *accepted;
	OFSocketAddress address;
	char buffer[6];

	server = [OFSCTPSocket socket];
	client = [OFSCTPSocket socket];

	@try {
		address = [server bindToHost: @"127.0.0.1" port: 0];
	} @catch (OFBindSocketFailedException *e) {
		switch (e.errNo) {
		case EPROTONOSUPPORT:
			OTSkip(@"SCTP unsupported");
		default:
			@throw e;
		}
	}

	[server listen];

	[client connectToHost: @"127.0.0.1"
			 port: OFSocketAddressIPPort(&address)];

	accepted = [server accept];
	OTAssertEqualObjects(OFSocketAddressString(accepted.remoteAddress),
	    @"127.0.0.1");

	[client sendBuffer: "Hello!" length: 6];

	[accepted receiveIntoBuffer: buffer length: 6];
	OTAssertEqual(memcmp(buffer, "Hello!", 6), 0);
}
@end