w32sock.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* w32sock.h --- internal auxilliary functions for Windows socket functions
  2. Copyright (C) 2008, 2009, 2010 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 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 General Public License for more details.
  11. You should have received a copy of the GNU 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 _get_osfhandle() and _open_osfhandle(). */
  18. #include <io.h>
  19. #define FD_TO_SOCKET(fd) ((SOCKET) _get_osfhandle ((fd)))
  20. #define SOCKET_TO_FD(fh) (_open_osfhandle ((long) (fh), O_RDWR | O_BINARY))
  21. static inline void
  22. set_winsock_errno (void)
  23. {
  24. int err = WSAGetLastError ();
  25. /* Map some WSAE* errors to the runtime library's error codes. */
  26. switch (err)
  27. {
  28. case WSA_INVALID_HANDLE:
  29. errno = EBADF;
  30. break;
  31. case WSA_NOT_ENOUGH_MEMORY:
  32. errno = ENOMEM;
  33. break;
  34. case WSA_INVALID_PARAMETER:
  35. errno = EINVAL;
  36. break;
  37. case WSAEWOULDBLOCK:
  38. errno = EWOULDBLOCK;
  39. break;
  40. case WSAENAMETOOLONG:
  41. errno = ENAMETOOLONG;
  42. break;
  43. case WSAENOTEMPTY:
  44. errno = ENOTEMPTY;
  45. break;
  46. default:
  47. errno = (err > 10000 && err < 10025) ? err - 10000 : err;
  48. break;
  49. }
  50. }