8 #include <boost/assign/list_of.hpp>
15 static const std::map<string, Address::Family>
28 static const std::map<Address::Family, string>
41 static const std::map<Address::Family, string>
55 std::map<string, Address::Family>::const_iterator loc =
64 return toString.find(family)->second;
94 while (pos < str.size()) {
95 pos = str.find(
'.', pos);
96 if (pos == string::npos) {
109 size_t pos = str.find(
'/');
110 if (pos == string::npos) {
111 return make_error_code(boost::system::errc::invalid_argument);
113 *plen = atoi(str.c_str() + pos + 1);
115 return make_error_code(boost::system::errc::invalid_argument);
118 string addrstr = str.substr(0, pos);
121 addrstr.append(
".0");
125 boost::system::error_code err;
126 *addr = Ip4Address::from_string(addrstr, err);
134 boost::system::error_code err;
146 size_t pos = str.find(
'/');
147 if (pos == string::npos) {
148 return make_error_code(boost::system::errc::invalid_argument);
150 *plen = atoi(str.c_str() + pos + 1);
152 return make_error_code(boost::system::errc::invalid_argument);
155 string addrstr = str.substr(0, pos);
156 boost::system::error_code err;
157 *addr = Ip6Address::from_string(addrstr, err);
165 boost::system::error_code err;
181 return boost::asio::ip::address_v4(0);
184 Ip4Address subnet(prefix.to_ulong() & (0xFFFFFFFF << (32 - plen)));
202 return boost::asio::ip::address_v6();
209 uint16_t ip6[8], in_ip6[8];
210 unsigned char bytes[16];
212 inet_pton(AF_INET6, prefix.to_string().c_str(), ip6);
214 for (
int i = 0; i < 8; ++i) {
215 in_ip6[i] = ntohs(ip6[i]);
218 int index = (int) (plen / 16);
219 int remain_mask = plen % 16;
221 switch (remain_mask) {
226 in_ip6[index++] &= 0x8000;
229 in_ip6[index++] &= 0xc000;
232 in_ip6[index++] &= 0xe000;
235 in_ip6[index++] &= 0xf000;
238 in_ip6[index++] &= 0xf800;
241 in_ip6[index++] &= 0xfc00;
244 in_ip6[index++] &= 0xfe00;
247 in_ip6[index++] &= 0xff00;
250 in_ip6[index++] &= 0xff80;
253 in_ip6[index++] &= 0xffc0;
256 in_ip6[index++] &= 0xffe0;
259 in_ip6[index++] &= 0xfff0;
262 in_ip6[index++] &= 0xfff8;
265 in_ip6[index++] &= 0xfffc;
268 in_ip6[index++] &= 0xfffe;
272 for (
int i = index; i < 8; ++i) {
276 for (
int i = 0; i < 8; ++i) {
277 ip6[i] = htons(in_ip6[i]);
279 memcpy(bytes, ip6,
sizeof(ip6));
280 Ip6Address::bytes_type to_bytes;
281 for (
int i = 0; i < 16; ++i) {
282 to_bytes.at(i) = bytes[i];
284 return boost::asio::ip::address_v6(to_bytes);
293 if (v6_address.is_v4_mapped()) {
294 Ip6Address::bytes_type v6_bt = v6_address.to_bytes();
295 Ip4Address::bytes_type v4_bt =
296 { { v6_bt[12], v6_bt[13], v6_bt[14], v6_bt[15] } };
static Family VpnFamilyFromFamily(Family family)
static const uint8_t kMaxV6PrefixLen
boost::system::error_code Ip4PrefixParse(const string &str, Ip4Address *addr, int *plen)
static Family FamilyFromString(const std::string &family)
static const uint8_t kMaxV4PrefixLen
static std::string FamilyToTableString(Family family)
static Ip4Address GetIp4SubnetAddress(const Ip4Address &prefix, uint16_t plen)
static Ip6Address GetIp6SubnetAddress(const Ip6Address &prefix, uint16_t plen)
static int CountDots(const string &str)
boost::asio::ip::address_v6 Ip6Address
boost::asio::ip::address_v4 Ip4Address
boost::system::error_code Ip4SubnetParse(const string &str, Ip4Address *addr, int *plen)
static const std::map< Address::Family, string > toTableName
static std::string FamilyToString(Family fmly)
boost::system::error_code Inet6PrefixParse(const string &str, Ip6Address *addr, int *plen)
static const std::map< Address::Family, string > toString
static Ip4Address V4FromV4MappedV6(const Ip6Address &v6_address)
static const std::map< string, Address::Family > fromString
boost::system::error_code Inet6SubnetParse(const string &str, Ip6Address *addr, int *plen)