ObjFW  Check-in [fb590316ba]

Overview
Comment:Rename +[UDPSocket hostForAddress:port:]

It is now called +[getHost:andPort:forAddress:]. This makes it much
clearer from the selector alone what it actually does.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fb590316ba20dc31dbed2030a46705ecb1db9e328db135803c433ab19c12ff3a
User & Date: js on 2014-01-30 12:26:12
Other Links: manifest | tags
Context
2014-01-30
12:26
Rename of_tcpsocket* -> of_tcp_socket_* check-in: 7908c035e4 user: js tags: trunk
12:26
Rename +[UDPSocket hostForAddress:port:] check-in: fb590316ba user: js tags: trunk
2014-01-29
15:59
OFUDPSocket: Add observing check-in: c4f36e3692 user: js tags: trunk
Changes

Modified src/OFTCPSocket.m from [f24c3b3ba4] to [fb3f587e28].

496
497
498
499
500
501
502


503
504
505
506
507
508
509


510
511
512
513
514
515
516

	if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}

- (OFString*)remoteAddress
{


	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (_address == NULL)
		@throw [OFInvalidArgumentException exception];

	return of_address_to_string_and_port(_address, _addressLength, NULL);


}

- (bool)isListening
{
	return _listening;
}








>
>






|
>
>







496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520

	if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}

- (OFString*)remoteAddress
{
	OFString *ret;

	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

	if (_address == NULL)
		@throw [OFInvalidArgumentException exception];

	of_address_to_string_and_port(_address, _addressLength, &ret, NULL);

	return ret;
}

- (bool)isListening
{
	return _listening;
}

Modified src/OFUDPSocket.h from [7885c380e7] to [2a7d7dd320].

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

79
80
81
82
83
84
85
 *		  host / port pair
 */
+ (void)resolveAddressForHost: (OFString*)host
			 port: (uint16_t)port
		      address: (of_udp_socket_address_t*)address;

/*!
 * @brief Returns the host for the specified address and optionally the port.
 *
 * @param address The address for which the host and (optionally) the port
 *		  should be returned
 * @param port A pointer to an uint16_t. If it is not NULL, the port of the
 *	       host / port pair will be written to it.
 * @return The host of the host / port pair
 */
+ (OFString*)hostForAddress: (of_udp_socket_address_t*)address
		       port: (uint16_t*)port;


/*!
 * @brief Binds 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







|

|
|


|

|
|
>







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 *		  host / port pair
 */
+ (void)resolveAddressForHost: (OFString*)host
			 port: (uint16_t)port
		      address: (of_udp_socket_address_t*)address;

/*!
 * @brief Gets the host and port for the specified address.
 *
 * @param host A pointer to an OFString*. If it is not NULL, it will be set to
 *	       the host of the host / port pair.
 * @param port A pointer to an uint16_t. If it is not NULL, the port of the
 *	       host / port pair will be written to it.
 * @param address The address for which the host and port should be retrieved
 */
+ (void)getHost: (OFString *__autoreleasing*)host
	andPort: (uint16_t*)port
     forAddress: (of_udp_socket_address_t*)address;

