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.

296 lines
7.9KB

  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. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  113. char *host, int hostlen,
  114. char *serv, int servlen, int flags)
  115. {
  116. const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
  117. if (sa->sa_family != AF_INET)
  118. return EAI_FAMILY;
  119. if (!host && !serv)
  120. return EAI_NONAME;
  121. if (host && hostlen > 0) {
  122. struct hostent *ent = NULL;
  123. uint32_t a;
  124. if (!(flags & NI_NUMERICHOST))
  125. ent = gethostbyaddr((const char *)&sin->sin_addr,
  126. sizeof(sin->sin_addr), AF_INET);
  127. if (ent) {
  128. snprintf(host, hostlen, "%s", ent->h_name);
  129. } else if (flags & NI_NAMERQD) {
  130. return EAI_NONAME;
  131. } else {
  132. a = ntohl(sin->sin_addr.s_addr);
  133. snprintf(host, hostlen, "%d.%d.%d.%d",
  134. ((a >> 24) & 0xff), ((a >> 16) & 0xff),
  135. ((a >> 8) & 0xff), ( a & 0xff));
  136. }
  137. }
  138. if (serv && servlen > 0) {
  139. struct servent *ent = NULL;
  140. if (!(flags & NI_NUMERICSERV))
  141. ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
  142. if (ent) {
  143. snprintf(serv, servlen, "%s", ent->s_name);
  144. } else
  145. snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
  146. }
  147. return 0;
  148. }
  149. #endif
  150. /* resolve host with also IP address parsing */
  151. int resolve_host(struct in_addr *sin_addr, const char *hostname)
  152. {
  153. if (!inet_aton(hostname, sin_addr)) {
  154. #if HAVE_GETADDRINFO
  155. struct addrinfo *ai, *cur;
  156. struct addrinfo hints;
  157. memset(&hints, 0, sizeof(hints));
  158. hints.ai_family = AF_INET;
  159. if (getaddrinfo(hostname, NULL, &hints, &ai))
  160. return -1;
  161. /* getaddrinfo returns a linked list of addrinfo structs.
  162. * Even if we set ai_family = AF_INET above, make sure
  163. * that the returned one actually is of the correct type. */
  164. for (cur = ai; cur; cur = cur->ai_next) {
  165. if (cur->ai_family == AF_INET) {
  166. *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
  167. freeaddrinfo(ai);
  168. return 0;
  169. }
  170. }
  171. freeaddrinfo(ai);
  172. return -1;
  173. #else
  174. struct hostent *hp;
  175. hp = gethostbyname(hostname);
  176. if (!hp)
  177. return -1;
  178. memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  179. #endif
  180. }
  181. return 0;
  182. }
  183. int ff_socket_nonblock(int socket, int enable)
  184. {
  185. #if HAVE_WINSOCK2_H
  186. return ioctlsocket(socket, FIONBIO, &enable);
  187. #else
  188. if (enable)
  189. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  190. else
  191. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  192. #endif
  193. }
  194. #endif /* CONFIG_NETWORK */
  195. #if CONFIG_FFSERVER
  196. #if !HAVE_POLL_H
  197. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  198. {
  199. fd_set read_set;
  200. fd_set write_set;
  201. fd_set exception_set;
  202. nfds_t i;
  203. int n;
  204. int rc;
  205. #if HAVE_WINSOCK2_H
  206. if (numfds >= FD_SETSIZE) {
  207. errno = EINVAL;
  208. return -1;
  209. }
  210. #endif
  211. FD_ZERO(&read_set);
  212. FD_ZERO(&write_set);
  213. FD_ZERO(&exception_set);
  214. n = -1;
  215. for(i = 0; i < numfds; i++) {
  216. if (fds[i].fd < 0)
  217. continue;
  218. #if !HAVE_WINSOCK2_H
  219. if (fds[i].fd >= FD_SETSIZE) {
  220. errno = EINVAL;
  221. return -1;
  222. }
  223. #endif
  224. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  225. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  226. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  227. if (fds[i].fd > n)
  228. n = fds[i].fd;
  229. };
  230. if (n == -1)
  231. /* Hey!? Nothing to poll, in fact!!! */
  232. return 0;
  233. if (timeout < 0)
  234. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  235. else {
  236. struct timeval tv;
  237. tv.tv_sec = timeout / 1000;
  238. tv.tv_usec = 1000 * (timeout % 1000);
  239. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  240. };
  241. if (rc < 0)
  242. return rc;
  243. for(i = 0; i < (nfds_t) n; i++) {
  244. fds[i].revents = 0;
  245. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  246. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  247. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  248. };
  249. return rc;
  250. }
  251. #endif /* HAVE_POLL_H */
  252. #endif /* CONFIG_FFSERVER */