17 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <arpa/inet.h>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/foreach.hpp>
33 using boost::algorithm::is_any_of;
34 using boost::algorithm::split;
38 static const string LOCALHOST(
"127.0.0.1");
41 char name[HOST_NAME_MAX];
42 int ret = gethostname(name, HOST_NAME_MAX);
46 ss <<
"Could not get hostname: " << error_msg;
49 *hostname = string(name);
55 memset(&hints, 0,
sizeof(
struct addrinfo));
56 hints.ai_family = AF_INET;
57 hints.ai_socktype = SOCK_STREAM;
59 struct addrinfo* addr_info;
60 if (getaddrinfo(name.c_str(), NULL, &hints, &addr_info) != 0) {
62 ss <<
"Could not find IPv4 address for: " <<
name;
66 addrinfo* it = addr_info;
70 inet_ntop(AF_INET, &((sockaddr_in*)it->ai_addr)->sin_addr, addr_buf, 64);
73 ss <<
"Could not convert IPv4 address for: " <<
name;
74 freeaddrinfo(addr_info);
77 addresses->push_back(
string(addr_buf));
81 freeaddrinfo(addr_info);
86 BOOST_FOREACH(
const string& candidate, addresses) {
98 ret.__set_hostname(hostname);
104 vector<string> tokens;
105 split(tokens, address, is_any_of(
":"));
107 if (tokens.size() == 1) {
108 ret.__set_hostname(tokens[0]);
112 if (tokens.size() != 2)
return ret;
113 ret.__set_hostname(tokens[0]);
115 int32_t port = StringParser::StringToInt<int32_t>(
116 tokens[1].data(), tokens[1].length(), &parse_result);
118 ret.__set_port(port);
123 return ipaddress ==
"0.0.0.0";
132 ostream&
operator<<(ostream& out,
const TNetworkAddress& hostport) {
133 out << hostport.hostname <<
":" << dec << hostport.port;
string TNetworkAddressToString(const TNetworkAddress &address)
Utility method to print address as address:port.
bool FindFirstNonLocalhost(const vector< string > &addresses, string *addr)
TNetworkAddress MakeNetworkAddress(const string &hostname, int port)
static const string LOCALHOST("127.0.0.1")
bool IsWildcardAddress(const string &ipaddress)
Status HostnameToIpAddrs(const string &name, vector< string > *addresses)
Status GetHostname(string *hostname)
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)