FACT++  1.0
DimSetup.cc
Go to the documentation of this file.
1 // **************************************************************************
7 // **************************************************************************
8 #include "Dim.h"
9 
10 /*
11 #include <netdb.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 */
16 #include <boost/asio.hpp>
17 
18 #include <iostream>
19 
20 using namespace std;
21 
22 // --------------------------------------------------------------------------
23 //
51 string Dim::GetLocalIp(const string &dns)
52 {
53  using namespace boost::asio;
54  using namespace boost::asio::ip;
55 
56  cout << "Trying to resolve local IP address..." << endl;
57 
58  boost::system::error_code ec;
59 
60  boost::asio::io_service io_service;
61 
62  udp::socket socket(io_service);
63 
64  udp::resolver resolver(io_service);
65  udp::resolver::query query(dns, "0");
66  udp::resolver::iterator iterator = resolver.resolve(query, ec);
67  if (ec)
68  {
69  //cout << "WARNING - Failure in name-resolution of '" << dns << ":0': ";
70  cout << "WARNING - Could not resolve local ip address: ";
71  cout << ec.message() << " (" << ec << ")" << endl;
72  return dns;
73  }
74 
75  for (; iterator != udp::resolver::iterator(); ++iterator)
76  {
77  udp::endpoint endpoint = *iterator;
78  socket.connect(endpoint, ec);
79  if (ec)
80  {
81  cout << "WARNING - Could not resolve local ip address: ";
82  cout << ec.message() << " (" << ec << ")" << endl;
83  continue;
84  }
85 
86  const string addr = socket.local_endpoint().address().to_v4().to_string();
87  return addr;
88  }
89 
90  return "localhost";
91 
92 /*
93  struct addrinfo hints, *servinfo, *p;
94 
95  memset(&hints, 0, sizeof hints);
96  hints.ai_family = AF_INET; //AF_UNSPEC; // use AF_INET6 to force IPv6
97  hints.ai_socktype = SOCK_STREAM;
98 
99  int rv;
100  if ((rv = getaddrinfo(dns.c_str(), NULL, &hints, &servinfo)) != 0)
101  {
102  cout << "WARNING - getaddrinfo: " << gai_strerror(rv) << endl;
103  return dns;
104  }
105 
106  // loop through all the results and connect to the first we can
107  for (p=servinfo; p; p=p->ai_next)
108  {
109  const int sock = socket(AF_INET, SOCK_DGRAM, 0);
110  if (sock==-1)
111  continue;
112 
113  if (connect(sock, p->ai_addr, p->ai_addrlen)==-1)
114  {
115  cout << "WARNING - connect: " << strerror(errno) << endl;
116  close(sock);
117  continue;
118  }
119 
120  sockaddr_in name;
121  socklen_t namelen = sizeof(name);
122  if (getsockname(sock, (sockaddr*)&name, &namelen)==-1)
123  {
124  cout << "WARNING - getsockname: " << strerror(errno) << endl;
125  close(sock);
126  continue;
127  }
128 
129  char buffer[16];
130  if (!inet_ntop(AF_INET, &name.sin_addr, buffer, 16))
131  {
132  cout << "WARNING - inet_ntop: " << strerror(errno) << endl;
133  close(sock);
134  continue;
135  }
136 
137  close(sock);
138 
139  freeaddrinfo(servinfo); // all done with this structure
140 
141  cout << "DIM_HOST_NODE=" << buffer << endl;
142  return buffer;
143  }
144 
145  freeaddrinfo(servinfo); // all done with this structure
146 
147  return dns;
148 */
149 }
150 
151 // --------------------------------------------------------------------------
152 //
160 void Dim::Setup(const std::string &dns, const std::string &host)
161 {
162  if (dns.empty())
163  {
164  setenv("DIM_DNS_NODE", "...", 1);
165  //unsetenv("DIM_DNS_NODE");
166  //unsetenv("DIM_HOST_NODE");
167  return;
168  }
169 
170  const string loc = host.empty() ? Dim::GetLocalIp(dns.c_str()) : host;
171 
172  setenv("DIM_DNS_NODE", dns.c_str(), 1);
173  setenv("DIM_HOST_NODE", loc.c_str(), 1);
174 
175  cout << "Setting DIM_DNS_NODE =" << dns << endl;
176  cout << "Setting DIM_HOST_NODE=" << loc << endl;
177 }
178 
179 extern "C"
180 {
181  const char *GetLocalIp()
182  {
183  static string rc;
184  rc = Dim::GetLocalIp();
185  cout << "Setting DIM_HOST_NODE=" << rc << endl;
186  return rc.c_str();
187  }
188 }
std::string GetLocalIp(const std::string &dns="10.0.100.1")
Definition: DimSetup.cc:51
STL namespace.
void Setup(const std::string &dns="", const std::string &host="")
Definition: DimSetup.cc:160
const char * GetLocalIp()
Definition: DimSetup.cc:181