socket.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* socket.c --- wrappers for Windows socket function
  2. Copyright (C) 2008-2021 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 <https://www.gnu.org/licenses/>. */
  13. /* Written by Paolo Bonzini */
  14. #include <config.h>
  15. #define WIN32_LEAN_AND_MEAN
  16. /* Get winsock2.h. */
  17. #include <sys/socket.h>
  18. /* Get set_winsock_errno, FD_TO_SOCKET etc. */
  19. #include "w32sock.h"
  20. #include "sockets.h"
  21. /* Don't assume that UNICODE is defined. */
  22. #undef WSASocket
  23. #define WSASocket WSASocketW
  24. int
  25. rpl_socket (int domain, int type, int protocol)
  26. {
  27. SOCKET fh;
  28. gl_sockets_startup (SOCKETS_1_1);
  29. /* We have to use WSASocket() to create non-overlapped IO sockets.
  30. Overlapped IO sockets cannot be used with read/write. */
  31. fh = WSASocket (domain, type, protocol, NULL, 0, 0);
  32. if (fh == INVALID_SOCKET)
  33. {
  34. set_winsock_errno ();
  35. return -1;
  36. }
  37. else
  38. return SOCKET_TO_FD (fh);
  39. }