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.

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