2
0

nonblock.c 2.7 KB

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