Line data Source code
1 : // Copyright (c) 2012-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libaddr
4 : //
5 : // Permission is hereby granted, free of charge, to any person obtaining a
6 : // copy of this software and associated documentation files (the
7 : // "Software"), to deal in the Software without restriction, including
8 : // without limitation the rights to use, copy, modify, merge, publish,
9 : // distribute, sublicense, and/or sell copies of the Software, and to
10 : // permit persons to whom the Software is furnished to do so, subject to
11 : // the following conditions:
12 : //
13 : // The above copyright notice and this permission notice shall be included
14 : // in all copies or substantial portions of the Software.
15 : //
16 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 : #pragma once
24 :
25 : /** \file
26 : * \brief The declaration of the parser of the libaddr classes.
27 : *
28 : * This header includes the addr_parser class used to parse user input
29 : * string and convert them to binary IP addresses.
30 : */
31 :
32 : // self
33 : //
34 : #include "libaddr/addr_range.h"
35 :
36 :
37 :
38 : namespace addr
39 : {
40 :
41 :
42 : enum class allow_t
43 : {
44 : ALLOW_ADDRESS, // address (IP)
45 : ALLOW_REQUIRED_ADDRESS, // address cannot be empty
46 : ALLOW_MULTI_ADDRESSES_COMMAS, // IP:port/mask,IP:port/mask,...
47 : ALLOW_MULTI_ADDRESSES_SPACES, // IP:port/mask IP:port/mask ...
48 : ALLOW_ADDRESS_LOOKUP, // whether DNS lookup is allowed
49 :
50 : ALLOW_PORT, // port
51 : ALLOW_REQUIRED_PORT, // port cannot be empty
52 :
53 : ALLOW_MASK, // mask
54 :
55 : // TODO: the following are not yet implemented
56 : ALLOW_MULTI_PORTS_SEMICOLONS, // port1;port2;...
57 : ALLOW_MULTI_PORTS_COMMAS, // port1,port2,...
58 : ALLOW_PORT_RANGE, // port1-port2
59 : ALLOW_ADDRESS_RANGE, // IP-IP
60 :
61 : ALLOW_max
62 : };
63 :
64 :
65 : typedef std::uint_fast16_t sort_t;
66 :
67 : constexpr sort_t const SORT_NO = 0x0000; // keep IPs as found
68 : constexpr sort_t const SORT_IPV6_FIRST = 0x0001; // put IPv6 first (IPv6, IPv4, empty)
69 : constexpr sort_t const SORT_IPV4_FIRST = 0x0002; // put IPv4 first (IPv4, IPv6, empty)
70 : constexpr sort_t const SORT_FULL = 0x0004; // sort IPs between each others (default keep in order found)
71 : constexpr sort_t const SORT_MERGE = 0x0008 | SORT_FULL; // merge ranges which support a union (implies SORT_FULL)
72 : constexpr sort_t const SORT_NO_EMPTY = 0x0010; // remove empty entries
73 :
74 :
75 131953 : class addr_parser
76 : {
77 : public:
78 : addr_parser();
79 :
80 : void set_default_address(std::string const & address);
81 : std::string const & get_default_address4() const;
82 : std::string const & get_default_address6() const;
83 :
84 : void set_default_port(int const port);
85 : int get_default_port() const;
86 :
87 : void set_default_mask(std::string const & mask);
88 : std::string const & get_default_mask4() const;
89 : std::string const & get_default_mask6() const;
90 :
91 : void set_protocol(std::string const & protocol);
92 : void set_protocol(int const protocol);
93 : void clear_protocol();
94 : int get_protocol() const;
95 :
96 : void set_sort_order(sort_t const sort);
97 : sort_t get_sort_order() const;
98 :
99 : void set_allow(allow_t const flag, bool const allow);
100 : bool get_allow(allow_t const flag) const;
101 :
102 : bool has_errors() const;
103 : void emit_error(std::string const & msg);
104 : std::string const & error_messages() const;
105 : int error_count() const;
106 : void clear_errors();
107 :
108 : addr_range::vector_t parse(std::string const & in);
109 :
110 : private:
111 : void parse_cidr(std::string const & in, addr_range::vector_t & result);
112 : void parse_address(std::string const & in, std::string const & mask, addr_range::vector_t & result);
113 : void parse_address4(std::string const & in, addr_range::vector_t & result);
114 : void parse_address6(std::string const & in, addr_range::vector_t & result);
115 : void parse_address_port(std::string address, std::string port_str, addr_range::vector_t & result, bool ipv6);
116 : void parse_mask(std::string const & mask, addr & cidr);
117 :
118 : bool f_flags[static_cast<int>(allow_t::ALLOW_max)] = {};
119 : sort_t f_sort = SORT_NO;
120 : std::string f_default_address4 = std::string();
121 : std::string f_default_address6 = std::string();
122 : std::string f_default_mask4 = std::string();
123 : std::string f_default_mask6 = std::string();
124 : int f_protocol = -1;
125 : int f_default_port = -1;
126 : std::string f_error = std::string();
127 : int f_error_count = 0;
128 : };
129 :
130 : addr string_to_addr(
131 : std::string const & a
132 : , std::string const & default_address = std::string()
133 : , int default_port = -1
134 : , std::string const & protocol = std::string()
135 : , bool mask = false);
136 :
137 :
138 :
139 : }
140 : // namespace addr
141 : // vim: ts=4 sw=4 et
|