1 /*
2  * include/haproxy/tools-t.h
3  * This files contains some general purpose macros and structures.
4  *
5  * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _HAPROXY_TOOLS_T_H
23 #define _HAPROXY_TOOLS_T_H
24 
25 /* size used for max length of decimal representation of long long int. */
26 #define NB_LLMAX_STR (sizeof("-9223372036854775807")-1)
27 
28 /* number of itoa_str entries */
29 #define NB_ITOA_STR	16
30 
31 /* maximum quoted string length (truncated above) */
32 #define QSTR_SIZE 200
33 #define NB_QSTR 10
34 
35 /* returns 1 only if only zero or one bit is set in X, which means that X is a
36  * power of 2, and 0 otherwise */
37 #define POWEROF2(x) (((x) & ((x)-1)) == 0)
38 
39 /* return an integer of type <ret> with only the highest bit set. <ret> may be
40  * both a variable or a type.
41  */
42 #define MID_RANGE(ret) ((typeof(ret))1 << (8*sizeof(ret) - 1))
43 
44 /* return the largest possible integer of type <ret>, with all bits set */
45 #define MAX_RANGE(ret) (~(typeof(ret))0)
46 
47 /* DEFNULL() returns either the argument as-is, or NULL if absent. This is for
48  * use in macros arguments.
49  */
50 #define DEFNULL(...) _FIRST_ARG(NULL, ##__VA_ARGS__, NULL)
51 #define _FIRST_ARG(a, b, ...) b
52 
53 /* options flags for parse_line() */
54 #define PARSE_OPT_SHARP         0x00000001      // '#' ends the line
55 #define PARSE_OPT_BKSLASH       0x00000002      // '\' escapes chars
56 #define PARSE_OPT_SQUOTE        0x00000004      // "'" encloses a string
57 #define PARSE_OPT_DQUOTE        0x00000008      // '"' encloses a string
58 #define PARSE_OPT_ENV           0x00000010      // '$' is followed by environment variables
59 #define PARSE_OPT_INPLACE       0x00000020      // parse and tokenize in-place (src == dst)
60 #define PARSE_OPT_WORD_EXPAND   0x00000040      // '[*]' suffix to expand an environment variable as several individual arguments
61 
62 /* return error flags from parse_line() */
63 #define PARSE_ERR_TOOLARGE      0x00000001      // result is too large for initial outlen
64 #define PARSE_ERR_TOOMANY       0x00000002      // more words than initial nbargs
65 #define PARSE_ERR_QUOTE         0x00000004      // unmatched quote (offending one at errptr)
66 #define PARSE_ERR_BRACE         0x00000008      // unmatched brace (offending one at errptr)
67 #define PARSE_ERR_HEX           0x00000010      // unparsable hex sequence (at errptr)
68 #define PARSE_ERR_VARNAME       0x00000020      // invalid variable name (at errptr)
69 #define PARSE_ERR_OVERLAP       0x00000040      // output overlaps with input, need to allocate
70 #define PARSE_ERR_WRONG_EXPAND  0x00000080      // unparsable word expansion sequence
71 
72 /* special return values for the time parser (parse_time_err()) */
73 #define PARSE_TIME_UNDER ((char *)1)
74 #define PARSE_TIME_OVER  ((char *)2)
75 
76 /* unit flags to pass to parse_time_err() */
77 #define TIME_UNIT_US   0x0000
78 #define TIME_UNIT_MS   0x0001
79 #define TIME_UNIT_S    0x0002
80 #define TIME_UNIT_MIN  0x0003
81 #define TIME_UNIT_HOUR 0x0004
82 #define TIME_UNIT_DAY  0x0005
83 #define TIME_UNIT_MASK 0x0007
84 
85 #define SEC 1
86 #define MINUTE (60 * SEC)
87 #define HOUR (60 * MINUTE)
88 #define DAY (24 * HOUR)
89 
90 /* Address parsing options for use with str2sa_range() */
91 #define PA_O_RESOLVE            0x00000001   /* do resolve the FQDN to an IP address */
92 #define PA_O_PORT_OK            0x00000002   /* ports are supported */
93 #define PA_O_PORT_MAND          0x00000004   /* ports are mandatory */
94 #define PA_O_PORT_RANGE         0x00000008   /* port ranges are supported */
95 #define PA_O_PORT_OFS           0x00000010   /* port offsets are supported */
96 #define PA_O_SOCKET_FD          0x00000020   /* inherited socket FDs are supported */
97 #define PA_O_RAW_FD             0x00000040   /* inherited raw FDs are supported (pipes, ttys, ...) */
98 #define PA_O_DGRAM              0x00000080   /* the address will be used for a datagram socket (in or out) */
99 #define PA_O_STREAM             0x00000100   /* the address will be used for streams (in or out) */
100 #define PA_O_XPRT               0x00000200   /* transport protocols may be specified */
101 #define PA_O_CONNECT            0x00000400   /* the protocol must have a ->connect method */
102 
103 /* UTF-8 decoder status */
104 #define UTF8_CODE_OK       0x00
105 #define UTF8_CODE_OVERLONG 0x10
106 #define UTF8_CODE_INVRANGE 0x20
107 #define UTF8_CODE_BADSEQ   0x40
108 
109 /* HAP_STRING() makes a string from a literal while HAP_XSTRING() first
110  * evaluates the argument and is suited to pass macros.
111  *
112  * They allow macros like PCRE_MAJOR to be defined without quotes, which
113  * is convenient for applications that want to test its value.
114  */
115 #define HAP_STRING(...) #__VA_ARGS__
116 #define HAP_XSTRING(...) HAP_STRING(__VA_ARGS__)
117 
118 /* operators to compare values. They're ordered that way so that the lowest bit
119  * serves as a negation for the test and contains all tests that are not equal.
120  */
121 enum {
122 	STD_OP_LE = 0, STD_OP_GT = 1,
123 	STD_OP_EQ = 2, STD_OP_NE = 3,
124 	STD_OP_GE = 4, STD_OP_LT = 5,
125 };
126 
127 enum http_scheme {
128 	SCH_HTTP,
129 	SCH_HTTPS,
130 };
131 
132 /* output format used by url2sa() */
133 struct split_url {
134 	enum http_scheme scheme;
135 	const char *host;
136 	int host_len;
137 };
138 
139 /* generic structure associating a name and a value, for use in arrays */
140 struct name_desc {
141 	const char *name;
142 	const char *desc;
143 };
144 
145 #endif /* _HAPROXY_TOOLS_T_H */
146