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.

160 lines
4.0KB

  1. /*
  2. * Various utilities for ffmpeg system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * copyright (c) 2002 Francois Revol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /* needed by inet_aton() */
  23. #define _SVID_SOURCE
  24. #include "config.h"
  25. #include "avformat.h"
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include "os_support.h"
  29. #ifdef CONFIG_NETWORK
  30. #ifndef HAVE_POLL_H
  31. #ifdef HAVE_WINSOCK2_H
  32. #include <winsock2.h>
  33. #elif defined (HAVE_SYS_SELECT_H)
  34. #include <sys/select.h>
  35. #endif
  36. #endif
  37. #include "network.h"
  38. #if !defined(HAVE_INET_ATON)
  39. #include <stdlib.h>
  40. #include <strings.h>
  41. int inet_aton (const char * str, struct in_addr * add)
  42. {
  43. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  44. if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
  45. return 0;
  46. if (!add1 || (add1|add2|add3|add4) > 255) return 0;
  47. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  48. return 1;
  49. }
  50. #endif /* !defined(HAVE_INET_ATON) */
  51. /* resolve host with also IP address parsing */
  52. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  53. {
  54. struct hostent *hp;
  55. if (!inet_aton(hostname, sin_addr)) {
  56. hp = gethostbyname(hostname);
  57. if (!hp)
  58. return -1;
  59. memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  60. }
  61. return 0;
  62. }
  63. int ff_socket_nonblock(int socket, int enable)
  64. {
  65. #ifdef HAVE_WINSOCK2_H
  66. return ioctlsocket(socket, FIONBIO, &enable);
  67. #else
  68. if (enable)
  69. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  70. else
  71. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  72. #endif
  73. }
  74. #endif /* CONFIG_NETWORK */
  75. #ifdef CONFIG_FFSERVER
  76. #ifndef HAVE_POLL_H
  77. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  78. {
  79. fd_set read_set;
  80. fd_set write_set;
  81. fd_set exception_set;
  82. nfds_t i;
  83. int n;
  84. int rc;
  85. #ifdef HAVE_WINSOCK2_H
  86. if (numfds >= FD_SETSIZE) {
  87. errno = EINVAL;
  88. return -1;
  89. }
  90. #endif
  91. FD_ZERO(&read_set);
  92. FD_ZERO(&write_set);
  93. FD_ZERO(&exception_set);
  94. n = -1;
  95. for(i = 0; i < numfds; i++) {
  96. if (fds[i].fd < 0)
  97. continue;
  98. #ifndef HAVE_WINSOCK2_H
  99. if (fds[i].fd >= FD_SETSIZE) {
  100. errno = EINVAL;
  101. return -1;
  102. }
  103. #endif
  104. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  105. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  106. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  107. if (fds[i].fd > n)
  108. n = fds[i].fd;
  109. };
  110. if (n == -1)
  111. /* Hey!? Nothing to poll, in fact!!! */
  112. return 0;
  113. if (timeout < 0)
  114. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  115. else {
  116. struct timeval tv;
  117. tv.tv_sec = timeout / 1000;
  118. tv.tv_usec = 1000 * (timeout % 1000);
  119. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  120. };
  121. if (rc < 0)
  122. return rc;
  123. for(i = 0; i < (nfds_t) n; i++) {
  124. fds[i].revents = 0;
  125. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  126. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  127. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  128. };
  129. return rc;
  130. }
  131. #endif /* HAVE_POLL_H */
  132. #endif /* CONFIG_FFSERVER */