1 | // **************************************************************************
|
---|
2 | /** @namespace Dim
|
---|
3 |
|
---|
4 | @brief Namespace to host some global Dim helper functions
|
---|
5 |
|
---|
6 | */
|
---|
7 | // **************************************************************************
|
---|
8 | #include "Dim.h"
|
---|
9 |
|
---|
10 | #include <netdb.h>
|
---|
11 | #include <sys/types.h>
|
---|
12 | #include <sys/socket.h>
|
---|
13 | #include <arpa/inet.h>
|
---|
14 |
|
---|
15 | #include <iostream>
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | // --------------------------------------------------------------------------
|
---|
20 | //
|
---|
21 | //! Tries to determine the local IP address with which we will connect
|
---|
22 | //! to the DIM dns.
|
---|
23 | //!
|
---|
24 | //! @param dns
|
---|
25 | //! Address of the Dim-dns
|
---|
26 | //!
|
---|
27 | //! @returns
|
---|
28 | //! The IP Address through which the connection to the DNS will
|
---|
29 | //! take place.
|
---|
30 | //!
|
---|
31 | string Dim::GetLocalIp(const string &dns)
|
---|
32 | {
|
---|
33 | struct addrinfo hints, *servinfo, *p;
|
---|
34 |
|
---|
35 | memset(&hints, 0, sizeof hints);
|
---|
36 | hints.ai_family = AF_INET; //AF_UNSPEC; // use AF_INET6 to force IPv6
|
---|
37 | hints.ai_socktype = SOCK_STREAM;
|
---|
38 |
|
---|
39 | int rv;
|
---|
40 | if ((rv = getaddrinfo(dns.c_str(), NULL, &hints, &servinfo)) != 0)
|
---|
41 | {
|
---|
42 | cout << "WARNING - getaddrinfo: " << gai_strerror(rv) << endl;
|
---|
43 | return dns;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // loop through all the results and connect to the first we can
|
---|
47 | for (p=servinfo; p; p=p->ai_next)
|
---|
48 | {
|
---|
49 | const int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
50 | if (sock==-1)
|
---|
51 | continue;
|
---|
52 |
|
---|
53 | if (connect(sock, p->ai_addr, p->ai_addrlen)==-1)
|
---|
54 | {
|
---|
55 | cout << "WARNING - connect: " << strerror(errno) << endl;
|
---|
56 | close(sock);
|
---|
57 | continue;
|
---|
58 | }
|
---|
59 |
|
---|
60 | sockaddr_in name;
|
---|
61 | socklen_t namelen = sizeof(name);
|
---|
62 | if (getsockname(sock, (sockaddr*)&name, &namelen)==-1)
|
---|
63 | {
|
---|
64 | cout << "WARNING - getsockname: " << strerror(errno) << endl;
|
---|
65 | close(sock);
|
---|
66 | continue;
|
---|
67 | }
|
---|
68 |
|
---|
69 | char buffer[16];
|
---|
70 | if (!inet_ntop(AF_INET, &name.sin_addr, buffer, 16))
|
---|
71 | {
|
---|
72 | cout << "WARNING - inet_ntop: " << strerror(errno) << endl;
|
---|
73 | close(sock);
|
---|
74 | continue;
|
---|
75 | }
|
---|
76 |
|
---|
77 | close(sock);
|
---|
78 |
|
---|
79 | freeaddrinfo(servinfo); // all done with this structure
|
---|
80 |
|
---|
81 | cout << "DIM_HOST_NODE=" << buffer << endl;
|
---|
82 | return buffer;
|
---|
83 | }
|
---|
84 |
|
---|
85 | freeaddrinfo(servinfo); // all done with this structure
|
---|
86 |
|
---|
87 | return dns;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // --------------------------------------------------------------------------
|
---|
91 | //
|
---|
92 | //! Set the environment variable DIM_DNS_NODE to the given string and
|
---|
93 | //! DIM_HOST_NODE to the IP-address through which this machine connects
|
---|
94 | //! to the dns.
|
---|
95 | //!
|
---|
96 | //! @param dns
|
---|
97 | //! Address of the Dim-dns
|
---|
98 | //!
|
---|
99 | void Dim::Setup(const std::string &dns)
|
---|
100 | {
|
---|
101 | setenv("DIM_DNS_NODE", dns.c_str(), 1);
|
---|
102 | setenv("DIM_HOST_NODE", GetLocalIp(dns).c_str(), 1);
|
---|
103 | }
|
---|