1 | #ifndef NET_H |
---|
2 | #define NET_H |
---|
3 | |
---|
4 | #include <cstdlib> |
---|
5 | #include <iostream> |
---|
6 | #include <string> |
---|
7 | |
---|
8 | class Net { |
---|
9 | // A network. It has a mask and an address. The mask is the mask size in |
---|
10 | // consecutive bits: net= 10.0.0.0 mask =8 means 0xa000000000 network and |
---|
11 | // 0xff000000 mask. Masked bits are always 0 in net. |
---|
12 | protected: |
---|
13 | int net; // Network |
---|
14 | int mask; // Mask |
---|
15 | // Clear the net to the mask. |
---|
16 | void normalize() { net &= 0xffffffff & (0xffffffff << (32 - mask)); } |
---|
17 | public: |
---|
18 | Net(const Net& n) : net(n.net), mask(n.mask) { normalize(); } |
---|
19 | // m is the number of consecutive 1 bits in the mask. |
---|
20 | Net(int n, int m) : net(n), mask(m) { normalize(); } |
---|
21 | |
---|
22 | // Parse a string of the form n.n.n.n/m |
---|
23 | Net(const std::string& s) : net(0), mask(0) { |
---|
24 | std::string::size_type mi = s.find("/"); |
---|
25 | |
---|
26 | if ( mi != std::string::npos ) mask = atoi(s.substr(mi+1).c_str()); |
---|
27 | else mask = 32; |
---|
28 | |
---|
29 | std::string::size_type p = 0; |
---|
30 | for (int i = 24; i >= 0; i -= 8 ) { |
---|
31 | int x = atoi(s.substr(p).c_str()); |
---|
32 | net |= (x) << i; |
---|
33 | |
---|
34 | if ( (p = s.find(".", p)) != std::string::npos) p++; |
---|
35 | } |
---|
36 | normalize(); |
---|
37 | } |
---|
38 | |
---|
39 | // Accessors |
---|
40 | int get_net() const { return net; } |
---|
41 | void set_net(int n) { net = n; normalize(); } |
---|
42 | int get_mask() const { return mask; } |
---|
43 | void set_mask(int m) { mask = m; normalize(); } |
---|
44 | |
---|
45 | // Output in n.n.n.n/m format. |
---|
46 | std::ostream& print(std::ostream& f) const { |
---|
47 | f << ( (net & 0xff000000) >> 24) << "."; |
---|
48 | f << ( (net & 0x00ff0000) >> 16) << "."; |
---|
49 | f << ( (net & 0x0000ff00) >> 8) << "."; |
---|
50 | f << ( (net & 0x000000ff)); |
---|
51 | f << "/" << mask; |
---|
52 | return f; |
---|
53 | } |
---|
54 | |
---|
55 | // True if the given net is inside this net using this net's mask |
---|
56 | bool inside(const Net& n) const { |
---|
57 | Net tn = Net(net, n.mask); |
---|
58 | return (tn.net ^ n.net) == 0; |
---|
59 | } |
---|
60 | |
---|
61 | // Standard operators |
---|
62 | bool operator==(const Net& n) const { |
---|
63 | return net == n.net && mask == n.mask; |
---|
64 | } |
---|
65 | |
---|
66 | bool operator<(const Net& n) const { |
---|
67 | return mask < n.mask || ( (mask == n.mask) && (net < n.net)); |
---|
68 | } |
---|
69 | }; |
---|
70 | |
---|
71 | // Convenient << defintion for output |
---|
72 | inline std::ostream& operator<<(std::ostream& f, const Net& n) { |
---|
73 | return n.print(f); |
---|
74 | } |
---|
75 | |
---|
76 | // A Net specialized to have a 32 bit mask and print without the / |
---|
77 | class Host : public Net { |
---|
78 | public: |
---|
79 | Host(const Host& h) : Net(h) { } |
---|
80 | Host(int h) : Net(h, 32) { } |
---|
81 | Host(const std::string& s) : Net(s) { } |
---|
82 | |
---|
83 | std::ostream& print(std::ostream& f) const { |
---|
84 | f << ( (net & 0xff000000) >> 24) << "."; |
---|
85 | f << ( (net & 0x00ff0000) >> 16) << "."; |
---|
86 | f << ( (net & 0x0000ff00) >> 8) << "."; |
---|
87 | f << ( (net & 0x000000ff)); |
---|
88 | return f; |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | // Convenient << defintion for output |
---|
93 | inline std::ostream& operator<<(std::ostream& f, const Host& n) { |
---|
94 | return n.print(f); |
---|
95 | } |
---|
96 | |
---|
97 | #endif |
---|