os_support.c 10 KB

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