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