2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef PDNS_IPUTILSHH
23 #define PDNS_IPUTILSHH
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
33 #include "pdnsexception.hh"
35 #include <sys/socket.h>
38 #include <boost/tuple/tuple.hpp>
39 #include <boost/tuple/tuple_comparison.hpp>
41 #include "namespaces.hh"
44 #include <libkern/OSByteOrder.h>
46 #define htobe16(x) OSSwapHostToBigInt16(x)
47 #define htole16(x) OSSwapHostToLittleInt16(x)
48 #define be16toh(x) OSSwapBigToHostInt16(x)
49 #define le16toh(x) OSSwapLittleToHostInt16(x)
51 #define htobe32(x) OSSwapHostToBigInt32(x)
52 #define htole32(x) OSSwapHostToLittleInt32(x)
53 #define be32toh(x) OSSwapBigToHostInt32(x)
54 #define le32toh(x) OSSwapLittleToHostInt32(x)
56 #define htobe64(x) OSSwapHostToBigInt64(x)
57 #define htole64(x) OSSwapHostToLittleInt64(x)
58 #define be64toh(x) OSSwapBigToHostInt64(x)
59 #define le64toh(x) OSSwapLittleToHostInt64(x)
64 #define htobe16(x) BE_16(x)
65 #define htole16(x) LE_16(x)
66 #define be16toh(x) BE_IN16(&(x))
67 #define le16toh(x) LE_IN16(&(x))
69 #define htobe32(x) BE_32(x)
70 #define htole32(x) LE_32(x)
71 #define be32toh(x) BE_IN32(&(x))
72 #define le32toh(x) LE_IN32(&(x))
74 #define htobe64(x) BE_64(x)
75 #define htole64(x) LE_64(x)
76 #define be64toh(x) BE_IN64(&(x))
77 #define le64toh(x) LE_IN64(&(x))
82 #include <sys/endian.h>
85 #if defined(__NetBSD__) && defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR)
86 // The IP_PKTINFO option in NetBSD was incompatible with Linux until a
87 // change that also introduced IP_SENDSRCADDR for FreeBSD compatibility.
92 struct sockaddr_in sin4;
93 struct sockaddr_in6 sin6;
95 bool operator==(const ComboAddress& rhs) const
97 if(boost::tie(sin4.sin_family, sin4.sin_port) != boost::tie(rhs.sin4.sin_family, rhs.sin4.sin_port))
99 if(sin4.sin_family == AF_INET)
100 return sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr;
102 return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr))==0;
105 bool operator!=(const ComboAddress& rhs) const
107 return(!operator==(rhs));
110 bool operator<(const ComboAddress& rhs) const
112 if(sin4.sin_family == 0) {
115 if(boost::tie(sin4.sin_family, sin4.sin_port) < boost::tie(rhs.sin4.sin_family, rhs.sin4.sin_port))
117 if(boost::tie(sin4.sin_family, sin4.sin_port) > boost::tie(rhs.sin4.sin_family, rhs.sin4.sin_port))
120 if(sin4.sin_family == AF_INET)
121 return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr;
123 return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0;
126 bool operator>(const ComboAddress& rhs) const
128 return rhs.operator<(*this);
131 struct addressOnlyHash
133 uint32_t operator()(const ComboAddress& ca) const
135 const unsigned char* start;
137 if(ca.sin4.sin_family == AF_INET) {
138 start =(const unsigned char*)&ca.sin4.sin_addr.s_addr;
142 start =(const unsigned char*)&ca.sin6.sin6_addr.s6_addr;
145 return burtle(start, len, 0);
149 struct addressOnlyLessThan: public std::binary_function<ComboAddress, ComboAddress, bool>
151 bool operator()(const ComboAddress& a, const ComboAddress& b) const
153 if(a.sin4.sin_family < b.sin4.sin_family)
155 if(a.sin4.sin_family > b.sin4.sin_family)
157 if(a.sin4.sin_family == AF_INET)
158 return a.sin4.sin_addr.s_addr < b.sin4.sin_addr.s_addr;
160 return memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr)) < 0;
164 struct addressOnlyEqual: public std::binary_function<ComboAddress, ComboAddress, bool>
166 bool operator()(const ComboAddress& a, const ComboAddress& b) const
168 if(a.sin4.sin_family != b.sin4.sin_family)
170 if(a.sin4.sin_family == AF_INET)
171 return a.sin4.sin_addr.s_addr == b.sin4.sin_addr.s_addr;
173 return !memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr));
178 socklen_t getSocklen() const
180 if(sin4.sin_family == AF_INET)
188 sin4.sin_family=AF_INET;
189 sin4.sin_addr.s_addr=0;
191 sin6.sin6_scope_id = 0;
192 sin6.sin6_flowinfo = 0;
195 ComboAddress(const struct sockaddr *sa, socklen_t salen) {
196 setSockaddr(sa, salen);
199 ComboAddress(const struct sockaddr_in6 *sa) {
200 setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in6));
203 ComboAddress(const struct sockaddr_in *sa) {
204 setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in));
207 void setSockaddr(const struct sockaddr *sa, socklen_t salen) {
208 if (salen > sizeof(struct sockaddr_in6)) throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6");
209 memcpy(this, sa, salen);
212 // 'port' sets a default value in case 'str' does not set a port
213 explicit ComboAddress(const string& str, uint16_t port=0)
215 memset(&sin6, 0, sizeof(sin6));
216 sin4.sin_family = AF_INET;
218 if(makeIPv4sockaddr(str, &sin4)) {
219 sin6.sin6_family = AF_INET6;
220 if(makeIPv6sockaddr(str, &sin6) < 0)
221 throw PDNSException("Unable to convert presentation address '"+ str +"'");
224 if(!sin4.sin_port) // 'str' overrides port!
225 sin4.sin_port=htons(port);
230 return sin4.sin_family == AF_INET6;
234 return sin4.sin_family == AF_INET;
237 bool isMappedIPv4() const
239 if(sin4.sin_family!=AF_INET6)
243 const unsigned char*ptr = (unsigned char*) &sin6.sin6_addr.s6_addr;
244 for(n=0; n < 10; ++n)
255 ComboAddress mapToIPv4() const
258 throw PDNSException("ComboAddress can't map non-mapped IPv6 address back to IPv4");
260 ret.sin4.sin_family=AF_INET;
261 ret.sin4.sin_port=sin4.sin_port;
263 const unsigned char*ptr = (unsigned char*) &sin6.sin6_addr.s6_addr;
264 ptr+=(sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr));
265 memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr));
269 string toString() const
273 if(sin4.sin_family && !(retval = getnameinfo((struct sockaddr*) this, getSocklen(), host, sizeof(host),0, 0, NI_NUMERICHOST)))
276 return "invalid "+string(gai_strerror(retval));
279 string toStringWithPort() const
281 if(sin4.sin_family==AF_INET)
282 return toString() + ":" + std::to_string(ntohs(sin4.sin_port));
284 return "["+toString() + "]:" + std::to_string(ntohs(sin4.sin_port));
287 string toStringWithPortExcept(int port) const
289 if(ntohs(sin4.sin_port) == port)
291 if(sin4.sin_family==AF_INET)
292 return toString() + ":" + std::to_string(ntohs(sin4.sin_port));
294 return "["+toString() + "]:" + std::to_string(ntohs(sin4.sin_port));
297 string toLogString() const
299 return toStringWithPortExcept(53);
302 void truncate(unsigned int bits) noexcept;
304 uint16_t getPort() const
306 return ntohs(sin4.sin_port);
309 ComboAddress setPort(uint16_t port) const
311 ComboAddress ret(*this);
312 ret.sin4.sin_port=htons(port);
318 memset(&sin4, 0, sizeof(sin4));
319 memset(&sin6, 0, sizeof(sin6));
324 /** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */
325 class NetmaskException: public PDNSException
328 NetmaskException(const string &a) : PDNSException(a) {}
331 inline ComboAddress makeComboAddress(const string& str)
333 ComboAddress address;
334 address.sin4.sin_family=AF_INET;
335 if(inet_pton(AF_INET, str.c_str(), &address.sin4.sin_addr) <= 0) {
336 address.sin4.sin_family=AF_INET6;
337 if(makeIPv6sockaddr(str, &address.sin6) < 0)
338 throw NetmaskException("Unable to convert '"+str+"' to a netmask");
343 inline ComboAddress makeComboAddressFromRaw(uint8_t version, const char* raw, size_t len)
345 ComboAddress address;
348 address.sin4.sin_family = AF_INET;
349 if (len != sizeof(address.sin4.sin_addr)) throw NetmaskException("invalid raw address length");
350 memcpy(&address.sin4.sin_addr, raw, sizeof(address.sin4.sin_addr));
352 else if (version == 6) {
353 address.sin6.sin6_family = AF_INET6;
354 if (len != sizeof(address.sin6.sin6_addr)) throw NetmaskException("invalid raw address length");
355 memcpy(&address.sin6.sin6_addr, raw, sizeof(address.sin6.sin6_addr));
357 else throw NetmaskException("invalid address family");
362 inline ComboAddress makeComboAddressFromRaw(uint8_t version, const string &str)
364 return makeComboAddressFromRaw(version, str.c_str(), str.size());
367 /** This class represents a netmask and can be queried to see if a certain
368 IP address is matched by this mask */
374 d_network.sin4.sin_family=0; // disable this doing anything useful
375 d_network.sin4.sin_port = 0; // this guarantees d_network compares identical
380 Netmask(const ComboAddress& network, uint8_t bits=0xff): d_network(network)
382 d_network.sin4.sin_port=0;
384 bits = (network.sin4.sin_family == AF_INET) ? 32 : 128;
388 d_mask=~(0xFFFFFFFF>>d_bits);
390 d_mask=0xFFFFFFFF; // not actually used for IPv6
393 //! Constructor supplies the mask, which cannot be changed
394 Netmask(const string &mask)
396 pair<string,string> split=splitField(mask,'/');
397 d_network=makeComboAddress(split.first);
399 if(!split.second.empty()) {
400 d_bits = (uint8_t)pdns_stou(split.second);
402 d_mask=~(0xFFFFFFFF>>d_bits);
406 else if(d_network.sin4.sin_family==AF_INET) {
412 d_mask=0; // silence silly warning - d_mask is unused for IPv6
416 bool match(const ComboAddress& ip) const
421 //! If this IP address in socket address matches
422 bool match(const ComboAddress *ip) const
424 if(d_network.sin4.sin_family != ip->sin4.sin_family) {
427 if(d_network.sin4.sin_family == AF_INET) {
428 return match4(htonl((unsigned int)ip->sin4.sin_addr.s_addr));
430 if(d_network.sin6.sin6_family == AF_INET6) {
431 uint8_t bytes=d_bits/8, n;
432 const uint8_t *us=(const uint8_t*) &d_network.sin6.sin6_addr.s6_addr;
433 const uint8_t *them=(const uint8_t*) &ip->sin6.sin6_addr.s6_addr;
435 for(n=0; n < bytes; ++n) {
440 // still here, now match remaining bits
441 uint8_t bits= d_bits % 8;
442 uint8_t mask= (uint8_t) ~(0xFF>>bits);
444 return((us[n] & mask) == (them[n] & mask));
449 //! If this ASCII IP address matches
450 bool match(const string &ip) const
452 ComboAddress address=makeComboAddress(ip);
453 return match(&address);
456 //! If this IP address in native format matches
457 bool match4(uint32_t ip) const
459 return (ip & d_mask) == (ntohl(d_network.sin4.sin_addr.s_addr) & d_mask);
462 string toString() const
464 return d_network.toString()+"/"+std::to_string((unsigned int)d_bits);
467 string toStringNoMask() const
469 return d_network.toString();
471 const ComboAddress& getNetwork() const
475 const ComboAddress getMaskedNetwork() const
477 ComboAddress result(d_network);
479 result.sin4.sin_addr.s_addr = htonl(ntohl(result.sin4.sin_addr.s_addr) & d_mask);
483 uint8_t bytes=d_bits/8;
484 uint8_t *us=(uint8_t*) &result.sin6.sin6_addr.s6_addr;
485 uint8_t bits= d_bits % 8;
486 uint8_t mask= (uint8_t) ~(0xFF>>bits);
488 if (bytes < sizeof(result.sin6.sin6_addr.s6_addr)) {
492 for(idx = bytes + 1; idx < sizeof(result.sin6.sin6_addr.s6_addr); ++idx) {
498 uint8_t getBits() const
504 return d_network.sin6.sin6_family == AF_INET6;
508 return d_network.sin4.sin_family == AF_INET;
511 bool operator<(const Netmask& rhs) const
513 if (empty() && !rhs.empty())
516 if (!empty() && rhs.empty())
519 if (d_bits > rhs.d_bits)
521 if (d_bits < rhs.d_bits)
524 return d_network < rhs.d_network;
527 bool operator>(const Netmask& rhs) const
529 return rhs.operator<(*this);
532 bool operator==(const Netmask& rhs) const
534 return tie(d_network, d_bits) == tie(rhs.d_network, rhs.d_bits);
539 return d_network.sin4.sin_family==0;
543 ComboAddress d_network;
548 /** Per-bit binary tree map implementation with <Netmask,T> pair.
550 * This is an binary tree implementation for storing attributes for IPv4 and IPv6 prefixes.
551 * The most simple use case is simple NetmaskTree<bool> used by NetmaskGroup, which only
552 * wants to know if given IP address is matched in the prefixes stored.
554 * This element is useful for anything that needs to *STORE* prefixes, and *MATCH* IP addresses
555 * to a *LIST* of *PREFIXES*. Not the other way round.
557 * You can store IPv4 and IPv6 addresses to same tree, separate payload storage is kept per AFI.
559 * To erase something copy values to new tree sans the value you want to erase.
561 * Use swap if you need to move the tree to another NetmaskTree instance, it is WAY faster
562 * than using copy ctor or assignment operator, since it moves the nodes and tree root to
563 * new home instead of actually recreating the tree.
565 * Please see NetmaskGroup for example of simple use case. Other usecases can be found
566 * from GeoIPBackend and Sortlist, and from dnsdist.
568 template <typename T>
571 typedef Netmask key_type;
572 typedef T value_type;
573 typedef std::pair<key_type,value_type> node_type;
574 typedef size_t size_type;
577 /** Single node in tree, internal use only.
579 class TreeNode : boost::noncopyable {
581 explicit TreeNode(int bits) noexcept : parent(NULL),d_bits(bits) {
584 //<! Makes a left node with one more bit than parent
585 TreeNode* make_left() {
587 left = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
593 //<! Makes a right node with one more bit than parent
594 TreeNode* make_right() {
596 right = unique_ptr<TreeNode>(new TreeNode(d_bits+1));
597 right->parent = this;
602 unique_ptr<TreeNode> left;
603 unique_ptr<TreeNode> right;
606 unique_ptr<node_type> node4; //<! IPv4 value-pair
607 unique_ptr<node_type> node6; //<! IPv6 value-pair
609 int d_bits; //<! How many bits have been used so far
613 NetmaskTree() noexcept : NetmaskTree(false) {
616 NetmaskTree(bool cleanup) noexcept : d_cleanup_tree(cleanup) {
619 NetmaskTree(const NetmaskTree& rhs): d_cleanup_tree(rhs.d_cleanup_tree) {
620 // it is easier to copy the nodes than tree.
621 // also acts as handy compactor
622 for(auto const& node: rhs._nodes)
623 insert(node->first).second = node->second;
626 NetmaskTree& operator=(const NetmaskTree& rhs) {
629 for(auto const& node: rhs._nodes)
630 insert(node->first).second = node->second;
631 d_cleanup_tree = rhs.d_cleanup_tree;
635 const typename std::set<node_type*>::const_iterator begin() const { return _nodes.begin(); }
636 const typename std::set<node_type*>::const_iterator end() const { return _nodes.end(); }
638 typename std::set<node_type*>::iterator begin() { return _nodes.begin(); }
639 typename std::set<node_type*>::iterator end() { return _nodes.end(); }
641 node_type& insert(const string &mask) {
642 return insert(key_type(mask));
645 //<! Creates new value-pair in tree and returns it.
646 node_type& insert(const key_type& key) {
647 // lazily initialize tree on first insert.
648 if (!root) root = unique_ptr<TreeNode>(new TreeNode(0));
649 TreeNode* node = root.get();
650 node_type* value = nullptr;
652 if (key.getNetwork().sin4.sin_family == AF_INET) {
653 std::bitset<32> addr(be32toh(key.getNetwork().sin4.sin_addr.s_addr));
655 // we turn left on 0 and right on 1
656 while(bits < key.getBits()) {
657 uint8_t val = addr[31-bits];
659 node = node->make_right();
661 node = node->make_left();
664 // only create node if not yet assigned
666 node->node4 = unique_ptr<node_type>(new node_type());
667 _nodes.insert(node->node4.get());
669 value = node->node4.get();
671 uint64_t* addr = (uint64_t*)key.getNetwork().sin6.sin6_addr.s6_addr;
672 std::bitset<64> addr_low(be64toh(addr[1]));
673 std::bitset<64> addr_high(be64toh(addr[0]));
675 while(bits < key.getBits()) {
677 // we use high address until we are
678 if (bits < 64) val = addr_high[63-bits];
679 // past 64 bits, and start using low address
680 else val = addr_low[127-bits];
682 // we turn left on 0 and right on 1
684 node = node->make_right();
686 node = node->make_left();
689 // only create node if not yet assigned
691 node->node6 = unique_ptr<node_type>(new node_type());
692 _nodes.insert(node->node6.get());
694 value = node->node6.get();
701 //<! Creates or updates value
702 void insert_or_assign(const key_type& mask, const value_type& value) {
703 insert(mask).second = value;
706 void insert_or_assign(const string& mask, const value_type& value) {
707 insert(key_type(mask)).second = value;
710 //<! check if given key is present in TreeMap
711 bool has_key(const key_type& key) const {
712 const node_type *ptr = lookup(key);
713 return ptr && ptr->first == key;
716 //<! Returns "best match" for key_type, which might not be value
717 const node_type* lookup(const key_type& value) const {
718 return lookup(value.getNetwork(), value.getBits());
721 //<! Perform best match lookup for value, using at most max_bits
722 const node_type* lookup(const ComboAddress& value, int max_bits = 128) const {
723 if (!root) return nullptr;
725 TreeNode *node = root.get();
726 node_type *ret = nullptr;
728 // exact same thing as above, except
729 if (value.sin4.sin_family == AF_INET) {
730 max_bits = std::max(0,std::min(max_bits,32));
731 std::bitset<32> addr(be32toh(value.sin4.sin_addr.s_addr));
734 while(bits < max_bits) {
735 // ...we keep track of last non-empty node
736 if (node->node4) ret = node->node4.get();
737 uint8_t val = addr[31-bits];
738 // ...and we don't create left/right hand
740 if (node->right) node = node->right.get();
741 // ..and we break when road ends
744 if (node->left) node = node->left.get();
749 // needed if we did not find one in loop
750 if (node->node4) ret = node->node4.get();
752 uint64_t* addr = (uint64_t*)value.sin6.sin6_addr.s6_addr;
753 max_bits = std::max(0,std::min(max_bits,128));
754 std::bitset<64> addr_low(be64toh(addr[1]));
755 std::bitset<64> addr_high(be64toh(addr[0]));
757 while(bits < max_bits) {
758 if (node->node6) ret = node->node6.get();
760 if (bits < 64) val = addr_high[63-bits];
761 else val = addr_low[127-bits];
763 if (node->right) node = node->right.get();
766 if (node->left) node = node->left.get();
771 if (node->node6) ret = node->node6.get();
774 // this can be nullptr.
778 void cleanup_tree(TreeNode* node)
780 // only cleanup this node if it has no children and node4 and node6 are both empty
781 if (!(node->left || node->right || node->node6 || node->node4)) {
782 // get parent node ptr
783 TreeNode* parent = node->parent;
786 if (parent->left.get() == node)
787 parent->left.reset();
789 parent->right.reset();
790 // now recurse up to the parent
791 cleanup_tree(parent);
796 //<! Removes key from TreeMap. This does not clean up the tree.
797 void erase(const key_type& key) {
798 TreeNode *node = root.get();
801 if ( node == nullptr ) return;
803 // exact same thing as above, except
804 if (key.getNetwork().sin4.sin_family == AF_INET) {
805 std::bitset<32> addr(be32toh(key.getNetwork().sin4.sin_addr.s_addr));
807 while(node && bits < key.getBits()) {
808 uint8_t val = addr[31-bits];
810 node = node->right.get();
812 node = node->left.get();
817 _nodes.erase(node->node4.get());
824 uint64_t* addr = (uint64_t*)key.getNetwork().sin6.sin6_addr.s6_addr;
825 std::bitset<64> addr_low(be64toh(addr[1]));
826 std::bitset<64> addr_high(be64toh(addr[0]));
828 while(node && bits < key.getBits()) {
830 if (bits < 64) val = addr_high[63-bits];
831 else val = addr_low[127-bits];
833 node = node->right.get();
835 node = node->left.get();
840 _nodes.erase(node->node6.get());
849 void erase(const string& key) {
850 erase(key_type(key));
853 //<! checks whether the container is empty.
855 return _nodes.empty();
858 //<! returns the number of elements
859 size_type size() const {
860 return _nodes.size();
863 //<! See if given ComboAddress matches any prefix
864 bool match(const ComboAddress& value) const {
865 return (lookup(value) != nullptr);
868 bool match(const std::string& value) const {
869 return match(ComboAddress(value));
872 //<! Clean out the tree
878 //<! swaps the contents, rhs is left with nullptr.
879 void swap(NetmaskTree& rhs) {
881 _nodes.swap(rhs._nodes);
885 unique_ptr<TreeNode> root; //<! Root of our tree
886 std::set<node_type*> _nodes; //<! Container for actual values
887 bool d_cleanup_tree; //<! Whether or not to cleanup the tree on erase
890 /** This class represents a group of supplemental Netmask classes. An IP address matchs
891 if it is matched by zero or more of the Netmask classes within.
896 //! By default, initialise the tree to cleanup
897 NetmaskGroup() noexcept : NetmaskGroup(true) {
900 //! This allows control over whether to cleanup or not
901 NetmaskGroup(bool cleanup) noexcept : tree(cleanup) {
904 //! If this IP address is matched by any of the classes within
906 bool match(const ComboAddress *ip) const
908 const auto &ret = tree.lookup(*ip);
909 if(ret) return ret->second;
913 bool match(const ComboAddress& ip) const
918 bool lookup(const ComboAddress* ip, Netmask* nmp) const
920 const auto &ret = tree.lookup(*ip);
930 bool lookup(const ComboAddress& ip, Netmask* nmp) const
932 return lookup(&ip, nmp);
935 //! Add this string to the list of possible matches
936 void addMask(const string &ip, bool positive=true)
938 if(!ip.empty() && ip[0] == '!') {
939 addMask(Netmask(ip.substr(1)), false);
941 addMask(Netmask(ip), positive);
945 //! Add this Netmask to the list of possible matches
946 void addMask(const Netmask& nm, bool positive=true)
948 tree.insert(nm).second=positive;
951 //! Delete this Netmask from the list of possible matches
952 void deleteMask(const Netmask& nm)
957 void deleteMask(const std::string& ip)
960 deleteMask(Netmask(ip));
978 string toString() const
981 for(auto iter = tree.begin(); iter != tree.end(); ++iter) {
982 if(iter != tree.begin())
984 if(!((*iter)->second))
986 str<<(*iter)->first.toString();
991 void toStringVector(vector<string>* vec) const
993 for(auto iter = tree.begin(); iter != tree.end(); ++iter) {
994 vec->push_back(((*iter)->second ? "" : "!") + (*iter)->first.toString());
998 void toMasks(const string &ips)
1000 vector<string> parts;
1001 stringtok(parts, ips, ", \t");
1003 for (vector<string>::const_iterator iter = parts.begin(); iter != parts.end(); ++iter)
1008 NetmaskTree<bool> tree;
1012 struct SComboAddress
1014 SComboAddress(const ComboAddress& orig) : ca(orig) {}
1016 bool operator<(const SComboAddress& rhs) const
1018 return ComboAddress::addressOnlyLessThan()(ca, rhs.ca);
1020 operator const ComboAddress&()
1026 class NetworkError : public runtime_error
1029 NetworkError(const string& why="Network Error") : runtime_error(why.c_str())
1031 NetworkError(const char *why="Network Error") : runtime_error(why)
1035 int SSocket(int family, int type, int flags);
1036 int SConnect(int sockfd, const ComboAddress& remote);
1037 /* tries to connect to remote for a maximum of timeout seconds.
1038 sockfd should be set to non-blocking beforehand.
1039 returns 0 on success (the socket is writable), throw a
1040 runtime_error otherwise */
1041 int SConnectWithTimeout(int sockfd, const ComboAddress& remote, int timeout);
1042 int SBind(int sockfd, const ComboAddress& local);
1043 int SAccept(int sockfd, ComboAddress& remote);
1044 int SListen(int sockfd, int limit);
1045 int SSetsockopt(int sockfd, int level, int opname, int value);
1047 #if defined(IP_PKTINFO)
1048 #define GEN_IP_PKTINFO IP_PKTINFO
1049 #elif defined(IP_RECVDSTADDR)
1050 #define GEN_IP_PKTINFO IP_RECVDSTADDR
1053 bool IsAnyAddress(const ComboAddress& addr);
1054 bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destination);
1055 bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv);
1056 void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, char* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr);
1057 ssize_t sendfromto(int sock, const char* data, size_t len, int flags, const ComboAddress& from, const ComboAddress& to);
1058 ssize_t sendMsgWithTimeout(int fd, const char* buffer, size_t len, int timeout, ComboAddress& dest, const ComboAddress& local, unsigned int localItf);
1059 bool sendSizeAndMsgWithTimeout(int sock, uint16_t bufferLen, const char* buffer, int idleTimeout, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int totalTimeout, int flags);
1060 /* requires a non-blocking, connected TCP socket */
1061 bool isTCPSocketUsable(int sock);
1063 extern template class NetmaskTree<bool>;