ObjFW  Check-in [8aeee6680a]

Overview
Comment:Add of_socket_address_ip_string()

This replaces of_address_to_string_and_port().

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8aeee6680ab34f188f60679252a25846b9204707182154bcdcbd0d24b09a5019
User & Date: js on 2018-07-28 20:21:01
Other Links: manifest | tags
Context
2018-07-29
14:32
OFDNSResolver: Initial support for resolving check-in: af4b18903d user: js tags: trunk
2018-07-28
20:21
Add of_socket_address_ip_string() check-in: 8aeee6680a user: js tags: trunk
18:45
Rename OFLocalization -> OFLocale check-in: aa0384d1bf user: js tags: trunk
Changes

Modified src/OFTCPSocket.m from [596449d526] to [7dca3c8c74].

44
45
46
47
48
49
50

51
52
53
54
55
56
57
#import "OFConnectionFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"

#import "OFSetOptionFailedException.h"

#import "socket.h"
#import "socket_helpers.h"
#import "resolver.h"

/* References for static linking */







>







44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#import "OFConnectionFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFSetOptionFailedException.h"

#import "socket.h"
#import "socket_helpers.h"
#import "resolver.h"

/* References for static linking */
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611




612

613
614
615
616
617
618
619
620
621
	[OFRunLoop of_addAsyncAcceptForTCPSocket: self
					   block: block];
}
#endif

- (OFString *)remoteAddress
{
	OFString *ret;

	if (_socket == INVALID_SOCKET)
		@throw [OFNotOpenException exceptionWithObject: self];

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





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


	return ret;
}

- (bool)isListening
{
	return _listening;
}








|







>
>
>
>
|
>

|







598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
	[OFRunLoop of_addAsyncAcceptForTCPSocket: self
					   block: block];
}
#endif

- (OFString *)remoteAddress
{
	of_socket_address_t address;

	if (_socket == INVALID_SOCKET)
		@throw [OFNotOpenException exceptionWithObject: self];

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

	if (_addressLength > sizeof(address.address))
		@throw [OFOutOfRangeException exception];

	memset(&address, '\0', sizeof(address));
	memcpy(&address.address, _address, _addressLength);
	address.length = _addressLength;

	return of_socket_address_ip_string(&address, NULL);
}

- (bool)isListening
{
	return _listening;
}

Modified src/OFUDPSocket.h from [b1604a829a] to [362b51f3c0].

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*!
 * @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h
 *
 * @brief A class which provides methods to create and use UDP sockets.
 *
 * Addresses are of type @ref of_socket_address_t. You can use
 * @ref resolveAddressForHost:port:address: to create an address for a host /
 * port pair and @ref getHost:andPort:forAddress: to get the host / port pair
 * for an address. If you want to compare two addresses, you can use
 * @ref of_socket_address_equal and you can use @ref of_socket_address_hash to
 * get a hash to use in e.g. @ref OFMapTable.
 *
 * @warning Even though the OFCopying protocol is implemented, it does *not*
 *	    return an independent copy of the socket, but instead retains it.
 *	    This is so that the socket can be used as a key for a dictionary,
 *	    so context can be associated with a socket. Using a socket in more







|
|







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*!
 * @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h
 *
 * @brief A class which provides methods to create and use UDP sockets.
 *
 * Addresses are of type @ref of_socket_address_t. You can use
 * @ref resolveAddressForHost:port:address: to create an address for a host /
 * port pair and @ref of_socket_address_ip_string to get the IP string / port
 * pair for an address. If you want to compare two addresses, you can use
 * @ref of_socket_address_equal and you can use @ref of_socket_address_hash to
 * get a hash to use in e.g. @ref OFMapTable.
 *
 * @warning Even though the OFCopying protocol is implemented, it does *not*
 *	    return an independent copy of the socket, but instead retains it.
 *	    This is so that the socket can be used as a key for a dictionary,
 *	    so context can be associated with a socket. Using a socket in more
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
183
 */
+ (void)asyncResolveAddressForHost: (OFString *)host
			      port: (uint16_t)port
			     block: (of_udp_socket_async_resolve_block_t)block;
# endif
#endif

/*!
 * @brief Gets the host and port for the specified address.
 *
 * @param host A pointer to an @ref 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 _Nonnull *_Nullable)host
	andPort: (nullable uint16_t *)port
     forAddress: (of_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
 *	       chosen, which can be obtained using the return value.







<
<
<
<
<
<
<
<
<
<
<
<
<







157
158
159
160
161
162
163













164
165
166
167
168
169
170
 */
+ (void)asyncResolveAddressForHost: (OFString *)host
			      port: (uint16_t)port
			     block: (of_udp_socket_async_resolve_block_t)block;
# endif
#endif














