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.

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