ObjFW  Check-in [d983af3d93]

Overview
Comment:Make it possible to let -[bindToPort:onHost:] choose a port.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d983af3d934bc3a335491fa6559896a1f984022f85d43d541252fc8603c0949a
User & Date: js on 2011-03-29 23:19:11
Other Links: manifest | tags
Context
2011-03-29
23:27
Work around compiler bugs. check-in: 6022cfa458 user: js tags: trunk
23:19
Make it possible to let -[bindToPort:onHost:] choose a port. check-in: d983af3d93 user: js tags: trunk
19:28
Work around a compiler bug. check-in: 0972a6eb56 user: js tags: trunk
Changes

Modified src/OFTCPSocket.h from [c8cf94209d] to [146d82e777].

48
49
50
51
52
53
54
55

56
57

58
59
60
61
62
63
64
65
66
67
 */
- (void)connectToHost: (OFString*)host
	       onPort: (uint16_t)port;

/**
 * Bind the socket on the specified port and host.
 *
 * \param port The port to bind to

 * \param host The host to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6
 *	       to bind to all.

 */
- (void)bindToPort: (uint16_t)port
	    onHost: (OFString*)host;

/**
 * Listen on the socket.
 *
 * \param backlog Maximum length for the queue of pending connections.
 */
- (void)listenWithBackLog: (int)backlog;







|
>


>

|
|







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 */
- (void)connectToHost: (OFString*)host
	       onPort: (uint16_t)port;

/**
 * Bind the socket on the specified port and host.
 *
 * \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.
 * \param host The host to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6
 *	       to bind to all.
 * \return The port the socket was bound to
 */
- (uint16_t)bindToPort: (uint16_t)port
		onHost: (OFString*)host;

/**
 * Listen on the socket.
 *
 * \param backlog Maximum length for the queue of pending connections.
 */
- (void)listenWithBackLog: (int)backlog;

Modified src/OFTCPSocket.m from [f3c6437253] to [c47e3134d6].

191
192
193
194
195
196
197
198
199
200



201
202
203
204
205
206
207
	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							  socket: self
							    host: host
							    port: port];
}

- (void)bindToPort: (uint16_t)port
	    onHost: (OFString*)host
{



	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char port_s[7];







|
|

>
>
>







191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							  socket: self
							    host: host
							    port: port];
}

- (uint16_t)bindToPort: (uint16_t)port
		onHost: (OFString*)host
{
	struct sockaddr_storage addr;
	socklen_t addrLen;

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa
							  socket: self];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char port_s[7];
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
281


























282
283
284
285
286
287
288
						      host: host
						      port: port];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;
	struct sockaddr_in addr;

# ifdef OF_THREADS
	[mutex lock];
# endif

	if ((he = gethostbyname([host cString])) == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa

								    host: host];
	}

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = of_bswap16_if_le(port);

	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa

								    host: host];
	}


	memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);

# ifdef OF_THREADS
	[mutex unlock];
# endif
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa

						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa

						      host: host
						      port: port];
	}
#endif


























}

- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];







<










>




|
|






>



>
|






>







>




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







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
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
316
317
318
319
320
321
						      host: host
						      port: port];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;


# ifdef OF_THREADS
	[mutex lock];
# endif

	if ((he = gethostbyname([host cString])) == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}

	memset(&addr, 0, sizeof(addr));
	((struct sockaddr_in*)&addr)->sin_family = AF_INET;
	((struct sockaddr_in*)&addr)->sin_port = of_bswap16_if_le(port);

	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException newWithClass: isa
								  socket: self
								    host: host];
	}

	memcpy(&((struct sockaddr_in*)&addr)->sin_addr.s_addr,
	    he->h_addr_list[0], he->h_length);

# ifdef OF_THREADS
	[mutex unlock];
# endif
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}
#endif

	if (port > 0)
		return port;

	addrLen = sizeof(addr);
	if (getsockname(sock, (struct sockaddr*)&addr, &addrLen)) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						    socket: self
						      host: host
						      port: port];
	}

	if (addr.ss_family == AF_INET)
		return of_bswap16_if_le(((struct sockaddr_in*)&addr)->sin_port);
	if (addr.ss_family == AF_INET6)
		return of_bswap16_if_le(
		    ((struct sockaddr_in6*)&addr)->sin6_port);

	close(sock);
	sock = INVALID_SOCKET;
	@throw [OFBindFailedException newWithClass: isa
					    socket: self
					      host: host
					      port: port];
}

- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa
						      socket: self];

Modified tests/OFHTTPRequestTests.m from [d40fc902b8] to [718beda726].

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
- main
{
	OFTCPSocket *listener, *client;

	[cond lock];

	listener = [OFTCPSocket socket];
	[listener bindToPort: port
		      onHost: @"127.0.0.1"];
	[listener listen];

	[cond signal];
	[cond unlock];

	client = [listener accept];








|
|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
- main
{
	OFTCPSocket *listener, *client;

	[cond lock];

	listener = [OFTCPSocket socket];
	port = [listener bindToPort: 0
			     onHost: @"127.0.0.1"];
	[listener listen];

	[cond signal];
	[cond unlock];

	client = [listener accept];

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
	OFHTTPRequest *req;
	OFHTTPRequestResult *res;

	cond = [OFCondition condition];
	[cond lock];

	server = [[[OFHTTPRequestTestsServer alloc] init] autorelease];
	/* srand(time(NULL)) was already called by OFTCPSocket */
	server->port = (uint16_t)rand();
	if (server->port < 1024)
		server->port += 1024;
	[server start];




	url = [OFURL URLWithString:
	    [OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
					server->port]];

	TEST(@"+[requestWithURL]", (req = [OFHTTPRequest requestWithURL: url]))

	[cond wait];
	[cond unlock];

	TEST(@"-[perform]", (res = [req perform]))

	[server join];

	[pool drain];
}
@end







<
<
<
<


>
>
>






<
<
<







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
	OFHTTPRequest *req;
	OFHTTPRequestResult *res;

	cond = [OFCondition condition];
	[cond lock];

	server = [[[OFHTTPRequestTestsServer alloc] init] autorelease];




	[server start];

	[cond wait];
	[cond unlock];

	url = [OFURL URLWithString:
	    [OFString stringWithFormat: @"http://127.0.0.1:%" @PRIu16 "/foo",
					server->port]];

	TEST(@"+[requestWithURL]", (req = [OFHTTPRequest requestWithURL: url]))




	TEST(@"-[perform]", (res = [req perform]))

	[server join];

	[pool drain];
}
@end

Modified tests/OFTCPSocketTests.m from [28dc0fa910] to [5ae827977d].

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
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <time.h>

#import "OFTCPSocket.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "macros.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFTCPSocket";

@implementation TestsAppDelegate (OFTCPSocketTests)
- (void)TCPSocketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFTCPSocket *server, *client = nil, *accepted;
	OFString *msg;
	uint16_t port;
	char buf[6];

	srand((unsigned)time(NULL));
	port = (uint16_t)rand();
	if (port < 1024)
		port += 1024;

	TEST(@"+[socket]", (server = [OFTCPSocket socket]) &&
	    (client = [OFTCPSocket socket]))

	msg = [OFString stringWithFormat:
	    @"-[bindToPort:onHost:] (port %" @PRIu16 @")", port];
	TEST(msg, R([server bindToPort: port
				onHost: @"127.0.0.1"]))

	TEST(@"-[listen]", R([server listen]))

	TEST(@"-[connectToHost:onPort:]",
	    R([client connectToHost: @"127.0.0.1"
			     onPort: port]))







<

<
















<



<
<
<
<
<



<
|
|







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
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"


#include <string.h>


#import "OFTCPSocket.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "macros.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFTCPSocket";

@implementation TestsAppDelegate (OFTCPSocketTests)
- (void)TCPSocketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFTCPSocket *server, *client = nil, *accepted;

	uint16_t port;
	char buf[6];






	TEST(@"+[socket]", (server = [OFTCPSocket socket]) &&
	    (client = [OFTCPSocket socket]))


	TEST(@"-[bindToPort:onHost:]",
	    (port = [server bindToPort: 0
				onHost: @"127.0.0.1"]))

	TEST(@"-[listen]", R([server listen]))

	TEST(@"-[connectToHost:onPort:]",
	    R([client connectToHost: @"127.0.0.1"
			     onPort: port]))