os_support.c 8.5 KB

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