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