1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, 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 http://curl.haxx.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 ***************************************************************************/
22
23#include "setup.h"
24
25#ifdef HAVE_SYS_SOCKET_H
26#include <sys/socket.h>
27#endif
28#ifdef HAVE_SYS_IOCTL_H
29#include <sys/ioctl.h>
30#endif
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34#ifdef HAVE_FCNTL_H
35#include <fcntl.h>
36#endif
37#ifdef HAVE_STDLIB_H
38#include <stdlib.h>
39#endif
40
41#if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
42#include <sys/filio.h>
43#endif
44#ifdef __VMS
45#include <in.h>
46#include <inet.h>
47#endif
48
49#include "nonblock.h"
50
51/*
52 * curlx_nonblock() set the given socket to either blocking or non-blocking
53 * mode based on the 'nonblock' boolean argument. This function is highly
54 * portable.
55 */
56int curlx_nonblock(curl_socket_t sockfd,    /* operate on this */
57                   int nonblock   /* TRUE or FALSE */)
58{
59#if defined(USE_BLOCKING_SOCKETS)
60
61  return 0; /* returns success */
62
63#elif defined(HAVE_FCNTL_O_NONBLOCK)
64
65  /* most recent unix versions */
66  int flags;
67  flags = fcntl(sockfd, F_GETFL, 0);
68  if(FALSE != nonblock)
69    return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
70  else
71    return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
72
73#elif defined(HAVE_IOCTL_FIONBIO)
74
75  /* older unix versions */
76  int flags;
77  flags = nonblock;
78  return ioctl(sockfd, FIONBIO, &flags);
79
80#elif defined(HAVE_IOCTLSOCKET_FIONBIO)
81
82  /* Windows */
83  unsigned long flags;
84  flags = nonblock;
85  return ioctlsocket(sockfd, FIONBIO, &flags);
86
87#elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
88
89  /* Amiga */
90  return IoctlSocket(sockfd, FIONBIO, (long)nonblock);
91
92#elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
93
94  /* BeOS */
95  long b = nonblock ? 1 : 0;
96  return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
97
98#else
99#  error "no non-blocking method was found/used/set"
100#endif
101}
102