ObjFW  Check-in [ab9810ef69]

Overview
Comment:socket.m: Replace inet_{ntop,pton} with own impl

Not all OSes supported by ObjFW provide them.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ab9810ef698f23037978cbc7c1546876d88470f7f3540789fbfcb2bde0ba1ed9
User & Date: js on 2018-08-06 22:34:30
Other Links: manifest | tags
Context
2018-08-11
11:58
OFDNSResolver: Minor refactoring check-in: 4afaa63320 user: js tags: trunk
2018-08-06
22:34
socket.m: Replace inet_{ntop,pton} with own impl check-in: ab9810ef69 user: js tags: trunk
2018-08-05
20:00
Add a few missing casts check-in: 4f49f55859 user: js tags: trunk
Changes

Modified src/socket.h from [f9393485f9] to [232e64699e].

123
124
125
126
127
128
129






















130
131
132
133
134
135
136
 * @param IP The IP to parse
 * @param port The port to use
 * @return The parsed IP and port as an of_socket_address_t
 */
extern of_socket_address_t of_socket_address_parse_ip(
    OFString *IP, uint16_t port);























/*!
 * @brief Compares two of_socket_address_t for equality.
 *
 * @param address1 The address to compare with the second address
 * @param address2 The second address
 * @return Whether the two addresses are equal
 */







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







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 * @param IP The IP to parse
 * @param port The port to use
 * @return The parsed IP and port as an of_socket_address_t
 */
extern of_socket_address_t of_socket_address_parse_ip(
    OFString *IP, uint16_t port);

/*!
 * @brief Parses the specified IPv4 and port into an of_socket_address_t.
 *
 * @param IP The IPv4 to parse
 * @param port The port to use
 * @return The parsed IPv4 and port as an of_socket_address_t
 */
extern of_socket_address_t of_socket_address_parse_ipv4(
    OFString *IP, uint16_t port);

#ifdef OF_HAVE_IPV6
/*!
 * @brief Parses the specified IPv6 and port into an of_socket_address_t.
 *
 * @param IP The IPv6 to parse
 * @param port The port to use
 * @return The parsed IPv6 and port as an of_socket_address_t
 */
extern of_socket_address_t of_socket_address_parse_ipv6(
    OFString *IP, uint16_t port);
#endif

/*!
 * @brief Compares two of_socket_address_t for equality.
 *
 * @param address1 The address to compare with the second address
 * @param address2 The second address
 * @return Whether the two addresses are equal
 */

Modified src/socket.m from [06c3fc8fe2] to [2650e52f57].

19
20
21
22
23
24
25


26

27
28
29
30
31
32
33

#ifdef OF_NINTENDO_3DS
# include <malloc.h>  /* For memalign() */
#endif

#include <errno.h>



#import "OFLocale.h"


#import "OFException.h"  /* For some E* -> WSAE* defines */
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"








>
>

>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#ifdef OF_NINTENDO_3DS
# include <malloc.h>  /* For memalign() */
#endif

#include <errno.h>

#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFLocale.h"
#import "OFString.h"

#import "OFException.h"  /* For some E* -> WSAE* defines */
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"

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
		@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 *addrIn = (struct sockaddr_in *)&ret.address;



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

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


	if (inet_pton(AF_INET, [IPv4 cStringWithEncoding: [OFLocale encoding]],


	    &addrIn->sin_addr) != 1)






		@throw [OFInvalidFormatException exception];















	objc_autoreleasePoolPop(pool);

	return ret;
}

#ifdef OF_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 *addrIn6 = (struct sockaddr_in6 *)&ret.address;


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

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


	if (inet_pton(AF_INET6, [IPv6 cStringWithEncoding: [OFLocale encoding]],























	    &addrIn6->sin6_addr) != 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 OF_HAVE_IPV6
	@try {
		return parseIPv6(IP, port);
	} @catch (OFInvalidFormatException *e) {
#endif
		return parseIPv4(IP, port);
#ifdef OF_HAVE_IPV6
	}
#endif
}

