blob: 14996c8d85f831200ae811959e35c91c944c5a39 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau17f449b2010-11-07 11:44:13 +01002 * 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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_COMPAT_H
23#define _COMMON_COMPAT_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Joris Dedieu9dd44ba2015-06-13 01:05:11 +020025#include <limits.h>
Willy Tarreaub81939c2019-05-20 20:18:26 +020026#include <time.h>
27#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028/* This is needed on Linux for Netfilter includes */
Willy Tarreau17f449b2010-11-07 11:44:13 +010029#include <sys/param.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010030#include <sys/types.h>
Willy Tarreau0e996c62010-02-26 22:00:19 +010031#include <sys/socket.h>
Willy Tarreau17f449b2010-11-07 11:44:13 +010032#include <arpa/inet.h>
Joris Dedieu9dd44ba2015-06-13 01:05:11 +020033#include <netinet/in.h>
Willy Tarreau9d87ca02016-08-10 21:09:24 +020034#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau9a85a172019-05-21 19:48:44 +020036/* 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 */
40typedef struct { } empty_t;
41
Willy Tarreaue3e2b722019-05-20 19:10:52 +020042// 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 Tarreau177e2b02008-07-15 00:36:31 +020056#ifndef BITS_PER_INT
57#define BITS_PER_INT (8*sizeof(int))
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
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 Tarreaud6d06902009-08-19 11:22:33 +020073/* only Linux defines it */
74#ifndef MSG_NOSIGNAL
75#define MSG_NOSIGNAL 0
76#endif
77
Willy Tarreauc8f24f82007-11-30 18:38:35 +010078/* 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 Tarreau6db06d32009-08-19 11:14:11 +020085/* Only Linux defines MSG_MORE */
86#ifndef MSG_MORE
87#define MSG_MORE 0
88#endif
89
Willy Tarreau2b57cb82013-06-10 19:56:38 +020090/* 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 */
Willy Tarreaue5733232019-05-22 19:24:06 +020093#ifdef USE_NETFILTER
Willy Tarreau2b57cb82013-06-10 19:56:38 +020094#define MSG_TRUNC_CLEARS_INPUT
95#endif
96
Willy Tarreau17f449b2010-11-07 11:44:13 +010097/* Maximum path length, OS-dependant */
98#ifndef MAXPATHLEN
99#define MAXPATHLEN 128
100#endif
101
Willy Tarreaubd9a0a72011-10-23 21:14:29 +0200102/* On Linux, allows pipes to be resized */
103#ifndef F_SETPIPE_SZ
104#define F_SETPIPE_SZ (1024 + 7)
105#endif
106
Willy Tarreau430f5902019-05-21 20:01:26 +0200107/* systems without such defines do not know clockid_t or timer_t */
Willy Tarreau823bda02019-05-22 19:12:54 +0200108#if !(_POSIX_TIMERS > 0)
Willy Tarreau80daaa12019-05-21 19:58:16 +0200109#undef clockid_t
110#define clockid_t empty_t
Willy Tarreau430f5902019-05-21 20:01:26 +0200111#undef timer_t
112#define timer_t empty_t
113#endif
114
115/* define a dummy value to designate "no timer". Use only 32 bits. */
116#ifndef TIMER_INVALID
117#define TIMER_INVALID ((timer_t)(unsigned long)(0xfffffffful))
Willy Tarreaub81939c2019-05-20 20:18:26 +0200118#endif
119
Willy Tarreaue5733232019-05-22 19:24:06 +0200120#if defined(USE_TPROXY) && defined(USE_NETFILTER)
Jeremy Hinegardnere7dd2f22008-04-21 07:34:31 +0200121#include <linux/types.h>
122#include <linux/netfilter_ipv6.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123#include <linux/netfilter_ipv4.h>
124#endif
125
Willy Tarreau0a459892008-01-13 17:37:16 +0100126/* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */
Willy Tarreaue5733232019-05-22 19:24:06 +0200127#if defined(USE_LINUX_TPROXY)
Willy Tarreau0a459892008-01-13 17:37:16 +0100128#if !defined(IP_FREEBIND)
129#define IP_FREEBIND 15
130#endif /* !IP_FREEBIND */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100131#if !defined(IP_TRANSPARENT)
132#define IP_TRANSPARENT 19
133#endif /* !IP_TRANSPARENT */
David du Colombier65c17962012-07-13 14:34:59 +0200134#if !defined(IPV6_TRANSPARENT)
135#define IPV6_TRANSPARENT 75
136#endif /* !IPV6_TRANSPARENT */
Willy Tarreaue5733232019-05-22 19:24:06 +0200137#endif /* USE_LINUX_TPROXY */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100138
Pieter Baauwd551fb52013-05-08 22:49:23 +0200139#if defined(IP_FREEBIND) \
Pieter Baauwff30b662013-05-08 23:22:39 +0200140 || defined(IP_BINDANY) \
141 || defined(IPV6_BINDANY) \
Pieter Baauw1eb75922013-05-08 23:30:23 +0200142 || defined(SO_BINDANY) \
Pieter Baauwd551fb52013-05-08 22:49:23 +0200143 || defined(IP_TRANSPARENT) \
144 || defined(IPV6_TRANSPARENT)
145#define CONFIG_HAP_TRANSPARENT
146#endif
147
Willy Tarreau58b2f832006-11-13 01:22:38 +0100148/* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined.
149 * There are two families of values depending on the architecture. Those
150 * are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the
Willy Tarreaue5733232019-05-22 19:24:06 +0200151 * USE_NETFILTER define.
Willy Tarreau58b2f832006-11-13 01:22:38 +0100152 */
Willy Tarreaue5733232019-05-22 19:24:06 +0200153#if !defined(SO_REUSEPORT) && defined(USE_NETFILTER)
Willy Tarreau58b2f832006-11-13 01:22:38 +0100154#if (SO_REUSEADDR == 2)
155#define SO_REUSEPORT 15
156#elif (SO_REUSEADDR == 0x0004)
157#define SO_REUSEPORT 0x0200
158#endif /* SO_REUSEADDR */
159#endif /* SO_REUSEPORT */
160
Lukas Tribus0999f762013-04-02 16:43:24 +0200161/* only Linux defines TCP_FASTOPEN */
162#ifdef USE_TFO
163#ifndef TCP_FASTOPEN
164#define TCP_FASTOPEN 23
165#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100166
167#ifndef TCP_FASTOPEN_CONNECT
168#define TCP_FASTOPEN_CONNECT 30
169#endif
Lukas Tribus0999f762013-04-02 16:43:24 +0200170#endif
171
Willy Tarreauae459f32015-09-29 18:19:32 +0200172/* FreeBSD doesn't define SOL_IP and prefers IPPROTO_IP */
173#ifndef SOL_IP
174#define SOL_IP IPPROTO_IP
175#endif
176
Willy Tarreau9d87ca02016-08-10 21:09:24 +0200177/* same for SOL_TCP */
178#ifndef SOL_TCP
179#define SOL_TCP IPPROTO_TCP
180#endif
181
Lukas Tribus67db8df2013-06-23 17:37:13 +0200182/* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */
183#if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED)
184#define IN6_IS_ADDR_V4MAPPED(a) \
185((((const uint32_t *) (a))[0] == 0) \
186&& (((const uint32_t *) (a))[1] == 0) \
187&& (((const uint32_t *) (a))[2] == htonl (0xffff)))
188#endif
189
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190#if defined(__dietlibc__)
191#include <strings.h>
192#endif
193
Willy Tarreau4698adf2018-10-29 19:14:14 +0100194/* crypt_r() has been present in glibc since 2.2 and on FreeBSD since 12.0
195 * (12000002). No other OS makes any mention of it for now. Feel free to add
196 * valid known combinations below if needed to relax the crypt() lock when
197 * using threads.
198 */
199#if (defined(__GNU_LIBRARY__) && (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) \
200 || (defined(__FreeBSD__) && __FreeBSD_version >= 1200002)
201#define HA_HAVE_CRYPT_R
202#endif
203
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200204#endif /* _COMMON_COMPAT_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
206/*
207 * Local variables:
208 * c-indent-level: 8
209 * c-basic-offset: 8
210 * End:
211 */