/*!
 * @brief Binds 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

Modified src/OFUDPSocket.m from [e5e2000892] to [584c86fff6].

157
158
159
160
161
162
163
164
165

166
167
168
169
170
171
172
173
174
175
	memcpy(&address->address, results[0]->address,
	    results[0]->addressLength);
	address->length = results[0]->addressLength;

	of_resolver_free(results);
}

+ (OFString*)hostForAddress: (of_udp_socket_address_t*)address
		       port: (uint16_t*)port

{
	return of_address_to_string_and_port(
	    (struct sockaddr*)&address->address, address->length, port);
}

- init
{
	self = [super init];

	_socket = INVALID_SOCKET;







|
|
>

|
|







157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	memcpy(&address->address, results[0]->address,
	    results[0]->addressLength);
	address->length = results[0]->addressLength;

	of_resolver_free(results);
}

+ (void)getHost: (OFString *__autoreleasing*)host
	andPort: (uint16_t*)port
     forAddress: (of_udp_socket_address_t*)address
{
	of_address_to_string_and_port(
	    (struct sockaddr*)&address->address, address->length, host, port);
}

- init
{
	self = [super init];

	_socket = INVALID_SOCKET;

Modified src/resolver.h from [2514a83524] to [2d84450251].

49
50
51
52
53
54
55


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    int protocol);

/*!
 * @brief Converts the specified address to a string and port pair.
 *
 * @param address The address to convert to a string
 * @param addressLength The length of the address to convert to a string


 * @param port A pointer to an uint16_t which should be set to the port of the
 *	       address or NULL if the port is not needed
 * @return The address as a string
 */
extern OFString* of_address_to_string_and_port(struct sockaddr *address,
    socklen_t addressLength, uint16_t *port);

/*!
 * @brief Frees the results returned by @ref of_resolve_host.
 *
 * @param results The results returned by @ref of_resolve_host
 */
extern void of_resolver_free(of_resolver_result_t **results);
#ifdef __cplusplus
}
#endif







>
>


<

|
|










49
50
51
52
53
54
55
56
57
58
59

60
61
62
63
64
65
66
67
68
69
70
71
72
    int protocol);

/*!
 * @brief Converts the specified address to a string and port pair.
 *
 * @param address The address to convert to a string
 * @param addressLength The length of the address to convert to a string
 * @param host A pointer to an OFString* which should be set to the host of the
 *	       address or NULL if the host is not needed
 * @param port A pointer to an uint16_t which should be set to the port of the
 *	       address or NULL if the port is not needed

 */
extern void of_address_to_string_and_port(struct sockaddr *address,
    socklen_t addressLength, OFString *__autoreleasing *host, uint16_t *port);

/*!
 * @brief Frees the results returned by @ref of_resolve_host.
 *
 * @param results The results returned by @ref of_resolve_host
 */
extern void of_resolver_free(of_resolver_result_t **results);
#ifdef __cplusplus
}
#endif

Modified src/resolver.m from [03983c4b06] to [5111ebd364].

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
250

251
252
253
254

255
256
257
258
259
260
261
262
		@throw [OFUnlockFailedException exception];
# endif
#endif

	return ret;
}

OFString*
of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength,
    uint16_t *port)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	char host[NI_MAXHOST];
	char portCString[NI_MAXSERV];

	/* FIXME: Add NI_DGRAM for UDP? */
	if (getnameinfo(address, addressLength, host, NI_MAXHOST,
	    portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))
		@throw [OFAddressTranslationFailedException exception];




	if (port != NULL) {
		char *endptr;
		long tmp;

		if ((tmp = strtol(portCString, &endptr, 10)) > UINT16_MAX)
			@throw [OFOutOfRangeException exception];

		if (endptr != NULL && *endptr != '\0')
			@throw [OFAddressTranslationFailedException exception];

		*port = (uint16_t)tmp;
	}

	return [OFString stringWithUTF8String: host];
#else
	OFString *ret;
	char *host;

	if (address->sa_family != AF_INET)
		@throw [OFInvalidArgumentException exception];

# if OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];
# endif


	host = inet_ntoa(((struct sockaddr_in*)(void*)address)->sin_addr);
	if (host == NULL)
		@throw [OFAddressTranslationFailedException exception];


	ret = [OFString stringWithUTF8String: host];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(
		    ((struct sockaddr_in*)(void*)address)->sin_port);

# if OF_HAVE_THREADS
	if (!of_mutex_unlock(&mutex))







|

|


|



|



>
>
>












<
<

<
|









>
|
<


>
|







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
250
251
252

253
254
255
256
257
258
259
260
261
262
263
		@throw [OFUnlockFailedException exception];
