os_support.c 9.2 KB

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