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.

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