Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 2 | * include/common/compat.h |
| 3 | * Operating system compatibility interface. |
| 4 | * |
| 5 | * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_COMPAT_H |
| 23 | #define _COMMON_COMPAT_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Joris Dedieu | 9dd44ba | 2015-06-13 01:05:11 +0200 | [diff] [blame] | 25 | #include <limits.h> |
Willy Tarreau | b81939c | 2019-05-20 20:18:26 +0200 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | /* This is needed on Linux for Netfilter includes */ |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 29 | #include <sys/param.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 30 | #include <sys/types.h> |
Willy Tarreau | 0e996c6 | 2010-02-26 22:00:19 +0100 | [diff] [blame] | 31 | #include <sys/socket.h> |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Joris Dedieu | 9dd44ba | 2015-06-13 01:05:11 +0200 | [diff] [blame] | 33 | #include <netinet/in.h> |
Willy Tarreau | 9d87ca0 | 2016-08-10 21:09:24 +0200 | [diff] [blame] | 34 | #include <netinet/tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 9a85a17 | 2019-05-21 19:48:44 +0200 | [diff] [blame] | 36 | /* set any optional field in a struct to this type to save ifdefs. Its address |
| 37 | * will still be valid but it will not reserve any room nor require any |
| 38 | * initialization. |
| 39 | */ |
| 40 | typedef struct { } empty_t; |
| 41 | |
Willy Tarreau | e3e2b72 | 2019-05-20 19:10:52 +0200 | [diff] [blame] | 42 | // Redefine some limits that are not present everywhere |
| 43 | #ifndef LLONG_MAX |
| 44 | # define LLONG_MAX 9223372036854775807LL |
| 45 | # define LLONG_MIN (-LLONG_MAX - 1LL) |
| 46 | #endif |
| 47 | |
| 48 | #ifndef ULLONG_MAX |
| 49 | # define ULLONG_MAX (LLONG_MAX * 2ULL + 1) |
| 50 | #endif |
| 51 | |
| 52 | #ifndef LONGBITS |
| 53 | #define LONGBITS ((unsigned int)sizeof(long) * 8) |
| 54 | #endif |
| 55 | |
Willy Tarreau | 177e2b0 | 2008-07-15 00:36:31 +0200 | [diff] [blame] | 56 | #ifndef BITS_PER_INT |
| 57 | #define BITS_PER_INT (8*sizeof(int)) |
| 58 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | |
| 60 | /* this is for libc5 for example */ |
| 61 | #ifndef TCP_NODELAY |
| 62 | #define TCP_NODELAY 1 |
| 63 | #endif |
| 64 | |
| 65 | #ifndef SHUT_RD |
| 66 | #define SHUT_RD 0 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef SHUT_WR |
| 70 | #define SHUT_WR 1 |
| 71 | #endif |
| 72 | |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 73 | /* only Linux defines it */ |
| 74 | #ifndef MSG_NOSIGNAL |
| 75 | #define MSG_NOSIGNAL 0 |
| 76 | #endif |
| 77 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 78 | /* AIX does not define MSG_DONTWAIT. We'll define it to zero, and test it |
| 79 | * wherever appropriate. |
| 80 | */ |
| 81 | #ifndef MSG_DONTWAIT |
| 82 | #define MSG_DONTWAIT 0 |
| 83 | #endif |
| 84 | |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 85 | /* Only Linux defines MSG_MORE */ |
| 86 | #ifndef MSG_MORE |
| 87 | #define MSG_MORE 0 |
| 88 | #endif |
| 89 | |
Willy Tarreau | 2b57cb8 | 2013-06-10 19:56:38 +0200 | [diff] [blame] | 90 | /* On Linux 2.4 and above, MSG_TRUNC can be used on TCP sockets to drop any |
| 91 | * pending data. Let's rely on NETFILTER to detect if this is supported. |
| 92 | */ |
| 93 | #ifdef NETFILTER |
| 94 | #define MSG_TRUNC_CLEARS_INPUT |
| 95 | #endif |
| 96 | |
Willy Tarreau | 17f449b | 2010-11-07 11:44:13 +0100 | [diff] [blame] | 97 | /* Maximum path length, OS-dependant */ |
| 98 | #ifndef MAXPATHLEN |
| 99 | #define MAXPATHLEN 128 |
| 100 | #endif |
| 101 | |
Willy Tarreau | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 102 | /* On Linux, allows pipes to be resized */ |
| 103 | #ifndef F_SETPIPE_SZ |
| 104 | #define F_SETPIPE_SZ (1024 + 7) |
| 105 | #endif |
| 106 | |
Willy Tarreau | b81939c | 2019-05-20 20:18:26 +0200 | [diff] [blame] | 107 | /* systems without such defines do not know clockid_t */ |
Willy Tarreau | f617824 | 2019-05-21 19:46:58 +0200 | [diff] [blame] | 108 | #if !(_POSIX_TIMERS > 0) || (_POSIX_C_SOURCE < 199309L) |
Willy Tarreau | 80daaa1 | 2019-05-21 19:58:16 +0200 | [diff] [blame] | 109 | #undef clockid_t |
| 110 | #define clockid_t empty_t |
Willy Tarreau | b81939c | 2019-05-20 20:18:26 +0200 | [diff] [blame] | 111 | #endif |
| 112 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | #if defined(TPROXY) && defined(NETFILTER) |
Jeremy Hinegardner | e7dd2f2 | 2008-04-21 07:34:31 +0200 | [diff] [blame] | 114 | #include <linux/types.h> |
| 115 | #include <linux/netfilter_ipv6.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | #include <linux/netfilter_ipv4.h> |
| 117 | #endif |
| 118 | |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 119 | /* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 120 | #if defined(CONFIG_HAP_LINUX_TPROXY) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 121 | #if !defined(IP_FREEBIND) |
| 122 | #define IP_FREEBIND 15 |
| 123 | #endif /* !IP_FREEBIND */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 124 | #if !defined(IP_TRANSPARENT) |
| 125 | #define IP_TRANSPARENT 19 |
| 126 | #endif /* !IP_TRANSPARENT */ |
David du Colombier | 65c1796 | 2012-07-13 14:34:59 +0200 | [diff] [blame] | 127 | #if !defined(IPV6_TRANSPARENT) |
| 128 | #define IPV6_TRANSPARENT 75 |
| 129 | #endif /* !IPV6_TRANSPARENT */ |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 130 | #endif /* CONFIG_HAP_LINUX_TPROXY */ |
| 131 | |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 132 | #if defined(IP_FREEBIND) \ |
Pieter Baauw | ff30b66 | 2013-05-08 23:22:39 +0200 | [diff] [blame] | 133 | || defined(IP_BINDANY) \ |
| 134 | || defined(IPV6_BINDANY) \ |
Pieter Baauw | 1eb7592 | 2013-05-08 23:30:23 +0200 | [diff] [blame] | 135 | || defined(SO_BINDANY) \ |
Pieter Baauw | d551fb5 | 2013-05-08 22:49:23 +0200 | [diff] [blame] | 136 | || defined(IP_TRANSPARENT) \ |
| 137 | || defined(IPV6_TRANSPARENT) |
| 138 | #define CONFIG_HAP_TRANSPARENT |
| 139 | #endif |
| 140 | |
Willy Tarreau | 58b2f83 | 2006-11-13 01:22:38 +0100 | [diff] [blame] | 141 | /* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined. |
| 142 | * There are two families of values depending on the architecture. Those |
| 143 | * are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the |
| 144 | * NETFILTER define. |
| 145 | */ |
| 146 | #if !defined(SO_REUSEPORT) && defined(NETFILTER) |
| 147 | #if (SO_REUSEADDR == 2) |
| 148 | #define SO_REUSEPORT 15 |
| 149 | #elif (SO_REUSEADDR == 0x0004) |
| 150 | #define SO_REUSEPORT 0x0200 |
| 151 | #endif /* SO_REUSEADDR */ |
| 152 | #endif /* SO_REUSEPORT */ |
| 153 | |
Lukas Tribus | 0999f76 | 2013-04-02 16:43:24 +0200 | [diff] [blame] | 154 | /* only Linux defines TCP_FASTOPEN */ |
| 155 | #ifdef USE_TFO |
| 156 | #ifndef TCP_FASTOPEN |
| 157 | #define TCP_FASTOPEN 23 |
| 158 | #endif |
Willy Tarreau | 034c88c | 2017-01-23 23:36:45 +0100 | [diff] [blame] | 159 | |
| 160 | #ifndef TCP_FASTOPEN_CONNECT |
| 161 | #define TCP_FASTOPEN_CONNECT 30 |
| 162 | #endif |
Lukas Tribus | 0999f76 | 2013-04-02 16:43:24 +0200 | [diff] [blame] | 163 | #endif |
| 164 | |
Willy Tarreau | ae459f3 | 2015-09-29 18:19:32 +0200 | [diff] [blame] | 165 | /* FreeBSD doesn't define SOL_IP and prefers IPPROTO_IP */ |
| 166 | #ifndef SOL_IP |
| 167 | #define SOL_IP IPPROTO_IP |
| 168 | #endif |
| 169 | |
Willy Tarreau | 9d87ca0 | 2016-08-10 21:09:24 +0200 | [diff] [blame] | 170 | /* same for SOL_TCP */ |
| 171 | #ifndef SOL_TCP |
| 172 | #define SOL_TCP IPPROTO_TCP |
| 173 | #endif |
| 174 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 175 | /* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */ |
| 176 | #if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED) |
| 177 | #define IN6_IS_ADDR_V4MAPPED(a) \ |
| 178 | ((((const uint32_t *) (a))[0] == 0) \ |
| 179 | && (((const uint32_t *) (a))[1] == 0) \ |
| 180 | && (((const uint32_t *) (a))[2] == htonl (0xffff))) |
| 181 | #endif |
| 182 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | #if defined(__dietlibc__) |
| 184 | #include <strings.h> |
| 185 | #endif |
| 186 | |
Willy Tarreau | 4698adf | 2018-10-29 19:14:14 +0100 | [diff] [blame] | 187 | /* crypt_r() has been present in glibc since 2.2 and on FreeBSD since 12.0 |
| 188 | * (12000002). No other OS makes any mention of it for now. Feel free to add |
| 189 | * valid known combinations below if needed to relax the crypt() lock when |
| 190 | * using threads. |
| 191 | */ |
| 192 | #if (defined(__GNU_LIBRARY__) && (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) \ |
| 193 | || (defined(__FreeBSD__) && __FreeBSD_version >= 1200002) |
| 194 | #define HA_HAVE_CRYPT_R |
| 195 | #endif |
| 196 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 197 | #endif /* _COMMON_COMPAT_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Local variables: |
| 201 | * c-indent-level: 8 |
| 202 | * c-basic-offset: 8 |
| 203 | * End: |
| 204 | */ |