nonblock.c 2.6 KB

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