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.

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