bool
of_socket_address_equal(of_socket_address_t *address1,







|
|

>
>

>
>


>
>







>
|
>
>
|
>
>
>
>
>
>
|

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






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|




>







>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|


|







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
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
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
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
}
#endif

of_socket_address_t
of_socket_address_parse_ipv4(OFString *IPv4, uint16_t port)
{
	/* TODO: Support IPs that are not in the a.b.c.d format? */

	void *pool = objc_autoreleasePoolPush();
	OFCharacterSet *whitespaceCharacterSet =
	    [OFCharacterSet whitespaceCharacterSet];
	of_socket_address_t ret;
	struct sockaddr_in *addrIn = (struct sockaddr_in *)&ret.address;
	OFArray OF_GENERIC(OFString *) *components;
	uint32_t addr;

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

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

	components = [IPv4 componentsSeparatedByString: @"."];

	if ([components count] != 4)
		@throw [OFInvalidFormatException exception];

	addr = 0;

	for (OFString *component in components) {
		intmax_t number;

		if ([component length] == 0)
			@throw [OFInvalidFormatException exception];

		if ([component indexOfCharacterFromSet:
		    whitespaceCharacterSet] != OF_NOT_FOUND)
			@throw [OFInvalidFormatException exception];

		number = [component decimalValue];

		if (number < 0 || number > UINT8_MAX)
			@throw [OFInvalidFormatException exception];

		addr = (addr << 8) | (number & 0xFF);
	}

	addrIn->sin_addr.s_addr = OF_BSWAP32_IF_LE(addr);

	objc_autoreleasePoolPop(pool);

	return ret;
}

#ifdef OF_HAVE_IPV6
static uint16_t
parseIPv6Component(OFString *component)
{
	uintmax_t number;

	if ([component indexOfCharacterFromSet:
	    [OFCharacterSet whitespaceCharacterSet]] != OF_NOT_FOUND)
		@throw [OFInvalidFormatException exception];

	number = [component hexadecimalValue];

	if (number > UINT16_MAX)
		@throw [OFInvalidFormatException exception];

	return (uint16_t)number;
}

of_socket_address_t
of_socket_address_parse_ipv6(OFString *IPv6, uint16_t port)
{
	void *pool = objc_autoreleasePoolPush();
	of_socket_address_t ret;
	struct sockaddr_in6 *addrIn6 = (struct sockaddr_in6 *)&ret.address;
	size_t doubleColon;

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

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

	doubleColon = [IPv6 rangeOfString: @"::"].location;

	if (doubleColon != OF_NOT_FOUND) {
		OFString *left = [IPv6 substringWithRange:
		    of_range(0, doubleColon)];
		OFString *right = [IPv6 substringWithRange:
		    of_range(doubleColon + 2, [IPv6 length] - doubleColon - 2)];
		OFArray OF_GENERIC(OFString *) *leftComponents;
		OFArray OF_GENERIC(OFString *) *rightComponents;
		size_t i;

		if ([right hasPrefix: @":"] || [right containsString: @"::"])
			@throw [OFInvalidFormatException exception];

		leftComponents = [left componentsSeparatedByString: @":"];
		rightComponents = [right componentsSeparatedByString: @":"];

		if ([leftComponents count] + [rightComponents count] > 7)
			@throw [OFInvalidFormatException exception];

		i = 0;
		for (OFString *component in leftComponents) {
			uint16_t number = parseIPv6Component(component);

			addrIn6->sin6_addr.s6_addr[i++] = number >> 8;
			addrIn6->sin6_addr.s6_addr[i++] = number;
		}

		i = 16;
		for (OFString *component in [rightComponents reversedArray]) {
			uint16_t number = parseIPv6Component(component);

			addrIn6->sin6_addr.s6_addr[--i] = number >> 8;
			addrIn6->sin6_addr.s6_addr[--i] = number;
		}
	} else {
		OFArray OF_GENERIC(OFString *) *components =
		    [IPv6 componentsSeparatedByString: @":"];
		size_t i;

		if ([components count] != 8)
			@throw [OFInvalidFormatException exception];

		i = 0;
		for (OFString *component in components) {
			uint16_t number;

			if ([component length] == 0)
				@throw [OFInvalidFormatException exception];

			number = parseIPv6Component(component);

			addrIn6->sin6_addr.s6_addr[i++] = number >> 8;
			addrIn6->sin6_addr.s6_addr[i++] = number;
		}
	}

	objc_autoreleasePoolPop(pool);

	return ret;
}
#endif