/*!
 * @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
 *	       chosen, which can be obtained using the return value.

Modified src/OFUDPSocket.m from [a0a08f26fb] to [df2fd645d0].

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
			   block: block] autorelease] start];

	objc_autoreleasePoolPop(pool);
}
# endif
#endif

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

- (instancetype)init
{
	self = [super init];

	_socket = INVALID_SOCKET;

	return self;







<
<
<
<
<
<
<
<







248
249
250
251
252
253
254








255
256
257
258
259
260
261
			   block: block] autorelease] start];

	objc_autoreleasePoolPop(pool);
}
# endif
#endif









- (instancetype)init
{
	self = [super init];

	_socket = INVALID_SOCKET;

	return self;

Modified src/resolver.h from [89d38aa172] to [8e4bcb1595].

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 *
 * @return An array of results. The list is terminated by NULL and should be
 *	   freed after use.
 */
extern of_resolver_result_t *_Nullable *_Nonnull of_resolve_host(OFString *host,
    uint16_t port, 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 @ref 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 _Nonnull *_Nullable host,
    uint16_t *_Nullable 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 *_Nullable *_Nonnull results);







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







49
50
51
52
53
54
55















56
57
58
59
60
61
62
 *
 * @return An array of results. The list is terminated by NULL and should be
 *	   freed after use.
 */
extern of_resolver_result_t *_Nullable *_Nonnull of_resolve_host(OFString *host,
    uint16_t port, int protocol);
















/*!
 * @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 *_Nullable *_Nonnull results);

Modified src/resolver.m from [444cb9ff29] to [de3749afa6].

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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	}
# 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_GETADDRINFO
	char hostCString[NI_MAXHOST];
	char portCString[NI_MAXSERV];

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

	@try {
# endif
		int error;

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

		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;
		}
# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#else
	char *hostCString;

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

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

	@try {
# endif
		if ((hostCString = inet_ntoa(
		    ((struct sockaddr_in *)(void *)address)->sin_addr)) == NULL)
			@throw [OFAddressTranslationFailedException
			    exceptionWithError: h_errno];

		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
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#endif
}

void
of_resolver_free(of_resolver_result_t **results)
{
#ifdef HAVE_GETADDRINFO
	freeaddrinfo(results[0]->private_);
#else
	free(results[0]->address);







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







250
251
252
253
254
255
256














































































257
258
259
260
261
262
263
	}
# endif
#endif

	return ret;
}















































































void
of_resolver_free(of_resolver_result_t **results)
{
#ifdef HAVE_GETADDRINFO
	freeaddrinfo(results[0]->private_);
#else
	free(results[0]->address);

Modified src/socket.h from [b7c9f96c1f] to [2892632dc8].

147
148
149
150
151
152
153











154
155
156
157
158
/*!
 * @brief Returns the hash for the specified of_socket_address_t.
 *
 * @param address The address to hash
 * @return The hash for the specified of_socket_address_t
 */
extern uint32_t of_socket_address_hash(of_socket_address_t *address);











#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END







>
>
>
>
>
>
>
>
>
>
>





147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
 * @brief Returns the hash for the specified of_socket_address_t.
 *
 * @param address The address to hash
 * @return The hash for the specified of_socket_address_t
 */
extern uint32_t of_socket_address_hash(of_socket_address_t *address);

/*!
 * @brief Converts the specified of_socket_address_t to an IP string and port.
 *
 * @param address 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 an IP string
 */
extern OFString *_Nonnull of_socket_address_ip_string(
    const of_socket_address_t *_Nonnull address, uint16_t *_Nullable port);
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END

Modified src/socket.m from [2bd834cec4] to [9e65efeb36].

218
219
220
221
222
223
224




























































225
226
227
228
229
230
231
	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
}
#endif





























































bool
of_socket_address_equal(of_socket_address_t *address1,
    of_socket_address_t *address2)
{
	struct sockaddr_in *addrIn1, *addrIn2;
#ifdef HAVE_IPV6







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







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
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
	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
}
#endif

static of_socket_address_t
parseIPv4(OFString *IPv4, uint16_t port)
{
	void *pool = objc_autoreleasePoolPush();
	of_socket_address_t ret;
	struct sockaddr_in *sin = (struct sockaddr_in *)&ret.address;

	memset(&ret, '\0', sizeof(ret));
	ret.length = sizeof(struct sockaddr_in);

	sin->sin_family = AF_INET;
	sin->sin_port = OF_BSWAP16_IF_LE(port);

	if (inet_pton(AF_INET, [IPv4 cStringWithEncoding: [OFLocale encoding]],
	    &sin->sin_addr) != 1)
		@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	return ret;
}

#ifdef HAVE_IPV6
static of_socket_address_t
parseIPv6(OFString *IPv6, uint16_t port)
{
	void *pool = objc_autoreleasePoolPush();
	of_socket_address_t ret;
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ret.address;

	memset(&ret, '\0', sizeof(ret));
	ret.length = sizeof(struct sockaddr_in6);

	sin6->sin6_family = AF_INET6;
	sin6->sin6_port = OF_BSWAP16_IF_LE(port);

	if (inet_pton(AF_INET6, [IPv6 cStringWithEncoding: [OFLocale encoding]],
	    &sin6->sin_addr6) != 1)
		@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);

	return ret;
}
#endif

