blob: d43b89dd614f68b186d1d7f8214a30d7ae2574e4 [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 Tarreaue3e2b722019-05-20 19:10:52 +020036// Redefine some limits that are not present everywhere
37#ifndef LLONG_MAX
38# define LLONG_MAX 9223372036854775807LL
39# define LLONG_MIN (-LLONG_MAX - 1LL)
40#endif
41
42#ifndef ULLONG_MAX
43# define ULLONG_MAX (LLONG_MAX * 2ULL + 1)
44#endif
45
46#ifndef LONGBITS
47#define LONGBITS ((unsigned int)sizeof(long) * 8)
48#endif
49
Willy Tarreau177e2b02008-07-15 00:36:31 +020050#ifndef BITS_PER_INT
51#define BITS_PER_INT (8*sizeof(int))
52#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
54/* this is for libc5 for example */
55#ifndef TCP_NODELAY
56#define TCP_NODELAY 1
57#endif
58
59#ifndef SHUT_RD
60#define SHUT_RD 0
61#endif
62
63#ifndef SHUT_WR
64#define SHUT_WR 1
65#endif
66
Willy Tarreaud6d06902009-08-19 11:22:33 +020067/* only Linux defines it */
68#ifndef MSG_NOSIGNAL
69#define MSG_NOSIGNAL 0
70#endif
71
Willy Tarreauc8f24f82007-11-30 18:38:35 +010072/* AIX does not define MSG_DONTWAIT. We'll define it to zero, and test it
73 * wherever appropriate.
74 */
75#ifndef MSG_DONTWAIT
76#define MSG_DONTWAIT 0
77#endif
78
Willy Tarreau6db06d32009-08-19 11:14:11 +020079/* Only Linux defines MSG_MORE */
80#ifndef MSG_MORE
81#define MSG_MORE 0
82#endif
83
Willy Tarreau2b57cb82013-06-10 19:56:38 +020084/* On Linux 2.4 and above, MSG_TRUNC can be used on TCP sockets to drop any
85 * pending data. Let's rely on NETFILTER to detect if this is supported.
86 */
87#ifdef NETFILTER
88#define MSG_TRUNC_CLEARS_INPUT
89#endif
90
Willy Tarreau17f449b2010-11-07 11:44:13 +010091/* Maximum path length, OS-dependant */
92#ifndef MAXPATHLEN
93#define MAXPATHLEN 128
94#endif
95
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020096/* On Linux, allows pipes to be resized */
97#ifndef F_SETPIPE_SZ
98#define F_SETPIPE_SZ (1024 + 7)
99#endif
100
Willy Tarreaub81939c2019-05-20 20:18:26 +0200101/* systems without such defines do not know clockid_t */
102#if !defined(_POSIX_TIMERS) || (_POSIX_C_SOURCE < 199309L)
103#define clockid_t int
104#undef CLOCK_REALTIME
105#undef CLOCK_MONOTONIC
106#undef CLOCK_THREAD_CPUTIME_ID
107#define CLOCK_REALTIME 0
108#define CLOCK_MONOTONIC 1
109#define CLOCK_THREAD_CPUTIME_ID 2
110#endif
111
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112#if defined(TPROXY) && defined(NETFILTER)
Jeremy Hinegardnere7dd2f22008-04-21 07:34:31 +0200113#include <linux/types.h>
114#include <linux/netfilter_ipv6.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115#include <linux/netfilter_ipv4.h>
116#endif
117
Willy Tarreau0a459892008-01-13 17:37:16 +0100118/* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100119#if defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau0a459892008-01-13 17:37:16 +0100120#if !defined(IP_FREEBIND)
121#define IP_FREEBIND 15
122#endif /* !IP_FREEBIND */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100123#if !defined(IP_TRANSPARENT)
124#define IP_TRANSPARENT 19
125#endif /* !IP_TRANSPARENT */
David du Colombier65c17962012-07-13 14:34:59 +0200126#if !defined(IPV6_TRANSPARENT)
127#define IPV6_TRANSPARENT 75
128#endif /* !IPV6_TRANSPARENT */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100129#endif /* CONFIG_HAP_LINUX_TPROXY */
130
Pieter Baauwd551fb52013-05-08 22:49:23 +0200131#if defined(IP_FREEBIND) \
Pieter Baauwff30b662013-05-08 23:22:39 +0200132 || defined(IP_BINDANY) \
133 || defined(IPV6_BINDANY) \
Pieter Baauw1eb75922013-05-08 23:30:23 +0200134 || defined(SO_BINDANY) \
Pieter Baauwd551fb52013-05-08 22:49:23 +0200135 || defined(IP_TRANSPARENT) \
136 || defined(IPV6_TRANSPARENT)
137#define CONFIG_HAP_TRANSPARENT
138#endif
139
Willy Tarreau58b2f832006-11-13 01:22:38 +0100140/* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined.
141 * There are two families of values depending on the architecture. Those
142 * are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the
143 * NETFILTER define.
144 */
145#if !defined(SO_REUSEPORT) && defined(NETFILTER)
146#if (SO_REUSEADDR == 2)
147#define SO_REUSEPORT 15
148#elif (SO_REUSEADDR == 0x0004)
149#define SO_REUSEPORT 0x0200
150#endif /* SO_REUSEADDR */
151#endif /* SO_REUSEPORT */
152
Lukas Tribus0999f762013-04-02 16:43:24 +0200153/* only Linux defines TCP_FASTOPEN */
154#ifdef USE_TFO
155#ifndef TCP_FASTOPEN
156#define TCP_FASTOPEN 23
157#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100158
159#ifndef TCP_FASTOPEN_CONNECT
160#define TCP_FASTOPEN_CONNECT 30
161#endif
Lukas Tribus0999f762013-04-02 16:43:24 +0200162#endif
163
Willy Tarreauae459f32015-09-29 18:19:32 +0200164/* FreeBSD doesn't define SOL_IP and prefers IPPROTO_IP */
165#ifndef SOL_IP
166#define SOL_IP IPPROTO_IP
167#endif
168
Willy Tarreau9d87ca02016-08-10 21:09:24 +0200169/* same for SOL_TCP */
170#ifndef SOL_TCP
171#define SOL_TCP IPPROTO_TCP
172#endif
173
Lukas Tribus67db8df2013-06-23 17:37:13 +0200174/* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */
175#if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED)
176#define IN6_IS_ADDR_V4MAPPED(a) \
177((((const uint32_t *) (a))[0] == 0) \
178&& (((const uint32_t *) (a))[1] == 0) \
179&& (((const uint32_t *) (a))[2] == htonl (0xffff)))
180#endif
181
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182#if defined(__dietlibc__)
183#include <strings.h>
184#endif
185
Willy Tarreau4698adf2018-10-29 19:14:14 +0100186/* crypt_r() has been present in glibc since 2.2 and on FreeBSD since 12.0
187 * (12000002). No other OS makes any mention of it for now. Feel free to add
188 * valid known combinations below if needed to relax the crypt() lock when
189 * using threads.
190 */
191#if (defined(__GNU_LIBRARY__) && (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) \
192 || (defined(__FreeBSD__) && __FreeBSD_version >= 1200002)
193#define HA_HAVE_CRYPT_R
194#endif
195
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200196#endif /* _COMMON_COMPAT_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
198/*
199 * Local variables:
200 * c-indent-level: 8
201 * c-basic-offset: 8
202 * End:
203 */