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 263896 : class addr_parser
43 : {
44 : public:
45 : enum class flag_t
46 : {
47 : ADDRESS, // address (IP)
48 : REQUIRED_ADDRESS, // address cannot be empty
49 : PORT, // port
50 : REQUIRED_PORT, // port cannot be empty
51 : MASK, // mask
52 : MULTI_ADDRESSES_COMMAS, // IP:port/mask,IP:port/mask,...
53 : MULTI_ADDRESSES_SPACES, // IP:port/mask IP:port/mask ...
54 : MULTI_ADDRESSES_COMMAS_AND_SPACES, // IP:port/mask, IP:port/mask, ...
55 :
56 : // the following are not yet implemented
57 : MULTI_PORTS_SEMICOLONS, // port1;port2;...
58 : MULTI_PORTS_COMMAS, // port1,port2,...
59 : PORT_RANGE, // port1-port2
60 : ADDRESS_RANGE, // IP-IP
61 :
62 : FLAG_max
63 : };
64 :
65 : void set_default_address(std::string const & address);
66 : std::string const & get_default_address4() const;
67 : std::string const & get_default_address6() const;
68 :
69 : void set_default_port(int const port);
70 : int get_default_port() const;
71 :
72 : void set_default_mask(std::string const & mask);
73 : std::string const & get_default_mask4() const;
74 : std::string const & get_default_mask6() const;
75 :
76 : void set_protocol(std::string const & protocol);
77 : void set_protocol(int const protocol);
78 : void clear_protocol();
79 : int get_protocol() const;
80 :
81 : void set_allow(flag_t const flag, bool const allow);
82 : bool get_allow(flag_t const flag) const;
83 :
84 : bool has_errors() const;
85 : void emit_error(std::string const & msg);
86 : std::string const & error_messages() const;
87 : int error_count() const;
88 : void clear_errors();
89 :
90 : addr_range::vector_t parse(std::string const & in);
91 :
92 : private:
93 : void parse_cidr(std::string const & in, addr_range::vector_t & result);
94 : void parse_address(std::string const & in, std::string const & mask, addr_range::vector_t & result);
95 : void parse_address4(std::string const & in, addr_range::vector_t & result);
96 : void parse_address6(std::string const & in, addr_range::vector_t & result);
97 : void parse_address_port(std::string address, std::string port_str, addr_range::vector_t & result, bool ipv6);
98 : void parse_mask(std::string const & mask, addr & cidr);
99 :
100 : bool f_flags[static_cast<int>(flag_t::FLAG_max)] = { true, false, true, false, false, false, false, false, false, false, false, false };
101 : std::string f_default_address4 = std::string();
102 : std::string f_default_address6 = std::string();
103 : std::string f_default_mask4 = std::string();
104 : std::string f_default_mask6 = std::string();
105 : int f_protocol = -1;
106 : int f_default_port = -1;
107 : std::string f_error = std::string();
108 : int f_error_count = 0;
109 : };
110 :
111 : addr string_to_addr(
112 : std::string const & a
113 : , std::string const & default_address = std::string()
114 : , int default_port = -1
115 : , std::string const & protocol = std::string()
116 : , bool mask = false);
117 :
118 :
119 :
120 : }
121 : // namespace addr
122 : // vim: ts=4 sw=4 et
|