1#ifndef HEADER_CURL_SELECT_H
2#define HEADER_CURL_SELECT_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
11 *
12 * This software is licensed as described in the file COPYING, which
13 * you should have received as part of this distribution. The terms
14 * are also available at https://curl.se/docs/copyright.html.
15 *
16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17 * copies of the Software, and permit persons to whom the Software is
18 * furnished to do so, under the terms of the COPYING file.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 * SPDX-License-Identifier: curl
24 *
25 ***************************************************************************/
26
27#include "curl_setup.h"
28
29#ifdef HAVE_POLL_H
30#include <poll.h>
31#elif defined(HAVE_SYS_POLL_H)
32#include <sys/poll.h>
33#endif
34
35/*
36 * Definition of pollfd struct and constants for platforms lacking them.
37 */
38
39#if !defined(HAVE_STRUCT_POLLFD) && \
40 !defined(HAVE_SYS_POLL_H) && \
41 !defined(HAVE_POLL_H) && \
42 !defined(POLLIN)
43
44#define POLLIN 0x01
45#define POLLPRI 0x02
46#define POLLOUT 0x04
47#define POLLERR 0x08
48#define POLLHUP 0x10
49#define POLLNVAL 0x20
50
51struct pollfd
52{
53 curl_socket_t fd;
54 short events;
55 short revents;
56};
57
58#endif
59
60#ifndef POLLRDNORM
61#define POLLRDNORM POLLIN
62#endif
63
64#ifndef POLLWRNORM
65#define POLLWRNORM POLLOUT
66#endif
67
68#ifndef POLLRDBAND
69#define POLLRDBAND POLLPRI
70#endif
71
72/* there are three CSELECT defines that are defined in the public header that
73 are exposed to users, but this *IN2 bit is only ever used internally and
74 therefore defined here */
75#define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
76
77int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
78 curl_socket_t writefd,
79 timediff_t timeout_ms);
80#define SOCKET_READABLE(x,z) \
81 Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
82#define SOCKET_WRITABLE(x,z) \
83 Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
84
85int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
86int Curl_wait_ms(timediff_t timeout_ms);
87
88/*
89 With Winsock the valid range is [0..INVALID_SOCKET-1] according to
90 https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
91*/
92#ifdef USE_WINSOCK
93#define VALID_SOCK(s) ((s) < INVALID_SOCKET)
94#define FDSET_SOCK(x) 1
95#define VERIFY_SOCK(x) do { \
96 if(!VALID_SOCK(x)) { \
97 SET_SOCKERRNO(WSAEINVAL); \
98 return -1; \
99 } \
100} while(0)
101#else
102#define VALID_SOCK(s) ((s) >= 0)
103
104/* If the socket is small enough to get set or read from an fdset */
105#define FDSET_SOCK(s) ((s) < FD_SETSIZE)
106
107#define VERIFY_SOCK(x) do { \
108 if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \
109 SET_SOCKERRNO(EINVAL); \
110 return -1; \
111 } \
112 } while(0)
113#endif
114
115#endif /* HEADER_CURL_SELECT_H */
116