getsockopt.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* getsockopt.c --- wrappers for Windows getsockopt function
  2. Copyright (C) 2008-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 struct timeval. */
  19. #include <sys/time.h>
  20. /* Get memcpy. */
  21. #include <string.h>
  22. /* Get set_winsock_errno, FD_TO_SOCKET etc. */
  23. #include "w32sock.h"
  24. #undef getsockopt
  25. int
  26. rpl_getsockopt (int fd, int level, int optname, void *optval, socklen_t *optlen)
  27. {
  28. SOCKET sock = FD_TO_SOCKET (fd);
  29. if (sock == INVALID_SOCKET)
  30. {
  31. errno = EBADF;
  32. return -1;
  33. }
  34. else
  35. {
  36. int r;
  37. if (level == SOL_SOCKET
  38. && (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO))
  39. {
  40. int milliseconds;
  41. int milliseconds_len = sizeof (int);
  42. struct timeval tv;
  43. size_t n;
  44. r = getsockopt (sock, level, optname, (char *) &milliseconds,
  45. &milliseconds_len);
  46. tv.tv_sec = milliseconds / 1000;
  47. tv.tv_usec = (milliseconds - 1000 * tv.tv_sec) * 1000;
  48. n = sizeof (struct timeval);
  49. if (n > *optlen)
  50. n = *optlen;
  51. memcpy (optval, &tv, n);
  52. *optlen = n;
  53. }
  54. else
  55. {
  56. r = getsockopt (sock, level, optname, optval, optlen);
  57. }
  58. if (r < 0)
  59. set_winsock_errno ();
  60. return r;
  61. }
  62. }