Coverage Report

Created: 2026-08-13 07:18

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