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.

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