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.

495 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. snprintf(sport, sizeof(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 = NULL;
  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)
  157. goto fail;
  158. family = res0->ai_family;
  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. freeaddrinfo(res0);
  173. res0 = NULL;
  174. }
  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. #ifdef CONFIG_BEOS_NETSERVER
  189. closesocket(udp_fd);
  190. #else
  191. close(udp_fd);
  192. #endif
  193. if(res0)
  194. freeaddrinfo(res0);
  195. return -1;
  196. }
  197. #endif
  198. /**
  199. * If no filename is given to av_open_input_file because you want to
  200. * get the local port first, then you must call this function to set
  201. * the remote server address.
  202. *
  203. * url syntax: udp://host:port[?option=val...]
  204. * option: 'multicast=1' : enable multicast
  205. * 'ttl=n' : set the ttl value (for multicast only)
  206. * 'localport=n' : set the local port
  207. * 'pkt_size=n' : set max packet size
  208. *
  209. * @param s1 media file context
  210. * @param uri of the remote server
  211. * @return zero if no error.
  212. */
  213. int udp_set_remote_url(URLContext *h, const char *uri)
  214. {
  215. #ifdef CONFIG_IPV6
  216. return udp_ipv6_set_remote_url(h, uri);
  217. #else
  218. UDPContext *s = h->priv_data;
  219. char hostname[256];
  220. int port;
  221. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  222. /* set the destination address */
  223. if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
  224. return AVERROR_IO;
  225. s->dest_addr.sin_family = AF_INET;
  226. s->dest_addr.sin_port = htons(port);
  227. return 0;
  228. #endif
  229. }
  230. /**
  231. * Return the local port used by the UDP connexion
  232. * @param s1 media file context
  233. * @return the local port number
  234. */
  235. int udp_get_local_port(URLContext *h)
  236. {
  237. UDPContext *s = h->priv_data;
  238. return s->local_port;
  239. }
  240. /**
  241. * Return the udp file handle for select() usage to wait for several RTP
  242. * streams at the same time.
  243. * @param h media file context
  244. */
  245. int udp_get_file_handle(URLContext *h)
  246. {
  247. UDPContext *s = h->priv_data;
  248. return s->udp_fd;
  249. }
  250. /* put it in UDP context */
  251. /* return non zero if error */
  252. static int udp_open(URLContext *h, const char *uri, int flags)
  253. {
  254. char hostname[1024];
  255. int port, udp_fd = -1, tmp;
  256. UDPContext *s = NULL;
  257. int is_output;
  258. const char *p;
  259. char buf[256];
  260. #ifndef CONFIG_IPV6
  261. struct sockaddr_in my_addr, my_addr1;
  262. int len;
  263. #endif
  264. h->is_streamed = 1;
  265. h->max_packet_size = 1472;
  266. is_output = (flags & URL_WRONLY);
  267. s = av_malloc(sizeof(UDPContext));
  268. if (!s)
  269. return -ENOMEM;
  270. h->priv_data = s;
  271. s->ttl = 16;
  272. s->is_multicast = 0;
  273. s->local_port = 0;
  274. p = strchr(uri, '?');
  275. if (p) {
  276. s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", 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(PF_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. /* the bind is needed to give a port to the socket now */
  310. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  311. goto fail;
  312. len = sizeof(my_addr1);
  313. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  314. s->local_port = ntohs(my_addr1.sin_port);
  315. #ifndef CONFIG_BEOS_NETSERVER
  316. if (s->is_multicast) {
  317. if (h->flags & URL_WRONLY) {
  318. /* output */
  319. if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
  320. &s->ttl, sizeof(s->ttl)) < 0) {
  321. perror("IP_MULTICAST_TTL");
  322. goto fail;
  323. }
  324. } else {
  325. /* input */
  326. memset(&s->mreq, 0, sizeof(s->mreq));
  327. s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
  328. s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  329. if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  330. &s->mreq, sizeof(s->mreq)) < 0) {
  331. perror("rtp: IP_ADD_MEMBERSHIP");
  332. goto fail;
  333. }
  334. }
  335. }
  336. #endif
  337. #else
  338. if (s->is_multicast && !(h->flags & URL_WRONLY))
  339. s->local_port = port;
  340. udp_fd = udp_ipv6_set_local(h);
  341. if (udp_fd < 0)
  342. goto fail;
  343. #ifndef CONFIG_BEOS_NETSERVER
  344. if (s->is_multicast) {
  345. if (h->flags & URL_WRONLY) {
  346. if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  347. goto fail;
  348. } else {
  349. if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  350. goto fail;
  351. }
  352. }
  353. #endif
  354. #endif
  355. if (is_output) {
  356. /* limit the tx buf size to limit latency */
  357. tmp = UDP_TX_BUF_SIZE;
  358. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  359. perror("setsockopt sndbuf");
  360. goto fail;
  361. }
  362. }
  363. s->udp_fd = udp_fd;
  364. return 0;
  365. fail:
  366. if (udp_fd >= 0)
  367. #ifdef CONFIG_BEOS_NETSERVER
  368. closesocket(udp_fd);
  369. #else
  370. close(udp_fd);
  371. #endif
  372. av_free(s);
  373. return AVERROR_IO;
  374. }
  375. static int udp_read(URLContext *h, uint8_t *buf, int size)
  376. {
  377. UDPContext *s = h->priv_data;
  378. #ifndef CONFIG_IPV6
  379. struct sockaddr_in from;
  380. #else
  381. struct sockaddr_storage from;
  382. #endif
  383. int from_len, len;
  384. for(;;) {
  385. from_len = sizeof(from);
  386. len = recvfrom (s->udp_fd, buf, size, 0,
  387. (struct sockaddr *)&from, &from_len);
  388. if (len < 0) {
  389. if (errno != EAGAIN && errno != EINTR)
  390. return AVERROR_IO;
  391. } else {
  392. break;
  393. }
  394. }
  395. return len;
  396. }
  397. static int udp_write(URLContext *h, uint8_t *buf, int size)
  398. {
  399. UDPContext *s = h->priv_data;
  400. int ret;
  401. for(;;) {
  402. ret = sendto (s->udp_fd, buf, size, 0,
  403. (struct sockaddr *) &s->dest_addr,
  404. #ifndef CONFIG_IPV6
  405. sizeof (s->dest_addr));
  406. #else
  407. s->dest_addr_len);
  408. #endif
  409. if (ret < 0) {
  410. if (errno != EINTR && errno != EAGAIN)
  411. return AVERROR_IO;
  412. } else {
  413. break;
  414. }
  415. }
  416. return size;
  417. }
  418. static int udp_close(URLContext *h)
  419. {
  420. UDPContext *s = h->priv_data;
  421. #ifndef CONFIG_BEOS_NETSERVER
  422. #ifndef CONFIG_IPV6
  423. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  424. if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
  425. &s->mreq, sizeof(s->mreq)) < 0) {
  426. perror("IP_DROP_MEMBERSHIP");
  427. }
  428. }
  429. #else
  430. if (s->is_multicast && !(h->flags & URL_WRONLY))
  431. udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  432. #endif
  433. close(s->udp_fd);
  434. #else
  435. closesocket(s->udp_fd);
  436. #endif
  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. };