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.

499 lines
14KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * UDP protocol
  24. */
  25. #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  26. #include "avformat.h"
  27. #include <unistd.h>
  28. #include "internal.h"
  29. #include "network.h"
  30. #include "os_support.h"
  31. #if HAVE_SYS_SELECT_H
  32. #include <sys/select.h>
  33. #endif
  34. #include <sys/time.h>
  35. #ifndef IPV6_ADD_MEMBERSHIP
  36. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  37. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  38. #endif
  39. #ifndef IN_MULTICAST
  40. #define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
  41. #endif
  42. #ifndef IN6_IS_ADDR_MULTICAST
  43. #define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
  44. #endif
  45. typedef struct {
  46. int udp_fd;
  47. int ttl;
  48. int buffer_size;
  49. int is_multicast;
  50. int local_port;
  51. int reuse_socket;
  52. struct sockaddr_storage dest_addr;
  53. int dest_addr_len;
  54. } UDPContext;
  55. #define UDP_TX_BUF_SIZE 32768
  56. #define UDP_MAX_PKT_SIZE 65536
  57. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  58. struct sockaddr *addr)
  59. {
  60. #ifdef IP_MULTICAST_TTL
  61. if (addr->sa_family == AF_INET) {
  62. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  63. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
  64. return -1;
  65. }
  66. }
  67. #endif
  68. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  69. if (addr->sa_family == AF_INET6) {
  70. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  71. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  72. return -1;
  73. }
  74. }
  75. #endif
  76. return 0;
  77. }
  78. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  79. {
  80. #ifdef IP_ADD_MEMBERSHIP
  81. if (addr->sa_family == AF_INET) {
  82. struct ip_mreq mreq;
  83. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  84. mreq.imr_interface.s_addr= INADDR_ANY;
  85. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  86. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
  87. return -1;
  88. }
  89. }
  90. #endif
  91. #if HAVE_STRUCT_IPV6_MREQ
  92. if (addr->sa_family == AF_INET6) {
  93. struct ipv6_mreq mreq6;
  94. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  95. mreq6.ipv6mr_interface= 0;
  96. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  97. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
  98. return -1;
  99. }
  100. }
  101. #endif
  102. return 0;
  103. }
  104. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  105. {
  106. #ifdef IP_DROP_MEMBERSHIP
  107. if (addr->sa_family == AF_INET) {
  108. struct ip_mreq mreq;
  109. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  110. mreq.imr_interface.s_addr= INADDR_ANY;
  111. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  112. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
  113. return -1;
  114. }
  115. }
  116. #endif
  117. #if HAVE_STRUCT_IPV6_MREQ
  118. if (addr->sa_family == AF_INET6) {
  119. struct ipv6_mreq mreq6;
  120. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  121. mreq6.ipv6mr_interface= 0;
  122. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  123. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
  124. return -1;
  125. }
  126. }
  127. #endif
  128. return 0;
  129. }
  130. static struct addrinfo* udp_resolve_host(const char *hostname, int port,
  131. int type, int family, int flags)
  132. {
  133. struct addrinfo hints, *res = 0;
  134. int error;
  135. char sport[16];
  136. const char *node = 0, *service = "0";
  137. if (port > 0) {
  138. snprintf(sport, sizeof(sport), "%d", port);
  139. service = sport;
  140. }
  141. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  142. node = hostname;
  143. }
  144. memset(&hints, 0, sizeof(hints));
  145. hints.ai_socktype = type;
  146. hints.ai_family = family;
  147. hints.ai_flags = flags;
  148. if ((error = getaddrinfo(node, service, &hints, &res))) {
  149. res = NULL;
  150. av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
  151. }
  152. return res;
  153. }
  154. static int udp_set_url(struct sockaddr_storage *addr,
  155. const char *hostname, int port)
  156. {
  157. struct addrinfo *res0;
  158. int addr_len;
  159. res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  160. if (res0 == 0) return AVERROR(EIO);
  161. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  162. addr_len = res0->ai_addrlen;
  163. freeaddrinfo(res0);
  164. return addr_len;
  165. }
  166. static int is_multicast_address(struct sockaddr_storage *addr)
  167. {
  168. if (addr->ss_family == AF_INET) {
  169. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  170. }
  171. #if HAVE_STRUCT_SOCKADDR_IN6
  172. if (addr->ss_family == AF_INET6) {
  173. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  174. }
  175. #endif
  176. return 0;
  177. }
  178. static int udp_socket_create(UDPContext *s,
  179. struct sockaddr_storage *addr, int *addr_len)
  180. {
  181. int udp_fd = -1;
  182. struct addrinfo *res0 = NULL, *res = NULL;
  183. int family = AF_UNSPEC;
  184. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  185. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  186. res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
  187. if (res0 == 0)
  188. goto fail;
  189. for (res = res0; res; res=res->ai_next) {
  190. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  191. if (udp_fd > 0) break;
  192. av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
  193. }
  194. if (udp_fd < 0)
  195. goto fail;
  196. memcpy(addr, res->ai_addr, res->ai_addrlen);
  197. *addr_len = res->ai_addrlen;
  198. freeaddrinfo(res0);
  199. return udp_fd;
  200. fail:
  201. if (udp_fd >= 0)
  202. closesocket(udp_fd);
  203. if(res0)
  204. freeaddrinfo(res0);
  205. return -1;
  206. }
  207. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  208. {
  209. char sbuf[sizeof(int)*3+1];
  210. if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
  211. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
  212. return -1;
  213. }
  214. return strtol(sbuf, NULL, 10);
  215. }
  216. /**
  217. * If no filename is given to av_open_input_file because you want to
  218. * get the local port first, then you must call this function to set
  219. * the remote server address.
  220. *
  221. * url syntax: udp://host:port[?option=val...]
  222. * option: 'ttl=n' : set the ttl value (for multicast only)
  223. * 'localport=n' : set the local port
  224. * 'pkt_size=n' : set max packet size
  225. * 'reuse=1' : enable reusing the socket
  226. *
  227. * @param s1 media file context
  228. * @param uri of the remote server
  229. * @return zero if no error.
  230. */
  231. int udp_set_remote_url(URLContext *h, const char *uri)
  232. {
  233. UDPContext *s = h->priv_data;
  234. char hostname[256];
  235. int port;
  236. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  237. /* set the destination address */
  238. s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
  239. if (s->dest_addr_len < 0) {
  240. return AVERROR(EIO);
  241. }
  242. s->is_multicast = is_multicast_address(&s->dest_addr);
  243. return 0;
  244. }
  245. /**
  246. * Return the local port used by the UDP connection
  247. * @param s1 media file context
  248. * @return the local port number
  249. */
  250. int udp_get_local_port(URLContext *h)
  251. {
  252. UDPContext *s = h->priv_data;
  253. return s->local_port;
  254. }
  255. /**
  256. * Return the udp file handle for select() usage to wait for several RTP
  257. * streams at the same time.
  258. * @param h media file context
  259. */
  260. #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
  261. static
  262. #endif
  263. int udp_get_file_handle(URLContext *h)
  264. {
  265. UDPContext *s = h->priv_data;
  266. return s->udp_fd;
  267. }
  268. /* put it in UDP context */
  269. /* return non zero if error */
  270. static int udp_open(URLContext *h, const char *uri, int flags)
  271. {
  272. char hostname[1024];
  273. int port, udp_fd = -1, tmp, bind_ret = -1;
  274. UDPContext *s = NULL;
  275. int is_output;
  276. const char *p;
  277. char buf[256];
  278. struct sockaddr_storage my_addr;
  279. int len;
  280. h->is_streamed = 1;
  281. h->max_packet_size = 1472;
  282. is_output = (flags & URL_WRONLY);
  283. s = av_mallocz(sizeof(UDPContext));
  284. if (!s)
  285. return AVERROR(ENOMEM);
  286. h->priv_data = s;
  287. s->ttl = 16;
  288. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  289. p = strchr(uri, '?');
  290. if (p) {
  291. s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
  292. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  293. s->ttl = strtol(buf, NULL, 10);
  294. }
  295. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  296. s->local_port = strtol(buf, NULL, 10);
  297. }
  298. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  299. h->max_packet_size = strtol(buf, NULL, 10);
  300. }
  301. if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  302. s->buffer_size = strtol(buf, NULL, 10);
  303. }
  304. }
  305. /* fill the dest addr */
  306. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  307. /* XXX: fix av_url_split */
  308. if (hostname[0] == '\0' || hostname[0] == '?') {
  309. /* only accepts null hostname if input */
  310. if (flags & URL_WRONLY)
  311. goto fail;
  312. } else {
  313. udp_set_remote_url(h, uri);
  314. }
  315. if (s->is_multicast && !(h->flags & URL_WRONLY))
  316. s->local_port = port;
  317. udp_fd = udp_socket_create(s, &my_addr, &len);
  318. if (udp_fd < 0)
  319. goto fail;
  320. if (s->reuse_socket)
  321. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  322. goto fail;
  323. /* the bind is needed to give a port to the socket now */
  324. /* if multicast, try the multicast address bind first */
  325. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  326. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  327. }
  328. /* bind to the local address if not multicast or if the multicast
  329. * bind failed */
  330. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
  331. goto fail;
  332. len = sizeof(my_addr);
  333. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  334. s->local_port = udp_port(&my_addr, len);
  335. if (s->is_multicast) {
  336. if (h->flags & URL_WRONLY) {
  337. /* output */
  338. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  339. goto fail;
  340. } else {
  341. /* input */
  342. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  343. goto fail;
  344. }
  345. }
  346. if (is_output) {
  347. /* limit the tx buf size to limit latency */
  348. tmp = s->buffer_size;
  349. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  350. av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
  351. goto fail;
  352. }
  353. } else {
  354. /* set udp recv buffer size to the largest possible udp packet size to
  355. * avoid losing data on OSes that set this too low by default. */
  356. tmp = s->buffer_size;
  357. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  358. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
  359. }
  360. /* make the socket non-blocking */
  361. ff_socket_nonblock(udp_fd, 1);
  362. }
  363. s->udp_fd = udp_fd;
  364. return 0;
  365. fail:
  366. if (udp_fd >= 0)
  367. closesocket(udp_fd);
  368. av_free(s);
  369. return AVERROR(EIO);
  370. }
  371. static int udp_read(URLContext *h, uint8_t *buf, int size)
  372. {
  373. UDPContext *s = h->priv_data;
  374. int len;
  375. fd_set rfds;
  376. int ret;
  377. struct timeval tv;
  378. for(;;) {
  379. if (url_interrupt_cb())
  380. return AVERROR(EINTR);
  381. FD_ZERO(&rfds);
  382. FD_SET(s->udp_fd, &rfds);
  383. tv.tv_sec = 0;
  384. tv.tv_usec = 100 * 1000;
  385. ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
  386. if (ret < 0) {
  387. if (ff_neterrno() == FF_NETERROR(EINTR))
  388. continue;
  389. return AVERROR(EIO);
  390. }
  391. if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
  392. continue;
  393. len = recv(s->udp_fd, buf, size, 0);
  394. if (len < 0) {
  395. if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
  396. ff_neterrno() != FF_NETERROR(EINTR))
  397. return AVERROR(EIO);
  398. } else {
  399. break;
  400. }
  401. }
  402. return len;
  403. }
  404. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  405. {
  406. UDPContext *s = h->priv_data;
  407. int ret;
  408. for(;;) {
  409. ret = sendto (s->udp_fd, buf, size, 0,
  410. (struct sockaddr *) &s->dest_addr,
  411. s->dest_addr_len);
  412. if (ret < 0) {
  413. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  414. ff_neterrno() != FF_NETERROR(EAGAIN))
  415. return AVERROR(EIO);
  416. } else {
  417. break;
  418. }
  419. }
  420. return size;
  421. }
  422. static int udp_close(URLContext *h)
  423. {
  424. UDPContext *s = h->priv_data;
  425. if (s->is_multicast && !(h->flags & URL_WRONLY))
  426. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  427. closesocket(s->udp_fd);
  428. av_free(s);
  429. return 0;
  430. }
  431. URLProtocol udp_protocol = {
  432. "udp",
  433. udp_open,
  434. udp_read,
  435. udp_write,
  436. NULL, /* seek */
  437. udp_close,
  438. .url_get_file_handle = udp_get_file_handle,
  439. };