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.

334 lines
9.2KB

  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. #include <share.h>
  30. #undef open
  31. int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
  32. {
  33. int fd;
  34. int num_chars;
  35. wchar_t *filename_w;
  36. /* convert UTF-8 to wide chars */
  37. num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
  38. if (num_chars <= 0)
  39. return -1;
  40. filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
  41. MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
  42. fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
  43. av_freep(&filename_w);
  44. /* filename maybe be in CP_ACP */
  45. if (fd == -1 && !(oflag & O_CREAT))
  46. return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
  47. return fd;
  48. }
  49. #endif
  50. #if CONFIG_NETWORK
  51. #include <fcntl.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. u_long param = enable;
  218. return ioctlsocket(socket, FIONBIO, &param);
  219. #else
  220. if (enable)
  221. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  222. else
  223. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  224. #endif
  225. }
  226. #if !HAVE_POLL_H
  227. int poll(struct pollfd *fds, nfds_t numfds, int timeout)
  228. {
  229. fd_set read_set;
  230. fd_set write_set;
  231. fd_set exception_set;
  232. nfds_t i;
  233. int n;
  234. int rc;
  235. #if HAVE_WINSOCK2_H
  236. if (numfds >= FD_SETSIZE) {
  237. errno = EINVAL;
  238. return -1;
  239. }
  240. #endif
  241. FD_ZERO(&read_set);
  242. FD_ZERO(&write_set);
  243. FD_ZERO(&exception_set);
  244. n = -1;
  245. for(i = 0; i < numfds; i++) {
  246. if (fds[i].fd < 0)
  247. continue;
  248. #if !HAVE_WINSOCK2_H
  249. if (fds[i].fd >= FD_SETSIZE) {
  250. errno = EINVAL;
  251. return -1;
  252. }
  253. #endif
  254. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  255. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  256. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  257. if (fds[i].fd > n)
  258. n = fds[i].fd;
  259. };
  260. if (n == -1)
  261. /* Hey!? Nothing to poll, in fact!!! */
  262. return 0;
  263. if (timeout < 0)
  264. rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
  265. else {
  266. struct timeval tv;
  267. tv.tv_sec = timeout / 1000;
  268. tv.tv_usec = 1000 * (timeout % 1000);
  269. rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
  270. };
  271. if (rc < 0)
  272. return rc;
  273. for(i = 0; i < numfds; i++) {
  274. fds[i].revents = 0;
  275. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  276. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  277. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  278. };
  279. return rc;
  280. }
  281. #endif /* HAVE_POLL_H */
  282. #endif /* CONFIG_NETWORK */