[FFmpeg-devel] Avoid sending packets to network when multicast ttl is 0 in udp

Submitted by Omid Ghaffarinia on Aug. 8, 2016, 6 a.m.

Details

Message ID CAC30CL8HKtwiHm_Cokm9QX9XZpCf1qptnrmiPcfSJXEk=3KeCw@mail.gmail.com
State New
Headers show

Commit Message

Omid Ghaffarinia Aug. 8, 2016, 6 a.m.
I made it into two patches, the first one only moves
udp_set_multicast_ttl and second one does the fix.

On Fri, Aug 5, 2016 at 3:19 AM, Michael Niedermayer
<michael@niedermayer.cc> wrote:
> On Fri, Aug 05, 2016 at 01:20:12AM +0430, Omid Ghaffarinia wrote:
>> Thanks for your comment, actually 'code move' is necessary to make the
>> code compile because it is needed to use udp_set_url in
>> udp_set_multicast_ttl and the code is moved to make it possible.
>> I can make it in two separate patches if needed, first to move
>> udp_set_multicast_ttl without any further changes and then do the
>> rest, but first patch would be redundant and does not actually fix
>> anything.
>
> yes, it results in more readable commits, also gives any interrested
> developer a last chance to comment on this patch
>
> thx
>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> Republics decline into democracies and democracies degenerate into
> despotisms. -- Aristotle
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

Patch hide | download patch | download mbox

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(-)

diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index 4e37f65..881127d 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -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);
diff --git a/libavformat/udp.c b/libavformat/udp.c
index 48f6a6e..6f79487 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -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