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.

335 lines
9.2KB

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