MINOR: logs: use the new bitmap functions instead of fd_sets for encoding maps

The fd_sets we've been using in the log encoding functions are not portable
and were shown to break at least under Cygwin. This patch gets rid of them
in favor of the new bitmap functions. It was verified with the config below
that the log output was exactly the same before and after the change :

    defaults
        mode http
        option httplog
        log stdout local0
        timeout client 1s
        timeout server 1s
        timeout connect 1s

    frontend foo
        bind :8001
        capture request header chars len 255

    backend bar
        option httpchk "GET" "/" "HTTP/1.0\r\nchars: \x01\x02\x03\x04\x05\x06\x07\x08\x09\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
        server foo 127.0.0.1:8001 check
This commit is contained in:
Willy Tarreau 2019-06-07 11:10:07 +02:00
parent 7355b040d1
commit 1bfd6020ce
3 changed files with 36 additions and 45 deletions

View File

@ -466,14 +466,14 @@ int addr_is_local(const struct netns_entry *ns,
*/
extern const char hextab[];
char *encode_string(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const char *string);
/*
* 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,
const char escape, const long *map,
const struct buffer *chunk);
/*
@ -485,7 +485,7 @@ char *encode_chunk(char *start, char *stop,
* completes.
*/
char *escape_string(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const char *string);
/*
@ -496,7 +496,7 @@ char *escape_string(char *start, char *stop,
* <stop>, and will return its position if the conversion completes.
*/
char *escape_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const struct buffer *chunk);

View File

