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.

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