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.

699 lines
24KB

  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 "libavutil/opt.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 UDPContext {
  40. const AVClass *class;
  41. int udp_fd;
  42. int ttl;
  43. int buffer_size;
  44. int pkt_size;
  45. int is_multicast;
  46. int local_port;
  47. int reuse_socket;
  48. struct sockaddr_storage dest_addr;
  49. int dest_addr_len;
  50. int is_connected;
  51. char *localaddr;
  52. char *sources;
  53. char *block;
  54. } UDPContext;
  55. #define UDP_TX_BUF_SIZE 32768
  56. #define UDP_MAX_PKT_SIZE 65536
  57. #define OFFSET(x) offsetof(UDPContext, x)
  58. #define D AV_OPT_FLAG_DECODING_PARAM
  59. #define E AV_OPT_FLAG_ENCODING_PARAM
  60. static const AVOption options[] = {
  61. { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, .flags = D|E },
  62. { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  63. { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  64. { "reuse_socket", "Reuse socket", OFFSET(reuse_socket), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  65. { "connect", "Connect socket", OFFSET(is_connected), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
  66. { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  67. { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  68. { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  69. { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  70. { NULL }
  71. };
  72. static const AVClass udp_class = {
  73. .class_name = "udp",
  74. .item_name = av_default_item_name,
  75. .option = options,
  76. .version = LIBAVUTIL_VERSION_INT,
  77. };
  78. static void log_net_error(void *ctx, int level, const char* prefix)
  79. {
  80. char errbuf[100];
  81. av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
  82. av_log(ctx, level, "%s: %s\n", prefix, errbuf);
  83. }
  84. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  85. struct sockaddr *addr)
  86. {
  87. #ifdef IP_MULTICAST_TTL
  88. if (addr->sa_family == AF_INET) {
  89. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  90. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
  91. return -1;
  92. }
  93. }
  94. #endif
  95. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  96. if (addr->sa_family == AF_INET6) {
  97. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  98. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
  99. return -1;
  100. }
  101. }
  102. #endif
  103. return 0;
  104. }
  105. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  106. {
  107. #ifdef IP_ADD_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_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  113. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_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_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  124. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
  125. return -1;
  126. }
  127. }
  128. #endif
  129. return 0;
  130. }
  131. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  132. {
  133. #ifdef IP_DROP_MEMBERSHIP
  134. if (addr->sa_family == AF_INET) {
  135. struct ip_mreq mreq;
  136. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  137. mreq.imr_interface.s_addr= INADDR_ANY;
  138. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  139. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
  140. return -1;
  141. }
  142. }
  143. #endif
  144. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  145. if (addr->sa_family == AF_INET6) {
  146. struct ipv6_mreq mreq6;
  147. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  148. mreq6.ipv6mr_interface= 0;
  149. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  150. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
  151. return -1;
  152. }
  153. }
  154. #endif
  155. return 0;
  156. }
  157. static struct addrinfo *udp_resolve_host(URLContext *h,
  158. const char *hostname, int port,
  159. int type, int family, int flags)
  160. {
  161. struct addrinfo hints = { 0 }, *res = 0;
  162. int error;
  163. char sport[16];
  164. const char *node = 0, *service = "0";
  165. if (port > 0) {
  166. snprintf(sport, sizeof(sport), "%d", port);
  167. service = sport;
  168. }
  169. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  170. node = hostname;
  171. }
  172. hints.ai_socktype = type;
  173. hints.ai_family = family;
  174. hints.ai_flags = flags;
  175. if ((error = getaddrinfo(node, service, &hints, &res))) {
  176. res = NULL;
  177. av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
  178. node ? node : "unknown",
  179. service ? service : "unknown",
  180. gai_strerror(error));
  181. }
  182. return res;
  183. }
  184. static int udp_set_multicast_sources(URLContext *h,
  185. int sockfd, struct sockaddr *addr,
  186. int addr_len, char **sources,
  187. int nb_sources, int include)
  188. {
  189. #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
  190. /* These ones are available in the microsoft SDK, but don't seem to work
  191. * as on linux, so just prefer the v4-only approach there for now. */
  192. int i;
  193. for (i = 0; i < nb_sources; i++) {
  194. struct group_source_req mreqs;
  195. int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
  196. struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
  197. SOCK_DGRAM, AF_UNSPEC,
  198. 0);
  199. if (!sourceaddr)
  200. return AVERROR(ENOENT);
  201. mreqs.gsr_interface = 0;
  202. memcpy(&mreqs.gsr_group, addr, addr_len);
  203. memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
  204. freeaddrinfo(sourceaddr);
  205. if (setsockopt(sockfd, level,
  206. include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
  207. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  208. if (include)
  209. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
  210. else
  211. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
  212. return ff_neterrno();
  213. }
  214. }
  215. #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
  216. int i;
  217. if (addr->sa_family != AF_INET) {
  218. av_log(NULL, AV_LOG_ERROR,
  219. "Setting multicast sources only supported for IPv4\n");
  220. return AVERROR(EINVAL);
  221. }
  222. for (i = 0; i < nb_sources; i++) {
  223. struct ip_mreq_source mreqs;
  224. struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
  225. SOCK_DGRAM, AF_UNSPEC,
  226. 0);
  227. if (!sourceaddr)
  228. return AVERROR(ENOENT);
  229. if (sourceaddr->ai_addr->sa_family != AF_INET) {
  230. freeaddrinfo(sourceaddr);
  231. av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n",
  232. sources[i]);
  233. return AVERROR(EINVAL);
  234. }
  235. mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  236. mreqs.imr_interface.s_addr = INADDR_ANY;
  237. mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr;
  238. freeaddrinfo(sourceaddr);
  239. if (setsockopt(sockfd, IPPROTO_IP,
  240. include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
  241. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  242. if (include)
  243. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
  244. else
  245. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
  246. return ff_neterrno();
  247. }
  248. }
  249. #else
  250. return AVERROR(ENOSYS);
  251. #endif
  252. return 0;
  253. }
  254. static int udp_set_url(URLContext *h,
  255. struct sockaddr_storage *addr,
  256. const char *hostname, int port)
  257. {
  258. struct addrinfo *res0;
  259. int addr_len;
  260. res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  261. if (res0 == 0) return AVERROR(EIO);
  262. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  263. addr_len = res0->ai_addrlen;
  264. freeaddrinfo(res0);
  265. return addr_len;
  266. }
  267. static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
  268. socklen_t *addr_len, const char *localaddr)
  269. {
  270. UDPContext *s = h->priv_data;
  271. int udp_fd = -1;
  272. struct addrinfo *res0 = NULL, *res = NULL;
  273. int family = AF_UNSPEC;
  274. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  275. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  276. res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
  277. s->local_port,
  278. SOCK_DGRAM, family, AI_PASSIVE);
  279. if (res0 == 0)
  280. goto fail;
  281. for (res = res0; res; res=res->ai_next) {
  282. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
  283. if (udp_fd != -1) break;
  284. log_net_error(NULL, AV_LOG_ERROR, "socket");
  285. }
  286. if (udp_fd < 0)
  287. goto fail;
  288. memcpy(addr, res->ai_addr, res->ai_addrlen);
  289. *addr_len = res->ai_addrlen;
  290. freeaddrinfo(res0);
  291. return udp_fd;
  292. fail:
  293. if (udp_fd >= 0)
  294. closesocket(udp_fd);
  295. if(res0)
  296. freeaddrinfo(res0);
  297. return -1;
  298. }
  299. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  300. {
  301. char sbuf[sizeof(int)*3+1];
  302. int error;
  303. if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
  304. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
  305. return -1;
  306. }
  307. return strtol(sbuf, NULL, 10);
  308. }
  309. /**
  310. * If no filename is given to av_open_input_file because you want to
  311. * get the local port first, then you must call this function to set
  312. * the remote server address.
  313. *
  314. * url syntax: udp://host:port[?option=val...]
  315. * option: 'ttl=n' : set the ttl value (for multicast only)
  316. * 'localport=n' : set the local port
  317. * 'pkt_size=n' : set max packet size
  318. * 'reuse=1' : enable reusing the socket
  319. *
  320. * @param h media file context
  321. * @param uri of the remote server
  322. * @return zero if no error.
  323. */
  324. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  325. {
  326. UDPContext *s = h->priv_data;
  327. char hostname[256], buf[10];
  328. int port;
  329. const char *p;
  330. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  331. /* set the destination address */
  332. s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
  333. if (s->dest_addr_len < 0) {
  334. return AVERROR(EIO);
  335. }
  336. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  337. p = strchr(uri, '?');
  338. if (p) {
  339. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  340. int was_connected = s->is_connected;
  341. s->is_connected = strtol(buf, NULL, 10);
  342. if (s->is_connected && !was_connected) {
  343. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  344. s->dest_addr_len)) {
  345. s->is_connected = 0;
  346. log_net_error(h, AV_LOG_ERROR, "connect");
  347. return AVERROR(EIO);
  348. }
  349. }
  350. }
  351. }
  352. return 0;
  353. }
  354. /**
  355. * Return the local port used by the UDP connection
  356. * @param h media file context
  357. * @return the local port number
  358. */
  359. int ff_udp_get_local_port(URLContext *h)
  360. {
  361. UDPContext *s = h->priv_data;
  362. return s->local_port;
  363. }
  364. /**
  365. * Return the udp file handle for select() usage to wait for several RTP
  366. * streams at the same time.
  367. * @param h media file context
  368. */
  369. static int udp_get_file_handle(URLContext *h)
  370. {
  371. UDPContext *s = h->priv_data;
  372. return s->udp_fd;
  373. }
  374. static int parse_source_list(char *buf, char **sources, int *num_sources,
  375. int max_sources)
  376. {
  377. char *source_start;
  378. source_start = buf;
  379. while (1) {
  380. char *next = strchr(source_start, ',');
  381. if (next)
  382. *next = '\0';
  383. sources[*num_sources] = av_strdup(source_start);
  384. if (!sources[*num_sources])
  385. return AVERROR(ENOMEM);
  386. source_start = next + 1;
  387. (*num_sources)++;
  388. if (*num_sources >= max_sources || !next)
  389. break;
  390. }
  391. return 0;
  392. }
  393. /* put it in UDP context */
  394. /* return non zero if error */
  395. static int udp_open(URLContext *h, const char *uri, int flags)
  396. {
  397. char hostname[1024], localaddr[1024] = "";
  398. int port, udp_fd = -1, tmp, bind_ret = -1;
  399. UDPContext *s = h->priv_data;
  400. int is_output;
  401. const char *p;
  402. char buf[256];
  403. struct sockaddr_storage my_addr;
  404. socklen_t len;
  405. int i, num_include_sources = 0, num_exclude_sources = 0;
  406. char *include_sources[32], *exclude_sources[32];
  407. h->is_streamed = 1;
  408. h->max_packet_size = 1472;
  409. is_output = !(flags & AVIO_FLAG_READ);
  410. if (s->buffer_size < 0)
  411. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  412. if (s->sources) {
  413. if (parse_source_list(s->sources, include_sources,
  414. &num_include_sources,
  415. FF_ARRAY_ELEMS(include_sources)))
  416. goto fail;
  417. }
  418. if (s->block) {
  419. if (parse_source_list(s->block, exclude_sources, &num_exclude_sources,
  420. FF_ARRAY_ELEMS(exclude_sources)))
  421. goto fail;
  422. }
  423. if (s->pkt_size > 0)
  424. h->max_packet_size = s->pkt_size;
  425. p = strchr(uri, '?');
  426. if (p) {
  427. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  428. char *endptr = NULL;
  429. s->reuse_socket = strtol(buf, &endptr, 10);
  430. /* assume if no digits were found it is a request to enable it */
  431. if (buf == endptr)
  432. s->reuse_socket = 1;
  433. }
  434. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  435. s->ttl = strtol(buf, NULL, 10);
  436. }
  437. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  438. s->local_port = strtol(buf, NULL, 10);
  439. }
  440. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  441. h->max_packet_size = strtol(buf, NULL, 10);
  442. }
  443. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  444. s->buffer_size = strtol(buf, NULL, 10);
  445. }
  446. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  447. s->is_connected = strtol(buf, NULL, 10);
  448. }
  449. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  450. av_strlcpy(localaddr, buf, sizeof(localaddr));
  451. }
  452. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  453. if (parse_source_list(buf, include_sources, &num_include_sources,
  454. FF_ARRAY_ELEMS(include_sources)))
  455. goto fail;
  456. }
  457. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  458. if (parse_source_list(buf, exclude_sources, &num_exclude_sources,
  459. FF_ARRAY_ELEMS(exclude_sources)))
  460. goto fail;
  461. }
  462. }
  463. /* fill the dest addr */
  464. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  465. /* XXX: fix av_url_split */
  466. if (hostname[0] == '\0' || hostname[0] == '?') {
  467. /* only accepts null hostname if input */
  468. if (!(flags & AVIO_FLAG_READ))
  469. goto fail;
  470. } else {
  471. if (ff_udp_set_remote_url(h, uri) < 0)
  472. goto fail;
  473. }
  474. if ((s->is_multicast || s->local_port < 0) && (h->flags & AVIO_FLAG_READ))
  475. s->local_port = port;
  476. if (localaddr[0])
  477. udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
  478. else
  479. udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
  480. if (udp_fd < 0)
  481. goto fail;
  482. /* Follow the requested reuse option, unless it's multicast in which
  483. * case enable reuse unless explicitly disabled.
  484. */
  485. if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
  486. s->reuse_socket = 1;
  487. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  488. goto fail;
  489. }
  490. /* If multicast, try binding the multicast address first, to avoid
  491. * receiving UDP packets from other sources aimed at the same UDP
  492. * port. This fails on windows. This makes sending to the same address
  493. * using sendto() fail, so only do it if we're opened in read-only mode. */
  494. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  495. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  496. }
  497. /* bind to the local address if not multicast or if the multicast
  498. * bind failed */
  499. /* the bind is needed to give a port to the socket now */
  500. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  501. log_net_error(h, AV_LOG_ERROR, "bind failed");
  502. goto fail;
  503. }
  504. len = sizeof(my_addr);
  505. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  506. s->local_port = udp_port(&my_addr, len);
  507. if (s->is_multicast) {
  508. if (h->flags & AVIO_FLAG_WRITE) {
  509. /* output */
  510. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  511. goto fail;
  512. }
  513. if (h->flags & AVIO_FLAG_READ) {
  514. /* input */
  515. if (num_include_sources && num_exclude_sources) {
  516. av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
  517. goto fail;
  518. }
  519. if (num_include_sources) {
  520. if (udp_set_multicast_sources(h, udp_fd,
  521. (struct sockaddr *)&s->dest_addr,
  522. s->dest_addr_len,
  523. include_sources,
  524. num_include_sources, 1) < 0)
  525. goto fail;
  526. } else {
  527. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  528. goto fail;
  529. }
  530. if (num_exclude_sources) {
  531. if (udp_set_multicast_sources(h, udp_fd,
  532. (struct sockaddr *)&s->dest_addr,
  533. s->dest_addr_len,
  534. exclude_sources,
  535. num_exclude_sources, 0) < 0)
  536. goto fail;
  537. }
  538. }
  539. }
  540. if (is_output) {
  541. /* limit the tx buf size to limit latency */
  542. tmp = s->buffer_size;
  543. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  544. log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  545. goto fail;
  546. }
  547. } else {
  548. /* set udp recv buffer size to the largest possible udp packet size to
  549. * avoid losing data on OSes that set this too low by default. */
  550. tmp = s->buffer_size;
  551. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  552. log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  553. }
  554. /* make the socket non-blocking */
  555. ff_socket_nonblock(udp_fd, 1);
  556. }
  557. if (s->is_connected) {
  558. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  559. log_net_error(h, AV_LOG_ERROR, "connect");
  560. goto fail;
  561. }
  562. }
  563. for (i = 0; i < num_include_sources; i++)
  564. av_freep(&include_sources[i]);
  565. for (i = 0; i < num_exclude_sources; i++)
  566. av_freep(&exclude_sources[i]);
  567. s->udp_fd = udp_fd;
  568. return 0;
  569. fail:
  570. if (udp_fd >= 0)
  571. closesocket(udp_fd);
  572. for (i = 0; i < num_include_sources; i++)
  573. av_freep(&include_sources[i]);
  574. for (i = 0; i < num_exclude_sources; i++)
  575. av_freep(&exclude_sources[i]);
  576. return AVERROR(EIO);
  577. }
  578. static int udp_read(URLContext *h, uint8_t *buf, int size)
  579. {
  580. UDPContext *s = h->priv_data;
  581. int ret;
  582. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  583. ret = ff_network_wait_fd(s->udp_fd, 0);
  584. if (ret < 0)
  585. return ret;
  586. }
  587. ret = recv(s->udp_fd, buf, size, 0);
  588. return ret < 0 ? ff_neterrno() : ret;
  589. }
  590. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  591. {
  592. UDPContext *s = h->priv_data;
  593. int ret;
  594. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  595. ret = ff_network_wait_fd(s->udp_fd, 1);
  596. if (ret < 0)
  597. return ret;
  598. }
  599. if (!s->is_connected) {
  600. ret = sendto (s->udp_fd, buf, size, 0,
  601. (struct sockaddr *) &s->dest_addr,
  602. s->dest_addr_len);
  603. } else
  604. ret = send(s->udp_fd, buf, size, 0);
  605. return ret < 0 ? ff_neterrno() : ret;
  606. }
  607. static int udp_close(URLContext *h)
  608. {
  609. UDPContext *s = h->priv_data;
  610. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  611. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  612. closesocket(s->udp_fd);
  613. return 0;
  614. }
  615. const URLProtocol ff_udp_protocol = {
  616. .name = "udp",
  617. .url_open = udp_open,
  618. .url_read = udp_read,
  619. .url_write = udp_write,
  620. .url_close = udp_close,
  621. .url_get_file_handle = udp_get_file_handle,
  622. .priv_data_size = sizeof(UDPContext),
  623. .flags = URL_PROTOCOL_FLAG_NETWORK,
  624. .priv_data_class = &udp_class,
  625. };