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.

165 lines
4.1KB

  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. #include "config.h"
  23. #include "avformat.h"
  24. #if !defined(__MINGW32__)
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #endif
  28. #ifndef HAVE_SYS_POLL_H
  29. #if defined(__MINGW32__)
  30. #include <winsock2.h>
  31. #else
  32. #include <sys/select.h>
  33. #endif
  34. #endif
  35. #ifdef CONFIG_NETWORK
  36. #include "network.h"
  37. #if !defined(HAVE_INET_ATON)
  38. #include <stdlib.h>
  39. #include <strings.h>
  40. int inet_aton (const char * str, struct in_addr * add)
  41. {
  42. const char * pch = str;
  43. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  44. add1 = atoi(pch);
  45. pch = strpbrk(pch,".");
  46. if (pch == 0 || ++pch == 0) goto done;
  47. add2 = atoi(pch);
  48. pch = strpbrk(pch,".");
  49. if (pch == 0 || ++pch == 0) goto done;
  50. add3 = atoi(pch);
  51. pch = strpbrk(pch,".");
  52. if (pch == 0 || ++pch == 0) goto done;
  53. add4 = atoi(pch);
  54. done:
  55. add->s_addr=(add4<<24)+(add3<<16)+(add2<<8)+add1;
  56. return 1;
  57. }
  58. #endif /* !defined(HAVE_INET_ATON) */
  59. /* resolve host with also IP address parsing */
  60. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  61. {
  62. struct hostent *hp;
  63. if (!inet_aton(hostname, sin_addr)) {
  64. hp = gethostbyname(hostname);
  65. if (!hp)
  66. return -1;
  67. memcpy(sin_addr, hp->h_addr, sizeof(struct in_addr));
  68. }
  69. return 0;
  70. }
  71. int ff_socket_nonblock(int socket, int enable)
  72. {
  73. #ifdef __MINGW32__
  74. return ioctlsocket(socket, FIONBIO, &enable);
  75. #else
  76. if (enable)
  77. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  78. else
  79. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  80. #endif
  81. }
  82. #endif /* CONFIG_NETWORK */
  83. #ifdef CONFIG_FFSERVER
  84. #ifndef HAVE_SYS_POLL_H
  85. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  86. {
  87. fd_set read_set;
  88. fd_set write_set;
  89. fd_set exception_set;
  90. nfds_t i;
  91. int n;
  92. int rc;
  93. #ifdef __MINGW32__
  94. if (numfds >= FD_SETSIZE) {
  95. errno = EINVAL;
  96. return -1;
  97. }
  98. #endif
  99. FD_ZERO(&read_set);
  100. FD_ZERO(&write_set);
  101. FD_ZERO(&exception_set);
  102. n = -1;
  103. for(i = 0; i < numfds; i++) {
  104. if (fds[i].fd < 0)
  105. continue;
  106. #ifndef __MINGW32__
  107. if (fds[i].fd >= FD_SETSIZE) {
  108. errno = EINVAL;
  109. return -1;
  110. }
  111. #endif
  112. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  113. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  114. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  115. if (fds[i].fd > n)
  116. n = fds[i].fd;
  117. };
  118. if (n == -1)
  119. /* Hey!? Nothing to poll, in fact!!! */
  120. return 0;
  121. if (timeout < 0)
  122. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  123. else {
  124. struct timeval tv;
  125. tv.tv_sec = timeout / 1000;
  126. tv.tv_usec = 1000 * (timeout % 1000);
  127. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  128. };
  129. if (rc < 0)
  130. return rc;
  131. for(i = 0; i < (nfds_t) n; i++) {
  132. fds[i].revents = 0;
  133. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  134. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  135. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  136. };
  137. return rc;
  138. }
  139. #endif /* HAVE_SYS_POLL_H */
  140. #endif /* CONFIG_FFSERVER */