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.

369 lines
10.0KB

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