w32sock.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* w32sock.h --- internal auxilliary functions for Windows socket functions
  2. Copyright (C) 2008-2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paolo Bonzini */
  14. #include <errno.h>
  15. /* Get O_RDWR and O_BINARY. */
  16. #include <fcntl.h>
  17. /* Get _open_osfhandle(). */
  18. #include <io.h>
  19. /* Get _get_osfhandle(). */
  20. #include "msvc-nothrow.h"
  21. #define FD_TO_SOCKET(fd) ((SOCKET) _get_osfhandle ((fd)))
  22. #define SOCKET_TO_FD(fh) (_open_osfhandle ((long) (fh), O_RDWR | O_BINARY))
  23. static inline void
  24. set_winsock_errno (void)
  25. {
  26. int err = WSAGetLastError ();
  27. /* Map some WSAE* errors to the runtime library's error codes. */
  28. switch (err)
  29. {
  30. case WSA_INVALID_HANDLE:
  31. errno = EBADF;
  32. break;
  33. case WSA_NOT_ENOUGH_MEMORY:
  34. errno = ENOMEM;
  35. break;
  36. case WSA_INVALID_PARAMETER:
  37. errno = EINVAL;
  38. break;
  39. case WSAEWOULDBLOCK:
  40. errno = EWOULDBLOCK;
  41. break;
  42. case WSAENAMETOOLONG:
  43. errno = ENAMETOOLONG;
  44. break;
  45. case WSAENOTEMPTY:
  46. errno = ENOTEMPTY;
  47. break;
  48. default:
  49. errno = (err > 10000 && err < 10025) ? err - 10000 : err;
  50. break;
  51. }
  52. }