sys_sockets_headers.h (8607B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2024 Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file src/mhd2/sys_sockets_headers.h 41 * @brief The header for system headers for the sockets and some basic macros 42 * @author Karlson2k (Evgeny Grin) 43 * 44 * The macros are limited to simple local constants definitions 45 */ 46 47 #ifndef MHD_SYS_SOCKETS_HEADERS_H 48 #define MHD_SYS_SOCKETS_HEADERS_H 1 49 50 #include "mhd_sys_options.h" 51 52 #include "mhd_socket_type.h" 53 54 #ifdef MHD_SOCKETS_KIND_POSIX 55 # include "sys_base_types.h" /* required on old platforms */ 56 # ifdef HAVE_SYS_SOCKET_H 57 # include <sys/socket.h> 58 # endif 59 # ifdef HAVE_SOCKLIB_H 60 # include <sockLib.h> 61 # endif /* HAVE_SOCKLIB_H */ 62 #elif defined(MHD_SOCKETS_KIND_WINSOCK) 63 # include <winsock2.h> 64 #endif 65 #ifdef HAVE_SYS_UN_H 66 # include <sys/un.h> 67 #endif 68 69 #if defined(SOCK_NONBLOCK) && ! defined(HAVE_DCLR_SOCK_NONBLOCK) 70 /* Mis-detected by configure */ 71 # define HAVE_DCLR_SOCK_NONBLOCK 1 72 #endif 73 74 #if defined(SOCK_CLOEXEC) && ! defined(HAVE_DCLR_SOCK_CLOEXEC) 75 /* Mis-detected by configure */ 76 # define HAVE_DCLR_SOCK_CLOEXEC 1 77 #endif 78 79 #if defined(SOCK_NOSIGPIPE) && ! defined(HAVE_DCLR_SOCK_NOSIGPIPE) 80 /* Mis-detected by configure */ 81 # define HAVE_DCLR_SOCK_NOSIGPIPE 1 82 #endif 83 84 #if defined(MSG_NOSIGNAL) && ! defined(HAVE_DCLR_MSG_NOSIGNAL) 85 /* Mis-detected by configure */ 86 # define HAVE_DCLR_MSG_NOSIGNAL 1 87 #endif 88 89 #if defined(MSG_MORE) && ! defined(HAVE_DCLR_MSG_MORE) 90 /* Mis-detected by configure */ 91 # define HAVE_DCLR_MSG_MORE 1 92 #endif 93 94 #if defined(SOL_SOCKET) && ! defined(HAVE_DCLR_SOL_SOCKET) 95 /* Mis-detected by configure */ 96 # define HAVE_DCLR_SOL_SOCKET 1 97 #endif 98 99 #if defined(SO_REUSEADDR) && ! defined(HAVE_DCLR_SO_REUSEADDR) 100 /* Mis-detected by configure */ 101 # define HAVE_DCLR_SO_REUSEADDR 1 102 #endif 103 104 #if defined(SO_REUSEPORT) && ! defined(HAVE_DCLR_SO_REUSEPORT) 105 /* Mis-detected by configure */ 106 # define HAVE_DCLR_SO_REUSEPORT 1 107 #endif 108 109 #if defined(SO_LINGER) && ! defined(HAVE_DCLR_SO_LINGER) 110 /* Mis-detected by configure */ 111 # define HAVE_DCLR_SO_LINGER 1 112 #endif 113 114 #if defined(SO_NOSIGPIPE) && ! defined(HAVE_DCLR_SO_NOSIGPIPE) 115 /* Mis-detected by configure */ 116 # define HAVE_DCLR_SO_NOSIGPIPE 1 117 #endif 118 119 #if defined(HAVE_DCLR_SOCK_NONBLOCK) && ! defined(MHD_SOCKETS_KIND_WINSOCK) 120 # define mhd_SOCK_NONBLOCK SOCK_NONBLOCK 121 #else 122 # define mhd_SOCK_NONBLOCK (0) 123 #endif 124 125 #if defined(HAVE_DCLR_SOCK_CLOEXEC) && ! defined(MHD_SOCKETS_KIND_WINSOCK) 126 # define mhd_SOCK_CLOEXEC SOCK_CLOEXEC 127 #else 128 # define mhd_SOCK_CLOEXEC (0) 129 #endif 130 131 #if defined(HAVE_DCLR_SOCK_NOSIGPIPE) && ! defined(MHD_SOCKETS_KIND_WINSOCK) 132 # define mhd_SOCK_NOSIGPIPE SOCK_NOSIGPIPE 133 #else 134 # define mhd_SOCK_NOSIGPIPE (0) 135 #endif 136 137 #if defined(HAVE_DCLR_MSG_NOSIGNAL) && ! defined(MHD_SOCKETS_KIND_WINSOCK) 138 # define mhd_MSG_NOSIGNAL MSG_NOSIGNAL 139 #else 140 # define mhd_MSG_NOSIGNAL (0) 141 #endif 142 143 #ifdef HAVE_DCLR_MSG_MORE 144 # ifdef __linux__ 145 /* MSG_MORE signal kernel to buffer outbound data and works like 146 TCP_CORK for a single call without actually setting the TCP_CORK flag. 147 It's known to work on Linux. Add more OSes if they are compatible. */ 148 /** 149 * Indicate MSG_MORE is usable for buffered send(). 150 */ 151 # define mhd_USE_MSG_MORE 1 152 # endif /* __linux__ */ 153 #endif /* MSG_MORE */ 154 155 #ifdef mhd_USE_MSG_MORE 156 # define mhd_MSG_MORE MSG_MORE 157 #else 158 # define mhd_MSG_MORE (0) 159 #endif 160 161 162 #if defined(HAVE_ACCEPT4) && (defined(HAVE_DCLR_SOCK_NONBLOCK) || \ 163 defined(HAVE_DCLR_SOCK_CLOEXEC) || defined(HAVE_DCLR_SOCK_NOSIGPIPE)) 164 # define mhd_USE_ACCEPT4 1 165 #endif 166 167 168 /** 169 * mhd_SCKT_OPT_BOOL is the type for bool parameters 170 * for setsockopt()/getsockopt() functions 171 */ 172 #if defined(MHD_SOCKETS_KIND_POSIX) 173 # define mhd_SCKT_OPT_BOOL int 174 #elif defined(MHD_SOCKETS_KIND_WINSOCK) 175 # define mhd_SCKT_OPT_BOOL BOOL 176 #endif /* MHD_SOCKETS_KIND_WINSOCK */ 177 178 /** 179 * mhd_setsockopt() is a wrapper for system setsockopt() 180 */ 181 #ifdef MHD_SOCKETS_KIND_POSIX 182 # define mhd_setsockopt(sk,l,o_name,po_value,o_len) \ 183 setsockopt ((sk),(l),(o_name),(po_value),(o_len)) 184 #else 185 # define mhd_setsockopt(sk,l,o_name,po_value,o_len) \ 186 setsockopt ((sk),(l),(o_name),(const char *) (po_value),(o_len)) 187 #endif 188 189 /** 190 * mhd_getsockopt() is a wrapper for system getsockopt() 191 */ 192 #ifdef MHD_SOCKETS_KIND_POSIX 193 # define mhd_getsockopt(sk,l,o_name,po_value,po_len) \ 194 getsockopt ((sk),(l),(o_name),(po_value),(po_len)) 195 #else 196 # define mhd_getsockopt(sk,l,o_name,po_value,po_len) \ 197 getsockopt ((sk),(l),(o_name),(char *) (po_value),(po_len)) 198 #endif 199 200 /** 201 * mhd_SCKT_SEND_SIZE is type used to specify size for send() and recv() 202 * functions 203 */ 204 #if defined(MHD_SOCKETS_KIND_POSIX) 205 typedef size_t mhd_SCKT_SEND_SIZE; 206 #elif defined(MHD_SOCKETS_KIND_WINSOCK) 207 typedef int mhd_SCKT_SEND_SIZE; 208 #endif 209 210 /** 211 * MHD_SCKT_SEND_MAX_SIZE_ is maximum send()/recv() size value. 212 */ 213 #if defined(MHD_SOCKETS_KIND_POSIX) 214 # define MHD_SCKT_SEND_MAX_SIZE_ SSIZE_MAX 215 #elif defined(MHD_SOCKETS_KIND_WINSOCK) 216 # define MHD_SCKT_SEND_MAX_SIZE_ (0x7FFFFFFF) /* INT_MAX */ 217 #endif 218 219 220 #if defined(AF_UNIX) || defined(HAVE_DCLR_AF_UNIX) 221 # define MHD_AF_UNIX AF_UNIX 222 #elif defined(AF_LOCAL) || defined(HAVE_DCLR_AF_LOCAL) 223 # define MHD_AF_UNIX AF_LOCAL 224 #endif /* AF_UNIX */ 225 226 227 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ 228 defined(__OpenBSD__) || defined(__NetBSD__) || \ 229 defined(MHD_SOCKETS_KIND_WINSOCK) || defined(__MACH__) || defined(__sun) || \ 230 defined(SOMEBSD) 231 /* Most of the OSes inherit nonblocking setting from the listen socket */ 232 # define MHD_ACCEPTED_INHERITS_NONBLOCK 1 233 #elif defined(__gnu_linux__) || defined(__linux__) 234 # define MHD_ACCEPTED_DOES_NOT_INHERIT_NONBLOCK 1 235 #endif 236 237 238 #if defined(HAVE_DCLR_SOL_SOCKET) && defined(HAVE_DCLR_SO_NOSIGPIPE) 239 /** 240 * Helper for mhd_socket_nosignal() 241 */ 242 # ifdef HAVE_COMPOUND_LITERALS_LVALUES 243 # define mhd_socket_nosig_helper_int_one ((mhd_SCKT_OPT_BOOL){1}) 244 # else 245 /** 246 * Internal static const helper for mhd_socket_nosignal() 247 */ 248 static const mhd_SCKT_OPT_BOOL mhd_socket_nosig_helper_int_one = 1; 249 # endif 250 251 /** 252 * Change socket options to no signal on remote disconnect / broken connection. 253 * 254 * @param sock socket to manipulate 255 * @return non-zero if succeeded, zero otherwise 256 */ 257 # define mhd_socket_nosignal(sock) \ 258 (! mhd_setsockopt ((sock),SOL_SOCKET,SO_NOSIGPIPE, \ 259 &mhd_socket_nosig_helper_int_one, \ 260 sizeof(mhd_SCKT_OPT_BOOL))) 261 #endif /* SOL_SOCKET && SO_NOSIGPIPE */ 262 263 264 #if defined(mhd_socket_nosignal) || defined(HAVE_DCLR_MSG_NOSIGNAL) 265 /** 266 * Indicate that SIGPIPE can be suppressed by MHD for normal send() by flags 267 * or socket options. 268 * If this macro is undefined, MHD cannot suppress SIGPIPE for socket functions 269 * so application need to handle SIGPIPE. 270 */ 271 # define mhd_SEND_SPIPE_SUPPRESS_POSSIBLE 1 272 #endif /* mhd_socket_nosignal || HAVE_DCLR_MSG_NOSIGNAL */ 273 274 275 #endif /* ! MHD_SYS_SOCKETS_HEADERS_H */