@ -82,23 +82,18 @@ static const struct log_fmt log_formats[LOG_FORMATS] = {
},
};
#define FD_SETS_ARE_BITFIELDS
#ifdef FD_SETS_ARE_BITFIELDS
/*
* This map is used with all the FD_* macros to check whether a particular bit
* is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
* which should be escaped. When FD_ISSET() returns non-zero, it means that the
* byte should be escaped. Be careful to always pass bytes from 0 to 255
* exclusively to the macros.
* is set or not. Each bit represents an ACSII code. ha_bit_set() sets those
* bytes which should be escaped. When ha_bit_test() returns non-zero, it means
* that the byte should be escaped. Be careful to always pass bytes from 0 to
* 255 exclusively to the macros.
*/
fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
long rfc5424_escape_map[(256/8) / sizeof(long)];
long hdr_encode_map[(256/8) / sizeof(long)];
long url_encode_map[(256/8) / sizeof(long)];
long http_encode_map[(256/8) / sizeof(long)];
#else
#error "Check if your OS uses bitfields for fd_sets"
#endif
const char *log_facilities[NB_LOG_FACILITIES] = {
"kern", "user", "mail", "daemon",
@ -1153,7 +1148,7 @@ int get_log_facility(const char *fac)
* <escape>.
*/
static char *lf_encode_string(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const char *string,
struct logformat_node *node)
{
@ -1161,8 +1156,8 @@ static char *lf_encode_string(char *start, char *stop,
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && *string != '\0') {
if (!FD_ISSET((unsigned char)(*string), map)) {
if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map))
if (!ha_bit_test((unsigned char)(*string), map)) {
if (!ha_bit_test((unsigned char)(*string), rfc5424_escape_map))
*start++ = *string;
else {
if (start + 2 >= stop)
@ -1198,7 +1193,7 @@ static char *lf_encode_string(char *start, char *stop,
* <escape>.
*/
static char *lf_encode_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const struct buffer *chunk,
struct logformat_node *node)
{
@ -1211,8 +1206,8 @@ static char *lf_encode_chunk(char *start, char *stop,
stop--; /* reserve one byte for the final '\0' */
while (start < stop && str < end) {
if (!FD_ISSET((unsigned char)(*str), map)) {
if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map))
if (!ha_bit_test((unsigned char)(*str), map)) {
if (!ha_bit_test((unsigned char)(*str), rfc5424_escape_map))
*start++ = *str;
else {
if (start + 2 >= stop)
@ -1765,10 +1760,6 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd
}
}
extern fd_set hdr_encode_map[];
extern fd_set url_encode_map[];
extern fd_set http_encode_map[];
const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */
const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
@ -1802,7 +1793,7 @@ static void init_log()
tmp = "\"\\]";
while (*tmp) {
FD_SET(*tmp, rfc5424_escape_map);
ha_bit_set(*tmp, rfc5424_escape_map);
tmp++;
}
@ -1814,23 +1805,23 @@ static void init_log()
memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
memset(url_encode_map, 0, sizeof(url_encode_map));
for (i = 0; i < 32; i++) {
FD_SET(i, hdr_encode_map);
FD_SET(i, url_encode_map);
ha_bit_set(i, hdr_encode_map);
ha_bit_set(i, url_encode_map);
}
for (i = 127; i < 256; i++) {
FD_SET(i, hdr_encode_map);
FD_SET(i, url_encode_map);
ha_bit_set(i, hdr_encode_map);
ha_bit_set(i, url_encode_map);
}
tmp = "\"#{|}";
while (*tmp) {
FD_SET(*tmp, hdr_encode_map);
ha_bit_set(*tmp, hdr_encode_map);
tmp++;
}
tmp = "\"#";
while (*tmp) {
FD_SET(*tmp, url_encode_map);
ha_bit_set(*tmp, url_encode_map);
tmp++;
}
@ -1855,10 +1846,10 @@ static void init_log()
*/
memset(http_encode_map, 0, sizeof(http_encode_map));
for (i = 0x00; i <= 0x08; i++)
FD_SET(i, http_encode_map);
ha_bit_set(i, http_encode_map);
for (i = 0x0a; i <= 0x1f; i++)
FD_SET(i, http_encode_map);
FD_SET(0x7f, http_encode_map);
ha_bit_set(i, http_encode_map);
ha_bit_set(0x7f, http_encode_map);
}
INITCALL0(STG_PREPARE, init_log);

View File

@ -1546,13 +1546,13 @@ int addr_is_local(const struct netns_entry *ns,
*/
const char hextab[16] = "0123456789ABCDEF";
char *encode_string(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const char *string)
{
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && *string != '\0') {
if (!FD_ISSET((unsigned char)(*string), map))
if (!ha_bit_test((unsigned char)(*string), map))
*start++ = *string;
else {
if (start + 3 >= stop)
@ -1573,7 +1573,7 @@ char *encode_string(char *start, char *stop,
* <chunk> instead of a string.
*/
char *encode_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const struct buffer *chunk)
{
char *str = chunk->area;
@ -1582,7 +1582,7 @@ char *encode_chunk(char *start, char *stop,
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && str < end) {
if (!FD_ISSET((unsigned char)(*str), map))
if (!ha_bit_test((unsigned char)(*str), map))
*start++ = *str;
else {
if (start + 3 >= stop)
@ -1607,13 +1607,13 @@ char *encode_chunk(char *start, char *stop,
* completes.
*/
char *escape_string(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const char *string)
{
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && *string != '\0') {
if (!FD_ISSET((unsigned char)(*string), map))
if (!ha_bit_test((unsigned char)(*string), map))
*start++ = *string;
else {
if (start + 2 >= stop)
@ -1636,7 +1636,7 @@ char *escape_string(char *start, char *stop,
* <stop>, and will return its position if the conversion completes.
*/
char *escape_chunk(char *start, char *stop,
const char escape, const fd_set *map,
const char escape, const long *map,
const struct buffer *chunk)
{
char *str = chunk->area;
@ -1645,7 +1645,7 @@ char *escape_chunk(char *start, char *stop,
if (start < stop) {
stop--; /* reserve one byte for the final '\0' */
while (start < stop && str < end) {
if (!FD_ISSET((unsigned char)(*str), map))
if (!ha_bit_test((unsigned char)(*str), map))
*start++ = *str;
else {
if (start + 2 >= stop)