Coverage Report

Created: 2026-07-16 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/select.h
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
/* there are three CSELECT defines that are defined in the public header that
68
   are exposed to users, but this *IN2 bit is only ever used internally and
69
   therefore defined here */
70
0
#define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
71
72
int Curl_socket_check(curl_socket_t readfd0,
73
                      curl_socket_t readfd1,
74
                      curl_socket_t writefd,
75
                      timediff_t timeout_ms);
76
#define SOCKET_READABLE(x, z)                               \
77
0
  Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
78
#define SOCKET_WRITABLE(x, z)                               \
79
0
  Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
80
81
int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
82
83
/*
84
   With Winsock the valid range is [0..INVALID_SOCKET-1] according to
85
   https://learn.microsoft.com/windows/win32/winsock/socket-data-type-2
86
*/
87
#ifdef USE_WINSOCK
88
#define VALID_SOCK(s) ((s) < INVALID_SOCKET)
89
#define FDSET_SOCK(x) 1
90
#define VERIFY_SOCK(x)           \
91
  do {                           \
92
    if(!VALID_SOCK(x)) {         \
93
      SET_SOCKERRNO(SOCKEINVAL); \
94
      return -1;                 \
95
    }                            \
96
  } while(0)
97
#else
98
0
#define VALID_SOCK(s) ((s) >= 0)
99
100
/* If the socket is small enough to get set or read from an fdset */
101
0
#define FDSET_SOCK(s) ((s) < FD_SETSIZE)
102
103
#define VERIFY_SOCK(x)                     \
104
  do {                                     \
105
    if(!VALID_SOCK(x) || !FDSET_SOCK(x)) { \
106
      SET_SOCKERRNO(SOCKEINVAL);           \
107
      return -1;                           \
108
    }                                      \
109
  } while(0)
110
#endif
111
112
/* Keep the sockets to poll for an easy handle.
113
 * `actions` are bitmaps of CURL_POLL_IN and CURL_POLL_OUT.
114
 * Starts with small capacity, grows on demand.
115
 */
116
#define EZ_POLLSET_DEF_COUNT 2
117
118
struct easy_pollset {
119
  curl_socket_t *sockets;
120
  unsigned char *actions;
121
  unsigned int n;
122
  unsigned int count;
123
#ifdef DEBUGBUILD
124
  int init;
125
#endif
126
  curl_socket_t def_sockets[EZ_POLLSET_DEF_COUNT];
127
  unsigned char def_actions[EZ_POLLSET_DEF_COUNT];
128
};
129
130
#ifdef DEBUGBUILD
131
0
#define CURL_EASY_POLLSET_MAGIC  0x7a657370
132
#endif
133
134
/* allocate and initialize */
135
struct easy_pollset *Curl_pollset_create(void);
136
137
/* Initialize before first use */
138
void Curl_pollset_init(struct easy_pollset *ps);
139
/* Free any allocated resources */
140
void Curl_pollset_cleanup(struct easy_pollset *ps);
141
/* Reset to an empty pollset */
142
void Curl_pollset_reset(struct easy_pollset *ps);
143
/* Move pollset from to pollset to, replacing all in to,
144
 * leaving from empty. */
145
void Curl_pollset_move(struct easy_pollset *to, struct easy_pollset *from);
146
147
/* Change the poll flags (CURL_POLL_IN/CURL_POLL_OUT) to the poll set for
148
 * socket `sock`. If the socket is not already part of the poll set, it
149
 * will be added.
150
 * If the socket is present and all poll flags are cleared, it will be removed.
151
 */
152
CURLcode Curl_pollset_change(struct Curl_easy *data,
153
                             struct easy_pollset *ps, curl_socket_t sock,
154
                             int add_flags,
155
                             int remove_flags) WARN_UNUSED_RESULT;
156
157
CURLcode Curl_pollset_set(struct Curl_easy *data,
158
                          struct easy_pollset *ps, curl_socket_t sock,
159
                          bool do_in, bool do_out) WARN_UNUSED_RESULT;
160
161
#define Curl_pollset_add_in(data, ps, sock) \
162
0
  Curl_pollset_change(data, ps, sock, CURL_POLL_IN, 0)
163
#define Curl_pollset_remove_in(data, ps, sock) \
164
0
  Curl_pollset_change(data, ps, sock, 0, CURL_POLL_IN)
165
#define Curl_pollset_add_out(data, ps, sock) \
166
0
  Curl_pollset_change(data, ps, sock, CURL_POLL_OUT, 0)
167
#define Curl_pollset_remove_out(data, ps, sock) \
168
0
  Curl_pollset_change(data, ps, sock, 0, CURL_POLL_OUT)
169
#define Curl_pollset_add_inout(data, ps, sock) \
170
  Curl_pollset_change(data, ps, sock, CURL_POLL_IN | CURL_POLL_OUT, 0)
171
#define Curl_pollset_set_in_only(data, ps, sock) \
172
0
  Curl_pollset_change(data, ps, sock, CURL_POLL_IN, CURL_POLL_OUT)
173
#define Curl_pollset_set_out_only(data, ps, sock) \
174
0
  Curl_pollset_change(data, ps, sock, CURL_POLL_OUT, CURL_POLL_IN)
175
176
/* return < = on error, 0 on timeout or how many sockets are ready */
177
int Curl_pollset_poll(struct Curl_easy *data,
178
                      struct easy_pollset *ps,
179
                      timediff_t timeout_ms);
180
181
/**
182
 * Check if the pollset, as is, wants to read and/or write regarding
183
 * the given socket.
184
 */
185
void Curl_pollset_check(struct Curl_easy *data,
186
                        struct easy_pollset *ps, curl_socket_t sock,
187
                        bool *pwant_read, bool *pwant_write);
188
189
/* TRUE if the pollset contains socket with CURL_POLL_IN. */
190
bool Curl_pollset_want_recv(struct Curl_easy *data,
191
                            struct easy_pollset *ps,
192
                            curl_socket_t sock);
193
/* TRUE if the pollset contains socket with CURL_POLL_OUT. */
194
bool Curl_pollset_want_send(struct Curl_easy *data,
195
                            struct easy_pollset *ps,
196
                            curl_socket_t sock);
197
198
struct curl_pollfds {
199
  struct pollfd *pfds;
200
  unsigned int n;
201
  unsigned int count;
202
  BIT(allocated_pfds);
203
};
204
205
void Curl_pollfds_init(struct curl_pollfds *cpfds,
206
                       struct pollfd *static_pfds,
207
                       unsigned int static_count);
208
209
void Curl_pollfds_reset(struct curl_pollfds *cpfds);
210
211
void Curl_pollfds_cleanup(struct curl_pollfds *cpfds);
212
213
CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
214
                             struct easy_pollset *ps);
215
216
CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds,
217
                               curl_socket_t sock, short events);
218
219
struct Curl_waitfds {
220
  struct curl_waitfd *wfds;
221
  unsigned int n;
222
  unsigned int count;
223
};
224
225
void Curl_waitfds_init(struct Curl_waitfds *cwfds,
226
                       struct curl_waitfd *static_wfds,
227
                       unsigned int static_count);
228
229
unsigned int Curl_waitfds_add_ps(struct Curl_waitfds *cwfds,
230
                                 struct easy_pollset *ps);
231
232
#endif /* HEADER_CURL_SELECT_H */