os_support.c 9.2 KB

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