Line data Source code
1 : // Network Address -- classes functions to ease handling IP addresses
2 : // Copyright (c) 2012-2018 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // https://snapwebsites.org/project/libaddr
5 : //
6 : // Permission is hereby granted, free of charge, to any person obtaining a
7 : // copy of this software and associated documentation files (the
8 : // "Software"), to deal in the Software without restriction, including
9 : // without limitation the rights to use, copy, modify, merge, publish,
10 : // distribute, sublicense, and/or sell copies of the Software, and to
11 : // permit persons to whom the Software is furnished to do so, subject to
12 : // the following conditions:
13 : //
14 : // The above copyright notice and this permission notice shall be included
15 : // in all copies or substantial portions of the Software.
16 : //
17 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 : // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 : // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 : // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 : // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 : // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 : // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 : //
25 : #pragma once
26 :
27 : /** \file
28 : * \brief The declaration of the parser of the libaddr classes.
29 : *
30 : * This header includes the addr_parser class used to parse user input
31 : * string and convert them to binary IP addresses.
32 : */
33 :
34 : // self
35 : //
36 : #include "libaddr/addr_range.h"
37 :
38 :
39 :
40 : namespace addr
41 : {
42 :
43 :
44 263894 : class addr_parser
45 : {
46 : public:
47 : enum class flag_t
48 : {
49 : ADDRESS, // address (IP)
50 : REQUIRED_ADDRESS, // address cannot be empty
51 : PORT, // port
52 : REQUIRED_PORT, // port cannot be empty
53 : MASK, // mask
54 : MULTI_ADDRESSES_COMMAS, // IP:port/mask,IP:port/mask,...
55 : MULTI_ADDRESSES_SPACES, // IP:port/mask IP:port/mask ...
56 : MULTI_ADDRESSES_COMMAS_AND_SPACES, // IP:port/mask, IP:port/mask, ...
57 :
58 : // 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 : void set_default_address(std::string const & address);
68 : std::string const & get_default_address4() const;
69 : std::string const & get_default_address6() const;
70 :
71 : void set_default_port(int const port);
72 : int get_default_port() const;
73 :
74 : void set_default_mask(std::string const & mask);
75 : std::string const & get_default_mask4() const;
76 : std::string const & get_default_mask6() const;
77 :
78 : void set_protocol(std::string const & protocol);
79 : void set_protocol(int const protocol);
80 : void clear_protocol();
81 : int get_protocol() const;
82 :
83 : void set_allow(flag_t const flag, bool const allow);
84 : bool get_allow(flag_t const flag) const;
85 :
86 : bool has_errors() const;
87 : void emit_error(std::string const & msg);
88 : std::string const & error_messages() const;
89 : int error_count() const;
90 : void clear_errors();
91 :
92 : addr_range::vector_t parse(std::string const & in);
93 :
94 : private:
95 : void parse_cidr(std::string const & in, addr_range::vector_t & result);
96 : void parse_address(std::string const & in, std::string const & mask, addr_range::vector_t & result);
97 : void parse_address4(std::string const & in, addr_range::vector_t & result);
98 : void parse_address6(std::string const & in, addr_range::vector_t & result);
99 : void parse_address_port(std::string address, std::string const & port_str, addr_range::vector_t & result, std::string const & default_address);
100 : void parse_mask(std::string const & mask, addr & cidr);
101 :
102 : bool f_flags[static_cast<int>(flag_t::FLAG_max)] = { true, false, true, false, false, false, false, false, false, false, false, false };
103 : std::string f_default_address4;
104 : std::string f_default_address6;
105 : std::string f_default_mask4;
106 : std::string f_default_mask6;
107 : int f_protocol = -1;
108 : int f_default_port = -1;
109 : std::string f_error;
110 : int f_error_count = 0;
111 : };
112 :
113 : addr string_to_addr(
114 : std::string const & a
115 : , std::string const & default_address = std::string()
116 : , int default_port = -1
117 : , std::string const & protocol = std::string()
118 : , bool mask = false);
119 :
120 :
121 :
122 : }
123 : // addr namespace
124 : // vim: ts=4 sw=4 et
|