summaryrefslogtreecommitdiff
path: root/src/net/ip.cpp
blob: bedbca8a81bb214a86c58adeeaf877ec6414e091 (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
#include "ip.hpp"
//    ip.cpp - Implementation of IP address functions.
//
//    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 "../strings/xstring.hpp"
#include "../strings/vstring.hpp"

#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"

#include "../poison.hpp"


namespace tmwa
{
bool impl_extract(XString str, IP4Address *rv)
{
    if (str.endswith('.'))
        return false;
    uint8_t buf[4];
    if (extract(str, record<'.'>(&buf[0], &buf[1], &buf[2], &buf[3])))
    {
        *rv = IP4Address(buf);
        return true;
    }
    return false;
}

bool impl_extract(XString str, IP4Mask *rv)
{
    IP4Address a, m;
    unsigned b;
    XString l, r;
    if (str.endswith('/'))
        return false;
    if (extract(str, record<'/'>(&l, &r)))
    {
        // a.b.c.d/e.f.g.h or a.b.c.d/n
        if (!extract(l, &a))
            return false;
        if (extract(r, &m))
        {
            *rv = IP4Mask(a, m);
            return true;
        }
        if (!extract(r, &b) || b > 32)
            return false;
    }
    else
    {
        // a.b.c.d or a.b.c.d. or a.b.c. or a.b. or a.
        if (extract(str, &a))
        {
            *rv = IP4Mask(a, IP4_BROADCAST);
            return true;
        }
        if (!str.endswith('.'))
            return false;
        uint8_t d[4] {};
        if (extract(str, record<'.'>(&d[0], &d[1], &d[2], &d[3])))
            b = 32;
        else if (extract(str, record<'.'>(&d[0], &d[1], &d[2])))
            b = 24;
        else if (extract(str, record<'.'>(&d[0], &d[1])))
            b = 16;
        else if (extract(str, record<'.'>(&d[0])))
            b = 8;
        else
            return false;
        a = IP4Address(d);
    }
    // a is set; need to construct m from b
    if (b == 0)
        m = IP4Address();
    else if (b == 32)
        m = IP4_BROADCAST;
    else
    {
        uint32_t s = -1;
        s <<= (32 - b);
        m = IP4Address({
                static_cast<uint8_t>(s >> 24),
                static_cast<uint8_t>(s >> 16),
                static_cast<uint8_t>(s >> 8),
                static_cast<uint8_t>(s >> 0),
        });
    }
    *rv = IP4Mask(a, m);
    return true;
}

bool impl_extract(XString str, std::vector<IP4Mask> *iv)
{
    if (str == "all"_s)
    {
        iv->clear();
        iv->push_back(IP4Mask());
        return true;
    }
    if (str == "clear"_s)
    {
        iv->clear();
        return true;
    }
    // don't add if already 'all'
    if (iv->size() == 1 && iv->front().mask() == IP4Address())
    {
        return true;
    }
    IP4Mask mask;
    if (extract(str, &mask))
    {
        iv->push_back(mask);
        return true;
    }
    return false;
}

VString<15> convert_for_printf(IP4Address a_)
{
    const uint8_t *a = a_.bytes();
    return STRNPRINTF(16, "%hhu.%hhu.%hhu.%hhu"_fmt, a[0], a[1], a[2], a[3]);
}

VString<31> convert_for_printf(IP4Mask a)
{
    return STRNPRINTF(32, "%s/%s"_fmt,
            a.addr(), a.mask());
}
} // namespace tmwa