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.

305 lines
8.0KB

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