setsockopt.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* setsockopt.c --- wrappers for Windows setsockopt 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 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 <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 struct timeval. */
  19. #include <sys/time.h>
  20. /* Get set_winsock_errno, FD_TO_SOCKET etc. */
  21. #include "w32sock.h"
  22. #undef setsockopt
  23. int
  24. rpl_setsockopt (int fd, int level, int optname, const void *optval, socklen_t optlen)
  25. {
  26. SOCKET sock = FD_TO_SOCKET (fd);
  27. int r;
  28. if (sock == INVALID_SOCKET)
  29. {
  30. errno = EBADF;
  31. return -1;
  32. }
  33. else
  34. {
  35. if (level == SOL_SOCKET
  36. && (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  37. {
  38. const struct timeval *tv = optval;
  39. int milliseconds = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  40. optval = &milliseconds;
  41. r = setsockopt (sock, level, optname, optval, sizeof (int));
  42. }
  43. else
  44. {
  45. r = setsockopt (sock, level, optname, optval, optlen);
  46. }
  47. if (r < 0)
  48. set_winsock_errno ();
  49. return r;
  50. }
  51. }