Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -498,17 +498,21 @@ @throw [OFSetOptionFailedException exceptionWithStream: self]; } - (OFString*)remoteAddress { + OFString *ret; + if (_socket == INVALID_SOCKET) @throw [OFNotConnectedException exceptionWithSocket: self]; if (_address == NULL) @throw [OFInvalidArgumentException exception]; - return of_address_to_string_and_port(_address, _addressLength, NULL); + of_address_to_string_and_port(_address, _addressLength, &ret, NULL); + + return ret; } - (bool)isListening { return _listening; Index: src/OFUDPSocket.h ================================================================== --- src/OFUDPSocket.h +++ src/OFUDPSocket.h @@ -64,20 +64,21 @@ + (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. + * @brief Gets the host and port for the specified address. * - * @param address The address for which the host and (optionally) the port - * should be returned + * @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. - * @return The host of the host / port pair + * @param address The address for which the host and port should be retrieved */ -+ (OFString*)hostForAddress: (of_udp_socket_address_t*)address - port: (uint16_t*)port; ++ (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 Index: src/OFUDPSocket.m ================================================================== --- src/OFUDPSocket.m +++ src/OFUDPSocket.m @@ -159,15 +159,16 @@ address->length = results[0]->addressLength; of_resolver_free(results); } -+ (OFString*)hostForAddress: (of_udp_socket_address_t*)address - port: (uint16_t*)port ++ (void)getHost: (OFString *__autoreleasing*)host + andPort: (uint16_t*)port + forAddress: (of_udp_socket_address_t*)address { - return of_address_to_string_and_port( - (struct sockaddr*)&address->address, address->length, port); + of_address_to_string_and_port( + (struct sockaddr*)&address->address, address->length, host, port); } - init { self = [super init]; Index: src/resolver.h ================================================================== --- src/resolver.h +++ src/resolver.h @@ -51,16 +51,17 @@ /*! * @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 - * @return The address as a string */ -extern OFString* of_address_to_string_and_port(struct sockaddr *address, - socklen_t addressLength, uint16_t *port); +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 Index: src/resolver.m ================================================================== --- src/resolver.m +++ src/resolver.m @@ -207,23 +207,26 @@ #endif return ret; } -OFString* +void of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength, - uint16_t *port) + OFString *__autoreleasing *host, uint16_t *port) { #ifdef HAVE_THREADSAFE_GETADDRINFO - char host[NI_MAXHOST]; + char hostCString[NI_MAXHOST]; char portCString[NI_MAXSERV]; /* FIXME: Add NI_DGRAM for UDP? */ - if (getnameinfo(address, addressLength, host, NI_MAXHOST, + 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) @@ -232,29 +235,27 @@ if (endptr != NULL && *endptr != '\0') @throw [OFAddressTranslationFailedException exception]; *port = (uint16_t)tmp; } - - return [OFString stringWithUTF8String: host]; #else - OFString *ret; - char *host; + char *hostCString; 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) + if ((hostCString = inet_ntoa( + ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL) @throw [OFAddressTranslationFailedException exception]; - ret = [OFString stringWithUTF8String: host]; + if (host != NULL) + *host = [OFString stringWithUTF8String: hostCString]; if (port != NULL) *port = OF_BSWAP16_IF_LE( ((struct sockaddr_in*)(void*)address)->sin_port); Index: tests/OFUDPSocketTests.m ================================================================== --- tests/OFUDPSocketTests.m +++ tests/OFUDPSocketTests.m @@ -32,10 +32,11 @@ 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" @@ -55,14 +56,15 @@ [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) + 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];