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.

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