{
if(sin4.sin_family == 0) {
return false;
- }
+ }
if(boost::tie(sin4.sin_family, sin4.sin_port) < boost::tie(rhs.sin4.sin_family, rhs.sin4.sin_port))
return true;
if(boost::tie(sin4.sin_family, sin4.sin_port) > boost::tie(rhs.sin4.sin_family, rhs.sin4.sin_port))
return false;
-
+
if(sin4.sin_family == AF_INET)
return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr;
else
struct addressOnlyHash
{
- uint32_t operator()(const ComboAddress& ca) const
- {
+ uint32_t operator()(const ComboAddress& ca) const
+ {
const unsigned char* start;
int len;
if(ca.sin4.sin_family == AF_INET) {
else
return sizeof(sin6);
}
-
- ComboAddress()
+
+ ComboAddress()
{
sin4.sin_family=AF_INET;
sin4.sin_addr.s_addr=0;
if(makeIPv4sockaddr(str, &sin4)) {
sin6.sin6_family = AF_INET6;
if(makeIPv6sockaddr(str, &sin6) < 0)
- throw PDNSException("Unable to convert presentation address '"+ str +"'");
-
+ throw PDNSException("Unable to convert presentation address '"+ str +"'");
+
}
if(!sin4.sin_port) // 'str' overrides port!
sin4.sin_port=htons(port);
{
if(sin4.sin_family!=AF_INET6)
return false;
-
+
int n=0;
const unsigned char*ptr = (unsigned char*) &sin6.sin6_addr.s6_addr;
for(n=0; n < 10; ++n)
if(ptr[n])
return false;
-
+
for(; n < 12; ++n)
if(ptr[n]!=0xff)
return false;
-
+
return true;
}
-
+
ComboAddress mapToIPv4() const
{
if(!isMappedIPv4())
ComboAddress ret;
ret.sin4.sin_family=AF_INET;
ret.sin4.sin_port=sin4.sin_port;
-
+
const unsigned char*ptr = (unsigned char*) &sin6.sin6_addr.s6_addr;
ptr+=(sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr));
memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr));
};
/** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */
-class NetmaskException: public PDNSException
+class NetmaskException: public PDNSException
{
public:
NetmaskException(const string &a) : PDNSException(a) {}
if(inet_pton(AF_INET, str.c_str(), &address.sin4.sin_addr) <= 0) {
address.sin4.sin_family=AF_INET6;
if(makeIPv6sockaddr(str, &address.sin6) < 0)
- throw NetmaskException("Unable to convert '"+str+"' to a netmask");
+ throw NetmaskException("Unable to convert '"+str+"' to a netmask");
}
return address;
}
public:
Netmask()
{
- d_network.sin4.sin_family=0; // disable this doing anything useful
- d_network.sin4.sin_port = 0; // this guarantees d_network compares identical
- d_mask=0;
- d_bits=0;
+ d_network.sin4.sin_family=0; // disable this doing anything useful
+ d_network.sin4.sin_port = 0; // this guarantees d_network compares identical
+ d_mask=0;
+ d_bits=0;
}
-
+
Netmask(const ComboAddress& network, uint8_t bits=0xff): d_network(network)
{
d_network.sin4.sin_port=0;
if(bits > 128)
bits = (network.sin4.sin_family == AF_INET) ? 32 : 128;
-
+
d_bits = bits;
if(d_bits<32)
d_mask=~(0xFFFFFFFF>>d_bits);
else
d_mask=0xFFFFFFFF; // not actually used for IPv6
}
-
- //! Constructor supplies the mask, which cannot be changed
- Netmask(const string &mask)
+
+ //! Constructor supplies the mask, which cannot be changed
+ Netmask(const string &mask)
{
pair<string,string> split=splitField(mask,'/');
d_network=makeComboAddress(split.first);
-
+
if(!split.second.empty()) {
d_bits = (uint8_t)pdns_stou(split.second);
if(d_bits<32)
uint8_t bytes=d_bits/8, n;
const uint8_t *us=(const uint8_t*) &d_network.sin6.sin6_addr.s6_addr;
const uint8_t *them=(const uint8_t*) &ip->sin6.sin6_addr.s6_addr;
-
+
for(n=0; n < bytes; ++n) {
if(us[n]!=them[n]) {
return false;
return d_network.sin4.sin_family == AF_INET;
}
- bool operator<(const Netmask& rhs) const
+ bool operator<(const Netmask& rhs) const
{
if (empty() && !rhs.empty())
return false;
return rhs.operator<(*this);
}
- bool operator==(const Netmask& rhs) const
+ bool operator==(const Netmask& rhs) const
{
return tie(d_network, d_bits) == tie(rhs.d_network, rhs.d_bits);
}
- bool empty() const
+ bool empty() const
{
return d_network.sin4.sin_family==0;
}
*/
class TreeNode : boost::noncopyable {
public:
- explicit TreeNode(int bits) noexcept : parent(NULL),d_bits(bits) {
- }
-
- //<! Makes a left node with one more bit than parent
- TreeNode* make_left() {
- if (!left) {
- left = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
- left->parent = this;
- }
- return left.get();
- }
-
- //<! Makes a right node with one more bit than parent
- TreeNode* make_right() {
- if (!right) {
- right = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
- right->parent = this;
- }
- return right.get();
- }
-
- unique_ptr<TreeNode> left;
- unique_ptr<TreeNode> right;
- TreeNode* parent;
-
- unique_ptr<node_type> node4; //<! IPv4 value-pair
- unique_ptr<node_type> node6; //<! IPv6 value-pair
-
- int d_bits; //<! How many bits have been used so far
+ explicit TreeNode(int bits) noexcept : parent(NULL),d_bits(bits) {
+ }
+
+ //<! Makes a left node with one more bit than parent
+ TreeNode* make_left() {
+ if (!left) {
+ left = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
+ left->parent = this;
+ }
+ return left.get();
+ }
+
+ //<! Makes a right node with one more bit than parent
+ TreeNode* make_right() {
+ if (!right) {
+ right = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
+ right->parent = this;
+ }
+ return right.get();
+ }
+
+ unique_ptr<TreeNode> left;
+ unique_ptr<TreeNode> right;
+ TreeNode* parent;
+
+ unique_ptr<node_type> node4; //<! IPv4 value-pair
+ unique_ptr<node_type> node6; //<! IPv6 value-pair
+
+ int d_bits; //<! How many bits have been used so far
};
public:
TreeNode* pparent = node->parent;
// delete this node
if (pparent) {
- if (pparent->left.get() == node)
- pparent->left.reset();
- else
- pparent->right.reset();
- // now recurse up to the parent
- cleanup_tree(pparent);
+ if (pparent->left.get() == node)
+ pparent->left.reset();
+ else
+ pparent->right.reset();
+ // now recurse up to the parent
+ cleanup_tree(pparent);
}
}
}
NetmaskTree<bool> tree;
};
-
struct SComboAddress
{
SComboAddress(const ComboAddress& orig) : ca(orig) {}
#if defined(IP_PKTINFO)
#define GEN_IP_PKTINFO IP_PKTINFO
#elif defined(IP_RECVDSTADDR)
- #define GEN_IP_PKTINFO IP_RECVDSTADDR
+ #define GEN_IP_PKTINFO IP_RECVDSTADDR
#endif
bool IsAnyAddress(const ComboAddress& addr);