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.

337 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 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) return 0;
  72. add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
  73. return 1;
  74. }
  75. #else
  76. int ff_inet_aton (const char * str, struct in_addr * add)
  77. {
  78. return inet_aton(str, add);
  79. }
  80. #endif /* !HAVE_INET_ATON */
  81. #if !HAVE_GETADDRINFO
  82. int ff_getaddrinfo(const char *node, const char *service,
  83. const struct addrinfo *hints, struct addrinfo **res)
  84. {
  85. struct hostent *h = NULL;
  86. struct addrinfo *ai;
  87. struct sockaddr_in *sin;
  88. #if HAVE_WINSOCK2_H
  89. int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
  90. const struct addrinfo *hints,
  91. struct addrinfo **res);
  92. HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
  93. win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
  94. if (win_getaddrinfo)
  95. return win_getaddrinfo(node, service, hints, res);
  96. #endif
  97. *res = NULL;
  98. sin = av_mallocz(sizeof(struct sockaddr_in));
  99. if (!sin)
  100. return EAI_FAIL;
  101. sin->sin_family = AF_INET;
  102. if (node) {
  103. if (!ff_inet_aton(node, &sin->sin_addr)) {
  104. if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
  105. av_free(sin);
  106. return EAI_FAIL;
  107. }
  108. h = gethostbyname(node);
  109. if (!h) {
  110. av_free(sin);
  111. return EAI_FAIL;
  112. }
  113. memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
  114. }
  115. } else {
  116. if (hints && (hints->ai_flags & AI_PASSIVE)) {
  117. sin->sin_addr.s_addr = INADDR_ANY;
  118. } else
  119. sin->sin_addr.s_addr = INADDR_LOOPBACK;
  120. }
  121. /* Note: getaddrinfo allows service to be a string, which
  122. * should be looked up using getservbyname. */
  123. if (service)
  124. sin->sin_port = htons(atoi(service));
  125. ai = av_mallocz(sizeof(struct addrinfo));
  126. if (!ai) {
  127. av_free(sin);
  128. return EAI_FAIL;
  129. }
  130. *res = ai;
  131. ai->ai_family = AF_INET;
  132. ai->ai_socktype = hints ? hints->ai_socktype : 0;
  133. switch (ai->ai_socktype) {
  134. case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
  135. case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
  136. default: ai->ai_protocol = 0; break;
  137. }
  138. ai->ai_addr = (struct sockaddr *)sin;
  139. ai->ai_addrlen = sizeof(struct sockaddr_in);
  140. if (hints && (hints->ai_flags & AI_CANONNAME))
  141. ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
  142. ai->ai_next = NULL;
  143. return 0;
  144. }
  145. void ff_freeaddrinfo(struct addrinfo *res)
  146. {
  147. #if HAVE_WINSOCK2_H
  148. void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
  149. HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
  150. win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
  151. GetProcAddress(ws2mod, "freeaddrinfo");
  152. if (win_freeaddrinfo) {
  153. win_freeaddrinfo(res);
  154. return;
  155. }
  156. #endif
  157. av_free(res->ai_canonname);
  158. av_free(res->ai_addr);
  159. av_free(res);
  160. }
  161. int ff_getnameinfo(const struct sockaddr *sa, int salen,
  162. char *host, int hostlen,
  163. char *serv, int servlen, int flags)
  164. {
  165. const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
  166. #if HAVE_WINSOCK2_H
  167. int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
  168. char *host, DWORD hostlen,
  169. char *serv, DWORD servlen, int flags);
  170. HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
  171. win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
  172. if (win_getnameinfo)
  173. return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
  174. #endif
  175. if (sa->sa_family != AF_INET)
  176. return EAI_FAMILY;
  177. if (!host && !serv)
  178. return EAI_NONAME;
  179. if (host && hostlen > 0) {
  180. struct hostent *ent = NULL;
  181. uint32_t a;
  182. if (!(flags & NI_NUMERICHOST))
  183. ent = gethostbyaddr((const char *)&sin->sin_addr,
  184. sizeof(sin->sin_addr), AF_INET);
  185. if (ent) {
  186. snprintf(host, hostlen, "%s", ent->h_name);
  187. } else if (flags & NI_NAMERQD) {
  188. return EAI_NONAME;
  189. } else {
  190. a = ntohl(sin->sin_addr.s_addr);
  191. snprintf(host, hostlen, "%d.%d.%d.%d",
  192. ((a >> 24) & 0xff), ((a >> 16) & 0xff),
  193. ((a >> 8) & 0xff), ( a & 0xff));
  194. }
  195. }
  196. if (serv && servlen > 0) {
  197. struct servent *ent = NULL;
  198. if (!(flags & NI_NUMERICSERV))
  199. ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
  200. if (ent) {
  201. snprintf(serv, servlen, "%s", ent->s_name);
  202. } else
  203. snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
  204. }
  205. return 0;
  206. }
  207. const char *ff_gai_strerror(int ecode)
  208. {
  209. switch(ecode) {
  210. case EAI_FAIL : return "A non-recoverable error occurred";
  211. case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
  212. case EAI_NONAME : return "The name does not resolve for the supplied parameters";
  213. }
  214. return "Unknown error";
  215. }
  216. #endif
  217. int ff_socket_nonblock(int socket, int enable)
  218. {
  219. #if HAVE_WINSOCK2_H
  220. u_long param = enable;
  221. return ioctlsocket(socket, FIONBIO, &param);
  222. #else
  223. if (enable)
  224. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
  225. else
  226. return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
  227. #endif
  228. }
  229. #if !HAVE_POLL_H
  230. int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
  231. {
  232. fd_set read_set;
  233. fd_set write_set;
  234. fd_set exception_set;
  235. nfds_t i;
  236. int n;
  237. int rc;
  238. #if HAVE_WINSOCK2_H
  239. if (numfds >= FD_SETSIZE) {
  240. errno = EINVAL;
  241. return -1;
  242. }
  243. #endif
  244. FD_ZERO(&read_set);
  245. FD_ZERO(&write_set);
  246. FD_ZERO(&exception_set);
  247. n = 0;
  248. for(i = 0; i < numfds; i++) {
  249. if (fds[i].fd < 0)
  250. continue;
  251. #if !HAVE_WINSOCK2_H
  252. if (fds[i].fd >= FD_SETSIZE) {
  253. errno = EINVAL;
  254. return -1;
  255. }
  256. #endif
  257. if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
  258. if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
  259. if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
  260. if (fds[i].fd >= n)
  261. n = fds[i].fd + 1;
  262. };
  263. if (n == 0)
  264. /* Hey!? Nothing to poll, in fact!!! */
  265. return 0;
  266. if (timeout < 0)
  267. rc = select(n, &read_set, &write_set, &exception_set, NULL);
  268. else {
  269. struct timeval tv;
  270. tv.tv_sec = timeout / 1000;
  271. tv.tv_usec = 1000 * (timeout % 1000);
  272. rc = select(n, &read_set, &write_set, &exception_set, &tv);
  273. };
  274. if (rc < 0)
  275. return rc;
  276. for(i = 0; i < numfds; i++) {
  277. fds[i].revents = 0;
  278. if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
  279. if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
  280. if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
  281. };
  282. return rc;
  283. }
  284. #endif /* HAVE_POLL_H */
  285. #endif /* CONFIG_NETWORK */