blob: 1401f91d4c8a8d520851c7adf00a53a0b2318a4e [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 Tarreaubaaee002006-06-26 02:48:02 +020026/* This is needed on Linux for Netfilter includes */
Willy Tarreau17f449b2010-11-07 11:44:13 +010027#include <sys/param.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010028#include <sys/types.h>
Willy Tarreau0e996c62010-02-26 22:00:19 +010029#include <sys/socket.h>
Willy Tarreau17f449b2010-11-07 11:44:13 +010030#include <arpa/inet.h>
Joris Dedieu9dd44ba2015-06-13 01:05:11 +020031#include <netinet/in.h>
Willy Tarreau9d87ca02016-08-10 21:09:24 +020032#include <netinet/tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Willy Tarreau177e2b02008-07-15 00:36:31 +020034#ifndef BITS_PER_INT
35#define BITS_PER_INT (8*sizeof(int))
36#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38/* this is for libc5 for example */
39#ifndef TCP_NODELAY
40#define TCP_NODELAY 1
41#endif
42
43#ifndef SHUT_RD
44#define SHUT_RD 0
45#endif
46
47#ifndef SHUT_WR
48#define SHUT_WR 1
49#endif
50
Willy Tarreaud6d06902009-08-19 11:22:33 +020051/* only Linux defines it */
52#ifndef MSG_NOSIGNAL
53#define MSG_NOSIGNAL 0
54#endif
55
Willy Tarreauc8f24f82007-11-30 18:38:35 +010056/* AIX does not define MSG_DONTWAIT. We'll define it to zero, and test it
57 * wherever appropriate.
58 */
59#ifndef MSG_DONTWAIT
60#define MSG_DONTWAIT 0
61#endif
62
Willy Tarreau6db06d32009-08-19 11:14:11 +020063/* Only Linux defines MSG_MORE */
64#ifndef MSG_MORE
65#define MSG_MORE 0
66#endif
67
Willy Tarreau2b57cb82013-06-10 19:56:38 +020068/* On Linux 2.4 and above, MSG_TRUNC can be used on TCP sockets to drop any
69 * pending data. Let's rely on NETFILTER to detect if this is supported.
70 */
71#ifdef NETFILTER
72#define MSG_TRUNC_CLEARS_INPUT
73#endif
74
Willy Tarreau17f449b2010-11-07 11:44:13 +010075/* Maximum path length, OS-dependant */
76#ifndef MAXPATHLEN
77#define MAXPATHLEN 128
78#endif
79
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020080/* On Linux, allows pipes to be resized */
81#ifndef F_SETPIPE_SZ
82#define F_SETPIPE_SZ (1024 + 7)
83#endif
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085#if defined(TPROXY) && defined(NETFILTER)
Jeremy Hinegardnere7dd2f22008-04-21 07:34:31 +020086#include <linux/types.h>
87#include <linux/netfilter_ipv6.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020088#include <linux/netfilter_ipv4.h>
89#endif
90
Willy Tarreau0a459892008-01-13 17:37:16 +010091/* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */
Willy Tarreaub1e52e82008-01-13 14:49:51 +010092#if defined(CONFIG_HAP_LINUX_TPROXY)
Willy Tarreau0a459892008-01-13 17:37:16 +010093#if !defined(IP_FREEBIND)
94#define IP_FREEBIND 15
95#endif /* !IP_FREEBIND */
Willy Tarreaub1e52e82008-01-13 14:49:51 +010096#if !defined(IP_TRANSPARENT)
97#define IP_TRANSPARENT 19
98#endif /* !IP_TRANSPARENT */
David du Colombier65c17962012-07-13 14:34:59 +020099#if !defined(IPV6_TRANSPARENT)
100#define IPV6_TRANSPARENT 75
101#endif /* !IPV6_TRANSPARENT */
Willy Tarreaub1e52e82008-01-13 14:49:51 +0100102#endif /* CONFIG_HAP_LINUX_TPROXY */
103
Pieter Baauwd551fb52013-05-08 22:49:23 +0200104#if defined(IP_FREEBIND) \
Pieter Baauwff30b662013-05-08 23:22:39 +0200105 || defined(IP_BINDANY) \
106 || defined(IPV6_BINDANY) \
Pieter Baauw1eb75922013-05-08 23:30:23 +0200107 || defined(SO_BINDANY) \
Pieter Baauwd551fb52013-05-08 22:49:23 +0200108 || defined(IP_TRANSPARENT) \
109 || defined(IPV6_TRANSPARENT)
110#define CONFIG_HAP_TRANSPARENT
111#endif
112
Willy Tarreau58b2f832006-11-13 01:22:38 +0100113/* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined.
114 * There are two families of values depending on the architecture. Those
115 * are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the
116 * NETFILTER define.
117 */
118#if !defined(SO_REUSEPORT) && defined(NETFILTER)
119#if (SO_REUSEADDR == 2)
120#define SO_REUSEPORT 15
121#elif (SO_REUSEADDR == 0x0004)
122#define SO_REUSEPORT 0x0200
123#endif /* SO_REUSEADDR */
124#endif /* SO_REUSEPORT */
125
Lukas Tribus0999f762013-04-02 16:43:24 +0200126/* only Linux defines TCP_FASTOPEN */
127#ifdef USE_TFO
128#ifndef TCP_FASTOPEN
129#define TCP_FASTOPEN 23
130#endif
Willy Tarreau034c88c2017-01-23 23:36:45 +0100131
132#ifndef TCP_FASTOPEN_CONNECT
133#define TCP_FASTOPEN_CONNECT 30
134#endif
Lukas Tribus0999f762013-04-02 16:43:24 +0200135#endif
136
Willy Tarreauae459f32015-09-29 18:19:32 +0200137/* FreeBSD doesn't define SOL_IP and prefers IPPROTO_IP */
138#ifndef SOL_IP
139#define SOL_IP IPPROTO_IP
140#endif
141
Willy Tarreau9d87ca02016-08-10 21:09:24 +0200142/* same for SOL_TCP */
143#ifndef SOL_TCP
144#define SOL_TCP IPPROTO_TCP
145#endif
146
Lukas Tribus67db8df2013-06-23 17:37:13 +0200147/* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */
148#if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED)
149#define IN6_IS_ADDR_V4MAPPED(a) \
150((((const uint32_t *) (a))[0] == 0) \
151&& (((const uint32_t *) (a))[1] == 0) \
152&& (((const uint32_t *) (a))[2] == htonl (0xffff)))
153#endif
154
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155#if defined(__dietlibc__)
156#include <strings.h>
157#endif
158
Willy Tarreau4698adf2018-10-29 19:14:14 +0100159/* crypt_r() has been present in glibc since 2.2 and on FreeBSD since 12.0
160 * (12000002). No other OS makes any mention of it for now. Feel free to add
161 * valid known combinations below if needed to relax the crypt() lock when
162 * using threads.
163 */
164#if (defined(__GNU_LIBRARY__) && (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) \
165 || (defined(__FreeBSD__) && __FreeBSD_version >= 1200002)
166#define HA_HAVE_CRYPT_R
167#endif
168
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200169#endif /* _COMMON_COMPAT_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170
171/*
172 * Local variables:
173 * c-indent-level: 8
174 * c-basic-offset: 8
175 * End:
176 */