- if (ip.find(':') != string::npos) {
- struct addrinfo* result;
- struct addrinfo hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET6;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_NUMERICHOST;
- if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) {
- struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr;
- addr_ = IPAddressV46(IPAddressV6(*ipAddr));
- family_ = AF_INET6;
- freeaddrinfo(result);
- } else {
- throwFormatException("getsockaddr failed for V6 address");
- }
- } else if (ip.find('.') != string::npos) {
- in_addr ipAddr;
- if (inet_pton(AF_INET, ip.c_str(), &ipAddr) != 1) {
- throwFormatException("inet_pton failed for V4 address");
- }
- addr_ = IPAddressV46(IPAddressV4(ipAddr));
- family_ = AF_INET;
+ if (str.find(':') != string::npos) {
+ return IPAddressV6::tryFromString(str);
+ } else if (str.find('.') != string::npos) {
+ return IPAddressV4::tryFromString(str);