nonblock.c 2.6 KB

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