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.

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