Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
network-util.cc
Go to the documentation of this file.
1 // Copyright 2013 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/network-util.h"
16 
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netdb.h>
20 #include <arpa/inet.h>
21 #include <limits.h>
22 #include <sstream>
23 #include <vector>
24 #include <boost/algorithm/string.hpp>
25 #include <boost/foreach.hpp>
26 
27 #include "util/debug-util.h"
28 #include "util/error-util.h"
29 #include <util/string-parser.h>
30 
31 #include "common/names.h"
32 
33 using boost::algorithm::is_any_of;
34 using boost::algorithm::split;
35 
36 namespace impala {
37 
38 static const string LOCALHOST("127.0.0.1");
39 
40 Status GetHostname(string* hostname) {
41  char name[HOST_NAME_MAX];
42  int ret = gethostname(name, HOST_NAME_MAX);
43  if (ret != 0) {
44  string error_msg = GetStrErrMsg();
45  stringstream ss;
46  ss << "Could not get hostname: " << error_msg;
47  return Status(ss.str());
48  }
49  *hostname = string(name);
50  return Status::OK;
51 }
52 
53 Status HostnameToIpAddrs(const string& name, vector<string>* addresses) {
54  addrinfo hints;
55  memset(&hints, 0, sizeof(struct addrinfo));
56  hints.ai_family = AF_INET; // IPv4 addresses only
57  hints.ai_socktype = SOCK_STREAM;
58 
59  struct addrinfo* addr_info;
60  if (getaddrinfo(name.c_str(), NULL, &hints, &addr_info) != 0) {
61  stringstream ss;
62  ss << "Could not find IPv4 address for: " << name;
63  return Status(ss.str());
64  }
65 
66  addrinfo* it = addr_info;
67  while (it != NULL) {
68  char addr_buf[64];
69  const char* result =
70  inet_ntop(AF_INET, &((sockaddr_in*)it->ai_addr)->sin_addr, addr_buf, 64);
71  if (result == NULL) {
72  stringstream ss;
73  ss << "Could not convert IPv4 address for: " << name;
74  freeaddrinfo(addr_info);
75  return Status(ss.str());
76  }
77  addresses->push_back(string(addr_buf));
78  it = it->ai_next;
79  }
80 
81  freeaddrinfo(addr_info);
82  return Status::OK;
83 }
84 
85 bool FindFirstNonLocalhost(const vector<string>& addresses, string* addr) {
86  BOOST_FOREACH(const string& candidate, addresses) {
87  if (candidate != LOCALHOST) {
88  *addr = candidate;
89  return true;
90  }
91  }
92 
93  return false;
94 }
95 
96 TNetworkAddress MakeNetworkAddress(const string& hostname, int port) {
97  TNetworkAddress ret;
98  ret.__set_hostname(hostname);
99  ret.__set_port(port);
100  return ret;
101 }
102 
103 TNetworkAddress MakeNetworkAddress(const string& address) {
104  vector<string> tokens;
105  split(tokens, address, is_any_of(":"));
106  TNetworkAddress ret;
107  if (tokens.size() == 1) {
108  ret.__set_hostname(tokens[0]);
109  ret.port = 0;
110  return ret;
111  }
112  if (tokens.size() != 2) return ret;
113  ret.__set_hostname(tokens[0]);
114  StringParser::ParseResult parse_result;
115  int32_t port = StringParser::StringToInt<int32_t>(
116  tokens[1].data(), tokens[1].length(), &parse_result);
117  if (parse_result != StringParser::PARSE_SUCCESS) return ret;
118  ret.__set_port(port);
119  return ret;
120 }
121 
122 bool IsWildcardAddress(const string& ipaddress) {
123  return ipaddress == "0.0.0.0";
124 }
125 
126 string TNetworkAddressToString(const TNetworkAddress& address) {
127  stringstream ss;
128  ss << address;
129  return ss.str();
130 }
131 
132 ostream& operator<<(ostream& out, const TNetworkAddress& hostport) {
133  out << hostport.hostname << ":" << dec << hostport.port;
134  return out;
135 }
136 
137 }
string TNetworkAddressToString(const TNetworkAddress &address)
Utility method to print address as address:port.
bool FindFirstNonLocalhost(const vector< string > &addresses, string *addr)
Definition: network-util.cc:85
TNetworkAddress MakeNetworkAddress(const string &hostname, int port)
Definition: network-util.cc:96
static const string LOCALHOST("127.0.0.1")
string GetStrErrMsg()
Definition: error-util.cc:30
bool IsWildcardAddress(const string &ipaddress)
Status HostnameToIpAddrs(const string &name, vector< string > *addresses)
Definition: network-util.cc:53
Status GetHostname(string *hostname)
Definition: network-util.cc:40
static const Status OK
Definition: status.h:87
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
string name
Definition: cpu-info.cc:50