openssl: guard against OOM on context creation
[platform/upstream/curl.git] / lib / select.h
1 #ifndef HEADER_CURL_SELECT_H
2 #define HEADER_CURL_SELECT_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2020, 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.haxx.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  ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #ifdef HAVE_POLL_H
28 #include <poll.h>
29 #elif defined(HAVE_SYS_POLL_H)
30 #include <sys/poll.h>
31 #endif
32
33 /*
34  * Definition of pollfd struct and constants for platforms lacking them.
35  */
36
37 #if !defined(HAVE_STRUCT_POLLFD) && \
38     !defined(HAVE_SYS_POLL_H) && \
39     !defined(HAVE_POLL_H) && \
40     !defined(POLLIN)
41
42 #define POLLIN      0x01
43 #define POLLPRI     0x02
44 #define POLLOUT     0x04
45 #define POLLERR     0x08
46 #define POLLHUP     0x10
47 #define POLLNVAL    0x20
48
49 struct pollfd
50 {
51     curl_socket_t fd;
52     short   events;
53     short   revents;
54 };
55
56 #endif
57
58 #ifndef POLLRDNORM
59 #define POLLRDNORM POLLIN
60 #endif
61
62 #ifndef POLLWRNORM
63 #define POLLWRNORM POLLOUT
64 #endif
65
66 #ifndef POLLRDBAND
67 #define POLLRDBAND POLLPRI
68 #endif
69
70 /* there are three CSELECT defines that are defined in the public header that
71    are exposed to users, but this *IN2 bit is only ever used internally and
72    therefore defined here */
73 #define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
74
75 int Curl_select(curl_socket_t maxfd,
76                 fd_set *fds_read,
77                 fd_set *fds_write,
78                 fd_set *fds_err,
79                 timediff_t timeout_ms);
80
81 int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
82                       curl_socket_t writefd,
83                       timediff_t timeout_ms);
84 #define SOCKET_READABLE(x,z) \
85   Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
86 #define SOCKET_WRITABLE(x,z) \
87   Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
88
89 int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
90 int Curl_wait_ms(timediff_t timeout_ms);
91
92 #ifdef TPF
93 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
94                        fd_set* excepts, struct timeval *tv);
95 #endif
96
97 /* TPF sockets are not in range [0..FD_SETSIZE-1], which
98    unfortunately makes it impossible for us to easily check if they're valid
99
100    With Winsock the valid range is [0..INVALID_SOCKET-1] according to
101    https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
102 */
103 #if defined(TPF)
104 #define VALID_SOCK(x) 1
105 #define VERIFY_SOCK(x) Curl_nop_stmt
106 #elif defined(USE_WINSOCK)
107 #define VALID_SOCK(s) ((s) < INVALID_SOCKET)
108 #define VERIFY_SOCK(x) do { \
109   if(!VALID_SOCK(x)) { \
110     SET_SOCKERRNO(WSAEINVAL); \
111     return -1; \
112   } \
113 } while(0)
114 #else
115 #define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
116 #define VERIFY_SOCK(x) do { \
117   if(!VALID_SOCK(x)) { \
118     SET_SOCKERRNO(EINVAL); \
119     return -1; \
120   } \
121 } while(0)
122 #endif
123
124 #endif /* HEADER_CURL_SELECT_H */