ObjFW  Check-in [2a1546da86]

Overview
Comment:Add of_socket_address_parse_ip()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2a1546da860e829bae72c2a8d6e449fcb95e766aaa9e69d0ee67ef42ebee6b8c
User & Date: js on 2018-07-28 18:29:59
Other Links: manifest | tags
Context
2018-07-28
18:45
Rename OFLocalization -> OFLocale check-in: aa0384d1bf user: js tags: trunk
18:29
Add of_socket_address_parse_ip() check-in: 2a1546da86 user: js tags: trunk
17:46
macros.h: Fix missing ) and #include check-in: d0b636d965 user: js tags: trunk
Changes

Modified src/socket.h from [01ae00b901] to [b7c9f96c1f].

120
121
122
123
124
125
126










127
128
129
130
131
132
133
extern bool of_socket_init(void);
extern int of_socket_errno(void);
# ifndef OF_WII
extern int of_getsockname(of_socket_t sock, struct sockaddr *restrict addr,
    socklen_t *restrict addrLen);
# 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
 */







>
>
>
>
>
>
>
>
>
>







120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
extern bool of_socket_init(void);
extern int of_socket_errno(void);
# ifndef OF_WII
extern int of_getsockname(of_socket_t sock, struct sockaddr *restrict addr,
    socklen_t *restrict addrLen);
# endif

/*!
 * @brief Parses the specified IP and port into an of_socket_address_t.
 *
 * @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
 */

Modified src/socket.m from [d4a188ba53] to [17b33daff7].

18
19
20
21
22
23
24


25
26
27

28
29
30
31

32
33
34
35
36
37
38
#include "config.h"

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

#include <errno.h>



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

#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"

#import "socket.h"

#ifdef OF_HAVE_THREADS
# include "threading.h"
#endif

#ifdef OF_NINTENDO_3DS
# include <3ds/types.h>
# include <3ds/services/soc.h>







>
>



>




>







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
#include "config.h"

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

#include <errno.h>

#import "OFLocalization.h"

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

#import "socket.h"
#import "socket_helpers.h"
#ifdef OF_HAVE_THREADS
# include "threading.h"
#endif

#ifdef OF_NINTENDO_3DS
# include <3ds/types.h>
# include <3ds/services/soc.h>
326
327
328
329
330
331
332




























































#endif
	default:
		@throw [OFInvalidArgumentException exception];
	}

	return hash;
}



































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
391
392
393
394
395
396
#endif
	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:
	    [OFLocalization 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:
	    [OFLocalization 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
}