ObjFW  Check-in [04dc76c8c3]

Overview
Comment:Add -[remoteAddress] to OFTCPSocket.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 04dc76c8c3a56365a3dce3115770d69a4abbfb640527a899c1784aa370a3a612
User & Date: js on 2010-04-11 17:55:39
Other Links: manifest | tags
Context
2010-04-11
18:04
Fix +[stringWithPath:] test on Win32 (it's \ there, not /!). check-in: 0bde0871bc user: js tags: trunk
17:55
Add -[remoteAddress] to OFTCPSocket. check-in: 04dc76c8c3 user: js tags: trunk
17:43
Don't require OFCopying protocol for the thread's object. check-in: e5240d68e8 user: js tags: trunk
Changes

Modified src/OFTCPSocket.h from [f3e771c9da] to [337485173f].

70
71
72
73
74
75
76







77
 */
- (OFTCPSocket*)accept;

/**
 * Enable or disable keep alives for the connection.
 */
- enableKeepAlives: (BOOL)enable;







@end







>
>
>
>
>
>
>

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 */
- (OFTCPSocket*)accept;

/**
 * Enable or disable keep alives for the connection.
 */
- enableKeepAlives: (BOOL)enable;

/**
 * Returns the remote address of the socket. Only works with accepted sockets!
 *
 * \return The remote address as a string.
 */
- (OFString*)remoteAddress;
@end

Modified src/OFTCPSocket.m from [605f8714f2] to [0f7c2e2900].

11
12
13
14
15
16
17


18
19
20

21
22
23
24
25
26
27

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>



#if !defined(HAVE_THREADSAFE_GETADDRINFO) && !defined(_WIN32)
# include <netinet/in.h>

#endif

#import "OFTCPSocket.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"








>
>



>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <assert.h>

#if !defined(HAVE_THREADSAFE_GETADDRINFO) && !defined(_WIN32)
# include <netinet/in.h>
# include <arpa/inet.h>
#endif

#import "OFTCPSocket.h"
#import "OFString.h"
#import "OFExceptions.h"
#import "macros.h"

365
366
367
368
369
370
371
















































372
373
374
375
376
377
378
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa];

	return self;
}

















































- close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	close(sock);







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







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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa];

	return self;
}

- (OFString*)remoteAddress
{
	if (saddr == NULL || saddr_len == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	char *node = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo(saddr, saddr_len, node, NI_MAXHOST, NULL, 0,
		    NI_NUMERICHOST))
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
	} @finally {
		[self freeMemory: node];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
#else
	char *node;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		node = inet_ntoa(((struct sockaddr_in*)saddr)->sin_addr);

		if (node == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
# endif
#endif
}

- close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	close(sock);

Modified tests/OFTCPSocketTests.m from [0def59b121] to [a3ffe30f8b].

53
54
55
56
57
58
59



60
61
62
63
64
65
66
67
68

	TEST(@"-[connectToService:onNode:]",
	    [client connectToService: service
			      onNode: @"localhost"])

	TEST(@"-[accept]", (accepted = [server accept]))




	TEST(@"-[writeString:]", [client writeString: @"Hello!"])

	TEST(@"-[readNBytes:intoBuffer:]", [accepted readNBytes: 6
						     intoBuffer: buf] &&
	    !memcmp(buf, "Hello!", 6))

	[pool drain];
}
@end







>
>
>









53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

	TEST(@"-[connectToService:onNode:]",
	    [client connectToService: service
			      onNode: @"localhost"])

	TEST(@"-[accept]", (accepted = [server accept]))

	TEST(@"-[remoteAddress]",
	    [[accepted remoteAddress] isEqual: @"127.0.0.1"])

	TEST(@"-[writeString:]", [client writeString: @"Hello!"])

	TEST(@"-[readNBytes:intoBuffer:]", [accepted readNBytes: 6
						     intoBuffer: buf] &&
	    !memcmp(buf, "Hello!", 6))

	[pool drain];
}
@end