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.

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