Line | Count | Source |
1 | | #ifndef HEADER_CURL_SELECT_H |
2 | | #define HEADER_CURL_SELECT_H |
3 | | /*************************************************************************** |
4 | | * _ _ ____ _ |
5 | | * Project ___| | | | _ \| | |
6 | | * / __| | | | |_) | | |
7 | | * | (__| |_| | _ <| |___ |
8 | | * \___|\___/|_| \_\_____| |
9 | | * |
10 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 | | #include "curl_setup.h" |
27 | | |
28 | | #ifdef HAVE_POLL_H |
29 | | #include <poll.h> |
30 | | #elif defined(HAVE_SYS_POLL_H) |
31 | | #include <sys/poll.h> |
32 | | #endif |
33 | | |
34 | | /* |
35 | | * Definition of pollfd struct and constants for platforms lacking them. |
36 | | */ |
37 | | |
38 | | #if !defined(HAVE_SYS_POLL_H) && !defined(HAVE_POLL_H) && !defined(POLLIN) |
39 | | |
40 | | #define POLLIN 0x01 |
41 | | #define POLLPRI 0x02 |
42 | | #define POLLOUT 0x04 |
43 | | #define POLLERR 0x08 |
44 | | #define POLLHUP 0x10 |
45 | | #define POLLNVAL 0x20 |
46 | | |
47 | | struct pollfd { |
48 | | curl_socket_t fd; |
49 | | short events; |
50 | | short revents; |
51 | | }; |
52 | | |
53 | | #endif |
54 | | |
55 | | #ifndef POLLRDNORM |
56 | | #define POLLRDNORM POLLIN |
57 | | #endif |
58 | | |
59 | | #ifndef POLLWRNORM |
60 | | #define POLLWRNORM POLLOUT |
61 | | #endif |
62 | | |
63 | | #ifndef POLLRDBAND |
64 | | #define POLLRDBAND POLLPRI |
65 | | #endif |
66 | | |
67 | | int Curl_socket_check(curl_socket_t readfd, |
68 | | curl_socket_t writefd, |
69 | | timediff_t timeout_ms); |
70 | 0 | #define SOCKET_READABLE(x, z) Curl_socket_check(x, CURL_SOCKET_BAD, z) |
71 | 0 | #define SOCKET_WRITABLE(x, z) Curl_socket_check(CURL_SOCKET_BAD, x, z) |
72 | | |
73 | | int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms); |
74 | | |
75 | | /* With Winsock the valid range is [0..INVALID_SOCKET-1] according to |
76 | | https://learn.microsoft.com/windows/win32/winsock/socket-data-type-2 */ |
77 | | #ifdef USE_WINSOCK |
78 | | #define VALID_SOCK(s) ((s) < INVALID_SOCKET) |
79 | | #define FDSET_SOCK(x) 1 |
80 | | #define VERIFY_SOCK(x) \ |
81 | | do { \ |
82 | | if(!VALID_SOCK(x)) { \ |
83 | | SET_SOCKERRNO(SOCKEINVAL); \ |
84 | | return -1; \ |
85 | | } \ |
86 | | } while(0) |
87 | | #else |
88 | 0 | #define VALID_SOCK(s) ((s) >= 0) |
89 | | |
90 | | /* If the socket is small enough to get set or read from an fdset */ |
91 | 0 | #define FDSET_SOCK(s) ((s) < FD_SETSIZE) |
92 | | |
93 | | #define VERIFY_SOCK(x) \ |
94 | | do { \ |
95 | | if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \ |
96 | | SET_SOCKERRNO(SOCKEINVAL); \ |
97 | | return -1; \ |
98 | | } \ |
99 | | } while(0) |
100 | | #endif |
101 | | |
102 | | /* Keep the sockets to poll for an easy handle. |
103 | | * `actions` are bitmaps of CURL_POLL_IN and CURL_POLL_OUT. |
104 | | * Starts with small capacity, grows on demand. |
105 | | */ |
106 | | #define EZ_POLLSET_DEF_COUNT 2 |
107 | | |
108 | | struct easy_pollset { |
109 | | curl_socket_t *sockets; |
110 | | unsigned char *actions; |
111 | | unsigned int n; |
112 | | unsigned int count; |
113 | | #ifdef DEBUGBUILD |
114 | | int init; |
115 | | #endif |
116 | | curl_socket_t def_sockets[EZ_POLLSET_DEF_COUNT]; |
117 | | unsigned char def_actions[EZ_POLLSET_DEF_COUNT]; |
118 | | }; |
119 | | |
120 | | #ifdef DEBUGBUILD |
121 | 11.1k | #define CURL_EASY_POLLSET_MAGIC 0x7a657370 |
122 | | #endif |
123 | | |
124 | | /* allocate and initialize */ |
125 | | struct easy_pollset *Curl_pollset_create(void); |
126 | | |
127 | | /* Initialize before first use */ |
128 | | void Curl_pollset_init(struct easy_pollset *ps); |
129 | | /* Free any allocated resources */ |
130 | | void Curl_pollset_cleanup(struct easy_pollset *ps); |
131 | | /* Reset to an empty pollset */ |
132 | | void Curl_pollset_reset(struct easy_pollset *ps); |
133 | | /* Move pollset from to pollset to, replacing all in to, |
134 | | * leaving from empty. */ |
135 | | void Curl_pollset_move(struct easy_pollset *to, struct easy_pollset *from); |
136 | | |
137 | | /* Change the poll flags (CURL_POLL_IN/CURL_POLL_OUT) to the poll set for |
138 | | * socket `sock`. If the socket is not already part of the poll set, it |
139 | | * will be added. |
140 | | * If the socket is present and all poll flags are cleared, it will be removed. |
141 | | */ |
142 | | CURLcode Curl_pollset_change(struct Curl_easy *data, |
143 | | struct easy_pollset *ps, curl_socket_t sock, |
144 | | int add_flags, |
145 | | int remove_flags) WARN_UNUSED_RESULT; |
146 | | |
147 | | CURLcode Curl_pollset_set(struct Curl_easy *data, |
148 | | struct easy_pollset *ps, curl_socket_t sock, |
149 | | bool do_in, bool do_out) WARN_UNUSED_RESULT; |
150 | | |
151 | | #define Curl_pollset_add_in(data, ps, sock) \ |
152 | 0 | Curl_pollset_change(data, ps, sock, CURL_POLL_IN, 0) |
153 | | #define Curl_pollset_remove_in(data, ps, sock) \ |
154 | 0 | Curl_pollset_change(data, ps, sock, 0, CURL_POLL_IN) |
155 | | #define Curl_pollset_add_out(data, ps, sock) \ |
156 | 0 | Curl_pollset_change(data, ps, sock, CURL_POLL_OUT, 0) |
157 | | #define Curl_pollset_remove_out(data, ps, sock) \ |
158 | 0 | Curl_pollset_change(data, ps, sock, 0, CURL_POLL_OUT) |
159 | | #define Curl_pollset_add_inout(data, ps, sock) \ |
160 | | Curl_pollset_change(data, ps, sock, CURL_POLL_IN | CURL_POLL_OUT, 0) |
161 | | #define Curl_pollset_set_in_only(data, ps, sock) \ |
162 | 0 | Curl_pollset_change(data, ps, sock, CURL_POLL_IN, CURL_POLL_OUT) |
163 | | #define Curl_pollset_set_out_only(data, ps, sock) \ |
164 | 0 | Curl_pollset_change(data, ps, sock, CURL_POLL_OUT, CURL_POLL_IN) |
165 | | |
166 | | void Curl_pollset_remove(struct easy_pollset *ps, curl_socket_t sock); |
167 | | |
168 | | /* return < = on error, 0 on timeout or how many sockets are ready */ |
169 | | int Curl_pollset_poll(struct Curl_easy *data, |
170 | | struct easy_pollset *ps, |
171 | | timediff_t timeout_ms); |
172 | | |
173 | | /** |
174 | | * Check if the pollset, as is, wants to read and/or write regarding |
175 | | * the given socket. |
176 | | */ |
177 | | void Curl_pollset_check(struct Curl_easy *data, |
178 | | struct easy_pollset *ps, curl_socket_t sock, |
179 | | bool *pwant_read, bool *pwant_write); |
180 | | |
181 | | /* TRUE if the pollset contains socket with CURL_POLL_IN. */ |
182 | | bool Curl_pollset_want_recv(struct Curl_easy *data, |
183 | | struct easy_pollset *ps, |
184 | | curl_socket_t sock); |
185 | | /* TRUE if the pollset contains socket with CURL_POLL_OUT. */ |
186 | | bool Curl_pollset_want_send(struct Curl_easy *data, |
187 | | struct easy_pollset *ps, |
188 | | curl_socket_t sock); |
189 | | |
190 | | struct curl_pollfds { |
191 | | struct pollfd *pfds; |
192 | | unsigned int n; |
193 | | unsigned int count; |
194 | | BIT(allocated_pfds); |
195 | | }; |
196 | | |
197 | | void Curl_pollfds_init(struct curl_pollfds *cpfds, |
198 | | struct pollfd *static_pfds, |
199 | | unsigned int static_count); |
200 | | |
201 | | void Curl_pollfds_reset(struct curl_pollfds *cpfds); |
202 | | |
203 | | void Curl_pollfds_cleanup(struct curl_pollfds *cpfds); |
204 | | |
205 | | CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds, |
206 | | struct easy_pollset *ps); |
207 | | |
208 | | CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds, |
209 | | curl_socket_t sock, short events); |
210 | | |
211 | | struct Curl_waitfds { |
212 | | struct curl_waitfd *wfds; |
213 | | unsigned int n; |
214 | | unsigned int count; |
215 | | }; |
216 | | |
217 | | void Curl_waitfds_init(struct Curl_waitfds *cwfds, |
218 | | struct curl_waitfd *static_wfds, |
219 | | unsigned int static_count); |
220 | | |
221 | | unsigned int Curl_waitfds_add_ps(struct Curl_waitfds *cwfds, |
222 | | struct easy_pollset *ps); |
223 | | |
224 | | #endif /* HEADER_CURL_SELECT_H */ |