ObjFW  Check-in [44c595058b]

Overview
Comment:of_address_to_string: Add a way to get the port
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 44c595058bba22c134e6631760288881f6bfe48f784fb75da37ac27f86213169
User & Date: js on 2014-01-27 12:34:10
Other Links: manifest | tags
Context
2014-01-27
22:01
Add OFUDPSocket check-in: 5025cba435 user: js tags: trunk
12:34
of_address_to_string: Add a way to get the port check-in: 44c595058b user: js tags: trunk
11:58
Fix socket.h not being installed check-in: d4c90c0750 user: js tags: trunk
Changes

Modified src/OFTCPSocket.m from [4b99095be9] to [f24c3b3ba4].

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	}

	if (port > 0)
		return port;

#ifndef __wii__
	addrLen = sizeof(addr.storage);
	if (getsockname(_socket, (struct sockaddr*)&addr, &addrLen)) {
		close(_socket);
		_socket = INVALID_SOCKET;
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];
	}








|







403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
	}

	if (port > 0)
		return port;

#ifndef __wii__
	addrLen = sizeof(addr.storage);
	if (getsockname(_socket, (struct sockaddr*)&addr.storage, &addrLen)) {
		close(_socket);
		_socket = INVALID_SOCKET;
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];
	}

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

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

	return of_address_to_string(_address, _addressLength);
}

- (bool)isListening
{
	return _listening;
}








|







502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
{
	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;
}

Modified src/resolver.h from [5eef0c526a] to [a012300f05].

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
 * @return An array of results. The list is terminated by NULL and should be
 *	   free'd after use.
 */
extern of_resolver_result_t** of_resolve_host(OFString *host, uint16_t port,
    int protocol);

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


 * @return The address as a string
 */
extern OFString* of_address_to_string(struct sockaddr *address,
    socklen_t addressLength);

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







|



>
>


|
|










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
 * @return An array of results. The list is terminated by NULL and should be
 *	   free'd after use.
 */
extern of_resolver_result_t** 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 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

Modified src/resolver.m from [cc5718b725] to [de483104d4].

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
# endif
#endif

	return ret;
}

OFString*
of_address_to_string(struct sockaddr *address, socklen_t addressLength)

{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	char host[NI_MAXHOST];



	if (getnameinfo(address, addressLength, host, NI_MAXHOST, NULL, 0,
	    NI_NUMERICHOST | NI_NUMERICSERV))
		@throw [OFAddressTranslationFailedException exception];














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

	return ret;
#endif







|
>



>

>
|
|

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




















>
>
>
>







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
264
265
266
267
268
# 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))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
#endif