in tensorpipe/transport/uv/sockaddr.cc [25:92]
Sockaddr Sockaddr::createInetSockAddr(const std::string& str) {
int port = 0;
std::string addrStr;
std::string portStr;
// If the input string is an IPv6 address with port, the address
// itself must be wrapped with brackets.
if (addrStr.empty()) {
auto start = str.find("[");
auto stop = str.find("]");
if (start < stop && start != std::string::npos &&
stop != std::string::npos) {
addrStr = str.substr(start + 1, stop - (start + 1));
if (stop + 1 < str.size() && str[stop + 1] == ':') {
portStr = str.substr(stop + 2);
}
}
}
// If the input string is an IPv4 address with port, we expect
// at least a single period and a single colon in the string.
if (addrStr.empty()) {
auto period = str.find(".");
auto colon = str.find(":");
if (period != std::string::npos && colon != std::string::npos) {
addrStr = str.substr(0, colon);
portStr = str.substr(colon + 1);
}
}
// Fallback to using entire input string as address without port.
if (addrStr.empty()) {
addrStr = str;
}
// Parse port number if specified.
if (!portStr.empty()) {
port = std::stoi(portStr);
if (port < 0 || port > std::numeric_limits<uint16_t>::max()) {
TP_THROW_EINVAL() << str;
}
}
// Try to convert an IPv4 address.
{
struct sockaddr_in addr;
auto rv = uv_ip4_addr(addrStr.c_str(), port, &addr);
if (rv == 0) {
return Sockaddr(reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
}
}
// Try to convert an IPv6 address.
{
struct sockaddr_in6 addr;
auto rv = uv_ip6_addr(addrStr.c_str(), port, &addr);
if (rv == 0) {
return Sockaddr(reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
}
}
// Invalid address.
TP_THROW_EINVAL() << str;
// Return bogus to silence "return from non-void function" warning.
// Note: we don't reach this point per the throw above.
return Sockaddr(nullptr, 0);
}