From 7900ed3665a4c7760554c84767a05fc2c6cb074a Mon Sep 17 00:00:00 2001
From: Omid Ghaffarinia <omid.ghaffarinia@gmail.com>
Date: Mon, 8 Aug 2016 10:24:42 +0430
Subject: [PATCH 2/2] Avoid sending packets to network when multicast ttl is 0
in udp
Signed-off-by: Omid Ghaffarinia <omid.ghaffarinia@gmail.com>
---
libavformat/sdp.c | 2 +-
libavformat/udp.c | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
@@ -61,7 +61,7 @@ static void sdp_write_address(char *buff, int size, const char *dest_addr,
if (dest_addr) {
if (!dest_type)
dest_type = "IP4";
- if (ttl > 0 && !strcmp(dest_type, "IP4")) {
+ if (ttl >= 0 && !strcmp(dest_type, "IP4")) {
/* The TTL should only be specified for IPv4 multicast addresses,
* not for IPv6. */
av_strlcatf(buff, size, "c=IN %s %s/%d\r\n", dest_type, dest_addr, ttl);
@@ -360,6 +360,27 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
}
}
#endif
+ if (mcastTTL == 0) {
+ struct sockaddr_storage localhost_addr;
+#ifdef IP_MULTICAST_IF
+ if (addr->sa_family == AF_INET) {
+ udp_set_url(NULL, &localhost_addr, "127.0.0.1", 0);
+ if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &((struct sockaddr_in *)&localhost_addr)->sin_addr, sizeof(struct in_addr)) < 0) {
+ log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_IF)");
+ return -1;
+ }
+ }
+#endif
+#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_IF)
+ if (addr->sa_family == AF_INET6) {
+ udp_set_url(NULL, &localhost_addr, "::1", 0);
+ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &((struct sockaddr_in6 *)&localhost_addr)->sin6_addr, sizeof(struct in6_addr)) < 0) {
+ log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_IF)");
+ return -1;
+ }
+ }
+#endif
+ }
return 0;
}
@@ -882,6 +903,9 @@ static int udp_open(URLContext *h, const char *uri, int flags)
}
if (h->flags & AVIO_FLAG_READ) {
/* input */
+ if (s->ttl == 0) {
+ udp_set_url(h, &s->local_addr_storage, s->dest_addr.ss_family == AF_INET ? "127.0.0.1" : "::1", 0);
+ }
if (num_include_sources && num_exclude_sources) {
av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
goto fail;
--
1.7.9.5