From: Peter Enderborg via ffmpeg-devel <ffmpeg-devel@ffmpeg.org> To: <michael@niedermayer.cc>, <ffmpeg-devel@ffmpeg.org>, <remi@remlab.net> Cc: Peter Enderborg <peterend@axis.com> Subject: [FFmpeg-devel] [PATCH v3 2/5] avformat/udp: Select output interfaces for ipv6 multicast Date: Fri, 29 Aug 2025 16:04:56 +0200 Message-ID: <20250829140459.3220037-3-peterend@axis.com> (raw) In-Reply-To: <20250829140459.3220037-1-peterend@axis.com> This fixes two old TODO's in ipv6 multicast handling. If the system as SIOCGIFINDEX ioctl a helper function to iterate over all interfaces added to join the multicast group on interface if approperite. The default value is IN6ADDR_ANY (::) and it then joins all interfaces that are up and have multicast support. Local address can be specified and then it join ALL interfaces that use that specific local address. Limitations (TODO's) Handling when network configuration is changed. if up/down etc. Change-Id: Ifea9c0504469b108de94643a525d9b55fad5466f Signed-off-by: Peter Enderborg <peterend@axis.com> --- libavformat/udp.c | 101 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/libavformat/udp.c b/libavformat/udp.c index 84f9d3e62e..9ffd55e017 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -68,6 +68,13 @@ #include "libavutil/thread.h" #endif +#if HAVE_STRUCT_IPV6_MREQ && HAVE_IOCTL_GIFINDEX && defined(IPPROTO_IPV6) +#include <sys/types.h> +#include <net/if.h> +#include <sys/ioctl.h> +#include <ifaddrs.h> +#endif + #ifndef IPV6_ADD_MEMBERSHIP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP @@ -205,6 +212,82 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, return 0; } +#if HAVE_STRUCT_IPV6_MREQ && HAVE_IOCTL_GIFINDEX && defined(IPPROTO_IPV6) +static int udp_ipv6_mc_devname_membership(int mop, const char *name, + int iindex_fd,struct in6_addr *mca, + int sockfd,void *logctx) +{ + struct ifreq if_req; + strncpy(if_req.ifr_name,name , IFNAMSIZ); + + if (ioctl(iindex_fd, SIOCGIFINDEX, &if_req) != -1) { + struct ipv6_mreq mreq6; + memcpy(&mreq6.ipv6mr_multiaddr, mca, sizeof(struct in6_addr)); + mreq6.ipv6mr_interface = if_req.ifr_ifindex; + + switch (mop) { + case IPV6_JOIN_GROUP: + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { + return 1; + } else { + /* Subscribe for all ipv6 addresses belong to a + interface.It can generate EADDRINUSE but + is accaptable. */ + if (errno != EADDRINUSE) + ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)"); + } + break; + case IPV6_LEAVE_GROUP: + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { + return 1; + } else { + /* Unsubscribe for all ipv6 addresses belong to a + interface. Ignore EADDRNOTAVAIL. */ + if (errno != EADDRNOTAVAIL) + ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)"); + } + break; + default: + av_log(logctx, AV_LOG_ERROR, "unknown multicast operation\n"); + } + } + return -1; +} + +static int udp_ipv6_multicast_iterate(int sockfd, struct sockaddr_in6 *addr, + struct sockaddr_in6 *local_addr, int mop, void *logctx) +{ + struct in6_addr anyipv6 = IN6ADDR_ANY_INIT; + struct ifaddrs *ifal=NULL,*ife=NULL; + int iindex_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); + int membership_changed = 0; + + if (iindex_fd >= 0 && !getifaddrs(&ifal)) { + for(ife=ifal; ife!=NULL; ife=ife->ifa_next) { + if (ife->ifa_addr && + (ife->ifa_addr->sa_family == AF_INET6) && + (ife->ifa_flags & IFF_MULTICAST) && + (ife->ifa_flags & IFF_UP)) { + if ((!memcmp(&local_addr->sin6_addr, &anyipv6, sizeof(struct in6_addr))) || + (!memcmp(&local_addr->sin6_addr, &((struct sockaddr_in6 *)ife->ifa_addr)->sin6_addr, sizeof(struct in6_addr)))) { + if (udp_ipv6_mc_devname_membership(mop,ife->ifa_name, + iindex_fd, &addr->sin6_addr, + sockfd, logctx) == 1) + membership_changed = 1; + } + } + } + } + freeifaddrs(ifal); + close(iindex_fd); + + if (!membership_changed) { + av_log(logctx, AV_LOG_ERROR, "no valid interfaces found\n"); + return AVERROR(EINVAL); + } + return 0; +} +#endif static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx) @@ -226,15 +309,22 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, #endif #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6) if (addr->sa_family == AF_INET6) { +#if HAVE_IOCTL_GIFINDEX + return udp_ipv6_multicast_iterate(sockfd, + (struct sockaddr_in6 *)addr, + (struct sockaddr_in6 *)local_addr, + IPV6_JOIN_GROUP, + logctx); +#else struct ipv6_mreq mreq6; memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); - //TODO: Interface index should be looked up from local_addr mreq6.ipv6mr_interface = 0; if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)"); return ff_neterrno(); } +#endif } #endif return 0; @@ -260,15 +350,22 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, #endif #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6) if (addr->sa_family == AF_INET6) { +#if HAVE_IOCTL_GIFINDEX + return udp_ipv6_multicast_iterate(sockfd, + (struct sockaddr_in6 *)addr, + (struct sockaddr_in6 *)local_addr, + IPV6_LEAVE_GROUP, + logctx); +#else struct ipv6_mreq mreq6; memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); - //TODO: Interface index should be looked up from local_addr mreq6.ipv6mr_interface = 0; if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)"); return -1; } +#endif } #endif return 0; -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
next prev parent reply other threads:[~2025-08-29 14:06 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top [not found] <0250811223419.GF29660@pb2> 2025-08-29 14:04 ` [FFmpeg-devel] [PATCH v3 0/5] " Peter Enderborg via ffmpeg-devel 2025-08-29 14:04 ` [FFmpeg-devel] [PATCH v3 1/5] configure: Add test_ioctl and test for SIOCGIFINDEX Peter Enderborg via ffmpeg-devel 2025-08-29 17:38 ` [FFmpeg-devel] " Rémi Denis-Courmont via ffmpeg-devel 2025-08-29 14:04 ` Peter Enderborg via ffmpeg-devel [this message] 2025-08-29 14:04 ` [FFmpeg-devel] [PATCH v3 3/5] libavformat: udp.c Add support for multi or single join Peter Enderborg via ffmpeg-devel 2025-08-29 14:04 ` [FFmpeg-devel] [PATCH v3 4/5] libavformat: add multicast interface Peter Enderborg via ffmpeg-devel 2025-08-29 14:04 ` [FFmpeg-devel] [PATCH v3 5/5] doc/protocols: Add command-line description for ipv6 options Peter Enderborg via ffmpeg-devel 2025-08-30 0:06 ` [FFmpeg-devel] " Marton Balint via ffmpeg-devel
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250829140459.3220037-3-peterend@axis.com \ --to=ffmpeg-devel@ffmpeg.org \ --cc=michael@niedermayer.cc \ --cc=peterend@axis.com \ --cc=remi@remlab.net \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git