You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
10KB

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