Overview
Comment: | Add of_socket_address_ip_string()
This replaces of_address_to_string_and_port(). |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
8aeee6680ab34f188f60679252a25846 |
User & Date: | js on 2018-07-28 20:21:01 |
Other Links: | manifest | tags |
Context
2018-07-29
| ||
14:32 | OFDNSResolver: Initial support for resolving check-in: af4b18903d user: js tags: trunk | |
2018-07-28
| ||
20:21 | Add of_socket_address_ip_string() check-in: 8aeee6680a user: js tags: trunk | |
18:45 | Rename OFLocalization -> OFLocale check-in: aa0384d1bf user: js tags: trunk | |
Changes
Modified src/OFTCPSocket.m from [596449d526] to [7dca3c8c74].
︙ | ︙ | |||
44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #import "OFConnectionFailedException.h" #import "OFGetOptionFailedException.h" #import "OFInvalidArgumentException.h" #import "OFListenFailedException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" #import "OFOutOfMemoryException.h" #import "OFSetOptionFailedException.h" #import "socket.h" #import "socket_helpers.h" #import "resolver.h" /* References for static linking */ | > | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #import "OFConnectionFailedException.h" #import "OFGetOptionFailedException.h" #import "OFInvalidArgumentException.h" #import "OFListenFailedException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "OFSetOptionFailedException.h" #import "socket.h" #import "socket_helpers.h" #import "resolver.h" /* References for static linking */ |
︙ | ︙ | |||
597 598 599 600 601 602 603 | [OFRunLoop of_addAsyncAcceptForTCPSocket: self block: block]; } #endif - (OFString *)remoteAddress { | | > > > > | > | | 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | [OFRunLoop of_addAsyncAcceptForTCPSocket: self block: block]; } #endif - (OFString *)remoteAddress { of_socket_address_t address; if (_socket == INVALID_SOCKET) @throw [OFNotOpenException exceptionWithObject: self]; if (_address == NULL) @throw [OFInvalidArgumentException exception]; if (_addressLength > sizeof(address.address)) @throw [OFOutOfRangeException exception]; memset(&address, '\0', sizeof(address)); memcpy(&address.address, _address, _addressLength); address.length = _addressLength; return of_socket_address_ip_string(&address, NULL); } - (bool)isListening { return _listening; } |
︙ | ︙ |
Modified src/OFUDPSocket.h from [b1604a829a] to [362b51f3c0].
︙ | ︙ | |||
81 82 83 84 85 86 87 | /*! * @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h * * @brief A class which provides methods to create and use UDP sockets. * * Addresses are of type @ref of_socket_address_t. You can use * @ref resolveAddressForHost:port:address: to create an address for a host / | | | | 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | /*! * @class OFUDPSocket OFUDPSocket.h ObjFW/OFUDPSocket.h * * @brief A class which provides methods to create and use UDP sockets. * * Addresses are of type @ref of_socket_address_t. You can use * @ref resolveAddressForHost:port:address: to create an address for a host / * port pair and @ref of_socket_address_ip_string to get the IP string / port * pair for an address. If you want to compare two addresses, you can use * @ref of_socket_address_equal and you can use @ref of_socket_address_hash to * get a hash to use in e.g. @ref OFMapTable. * * @warning Even though the OFCopying protocol is implemented, it does *not* * return an independent copy of the socket, but instead retains it. * This is so that the socket can be used as a key for a dictionary, * so context can be associated with a socket. Using a socket in more |
︙ | ︙ | |||
157 158 159 160 161 162 163 | */ + (void)asyncResolveAddressForHost: (OFString *)host port: (uint16_t)port block: (of_udp_socket_async_resolve_block_t)block; # endif #endif | < < < < < < < < < < < < < | 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | */ + (void)asyncResolveAddressForHost: (OFString *)host port: (uint16_t)port block: (of_udp_socket_async_resolve_block_t)block; # endif #endif /*! * @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 * IPv6 to bind to all. * @param port The port to bind to. If the port is 0, an unused port will be * chosen, which can be obtained using the return value. |
︙ | ︙ |
Modified src/OFUDPSocket.m from [a0a08f26fb] to [df2fd645d0].
︙ | ︙ | |||
248 249 250 251 252 253 254 | block: block] autorelease] start]; objc_autoreleasePoolPop(pool); } # endif #endif | < < < < < < < < | 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | block: block] autorelease] start]; objc_autoreleasePoolPop(pool); } # endif #endif - (instancetype)init { self = [super init]; _socket = INVALID_SOCKET; return self; |
︙ | ︙ |
Modified src/resolver.h from [89d38aa172] to [8e4bcb1595].
︙ | ︙ | |||
49 50 51 52 53 54 55 | * * @return An array of results. The list is terminated by NULL and should be * freed after use. */ extern of_resolver_result_t *_Nullable *_Nonnull of_resolve_host(OFString *host, uint16_t port, int protocol); | < < < < < < < < < < < < < < < | 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | * * @return An array of results. The list is terminated by NULL and should be * freed after use. */ extern of_resolver_result_t *_Nullable *_Nonnull of_resolve_host(OFString *host, uint16_t port, int protocol); /*! * @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 *_Nullable *_Nonnull results); |
︙ | ︙ |
Modified src/resolver.m from [444cb9ff29] to [de3749afa6].
︙ | ︙ | |||
250 251 252 253 254 255 256 | } # endif #endif return ret; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | } # endif #endif return ret; } void of_resolver_free(of_resolver_result_t **results) { #ifdef HAVE_GETADDRINFO freeaddrinfo(results[0]->private_); #else free(results[0]->address); |
︙ | ︙ |
Modified src/socket.h from [b7c9f96c1f] to [2892632dc8].
︙ | ︙ | |||
147 148 149 150 151 152 153 154 155 156 157 158 | /*! * @brief Returns the hash for the specified of_socket_address_t. * * @param address The address to hash * @return The hash for the specified of_socket_address_t */ extern uint32_t of_socket_address_hash(of_socket_address_t *address); #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END | > > > > > > > > > > > | 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | /*! * @brief Returns the hash for the specified of_socket_address_t. * * @param address The address to hash * @return The hash for the specified of_socket_address_t */ extern uint32_t of_socket_address_hash(of_socket_address_t *address); /*! * @brief Converts the specified of_socket_address_t to an IP string and port. * * @param address 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 an IP string */ extern OFString *_Nonnull of_socket_address_ip_string( const of_socket_address_t *_Nonnull address, uint16_t *_Nullable port); #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END |
Modified src/socket.m from [2bd834cec4] to [9e65efeb36].
︙ | ︙ | |||
218 219 220 221 222 223 224 225 226 227 228 229 230 231 | if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; # endif return ret; } #endif bool of_socket_address_equal(of_socket_address_t *address1, of_socket_address_t *address2) { struct sockaddr_in *addrIn1, *addrIn2; #ifdef HAVE_IPV6 | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | if (!of_mutex_unlock(&mutex)) @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 *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: [OFLocale 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: [OFLocale 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 } bool of_socket_address_equal(of_socket_address_t *address1, of_socket_address_t *address2) { struct sockaddr_in *addrIn1, *addrIn2; #ifdef HAVE_IPV6 |
︙ | ︙ | |||
331 332 333 334 335 336 337 | default: @throw [OFInvalidArgumentException exception]; } return hash; } | | | < < | < < | | < < < | | | > | > | | < < | > > < < | < < | < < | | > | > > | < > > > | | < < | > < | 391 392 393 394 395 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 440 441 442 443 444 445 446 447 448 | default: @throw [OFInvalidArgumentException exception]; } return hash; } static OFString * IPv4String(const of_socket_address_t *address, uint16_t *port) { const struct sockaddr_in *sin = (const struct sockaddr_in *)&address->address; char buffer[INET_ADDRSTRLEN]; if (inet_ntop(AF_INET, &sin->sin_addr, buffer, sizeof(buffer)) == NULL) @throw [OFInvalidArgumentException exception]; if (port != NULL) *port = OF_BSWAP16_IF_LE(sin->sin_port); return [OFString stringWithCString: buffer encoding: [OFLocale encoding]]; } #ifdef HAVE_IPV6 static OFString * IPv6String(const of_socket_address_t *address, uint16_t *port) { const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&address->address; char buffer[INET6_ADDRSTRLEN]; if (inet_ntop(AF_INET, &sin6->sin_addr6, buffer, sizeof(buffer)) == NULL) @throw [OFInvalidArgumentException exception]; if (port != NULL) *port = OF_BSWAP16_IF_LE(sin6->sin_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) { case AF_INET: return IPv4String(address, port); #ifdef HAVE_IPV6 case AF_INET6: return IPv6String(address, port); #endif default: @throw [OFInvalidArgumentException exception]; } } |
Modified tests/OFUDPSocketTests.m from [da76f2699e] to [1bbed581ea].
︙ | ︙ | |||
53 54 55 56 57 58 59 | length: 6 receiver: &addr1])) TEST(@"-[receiveIntoBuffer:length:sender:]", [sock receiveIntoBuffer: buf length: 6 sender: &addr2] == 6 && | | | < < < < | 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | length: 6 receiver: &addr1])) TEST(@"-[receiveIntoBuffer:length:sender:]", [sock receiveIntoBuffer: buf length: 6 sender: &addr2] == 6 && !memcmp(buf, "Hello", 6) && (host = of_socket_address_ip_string(&addr2, &port2)) && [host isEqual: @"127.0.0.1"] && port2 == port1) [OFUDPSocket resolveAddressForHost: @"127.0.0.1" port: port1 + 1 address: &addr3]; /* |
︙ | ︙ |