2
0

nonblock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 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. #include "curl_setup.h"
  25. #ifdef HAVE_SYS_IOCTL_H
  26. #include <sys/ioctl.h>
  27. #endif
  28. #ifdef HAVE_FCNTL_H
  29. #include <fcntl.h>
  30. #endif
  31. #if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
  32. #include <sys/filio.h>
  33. #endif
  34. #ifdef __VMS
  35. #include <in.h>
  36. #include <inet.h>
  37. #endif
  38. #include "nonblock.h"
  39. /*
  40. * curlx_nonblock() set the given socket to either blocking or non-blocking
  41. * mode based on the 'nonblock' boolean argument. This function is highly
  42. * portable.
  43. */
  44. int curlx_nonblock(curl_socket_t sockfd, /* operate on this */
  45. int nonblock /* TRUE or FALSE */)
  46. {
  47. #if defined(HAVE_FCNTL_O_NONBLOCK)
  48. /* most recent unix versions */
  49. int flags;
  50. flags = sfcntl(sockfd, F_GETFL, 0);
  51. if(nonblock)
  52. return sfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  53. return sfcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
  54. #elif defined(HAVE_IOCTL_FIONBIO)
  55. /* older unix versions */
  56. int flags = nonblock ? 1 : 0;
  57. return ioctl(sockfd, FIONBIO, &flags);
  58. #elif defined(HAVE_IOCTLSOCKET_FIONBIO)
  59. /* Windows */
  60. unsigned long flags = nonblock ? 1UL : 0UL;
  61. return ioctlsocket(sockfd, FIONBIO, &flags);
  62. #elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
  63. /* Amiga */
  64. long flags = nonblock ? 1L : 0L;
  65. return IoctlSocket(sockfd, FIONBIO, (char *)&flags);
  66. #elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
  67. /* Orbis OS */
  68. long b = nonblock ? 1L : 0L;
  69. return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
  70. #else
  71. # error "no non-blocking method was found/used/set"
  72. #endif
  73. }