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 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. av_log(NULL, AV_LOG_ERROR, "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, *res = NULL;
  153. if (s->local_port != 0) {
  154. res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
  155. if (res0 == 0)
  156. goto fail;
  157. for (res = res0; res; res=res->ai_next) {
  158. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  159. if (udp_fd > 0) break;
  160. perror("socket");
  161. }
  162. } else {
  163. udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
  164. if (udp_fd < 0)
  165. perror("socket");
  166. }
  167. if (udp_fd < 0)
  168. goto fail;
  169. if (s->local_port != 0) {
  170. if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
  171. perror("bind");
  172. goto fail;
  173. }
  174. freeaddrinfo(res0);
  175. res0 = NULL;
  176. }
  177. addrlen = sizeof(clientaddr);
  178. if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
  179. perror("getsockname");
  180. goto fail;
  181. }
  182. if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
  183. perror("getnameinfo");
  184. goto fail;
  185. }
  186. s->local_port = strtol(sbuf, NULL, 10);
  187. return udp_fd;
  188. fail:
  189. if (udp_fd >= 0)
  190. #ifdef CONFIG_BEOS_NETSERVER
  191. closesocket(udp_fd);
  192. #else
  193. close(udp_fd);
  194. #endif
  195. if(res0)
  196. freeaddrinfo(res0);
  197. return -1;
  198. }
  199. #endif
  200. /**
  201. * If no filename is given to av_open_input_file because you want to
  202. * get the local port first, then you must call this function to set
  203. * the remote server address.
  204. *
  205. * url syntax: udp://host:port[?option=val...]
  206. * option: 'multicast=1' : enable multicast
  207. * 'ttl=n' : set the ttl value (for multicast only)
  208. * 'localport=n' : set the local port
  209. * 'pkt_size=n' : set max packet size
  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. p = strchr(uri, '?');
  277. if (p) {
  278. s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
  279. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  280. s->ttl = strtol(buf, NULL, 10);
  281. }
  282. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  283. s->local_port = strtol(buf, NULL, 10);
  284. }
  285. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  286. h->max_packet_size = strtol(buf, NULL, 10);
  287. }
  288. }
  289. /* fill the dest addr */
  290. url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  291. /* XXX: fix url_split */
  292. if (hostname[0] == '\0' || hostname[0] == '?') {
  293. /* only accepts null hostname if input */
  294. if (s->is_multicast || (flags & URL_WRONLY))
  295. goto fail;
  296. } else {
  297. udp_set_remote_url(h, uri);
  298. }
  299. #ifndef CONFIG_IPV6
  300. udp_fd = socket(PF_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. /* the bind is needed to give a port to the socket now */
  312. if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
  313. goto fail;
  314. len = sizeof(my_addr1);
  315. getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
  316. s->local_port = ntohs(my_addr1.sin_port);
  317. #ifndef CONFIG_BEOS_NETSERVER
  318. if (s->is_multicast) {
  319. if (h->flags & URL_WRONLY) {
  320. /* output */
  321. if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
  322. &s->ttl, sizeof(s->ttl)) < 0) {
  323. perror("IP_MULTICAST_TTL");
  324. goto fail;
  325. }
  326. } else {
  327. /* input */
  328. memset(&s->mreq, 0, sizeof(s->mreq));
  329. s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
  330. s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  331. if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  332. &s->mreq, sizeof(s->mreq)) < 0) {
  333. perror("rtp: IP_ADD_MEMBERSHIP");
  334. goto fail;
  335. }
  336. }
  337. }
  338. #endif
  339. #else
  340. if (s->is_multicast && !(h->flags & URL_WRONLY))
  341. s->local_port = port;
  342. udp_fd = udp_ipv6_set_local(h);
  343. if (udp_fd < 0)
  344. goto fail;
  345. #ifndef CONFIG_BEOS_NETSERVER
  346. if (s->is_multicast) {
  347. if (h->flags & URL_WRONLY) {
  348. if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  349. goto fail;
  350. } else {
  351. if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  352. goto fail;
  353. }
  354. }
  355. #endif
  356. #endif
  357. if (is_output) {
  358. /* limit the tx buf size to limit latency */
  359. tmp = UDP_TX_BUF_SIZE;
  360. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  361. perror("setsockopt sndbuf");
  362. goto fail;
  363. }
  364. }
  365. s->udp_fd = udp_fd;
  366. return 0;
  367. fail:
  368. if (udp_fd >= 0)
  369. #ifdef CONFIG_BEOS_NETSERVER
  370. closesocket(udp_fd);
  371. #else
  372. close(udp_fd);
  373. #endif
  374. av_free(s);
  375. return AVERROR_IO;
  376. }
  377. static int udp_read(URLContext *h, uint8_t *buf, int size)
  378. {
  379. UDPContext *s = h->priv_data;
  380. #ifndef CONFIG_IPV6
  381. struct sockaddr_in from;
  382. #else
  383. struct sockaddr_storage from;
  384. #endif
  385. int from_len, 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_BEOS_NETSERVER
  424. #ifndef CONFIG_IPV6
  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. #else
  432. if (s->is_multicast && !(h->flags & URL_WRONLY))
  433. udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  434. #endif
  435. close(s->udp_fd);
  436. #else
  437. closesocket(s->udp_fd);
  438. #endif
  439. av_free(s);
  440. return 0;
  441. }
  442. URLProtocol udp_protocol = {
  443. "udp",
  444. udp_open,
  445. udp_read,
  446. udp_write,
  447. NULL, /* seek */
  448. udp_close,
  449. };