blob: 3990e6ffab2ad014d39de65bd67b0644e659dddc [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreaucc05fba2009-10-27 21:40:18 +01002 * include/common/standard.h
3 * This files contains some general purpose functions and macros.
4 *
Willy Tarreau348238b2010-01-18 15:05:57 +01005 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
Willy Tarreaucc05fba2009-10-27 21:40:18 +01006 *
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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_STANDARD_H
23#define _COMMON_STANDARD_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau167d8b52007-04-09 22:16:12 +020025#include <limits.h>
Willy Tarreau050737f2010-01-14 11:40:12 +010026#include <string.h>
Willy Tarreaufe944602007-10-25 10:34:16 +020027#include <time.h>
Willy Tarreau938b3032007-05-10 06:39:03 +020028#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <netinet/in.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020030#include <common/config.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010031#include <eb32tree.h>
Willy Tarreauca28d1e2007-01-24 18:20:50 +010032#include <proto/fd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34/****** string-specific macros and functions ******/
35/* if a > max, then bound <a> to <max>. The macro returns the new <a> */
36#define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); })
37
38/* if a < min, then bound <a> to <min>. The macro returns the new <a> */
39#define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); })
40
41/* returns 1 only if only zero or one bit is set in X, which means that X is a
42 * power of 2, and 0 otherwise */
43#define POWEROF2(x) (((x) & ((x)-1)) == 0)
44
Willy Tarreau5b180202010-07-18 10:40:48 +020045/* operators to compare values. They're ordered that way so that the lowest bit
46 * serves as a negation for the test and contains all tests that are not equal.
47 */
48enum {
49 STD_OP_LE = 0, STD_OP_GT = 1,
50 STD_OP_EQ = 2, STD_OP_NE = 3,
51 STD_OP_GE = 4, STD_OP_LT = 5,
52};
53
Willy Tarreau7d58a632007-01-13 23:06:06 +010054/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 * copies at most <size-1> chars from <src> to <dst>. Last char is always
56 * set to 0, unless <size> is 0. The number of chars copied is returned
57 * (excluding the terminating zero).
58 * This code has been optimized for size and speed : on x86, it's 45 bytes
59 * long, uses only registers, and consumes only 4 cycles per char.
60 */
61extern int strlcpy2(char *dst, const char *src, int size);
62
63/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020064 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 * the ascii representation for number 'n' in decimal.
66 */
Willy Tarreaue7239b52009-03-29 13:41:58 +020067extern char itoa_str[][171];
Emeric Brun3a7fce52010-01-04 14:54:38 +010068extern char *ultoa_r(unsigned long n, char *buffer, int size);
Willy Tarreaue7239b52009-03-29 13:41:58 +020069extern const char *ulltoh_r(unsigned long long n, char *buffer, int size);
Willy Tarreau72d759c2007-10-25 12:14:10 +020070static inline const char *ultoa(unsigned long n)
71{
72 return ultoa_r(n, itoa_str[0], sizeof(itoa_str[0]));
73}
74
75/* Fast macros to convert up to 10 different parameters inside a same call of
76 * expression.
77 */
78#define U2A0(n) ({ ultoa_r((n), itoa_str[0], sizeof(itoa_str[0])); })
79#define U2A1(n) ({ ultoa_r((n), itoa_str[1], sizeof(itoa_str[1])); })
80#define U2A2(n) ({ ultoa_r((n), itoa_str[2], sizeof(itoa_str[2])); })
81#define U2A3(n) ({ ultoa_r((n), itoa_str[3], sizeof(itoa_str[3])); })
82#define U2A4(n) ({ ultoa_r((n), itoa_str[4], sizeof(itoa_str[4])); })
83#define U2A5(n) ({ ultoa_r((n), itoa_str[5], sizeof(itoa_str[5])); })
84#define U2A6(n) ({ ultoa_r((n), itoa_str[6], sizeof(itoa_str[6])); })
85#define U2A7(n) ({ ultoa_r((n), itoa_str[7], sizeof(itoa_str[7])); })
86#define U2A8(n) ({ ultoa_r((n), itoa_str[8], sizeof(itoa_str[8])); })
87#define U2A9(n) ({ ultoa_r((n), itoa_str[9], sizeof(itoa_str[9])); })
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
Willy Tarreaue7239b52009-03-29 13:41:58 +020089/* The same macros provide HTML encoding of numbers */
90#define U2H0(n) ({ ulltoh_r((n), itoa_str[0], sizeof(itoa_str[0])); })
91#define U2H1(n) ({ ulltoh_r((n), itoa_str[1], sizeof(itoa_str[1])); })
92#define U2H2(n) ({ ulltoh_r((n), itoa_str[2], sizeof(itoa_str[2])); })
93#define U2H3(n) ({ ulltoh_r((n), itoa_str[3], sizeof(itoa_str[3])); })
94#define U2H4(n) ({ ulltoh_r((n), itoa_str[4], sizeof(itoa_str[4])); })
95#define U2H5(n) ({ ulltoh_r((n), itoa_str[5], sizeof(itoa_str[5])); })
96#define U2H6(n) ({ ulltoh_r((n), itoa_str[6], sizeof(itoa_str[6])); })
97#define U2H7(n) ({ ulltoh_r((n), itoa_str[7], sizeof(itoa_str[7])); })
98#define U2H8(n) ({ ulltoh_r((n), itoa_str[8], sizeof(itoa_str[8])); })
99#define U2H9(n) ({ ulltoh_r((n), itoa_str[9], sizeof(itoa_str[9])); })
100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200102 * This function simply returns a locally allocated string containing the ascii
103 * representation for number 'n' in decimal, unless n is 0 in which case it
104 * returns the alternate string (or an empty string if the alternate string is
105 * NULL). It use is intended for limits reported in reports, where it's
106 * desirable not to display anything if there is no limit. Warning! it shares
107 * the same vector as ultoa_r().
108 */
109extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt);
110
111/* Fast macros to convert up to 10 different parameters inside a same call of
112 * expression. Warning! they share the same vectors as U2A*!
113 */
114#define LIM2A0(n, alt) ({ limit_r((n), itoa_str[0], sizeof(itoa_str[0]), (alt)); })
115#define LIM2A1(n, alt) ({ limit_r((n), itoa_str[1], sizeof(itoa_str[1]), (alt)); })
116#define LIM2A2(n, alt) ({ limit_r((n), itoa_str[2], sizeof(itoa_str[2]), (alt)); })
117#define LIM2A3(n, alt) ({ limit_r((n), itoa_str[3], sizeof(itoa_str[3]), (alt)); })
118#define LIM2A4(n, alt) ({ limit_r((n), itoa_str[4], sizeof(itoa_str[4]), (alt)); })
119#define LIM2A5(n, alt) ({ limit_r((n), itoa_str[5], sizeof(itoa_str[5]), (alt)); })
120#define LIM2A6(n, alt) ({ limit_r((n), itoa_str[6], sizeof(itoa_str[6]), (alt)); })
121#define LIM2A7(n, alt) ({ limit_r((n), itoa_str[7], sizeof(itoa_str[7]), (alt)); })
122#define LIM2A8(n, alt) ({ limit_r((n), itoa_str[8], sizeof(itoa_str[8]), (alt)); })
123#define LIM2A9(n, alt) ({ limit_r((n), itoa_str[9], sizeof(itoa_str[9]), (alt)); })
124
125/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
127 */
128extern int ishex(char s);
129
130/*
Willy Tarreauda3b7c32009-11-02 20:12:52 +0100131 * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
132 * otherwise -1.
133 */
134extern int hex2i(int c);
135
136/*
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100137 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
138 * invalid character is found, a pointer to it is returned. If everything is
139 * fine, NULL is returned.
140 */
141extern const char *invalid_char(const char *name);
142
143/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200144 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
145 * If an invalid character is found, a pointer to it is returned.
146 * If everything is fine, NULL is returned.
147 */
148extern const char *invalid_domainchar(const char *name);
149
150/*
Robert Tsai81ae1952007-12-05 10:47:29 +0100151 * converts <str> to a struct sockaddr_un* which is locally allocated.
152 * The format is "/path", where "/path" is a path to a UNIX domain socket.
153 */
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100154struct sockaddr_un *str2sun(const char *str);
Robert Tsai81ae1952007-12-05 10:47:29 +0100155
156/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157 * converts <str> to a struct sockaddr_in* which is locally allocated.
158 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
159 * a host name, or empty or "*" to indicate INADDR_ANY.
160 */
161struct sockaddr_in *str2sa(char *str);
162
163/*
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200164 * converts <str> to a struct sockaddr_in* which is locally allocated, and a
165 * port range consisting in two integers. The low and high end are always set
166 * even if the port is unspecified, in which case (0,0) is returned. The low
167 * port is set in the sockaddr_in. Thus, it is enough to check the size of the
168 * returned range to know if an array must be allocated or not. The format is
169 * "addr[:port[-port]]", where "addr" can be a dotted IPv4 address, a host
170 * name, or empty or "*" to indicate INADDR_ANY.
171 */
172struct sockaddr_in *str2sa_range(char *str, int *low, int *high);
173
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100174/* converts <str> to a struct in_addr containing a network mask. It can be
175 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
176 * if the conversion succeeds otherwise non-zero.
177 */
178int str2mask(const char *str, struct in_addr *mask);
179
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200180/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200181 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
183 * is optionnal and either in the dotted or CIDR notation.
184 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
185 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200186int str2net(const char *str, struct in_addr *addr, struct in_addr *mask);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100188/*
Willy Tarreau106f9792009-09-19 07:54:16 +0200189 * Parse IP address found in url.
190 */
191int url2ip(const char *addr, struct in_addr *dst);
192
193/*
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100194 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
195 */
196int url2sa(const char *url, int ulen, struct sockaddr_in *addr);
197
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198/* will try to encode the string <string> replacing all characters tagged in
199 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
200 * prefixed by <escape>, and will store the result between <start> (included)
201 * and <stop> (excluded), and will always terminate the string with a '\0'
202 * before <stop>. The position of the '\0' is returned if the conversion
203 * completes. If bytes are missing between <start> and <stop>, then the
204 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
205 * cannot even be stored so we return <start> without writing the 0.
206 * The input string must also be zero-terminated.
207 */
208extern const char hextab[];
209char *encode_string(char *start, char *stop,
210 const char escape, const fd_set *map,
211 const char *string);
212
Willy Tarreau6911fa42007-03-04 18:06:08 +0100213/* This one is 6 times faster than strtoul() on athlon, but does
214 * no check at all.
215 */
216static inline unsigned int __str2ui(const char *s)
217{
218 unsigned int i = 0;
219 while (*s) {
220 i = i * 10 - '0';
221 i += (unsigned char)*s++;
222 }
223 return i;
224}
225
226/* This one is 5 times faster than strtoul() on athlon with checks.
227 * It returns the value of the number composed of all valid digits read.
228 */
229static inline unsigned int __str2uic(const char *s)
230{
231 unsigned int i = 0;
232 unsigned int j;
233 while (1) {
234 j = (*s++) - '0';
235 if (j > 9)
236 break;
237 i *= 10;
238 i += j;
239 }
240 return i;
241}
242
243/* This one is 28 times faster than strtoul() on athlon, but does
244 * no check at all!
245 */
246static inline unsigned int __strl2ui(const char *s, int len)
247{
248 unsigned int i = 0;
249 while (len-- > 0) {
250 i = i * 10 - '0';
251 i += (unsigned char)*s++;
252 }
253 return i;
254}
255
256/* This one is 7 times faster than strtoul() on athlon with checks.
257 * It returns the value of the number composed of all valid digits read.
258 */
259static inline unsigned int __strl2uic(const char *s, int len)
260{
261 unsigned int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200262 unsigned int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100263
264 while (len-- > 0) {
265 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200266 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100267 if (j > 9)
268 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200269 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100270 }
271 return i;
272}
273
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200274/* This function reads an unsigned integer from the string pointed to by <s>
275 * and returns it. The <s> pointer is adjusted to point to the first unread
276 * char. The function automatically stops at <end>.
277 */
278static inline unsigned int __read_uint(const char **s, const char *end)
279{
280 const char *ptr = *s;
281 unsigned int i = 0;
282 unsigned int j, k;
283
284 while (ptr < end) {
285 j = *ptr - '0';
286 k = i * 10;
287 if (j > 9)
288 break;
289 i = k + j;
290 ptr++;
291 }
292 *s = ptr;
293 return i;
294}
295
Willy Tarreau6911fa42007-03-04 18:06:08 +0100296extern unsigned int str2ui(const char *s);
297extern unsigned int str2uic(const char *s);
298extern unsigned int strl2ui(const char *s, int len);
299extern unsigned int strl2uic(const char *s, int len);
300extern int strl2ic(const char *s, int len);
301extern int strl2irc(const char *s, int len, int *ret);
302extern int strl2llrc(const char *s, int len, long long *ret);
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200303extern unsigned int read_uint(const char **s, const char *end);
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200304unsigned int inetaddr_host(const char *text);
305unsigned int inetaddr_host_lim(const char *text, const char *stop);
Willy Tarreau74172752010-10-15 23:21:42 +0200306unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret);
Willy Tarreau6911fa42007-03-04 18:06:08 +0100307
Krzysztof Piotr Oledzki3d5562b2009-10-10 20:11:17 +0200308static inline char *cut_crlf(char *s) {
309
310 while (*s != '\r' || *s == '\n') {
311 char *p = s++;
312
313 if (!*p)
314 return p;
315 }
316
317 *s++ = 0;
318
319 return s;
320}
321
322static inline char *ltrim(char *s, char c) {
323
324 if (c)
325 while (*s == c)
326 s++;
327
328 return s;
329}
330
331static inline char *rtrim(char *s, char c) {
332
333 char *p = s + strlen(s);
334
335 while (p-- > s)
336 if (*p == c)
337 *p = '\0';
338 else
339 break;
340
341 return s;
342}
343
344static inline char *alltrim(char *s, char c) {
345
346 rtrim(s, c);
347
348 return ltrim(s, c);
349}
350
Willy Tarreaufe944602007-10-25 10:34:16 +0200351/* This function converts the time_t value <now> into a broken out struct tm
352 * which must be allocated by the caller. It is highly recommended to use this
353 * function intead of localtime() because that one requires a time_t* which
354 * is not always compatible with tv_sec depending on OS/hardware combinations.
355 */
356static inline void get_localtime(const time_t now, struct tm *tm)
357{
358 localtime_r(&now, tm);
359}
360
Emeric Brun3a058f32009-06-30 18:26:00 +0200361/* This function converts the time_t value <now> into a broken out struct tm
362 * which must be allocated by the caller. It is highly recommended to use this
363 * function intead of gmtime() because that one requires a time_t* which
364 * is not always compatible with tv_sec depending on OS/hardware combinations.
365 */
366static inline void get_gmtime(const time_t now, struct tm *tm)
367{
368 gmtime_r(&now, tm);
369}
370
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100371/* This function parses a time value optionally followed by a unit suffix among
372 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
373 * expected by the caller. The computation does its best to avoid overflows.
374 * The value is returned in <ret> if everything is fine, and a NULL is returned
375 * by the function. In case of error, a pointer to the error is returned and
376 * <ret> is left untouched.
377 */
378extern const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags);
Emeric Brun39132b22010-01-04 14:57:24 +0100379extern const char *parse_size_err(const char *text, unsigned *ret);
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100380
381/* unit flags to pass to parse_time_err */
382#define TIME_UNIT_US 0x0000
383#define TIME_UNIT_MS 0x0001
384#define TIME_UNIT_S 0x0002
385#define TIME_UNIT_MIN 0x0003
386#define TIME_UNIT_HOUR 0x0004
387#define TIME_UNIT_DAY 0x0005
388#define TIME_UNIT_MASK 0x0007
389
Willy Tarreau7f062c42009-03-05 18:43:00 +0100390/* Multiply the two 32-bit operands and shift the 64-bit result right 32 bits.
391 * This is used to compute fixed ratios by setting one of the operands to
392 * (2^32*ratio).
393 */
394static inline unsigned int mul32hi(unsigned int a, unsigned int b)
395{
396 return ((unsigned long long)a * b) >> 32;
397}
398
Willy Tarreauf0d9eec2010-06-20 07:12:37 +0200399/* gcc does not know when it can safely divide 64 bits by 32 bits. Use this
400 * function when you know for sure that the result fits in 32 bits, because
401 * it is optimal on x86 and on 64bit processors.
402 */
403static inline unsigned int div64_32(unsigned long long o1, unsigned int o2)
404{
405 unsigned int result;
406#ifdef __i386__
407 asm("divl %2"
408 : "=a" (result)
409 : "A"(o1), "rm"(o2));
410#else
411 result = o1 / o2;
412#endif
413 return result;
414}
415
Willy Tarreau946ba592009-05-10 15:41:18 +0200416/* copies at most <n> characters from <src> and always terminates with '\0' */
417char *my_strndup(const char *src, int n);
418
Willy Tarreau482b00d2009-10-04 22:48:42 +0200419/* This function returns the first unused key greater than or equal to <key> in
420 * ID tree <root>. Zero is returned if no place is found.
421 */
422unsigned int get_next_id(struct eb_root *root, unsigned int key);
423
Willy Tarreau348238b2010-01-18 15:05:57 +0100424/* This function compares a sample word possibly followed by blanks to another
425 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
426 * otherwise zero. This intends to be used when checking HTTP headers for some
427 * values.
428 */
429int word_match(const char *sample, int slen, const char *word, int wlen);
430
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200431/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
432 * or the number of chars read in case of success.
433 */
434int buf2ip(const char *buf, size_t len, struct in_addr *dst);
435
Willy Tarreauacf95772010-06-14 19:09:21 +0200436/* To be used to quote config arg positions. Returns the string at <ptr>
437 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
438 * if ptr is NULL or empty. The string is locally allocated.
439 */
440const char *quote_arg(const char *ptr);
441
Willy Tarreau5b180202010-07-18 10:40:48 +0200442/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
443int get_std_op(const char *str);
444
Willy Tarreau4c14eaa2010-11-24 14:01:45 +0100445/* hash a 32-bit integer to another 32-bit integer */
446extern unsigned int full_hash(unsigned int a);
447static inline unsigned int __full_hash(unsigned int a)
448{
449 /* This function is one of Bob Jenkins' full avalanche hashing
450 * functions, which when provides quite a good distribution for little
451 * input variations. The result is quite suited to fit over a 32-bit
452 * space with enough variations so that a randomly picked number falls
453 * equally before any server position.
454 * Check http://burtleburtle.net/bob/hash/integer.html for more info.
455 */
456 a = (a+0x7ed55d16) + (a<<12);
457 a = (a^0xc761c23c) ^ (a>>19);
458 a = (a+0x165667b1) + (a<<5);
459 a = (a+0xd3a2646c) ^ (a<<9);
460 a = (a+0xfd7046c5) + (a<<3);
461 a = (a^0xb55a4f09) ^ (a>>16);
462
463 /* ensure values are better spread all around the tree by multiplying
464 * by a large prime close to 3/4 of the tree.
465 */
466 return a * 3221225473U;
467}
468
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200469#endif /* _COMMON_STANDARD_H */