of_socket_address_t
of_socket_address_parse_ip(OFString *IP, uint16_t port)
{
#ifdef OF_HAVE_IPV6
	@try {
		return of_socket_address_parse_ipv6(IP, port);
	} @catch (OFInvalidFormatException *e) {
#endif
		return of_socket_address_parse_ipv4(IP, port);
#ifdef OF_HAVE_IPV6
	}
#endif
}

bool
of_socket_address_equal(of_socket_address_t *address1,
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
}

static OFString *
IPv4String(const of_socket_address_t *address, uint16_t *port)
{
	const struct sockaddr_in *addrIn =
	    (const struct sockaddr_in *)&address->address;
	char buffer[INET_ADDRSTRLEN];


	if (inet_ntop(AF_INET, &addrIn->sin_addr, buffer, sizeof(buffer)) ==
	    NULL)

		@throw [OFInvalidArgumentException exception];


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

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

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

	const struct sockaddr_in6 *addrIn6 =
	    (const struct sockaddr_in6 *)&address->address;



	char buffer[INET6_ADDRSTRLEN];




























	if (inet_ntop(AF_INET, &addrIn6->sin6_addr, buffer, sizeof(buffer)) ==


	    NULL)


		@throw [OFInvalidArgumentException exception];




















	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(addrIn6->sin6_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) {







|
>

<
<
>
|
>




|
<






>


>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|
<







499
500
501
502
503
504
505
506
507
508


509
510
511
512
513
514
515
516

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

589
590
591
592
593
594
595
}

static OFString *
IPv4String(const of_socket_address_t *address, uint16_t *port)
{
	const struct sockaddr_in *addrIn =
	    (const struct sockaddr_in *)&address->address;
	uint32_t addr = OF_BSWAP32_IF_LE(addrIn->sin_addr.s_addr);
	OFString *string;



	string = [OFString stringWithFormat: @"%u.%u.%u.%u",
	    (addr & 0xFF000000) >> 24, (addr & 0x00FF0000) >> 16,
	    (addr & 0x0000FF00) >>  8, addr & 0x000000FF];

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

	return string;

}

#ifdef OF_HAVE_IPV6
static OFString *
IPv6String(const of_socket_address_t *address, uint16_t *port)
{
	OFMutableString *string = [OFMutableString string];
	const struct sockaddr_in6 *addrIn6 =
	    (const struct sockaddr_in6 *)&address->address;
	int_fast8_t zerosStart = -1, maxZerosStart = -1;
	uint_fast8_t zerosCount = 0, maxZerosCount = 0;
	bool first = true;

	for (uint_fast8_t i = 0; i < 16; i += 2) {
		if (addrIn6->sin6_addr.s6_addr[i] == 0 &&
		    addrIn6->sin6_addr.s6_addr[i + 1] == 0) {
			if (zerosStart >= 0)
				zerosCount++;
			else {
				zerosStart = i;
				zerosCount = 1;
			}
		} else {
			if (zerosCount > maxZerosCount) {
				maxZerosStart = zerosStart;
				maxZerosCount = zerosCount;
			}

			zerosStart = -1;
		}
	}
	if (zerosCount > maxZerosCount) {
		maxZerosStart = zerosStart;
		maxZerosCount = zerosCount;
	}

	if (maxZerosCount >= 2) {
		for (uint_fast8_t i = 0; i < maxZerosStart; i += 2) {
			[string appendFormat:
			    (first ? @"%x" : @":%x"),
			    (addrIn6->sin6_addr.s6_addr[i] << 8) |
			    addrIn6->sin6_addr.s6_addr[i + 1]];
			first = false;
		}

		[string appendString: @"::"];
		first = true;

		for (uint_fast8_t i = maxZerosStart + (maxZerosCount * 2);
		    i < 16; i += 2) {
			[string appendFormat:
			    (first ? @"%x" : @":%x"),
			    (addrIn6->sin6_addr.s6_addr[i] << 8) |
			    addrIn6->sin6_addr.s6_addr[i + 1]];
			first = false;
		}
	} else {
		for (uint_fast8_t i = 0; i < 16; i += 2) {
			[string appendFormat:
			    (first ? @"%x" : @":%x"),
			    (addrIn6->sin6_addr.s6_addr[i] << 8) |
			    addrIn6->sin6_addr.s6_addr[i + 1]];
			first = false;
		}
	}

	[string makeImmutable];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(addrIn6->sin6_port);

	return string;

}
#endif

OFString *
of_socket_address_ip_string(const of_socket_address_t *address, uint16_t *port)
{
	switch (address->address.ss_family) {

Modified tests/Makefile from [6fc41de27b] to [62b4edb1f2].

53
54
55
56
57
58
59
60

61
62
63
64
65
66
67
	     OFSHA224HashTests.m	\
	     OFSHA256HashTests.m	\
	     OFSHA384HashTests.m	\
	     OFSHA512HashTests.m
SRCS_PLUGINS = OFPluginTests.m
SRCS_SOCKETS = OFKernelEventObserverTests.m	\
	       OFTCPSocketTests.m		\
	       OFUDPSocketTests.m

SRCS_THREADS = OFThreadTests.m

IOS_USER ?= mobile
IOS_TMP ?= /tmp/objfw-test

include ../buildsys.mk








|
>







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	     OFSHA224HashTests.m	\
	     OFSHA256HashTests.m	\
	     OFSHA384HashTests.m	\
	     OFSHA512HashTests.m
SRCS_PLUGINS = OFPluginTests.m
SRCS_SOCKETS = OFKernelEventObserverTests.m	\
	       OFTCPSocketTests.m		\
	       OFUDPSocketTests.m		\
	       SocketTests.m
SRCS_THREADS = OFThreadTests.m

IOS_USER ?= mobile
IOS_TMP ?= /tmp/objfw-test

include ../buildsys.mk

Added tests/SocketTests.m version [25f3a27b64].



























































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
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
60
61
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
87
88
89
90
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018
 *   Jonathan Schleifer <js@heap.zone>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * 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"

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

#import "OFInvalidFormatException.h"

#import "TestsAppDelegate.h"

#import "socket.h"

#define SOCKADDR_IN(a) (*(struct sockaddr_in *)&a.address)
#define SOCKADDR_IN6(a) (*(struct sockaddr_in6 *)&a.address)
#define COMPARE_V6(a, a0, a1, a2, a3, a4, a5, a6, a7)		\
	(SOCKADDR_IN6(a).sin6_addr.s6_addr[0] == (a0 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[1] == (a0 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[2] == (a1 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[3] == (a1 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[4] == (a2 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[5] == (a2 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[6] == (a3 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[7] == (a3 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[8] == (a4 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[9] == (a4 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[10] == (a5 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[11] == (a5 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[12] == (a6 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[13] == (a6 & 0xFF) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[14] == (a7 >> 8) &&	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[15] == (a7 & 0xFF))
#define SET_V6(a, a0, a1, a2, a3, a4, a5, a6, a7)		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[0] = a0 >> 8;		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[1] = a0 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[2] = a1 >> 8;		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[3] = a1 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[4] = a2 >> 8;		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[5] = a2 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[6] = a3 >> 8;		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[7] = a3 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[8] = a4 >> 8;		\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[9] = a4 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[10] = a5 >> 8;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[11] = a5 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[12] = a6 >> 8;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[13] = a6 & 0xFF;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[14] = a7 >> 8;	\
	SOCKADDR_IN6(a).sin6_addr.s6_addr[15] = a7 & 0xFF;

static OFString *module = @"Socket";

@implementation TestsAppDelegate (SocketTests)
- (void)socketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	of_socket_address_t addr;
	uint16_t port;

	TEST(@"Parsing an IPv4",
	    R(addr = of_socket_address_parse_ip(@"127.0.0.1", 1234)) &&
	    OF_BSWAP32_IF_LE(SOCKADDR_IN(addr).sin_addr.s_addr) == 0x7F000001 &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN(addr).sin_port) == 1234)

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #1",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"127.0.0.0.1", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #2",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"127.0.0.256", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #3",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"127.0.0. 1", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #4",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@" 127.0.0.1", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #5",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"127.0.a.1", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv4 #6",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"127.0..1", 1234))

	TEST(@"Converting an IPv4 to a string",
	    [of_socket_address_ip_string(&addr, &port) isEqual: @"127.0.0.1"] &&
	    port == 1234)

#ifdef OF_HAVE_IPV6
	TEST(@"Parsing an IPv6 #1",
	    R(addr = of_socket_address_parse_ip(
	    @"1122:3344:5566:7788:99aa:bbCc:ddee:ff00", 1234)) &&
	    COMPARE_V6(addr,
	    0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00) &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN6(addr).sin6_port) == 1234)

	TEST(@"Parsing an IPv6 #2",
	    R(addr = of_socket_address_parse_ip(@"::", 1234)) &&
	    COMPARE_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0) &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN6(addr).sin6_port) == 1234)

	TEST(@"Parsing an IPv6 #3",
	    R(addr = of_socket_address_parse_ip(@"aaAa::bBbb", 1234)) &&
	    COMPARE_V6(addr, 0xAAAA, 0, 0, 0, 0, 0, 0, 0xBBBB) &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN6(addr).sin6_port) == 1234)

	TEST(@"Parsing an IPv6 #4",
	    R(addr = of_socket_address_parse_ip(@"aaAa::", 1234)) &&
	    COMPARE_V6(addr, 0xAAAA, 0, 0, 0, 0, 0, 0, 0) &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN6(addr).sin6_port) == 1234)

	TEST(@"Parsing an IPv6 #5",
	    R(addr = of_socket_address_parse_ip(@"::aaAa", 1234)) &&
	    COMPARE_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0xAAAA) &&
	    OF_BSWAP16_IF_LE(SOCKADDR_IN6(addr).sin6_port) == 1234)

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #1",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1:::2", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #2",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1: ::2", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #3",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1:: :2", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #4",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1::2::3", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #5",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"10000::1", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #6",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"::10000", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #7",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"::1::", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #8",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1:2:3:4:5:6:7:", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #9",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1:2:3:4:5:6:7::", 1234))

	EXPECT_EXCEPTION(@"Refusing invalid IPv6 #10",
	    OFInvalidFormatException,
	    of_socket_address_parse_ip(@"1:2", 1234))

	SET_V6(addr, 0, 0, 0, 0, 0, 0, 0, 0)
	TEST(@"Converting an IPv6 to a string #1",
	    [of_socket_address_ip_string(&addr, &port) isEqual: @"::"] &&
	    port == 1234)

	SET_V6(addr, 0, 0, 0, 0, 0, 0, 0, 1)
	TEST(@"Converting an IPv6 to a string #2",
	    [of_socket_address_ip_string(&addr, &port) isEqual: @"::1"] &&
	    port == 1234)

	SET_V6(addr, 1, 0, 0, 0, 0, 0, 0, 0)
	TEST(@"Converting an IPv6 to a string #3",
	    [of_socket_address_ip_string(&addr, &port) isEqual: @"1::"] &&
	    port == 1234)

	SET_V6(addr,
	    0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00)
	TEST(@"Converting an IPv6 to a string #4",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"1122:3344:5566:7788:99aa:bbcc:ddee:ff00"] &&
	    port == 1234)

	SET_V6(addr, 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0)
	TEST(@"Converting an IPv6 to a string #5",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"1122:3344:5566:7788:99aa:bbcc:ddee:0"] &&
	    port == 1234)

	SET_V6(addr, 0x1122, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0)
	TEST(@"Converting an IPv6 to a string #6",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"1122:3344:5566:7788:99aa:bbcc::"] &&
	    port == 1234)

	SET_V6(addr, 0, 0x3344, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00)
	TEST(@"Converting an IPv6 to a string #7",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"0:3344:5566:7788:99aa:bbcc:ddee:ff00"] &&
	    port == 1234)

	SET_V6(addr, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0xDDEE, 0xFF00)
	TEST(@"Converting an IPv6 to a string #8",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"::5566:7788:99aa:bbcc:ddee:ff00"] &&
	    port == 1234)

	SET_V6(addr, 0, 0, 0x5566, 0, 0, 0, 0xDDEE, 0xFF00)
	TEST(@"Converting an IPv6 to a string #9",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"0:0:5566::ddee:ff00"] &&
	    port == 1234)

	SET_V6(addr, 0, 0, 0x5566, 0x7788, 0x99AA, 0xBBCC, 0, 0)
	TEST(@"Converting an IPv6 to a string #10",
	    [of_socket_address_ip_string(&addr, &port) isEqual:
	    @"::5566:7788:99aa:bbcc:0:0"] &&
	    port == 1234)
#endif

	[pool drain];
}
@end

Modified tests/TestsAppDelegate.h from [30593ddc73] to [f163cc51a8].

263
264
265
266
267
268
269




    <OFXMLParserDelegate, OFXMLElementBuilderDelegate>
- (void)XMLParserTests;
@end

@interface TestsAppDelegate (PBKDF2Tests)
- (void)PBKDF2Tests;
@end











>
>
>
>
263
264
265
266
267
268
269
270
271
272
273
    <OFXMLParserDelegate, OFXMLElementBuilderDelegate>
- (void)XMLParserTests;
@end

@interface TestsAppDelegate (PBKDF2Tests)
- (void)PBKDF2Tests;
@end

@interface TestsAppDelegate (SocketTests)
- (void)socketTests;
@end

Modified tests/TestsAppDelegate.m from [ffb973f9b8] to [33db6e86ef].

404
405
406
407
408
409
410

411
412
413
414
415
416
417
#endif
	[self PBKDF2Tests];
	[self scryptTests];
#if defined(OF_HAVE_FILES) && defined(HAVE_CODEPAGE_437)
	[self INIFileTests];
#endif
#ifdef OF_HAVE_SOCKETS

	[self TCPSocketTests];
	[self UDPSocketTests];
	[self kernelEventObserverTests];
#endif
#ifdef OF_HAVE_THREADS
	[self threadTests];
#endif







>







404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#endif
	[self PBKDF2Tests];
	[self scryptTests];
#if defined(OF_HAVE_FILES) && defined(HAVE_CODEPAGE_437)
	[self INIFileTests];
#endif
#ifdef OF_HAVE_SOCKETS
	[self socketTests];
	[self TCPSocketTests];
	[self UDPSocketTests];
	[self kernelEventObserverTests];
#endif
#ifdef OF_HAVE_THREADS
	[self threadTests];
#endif