1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_SYS_IOCTL_H
26 #include <sys/ioctl.h>
32 #if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
33 #include <sys/filio.h>
43 * curlx_nonblock() set the given socket to either blocking or non-blocking
44 * mode based on the 'nonblock' boolean argument. This function is highly
47 int curlx_nonblock(curl_socket_t sockfd, /* operate on this */
48 int nonblock /* TRUE or FALSE */)
50 #if defined(USE_BLOCKING_SOCKETS)
53 return 0; /* returns success */
55 #elif defined(HAVE_FCNTL_O_NONBLOCK)
57 /* most recent unix versions */
59 flags = sfcntl(sockfd, F_GETFL, 0);
61 return sfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
62 return sfcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
64 #elif defined(HAVE_IOCTL_FIONBIO)
66 /* older unix versions */
67 int flags = nonblock ? 1 : 0;
68 return ioctl(sockfd, FIONBIO, &flags);
70 #elif defined(HAVE_IOCTLSOCKET_FIONBIO)
73 unsigned long flags = nonblock ? 1UL : 0UL;
74 return ioctlsocket(sockfd, FIONBIO, &flags);
76 #elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
79 long flags = nonblock ? 1L : 0L;
80 return IoctlSocket(sockfd, FIONBIO, (char *)&flags);
82 #elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
85 long b = nonblock ? 1L : 0L;
86 return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
89 # error "no non-blocking method was found/used/set"