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.

471 lines
13KB

  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. #endif
  40. size_t dest_addr_len;
  41. } UDPContext;
  42. #define UDP_TX_BUF_SIZE 32768
  43. #define UDP_MAX_PKT_SIZE 65536
  44. static int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
  45. #ifdef IP_MULTICAST_TTL
  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. #endif
  53. #ifdef CONFIG_IPV6
  54. if (addr->sa_family == AF_INET6) {
  55. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  56. perror("setsockopt(IPV6_MULTICAST_HOPS)");
  57. return -1;
  58. }
  59. }
  60. #endif
  61. return 0;
  62. }
  63. static int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
  64. #ifdef IP_ADD_MEMBERSHIP
  65. if (addr->sa_family == AF_INET) {
  66. struct ip_mreq mreq;
  67. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  68. mreq.imr_interface.s_addr= INADDR_ANY;
  69. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  70. perror("setsockopt(IP_ADD_MEMBERSHIP)");
  71. return -1;
  72. }
  73. }
  74. #endif
  75. #ifdef CONFIG_IPV6
  76. if (addr->sa_family == AF_INET6) {
  77. struct ipv6_mreq mreq6;
  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. #endif
  86. return 0;
  87. }
  88. static int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
  89. #ifdef IP_DROP_MEMBERSHIP
  90. if (addr->sa_family == AF_INET) {
  91. struct ip_mreq mreq;
  92. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  93. mreq.imr_interface.s_addr= INADDR_ANY;
  94. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  95. perror("setsockopt(IP_DROP_MEMBERSHIP)");
  96. return -1;
  97. }
  98. }
  99. #endif
  100. #ifdef CONFIG_IPV6
  101. if (addr->sa_family == AF_INET6) {
  102. struct ipv6_mreq mreq6;
  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. #endif
  111. return 0;
  112. }
  113. #ifdef CONFIG_IPV6
  114. static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
  115. struct addrinfo hints, *res = 0;
  116. int error;
  117. char sport[16];
  118. const char *node = 0, *service = "0";
  119. if (port > 0) {
  120. snprintf(sport, sizeof(sport), "%d", port);
  121. service = sport;
  122. }
  123. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  124. node = hostname;
  125. }
  126. memset(&hints, 0, sizeof(hints));
  127. hints.ai_socktype = type;
  128. hints.ai_family = family;
  129. hints.ai_flags = flags;
  130. if ((error = getaddrinfo(node, service, &hints, &res))) {
  131. av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
  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(EIO);
  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. int family = AF_UNSPEC;
  157. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  158. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  159. res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
  160. if (res0 == 0)
  161. goto fail;
  162. for (res = res0; res; res=res->ai_next) {
  163. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  164. if (udp_fd > 0) break;
  165. perror("socket");
  166. }
  167. if (udp_fd < 0)
  168. goto fail;
  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. addrlen = sizeof(clientaddr);
  176. if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
  177. perror("getsockname");
  178. goto fail;
  179. }
  180. if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
  181. perror("getnameinfo");
  182. goto fail;
  183. }
  184. s->local_port = strtol(sbuf, NULL, 10);
  185. return udp_fd;
  186. fail:
  187. if (udp_fd >= 0)
  188. closesocket(udp_fd);
  189. if(res0)
  190. freeaddrinfo(res0);
  191. return -1;
  192. }
  193. #endif /* CONFIG_IPV6 */
  194. /**
  195. * If no filename is given to av_open_input_file because you want to
  196. * get the local port first, then you must call this function to set
  197. * the remote server address.
  198. *
  199. * url syntax: udp://host:port[?option=val...]
  200. * option: 'multicast=1' : enable multicast
  201. * 'ttl=n' : set the ttl value (for multicast only)
  202. * 'localport=n' : set the local port
  203. * 'pkt_size=n' : set max packet size
  204. * 'reuse=1' : enable reusing the socket
  205. *
  206. * @param s1 media file context
  207. * @param uri of the remote server
  208. * @return zero if no error.
  209. */
  210. int udp_set_remote_url(URLContext *h, const char *uri)
  211. {
  212. #ifdef CONFIG_IPV6
  213. return udp_ipv6_set_remote_url(h, uri);
  214. #else
  215. UDPContext *s = h->priv_data;
  216. char hostname[256];
  217. int port;
  218. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  219. /* set the destination address */
  220. if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
  221. return AVERROR(EIO);
  222. s->dest_addr.sin_family = AF_INET;
  223. s->dest_addr.sin_port = htons(port);
  224. s->dest_addr_len = sizeof(s->dest_addr);
  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_mallocz(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. if(!ff_network_init())
  298. return AVERROR(EIO);
  299. #ifndef CONFIG_IPV6
  300. udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
  301. if (udp_fd < 0)
  302. goto fail;
  303. my_addr.sin_family = AF_INET;
  304. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  305. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  306. /* special case: the bind must be done on the multicast address port */
  307. my_addr.sin_port = s->dest_addr.sin_port;
  308. } else {
  309. my_addr.sin_port = htons(s->local_port);
  310. }
  311. if (s->reuse_socket)
  312. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  313. goto fail;
  314. /* the bind is needed to give a port to the socket now */
  315. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  316. goto fail;
  317. len = sizeof(my_addr1);
  318. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  319. s->local_port = ntohs(my_addr1.sin_port);
  320. #else
  321. if (s->is_multicast && !(h->flags & URL_WRONLY))
  322. s->local_port = port;
  323. udp_fd = udp_ipv6_set_local(h);
  324. if (udp_fd < 0)
  325. goto fail;
  326. #endif /* CONFIG_IPV6 */
  327. if (s->is_multicast) {
  328. if (h->flags & URL_WRONLY) {
  329. /* output */
  330. if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  331. goto fail;
  332. } else {
  333. /* input */
  334. if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  335. goto fail;
  336. }
  337. }
  338. if (is_output) {
  339. /* limit the tx buf size to limit latency */
  340. tmp = UDP_TX_BUF_SIZE;
  341. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  342. perror("setsockopt sndbuf");
  343. goto fail;
  344. }
  345. } else {
  346. /* set udp recv buffer size to the largest possible udp packet size to
  347. * avoid losing data on OSes that set this too low by default. */
  348. tmp = UDP_MAX_PKT_SIZE;
  349. setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
  350. }
  351. s->udp_fd = udp_fd;
  352. return 0;
  353. fail:
  354. if (udp_fd >= 0)
  355. closesocket(udp_fd);
  356. av_free(s);
  357. return AVERROR(EIO);
  358. }
  359. static int udp_read(URLContext *h, uint8_t *buf, int size)
  360. {
  361. UDPContext *s = h->priv_data;
  362. #ifndef CONFIG_IPV6
  363. struct sockaddr_in from;
  364. #else
  365. struct sockaddr_storage from;
  366. #endif
  367. socklen_t from_len;
  368. int len;
  369. for(;;) {
  370. from_len = sizeof(from);
  371. len = recvfrom (s->udp_fd, buf, size, 0,
  372. (struct sockaddr *)&from, &from_len);
  373. if (len < 0) {
  374. if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
  375. ff_neterrno() != FF_NETERROR(EINTR))
  376. return AVERROR(EIO);
  377. } else {
  378. break;
  379. }
  380. }
  381. return len;
  382. }
  383. static int udp_write(URLContext *h, uint8_t *buf, int size)
  384. {
  385. UDPContext *s = h->priv_data;
  386. int ret;
  387. for(;;) {
  388. ret = sendto (s->udp_fd, buf, size, 0,
  389. (struct sockaddr *) &s->dest_addr,
  390. s->dest_addr_len);
  391. if (ret < 0) {
  392. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  393. ff_neterrno() != FF_NETERROR(EAGAIN))
  394. return AVERROR(EIO);
  395. } else {
  396. break;
  397. }
  398. }
  399. return size;
  400. }
  401. static int udp_close(URLContext *h)
  402. {
  403. UDPContext *s = h->priv_data;
  404. if (s->is_multicast && !(h->flags & URL_WRONLY))
  405. udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  406. closesocket(s->udp_fd);
  407. ff_network_close();
  408. av_free(s);
  409. return 0;
  410. }
  411. URLProtocol udp_protocol = {
  412. "udp",
  413. udp_open,
  414. udp_read,
  415. udp_write,
  416. NULL, /* seek */
  417. udp_close,
  418. };