summaryrefslogtreecommitdiff
path: root/src/common/ip.hpp
blob: aceb7650e131fe55e19f1dad53c7229be831f705 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef TMWA_COMMON_IP_HPP
#define TMWA_COMMON_IP_HPP
//    ip.hpp - classes to deal with IP addresses.
//
//    Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "sanity.hpp"

#include <netinet/in.h>

#include "extract.hpp"
#include "strings.hpp"

// TODO - in the long run ports belong here also
// and of course, IPv6 stuff.
// But what about unix socket addresses?

/// Helper function
template<class T, size_t n>
constexpr
bool _ce_a_lt(T (&a)[n], T (&b)[n], size_t i=0)
{
    return (i != n
            && (a[i] < b[i]
                || (a[i] == b[i]
                    && _ce_a_lt(a, b, i + 1))));
}

/// A 32-bit Ipv4 address. Does not include a port.
/// Guaranteed to be laid out like the network wants.
class IP4Address
{
    uint8_t _addr[4];
public:
    constexpr
    IP4Address()
    : _addr{}
    {}
    constexpr explicit
    IP4Address(const uint8_t (&a)[4])
    : _addr{a[0], a[1], a[2], a[3]}
    {}
    explicit
    IP4Address(struct in_addr addr)
    {
        static_assert(sizeof(addr) == sizeof(_addr), "4 bytes");
        *this = IP4Address(reinterpret_cast<const uint8_t (&)[4]>(addr));
    }
    explicit
    operator struct in_addr() const
    {
        return reinterpret_cast<const struct in_addr&>(_addr);
    }

    constexpr friend
    IP4Address operator & (IP4Address l, IP4Address r)
    {
        return IP4Address({
                static_cast<uint8_t>(l._addr[0] & r._addr[0]),
                static_cast<uint8_t>(l._addr[1] & r._addr[1]),
                static_cast<uint8_t>(l._addr[2] & r._addr[2]),
                static_cast<uint8_t>(l._addr[3] & r._addr[3]),
        });
    }

    IP4Address& operator &= (IP4Address m)
    { return *this = *this & m; }

    const uint8_t *bytes() const
    { return _addr; }

    constexpr friend
    bool operator < (IP4Address l, IP4Address r)
    {
        return _ce_a_lt(l._addr, r._addr);
    }

    constexpr friend
    bool operator > (IP4Address l, IP4Address r)
    {
        return _ce_a_lt(r._addr, l._addr);
    }

    constexpr friend
    bool operator >= (IP4Address l, IP4Address r)
    {
        return !_ce_a_lt(l._addr, r._addr);
    }

    constexpr friend
    bool operator <= (IP4Address l, IP4Address r)
    {
        return !_ce_a_lt(r._addr, l._addr);
    }

    constexpr friend
    bool operator == (IP4Address l, IP4Address r)
    {
        return !(l < r || r < l);
    }

    constexpr friend
    bool operator != (IP4Address l, IP4Address r)
    {
        return (l < r || r < l);
    }
};

class IP4Mask
{
    IP4Address _addr, _mask;
public:
    constexpr
    IP4Mask() : _addr(), _mask()
    {}
    constexpr
    IP4Mask(IP4Address a, IP4Address m) : _addr(a & m), _mask(m)
    {}

    constexpr
    IP4Address addr() const
    { return _addr; }
    constexpr
    IP4Address mask() const
    { return _mask; }

    constexpr
    bool covers(IP4Address a) const
    {
        return (a & _mask) == _addr;
    }
};


constexpr
IP4Address IP4_LOCALHOST({127, 0, 0, 1});
constexpr
IP4Address IP4_BROADCAST({255, 255, 255, 255});


VString<15> convert_for_printf(IP4Address a);
VString<31> convert_for_printf(IP4Mask m);

bool extract(XString str, IP4Address *iv);

bool extract(XString str, IP4Mask *iv);

#endif // TMWA_COMMON_IP_HPP