1 | // **************************************************************************
|
---|
2 | /** @namespace Dim
|
---|
3 |
|
---|
4 | @brief Namespace to host some global Dim helper functions
|
---|
5 |
|
---|
6 | */
|
---|
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 | //
|
---|
24 | //! Tries to determine the local IP address with which we will connect
|
---|
25 | //! to the DIM dns.
|
---|
26 | //!
|
---|
27 | //! @param dns
|
---|
28 | //! Address of the Dim-dns
|
---|
29 | //!
|
---|
30 | //! @returns
|
---|
31 | //! The IP Address through which the connection to the DNS will
|
---|
32 | //! take place.
|
---|
33 | //!
|
---|
34 | string Dim::GetLocalIp(const string &dns)
|
---|
35 | {
|
---|
36 | using namespace boost::asio;
|
---|
37 | using namespace boost::asio::ip;
|
---|
38 |
|
---|
39 | cout << "Trying to resolve local IP address..." << endl;
|
---|
40 |
|
---|
41 | boost::system::error_code ec;
|
---|
42 |
|
---|
43 | boost::asio::io_service io_service;
|
---|
44 |
|
---|
45 | udp::socket socket(io_service);
|
---|
46 |
|
---|
47 | udp::resolver resolver(io_service);
|
---|
48 | udp::resolver::query query(dns, "0");
|
---|
49 | udp::resolver::iterator iterator = resolver.resolve(query, ec);
|
---|
50 | if (ec)
|
---|
51 | {
|
---|
52 | cout << "WARNING - Failure in name-resolution of '" << dns << ":0': ";
|
---|
53 | cout << ec.message() << " (" << ec << ")" << endl;
|
---|
54 | return dns;
|
---|
55 | }
|
---|
56 |
|
---|
57 | for (; iterator != udp::resolver::iterator(); ++iterator)
|
---|
58 | {
|
---|
59 | udp::endpoint endpoint = *iterator;
|
---|
60 | socket.connect(endpoint, ec);
|
---|
61 | if (ec)
|
---|
62 | {
|
---|
63 | cout << "WARNING - connect to '" << dns << ":0' failed: ";
|
---|
64 | cout << ec.message() << " (" << ec << ")" << endl;
|
---|
65 | continue;
|
---|
66 | }
|
---|
67 |
|
---|
68 | const string addr = socket.local_endpoint().address().to_v4().to_string();
|
---|
69 | cout << "Setting DIM_HOST_NODE=" << addr << endl;
|
---|
70 | return addr;
|
---|
71 | }
|
---|
72 |
|
---|
73 | return dns;
|
---|
74 |
|
---|
75 | /*
|
---|
76 | struct addrinfo hints, *servinfo, *p;
|
---|
77 |
|
---|
78 | memset(&hints, 0, sizeof hints);
|
---|
79 | hints.ai_family = AF_INET; //AF_UNSPEC; // use AF_INET6 to force IPv6
|
---|
80 | hints.ai_socktype = SOCK_STREAM;
|
---|
81 |
|
---|
82 | int rv;
|
---|
83 | if ((rv = getaddrinfo(dns.c_str(), NULL, &hints, &servinfo)) != 0)
|
---|
84 | {
|
---|
85 | cout << "WARNING - getaddrinfo: " << gai_strerror(rv) << endl;
|
---|
86 | return dns;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // loop through all the results and connect to the first we can
|
---|
90 | for (p=servinfo; p; p=p->ai_next)
|
---|
91 | {
|
---|
92 | const int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
93 | if (sock==-1)
|
---|
94 | continue;
|
---|
95 |
|
---|
96 | if (connect(sock, p->ai_addr, p->ai_addrlen)==-1)
|
---|
97 | {
|
---|
98 | cout << "WARNING - connect: " << strerror(errno) << endl;
|
---|
99 | close(sock);
|
---|
100 | continue;
|
---|
101 | }
|
---|
102 |
|
---|
103 | sockaddr_in name;
|
---|
104 | socklen_t namelen = sizeof(name);
|
---|
105 | if (getsockname(sock, (sockaddr*)&name, &namelen)==-1)
|
---|
106 | {
|
---|
107 | cout << "WARNING - getsockname: " << strerror(errno) << endl;
|
---|
108 | close(sock);
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 |
|
---|
112 | char buffer[16];
|
---|
113 | if (!inet_ntop(AF_INET, &name.sin_addr, buffer, 16))
|
---|
114 | {
|
---|
115 | cout << "WARNING - inet_ntop: " << strerror(errno) << endl;
|
---|
116 | close(sock);
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 |
|
---|
120 | close(sock);
|
---|
121 |
|
---|
122 | freeaddrinfo(servinfo); // all done with this structure
|
---|
123 |
|
---|
124 | cout << "DIM_HOST_NODE=" << buffer << endl;
|
---|
125 | return buffer;
|
---|
126 | }
|
---|
127 |
|
---|
128 | freeaddrinfo(servinfo); // all done with this structure
|
---|
129 |
|
---|
130 | return dns;
|
---|
131 | */
|
---|
132 | }
|
---|
133 |
|
---|
134 | // --------------------------------------------------------------------------
|
---|
135 | //
|
---|
136 | //! Set the environment variable DIM_DNS_NODE to the given string and
|
---|
137 | //! DIM_HOST_NODE to the IP-address through which this machine connects
|
---|
138 | //! to the dns.
|
---|
139 | //!
|
---|
140 | //! @param dns
|
---|
141 | //! Address of the Dim-dns
|
---|
142 | //!
|
---|
143 | void Dim::Setup(const std::string &dns)
|
---|
144 | {
|
---|
145 | setenv("DIM_DNS_NODE", dns.c_str(), 1);
|
---|
146 | setenv("DIM_HOST_NODE", GetLocalIp(dns).c_str(), 1);
|
---|
147 | }
|
---|