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.

343 lines
9.5KB

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