of_socket_address_t
of_socket_address_parse_ip(OFString *IP, uint16_t port)
{
#ifdef HAVE_IPV6
	@try {
		return parseIPv6(IP, port);
	} @catch (OFInvalidFormatException *e) {
#endif
		return parseIPv4(IP, port);
#ifdef HAVE_IPV6
	}
#endif
}

bool
of_socket_address_equal(of_socket_address_t *address1,
    of_socket_address_t *address2)
{
	struct sockaddr_in *addrIn1, *addrIn2;
#ifdef HAVE_IPV6
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

356
357

358
359
360
361
362
363
364
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
	default:
		@throw [OFInvalidArgumentException exception];
	}

	return hash;
}

static of_socket_address_t
parseIPv4(OFString *IPv4, uint16_t port)
{
	void *pool = objc_autoreleasePoolPush();
	of_socket_address_t ret;
	struct sockaddr_in *sin = (struct sockaddr_in *)&ret.address;

	memset(&ret, '\0', sizeof(ret));
	ret.length = sizeof(struct sockaddr_in);

	sin->sin_family = AF_INET;
	sin->sin_port = OF_BSWAP16_IF_LE(port);

	if (inet_pton(AF_INET, [IPv4 cStringWithEncoding: [OFLocale encoding]],
	    &sin->sin_addr) != 1)
		@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);


	return ret;

}

#ifdef HAVE_IPV6
static of_socket_address_t
parseIPv6(OFString *IPv6, uint16_t port)
{
	void *pool = objc_autoreleasePoolPush();
	of_socket_address_t ret;
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ret.address;



	memset(&ret, '\0', sizeof(ret));
	ret.length = sizeof(struct sockaddr_in6);

	sin6->sin6_family = AF_INET6;
	sin6->sin6_port = OF_BSWAP16_IF_LE(port);

	if (inet_pton(AF_INET6, [IPv6 cStringWithEncoding: [OFLocale encoding]],
	    &sin6->sin_addr6) != 1)
		@throw [OFInvalidFormatException exception];

	objc_autoreleasePoolPop(pool);


	return ret;

}
#endif


of_socket_address_t
of_socket_address_parse_ip(OFString *IP, uint16_t port)
{



#ifdef HAVE_IPV6
	@try {
		return parseIPv6(IP, port);
	} @catch (OFInvalidFormatException *e) {
#endif
		return parseIPv4(IP, port);
#ifdef HAVE_IPV6

	}
#endif
}







|
|

<
<
|
<
<
|
|
<
<

<
|
|

|
>

|
>



|
|

<
<
|
>
>

<
<
|
<
<
|
<
<
|

|
>

|
>



>
|
<

>
>
>

|
|
<

<
|
>

<

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
430
431
432
433
434
435
436

437
438
439
440
441
442
443

444

445
446
447

448
	default:
		@throw [OFInvalidArgumentException exception];
	}

	return hash;
}

static OFString *
IPv4String(const of_socket_address_t *address, uint16_t *port)
{


	const struct sockaddr_in *sin =


	    (const struct sockaddr_in *)&address->address;
	char buffer[INET_ADDRSTRLEN];




	if (inet_ntop(AF_INET, &sin->sin_addr, buffer, sizeof(buffer)) == NULL)
		@throw [OFInvalidArgumentException exception];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(sin->sin_port);

	return [OFString stringWithCString: buffer
				  encoding: [OFLocale encoding]];
}

#ifdef HAVE_IPV6
static OFString *
IPv6String(const of_socket_address_t *address, uint16_t *port)
{


	const struct sockaddr_in6 *sin6 =
	    (const struct sockaddr_in6 *)&address->address;
	char buffer[INET6_ADDRSTRLEN];



	if (inet_ntop(AF_INET, &sin6->sin_addr6, buffer, sizeof(buffer)) ==


	    NULL)


		@throw [OFInvalidArgumentException exception];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(sin6->sin_port);

	return [OFString stringWithCString: buffer
				  encoding: [OFLocale encoding]];
}
#endif

OFString *
of_socket_address_ip_string(const of_socket_address_t *address, uint16_t *port)

{
	switch (address->address.ss_family) {
	case AF_INET:
		return IPv4String(address, port);
#ifdef HAVE_IPV6
	case AF_INET6:
		return IPv6String(address, port);

#endif

	default:
		@throw [OFInvalidArgumentException exception];
	}

}

Modified tests/OFUDPSocketTests.m from [da76f2699e] to [1bbed581ea].

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
			length: 6
		      receiver: &addr1]))

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

	/*







|
|
<
<
<
<







53
54
55
56
57
58
59
60
61




62
63
64
65
66
67
68
			length: 6
		      receiver: &addr1]))

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




	    [host isEqual: @"127.0.0.1"] && port2 == port1)

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

	/*