Line data Source code
1 : // Network Address -- classes functions to ease handling IP addresses
2 : // Copyright (C) 2012-2017 Made to Order Software Corp.
3 : //
4 : // http://snapwebsites.org/project/libaddr
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : #pragma once
20 :
21 : /** \file
22 : * \brief The declaration of the parser of the libaddr classes.
23 : *
24 : * This header includes the addr_parser class used to parse user input
25 : * string and convert them to binary IP addresses.
26 : */
27 :
28 : // self
29 : //
30 : #include "libaddr/addr_range.h"
31 :
32 :
33 :
34 : namespace addr
35 : {
36 :
37 :
38 263868 : class addr_parser
39 : {
40 : public:
41 : enum class flag_t
42 : {
43 : ADDRESS, // address (IP)
44 : REQUIRED_ADDRESS, // address cannot be empty
45 : PORT, // port
46 : REQUIRED_PORT, // port cannot be empty
47 : MASK, // mask
48 : MULTI_ADDRESSES_COMMAS, // IP:port/mask,IP:port/mask,...
49 : MULTI_ADDRESSES_SPACES, // IP:port/mask IP:port/mask ...
50 : MULTI_ADDRESSES_COMMAS_AND_SPACES, // IP:port/mask, IP:port/mask, ...
51 :
52 : // the following are not yet implemented
53 : MULTI_PORTS_SEMICOLONS, // port1;port2;...
54 : MULTI_PORTS_COMMAS, // port1,port2,...
55 : PORT_RANGE, // port1-port2
56 : ADDRESS_RANGE, // IP-IP
57 :
58 : FLAG_max
59 : };
60 :
61 : void set_default_address(std::string const & addr);
62 : std::string const & get_default_address4() const;
63 : std::string const & get_default_address6() const;
64 : void set_default_port(int const port);
65 : int get_default_port() const;
66 : void set_default_mask(std::string const & mask);
67 : std::string const & get_default_mask4() const;
68 : std::string const & get_default_mask6() const;
69 : void set_protocol(std::string const & protocol);
70 : void set_protocol(int const protocol);
71 : void clear_protocol();
72 : int get_protocol() const;
73 :
74 : void set_allow(flag_t const flag, bool const allow);
75 : bool get_allow(flag_t const flag) const;
76 :
77 : bool has_errors() const;
78 : void emit_error(std::string const & msg);
79 : std::string const & error_messages() const;
80 : int error_count() const;
81 : void clear_errors();
82 : addr_range::vector_t parse(std::string const & in);
83 :
84 : private:
85 : void parse_cidr(std::string const & in, addr_range::vector_t & result);
86 : void parse_address(std::string const & in, std::string const & mask, addr_range::vector_t & result);
87 : void parse_address4(std::string const & in, addr_range::vector_t & result);
88 : void parse_address6(std::string const & in, addr_range::vector_t & result);
89 : void parse_address_port(std::string address, std::string const & port_str, addr_range::vector_t & result, std::string const & default_address);
90 : void parse_mask(std::string const & mask, addr & cidr);
91 :
92 : bool f_flags[static_cast<int>(flag_t::FLAG_max)] = { true, false, true, false, false, false, false, false, false, false, false, false };
93 : std::string f_default_address4;
94 : std::string f_default_address6;
95 : std::string f_default_mask4;
96 : std::string f_default_mask6;
97 : int f_protocol = -1;
98 : int f_default_port = -1;
99 : std::string f_error;
100 : int f_error_count = 0;
101 : };
102 :
103 :
104 :
105 : }
106 : // addr namespace
107 : // vim: ts=4 sw=4 et
|