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