nonblock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #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. #ifdef HAVE_STDLIB_H
  36. #include <stdlib.h>
  37. #endif
  38. #if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
  39. #include <sys/filio.h>
  40. #endif
  41. #ifdef __VMS
  42. #include <in.h>
  43. #include <inet.h>
  44. #endif
  45. #include "nonblock.h"
  46. /*
  47. * curlx_nonblock() set the given socket to either blocking or non-blocking
  48. * mode based on the 'nonblock' boolean argument. This function is highly
  49. * portable.
  50. */
  51. int curlx_nonblock(curl_socket_t sockfd, /* operate on this */
  52. int nonblock /* TRUE or FALSE */)
  53. {
  54. #if defined(USE_BLOCKING_SOCKETS)
  55. return 0; /* returns success */
  56. #elif defined(HAVE_FCNTL_O_NONBLOCK)
  57. /* most recent unix versions */
  58. int flags;
  59. flags = fcntl(sockfd, F_GETFL, 0);
  60. if(FALSE != nonblock)
  61. return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  62. else
  63. return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
  64. #elif defined(HAVE_IOCTL_FIONBIO)
  65. /* older unix versions */
  66. int flags;
  67. flags = nonblock;
  68. return ioctl(sockfd, FIONBIO, &flags);
  69. #elif defined(HAVE_IOCTLSOCKET_FIONBIO)
  70. /* Windows */
  71. unsigned long flags;
  72. flags = nonblock;
  73. return ioctlsocket(sockfd, FIONBIO, &flags);
  74. #elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
  75. /* Amiga */
  76. return IoctlSocket(sockfd, FIONBIO, (long)nonblock);
  77. #elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
  78. /* BeOS */
  79. long b = nonblock ? 1 : 0;
  80. return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
  81. #else
  82. # error "no non-blocking method was found/used/set"
  83. #endif
  84. }