1 //--------------------------------------------------------------------------
2 // Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 // parameter.h author Russ Combs <rucombs@cisco.com>
19 
20 #ifndef PARAMETER_H
21 #define PARAMETER_H
22 
23 // Parameter provides basic parsing from Lua types into meaningful C++
24 // types.  Modules support a list of parameters.
25 //
26 // number ranges are given by:
27 // nullptr -> any
28 // # | #: | :# | #:#
29 // where # is any valid pos|neg dec|hex|octal number
30 
31 #include <functional>
32 #include "main/snort_types.h"
33 
34 namespace snort
35 {
36 class Value;
37 
38 struct SO_PUBLIC Parameter
39 {
40     using RangeQuery = std::function<const char*()>;
41 
42     enum Type
43     {
44         PT_TABLE,      // range is Parameter*, no default
45         PT_LIST,       // range is Parameter*, no default
46         PT_DYNAMIC,    // range is RangeQuery*
47         PT_BOOL,       // if you are reading this, get more coffee
48         PT_INT,        // signed 53 bits or less determined by range
49         PT_INTERVAL,   // string that defines an interval, bounds within range
50         PT_REAL,       // double
51         PT_PORT,       // 0 to 64K-1 unless specified otherwise
52         PT_STRING,     // any string less than len chars
53                        // range = "(optional)" if not required (eg on cmd line)
54         PT_SELECT,     // any string appearing in range
55         PT_MULTI,      // one or more strings appearing in range
56         PT_ENUM,       // string converted to unsigned by range sequence
57         PT_MAC,        // 6-byte mac address
58         PT_IP4,        // inet_addr() compatible
59         PT_ADDR,       // ip4 or ip6 CIDR
60         PT_BIT_LIST,   // string that converts to bitset
61         PT_ADDR_LIST,  // snort address list format?
62         PT_IMPLIED,    // rule option args w/o values eg relative
63         PT_MAX
64     };
65     const char* name;
66     Type type;
67     const void* range;  // nullptr|const char*|RangeQuery*|const Parameter*
68     const char* deflt;
69     const char* help;
70 
ParameterParameter71     Parameter(const char* n, Type t, const void* r, const char* d, const char* h) :
72         name(n), type(t), range(r), deflt(d), help(h) { }
73 
74     const char* get_type() const;
75     const char* get_range() const;
76 
77     bool validate(Value&) const;
78 
is_positionalParameter79     bool is_positional() const
80     { return ( name && *name == '~' ); }
81 
is_wild_cardParameter82     bool is_wild_card() const
83     { return ( name && *name == '*' ); }
84 
is_tableParameter85     bool is_table() const
86     { return ( type == PT_TABLE || type == PT_LIST ); }
87 
is_quotedParameter88     bool is_quoted() const
89     { return ( type > PT_PORT ); }
90 
91     bool get_bool() const;
92     double get_number() const;
93     const char* get_string() const;
94 
95     static const Parameter* find(const Parameter*, const char*);
96 
97     // 0-based; -1 if not found; list is | delimited
98     static int index(const char* list, const char* key);
99 
100     // convert string to long (including 'maxN' literals)
101     static int64_t get_int(const char*);
102 };
103 }
104 #endif
105 
106