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.

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