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.

338 lines
9.3KB

  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) 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: ai->ai_protocol = IPPROTO_TCP; break;
  136. case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
  137. default: ai->ai_protocol = 0; break;
  138. }
  139. ai->ai_addr = (struct sockaddr *)sin;
  140. ai->ai_addrlen = sizeof(struct sockaddr_in);
  141. if (hints && (hints->ai_flags & AI_CANONNAME))
  142. ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
  143. ai->ai_next = NULL;
  144. return 0;
  145. }
  146. void ff_freeaddrinfo(struct addrinfo *res)
  147. {
  148. #if HAVE_WINSOCK2_H
  149. void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
  150. HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
  151. win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
  152. GetProcAddress(ws2mod, "freeaddrinfo");
  153. if (win_freeaddrinfo) {
  154. win_freeaddrinfo(res);
  155. return;
  156. }
  157. #endif
  158. av_free(res->ai_canonname);
  159. av_free(res->ai_addr);
  160. av_free(res);
  161. }
  162. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  163. char *host, int hostlen,
  164. char *serv, int servlen, int flags)
  165. {
  166. const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
  167. #if HAVE_WINSOCK2_H
  168. int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
  169. char *host, DWORD hostlen,
  170. char *serv, DWORD servlen, int flags);
  171. HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
  172. win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
  173. if (win_getnameinfo)
  174. return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
  175. #endif
  176. if (sa->sa_family != AF_INET)
  177. return EAI_FAMILY;
  178. if (!host && !serv)
  179. return EAI_NONAME;
  180. if (host && hostlen > 0) {
  181. struct hostent *ent = NULL;
  182. uint32_t a;
  183. if (!(flags & NI_NUMERICHOST))
  184. ent = gethostbyaddr((const char *)&sin->sin_addr,
  185. sizeof(sin->sin_addr), AF_INET);
  186. if (ent) {
  187. snprintf(host, hostlen, "%s", ent->h_name);
  188. } else if (flags & NI_NAMERQD) {
  189. return EAI_NONAME;
  190. } else {
  191. a = ntohl(sin->sin_addr.s_addr);
  192. snprintf(host, hostlen, "%d.%d.%d.%d",
  193. ((a >> 24) & 0xff), ((a >> 16) & 0xff),
  194. ((a >> 8) & 0xff), ( a & 0xff));
  195. }
  196. }
  197. if (serv && servlen > 0) {
  198. struct servent *ent = NULL;
  199. if (!(flags & NI_NUMERICSERV))
  200. ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
  201. if (ent) {
  202. snprintf(serv, servlen, "%s", ent->s_name);
  203. } else
  204. snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
  205. }
  206. return 0;
  207. }
  208. const char *ff_gai_strerror(int ecode)
  209. {
  210. switch(ecode) {
  211. case EAI_FAIL : return "A non-recoverable error occurred";
  212. case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
  213. case EAI_NONAME : return "The name does not resolve for the supplied parameters";
  214. }
  215. return "Unknown error";
  216. }
  217. #endif
  218. int ff_socket_nonblock(int socket, int enable)
  219. {
  220. #if HAVE_WINSOCK2_H
  221. u_long param = enable;
  222. return ioctlsocket(socket, FIONBIO, &param);
  223. #else
  224. if (enable)
  225. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  226. else
  227. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  228. #endif
  229. }
  230. #if !HAVE_POLL_H
  231. int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
  232. {
  233. fd_set read_set;
  234. fd_set write_set;
  235. fd_set exception_set;
  236. nfds_t i;
  237. int n;
  238. int rc;
  239. #if HAVE_WINSOCK2_H
  240. if (numfds >= FD_SETSIZE) {
  241. errno = EINVAL;
  242. return -1;
  243. }
  244. #endif
  245. FD_ZERO(&read_set);
  246. FD_ZERO(&write_set);
  247. FD_ZERO(&exception_set);
  248. n = 0;
  249. for(i = 0; i < numfds; i++) {
  250. if (fds[i].fd < 0)
  251. continue;
  252. #if !HAVE_WINSOCK2_H
  253. if (fds[i].fd >= FD_SETSIZE) {
  254. errno = EINVAL;
  255. return -1;
  256. }
  257. #endif
  258. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  259. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  260. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  261. if (fds[i].fd >= n)
  262. n = fds[i].fd + 1;
  263. };
  264. if (n == 0)
  265. /* Hey!? Nothing to poll, in fact!!! */
  266. return 0;
  267. if (timeout < 0)
  268. rc = select(n, &read_set, &write_set, &exception_set, NULL);
  269. else {
  270. struct timeval tv;
  271. tv.tv_sec = timeout / 1000;
  272. tv.tv_usec = 1000 * (timeout % 1000);
  273. rc = select(n, &read_set, &write_set, &exception_set, &tv);
  274. };
  275. if (rc < 0)
  276. return rc;
  277. for(i = 0; i < numfds; i++) {
  278. fds[i].revents = 0;
  279. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  280. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  281. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  282. };
  283. return rc;
  284. }
  285. #endif /* HAVE_POLL_H */
  286. #endif /* CONFIG_NETWORK */