9 static const char rcsid[] __attribute__((used)) =
"$Id: IPPrefix.cpp 1723 2016-11-03 19:34:33Z sella $";
12 #include "../net/IPPrefix.h"
13 #include "../util/CommonMacro.h"
15 using namespace sella::variant;
16 namespace sn = sella::net;
18 IPPrefix::IPPrefix() throw () :
Nullable(std::make_shared<sn::
IPPrefix>()) { }
20 IPPrefix::IPPrefix(
const std::string &value)
throw (TypeCastException) :
Nullable() {
21 if (LIKELY(boost::regex_match(value, IPPrefix::Pattern()))) {
22 this->value = std::make_shared<sn::IPPrefix>(value);
24 THROW(TypeCastException,
"unable to cast '%s' to a ipprefix value", value.c_str());
28 IPPrefix::IPPrefix(
const sn::IPPrefix &value)
throw () :
Nullable(std::make_shared<sn::IPPrefix>(value)) { }
33 if (LIKELY(
this != &other)) {
34 if (LIKELY(!other.isNull())) {
35 if (UNLIKELY(isNull())) {
36 this->value = std::make_shared<sn::IPPrefix>(other.get());
38 *std::static_pointer_cast<
sn::IPPrefix>(this->value) = *std::static_pointer_cast<sn::IPPrefix>(other.value);
47 if (LIKELY(!other.isNull())) {
48 this->value = std::move(other.value);
52 bool IPPrefix::operator==(
const IPPrefix &other)
const throw () {
53 return (*std::static_pointer_cast<sn::IPPrefix>(this->value) == *std::static_pointer_cast<sn::IPPrefix>(other.value));
56 bool IPPrefix::operator!=(
const IPPrefix &other)
const throw () {
57 return !(*
this == other);
60 bool IPPrefix::operator>(
const IPPrefix &other)
const throw () {
61 return (*std::static_pointer_cast<sn::IPPrefix>(this->value) > *std::static_pointer_cast<sn::IPPrefix>(other.value));
64 bool IPPrefix::operator<(
const IPPrefix &other)
const throw () {
65 return (*std::static_pointer_cast<sn::IPPrefix>(this->value) < *std::static_pointer_cast<sn::IPPrefix>(other.value));
69 if (LIKELY(
this != &other)) {
70 if (LIKELY(!other.isNull())) {
71 if (UNLIKELY(isNull())) {
72 this->value = std::make_shared<sn::IPPrefix>(other.get());
74 *std::static_pointer_cast<
sn::IPPrefix>(this->value) = *std::static_pointer_cast<sn::IPPrefix>(other.value);
85 if (LIKELY(
this != &other)) {
86 if (LIKELY(!other.isNull())) {
87 this->value = std::move(other.value);
95 if (UNLIKELY(isNull())) {
96 this->value = std::make_shared<sn::IPPrefix>(other);
98 *std::static_pointer_cast<
sn::IPPrefix>(this->value) = other;
104 IPPrefix::~IPPrefix() throw () { }
106 const sn::IPPrefix& IPPrefix::get()
const throw (UnsupportedOperatorException) {
107 if (UNLIKELY(isNull())) {
108 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
111 return *std::static_pointer_cast<
sn::IPPrefix>(this->value);
114 sn::IPPrefix& IPPrefix::get() throw (UnsupportedOperatorException) {
115 if (UNLIKELY(isNull())) {
116 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
119 return *std::static_pointer_cast<
sn::IPPrefix>(this->value);
122 void IPPrefix::clear() throw() {
123 if (UNLIKELY(isNull())) {
124 this->value = std::make_shared<sn::IPPrefix>();
133 if (UNLIKELY(isNull())) {
134 c = mpack.append(offset);
136 c = mpack.append(offset, std::static_pointer_cast<sella::net::IPPrefix>(this->value)->str());
141 if (UNLIKELY(c == 0)) {
142 THROW(TypeCastException,
"Failed to append '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
146 void IPPrefix::msgpackBufferSize(
size_t &size)
const throw () {
147 if (UNLIKELY(isNull())) {
153 size += 5 + (std::static_pointer_cast<
sella::net::IPPrefix>(this->value)->getFamily() == sn::IPAddress::IPv4Family) ? 18 : 43;
156 std::string IPPrefix::str(
int type)
const throw () {
157 if (UNLIKELY(isNull())) {
161 return this->
get().str();
164 bool IPPrefix::isType(
const char *value,
size_t size)
throw () {
168 for (
size_t i = size - 4; i < size; i++) {
169 if (value[i] ==
'/' && ::isdigit(value[i + 1]) != 0) {
170 if (::atoi(value + i + 1) > 128) {
179 unsigned char buf[
sizeof(
struct in6_addr)];
183 for (
size_t i = 0, len = (size > 5) ? 5 : size; i < len; i++) {
184 if (value[i] ==
'.') {
185 strncpy(tmp, value, cidr);
188 return (inet_pton(AF_INET, tmp, buf) == 1);
189 }
else if (value[i] ==
':') {
190 strncpy(tmp, value, cidr);
193 return (inet_pton(AF_INET6, tmp, buf) == 1);