7 #include <boost/algorithm/string.hpp>
8 #include <boost/asio/io_service.hpp>
9 #include <boost/asio/ip/tcp.hpp>
10 #include <boost/asio/ip/host_name.hpp>
11 #include <boost/foreach.hpp>
22 return ((prefix.to_ulong() | ~(0xFFFFFFFF << (32 - plen))) ==
23 (ip.to_ulong() | ~(0xFFFFFFFF << (32 - plen))));
42 boost::asio::io_context io_service;
43 boost::system::error_code error;
44 boost::asio::ip::tcp::resolver resolver (io_service);
45 boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(),
46 "", boost::asio::ip::resolver_query_base::canonical_name);
47 boost::asio::ip::tcp::resolver::iterator iter =
48 resolver.resolve (query,error), end;
50 return boost::asio::ip::host_name();
53 if (iter->host_name() !=
"") {
54 return iter->host_name();
58 return boost::asio::ip::host_name();
66 if (boost::starts_with(ipv4,
"127."))
68 boost::asio::ip::tcp::endpoint endpoint;
69 boost::asio::io_context io_service;
70 boost::asio::ip::address_v4 ip =
71 boost::asio::ip::address_v4::from_string(ipv4);
73 boost::asio::ip::tcp::resolver resolver(io_service);
74 boost::asio::ip::tcp::resolver::iterator iter =
75 resolver.resolve (endpoint), end;
77 if(iter->host_name() !=
""){
78 return iter->host_name();
82 return boost::asio::ip::host_name();
91 std::string loc_ip = ipv6;
92 if (boost::starts_with(loc_ip,
"::1"))
94 unsigned int perc_pos = loc_ip.find(
"%");
95 if (perc_pos != std::string::npos) {
96 loc_ip = loc_ip.substr(0,perc_pos);
123 const std::string &ip_address_str,
124 boost::system::error_code *ec) {
126 boost::asio::ip::address::from_string(ip_address_str, *ec);
127 boost::system::error_code error_code = *ec;
128 if (error_code.value() != 0) {
129 boost::asio::io_context io_service;
130 std::string ip_string =
GetHostIp(&io_service, ip_address_str);
131 addr = boost::asio::ip::address::from_string(ip_string, *ec);
138 Ip4Address subnet(ip_prefix.to_ulong() | ~(0xFFFFFFFF << (32 - plen)));
144 std::string *error_msg) {
145 boost::system::error_code error;
147 if (error.value() != 0) {
148 std::ostringstream out;
149 out <<
"Invalid IP address: " << ip_address_str << std::endl;
150 *error_msg = out.str();
159 if (prefix_len == 0) {
162 mask = (~((1 << (32 - prefix_len)) - 1));
172 netmask = (netmask - 1) & netmask;
183 boost::system::error_code ec;
185 (
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", ec);
189 Ip6Address::bytes_type bytes;
191 int index = (int) (plen / 8);
192 int remain_mask = plen % 8;
194 for (
int i = 0; i < index; i++) {
198 switch (remain_mask) {
200 bytes.at(index++) = 0;
203 bytes.at(index++) = 0x80;
206 bytes.at(index++) = 0xc0;
209 bytes.at(index++) = 0xe0;
212 bytes.at(index++) = 0xf0;
215 bytes.at(index++) = 0xf8;
218 bytes.at(index++) = 0xfc;
221 bytes.at(index++) = 0xfe;
225 for (
int i = index; i < 16; ++i) {
234 std::string *error_msg) {
235 std::ostringstream out;
237 BOOST_FOREACH(std::string endpoint, list) {
238 std::vector<std::string> tokens;
239 boost::split(tokens, endpoint, boost::is_any_of(
":"));
240 if (tokens.size() != 2) {
241 out <<
"Invalid endpoint " << endpoint << std::endl;
242 *error_msg = out.str();
246 boost::system::error_code error;
250 out <<
"Invalid IP address: " << tokens[0] << std::endl;
251 *error_msg = out.str();
255 unsigned long port = strtoul(tokens[1].c_str(), NULL, 0);
256 if (errno || port > 0xffFF) {
257 out <<
"Invalid port : " << tokens[1];
259 out <<
" " << strerror(errno) << std::endl;
261 *error_msg = out.str();
271 std::string
GetHostIp(boost::asio::io_context *io_service,
272 const std::string &hostname) {
273 boost::asio::ip::tcp::resolver::iterator iter, end;
274 boost::system::error_code error;
275 boost::asio::ip::tcp::resolver resolver(*io_service);
276 boost::asio::ip::tcp::resolver::query query(hostname,
"");
280 iter = resolver.resolve(query, error);
285 for (; iter != end; ++iter) {
286 const boost::asio::ip::address &addr = iter->endpoint().address();
288 boost::asio::ip::address_v6 addr6 = addr.to_v6();
289 if (addr6.is_link_local()) {
293 result = addr.to_string();
305 std::vector<std::string> tokens;
306 boost::split(tokens, vn, boost::is_any_of(
":"), boost::token_compress_on);
307 if (tokens.size() < 3)
return "";
308 return tokens[0] +
":" + tokens[1] +
":" + tokens[2];
312 uint64_t *sip_u, uint64_t *sip_l,
313 uint64_t *dip_u, uint64_t *dip_l) {
315 *sip_l = htonl(sip.to_v4().to_ulong());
325 *dip_l = htonl(dip.to_v4().to_ulong());
336 boost::asio::ip::address_v6::bytes_type bytes;
337 const unsigned char *ptr = (
const unsigned char *)&lower;
338 for (
unsigned int i = 0; i <
sizeof(uint64_t); i++) {
341 ptr = (
const unsigned char *)&upper;
342 for (
unsigned int i = 0; i <
sizeof(uint64_t); i++) {
343 bytes[8 + i] = ptr[i];
348 void U64ToIp(uint64_t sip_u, uint64_t sip_l, uint64_t dip_u, uint64_t dip_l,
362 unsigned int i, j, k;
367 memcpy(b, addr.to_bytes().data(),
sizeof(b));
369 for (i = 0, j = 0, k = 0; i < 4; i++, j = j+4) {
372 ip = ((((uint32_t)b[j]) << 24) & 0xFF000000) |
373 ((((uint32_t)b[j+1]) << 16) & 0x00FF0000) |
374 ((((uint32_t)b[j+2]) << 8) & 0x0000FF00) |
375 (((uint32_t)b[j+3]) & 0x000000FF);
381 arr[k] = arr[k] | (ip & 0x00000000FFFFFFFFU);
383 arr[k] = (ip << 32) & 0xFFFFFFFF00000000U;
388 arr[0] = htobe64(arr[0]);
389 arr[1] = htobe64(arr[1]);
394 if (ipv6.size() != 16) {
395 return "0000:0000:0000:0000";
397 std::ostringstream strm;
399 for(std::vector<signed char>::const_iterator it = ipv6.begin();
400 it != ipv6.end(); ++it) {
401 strm << std::hex << (int)((uint8_t) *it);
Ip4Address GetIp4SubnetBroadcastAddress(const Ip4Address &ip_prefix, uint16_t plen)
bool IsIp4SubnetMember(const Ip4Address &ip, const Ip4Address &prefix_ip, uint16_t plen)
void IpToU64(const IpAddress &sip, const IpAddress &dip, uint64_t *sip_u, uint64_t *sip_l, uint64_t *dip_u, uint64_t *dip_l)
boost::asio::ip::address IpAddress
std::string GetVNFromRoutingInstance(const std::string &vn)
void U64ToIpv6(uint64_t upper, uint64_t lower, IpAddress *ip)
bool ValidateServerEndpoints(std::vector< std::string > list, std::string *error_msg)
static Ip4Address GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen)
static Ip6Address GetIp6SubnetAddress(const Ip6Address &prefix, uint16_t plen)
std::string ResolveCanonicalNameIPv6(const std::string &ipv6)
boost::asio::ip::address_v6 Ip6Address
std::string VectorIpv6ToString(const std::vector< signed char > &ipv6)
void Ip6AddressToU64Array(const Ip6Address &addr, uint64_t *arr, int size)
IpAddress PrefixToIp6Netmask(uint32_t plen)
std::string ResolveCanonicalName()
boost::asio::ip::address_v4 Ip4Address
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
bool ValidateIPAddressString(std::string ip_address_str, std::string *error_msg)
std::string GetHostIp(boost::asio::io_context *io_service, const std::string &hostname)
void U64ToIp(uint64_t sip_u, uint64_t sip_l, uint64_t dip_u, uint64_t dip_l, int family, IpAddress *sip, IpAddress *dip)
uint32_t NetmaskToPrefix(uint32_t netmask)
IpAddress PrefixToIpNetmask(uint32_t prefix_len)
bool IsIp6SubnetMember(const Ip6Address &ip, const Ip6Address &subnet, uint8_t plen)