You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9KB

  1. /*
  2. * Copyright (c) 2007 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "network.h"
  21. int ff_network_init(void)
  22. {
  23. #if HAVE_WINSOCK2_H
  24. WSADATA wsaData;
  25. if (WSAStartup(MAKEWORD(1,1), &wsaData))
  26. return 0;
  27. #endif
  28. return 1;
  29. }
  30. int ff_network_wait_fd(int fd, int write)
  31. {
  32. int ev = write ? POLLOUT : POLLIN;
  33. struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
  34. int ret;
  35. ret = poll(&p, 1, 100);
  36. return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
  37. }
  38. void ff_network_close(void)
  39. {
  40. #if HAVE_WINSOCK2_H
  41. WSACleanup();
  42. #endif
  43. }
  44. #if HAVE_WINSOCK2_H
  45. int ff_neterrno(void)
  46. {
  47. int err = WSAGetLastError();
  48. switch (err) {
  49. case WSAEWOULDBLOCK:
  50. return AVERROR(EAGAIN);
  51. case WSAEINTR:
  52. return AVERROR(EINTR);
  53. }
  54. return -err;
  55. }
  56. #endif
  57. int ff_is_multicast_address(struct sockaddr *addr)
  58. {
  59. if (addr->sa_family == AF_INET) {
  60. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  61. }
  62. #if HAVE_STRUCT_SOCKADDR_IN6
  63. if (addr->sa_family == AF_INET6) {
  64. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  65. }
  66. #endif
  67. return 0;
  68. }