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.

339 lines
9.5KB

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