1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25#include "curl_setup.h"
26
27#ifdef HAVE_SYS_IOCTL_H
28#include <sys/ioctl.h>
29#endif
30#ifdef HAVE_FCNTL_H
31#include <fcntl.h>
32#endif
33
34#if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
35#include <sys/filio.h>
36#endif
37#ifdef __VMS
38#include <in.h>
39#include <inet.h>
40#endif
41
42#include "nonblock.h"
43
44/*
45 * curlx_nonblock() set the given socket to either blocking or non-blocking
46 * mode based on the 'nonblock' boolean argument. This function is highly
47 * portable.
48 */
49int curlx_nonblock(curl_socket_t sockfd, /* operate on this */
50 int nonblock /* TRUE or FALSE */)
51{
52#if defined(HAVE_FCNTL_O_NONBLOCK)
53 /* most recent unix versions */
54 int flags;
55 flags = sfcntl(sockfd, F_GETFL, 0);
56 if(nonblock)
57 return sfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
58 return sfcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
59
60#elif defined(HAVE_IOCTL_FIONBIO)
61
62 /* older unix versions */
63 int flags = nonblock ? 1 : 0;
64 return ioctl(sockfd, FIONBIO, &flags);
65
66#elif defined(HAVE_IOCTLSOCKET_FIONBIO)
67
68 /* Windows */
69 unsigned long flags = nonblock ? 1UL : 0UL;
70 return ioctlsocket(sockfd, FIONBIO, &flags);
71
72#elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
73
74 /* Amiga */
75 long flags = nonblock ? 1L : 0L;
76 return IoctlSocket(sockfd, FIONBIO, (char *)&flags);
77
78#elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
79
80 /* Orbis OS */
81 long b = nonblock ? 1L : 0L;
82 return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
83
84#else
85# error "no non-blocking method was found/used/set"
86#endif
87}
88