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.

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