aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/sys_util.cpp
blob: f011825be496975b1a2c57b0bca7dcb675c31ad5 (plain)
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
#include "sys_util.h"

namespace utils {

prefix::prefix()
{
}

prefix::prefix(const prefix &p)
{
    m_address = p.address();
    m_prefix_len = p.prefix_length();
}

prefix::prefix(std::string p)
{
    size_t found = p.find_last_of('/');

    if (found == std::string::npos) //not found
        throw std::runtime_error("missing '/' in " + p);

    m_address = boost::asio::ip::address::from_string(p.substr(0, found));
    m_prefix_len =  std::stoi(p.substr(found+1, p.length()));
}

prefix prefix::make_prefix(std::string str)
{
    prefix tmp(str);
    return prefix(tmp);
}

unsigned short prefix::prefix_length() const
{
    return m_prefix_len;
}

boost::asio::ip::address prefix::address() const
{
    return m_address;
}

std::string prefix::to_string() const
{
    ostringstream os;
    os << m_address << "/" << m_prefix_len;
    return os.str();
}

std::ostream& operator<<(std::ostream& os, const prefix& p)
{
    os << p.to_string();

    return os;
}

bool prefix::empty() const
{
    return to_string().empty();
}

}