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.

344 lines
9.6KB

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