FBB::Cidr(3bobcat) | CIDR matching | FBB::Cidr(3bobcat) |
FBB::Cidr - Compares IP4 addresses to CIDR specifications
#include <bobcat/cidr>
Linking option: -lbobcat
Objects of the class Cidr can be used for testing whether IP4 Internet addresses belong to address ranges defined by Classless Inter-Domain Routing (CIDR) address block specifications. CIDR blocks are specified as a.b.c.d/m where a.b.c.d are the four octets of a dotted decimal IP4 address specification (e.g., 129.125.14.80) and m is a mask-size (ranging from 0 to 32) defining the number of most significant bits to remain as-is. The CIDR specification 129.125.14.80/16 defines a class B network, with addresses ranging from 129.125.0.0 to 129.125.255.255.
The mask size does not have to be a multiple of 8. E.g., when specifying 129.125.14.80/5 only the most significant 5 bits of the first octed are fixed, resulting in an address range ranging from 128.0.0.0 to 135.255.255.255.
CIDR specifications passed to a Cidr object must be of the form a.b.c.d or a.b.c.d/m. If the mask is not specified a mask-size of 32 is used, effectively defining an address range of only one address. Mask values of 0 are ignored. Mask values of 0 are ignored by Cidr objects.
When specifying CIDRs on a stream, empty lines and comment lines (having a hash-character (#) as their first non-blank character) are ignored. Non-empty lines must start with a CIDR specification, and the Cidr object will ignore all information on a line trailing a CIDR specification.
FBB
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace FBB.
-
Default, copy and move constructors and the copy and move assignment operators are available.
The return valuess of the accessors (i.e., the const members) are only defined following a successful match (see below, the match members).
This is address 1.2.3.4 and this is 5.6.7.8and the CIDR specifications
5.1.1.1/8 1.2.1.1/16were provided to the Cidr object, then the object will report a match for 5.6.7.8.
#include <fstream> #include <iostream> #include <bobcat/exception> #ifdef BOBCAT #include <bobcat/cidr> #else #include "cidr" #endif using namespace std; using namespace FBB; int main(int argc, char **argv) { enum Spec { NONE, FILE, CIN }; Spec spec = CIN; ifstream in; if (argc > 1) { Exception::open(in, argv[1]); // file containing cidr-specs spec = FILE; } while (true) { string cidrSpec; if (spec == CIN) { cout << "Specify cidr (empty to quit): "; if (!getline(cin, cidrSpec) || cidrSpec.empty()) break; } try { Cidr cidr; switch (spec) { case NONE: return 0; case FILE: cidr.setCidr(in); spec = NONE; break; case CIN: cidr.setCidr(cidrSpec); } while (true) { cout << "Specify address to test (empty to " << (spec == CIN ? "respec. CIDR" : "quit") << "): "; string address; if (!getline(cin, address) || address.empty()) break; if (!cidr.match(address)) { cout << "Address " << address << " not in "; if (spec == CIN) cout << cidrSpec << ’\n’; else cout << "specifications in " << argv[1] << ’\n’; } else cout << "Address " << address << " in " << cidr.cidr() << "\n" "Lowest address: " << cidr.first() << "\n" "Highest address: " << cidr.last() << "\n" "CIDR mask: " << cidr.mask() << "\n" "Address: " << cidr.address() << ’\n’; } } catch (exception const &err) { cout << "Oops... " << err.what() << "\n" "Try again...\n"; } } }
bobcat/cidr - defines the class interface
bobcat(7)
Members of Cidr use static data. The current implementation of Cidr is therefore not thread-safe.
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
This is free software, distributed under the terms of the GNU General Public License (GPL).
Frank B. Brokken (f.b.brokken@rug.nl).
2005-2023 | libbobcat-dev_6.04.00 |