* Parse a sockaddr from an ip and port provided as strings. */
| 14 | * Parse a sockaddr from an ip and port provided as strings. |
| 15 | */ |
| 16 | address_t parse_address(const std::string& ip, const std::string& port) { |
| 17 | struct addrinfo hints, *res; |
| 18 | std::memset(&hints, 0, sizeof(hints)); |
| 19 | hints.ai_family = AF_UNSPEC; |
| 20 | hints.ai_socktype = SOCK_STREAM; |
| 21 | |
| 22 | int status = getaddrinfo(ip.c_str(), port.c_str(), &hints, &res); |
| 23 | if (status != 0) { |
| 24 | std::ostringstream msg; |
| 25 | msg << "Can't parse address " << ip << ":" << port; |
| 26 | throw std::runtime_error(msg.str()); |
| 27 | } |
| 28 | |
| 29 | address_t result; |
| 30 | memcpy(&result.addr, res->ai_addr, res->ai_addrlen); |
| 31 | result.len = res->ai_addrlen; |
| 32 | freeaddrinfo(res); |
| 33 | |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Parse a sockaddr provided as an <ip>:<port> string. |