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.

252 lines
6.6KB

  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 = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
  49. return 1;
  50. }
  51. #endif /* !HAVE_INET_ATON */
  52. #if !HAVE_GETADDRINFO
  53. int ff_getaddrinfo(const char *node, const char *service,
  54. const struct addrinfo *hints, struct addrinfo **res)
  55. {
  56. struct hostent *h = NULL;
  57. struct addrinfo *ai;
  58. struct sockaddr_in *sin;
  59. sin = av_mallocz(sizeof(struct sockaddr_in));
  60. if (!sin)
  61. return EAI_FAIL;
  62. sin->sin_family = AF_INET;
  63. if (node) {
  64. if (!inet_aton(node, &sin->sin_addr)) {
  65. if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
  66. av_free(sin);
  67. return EAI_FAIL;
  68. }
  69. h = gethostbyname(node);
  70. if (!h) {
  71. av_free(sin);
  72. return EAI_FAIL;
  73. }
  74. memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
  75. }
  76. } else {
  77. if (hints && (hints->ai_flags & AI_PASSIVE)) {
  78. sin->sin_addr.s_addr = INADDR_ANY;
  79. } else
  80. sin->sin_addr.s_addr = INADDR_LOOPBACK;
  81. }
  82. /* Note: getaddrinfo allows service to be a string, which
  83. * should be looked up using getservbyname. */
  84. if (service)
  85. sin->sin_port = htons(atoi(service));
  86. ai = av_mallocz(sizeof(struct addrinfo));
  87. if (!ai) {
  88. av_free(sin);
  89. return EAI_FAIL;
  90. }
  91. *res = ai;
  92. ai->ai_family = AF_INET;
  93. ai->ai_socktype = hints ? hints->ai_socktype : 0;
  94. switch (ai->ai_socktype) {
  95. case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
  96. case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
  97. default: ai->ai_protocol = 0; break;
  98. }
  99. ai->ai_addr = (struct sockaddr *)sin;
  100. ai->ai_addrlen = sizeof(struct sockaddr_in);
  101. if (hints && (hints->ai_flags & AI_CANONNAME))
  102. ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
  103. ai->ai_next = NULL;
  104. return 0;
  105. }
  106. void ff_freeaddrinfo(struct addrinfo *res)
  107. {
  108. av_free(res->ai_canonname);
  109. av_free(res->ai_addr);
  110. av_free(res);
  111. }
  112. #endif
  113. /* resolve host with also IP address parsing */
  114. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  115. {
  116. if (!inet_aton(hostname, sin_addr)) {
  117. #if HAVE_GETADDRINFO
  118. struct addrinfo *ai, *cur;
  119. struct addrinfo hints;
  120. memset(&hints, 0, sizeof(hints));
  121. hints.ai_family = AF_INET;
  122. if (getaddrinfo(hostname, NULL, &hints, &ai))
  123. return -1;
  124. /* getaddrinfo returns a linked list of addrinfo structs.
  125. * Even if we set ai_family = AF_INET above, make sure
  126. * that the returned one actually is of the correct type. */
  127. for (cur = ai; cur; cur = cur->ai_next) {
  128. if (cur->ai_family == AF_INET) {
  129. *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
  130. freeaddrinfo(ai);
  131. return 0;
  132. }
  133. }
  134. freeaddrinfo(ai);
  135. return -1;
  136. #else
  137. struct hostent *hp;
  138. hp = gethostbyname(hostname);
  139. if (!hp)
  140. return -1;
  141. memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  142. #endif
  143. }
  144. return 0;
  145. }
  146. int ff_socket_nonblock(int socket, int enable)
  147. {
  148. #if HAVE_WINSOCK2_H
  149. return ioctlsocket(socket, FIONBIO, &enable);
  150. #else
  151. if (enable)
  152. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  153. else
  154. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  155. #endif
  156. }
  157. #endif /* CONFIG_NETWORK */
  158. #if CONFIG_FFSERVER
  159. #if !HAVE_POLL_H
  160. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  161. {
  162. fd_set read_set;
  163. fd_set write_set;
  164. fd_set exception_set;
  165. nfds_t i;
  166. int n;
  167. int rc;
  168. #if HAVE_WINSOCK2_H
  169. if (numfds >= FD_SETSIZE) {
  170. errno = EINVAL;
  171. return -1;
  172. }
  173. #endif
  174. FD_ZERO(&read_set);
  175. FD_ZERO(&write_set);
  176. FD_ZERO(&exception_set);
  177. n = -1;
  178. for(i = 0; i < numfds; i++) {
  179. if (fds[i].fd < 0)
  180. continue;
  181. #if !HAVE_WINSOCK2_H
  182. if (fds[i].fd >= FD_SETSIZE) {
  183. errno = EINVAL;
  184. return -1;
  185. }
  186. #endif
  187. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  188. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  189. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  190. if (fds[i].fd > n)
  191. n = fds[i].fd;
  192. };
  193. if (n == -1)
  194. /* Hey!? Nothing to poll, in fact!!! */
  195. return 0;
  196. if (timeout < 0)
  197. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  198. else {
  199. struct timeval tv;
  200. tv.tv_sec = timeout / 1000;
  201. tv.tv_usec = 1000 * (timeout % 1000);
  202. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  203. };
  204. if (rc < 0)
  205. return rc;
  206. for(i = 0; i < (nfds_t) n; i++) {
  207. fds[i].revents = 0;
  208. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  209. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  210. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  211. };
  212. return rc;
  213. }
  214. #endif /* HAVE_POLL_H */
  215. #endif /* CONFIG_FFSERVER */