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.

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