-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cidr.cpp
94 lines (81 loc) · 1.58 KB
/
Cidr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Author: Raymond Burkholder
* raymond@burkholder.net
*/
#include <string.h>
#include <iostream>
#include "Cidr.hpp"
Cidr::Cidr( void ) {
memset(&m_cidr, 0, sizeof(cidr_addr));
}
Cidr::Cidr( const struct in_addr& in ) {
cidr_addr* p = cidr_from_inaddr( &in );
if ( 0 == p ) {
throw error( errno );
}
else {
m_cidr = *p;
cidr_free( p );
}
}
Cidr::Cidr( const struct in6_addr& in ) {
cidr_addr* p = cidr_from_in6addr( &in );
if ( 0 == p ) {
throw error( errno );
}
else {
m_cidr = *p;
cidr_free( p );
}
}
Cidr::Cidr( const std::string& s ) {
cidr_addr* p = cidr_from_str( s.c_str() );
if ( 0 == p ) {
throw error( errno );
}
else {
m_cidr = *p;
cidr_free( p );
}
}
Cidr::Cidr( const Cidr& rhs ): m_cidr( rhs.m_cidr ) {};
Cidr::~Cidr( void ) {
}
void Cidr::divide( Cidr& a, Cidr& b ) const {
cidr_addr** p = cidr_net_subnets( &m_cidr );
if ( 0 == p ) {
throw error( errno );
}
else {
a.m_cidr = *(p[0]);
b.m_cidr = *(p[1]);
cidr_free(p[0]);
cidr_free(p[1]);
free( p );
}
}
void Cidr::supernet(Cidr& c ) const {
cidr_addr* p = cidr_net_supernet( &m_cidr );
if ( 0 == p ) {
throw error( errno );
}
else {
c.m_cidr = *p;
cidr_free( p );
}
}
const std::string Cidr::str( int flags ) const {
char* p = cidr_to_str( &m_cidr, flags );
if ( 0 == p ) {
throw error( errno );
}
else {
std::string s( p );
free( p );
return s;
}
return ""; // shouldn't be reachable
}
std::ostream& operator<<(std::ostream& o, const Cidr& c ) {
return o << c.str();
}