/libavformat/os_support.c

http://github.com/FFmpeg/FFmpeg · C · 299 lines · 233 code · 41 blank · 25 comment · 53 complexity · f078c8a8efde6a3f4af10315e0f5cc0c MD5 · raw file

  1. /*
  2. * various OS-feature replacement utilities
  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 _DEFAULT_SOURCE
  24. #define _SVID_SOURCE
  25. #include "config.h"
  26. #include "avformat.h"
  27. #include "os_support.h"
  28. #if CONFIG_NETWORK
  29. #include <fcntl.h>
  30. #if !HAVE_POLL_H
  31. #if HAVE_SYS_TIME_H
  32. #include <sys/time.h>
  33. #endif /* HAVE_SYS_TIME_H */
  34. #if HAVE_WINSOCK2_H
  35. #include <winsock2.h>
  36. #elif HAVE_SYS_SELECT_H
  37. #include <sys/select.h>
  38. #endif /* HAVE_WINSOCK2_H */
  39. #endif /* !HAVE_POLL_H */
  40. #include "network.h"
  41. #if !HAVE_GETADDRINFO
  42. #if !HAVE_INET_ATON
  43. #include <stdlib.h>
  44. static int inet_aton(const char *str, struct in_addr *add)
  45. {
  46. unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
  47. if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
  48. return 0;
  49. if (!add1 || (add1 | add2 | add3 | add4) > 255)
  50. return 0;
  51. add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
  52. return 1;
  53. }
  54. #endif /* !HAVE_INET_ATON */
  55. int ff_getaddrinfo(const char *node, const char *service,
  56. const struct addrinfo *hints, struct addrinfo **res)
  57. {
  58. struct hostent *h = NULL;
  59. struct addrinfo *ai;
  60. struct sockaddr_in *sin;
  61. *res = NULL;
  62. sin = av_mallocz(sizeof(struct sockaddr_in));
  63. if (!sin)
  64. return EAI_FAIL;
  65. sin->sin_family = AF_INET;
  66. if (node) {
  67. if (!inet_aton(node, &sin->sin_addr)) {
  68. if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
  69. av_free(sin);
  70. return EAI_FAIL;
  71. }
  72. h = gethostbyname(node);
  73. if (!h) {
  74. av_free(sin);
  75. return EAI_FAIL;
  76. }
  77. memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
  78. }
  79. } else {
  80. if (hints && (hints->ai_flags & AI_PASSIVE))
  81. sin->sin_addr.s_addr = INADDR_ANY;
  82. else
  83. sin->sin_addr.s_addr = INADDR_LOOPBACK;
  84. }
  85. /* Note: getaddrinfo allows service to be a string, which
  86. * should be looked up using getservbyname. */
  87. if (service)
  88. sin->sin_port = htons(atoi(service));
  89. ai = av_mallocz(sizeof(struct addrinfo));
  90. if (!ai) {
  91. av_free(sin);
  92. return EAI_FAIL;
  93. }
  94. *res = ai;
  95. ai->ai_family = AF_INET;
  96. ai->ai_socktype = hints ? hints->ai_socktype : 0;
  97. switch (ai->ai_socktype) {
  98. case SOCK_STREAM:
  99. ai->ai_protocol = IPPROTO_TCP;
  100. break;
  101. case SOCK_DGRAM:
  102. ai->ai_protocol = IPPROTO_UDP;
  103. break;
  104. default:
  105. ai->ai_protocol = 0;
  106. break;
  107. }
  108. ai->ai_addr = (struct sockaddr *)sin;
  109. ai->ai_addrlen = sizeof(struct sockaddr_in);
  110. if (hints && (hints->ai_flags & AI_CANONNAME))
  111. ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
  112. ai->ai_next = NULL;
  113. return 0;
  114. }
  115. void ff_freeaddrinfo(struct addrinfo *res)
  116. {
  117. av_freep(&res->ai_canonname);
  118. av_freep(&res->ai_addr);
  119. av_freep(&res);
  120. }
  121. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  122. char *host, int hostlen,
  123. char *serv, int servlen, int flags)
  124. {
  125. const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
  126. if (sa->sa_family != AF_INET)
  127. return EAI_FAMILY;
  128. if (!host && !serv)
  129. return EAI_NONAME;
  130. if (host && hostlen > 0) {
  131. struct hostent *ent = NULL;
  132. uint32_t a;
  133. if (!(flags & NI_NUMERICHOST))
  134. ent = gethostbyaddr((const char *)&sin->sin_addr,
  135. sizeof(sin->sin_addr), AF_INET);
  136. if (ent) {
  137. snprintf(host, hostlen, "%s", ent->h_name);
  138. } else if (flags & NI_NAMERQD) {
  139. return EAI_NONAME;
  140. } else {
  141. a = ntohl(sin->sin_addr.s_addr);
  142. snprintf(host, hostlen, "%d.%d.%d.%d",
  143. ((a >> 24) & 0xff), ((a >> 16) & 0xff),
  144. ((a >> 8) & 0xff), (a & 0xff));
  145. }
  146. }
  147. if (serv && servlen > 0) {
  148. if (!(flags & NI_NUMERICSERV))
  149. return EAI_FAIL;
  150. snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
  151. }
  152. return 0;
  153. }
  154. #endif /* !HAVE_GETADDRINFO */
  155. #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
  156. const char *ff_gai_strerror(int ecode)
  157. {
  158. switch (ecode) {
  159. case EAI_AGAIN:
  160. return "Temporary failure in name resolution";
  161. case EAI_BADFLAGS:
  162. return "Invalid flags for ai_flags";
  163. case EAI_FAIL:
  164. return "A non-recoverable error occurred";
  165. case EAI_FAMILY:
  166. return "The address family was not recognized or the address "
  167. "length was invalid for the specified family";
  168. case EAI_MEMORY:
  169. return "Memory allocation failure";
  170. #if EAI_NODATA != EAI_NONAME
  171. case EAI_NODATA:
  172. return "No address associated with hostname";
  173. #endif /* EAI_NODATA != EAI_NONAME */
  174. case EAI_NONAME:
  175. return "The name does not resolve for the supplied parameters";
  176. case EAI_SERVICE:
  177. return "servname not supported for ai_socktype";
  178. case EAI_SOCKTYPE:
  179. return "ai_socktype not supported";
  180. }
  181. return "Unknown error";
  182. }
  183. #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
  184. int ff_socket_nonblock(int socket, int enable)
  185. {
  186. #if HAVE_WINSOCK2_H
  187. u_long param = enable;
  188. return ioctlsocket(socket, FIONBIO, &param);
  189. #else
  190. if (enable)
  191. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  192. else
  193. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  194. #endif /* HAVE_WINSOCK2_H */
  195. }
  196. #if !HAVE_POLL_H
  197. int ff_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 /* HAVE_WINSOCK2_H */
  211. FD_ZERO(&read_set);
  212. FD_ZERO(&write_set);
  213. FD_ZERO(&exception_set);
  214. n = 0;
  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 /* !HAVE_WINSOCK2_H */
  224. if (fds[i].events & POLLIN)
  225. FD_SET(fds[i].fd, &read_set);
  226. if (fds[i].events & POLLOUT)
  227. FD_SET(fds[i].fd, &write_set);
  228. if (fds[i].events & POLLERR)
  229. FD_SET(fds[i].fd, &exception_set);
  230. if (fds[i].fd >= n)
  231. n = fds[i].fd + 1;
  232. }
  233. if (n == 0)
  234. /* Hey!? Nothing to poll, in fact!!! */
  235. return 0;
  236. if (timeout < 0) {
  237. rc = select(n, &read_set, &write_set, &exception_set, NULL);
  238. } else {
  239. struct timeval tv;
  240. tv.tv_sec = timeout / 1000;
  241. tv.tv_usec = 1000 * (timeout % 1000);
  242. rc = select(n, &read_set, &write_set, &exception_set, &tv);
  243. }
  244. if (rc < 0)
  245. return rc;
  246. for (i = 0; i < numfds; i++) {
  247. fds[i].revents = 0;
  248. if (FD_ISSET(fds[i].fd, &read_set))
  249. fds[i].revents |= POLLIN;
  250. if (FD_ISSET(fds[i].fd, &write_set))
  251. fds[i].revents |= POLLOUT;
  252. if (FD_ISSET(fds[i].fd, &exception_set))
  253. fds[i].revents |= POLLERR;
  254. }
  255. return rc;
  256. }
  257. #endif /* !HAVE_POLL_H */
  258. #endif /* CONFIG_NETWORK */