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.

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