113498266Sopenharmony_ci#ifndef HEADER_CURL_SELECT_H
213498266Sopenharmony_ci#define HEADER_CURL_SELECT_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "curl_setup.h"
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#ifdef HAVE_POLL_H
3013498266Sopenharmony_ci#include <poll.h>
3113498266Sopenharmony_ci#elif defined(HAVE_SYS_POLL_H)
3213498266Sopenharmony_ci#include <sys/poll.h>
3313498266Sopenharmony_ci#endif
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci/*
3613498266Sopenharmony_ci * Definition of pollfd struct and constants for platforms lacking them.
3713498266Sopenharmony_ci */
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#if !defined(HAVE_SYS_POLL_H) && \
4013498266Sopenharmony_ci    !defined(HAVE_POLL_H) && \
4113498266Sopenharmony_ci    !defined(POLLIN)
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci#define POLLIN      0x01
4413498266Sopenharmony_ci#define POLLPRI     0x02
4513498266Sopenharmony_ci#define POLLOUT     0x04
4613498266Sopenharmony_ci#define POLLERR     0x08
4713498266Sopenharmony_ci#define POLLHUP     0x10
4813498266Sopenharmony_ci#define POLLNVAL    0x20
4913498266Sopenharmony_ci
5013498266Sopenharmony_cistruct pollfd
5113498266Sopenharmony_ci{
5213498266Sopenharmony_ci    curl_socket_t fd;
5313498266Sopenharmony_ci    short   events;
5413498266Sopenharmony_ci    short   revents;
5513498266Sopenharmony_ci};
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci#endif
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci#ifndef POLLRDNORM
6013498266Sopenharmony_ci#define POLLRDNORM POLLIN
6113498266Sopenharmony_ci#endif
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci#ifndef POLLWRNORM
6413498266Sopenharmony_ci#define POLLWRNORM POLLOUT
6513498266Sopenharmony_ci#endif
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci#ifndef POLLRDBAND
6813498266Sopenharmony_ci#define POLLRDBAND POLLPRI
6913498266Sopenharmony_ci#endif
7013498266Sopenharmony_ci
7113498266Sopenharmony_ci/* there are three CSELECT defines that are defined in the public header that
7213498266Sopenharmony_ci   are exposed to users, but this *IN2 bit is only ever used internally and
7313498266Sopenharmony_ci   therefore defined here */
7413498266Sopenharmony_ci#define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
7513498266Sopenharmony_ci
7613498266Sopenharmony_ciint Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
7713498266Sopenharmony_ci                      curl_socket_t writefd,
7813498266Sopenharmony_ci                      timediff_t timeout_ms);
7913498266Sopenharmony_ci#define SOCKET_READABLE(x,z) \
8013498266Sopenharmony_ci  Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
8113498266Sopenharmony_ci#define SOCKET_WRITABLE(x,z) \
8213498266Sopenharmony_ci  Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
8313498266Sopenharmony_ci
8413498266Sopenharmony_ciint Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
8513498266Sopenharmony_ciint Curl_wait_ms(timediff_t timeout_ms);
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci/*
8813498266Sopenharmony_ci   With Winsock the valid range is [0..INVALID_SOCKET-1] according to
8913498266Sopenharmony_ci   https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
9013498266Sopenharmony_ci*/
9113498266Sopenharmony_ci#ifdef USE_WINSOCK
9213498266Sopenharmony_ci#define VALID_SOCK(s) ((s) < INVALID_SOCKET)
9313498266Sopenharmony_ci#define FDSET_SOCK(x) 1
9413498266Sopenharmony_ci#define VERIFY_SOCK(x) do { \
9513498266Sopenharmony_ci  if(!VALID_SOCK(x)) { \
9613498266Sopenharmony_ci    SET_SOCKERRNO(WSAEINVAL); \
9713498266Sopenharmony_ci    return -1; \
9813498266Sopenharmony_ci  } \
9913498266Sopenharmony_ci} while(0)
10013498266Sopenharmony_ci#else
10113498266Sopenharmony_ci#define VALID_SOCK(s) ((s) >= 0)
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci/* If the socket is small enough to get set or read from an fdset */
10413498266Sopenharmony_ci#define FDSET_SOCK(s) ((s) < FD_SETSIZE)
10513498266Sopenharmony_ci
10613498266Sopenharmony_ci#define VERIFY_SOCK(x) do {                     \
10713498266Sopenharmony_ci    if(!VALID_SOCK(x) || !FDSET_SOCK(x)) {      \
10813498266Sopenharmony_ci      SET_SOCKERRNO(EINVAL);                    \
10913498266Sopenharmony_ci      return -1;                                \
11013498266Sopenharmony_ci    }                                           \
11113498266Sopenharmony_ci  } while(0)
11213498266Sopenharmony_ci#endif
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci#endif /* HEADER_CURL_SELECT_H */
115