# endif
#endif

	return ret;
}

void
of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength,
    OFString *__autoreleasing *host, uint16_t *port)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	char hostCString[NI_MAXHOST];
	char portCString[NI_MAXSERV];

	/* FIXME: Add NI_DGRAM for UDP? */
	if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST,
	    portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))
		@throw [OFAddressTranslationFailedException exception];

	if (host != NULL)
		*host = [OFString stringWithUTF8String: hostCString];

	if (port != NULL) {
		char *endptr;
		long tmp;

		if ((tmp = strtol(portCString, &endptr, 10)) > UINT16_MAX)
			@throw [OFOutOfRangeException exception];

		if (endptr != NULL && *endptr != '\0')
			@throw [OFAddressTranslationFailedException exception];

		*port = (uint16_t)tmp;
	}


#else

	char *hostCString;

	if (address->sa_family != AF_INET)
		@throw [OFInvalidArgumentException exception];

# if OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];
# endif

	if ((hostCString = inet_ntoa(
	    ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL)

		@throw [OFAddressTranslationFailedException exception];

	if (host != NULL)
		*host = [OFString stringWithUTF8String: hostCString];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(
		    ((struct sockaddr_in*)(void*)address)->sin_port);

# if OF_HAVE_THREADS
	if (!of_mutex_unlock(&mutex))

Modified tests/OFUDPSocketTests.m from [c4340e1e26] to [d252ab9bf0].

30
31
32
33
34
35
36

37
38
39
40
41
42
43
- (void)UDPSocketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFUDPSocket *sock;
	uint16_t port1, port2;
	of_udp_socket_address_t addr1, addr2, addr3;
	char buf[6];


	TEST(@"+[socket]", (sock = [OFUDPSocket socket]))

	TEST(@"-[bindToHost:port:]",
	    (port1 = [sock bindToHost: @"127.0.0.1"
				 port: 0]))








>







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
- (void)UDPSocketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFUDPSocket *sock;
	uint16_t port1, port2;
	of_udp_socket_address_t addr1, addr2, addr3;
	char buf[6];
	OFString *host;

	TEST(@"+[socket]", (sock = [OFUDPSocket socket]))

	TEST(@"-[bindToHost:port:]",
	    (port1 = [sock bindToHost: @"127.0.0.1"
				 port: 0]))

53
54
55
56
57
58
59
60
61
62

63
64
65
66
67
68
69
70

	TEST(@"-[receiveIntoBuffer:length:sender:]",
	    [sock receiveIntoBuffer: buf
			     length: 6
			     sender: &addr2] == 6 &&
	    !memcmp(buf, "Hello", 6))

	TEST(@"+[hostForAddress:port:]",
	    [[OFUDPSocket hostForAddress: &addr2
				    port: &port2] isEqual: @"127.0.0.1"] &&

	    port2 == port1)

	[OFUDPSocket resolveAddressForHost: @"127.0.0.1"
				      port: port1 + 1
				   address: &addr3];

	TEST(@"of_udp_socket_address_equal()",
	    of_udp_socket_address_equal(&addr1, &addr2) &&







|
|
|
>
|







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

	TEST(@"-[receiveIntoBuffer:length:sender:]",
	    [sock receiveIntoBuffer: buf
			     length: 6
			     sender: &addr2] == 6 &&
	    !memcmp(buf, "Hello", 6))

	TEST(@"+[getHost:andPort:forAddress:]",
	    R([OFUDPSocket getHost: &host
			   andPort: &port2
			forAddress: &addr2]) &&
	    [host isEqual: @"127.0.0.1"] && port2 == port1)

	[OFUDPSocket resolveAddressForHost: @"127.0.0.1"
				      port: port1 + 1
				   address: &addr3];

	TEST(@"of_udp_socket_address_equal()",
	    of_udp_socket_address_equal(&addr1, &addr2) &&