2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2009-10-27 20:40:18 +00:00
|
|
|
* include/common/standard.h
|
|
|
|
* This files contains some general purpose functions and macros.
|
|
|
|
*
|
2010-01-18 14:05:57 +00:00
|
|
|
* Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
|
2009-10-27 20:40:18 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_STANDARD_H
|
|
|
|
#define _COMMON_STANDARD_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-04-09 20:16:12 +00:00
|
|
|
#include <limits.h>
|
2010-01-14 10:40:12 +00:00
|
|
|
#include <string.h>
|
2016-12-04 23:10:57 +00:00
|
|
|
#include <stdio.h>
|
2007-10-25 08:34:16 +00:00
|
|
|
#include <time.h>
|
2017-11-03 12:00:26 +00:00
|
|
|
#include <stdarg.h>
|
2015-04-29 15:13:35 +00:00
|
|
|
#include <sys/time.h>
|
2007-05-10 04:39:03 +00:00
|
|
|
#include <sys/types.h>
|
2012-09-04 12:18:33 +00:00
|
|
|
#include <sys/socket.h>
|
2012-09-02 20:34:23 +00:00
|
|
|
#include <sys/un.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <netinet/in.h>
|
2015-07-21 21:47:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-03-17 11:01:13 +00:00
|
|
|
#include <common/chunk.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
MINOR: tcp: add dst_is_local and src_is_local
It is sometimes needed in application server environments to easily tell
if a source is local to the machine or a remote one, without necessarily
knowing all the local addresses (dhcp, vrrp, etc). Similarly in transparent
proxy configurations it is sometimes desired to tell the difference between
local and remote destination addresses.
This patch adds two new sample fetch functions for this :
dst_is_local : boolean
Returns true if the destination address of the incoming connection is local
to the system, or false if the address doesn't exist on the system, meaning
that it was intercepted in transparent mode. It can be useful to apply
certain rules by default to forwarded traffic and other rules to the traffic
targetting the real address of the machine. For example the stats page could
be delivered only on this address, or SSH access could be locally redirected.
Please note that the check involves a few system calls, so it's better to do
it only once per connection.
src_is_local : boolean
Returns true if the source address of the incoming connection is local to the
system, or false if the address doesn't exist on the system, meaning that it
comes from a remote machine. Note that UNIX addresses are considered local.
It can be useful to apply certain access restrictions based on where the
client comes from (eg: require auth or https for remote machines). Please
note that the check involves a few system calls, so it's better to do it only
once per connection.
2016-08-09 14:46:18 +00:00
|
|
|
#include <common/namespace.h>
|
2009-10-26 20:10:04 +00:00
|
|
|
#include <eb32tree.h>
|
2017-11-15 17:51:29 +00:00
|
|
|
#include <eb32sctree.h>
|
2018-09-11 14:51:29 +00:00
|
|
|
#include <types/protocol.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2012-01-24 10:15:39 +00:00
|
|
|
#ifndef LLONG_MAX
|
|
|
|
# define LLONG_MAX 9223372036854775807LL
|
|
|
|
# define LLONG_MIN (-LLONG_MAX - 1LL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ULLONG_MAX
|
|
|
|
# define ULLONG_MAX (LLONG_MAX * 2ULL + 1)
|
|
|
|
#endif
|
|
|
|
|
2013-01-18 10:29:29 +00:00
|
|
|
#ifndef LONGBITS
|
|
|
|
#define LONGBITS ((unsigned int)sizeof(long) * 8)
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 16:40:34 +00:00
|
|
|
/* size used for max length of decimal representation of long long int. */
|
|
|
|
#define NB_LLMAX_STR (sizeof("-9223372036854775807")-1)
|
|
|
|
|
2012-12-23 17:00:29 +00:00
|
|
|
/* number of itoa_str entries */
|
|
|
|
#define NB_ITOA_STR 10
|
2012-01-24 10:15:39 +00:00
|
|
|
|
2014-06-16 13:16:40 +00:00
|
|
|
/* maximum quoted string length (truncated above) */
|
|
|
|
#define QSTR_SIZE 200
|
|
|
|
#define NB_QSTR 10
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/****** string-specific macros and functions ******/
|
|
|
|
/* if a > max, then bound <a> to <max>. The macro returns the new <a> */
|
|
|
|
#define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); })
|
|
|
|
|
|
|
|
/* if a < min, then bound <a> to <min>. The macro returns the new <a> */
|
|
|
|
#define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); })
|
|
|
|
|
|
|
|
/* returns 1 only if only zero or one bit is set in X, which means that X is a
|
|
|
|
* power of 2, and 0 otherwise */
|
|
|
|
#define POWEROF2(x) (((x) & ((x)-1)) == 0)
|
|
|
|
|
2010-07-18 08:40:48 +00:00
|
|
|
/* operators to compare values. They're ordered that way so that the lowest bit
|
|
|
|
* serves as a negation for the test and contains all tests that are not equal.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
STD_OP_LE = 0, STD_OP_GT = 1,
|
|
|
|
STD_OP_EQ = 2, STD_OP_NE = 3,
|
|
|
|
STD_OP_GE = 4, STD_OP_LT = 5,
|
|
|
|
};
|
|
|
|
|
2014-03-21 13:51:46 +00:00
|
|
|
enum http_scheme {
|
|
|
|
SCH_HTTP,
|
|
|
|
SCH_HTTPS,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct split_url {
|
|
|
|
enum http_scheme scheme;
|
|
|
|
const char *host;
|
|
|
|
int host_len;
|
|
|
|
};
|
|
|
|
|
2017-11-14 15:47:26 +00:00
|
|
|
extern THREAD_LOCAL int itoa_idx; /* index of next itoa_str to use */
|
2012-12-23 17:00:29 +00:00
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* copies at most <size-1> chars from <src> to <dst>. Last char is always
|
|
|
|
* set to 0, unless <size> is 0. The number of chars copied is returned
|
|
|
|
* (excluding the terminating zero).
|
|
|
|
* This code has been optimized for size and speed : on x86, it's 45 bytes
|
|
|
|
* long, uses only registers, and consumes only 4 cycles per char.
|
|
|
|
*/
|
|
|
|
extern int strlcpy2(char *dst, const char *src, int size);
|
|
|
|
|
|
|
|
/*
|
2007-10-25 10:14:10 +00:00
|
|
|
* This function simply returns a locally allocated string containing
|
2006-06-26 00:48:02 +00:00
|
|
|
* the ascii representation for number 'n' in decimal.
|
|
|
|
*/
|
2017-11-14 15:47:26 +00:00
|
|
|
extern THREAD_LOCAL char itoa_str[][171];
|
2010-01-04 13:54:38 +00:00
|
|
|
extern char *ultoa_r(unsigned long n, char *buffer, int size);
|
2015-07-06 21:09:52 +00:00
|
|
|
extern char *lltoa_r(long long int n, char *buffer, int size);
|
2015-06-06 17:14:59 +00:00
|
|
|
extern char *sltoa_r(long n, char *buffer, int size);
|
2009-03-29 11:41:58 +00:00
|
|
|
extern const char *ulltoh_r(unsigned long long n, char *buffer, int size);
|
2007-10-25 10:14:10 +00:00
|
|
|
static inline const char *ultoa(unsigned long n)
|
|
|
|
{
|
|
|
|
return ultoa_r(n, itoa_str[0], sizeof(itoa_str[0]));
|
|
|
|
}
|
|
|
|
|
2012-01-24 10:15:39 +00:00
|
|
|
/*
|
|
|
|
* unsigned long long ASCII representation
|
|
|
|
*
|
|
|
|
* return the last char '\0' or NULL if no enough
|
|
|
|
* space in dst
|
|
|
|
*/
|
|
|
|
char *ulltoa(unsigned long long n, char *dst, size_t size);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unsigned long ASCII representation
|
|
|
|
*
|
|
|
|
* return the last char '\0' or NULL if no enough
|
|
|
|
* space in dst
|
|
|
|
*/
|
|
|
|
char *ultoa_o(unsigned long n, char *dst, size_t size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signed long ASCII representation
|
|
|
|
*
|
|
|
|
* return the last char '\0' or NULL if no enough
|
|
|
|
* space in dst
|
|
|
|
*/
|
|
|
|
char *ltoa_o(long int n, char *dst, size_t size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signed long long ASCII representation
|
|
|
|
*
|
|
|
|
* return the last char '\0' or NULL if no enough
|
|
|
|
* space in dst
|
|
|
|
*/
|
|
|
|
char *lltoa(long long n, char *dst, size_t size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a ascii representation of a unsigned into dst,
|
|
|
|
* return a pointer to the last character
|
|
|
|
* Pad the ascii representation with '0', using size.
|
|
|
|
*/
|
|
|
|
char *utoa_pad(unsigned int n, char *dst, size_t size);
|
|
|
|
|
2007-10-25 14:58:42 +00:00
|
|
|
/*
|
|
|
|
* This function simply returns a locally allocated string containing the ascii
|
|
|
|
* representation for number 'n' in decimal, unless n is 0 in which case it
|
|
|
|
* returns the alternate string (or an empty string if the alternate string is
|
|
|
|
* NULL). It use is intended for limits reported in reports, where it's
|
|
|
|
* desirable not to display anything if there is no limit. Warning! it shares
|
|
|
|
* the same vector as ultoa_r().
|
|
|
|
*/
|
|
|
|
extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt);
|
|
|
|
|
2012-12-23 17:00:29 +00:00
|
|
|
/* returns a locally allocated string containing the ASCII representation of
|
|
|
|
* the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same
|
|
|
|
* function call (eg: printf), shared with the other similar functions making
|
|
|
|
* use of itoa_str[].
|
|
|
|
*/
|
|
|
|
static inline const char *U2A(unsigned long n)
|
|
|
|
{
|
|
|
|
const char *ret = ultoa_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]));
|
|
|
|
if (++itoa_idx >= NB_ITOA_STR)
|
|
|
|
itoa_idx = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns a locally allocated string containing the HTML representation of
|
|
|
|
* the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same
|
|
|
|
* function call (eg: printf), shared with the other similar functions making
|
|
|
|
* use of itoa_str[].
|
|
|
|
*/
|
|
|
|
static inline const char *U2H(unsigned long long n)
|
|
|
|
{
|
|
|
|
const char *ret = ulltoh_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]));
|
|
|
|
if (++itoa_idx >= NB_ITOA_STR)
|
|
|
|
itoa_idx = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:05:19 +00:00
|
|
|
/* returns a locally allocated string containing the ASCII representation of
|
2012-12-23 17:00:29 +00:00
|
|
|
* the number 'n' in decimal. Up to NB_ITOA_STR calls may be used in the same
|
|
|
|
* function call (eg: printf), shared with the other similar functions making
|
|
|
|
* use of itoa_str[].
|
|
|
|
*/
|
|
|
|
static inline const char *LIM2A(unsigned long n, const char *alt)
|
|
|
|
{
|
|
|
|
const char *ret = limit_r(n, itoa_str[itoa_idx], sizeof(itoa_str[0]), alt);
|
|
|
|
if (++itoa_idx >= NB_ITOA_STR)
|
|
|
|
itoa_idx = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2007-10-25 14:58:42 +00:00
|
|
|
|
2017-04-19 09:49:44 +00:00
|
|
|
/* Encode the integer <i> into a varint (variable-length integer). The encoded
|
|
|
|
* value is copied in <*buf>. Here is the encoding format:
|
|
|
|
*
|
|
|
|
* 0 <= X < 240 : 1 byte (7.875 bits) [ XXXX XXXX ]
|
|
|
|
* 240 <= X < 2288 : 2 bytes (11 bits) [ 1111 XXXX ] [ 0XXX XXXX ]
|
|
|
|
* 2288 <= X < 264432 : 3 bytes (18 bits) [ 1111 XXXX ] [ 1XXX XXXX ] [ 0XXX XXXX ]
|
|
|
|
* 264432 <= X < 33818864 : 4 bytes (25 bits) [ 1111 XXXX ] [ 1XXX XXXX ]*2 [ 0XXX XXXX ]
|
|
|
|
* 33818864 <= X < 4328786160 : 5 bytes (32 bits) [ 1111 XXXX ] [ 1XXX XXXX ]*3 [ 0XXX XXXX ]
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* On success, it returns the number of written bytes and <*buf> is moved after
|
|
|
|
* the encoded value. Otherwise, it returns -1. */
|
|
|
|
static inline int
|
|
|
|
encode_varint(uint64_t i, char **buf, char *end)
|
|
|
|
{
|
|
|
|
unsigned char *p = (unsigned char *)*buf;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (p >= (unsigned char *)end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (i < 240) {
|
|
|
|
*p++ = i;
|
|
|
|
*buf = (char *)p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = (unsigned char)i | 240;
|
|
|
|
i = (i - 240) >> 4;
|
|
|
|
while (i >= 128) {
|
|
|
|
if (p >= (unsigned char *)end)
|
|
|
|
return -1;
|
|
|
|
*p++ = (unsigned char)i | 128;
|
|
|
|
i = (i - 128) >> 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p >= (unsigned char *)end)
|
|
|
|
return -1;
|
|
|
|
*p++ = (unsigned char)i;
|
|
|
|
|
|
|
|
r = ((char *)p - *buf);
|
|
|
|
*buf = (char *)p;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode a varint from <*buf> and save the decoded value in <*i>. See
|
|
|
|
* 'spoe_encode_varint' for details about varint.
|
|
|
|
* On success, it returns the number of read bytes and <*buf> is moved after the
|
|
|
|
* varint. Otherwise, it returns -1. */
|
|
|
|
static inline int
|
|
|
|
decode_varint(char **buf, char *end, uint64_t *i)
|
|
|
|
{
|
|
|
|
unsigned char *p = (unsigned char *)*buf;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (p >= (unsigned char *)end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*i = *p++;
|
|
|
|
if (*i < 240) {
|
|
|
|
*buf = (char *)p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 4;
|
|
|
|
do {
|
|
|
|
if (p >= (unsigned char *)end)
|
|
|
|
return -1;
|
|
|
|
*i += (uint64_t)*p << r;
|
|
|
|
r += 7;
|
|
|
|
} while (*p++ >= 128);
|
|
|
|
|
|
|
|
r = ((char *)p - *buf);
|
|
|
|
*buf = (char *)p;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:16:40 +00:00
|
|
|
/* returns a locally allocated string containing the quoted encoding of the
|
|
|
|
* input string. The output may be truncated to QSTR_SIZE chars, but it is
|
|
|
|
* guaranteed that the string will always be properly terminated. Quotes are
|
|
|
|
* encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must
|
|
|
|
* always be at least 4 chars.
|
|
|
|
*/
|
|
|
|
const char *qstr(const char *str);
|
|
|
|
|
|
|
|
/* returns <str> or its quote-encoded equivalent if it contains at least one
|
|
|
|
* quote or a comma. This is aimed at build CSV-compatible strings.
|
|
|
|
*/
|
|
|
|
static inline const char *cstr(const char *str)
|
|
|
|
{
|
|
|
|
const char *p = str;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p == ',' || *p == '"')
|
|
|
|
return qstr(str);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
|
|
|
|
*/
|
|
|
|
extern int ishex(char s);
|
|
|
|
|
2009-11-02 19:12:52 +00:00
|
|
|
/*
|
|
|
|
* Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
|
2012-10-25 22:58:22 +00:00
|
|
|
* otherwise -1. This compact form helps gcc produce efficient code.
|
2009-11-02 19:12:52 +00:00
|
|
|
*/
|
2012-10-25 22:58:22 +00:00
|
|
|
static inline int hex2i(int c)
|
|
|
|
{
|
2017-11-10 10:19:54 +00:00
|
|
|
if ((unsigned char)(c -= '0') > 9) {
|
|
|
|
if ((unsigned char)(c -= 'A' - '0') > 5 &&
|
|
|
|
(unsigned char)(c -= 'a' - 'A') > 5)
|
2012-10-25 22:58:22 +00:00
|
|
|
c = -11;
|
|
|
|
c += 10;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-01-15 17:43:49 +00:00
|
|
|
/* rounds <i> down to the closest value having max 2 digits */
|
|
|
|
unsigned int round_2dig(unsigned int i);
|
2009-11-02 19:12:52 +00:00
|
|
|
|
2007-12-02 17:45:09 +00:00
|
|
|
/*
|
|
|
|
* Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
|
|
|
|
* invalid character is found, a pointer to it is returned. If everything is
|
|
|
|
* fine, NULL is returned.
|
|
|
|
*/
|
|
|
|
extern const char *invalid_char(const char *name);
|
|
|
|
|
2008-05-23 21:49:32 +00:00
|
|
|
/*
|
2017-04-13 16:24:23 +00:00
|
|
|
* Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_.-].
|
2008-05-23 21:49:32 +00:00
|
|
|
* If an invalid character is found, a pointer to it is returned.
|
|
|
|
* If everything is fine, NULL is returned.
|
|
|
|
*/
|
|
|
|
extern const char *invalid_domainchar(const char *name);
|
|
|
|
|
2017-04-13 16:24:23 +00:00
|
|
|
/*
|
|
|
|
* Checks <name> for invalid characters. Valid chars are [A-Za-z_.-].
|
|
|
|
* If an invalid character is found, a pointer to it is returned.
|
|
|
|
* If everything is fine, NULL is returned.
|
|
|
|
*/
|
|
|
|
extern const char *invalid_prefix_char(const char *name);
|
|
|
|
|
2011-03-04 14:31:53 +00:00
|
|
|
/*
|
|
|
|
* converts <str> to a locally allocated struct sockaddr_storage *, and a
|
2009-06-10 09:09:37 +00:00
|
|
|
* port range consisting in two integers. The low and high end are always set
|
|
|
|
* even if the port is unspecified, in which case (0,0) is returned. The low
|
2011-03-04 14:31:53 +00:00
|
|
|
* port is set in the sockaddr. Thus, it is enough to check the size of the
|
2009-06-10 09:09:37 +00:00
|
|
|
* returned range to know if an array must be allocated or not. The format is
|
2011-03-04 14:31:53 +00:00
|
|
|
* "addr[:[port[-port]]]", where "addr" can be a dotted IPv4 address, an IPv6
|
|
|
|
* address, a host name, or empty or "*" to indicate INADDR_ANY. If an IPv6
|
|
|
|
* address wants to ignore port, it must be terminated by a trailing colon (':').
|
|
|
|
* The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on
|
|
|
|
* IPv6, use ":::port". NULL is returned if the host part cannot be resolved.
|
2013-03-04 17:22:00 +00:00
|
|
|
* If <pfx> is non-null, it is used as a string prefix before any path-based
|
2015-09-26 18:03:36 +00:00
|
|
|
* address (typically the path to a unix socket). If use_dns is not true,
|
2018-11-15 19:58:28 +00:00
|
|
|
* the function cannot accept the DNS resolution.
|
2009-06-10 09:09:37 +00:00
|
|
|
*/
|
2017-01-06 17:32:38 +00:00
|
|
|
struct sockaddr_storage *str2sa_range(const char *str,
|
|
|
|
int *port, int *low, int *high,
|
|
|
|
char **err, const char *pfx,
|
|
|
|
char **fqdn, int resolve);
|
2009-06-10 09:09:37 +00:00
|
|
|
|
2010-01-26 16:36:17 +00:00
|
|
|
/* converts <str> to a struct in_addr containing a network mask. It can be
|
|
|
|
* passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
|
2017-05-21 14:32:21 +00:00
|
|
|
* if the conversion succeeds otherwise zero.
|
2010-01-26 16:36:17 +00:00
|
|
|
*/
|
|
|
|
int str2mask(const char *str, struct in_addr *mask);
|
|
|
|
|
2018-01-25 15:24:49 +00:00
|
|
|
/* converts <str> to a struct in6_addr containing a network mask. It can be
|
2018-02-20 16:02:18 +00:00
|
|
|
* passed in quadruplet form (ffff:ffff::) or in CIDR form (64). It returns 1
|
2018-01-25 15:24:49 +00:00
|
|
|
* if the conversion succeeds otherwise zero.
|
|
|
|
*/
|
|
|
|
int str2mask6(const char *str, struct in6_addr *mask);
|
|
|
|
|
2013-12-14 14:39:02 +00:00
|
|
|
/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
|
|
|
|
* succeeds otherwise non-zero.
|
|
|
|
*/
|
|
|
|
int cidr2dotted(int cidr, struct in_addr *mask);
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2007-05-08 16:28:09 +00:00
|
|
|
* converts <str> to two struct in_addr* which must be pre-allocated.
|
2006-06-26 00:48:02 +00:00
|
|
|
* The format is "addr[/mask]", where "addr" cannot be empty, and mask
|
|
|
|
* is optionnal and either in the dotted or CIDR notation.
|
|
|
|
* Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
|
|
|
|
*/
|
2014-02-11 14:23:04 +00:00
|
|
|
int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2014-11-25 11:02:25 +00:00
|
|
|
/* str2ip and str2ip2:
|
|
|
|
*
|
|
|
|
* converts <str> to a struct sockaddr_storage* provided by the caller. The
|
|
|
|
* caller must have zeroed <sa> first, and may have set sa->ss_family to force
|
|
|
|
* parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
|
|
|
|
* the function tries to guess the address family from the syntax. If the
|
|
|
|
* family is forced and the format doesn't match, an error is returned. The
|
|
|
|
* string is assumed to contain only an address, no port. The address can be a
|
|
|
|
* dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
|
|
|
|
* indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
|
|
|
|
* The return address will only have the address family and the address set,
|
|
|
|
* all other fields remain zero. The string is not supposed to be modified.
|
|
|
|
* The IPv6 '::' address is IN6ADDR_ANY.
|
|
|
|
*
|
|
|
|
* str2ip2:
|
|
|
|
*
|
|
|
|
* If <resolve> is set, this function try to resolve DNS, otherwise, it returns
|
|
|
|
* NULL result.
|
|
|
|
*/
|
|
|
|
struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve);
|
|
|
|
static inline struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa)
|
|
|
|
{
|
|
|
|
return str2ip2(str, sa, 1);
|
|
|
|
}
|
|
|
|
|
2012-04-27 20:49:47 +00:00
|
|
|
/*
|
|
|
|
* converts <str> to two struct in6_addr* which must be pre-allocated.
|
|
|
|
* The format is "addr[/mask]", where "addr" cannot be empty, and mask
|
|
|
|
* is an optionnal number of bits (128 being the default).
|
|
|
|
* Returns 1 if OK, 0 if error.
|
|
|
|
*/
|
|
|
|
int str62net(const char *str, struct in6_addr *addr, unsigned char *mask);
|
|
|
|
|
2009-09-19 05:54:16 +00:00
|
|
|
/*
|
|
|
|
* Parse IP address found in url.
|
|
|
|
*/
|
2011-03-10 21:26:24 +00:00
|
|
|
int url2ipv4(const char *addr, struct in_addr *dst);
|
2009-09-19 05:54:16 +00:00
|
|
|
|
2007-11-29 14:43:32 +00:00
|
|
|
/*
|
2011-03-10 21:26:24 +00:00
|
|
|
* Resolve destination server from URL. Convert <str> to a sockaddr_storage*.
|
2007-11-29 14:43:32 +00:00
|
|
|
*/
|
2014-03-21 13:51:46 +00:00
|
|
|
int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out);
|
2007-11-29 14:43:32 +00:00
|
|
|
|
2011-09-04 22:36:48 +00:00
|
|
|
/* Tries to convert a sockaddr_storage address to text form. Upon success, the
|
|
|
|
* address family is returned so that it's easy for the caller to adapt to the
|
|
|
|
* output format. Zero is returned if the address family is not supported. -1
|
|
|
|
* is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
|
|
|
|
* supported.
|
|
|
|
*/
|
|
|
|
int addr_to_str(struct sockaddr_storage *addr, char *str, int size);
|
|
|
|
|
2014-06-16 00:39:41 +00:00
|
|
|
/* Tries to convert a sockaddr_storage port to text form. Upon success, the
|
|
|
|
* address family is returned so that it's easy for the caller to adapt to the
|
|
|
|
* output format. Zero is returned if the address family is not supported. -1
|
|
|
|
* is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
|
|
|
|
* supported.
|
|
|
|
*/
|
|
|
|
int port_to_str(struct sockaddr_storage *addr, char *str, int size);
|
|
|
|
|
MINOR: tcp: add dst_is_local and src_is_local
It is sometimes needed in application server environments to easily tell
if a source is local to the machine or a remote one, without necessarily
knowing all the local addresses (dhcp, vrrp, etc). Similarly in transparent
proxy configurations it is sometimes desired to tell the difference between
local and remote destination addresses.
This patch adds two new sample fetch functions for this :
dst_is_local : boolean
Returns true if the destination address of the incoming connection is local
to the system, or false if the address doesn't exist on the system, meaning
that it was intercepted in transparent mode. It can be useful to apply
certain rules by default to forwarded traffic and other rules to the traffic
targetting the real address of the machine. For example the stats page could
be delivered only on this address, or SSH access could be locally redirected.
Please note that the check involves a few system calls, so it's better to do
it only once per connection.
src_is_local : boolean
Returns true if the source address of the incoming connection is local to the
system, or false if the address doesn't exist on the system, meaning that it
comes from a remote machine. Note that UNIX addresses are considered local.
It can be useful to apply certain access restrictions based on where the
client comes from (eg: require auth or https for remote machines). Please
note that the check involves a few system calls, so it's better to do it only
once per connection.
2016-08-09 14:46:18 +00:00
|
|
|
/* check if the given address is local to the system or not. It will return
|
|
|
|
* -1 when it's not possible to know, 0 when the address is not local, 1 when
|
|
|
|
* it is. We don't want to iterate over all interfaces for this (and it is not
|
|
|
|
* portable). So instead we try to bind in UDP to this address on a free non
|
|
|
|
* privileged port and to connect to the same address, port 0 (connect doesn't
|
|
|
|
* care). If it succeeds, we own the address. Note that non-inet addresses are
|
|
|
|
* considered local since they're most likely AF_UNIX.
|
|
|
|
*/
|
|
|
|
int addr_is_local(const struct netns_entry *ns,
|
|
|
|
const struct sockaddr_storage *orig);
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/* will try to encode the string <string> replacing all characters tagged in
|
|
|
|
* <map> with the hexadecimal representation of their ASCII-code (2 digits)
|
|
|
|
* prefixed by <escape>, and will store the result between <start> (included)
|
|
|
|
* and <stop> (excluded), and will always terminate the string with a '\0'
|
|
|
|
* before <stop>. The position of the '\0' is returned if the conversion
|
|
|
|
* completes. If bytes are missing between <start> and <stop>, then the
|
|
|
|
* conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
|
|
|
|
* cannot even be stored so we return <start> without writing the 0.
|
|
|
|
* The input string must also be zero-terminated.
|
|
|
|
*/
|
|
|
|
extern const char hextab[];
|
|
|
|
char *encode_string(char *start, char *stop,
|
|
|
|
const char escape, const fd_set *map,
|
|
|
|
const char *string);
|
|
|
|
|
2014-03-17 11:01:13 +00:00
|
|
|
/*
|
|
|
|
* Same behavior, except that it encodes chunk <chunk> instead of a string.
|
|
|
|
*/
|
|
|
|
char *encode_chunk(char *start, char *stop,
|
|
|
|
const char escape, const fd_set *map,
|
2018-07-13 09:56:34 +00:00
|
|
|
const struct buffer *chunk);
|
2014-03-17 11:01:13 +00:00
|
|
|
|
2016-07-22 14:00:31 +00:00
|
|
|
/*
|
|
|
|
* Tries to prefix characters tagged in the <map> with the <escape>
|
|
|
|
* character. The input <string> must be zero-terminated. The result will
|
|
|
|
* be stored between <start> (included) and <stop> (excluded). This
|
|
|
|
* function will always try to terminate the resulting string with a '\0'
|
|
|
|
* before <stop>, and will return its position if the conversion
|
|
|
|
* completes.
|
|
|
|
*/
|
|
|
|
char *escape_string(char *start, char *stop,
|
|
|
|
const char escape, const fd_set *map,
|
|
|
|
const char *string);
|
|
|
|
|
2016-02-12 12:23:02 +00:00
|
|
|
/*
|
|
|
|
* Tries to prefix characters tagged in the <map> with the <escape>
|
|
|
|
* character. <chunk> contains the input to be escaped. The result will be
|
|
|
|
* stored between <start> (included) and <stop> (excluded). The function
|
|
|
|
* will always try to terminate the resulting string with a '\0' before
|
|
|
|
* <stop>, and will return its position if the conversion completes.
|
|
|
|
*/
|
|
|
|
char *escape_chunk(char *start, char *stop,
|
|
|
|
const char escape, const fd_set *map,
|
2018-07-13 09:56:34 +00:00
|
|
|
const struct buffer *chunk);
|
2016-02-12 12:23:02 +00:00
|
|
|
|
2014-03-17 11:01:13 +00:00
|
|
|
|
2015-05-28 14:00:28 +00:00
|
|
|
/* Check a string for using it in a CSV output format. If the string contains
|
|
|
|
* one of the following four char <">, <,>, CR or LF, the string is
|
|
|
|
* encapsulated between <"> and the <"> are escaped by a <""> sequence.
|
|
|
|
* <str> is the input string to be escaped. The function assumes that
|
|
|
|
* the input string is null-terminated.
|
|
|
|
*
|
|
|
|
* If <quote> is 0, the result is returned escaped but without double quote.
|
2016-01-06 17:07:04 +00:00
|
|
|
* It is useful if the escaped string is used between double quotes in the
|
2015-05-28 14:00:28 +00:00
|
|
|
* format.
|
|
|
|
*
|
2016-01-06 17:07:04 +00:00
|
|
|
* printf("..., \"%s\", ...\r\n", csv_enc(str, 0, &trash));
|
2015-05-28 14:00:28 +00:00
|
|
|
*
|
2016-01-06 17:07:04 +00:00
|
|
|
* If <quote> is 1, the converter puts the quotes only if any character is
|
|
|
|
* escaped. If <quote> is 2, the converter always puts the quotes.
|
2015-05-28 14:00:28 +00:00
|
|
|
*
|
2016-01-06 17:07:04 +00:00
|
|
|
* <output> is a struct chunk used for storing the output string.
|
2015-05-28 14:00:28 +00:00
|
|
|
*
|
2016-01-06 17:07:04 +00:00
|
|
|
* The function returns the converted string on its output. If an error
|
|
|
|
* occurs, the function returns an empty string. This type of output is useful
|
2015-05-28 14:00:28 +00:00
|
|
|
* for using the function directly as printf() argument.
|
|
|
|
*
|
2016-01-06 17:07:04 +00:00
|
|
|
* If the output buffer is too short to contain the input string, the result
|
2015-05-28 14:00:28 +00:00
|
|
|
* is truncated.
|
2016-01-06 17:07:04 +00:00
|
|
|
*
|
|
|
|
* This function appends the encoding to the existing output chunk. Please
|
|
|
|
* use csv_enc() instead if you want to replace the output chunk.
|
2015-05-28 14:00:28 +00:00
|
|
|
*/
|
2018-07-13 09:56:34 +00:00
|
|
|
const char *csv_enc_append(const char *str, int quote, struct buffer *output);
|
2016-01-06 17:07:04 +00:00
|
|
|
|
|
|
|
/* same as above but the output chunk is reset first */
|
2018-07-13 09:56:34 +00:00
|
|
|
static inline const char *csv_enc(const char *str, int quote,
|
|
|
|
struct buffer *output)
|
2016-01-06 17:07:04 +00:00
|
|
|
{
|
|
|
|
chunk_reset(output);
|
|
|
|
return csv_enc_append(str, quote, output);
|
|
|
|
}
|
2015-05-28 14:00:28 +00:00
|
|
|
|
2011-05-31 16:06:18 +00:00
|
|
|
/* Decode an URL-encoded string in-place. The resulting string might
|
|
|
|
* be shorter. If some forbidden characters are found, the conversion is
|
|
|
|
* aborted, the string is truncated before the issue and non-zero is returned,
|
|
|
|
* otherwise the operation returns non-zero indicating success.
|
|
|
|
*/
|
|
|
|
int url_decode(char *string);
|
|
|
|
|
2007-03-04 17:06:08 +00:00
|
|
|
/* This one is 6 times faster than strtoul() on athlon, but does
|
|
|
|
* no check at all.
|
|
|
|
*/
|
|
|
|
static inline unsigned int __str2ui(const char *s)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (*s) {
|
|
|
|
i = i * 10 - '0';
|
|
|
|
i += (unsigned char)*s++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This one is 5 times faster than strtoul() on athlon with checks.
|
|
|
|
* It returns the value of the number composed of all valid digits read.
|
|
|
|
*/
|
|
|
|
static inline unsigned int __str2uic(const char *s)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned int j;
|
|
|
|
while (1) {
|
|
|
|
j = (*s++) - '0';
|
|
|
|
if (j > 9)
|
|
|
|
break;
|
|
|
|
i *= 10;
|
|
|
|
i += j;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This one is 28 times faster than strtoul() on athlon, but does
|
|
|
|
* no check at all!
|
|
|
|
*/
|
|
|
|
static inline unsigned int __strl2ui(const char *s, int len)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
while (len-- > 0) {
|
|
|
|
i = i * 10 - '0';
|
|
|
|
i += (unsigned char)*s++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This one is 7 times faster than strtoul() on athlon with checks.
|
|
|
|
* It returns the value of the number composed of all valid digits read.
|
|
|
|
*/
|
|
|
|
static inline unsigned int __strl2uic(const char *s, int len)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
2007-10-25 07:42:24 +00:00
|
|
|
unsigned int j, k;
|
2007-03-04 17:06:08 +00:00
|
|
|
|
|
|
|
while (len-- > 0) {
|
|
|
|
j = (*s++) - '0';
|
2007-10-25 07:42:24 +00:00
|
|
|
k = i * 10;
|
2007-03-04 17:06:08 +00:00
|
|
|
if (j > 9)
|
|
|
|
break;
|
2007-10-25 07:42:24 +00:00
|
|
|
i = k + j;
|
2007-03-04 17:06:08 +00:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2010-10-15 21:19:55 +00:00
|
|
|
/* This function reads an unsigned integer from the string pointed to by <s>
|
|
|
|
* and returns it. The <s> pointer is adjusted to point to the first unread
|
|
|
|
* char. The function automatically stops at <end>.
|
|
|
|
*/
|
|
|
|
static inline unsigned int __read_uint(const char **s, const char *end)
|
|
|
|
{
|
|
|
|
const char *ptr = *s;
|
|
|
|
unsigned int i = 0;
|
|
|
|
unsigned int j, k;
|
|
|
|
|
|
|
|
while (ptr < end) {
|
|
|
|
j = *ptr - '0';
|
|
|
|
k = i * 10;
|
|
|
|
if (j > 9)
|
|
|
|
break;
|
|
|
|
i = k + j;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
*s = ptr;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:09:52 +00:00
|
|
|
unsigned long long int read_uint64(const char **s, const char *end);
|
|
|
|
long long int read_int64(const char **s, const char *end);
|
|
|
|
|
2007-03-04 17:06:08 +00:00
|
|
|
extern unsigned int str2ui(const char *s);
|
|
|
|
extern unsigned int str2uic(const char *s);
|
|
|
|
extern unsigned int strl2ui(const char *s, int len);
|
|
|
|
extern unsigned int strl2uic(const char *s, int len);
|
|
|
|
extern int strl2ic(const char *s, int len);
|
|
|
|
extern int strl2irc(const char *s, int len, int *ret);
|
|
|
|
extern int strl2llrc(const char *s, int len, long long *ret);
|
2014-01-23 16:40:34 +00:00
|
|
|
extern int strl2llrc_dotted(const char *text, int len, long long *ret);
|
2010-10-15 21:19:55 +00:00
|
|
|
extern unsigned int read_uint(const char **s, const char *end);
|
2009-09-07 09:00:31 +00:00
|
|
|
unsigned int inetaddr_host(const char *text);
|
|
|
|
unsigned int inetaddr_host_lim(const char *text, const char *stop);
|
2010-10-15 21:21:42 +00:00
|
|
|
unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret);
|
2007-03-04 17:06:08 +00:00
|
|
|
|
2009-10-10 18:11:17 +00:00
|
|
|
static inline char *cut_crlf(char *s) {
|
|
|
|
|
2013-02-13 08:48:00 +00:00
|
|
|
while (*s != '\r' && *s != '\n') {
|
2009-10-10 18:11:17 +00:00
|
|
|
char *p = s++;
|
|
|
|
|
|
|
|
if (!*p)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-02-13 08:48:00 +00:00
|
|
|
*s++ = '\0';
|
2009-10-10 18:11:17 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *ltrim(char *s, char c) {
|
|
|
|
|
|
|
|
if (c)
|
|
|
|
while (*s == c)
|
|
|
|
s++;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *rtrim(char *s, char c) {
|
|
|
|
|
|
|
|
char *p = s + strlen(s);
|
|
|
|
|
|
|
|
while (p-- > s)
|
|
|
|
if (*p == c)
|
|
|
|
*p = '\0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *alltrim(char *s, char c) {
|
|
|
|
|
|
|
|
rtrim(s, c);
|
|
|
|
|
|
|
|
return ltrim(s, c);
|
|
|
|
}
|
|
|
|
|
2007-10-25 08:34:16 +00:00
|
|
|
/* This function converts the time_t value <now> into a broken out struct tm
|
|
|
|
* which must be allocated by the caller. It is highly recommended to use this
|
|
|
|
* function intead of localtime() because that one requires a time_t* which
|
|
|
|
* is not always compatible with tv_sec depending on OS/hardware combinations.
|
|
|
|
*/
|
|
|
|
static inline void get_localtime(const time_t now, struct tm *tm)
|
|
|
|
{
|
|
|
|
localtime_r(&now, tm);
|
|
|
|
}
|
|
|
|
|
2009-06-30 16:26:00 +00:00
|
|
|
/* This function converts the time_t value <now> into a broken out struct tm
|
|
|
|
* which must be allocated by the caller. It is highly recommended to use this
|
|
|
|
* function intead of gmtime() because that one requires a time_t* which
|
|
|
|
* is not always compatible with tv_sec depending on OS/hardware combinations.
|
|
|
|
*/
|
|
|
|
static inline void get_gmtime(const time_t now, struct tm *tm)
|
|
|
|
{
|
|
|
|
gmtime_r(&now, tm);
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:05:29 +00:00
|
|
|
/* Counts a number of elapsed days since 01/01/0000 based solely on elapsed
|
|
|
|
* years and assuming the regular rule for leap years applies. It's fake but
|
|
|
|
* serves as a temporary origin. It's worth remembering that it's the first
|
|
|
|
* year of each period that is leap and not the last one, so for instance year
|
|
|
|
* 1 sees 366 days since year 0 was leap. For this reason we have to apply
|
|
|
|
* modular arithmetics which is why we offset the year by 399 before
|
|
|
|
* subtracting the excess at the end. No overflow here before ~11.7 million
|
|
|
|
* years.
|
|
|
|
*/
|
|
|
|
static inline unsigned int days_since_zero(unsigned int y)
|
|
|
|
{
|
|
|
|
return y * 365 + (y + 399) / 4 - (y + 399) / 100 + (y + 399) / 400
|
|
|
|
- 399 / 4 + 399 / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of seconds since 01/01/1970 0:0:0 GMT for GMT date <tm>.
|
|
|
|
* It is meant as a portable replacement for timegm() for use with valid inputs.
|
|
|
|
* Returns undefined results for invalid dates (eg: months out of range 0..11).
|
|
|
|
*/
|
|
|
|
extern time_t my_timegm(const struct tm *tm);
|
|
|
|
|
2007-12-02 21:00:35 +00:00
|
|
|
/* This function parses a time value optionally followed by a unit suffix among
|
|
|
|
* "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
|
|
|
|
* expected by the caller. The computation does its best to avoid overflows.
|
|
|
|
* The value is returned in <ret> if everything is fine, and a NULL is returned
|
|
|
|
* by the function. In case of error, a pointer to the error is returned and
|
|
|
|
* <ret> is left untouched.
|
|
|
|
*/
|
|
|
|
extern const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags);
|
2010-01-04 13:57:24 +00:00
|
|
|
extern const char *parse_size_err(const char *text, unsigned *ret);
|
2007-12-02 21:00:35 +00:00
|
|
|
|
|
|
|
/* unit flags to pass to parse_time_err */
|
|
|
|
#define TIME_UNIT_US 0x0000
|
|
|
|
#define TIME_UNIT_MS 0x0001
|
|
|
|
#define TIME_UNIT_S 0x0002
|
|
|
|
#define TIME_UNIT_MIN 0x0003
|
|
|
|
#define TIME_UNIT_HOUR 0x0004
|
|
|
|
#define TIME_UNIT_DAY 0x0005
|
|
|
|
#define TIME_UNIT_MASK 0x0007
|
|
|
|
|
2012-02-06 17:15:57 +00:00
|
|
|
#define SEC 1
|
|
|
|
#define MINUTE (60 * SEC)
|
|
|
|
#define HOUR (60 * MINUTE)
|
|
|
|
#define DAY (24 * HOUR)
|
|
|
|
|
2009-03-05 17:43:00 +00:00
|
|
|
/* Multiply the two 32-bit operands and shift the 64-bit result right 32 bits.
|
|
|
|
* This is used to compute fixed ratios by setting one of the operands to
|
|
|
|
* (2^32*ratio).
|
|
|
|
*/
|
|
|
|
static inline unsigned int mul32hi(unsigned int a, unsigned int b)
|
|
|
|
{
|
|
|
|
return ((unsigned long long)a * b) >> 32;
|
|
|
|
}
|
|
|
|
|
2010-06-20 05:12:37 +00:00
|
|
|
/* gcc does not know when it can safely divide 64 bits by 32 bits. Use this
|
|
|
|
* function when you know for sure that the result fits in 32 bits, because
|
|
|
|
* it is optimal on x86 and on 64bit processors.
|
|
|
|
*/
|
|
|
|
static inline unsigned int div64_32(unsigned long long o1, unsigned int o2)
|
|
|
|
{
|
|
|
|
unsigned int result;
|
|
|
|
#ifdef __i386__
|
|
|
|
asm("divl %2"
|
|
|
|
: "=a" (result)
|
|
|
|
: "A"(o1), "rm"(o2));
|
|
|
|
#else
|
|
|
|
result = o1 / o2;
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-07-02 07:00:17 +00:00
|
|
|
/* Simple popcountl implementation. It returns the number of ones in a word */
|
|
|
|
static inline unsigned int my_popcountl(unsigned long a)
|
2012-11-19 11:11:07 +00:00
|
|
|
{
|
|
|
|
unsigned int cnt;
|
|
|
|
for (cnt = 0; a; a >>= 1) {
|
|
|
|
if (a & 1)
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:33:41 +00:00
|
|
|
/* returns non-zero if <a> has at least 2 bits set */
|
|
|
|
static inline unsigned long atleast2(unsigned long a)
|
|
|
|
{
|
|
|
|
return a & (a - 1);
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:00:13 +00:00
|
|
|
/* Simple ffs implementation. It returns the position of the lowest bit set to
|
2018-10-10 17:05:56 +00:00
|
|
|
* one. It is illegal to call it with a==0 (undefined result).
|
|
|
|
*/
|
2017-11-22 14:00:13 +00:00
|
|
|
static inline unsigned int my_ffsl(unsigned long a)
|
|
|
|
{
|
2018-10-10 17:05:56 +00:00
|
|
|
unsigned long cnt;
|
2017-11-22 14:00:13 +00:00
|
|
|
|
2018-10-10 17:05:56 +00:00
|
|
|
#if defined(__x86_64__)
|
2018-10-29 15:09:57 +00:00
|
|
|
__asm__("bsf %1,%0\n" : "=r" (cnt) : "rm" (a));
|
2018-10-10 17:05:56 +00:00
|
|
|
cnt++;
|
|
|
|
#else
|
2017-11-22 14:00:13 +00:00
|
|
|
|
|
|
|
cnt = 1;
|
|
|
|
#if LONG_MAX > 0x7FFFFFFFL /* 64bits */
|
|
|
|
if (!(a & 0xFFFFFFFFUL)) {
|
|
|
|
a >>= 32;
|
|
|
|
cnt += 32;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!(a & 0XFFFFU)) {
|
|
|
|
a >>= 16;
|
|
|
|
cnt += 16;
|
|
|
|
}
|
|
|
|
if (!(a & 0XFF)) {
|
|
|
|
a >>= 8;
|
|
|
|
cnt += 8;
|
|
|
|
}
|
|
|
|
if (!(a & 0xf)) {
|
|
|
|
a >>= 4;
|
|
|
|
cnt += 4;
|
|
|
|
}
|
|
|
|
if (!(a & 0x3)) {
|
|
|
|
a >>= 2;
|
|
|
|
cnt += 2;
|
|
|
|
}
|
|
|
|
if (!(a & 0x1)) {
|
|
|
|
a >>= 1;
|
|
|
|
cnt += 1;
|
|
|
|
}
|
2018-10-10 17:05:56 +00:00
|
|
|
#endif /* x86_64 */
|
2017-11-22 14:00:13 +00:00
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2015-07-02 07:00:17 +00:00
|
|
|
/* Build a word with the <bits> lower bits set (reverse of my_popcountl) */
|
2013-01-18 10:29:29 +00:00
|
|
|
static inline unsigned long nbits(int bits)
|
|
|
|
{
|
|
|
|
if (--bits < 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (2UL << bits) - 1;
|
|
|
|
}
|
|
|
|
|
2013-12-03 16:50:47 +00:00
|
|
|
/*
|
|
|
|
* Parse binary string written in hexadecimal (source) and store the decoded
|
|
|
|
* result into binstr and set binstrlen to the lengh of binstr. Memory for
|
|
|
|
* binstr is allocated by the function. In case of error, returns 0 with an
|
|
|
|
* error message in err.
|
|
|
|
*/
|
|
|
|
int parse_binary(const char *source, char **binstr, int *binstrlen, char **err);
|
|
|
|
|
2009-05-10 13:41:18 +00:00
|
|
|
/* copies at most <n> characters from <src> and always terminates with '\0' */
|
|
|
|
char *my_strndup(const char *src, int n);
|
|
|
|
|
2013-10-06 21:24:13 +00:00
|
|
|
/*
|
|
|
|
* search needle in haystack
|
|
|
|
* returns the pointer if found, returns NULL otherwise
|
|
|
|
*/
|
|
|
|
const void *my_memmem(const void *, size_t, const void *, size_t);
|
|
|
|
|
2009-10-04 20:48:42 +00:00
|
|
|
/* This function returns the first unused key greater than or equal to <key> in
|
|
|
|
* ID tree <root>. Zero is returned if no place is found.
|
|
|
|
*/
|
|
|
|
unsigned int get_next_id(struct eb_root *root, unsigned int key);
|
|
|
|
|
2017-11-15 17:51:29 +00:00
|
|
|
/* dump the full tree to <file> in DOT format for debugging purposes. Will
|
|
|
|
* optionally highlight node <subj> if found, depending on operation <op> :
|
|
|
|
* 0 : nothing
|
|
|
|
* >0 : insertion, node/leaf are surrounded in red
|
|
|
|
* <0 : removal, node/leaf are dashed with no background
|
|
|
|
* Will optionally add "desc" as a label on the graph if set and non-null.
|
|
|
|
*/
|
|
|
|
void eb32sc_to_file(FILE *file, struct eb_root *root, const struct eb32sc_node *subj,
|
|
|
|
int op, const char *desc);
|
2017-11-15 14:04:05 +00:00
|
|
|
|
2010-01-18 14:05:57 +00:00
|
|
|
/* This function compares a sample word possibly followed by blanks to another
|
|
|
|
* clean word. The compare is case-insensitive. 1 is returned if both are equal,
|
|
|
|
* otherwise zero. This intends to be used when checking HTTP headers for some
|
|
|
|
* values.
|
|
|
|
*/
|
|
|
|
int word_match(const char *sample, int slen, const char *word, int wlen);
|
|
|
|
|
2010-06-06 11:22:23 +00:00
|
|
|
/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
|
|
|
|
* or the number of chars read in case of success.
|
|
|
|
*/
|
|
|
|
int buf2ip(const char *buf, size_t len, struct in_addr *dst);
|
2013-11-22 15:16:59 +00:00
|
|
|
int buf2ip6(const char *buf, size_t len, struct in6_addr *dst);
|
2010-06-06 11:22:23 +00:00
|
|
|
|
2010-06-14 17:09:21 +00:00
|
|
|
/* To be used to quote config arg positions. Returns the string at <ptr>
|
|
|
|
* surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
|
|
|
|
* if ptr is NULL or empty. The string is locally allocated.
|
|
|
|
*/
|
|
|
|
const char *quote_arg(const char *ptr);
|
|
|
|
|
2010-07-18 08:40:48 +00:00
|
|
|
/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
|
|
|
|
int get_std_op(const char *str);
|
|
|
|
|
2010-11-24 13:01:45 +00:00
|
|
|
/* hash a 32-bit integer to another 32-bit integer */
|
|
|
|
extern unsigned int full_hash(unsigned int a);
|
|
|
|
static inline unsigned int __full_hash(unsigned int a)
|
|
|
|
{
|
|
|
|
/* This function is one of Bob Jenkins' full avalanche hashing
|
|
|
|
* functions, which when provides quite a good distribution for little
|
|
|
|
* input variations. The result is quite suited to fit over a 32-bit
|
|
|
|
* space with enough variations so that a randomly picked number falls
|
|
|
|
* equally before any server position.
|
|
|
|
* Check http://burtleburtle.net/bob/hash/integer.html for more info.
|
|
|
|
*/
|
|
|
|
a = (a+0x7ed55d16) + (a<<12);
|
|
|
|
a = (a^0xc761c23c) ^ (a>>19);
|
|
|
|
a = (a+0x165667b1) + (a<<5);
|
|
|
|
a = (a+0xd3a2646c) ^ (a<<9);
|
|
|
|
a = (a+0xfd7046c5) + (a<<3);
|
|
|
|
a = (a^0xb55a4f09) ^ (a>>16);
|
|
|
|
|
|
|
|
/* ensure values are better spread all around the tree by multiplying
|
|
|
|
* by a large prime close to 3/4 of the tree.
|
|
|
|
*/
|
|
|
|
return a * 3221225473U;
|
|
|
|
}
|
|
|
|
|
2012-10-26 17:47:23 +00:00
|
|
|
/* sets the address family to AF_UNSPEC so that is_addr() does not match */
|
|
|
|
static inline void clear_addr(struct sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
addr->ss_family = AF_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2011-03-10 21:26:24 +00:00
|
|
|
/* returns non-zero if addr has a valid and non-null IPv4 or IPv6 address,
|
|
|
|
* otherwise zero.
|
|
|
|
*/
|
2014-05-09 20:40:55 +00:00
|
|
|
static inline int is_inet_addr(const struct sockaddr_storage *addr)
|
2011-03-10 21:26:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
2011-03-22 10:39:41 +00:00
|
|
|
return *(int *)&((struct sockaddr_in *)addr)->sin_addr;
|
2011-03-10 21:26:24 +00:00
|
|
|
case AF_INET6:
|
|
|
|
for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
|
|
|
|
if (((int *)&((struct sockaddr_in6 *)addr)->sin6_addr)[i] != 0)
|
|
|
|
return ((int *)&((struct sockaddr_in6 *)addr)->sin6_addr)[i];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-09 20:40:55 +00:00
|
|
|
|
|
|
|
/* returns non-zero if addr has a valid and non-null IPv4 or IPv6 address,
|
|
|
|
* or is a unix address, otherwise returns zero.
|
|
|
|
*/
|
|
|
|
static inline int is_addr(const struct sockaddr_storage *addr)
|
|
|
|
{
|
2018-09-11 14:51:29 +00:00
|
|
|
if (addr->ss_family == AF_UNIX || addr->ss_family == AF_CUST_SOCKPAIR)
|
2014-05-09 20:40:55 +00:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return is_inet_addr(addr);
|
|
|
|
}
|
2011-03-10 21:26:24 +00:00
|
|
|
|
2011-03-24 11:23:00 +00:00
|
|
|
/* returns port in network byte order */
|
|
|
|
static inline int get_net_port(struct sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return ((struct sockaddr_in *)addr)->sin_port;
|
|
|
|
case AF_INET6:
|
|
|
|
return ((struct sockaddr_in6 *)addr)->sin6_port;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns port in host byte order */
|
|
|
|
static inline int get_host_port(struct sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return ntohs(((struct sockaddr_in *)addr)->sin_port);
|
|
|
|
case AF_INET6:
|
|
|
|
return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-05 14:56:50 +00:00
|
|
|
/* returns address len for <addr>'s family, 0 for unknown families */
|
|
|
|
static inline int get_addr_len(const struct sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
case AF_UNIX:
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-24 11:23:00 +00:00
|
|
|
/* set port in host byte order */
|
|
|
|
static inline int set_net_port(struct sockaddr_storage *addr, int port)
|
|
|
|
{
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)addr)->sin_port = port;
|
2018-09-20 08:48:35 +00:00
|
|
|
break;
|
2011-03-24 11:23:00 +00:00
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)addr)->sin6_port = port;
|
2018-09-20 08:48:35 +00:00
|
|
|
break;
|
2011-03-24 11:23:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set port in network byte order */
|
|
|
|
static inline int set_host_port(struct sockaddr_storage *addr, int port)
|
|
|
|
{
|
|
|
|
switch (addr->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons(port);
|
2018-09-20 08:48:35 +00:00
|
|
|
break;
|
2011-03-24 11:23:00 +00:00
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
|
2018-09-20 08:48:35 +00:00
|
|
|
break;
|
2011-03-24 11:23:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:12:14 +00:00
|
|
|
/* Convert mask from bit length form to in_addr form.
|
|
|
|
* This function never fails.
|
|
|
|
*/
|
|
|
|
void len2mask4(int len, struct in_addr *addr);
|
|
|
|
|
|
|
|
/* Convert mask from bit length form to in6_addr form.
|
|
|
|
* This function never fails.
|
|
|
|
*/
|
|
|
|
void len2mask6(int len, struct in6_addr *addr);
|
|
|
|
|
2011-03-24 10:09:31 +00:00
|
|
|
/* Return true if IPv4 address is part of the network */
|
2016-07-13 09:59:39 +00:00
|
|
|
extern int in_net_ipv4(const void *addr, const struct in_addr *mask, const struct in_addr *net);
|
2011-03-24 10:09:31 +00:00
|
|
|
|
|
|
|
/* Return true if IPv6 address is part of the network */
|
2016-07-13 09:59:39 +00:00
|
|
|
extern int in_net_ipv6(const void *addr, const struct in6_addr *mask, const struct in6_addr *net);
|
2011-03-24 10:09:31 +00:00
|
|
|
|
2018-11-15 19:58:28 +00:00
|
|
|
/* Map IPv4 address on IPv6 address, as specified in RFC 3513. */
|
2011-03-24 10:09:31 +00:00
|
|
|
extern void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr);
|
|
|
|
|
2018-11-15 19:58:28 +00:00
|
|
|
/* Map IPv6 address on IPv4 address, as specified in RFC 3513.
|
2011-03-24 10:09:31 +00:00
|
|
|
* Return true if conversion is possible and false otherwise.
|
|
|
|
*/
|
|
|
|
extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr);
|
|
|
|
|
2016-01-23 22:39:12 +00:00
|
|
|
/* compare two struct sockaddr_storage and return:
|
|
|
|
* 0 (true) if the addr is the same in both
|
|
|
|
* 1 (false) if the addr is not the same in both
|
|
|
|
*/
|
|
|
|
int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2);
|
|
|
|
|
2016-01-30 23:27:17 +00:00
|
|
|
/* copy ip from <source> into <dest>
|
|
|
|
* the caller must clear <dest> before calling.
|
|
|
|
* Returns a pointer to the destination
|
|
|
|
*/
|
|
|
|
struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest);
|
|
|
|
|
2012-02-06 17:15:57 +00:00
|
|
|
char *human_time(int t, short hz_div);
|
|
|
|
|
|
|
|
extern const char *monthname[];
|
|
|
|
|
|
|
|
/* date2str_log: write a date in the format :
|
|
|
|
* sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
|
|
|
|
* tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
|
|
|
|
* tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
|
|
|
|
*
|
|
|
|
* without using sprintf. return a pointer to the last char written (\0) or
|
|
|
|
* NULL if there isn't enough space.
|
|
|
|
*/
|
2018-09-04 17:08:48 +00:00
|
|
|
char *date2str_log(char *dest, const struct tm *tm, const struct timeval *date, size_t size);
|
2012-02-06 17:15:57 +00:00
|
|
|
|
2016-03-27 09:08:03 +00:00
|
|
|
/* Return the GMT offset for a specific local time.
|
2016-03-27 01:04:16 +00:00
|
|
|
* Both t and tm must represent the same time.
|
2016-03-27 09:08:03 +00:00
|
|
|
* The string returned has the same format as returned by strftime(... "%z", tm).
|
|
|
|
* Offsets are kept in an internal cache for better performances.
|
|
|
|
*/
|
2016-03-27 01:04:16 +00:00
|
|
|
const char *get_gmt_offset(time_t t, struct tm *tm);
|
2016-03-27 09:08:03 +00:00
|
|
|
|
2012-02-06 17:15:57 +00:00
|
|
|
/* gmt2str_log: write a date in the format :
|
|
|
|
* "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
|
|
|
|
* return a pointer to the last char written (\0) or
|
|
|
|
* NULL if there isn't enough space.
|
|
|
|
*/
|
|
|
|
char *gmt2str_log(char *dst, struct tm *tm, size_t size);
|
|
|
|
|
2012-10-19 02:36:09 +00:00
|
|
|
/* localdate2str_log: write a date in the format :
|
|
|
|
* "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
|
2016-03-27 01:04:16 +00:00
|
|
|
* Both t and tm must represent the same time.
|
2012-10-19 02:36:09 +00:00
|
|
|
* return a pointer to the last char written (\0) or
|
|
|
|
* NULL if there isn't enough space.
|
|
|
|
*/
|
2016-03-27 01:04:16 +00:00
|
|
|
char *localdate2str_log(char *dst, time_t t, struct tm *tm, size_t size);
|
2012-10-19 02:36:09 +00:00
|
|
|
|
2016-01-20 17:49:45 +00:00
|
|
|
/* These 3 functions parses date string and fills the
|
|
|
|
* corresponding broken-down time in <tm>. In succes case,
|
|
|
|
* it returns 1, otherwise, it returns 0.
|
|
|
|
*/
|
|
|
|
int parse_http_date(const char *date, int len, struct tm *tm);
|
|
|
|
int parse_imf_date(const char *date, int len, struct tm *tm);
|
|
|
|
int parse_rfc850_date(const char *date, int len, struct tm *tm);
|
|
|
|
int parse_asctime_date(const char *date, int len, struct tm *tm);
|
|
|
|
|
2012-04-27 09:16:50 +00:00
|
|
|
/* Dynamically allocates a string of the proper length to hold the formatted
|
|
|
|
* output. NULL is returned on error. The caller is responsible for freeing the
|
|
|
|
* memory area using free(). The resulting string is returned in <out> if the
|
|
|
|
* pointer is not NULL. A previous version of <out> might be used to build the
|
|
|
|
* new string, and it will be freed before returning if it is not NULL, which
|
|
|
|
* makes it possible to build complex strings from iterative calls without
|
|
|
|
* having to care about freeing intermediate values, as in the example below :
|
|
|
|
*
|
|
|
|
* memprintf(&err, "invalid argument: '%s'", arg);
|
|
|
|
* ...
|
|
|
|
* memprintf(&err, "parser said : <%s>\n", *err);
|
|
|
|
* ...
|
|
|
|
* free(*err);
|
|
|
|
*
|
|
|
|
* This means that <err> must be initialized to NULL before first invocation.
|
|
|
|
* The return value also holds the allocated string, which eases error checking
|
|
|
|
* and immediate consumption. If the output pointer is not used, NULL must be
|
2012-09-20 17:43:14 +00:00
|
|
|
* passed instead and it will be ignored. The returned message will then also
|
|
|
|
* be NULL so that the caller does not have to bother with freeing anything.
|
2012-04-27 09:16:50 +00:00
|
|
|
*
|
|
|
|
* It is also convenient to use it without any free except the last one :
|
|
|
|
* err = NULL;
|
|
|
|
* if (!fct1(err)) report(*err);
|
|
|
|
* if (!fct2(err)) report(*err);
|
|
|
|
* if (!fct3(err)) report(*err);
|
|
|
|
* free(*err);
|
2017-10-24 09:25:33 +00:00
|
|
|
*
|
|
|
|
* memprintf relies on memvprintf. This last version can be called from any
|
|
|
|
* function with variadic arguments.
|
2012-04-27 09:16:50 +00:00
|
|
|
*/
|
2017-10-24 09:25:33 +00:00
|
|
|
char *memvprintf(char **out, const char *format, va_list args)
|
|
|
|
__attribute__ ((format(printf, 2, 0)));
|
|
|
|
|
2012-04-27 09:16:50 +00:00
|
|
|
char *memprintf(char **out, const char *format, ...)
|
|
|
|
__attribute__ ((format(printf, 2, 3)));
|
|
|
|
|
2012-09-14 09:40:36 +00:00
|
|
|
/* Used to add <level> spaces before each line of <out>, unless there is only one line.
|
|
|
|
* The input argument is automatically freed and reassigned. The result will have to be
|
|
|
|
* freed by the caller.
|
|
|
|
* Example of use :
|
|
|
|
* parse(cmd, &err); (callee: memprintf(&err, ...))
|
|
|
|
* fprintf(stderr, "Parser said: %s\n", indent_error(&err));
|
|
|
|
* free(err);
|
|
|
|
*/
|
|
|
|
char *indent_msg(char **out, int level);
|
2012-05-12 22:21:17 +00:00
|
|
|
|
2013-03-11 00:20:04 +00:00
|
|
|
/* Convert occurrences of environment variables in the input string to their
|
|
|
|
* corresponding value. A variable is identified as a series of alphanumeric
|
|
|
|
* characters or underscores following a '$' sign. The <in> string must be
|
|
|
|
* free()able. NULL returns NULL. The resulting string might be reallocated if
|
|
|
|
* some expansion is made.
|
|
|
|
*/
|
|
|
|
char *env_expand(char *in);
|
|
|
|
|
2012-05-12 22:21:17 +00:00
|
|
|
/* debugging macro to emit messages using write() on fd #-1 so that strace sees
|
|
|
|
* them.
|
|
|
|
*/
|
|
|
|
#define fddebug(msg...) do { char *_m = NULL; memprintf(&_m, ##msg); if (_m) write(-1, _m, strlen(_m)); free(_m); } while (0)
|
|
|
|
|
2016-12-04 23:10:57 +00:00
|
|
|
/* displays a <len> long memory block at <buf>, assuming first byte of <buf>
|
2017-04-27 16:03:20 +00:00
|
|
|
* has address <baseaddr>. String <pfx> may be placed as a prefix in front of
|
|
|
|
* each line. It may be NULL if unused. The output is emitted to file <out>.
|
2016-12-04 23:10:57 +00:00
|
|
|
*/
|
2017-04-27 16:03:20 +00:00
|
|
|
void debug_hexdump(FILE *out, const char *pfx, const char *buf, unsigned int baseaddr, int len);
|
2016-12-04 23:10:57 +00:00
|
|
|
|
2017-10-24 08:54:08 +00:00
|
|
|
/* this is used to emit traces when building with TRACE=1 */
|
|
|
|
__attribute__((format(printf, 1, 2)))
|
|
|
|
void trace(char *fmt, ...);
|
|
|
|
|
2013-12-13 14:14:55 +00:00
|
|
|
/* used from everywhere just to drain results we don't want to read and which
|
|
|
|
* recent versions of gcc increasingly and annoyingly complain about.
|
|
|
|
*/
|
|
|
|
extern int shut_your_big_mouth_gcc_int;
|
|
|
|
|
|
|
|
/* used from everywhere just to drain results we don't want to read and which
|
|
|
|
* recent versions of gcc increasingly and annoyingly complain about.
|
|
|
|
*/
|
|
|
|
static inline void shut_your_big_mouth_gcc(int r)
|
|
|
|
{
|
|
|
|
shut_your_big_mouth_gcc_int = r;
|
|
|
|
}
|
|
|
|
|
2013-04-15 17:27:10 +00:00
|
|
|
/* same as strstr() but case-insensitive */
|
|
|
|
const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2);
|
|
|
|
|
2018-09-10 16:41:28 +00:00
|
|
|
/* after increasing a pointer value, it can exceed the first buffer
|
|
|
|
* size. This function transform the value of <ptr> according with
|
|
|
|
* the expected position. <chunks> is an array of the one or two
|
2018-11-15 19:58:28 +00:00
|
|
|
* available chunks. The first value is the start of the first chunk,
|
2018-09-10 16:41:28 +00:00
|
|
|
* the second value if the end+1 of the first chunks. The third value
|
|
|
|
* is NULL or the start of the second chunk and the fourth value is
|
|
|
|
* the end+1 of the second chunk. The function returns 1 if does a
|
|
|
|
* wrap, else returns 0.
|
|
|
|
*/
|
|
|
|
static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
|
|
|
|
{
|
|
|
|
if (*ptr < chunks[1])
|
|
|
|
return 0;
|
|
|
|
if (!chunks[2])
|
|
|
|
return 0;
|
|
|
|
*ptr = chunks[2] + ( *ptr - chunks[1] );
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-28 22:04:39 +00:00
|
|
|
|
|
|
|
/************************* Composite address manipulation *********************
|
|
|
|
* Composite addresses are simply unsigned long data in which the higher bits
|
|
|
|
* represent a pointer, and the two lower bits are flags. There are several
|
|
|
|
* places where we just want to associate one or two flags to a pointer (eg,
|
|
|
|
* to type it), and these functions permit this. The pointer is necessarily a
|
|
|
|
* 32-bit aligned pointer, as its two lower bits will be cleared and replaced
|
|
|
|
* with the flags.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* Masks the two lower bits of a composite address and converts it to a
|
|
|
|
* pointer. This is used to mix some bits with some aligned pointers to
|
|
|
|
* structs and to retrieve the original (32-bit aligned) pointer.
|
|
|
|
*/
|
|
|
|
static inline void *caddr_to_ptr(unsigned long caddr)
|
|
|
|
{
|
|
|
|
return (void *)(caddr & ~3UL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only retrieves the two lower bits of a composite address. This is used to mix
|
|
|
|
* some bits with some aligned pointers to structs and to retrieve the original
|
|
|
|
* data (2 bits).
|
|
|
|
*/
|
|
|
|
static inline unsigned int caddr_to_data(unsigned long caddr)
|
|
|
|
{
|
|
|
|
return (caddr & 3UL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Combines the aligned pointer whose 2 lower bits will be masked with the bits
|
|
|
|
* from <data> to form a composite address. This is used to mix some bits with
|
|
|
|
* some aligned pointers to structs and to retrieve the original (32-bit aligned)
|
|
|
|
* pointer.
|
|
|
|
*/
|
|
|
|
static inline unsigned long caddr_from_ptr(void *ptr, unsigned int data)
|
|
|
|
{
|
|
|
|
return (((unsigned long)ptr) & ~3UL) + (data & 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sets the 2 bits of <data> in the <caddr> composite address */
|
|
|
|
static inline unsigned long caddr_set_flags(unsigned long caddr, unsigned int data)
|
|
|
|
{
|
|
|
|
return caddr | (data & 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clears the 2 bits of <data> in the <caddr> composite address */
|
|
|
|
static inline unsigned long caddr_clr_flags(unsigned long caddr, unsigned int data)
|
|
|
|
{
|
|
|
|
return caddr & ~(unsigned long)(data & 3);
|
|
|
|
}
|
|
|
|
|
MINOR: sample: add "json" converter
This converter escapes string to use it as json/ascii escaped string.
It can read UTF-8 with differents behavior on errors and encode it in
json/ascii.
json([<input-code>])
Escapes the input string and produces an ASCII ouput string ready to use as a
JSON string. The converter tries to decode the input string according to the
<input-code> parameter. It can be "ascii", "utf8", "utf8s", "utf8"" or
"utf8ps". The "ascii" decoder never fails. The "utf8" decoder detects 3 types
of errors:
- bad UTF-8 sequence (lone continuation byte, bad number of continuation
bytes, ...)
- invalid range (the decoded value is within a UTF-8 prohibited range),
- code overlong (the value is encoded with more bytes than necessary).
The UTF-8 JSON encoding can produce a "too long value" error when the UTF-8
character is greater than 0xffff because the JSON string escape specification
only authorizes 4 hex digits for the value encoding. The UTF-8 decoder exists
in 4 variants designated by a combination of two suffix letters : "p" for
"permissive" and "s" for "silently ignore". The behaviors of the decoders
are :
- "ascii" : never fails ;
- "utf8" : fails on any detected errors ;
- "utf8s" : never fails, but removes characters corresponding to errors ;
- "utf8p" : accepts and fixes the overlong errors, but fails on any other
error ;
- "utf8ps" : never fails, accepts and fixes the overlong errors, but removes
characters corresponding to the other errors.
This converter is particularly useful for building properly escaped JSON for
logging to servers which consume JSON-formated traffic logs.
Example:
capture request header user-agent len 150
capture request header Host len 15
log-format {"ip":"%[src]","user-agent":"%[capture.req.hdr(1),json]"}
Input request from client 127.0.0.1:
GET / HTTP/1.0
User-Agent: Very "Ugly" UA 1/2
Output log:
{"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"}
2014-08-12 08:20:47 +00:00
|
|
|
/* UTF-8 decoder status */
|
|
|
|
#define UTF8_CODE_OK 0x00
|
|
|
|
#define UTF8_CODE_OVERLONG 0x10
|
|
|
|
#define UTF8_CODE_INVRANGE 0x20
|
|
|
|
#define UTF8_CODE_BADSEQ 0x40
|
|
|
|
|
|
|
|
unsigned char utf8_next(const char *s, int len, unsigned int *c);
|
|
|
|
|
|
|
|
static inline unsigned char utf8_return_code(unsigned int code)
|
|
|
|
{
|
|
|
|
return code & 0xf0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned char utf8_return_length(unsigned char code)
|
|
|
|
{
|
|
|
|
return code & 0x0f;
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:47:18 +00:00
|
|
|
/* Turns 64-bit value <a> from host byte order to network byte order.
|
|
|
|
* The principle consists in letting the compiler detect we're playing
|
|
|
|
* with a union and simplify most or all operations. The asm-optimized
|
|
|
|
* htonl() version involving bswap (x86) / rev (arm) / other is a single
|
|
|
|
* operation on little endian, or a NOP on big-endian. In both cases,
|
|
|
|
* this lets the compiler "see" that we're rebuilding a 64-bit word from
|
|
|
|
* two 32-bit quantities that fit into a 32-bit register. In big endian,
|
|
|
|
* the whole code is optimized out. In little endian, with a decent compiler,
|
|
|
|
* a few bswap and 2 shifts are left, which is the minimum acceptable.
|
|
|
|
*/
|
2016-05-20 04:29:59 +00:00
|
|
|
static inline unsigned long long my_htonll(unsigned long long a)
|
2015-07-21 21:47:18 +00:00
|
|
|
{
|
2017-09-20 06:18:49 +00:00
|
|
|
#if defined(__x86_64__)
|
2017-10-18 09:39:33 +00:00
|
|
|
__asm__ volatile("bswap %0" : "=r"(a) : "0"(a));
|
2017-09-20 06:18:49 +00:00
|
|
|
return a;
|
|
|
|
#else
|
2015-07-21 21:47:18 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
unsigned int w1;
|
|
|
|
unsigned int w2;
|
|
|
|
} by32;
|
|
|
|
unsigned long long by64;
|
|
|
|
} w = { .by64 = a };
|
|
|
|
return ((unsigned long long)htonl(w.by32.w1) << 32) | htonl(w.by32.w2);
|
2017-09-20 06:18:49 +00:00
|
|
|
#endif
|
2015-07-21 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Turns 64-bit value <a> from network byte order to host byte order. */
|
2016-05-20 04:29:59 +00:00
|
|
|
static inline unsigned long long my_ntohll(unsigned long long a)
|
2015-07-21 21:47:18 +00:00
|
|
|
{
|
2016-05-20 04:29:59 +00:00
|
|
|
return my_htonll(a);
|
2015-07-21 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:13:35 +00:00
|
|
|
/* returns a 64-bit a timestamp with the finest resolution available. The
|
|
|
|
* unit is intentionally not specified. It's mostly used to compare dates.
|
|
|
|
*/
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
static inline unsigned long long rdtsc()
|
|
|
|
{
|
|
|
|
unsigned int a, d;
|
|
|
|
asm volatile("rdtsc" : "=a" (a), "=d" (d));
|
|
|
|
return a + ((unsigned long long)d << 32);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline unsigned long long rdtsc()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000000 + tv.tv_usec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-13 21:52:54 +00:00
|
|
|
/* append a copy of string <str> (in a wordlist) at the end of the list <li>
|
|
|
|
* On failure : return 0 and <err> filled with an error message.
|
|
|
|
* The caller is responsible for freeing the <err> and <str> copy
|
|
|
|
* memory area using free()
|
|
|
|
*/
|
|
|
|
struct list;
|
|
|
|
int list_append_word(struct list *li, const char *str, char **err);
|
|
|
|
|
2018-07-13 09:56:34 +00:00
|
|
|
int dump_text(struct buffer *out, const char *buf, int bsize);
|
|
|
|
int dump_binary(struct buffer *out, const char *buf, int bsize);
|
|
|
|
int dump_text_line(struct buffer *out, const char *buf, int bsize, int len,
|
2016-11-22 17:00:20 +00:00
|
|
|
int *line, int ptr);
|
|
|
|
|
2016-06-28 20:41:00 +00:00
|
|
|
/* same as realloc() except that ptr is also freed upon failure */
|
|
|
|
static inline void *my_realloc2(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = realloc(ptr, size);
|
|
|
|
if (!ret && size)
|
|
|
|
free(ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-12 21:42:07 +00:00
|
|
|
/* HAP_STRING() makes a string from a literal while HAP_XSTRING() first
|
|
|
|
* evaluates the argument and is suited to pass macros.
|
|
|
|
*
|
|
|
|
* They allow macros like PCRE_MAJOR to be defined without quotes, which
|
|
|
|
* is convenient for applications that want to test its value.
|
|
|
|
*/
|
|
|
|
#define HAP_STRING(...) #__VA_ARGS__
|
|
|
|
#define HAP_XSTRING(...) HAP_STRING(__VA_ARGS__)
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#endif /* _COMMON_STANDARD_H */
|