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.

488 lines
14KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #ifndef __BEOS__
  25. # include <arpa/inet.h>
  26. #else
  27. # include "barpainet.h"
  28. #endif
  29. #include <netdb.h>
  30. typedef struct {
  31. int udp_fd;
  32. int ttl;
  33. int is_multicast;
  34. int local_port;
  35. #ifndef CONFIG_IPV6
  36. struct ip_mreq mreq;
  37. struct sockaddr_in dest_addr;
  38. #else
  39. struct sockaddr_storage dest_addr;
  40. size_t dest_addr_len;
  41. #endif
  42. } UDPContext;
  43. #define UDP_TX_BUF_SIZE 32768
  44. #ifdef CONFIG_IPV6
  45. int udp_ipv6_is_multicast_address(const struct sockaddr *addr) {
  46. if (addr->sa_family == AF_INET)
  47. return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
  48. if (addr->sa_family == AF_INET6)
  49. return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
  50. return -1;
  51. }
  52. int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
  53. if (addr->sa_family == AF_INET) {
  54. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  55. perror("setsockopt(IP_MULTICAST_TTL)");
  56. return -1;
  57. }
  58. }
  59. if (addr->sa_family == AF_INET6) {
  60. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  61. perror("setsockopt(IPV6_MULTICAST_HOPS)");
  62. return -1;
  63. }
  64. }
  65. return 0;
  66. }
  67. int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
  68. struct ip_mreq mreq;
  69. struct ipv6_mreq mreq6;
  70. if (addr->sa_family == AF_INET) {
  71. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  72. mreq.imr_interface.s_addr= INADDR_ANY;
  73. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  74. perror("setsockopt(IP_ADD_MEMBERSHIP)");
  75. return -1;
  76. }
  77. }
  78. if (addr->sa_family == AF_INET6) {
  79. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  80. mreq6.ipv6mr_interface= 0;
  81. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  82. perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
  83. return -1;
  84. }
  85. }
  86. return 0;
  87. }
  88. int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
  89. struct ip_mreq mreq;
  90. struct ipv6_mreq mreq6;
  91. if (addr->sa_family == AF_INET) {
  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. if (addr->sa_family == AF_INET6) {
  100. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  101. mreq6.ipv6mr_interface= 0;
  102. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  103. perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
  104. return -1;
  105. }
  106. }
  107. return 0;
  108. }
  109. struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
  110. struct addrinfo hints, *res = 0;
  111. int error;
  112. char sport[16];
  113. const char *node = 0, *service = 0;
  114. if (port > 0) {
  115. sprintf(sport, "%d", port);
  116. service = sport;
  117. }
  118. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  119. node = hostname;
  120. }
  121. if ((node) || (service)) {
  122. memset(&hints, 0, sizeof(hints));
  123. hints.ai_socktype = type;
  124. hints.ai_family = family;
  125. hints.ai_flags = flags;
  126. if ((error = getaddrinfo(node, service, &hints, &res))) {
  127. fprintf(stderr, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
  128. }
  129. }
  130. return res;
  131. }
  132. int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
  133. UDPContext *s = h->priv_data;
  134. char hostname[256];
  135. int port;
  136. struct addrinfo *res0;
  137. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  138. res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  139. if (res0 == 0) return AVERROR_IO;
  140. memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
  141. s->dest_addr_len = res0->ai_addrlen;
  142. freeaddrinfo(res0);
  143. return 0;
  144. }
  145. int udp_ipv6_set_local(URLContext *h) {
  146. UDPContext *s = h->priv_data;
  147. int udp_fd = -1;
  148. struct sockaddr_storage clientaddr;
  149. socklen_t addrlen;
  150. char sbuf[NI_MAXSERV];
  151. char hbuf[NI_MAXHOST];
  152. struct addrinfo *res0;
  153. int family;
  154. if (s->local_port != 0) {
  155. res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
  156. if (res0 == 0) return -1;
  157. family = res0->ai_family;
  158. freeaddrinfo(res0);
  159. } else {
  160. family = s->dest_addr.ss_family;
  161. }
  162. udp_fd = socket(family, SOCK_DGRAM, 0);
  163. if (udp_fd < 0) {
  164. perror("socket");
  165. goto fail;
  166. }
  167. if (s->local_port != 0) {
  168. if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
  169. perror("bind");
  170. goto fail;
  171. }
  172. }
  173. addrlen = sizeof(clientaddr);
  174. if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
  175. perror("getsockname");
  176. goto fail;
  177. }
  178. if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
  179. perror("getnameinfo");
  180. goto fail;
  181. }
  182. s->local_port = strtol(sbuf, NULL, 10);
  183. return udp_fd;
  184. fail:
  185. if (udp_fd >= 0)
  186. #ifdef CONFIG_BEOS_NETSERVER
  187. closesocket(udp_fd);
  188. #else
  189. close(udp_fd);
  190. #endif
  191. return -1;
  192. }
  193. #endif
  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. *
  205. * @param s1 media file context
  206. * @param uri of the remote server
  207. * @return zero if no error.
  208. */
  209. int udp_set_remote_url(URLContext *h, const char *uri)
  210. {
  211. #ifdef CONFIG_IPV6
  212. return udp_ipv6_set_remote_url(h, uri);
  213. #else
  214. UDPContext *s = h->priv_data;
  215. char hostname[256];
  216. int port;
  217. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  218. /* set the destination address */
  219. if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
  220. return AVERROR_IO;
  221. s->dest_addr.sin_family = AF_INET;
  222. s->dest_addr.sin_port = htons(port);
  223. return 0;
  224. #endif
  225. }
  226. /**
  227. * Return the local port used by the UDP connexion
  228. * @param s1 media file context
  229. * @return the local port number
  230. */
  231. int udp_get_local_port(URLContext *h)
  232. {
  233. UDPContext *s = h->priv_data;
  234. return s->local_port;
  235. }
  236. /**
  237. * Return the udp file handle for select() usage to wait for several RTP
  238. * streams at the same time.
  239. * @param h media file context
  240. */
  241. int udp_get_file_handle(URLContext *h)
  242. {
  243. UDPContext *s = h->priv_data;
  244. return s->udp_fd;
  245. }
  246. /* put it in UDP context */
  247. /* return non zero if error */
  248. static int udp_open(URLContext *h, const char *uri, int flags)
  249. {
  250. struct sockaddr_in my_addr, my_addr1;
  251. char hostname[1024];
  252. int port, udp_fd = -1, tmp;
  253. UDPContext *s = NULL;
  254. int is_output, len;
  255. const char *p;
  256. char buf[256];
  257. h->is_streamed = 1;
  258. h->max_packet_size = 1472;
  259. is_output = (flags & URL_WRONLY);
  260. s = av_malloc(sizeof(UDPContext));
  261. if (!s)
  262. return -ENOMEM;
  263. h->priv_data = s;
  264. s->ttl = 16;
  265. s->is_multicast = 0;
  266. s->local_port = 0;
  267. p = strchr(uri, '?');
  268. if (p) {
  269. s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  270. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  271. s->ttl = strtol(buf, NULL, 10);
  272. }
  273. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  274. s->local_port = strtol(buf, NULL, 10);
  275. }
  276. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  277. h->max_packet_size = strtol(buf, NULL, 10);
  278. }
  279. }
  280. /* fill the dest addr */
  281. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  282. /* XXX: fix url_split */
  283. if (hostname[0] == '\0' || hostname[0] == '?') {
  284. /* only accepts null hostname if input */
  285. if (s->is_multicast || (flags & URL_WRONLY))
  286. goto fail;
  287. } else {
  288. udp_set_remote_url(h, uri);
  289. }
  290. #ifndef CONFIG_IPV6
  291. udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
  292. if (udp_fd < 0)
  293. goto fail;
  294. my_addr.sin_family = AF_INET;
  295. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  296. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  297. /* special case: the bind must be done on the multicast address port */
  298. my_addr.sin_port = s->dest_addr.sin_port;
  299. } else {
  300. my_addr.sin_port = htons(s->local_port);
  301. }
  302. /* the bind is needed to give a port to the socket now */
  303. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  304. goto fail;
  305. len = sizeof(my_addr1);
  306. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  307. s->local_port = ntohs(my_addr1.sin_port);
  308. #ifndef CONFIG_BEOS_NETSERVER
  309. if (s->is_multicast) {
  310. if (h->flags & URL_WRONLY) {
  311. /* output */
  312. if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
  313. &s->ttl, sizeof(s->ttl)) < 0) {
  314. perror("IP_MULTICAST_TTL");
  315. goto fail;
  316. }
  317. } else {
  318. /* input */
  319. memset(&s->mreq, 0, sizeof(s->mreq));
  320. s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
  321. s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  322. if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  323. &s->mreq, sizeof(s->mreq)) < 0) {
  324. perror("rtp: IP_ADD_MEMBERSHIP");
  325. goto fail;
  326. }
  327. }
  328. }
  329. #endif
  330. #else
  331. if (s->is_multicast && !(h->flags & URL_WRONLY))
  332. s->local_port = port;
  333. udp_fd = udp_ipv6_set_local(h);
  334. if (udp_fd < 0)
  335. goto fail;
  336. #ifndef CONFIG_BEOS_NETSERVER
  337. if (s->is_multicast) {
  338. if (h->flags & URL_WRONLY) {
  339. if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  340. goto fail;
  341. } else {
  342. if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  343. goto fail;
  344. }
  345. }
  346. #endif
  347. #endif
  348. if (is_output) {
  349. /* limit the tx buf size to limit latency */
  350. tmp = UDP_TX_BUF_SIZE;
  351. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  352. perror("setsockopt sndbuf");
  353. goto fail;
  354. }
  355. }
  356. s->udp_fd = udp_fd;
  357. return 0;
  358. fail:
  359. if (udp_fd >= 0)
  360. #ifdef CONFIG_BEOS_NETSERVER
  361. closesocket(udp_fd);
  362. #else
  363. close(udp_fd);
  364. #endif
  365. av_free(s);
  366. return AVERROR_IO;
  367. }
  368. static int udp_read(URLContext *h, uint8_t *buf, int size)
  369. {
  370. UDPContext *s = h->priv_data;
  371. #ifndef CONFIG_IPV6
  372. struct sockaddr_in from;
  373. #else
  374. struct sockaddr_storage from;
  375. #endif
  376. int from_len, len;
  377. for(;;) {
  378. from_len = sizeof(from);
  379. len = recvfrom (s->udp_fd, buf, size, 0,
  380. (struct sockaddr *)&from, &from_len);
  381. if (len < 0) {
  382. if (errno != EAGAIN && errno != EINTR)
  383. return AVERROR_IO;
  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 (errno != EINTR && errno != EAGAIN)
  404. return AVERROR_IO;
  405. } else {
  406. break;
  407. }
  408. }
  409. return size;
  410. }
  411. static int udp_close(URLContext *h)
  412. {
  413. UDPContext *s = h->priv_data;
  414. #ifndef CONFIG_BEOS_NETSERVER
  415. #ifndef CONFIG_IPV6
  416. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  417. if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
  418. &s->mreq, sizeof(s->mreq)) < 0) {
  419. perror("IP_DROP_MEMBERSHIP");
  420. }
  421. }
  422. #else
  423. if (s->is_multicast && !(h->flags & URL_WRONLY))
  424. udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  425. #endif
  426. close(s->udp_fd);
  427. #else
  428. closesocket(s->udp_fd);
  429. #endif
  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. };