Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -405,11 +405,11 @@ if (port > 0) return port; #ifndef __wii__ addrLen = sizeof(addr.storage); - if (getsockname(_socket, (struct sockaddr*)&addr, &addrLen)) { + if (getsockname(_socket, (struct sockaddr*)&addr.storage, &addrLen)) { close(_socket); _socket = INVALID_SOCKET; @throw [OFBindFailedException exceptionWithHost: host port: port socket: self]; @@ -504,11 +504,11 @@ @throw [OFNotConnectedException exceptionWithSocket: self]; if (_address == NULL) @throw [OFInvalidArgumentException exception]; - return of_address_to_string(_address, _addressLength); + return of_address_to_string_and_port(_address, _addressLength, NULL); } - (bool)isListening { return _listening; Index: src/resolver.h ================================================================== --- src/resolver.h +++ src/resolver.h @@ -47,18 +47,20 @@ */ extern of_resolver_result_t** of_resolve_host(OFString *host, uint16_t port, int protocol); /*! - * @brief Converts the specified address to a string. + * @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(struct sockaddr *address, - socklen_t addressLength); +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 Index: src/resolver.m ================================================================== --- src/resolver.m +++ src/resolver.m @@ -209,18 +209,34 @@ return ret; } OFString* -of_address_to_string(struct sockaddr *address, socklen_t addressLength) +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]; - if (getnameinfo(address, addressLength, host, NI_MAXHOST, NULL, 0, - NI_NUMERICHOST | NI_NUMERICSERV)) + /* 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; @@ -237,10 +253,14 @@ 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