os_support.c 3.9 KB

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