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.

508 lines
15KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * UDP protocol
  24. */
  25. #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/avstring.h"
  30. #include <unistd.h>
  31. #include "internal.h"
  32. #include "network.h"
  33. #include "os_support.h"
  34. #include "url.h"
  35. #ifndef IPV6_ADD_MEMBERSHIP
  36. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  37. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  38. #endif
  39. typedef struct {
  40. int udp_fd;
  41. int ttl;
  42. int buffer_size;
  43. int is_multicast;
  44. int local_port;
  45. int reuse_socket;
  46. struct sockaddr_storage dest_addr;
  47. int dest_addr_len;
  48. int is_connected;
  49. } UDPContext;
  50. #define UDP_TX_BUF_SIZE 32768
  51. #define UDP_MAX_PKT_SIZE 65536
  52. static void log_net_error(void *ctx, int level, const char* prefix)
  53. {
  54. char errbuf[100];
  55. av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
  56. av_log(ctx, level, "%s: %s\n", prefix, errbuf);
  57. }
  58. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  59. struct sockaddr *addr)
  60. {
  61. #ifdef IP_MULTICAST_TTL
  62. if (addr->sa_family == AF_INET) {
  63. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  64. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
  65. return -1;
  66. }
  67. }
  68. #endif
  69. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  70. if (addr->sa_family == AF_INET6) {
  71. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  72. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
  73. return -1;
  74. }
  75. }
  76. #endif
  77. return 0;
  78. }
  79. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  80. {
  81. #ifdef IP_ADD_MEMBERSHIP
  82. if (addr->sa_family == AF_INET) {
  83. struct ip_mreq mreq;
  84. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  85. mreq.imr_interface.s_addr= INADDR_ANY;
  86. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  87. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
  88. return -1;
  89. }
  90. }
  91. #endif
  92. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  93. if (addr->sa_family == AF_INET6) {
  94. struct ipv6_mreq mreq6;
  95. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  96. mreq6.ipv6mr_interface= 0;
  97. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  98. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
  99. return -1;
  100. }
  101. }
  102. #endif
  103. return 0;
  104. }
  105. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  106. {
  107. #ifdef IP_DROP_MEMBERSHIP
  108. if (addr->sa_family == AF_INET) {
  109. struct ip_mreq mreq;
  110. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  111. mreq.imr_interface.s_addr= INADDR_ANY;
  112. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  113. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
  114. return -1;
  115. }
  116. }
  117. #endif
  118. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  119. if (addr->sa_family == AF_INET6) {
  120. struct ipv6_mreq mreq6;
  121. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  122. mreq6.ipv6mr_interface= 0;
  123. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  124. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
  125. return -1;
  126. }
  127. }
  128. #endif
  129. return 0;
  130. }
  131. static struct addrinfo* udp_resolve_host(const char *hostname, int port,
  132. int type, int family, int flags)
  133. {
  134. struct addrinfo hints = { 0 }, *res = 0;
  135. int error;
  136. char sport[16];
  137. const char *node = 0, *service = "0";
  138. if (port > 0) {
  139. snprintf(sport, sizeof(sport), "%d", port);
  140. service = sport;
  141. }
  142. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  143. node = hostname;
  144. }
  145. hints.ai_socktype = type;
  146. hints.ai_family = family;
  147. hints.ai_flags = flags;
  148. if ((error = getaddrinfo(node, service, &hints, &res))) {
  149. res = NULL;
  150. av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
  151. }
  152. return res;
  153. }
  154. static int udp_set_url(struct sockaddr_storage *addr,
  155. const char *hostname, int port)
  156. {
  157. struct addrinfo *res0;
  158. int addr_len;
  159. res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  160. if (res0 == 0) return AVERROR(EIO);
  161. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  162. addr_len = res0->ai_addrlen;
  163. freeaddrinfo(res0);
  164. return addr_len;
  165. }
  166. static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
  167. int *addr_len, const char *localaddr)
  168. {
  169. int udp_fd = -1;
  170. struct addrinfo *res0 = NULL, *res = NULL;
  171. int family = AF_UNSPEC;
  172. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  173. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  174. res0 = udp_resolve_host(localaddr[0] ? localaddr : NULL, s->local_port,
  175. SOCK_DGRAM, family, AI_PASSIVE);
  176. if (res0 == 0)
  177. goto fail;
  178. for (res = res0; res; res=res->ai_next) {
  179. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  180. if (udp_fd != -1) break;
  181. log_net_error(NULL, AV_LOG_ERROR, "socket");
  182. }
  183. if (udp_fd < 0)
  184. goto fail;
  185. memcpy(addr, res->ai_addr, res->ai_addrlen);
  186. *addr_len = res->ai_addrlen;
  187. freeaddrinfo(res0);
  188. return udp_fd;
  189. fail:
  190. if (udp_fd >= 0)
  191. closesocket(udp_fd);
  192. if(res0)
  193. freeaddrinfo(res0);
  194. return -1;
  195. }
  196. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  197. {
  198. char sbuf[sizeof(int)*3+1];
  199. int error;
  200. if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
  201. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
  202. return -1;
  203. }
  204. return strtol(sbuf, NULL, 10);
  205. }
  206. /**
  207. * If no filename is given to av_open_input_file because you want to
  208. * get the local port first, then you must call this function to set
  209. * the remote server address.
  210. *
  211. * url syntax: udp://host:port[?option=val...]
  212. * option: 'ttl=n' : set the ttl value (for multicast only)
  213. * 'localport=n' : set the local port
  214. * 'pkt_size=n' : set max packet size
  215. * 'reuse=1' : enable reusing the socket
  216. *
  217. * @param h media file context
  218. * @param uri of the remote server
  219. * @return zero if no error.
  220. */
  221. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  222. {
  223. UDPContext *s = h->priv_data;
  224. char hostname[256], buf[10];
  225. int port;
  226. const char *p;
  227. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  228. /* set the destination address */
  229. s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
  230. if (s->dest_addr_len < 0) {
  231. return AVERROR(EIO);
  232. }
  233. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  234. p = strchr(uri, '?');
  235. if (p) {
  236. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  237. int was_connected = s->is_connected;
  238. s->is_connected = strtol(buf, NULL, 10);
  239. if (s->is_connected && !was_connected) {
  240. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  241. s->dest_addr_len)) {
  242. s->is_connected = 0;
  243. log_net_error(h, AV_LOG_ERROR, "connect");
  244. return AVERROR(EIO);
  245. }
  246. }
  247. }
  248. }
  249. return 0;
  250. }
  251. /**
  252. * Return the local port used by the UDP connection
  253. * @param h media file context
  254. * @return the local port number
  255. */
  256. int ff_udp_get_local_port(URLContext *h)
  257. {
  258. UDPContext *s = h->priv_data;
  259. return s->local_port;
  260. }
  261. /**
  262. * Return the udp file handle for select() usage to wait for several RTP
  263. * streams at the same time.
  264. * @param h media file context
  265. */
  266. static int udp_get_file_handle(URLContext *h)
  267. {
  268. UDPContext *s = h->priv_data;
  269. return s->udp_fd;
  270. }
  271. /* put it in UDP context */
  272. /* return non zero if error */
  273. static int udp_open(URLContext *h, const char *uri, int flags)
  274. {
  275. char hostname[1024], localaddr[1024] = "";
  276. int port, udp_fd = -1, tmp, bind_ret = -1;
  277. UDPContext *s = h->priv_data;
  278. int is_output;
  279. const char *p;
  280. char buf[256];
  281. struct sockaddr_storage my_addr;
  282. int len;
  283. int reuse_specified = 0;
  284. h->is_streamed = 1;
  285. h->max_packet_size = 1472;
  286. is_output = !(flags & AVIO_FLAG_READ);
  287. s->ttl = 16;
  288. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  289. p = strchr(uri, '?');
  290. if (p) {
  291. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  292. char *endptr = NULL;
  293. s->reuse_socket = strtol(buf, &endptr, 10);
  294. /* assume if no digits were found it is a request to enable it */
  295. if (buf == endptr)
  296. s->reuse_socket = 1;
  297. reuse_specified = 1;
  298. }
  299. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  300. s->ttl = strtol(buf, NULL, 10);
  301. }
  302. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  303. s->local_port = strtol(buf, NULL, 10);
  304. }
  305. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  306. h->max_packet_size = strtol(buf, NULL, 10);
  307. }
  308. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  309. s->buffer_size = strtol(buf, NULL, 10);
  310. }
  311. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  312. s->is_connected = strtol(buf, NULL, 10);
  313. }
  314. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  315. av_strlcpy(localaddr, buf, sizeof(localaddr));
  316. }
  317. }
  318. /* fill the dest addr */
  319. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  320. /* XXX: fix av_url_split */
  321. if (hostname[0] == '\0' || hostname[0] == '?') {
  322. /* only accepts null hostname if input */
  323. if (!(flags & AVIO_FLAG_READ))
  324. goto fail;
  325. } else {
  326. if (ff_udp_set_remote_url(h, uri) < 0)
  327. goto fail;
  328. }
  329. if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
  330. s->local_port = port;
  331. udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
  332. if (udp_fd < 0)
  333. goto fail;
  334. /* Follow the requested reuse option, unless it's multicast in which
  335. * case enable reuse unless explicitly disabled.
  336. */
  337. if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
  338. s->reuse_socket = 1;
  339. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  340. goto fail;
  341. }
  342. /* If multicast, try binding the multicast address first, to avoid
  343. * receiving UDP packets from other sources aimed at the same UDP
  344. * port. This fails on windows. This makes sending to the same address
  345. * using sendto() fail, so only do it if we're opened in read-only mode. */
  346. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  347. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  348. }
  349. /* bind to the local address if not multicast or if the multicast
  350. * bind failed */
  351. /* the bind is needed to give a port to the socket now */
  352. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  353. log_net_error(h, AV_LOG_ERROR, "bind failed");
  354. goto fail;
  355. }
  356. len = sizeof(my_addr);
  357. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  358. s->local_port = udp_port(&my_addr, len);
  359. if (s->is_multicast) {
  360. if (h->flags & AVIO_FLAG_WRITE) {
  361. /* output */
  362. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  363. goto fail;
  364. }
  365. if (h->flags & AVIO_FLAG_READ) {
  366. /* input */
  367. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  368. goto fail;
  369. }
  370. }
  371. if (is_output) {
  372. /* limit the tx buf size to limit latency */
  373. tmp = s->buffer_size;
  374. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  375. log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  376. goto fail;
  377. }
  378. } else {
  379. /* set udp recv buffer size to the largest possible udp packet size to
  380. * avoid losing data on OSes that set this too low by default. */
  381. tmp = s->buffer_size;
  382. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  383. log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  384. }
  385. /* make the socket non-blocking */
  386. ff_socket_nonblock(udp_fd, 1);
  387. }
  388. if (s->is_connected) {
  389. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  390. log_net_error(h, AV_LOG_ERROR, "connect");
  391. goto fail;
  392. }
  393. }
  394. s->udp_fd = udp_fd;
  395. return 0;
  396. fail:
  397. if (udp_fd >= 0)
  398. closesocket(udp_fd);
  399. return AVERROR(EIO);
  400. }
  401. static int udp_read(URLContext *h, uint8_t *buf, int size)
  402. {
  403. UDPContext *s = h->priv_data;
  404. int ret;
  405. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  406. ret = ff_network_wait_fd(s->udp_fd, 0);
  407. if (ret < 0)
  408. return ret;
  409. }
  410. ret = recv(s->udp_fd, buf, size, 0);
  411. return ret < 0 ? ff_neterrno() : ret;
  412. }
  413. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  414. {
  415. UDPContext *s = h->priv_data;
  416. int ret;
  417. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  418. ret = ff_network_wait_fd(s->udp_fd, 1);
  419. if (ret < 0)
  420. return ret;
  421. }
  422. if (!s->is_connected) {
  423. ret = sendto (s->udp_fd, buf, size, 0,
  424. (struct sockaddr *) &s->dest_addr,
  425. s->dest_addr_len);
  426. } else
  427. ret = send(s->udp_fd, buf, size, 0);
  428. return ret < 0 ? ff_neterrno() : ret;
  429. }
  430. static int udp_close(URLContext *h)
  431. {
  432. UDPContext *s = h->priv_data;
  433. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  434. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  435. closesocket(s->udp_fd);
  436. return 0;
  437. }
  438. URLProtocol ff_udp_protocol = {
  439. .name = "udp",
  440. .url_open = udp_open,
  441. .url_read = udp_read,
  442. .url_write = udp_write,
  443. .url_close = udp_close,
  444. .url_get_file_handle = udp_get_file_handle,
  445. .priv_data_size = sizeof(UDPContext),
  446. .flags = URL_PROTOCOL_FLAG_NETWORK,
  447. };