-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbasic_otp_node_local.cpp
124 lines (104 loc) · 4.05 KB
/
basic_otp_node_local.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
***** BEGIN LICENSE BLOCK *****
Copyright 2010 Serge Aleynikov <saleyn at gmail dot com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***** END LICENSE BLOCK *****
*/
#include <boost/version.hpp>
#include <fstream>
#include <sstream>
#include <boost/asio.hpp>
#include <boost/filesystem/path.hpp>
#include <eixx/connect/basic_otp_node_local.hpp>
#include <ei.h>
namespace eixx {
namespace connect {
namespace {
atom get_default_cookie() {
const char* home = getenv("HOME") ? getenv("HOME") : "";
if (home) {
std::stringstream s;
s << home
#if BOOST_VERSION >= 104600
<< boost::filesystem::path("/").make_preferred().native()
#else
<< boost::filesystem2::slash<std::string>::value
#endif
<< ".erlang.cookie";
std::ifstream file(s.str().c_str());
if (! file.fail()) {
std::string cookie;
file >> cookie;
size_t n = cookie.find('\n');
if (n != std::string::npos) cookie.erase(n);
if (cookie.size() > EI_MAX_COOKIE_SIZE)
throw err_bad_argument("Cookie size too long", cookie.size());
return atom(cookie);
}
}
return atom();
}
} // namespace
atom basic_otp_node_local::s_default_cookie = get_default_cookie();
std::string basic_otp_node_local::s_localhost = boost::asio::ip::host_name();
basic_otp_node_local::basic_otp_node_local(
const std::string& a_nodename, const std::string& a_cookie)
{
set_nodename(a_nodename, a_cookie);
}
void basic_otp_node_local::set_nodename(
const std::string& a_nodename, const std::string& a_cookie)
{
if (m_cookie.size() > EI_MAX_COOKIE_SIZE)
throw err_bad_argument("Cookie size too long", m_cookie.size());
m_cookie = a_cookie.empty() ? s_default_cookie : atom(a_cookie);
std::string::size_type pos = a_nodename.find('@');
if (pos == std::string::npos) {
m_alivename = a_nodename;
m_hostname = s_localhost;
} else {
m_alivename = a_nodename.substr(0, pos);
m_hostname = a_nodename.substr(pos+1, a_nodename.size());
}
std::string short_hostname;
boost::system::error_code ec;
boost::asio::ip::address::from_string( m_hostname, ec );
pos = m_hostname.find('.');
std::stringstream str;
if(! ec) {
str << m_alivename << '@' << m_hostname;
m_longname = m_alivename+'@'+m_hostname;
} else if (pos != std::string::npos) {
str << m_alivename << '@' << m_hostname.substr(0, pos);
m_longname = m_alivename+'@'+m_hostname;
} else {
str << m_alivename << '@' << m_hostname;
try {
boost::asio::io_service svc;
boost::asio::ip::tcp::resolver resolver(svc);
boost::asio::ip::tcp::resolver::query query(m_hostname, "");
boost::asio::ip::tcp::resolver::iterator it;
for (it = resolver.resolve(query); it != boost::asio::ip::tcp::resolver::iterator(); ++it) {
std::string l_host_name = it->host_name();
if (l_host_name.find('.') != std::string::npos) {
m_hostname = l_host_name;
break;
}
}
m_longname = m_alivename+'@'+m_hostname;
} catch (std::exception& e) {
throw err_bad_argument(std::string("Can't resolve ") + m_hostname + ": " + e.what());
}
}
m_nodename = marshal::make_node_name(str.str());
}
} // namespace connect
} // namespace eixx