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.

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