* [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast @ 2025-08-05 7:56 Peter Enderborg 2025-08-06 15:28 ` Rémi Denis-Courmont ` (3 more replies) 0 siblings, 4 replies; 6+ messages in thread From: Peter Enderborg @ 2025-08-05 7:56 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Peter Enderborg This fixes two old TODO's in ipv6 multicast handling. A helper function to iterate over all interfaces added to help 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. Signed-off-by: Peter Enderborg <peterend@axis.com> --- libavformat/udp.c | 108 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 90 insertions(+), 18 deletions(-) diff --git a/libavformat/udp.c b/libavformat/udp.c index 035db785c2..0a981447a5 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -68,6 +68,13 @@ #include "libavutil/thread.h" #endif +#if HAVE_STRUCT_IPV6_MREQ && 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,79 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, return 0; } +#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6) +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)))) { + struct ifreq if_req; + strncpy(if_req.ifr_name, ife->ifa_name, IFNAMSIZ); + if (ioctl(iindex_fd, SIOCGIFINDEX, &if_req) != -1) { + struct ipv6_mreq mreq6; + memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6_addr, 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) { + membership_changed = 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)"); + goto error; + } + } + break; + case IPV6_LEAVE_GROUP: + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { + membership_changed = 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)"); + goto error; + } + } + break; + default: + av_log(logctx, AV_LOG_ERROR, "unknown multicast operation\n"); + goto error; + } + } + } + } + } + } + freeifaddrs(ifal); + close(iindex_fd); + + if (!membership_changed) { + av_log(logctx, AV_LOG_ERROR, "no valid interfaces found\n"); + return AVERROR(EINVAL); + } + return 0; +error: + freeifaddrs(ifal); + close(iindex_fd); + av_log(logctx, AV_LOG_ERROR, "udp_ipv6_multicast_iterate failed\n"); + return AVERROR(EINVAL); +} +#endif static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx) @@ -226,15 +306,11 @@ 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) { - 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(); - } + return udp_ipv6_multicast_iterate(sockfd, + (struct sockaddr_in6 *)addr, + (struct sockaddr_in6 *)local_addr, + IPV6_JOIN_GROUP, + logctx); } #endif return 0; @@ -260,15 +336,11 @@ 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) { - 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; - } + return udp_ipv6_multicast_iterate(sockfd, + (struct sockaddr_in6 *)addr, + (struct sockaddr_in6 *)local_addr, + IPV6_LEAVE_GROUP, + logctx); } #endif return 0; -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast 2025-08-05 7:56 [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg @ 2025-08-06 15:28 ` Rémi Denis-Courmont 2025-08-12 10:59 ` Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 0/2] " Peter Enderborg ` (2 subsequent siblings) 3 siblings, 1 reply; 6+ messages in thread From: Rémi Denis-Courmont @ 2025-08-06 15:28 UTC (permalink / raw) To: FFmpeg development discussions and patches Le 5 août 2025 14:56:18 GMT+07:00, Peter Enderborg <peterend@axis.com> a écrit : >This fixes two old TODO's in ipv6 multicast handling. >A helper function to iterate over all interfaces added to >help 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. > >Signed-off-by: Peter Enderborg <peterend@axis.com> >--- > libavformat/udp.c | 108 ++++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 90 insertions(+), 18 deletions(-) > >diff --git a/libavformat/udp.c b/libavformat/udp.c >index 035db785c2..0a981447a5 100644 >--- a/libavformat/udp.c >+++ b/libavformat/udp.c >@@ -68,6 +68,13 @@ > #include "libavutil/thread.h" > #endif > >+#if HAVE_STRUCT_IPV6_MREQ && 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,79 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, > > return 0; > } >+#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6) >+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)))) { >+ struct ifreq if_req; >+ strncpy(if_req.ifr_name, ife->ifa_name, IFNAMSIZ); >+ if (ioctl(iindex_fd, SIOCGIFINDEX, &if_req) != -1) { >+ struct ipv6_mreq mreq6; >+ memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6_addr, 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) { >+ membership_changed = 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)"); >+ goto error; >+ } >+ } >+ break; >+ case IPV6_LEAVE_GROUP: >+ if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { >+ membership_changed = 1; You can't leave a group you didn't join and you can't assume that the set of interfaces is constant over time. Besides joining a group on more than one interface is generally a terrible idea, as you would receive the same data multiple time (+ reordering). It makes sense to track interfaces and subscribe to all of them for discovery protocols, but it's way more involved (and less portable) than this. >+ } 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)"); >+ goto error; >+ } >+ } >+ break; >+ default: >+ av_log(logctx, AV_LOG_ERROR, "unknown multicast operation\n"); >+ goto error; >+ } >+ } >+ } >+ } >+ } >+ } >+ freeifaddrs(ifal); >+ close(iindex_fd); >+ >+ if (!membership_changed) { >+ av_log(logctx, AV_LOG_ERROR, "no valid interfaces found\n"); >+ return AVERROR(EINVAL); >+ } >+ return 0; >+error: >+ freeifaddrs(ifal); >+ close(iindex_fd); >+ av_log(logctx, AV_LOG_ERROR, "udp_ipv6_multicast_iterate failed\n"); >+ return AVERROR(EINVAL); >+} >+#endif > > static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, > struct sockaddr *local_addr, void *logctx) >@@ -226,15 +306,11 @@ 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) { >- 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(); >- } >+ return udp_ipv6_multicast_iterate(sockfd, >+ (struct sockaddr_in6 *)addr, >+ (struct sockaddr_in6 *)local_addr, >+ IPV6_JOIN_GROUP, >+ logctx); > } > #endif > return 0; >@@ -260,15 +336,11 @@ 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) { >- 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; >- } >+ return udp_ipv6_multicast_iterate(sockfd, >+ (struct sockaddr_in6 *)addr, >+ (struct sockaddr_in6 *)local_addr, >+ IPV6_LEAVE_GROUP, >+ logctx); > } > #endif > return 0; _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast 2025-08-06 15:28 ` Rémi Denis-Courmont @ 2025-08-12 10:59 ` Peter Enderborg 0 siblings, 0 replies; 6+ messages in thread From: Peter Enderborg @ 2025-08-12 10:59 UTC (permalink / raw) To: FFmpeg development discussions and patches, Rémi Denis-Courmont On 8/6/25 17:28, Rémi Denis-Courmont wrote: > > Le 5 août 2025 14:56:18 GMT+07:00, Peter Enderborg <peterend@axis.com> a écrit : >> This fixes two old TODO's in ipv6 multicast handling. >> A helper function to iterate over all interfaces added to >> help 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. >> >> Signed-off-by: Peter Enderborg <peterend@axis.com> >> --- >> libavformat/udp.c | 108 ++++++++++++++++++++++++++++++++++++++-------- >> 1 file changed, 90 insertions(+), 18 deletions(-) >> >> diff --git a/libavformat/udp.c b/libavformat/udp.c >> index 035db785c2..0a981447a5 100644 >> --- a/libavformat/udp.c >> +++ b/libavformat/udp.c >> @@ -68,6 +68,13 @@ >> #include "libavutil/thread.h" >> #endif >> >> +#if HAVE_STRUCT_IPV6_MREQ && 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,79 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, >> >> return 0; >> } >> +#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6) >> +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)))) { >> + struct ifreq if_req; >> + strncpy(if_req.ifr_name, ife->ifa_name, IFNAMSIZ); >> + if (ioctl(iindex_fd, SIOCGIFINDEX, &if_req) != -1) { >> + struct ipv6_mreq mreq6; >> + memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6_addr, 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) { >> + membership_changed = 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)"); >> + goto error; >> + } >> + } >> + break; >> + case IPV6_LEAVE_GROUP: >> + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { >> + membership_changed = 1; > You can't leave a group you didn't join and you can't assume that the set of interfaces is constant over time. Did you read the commit message? > Besides joining a group on more than one interface is generally a terrible idea, as you would receive the same data multiple time (+ reordering). It's not a problem. At least not in linux. You will get an EADDRINUSE if you have another request for the same interface. The problem occurs when the network topology is changed. Still, it's a better solution to try with the topology that exist when the application tries to join. You will get duplicates if multiple network interfaces are connected to the same network segment, but not in a more reasonable setup, and you can still narrow it down by using a specific interface address in that case. > It makes sense to track interfaces and subscribe to all of them for discovery protocols, but it's way more involved (and less portable) than this. It make sense to try all interfaces when the application uses ANY address as input parameter. Without the patch it uses interface 0, then the linux kernel will fallback to do a rt6_lookup. The function rt6_lookup fallback selects one interface based on the routing table. However, the routing table is a function for sending, not for receiving, and to change routing tables it requires admin privileges. This will result in that users without this admin privilege can not access the multicast stream. > >> + } 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)"); >> + goto error; >> + } >> + } >> + break; >> + default: >> + av_log(logctx, AV_LOG_ERROR, "unknown multicast operation\n"); >> + goto error; >> + } >> + } >> + } >> + } >> + } >> + } >> + freeifaddrs(ifal); >> + close(iindex_fd); >> + >> + if (!membership_changed) { >> + av_log(logctx, AV_LOG_ERROR, "no valid interfaces found\n"); >> + return AVERROR(EINVAL); >> + } >> + return 0; >> +error: >> + freeifaddrs(ifal); >> + close(iindex_fd); >> + av_log(logctx, AV_LOG_ERROR, "udp_ipv6_multicast_iterate failed\n"); >> + return AVERROR(EINVAL); >> +} >> +#endif >> >> static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, >> struct sockaddr *local_addr, void *logctx) >> @@ -226,15 +306,11 @@ 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) { >> - 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(); >> - } >> + return udp_ipv6_multicast_iterate(sockfd, >> + (struct sockaddr_in6 *)addr, >> + (struct sockaddr_in6 *)local_addr, >> + IPV6_JOIN_GROUP, >> + logctx); >> } >> #endif >> return 0; >> @@ -260,15 +336,11 @@ 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) { >> - 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; >> - } >> + return udp_ipv6_multicast_iterate(sockfd, >> + (struct sockaddr_in6 *)addr, >> + (struct sockaddr_in6 *)local_addr, >> + IPV6_LEAVE_GROUP, >> + logctx); >> } >> #endif >> return 0; > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
* [FFmpeg-devel] [PATCH v2 0/2] Select output interfaces for ipv6 multicast 2025-08-05 7:56 [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg 2025-08-06 15:28 ` Rémi Denis-Courmont @ 2025-08-07 10:49 ` Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 1/2] configure: Add test_ioctl and test for SIOCGIFINDEX Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 2/2] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg 3 siblings, 0 replies; 6+ messages in thread From: Peter Enderborg @ 2025-08-07 10:49 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Peter Enderborg History: v2 Added configure script to disable interface selection for systems that do not support ioctl SIOCGIFINDEX for mapping name to index-id. Peter Enderborg (2): configure: Add test_ioctl and test for SIOCGIFINDEX avformat/udp: Select output interfaces for ipv6 multicast configure | 18 +++++++++ libavformat/udp.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
* [FFmpeg-devel] [PATCH v2 1/2] configure: Add test_ioctl and test for SIOCGIFINDEX 2025-08-05 7:56 [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg 2025-08-06 15:28 ` Rémi Denis-Courmont 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 0/2] " Peter Enderborg @ 2025-08-07 10:49 ` Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 2/2] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg 3 siblings, 0 replies; 6+ messages in thread From: Peter Enderborg @ 2025-08-07 10:49 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Peter Enderborg Adds a generic ioctl tester and a specific test SIOCGIFINDEX that sets ioctl_gifindex. It is a network specific feature and the tests are run only in network context. Signed-off-by: Peter Enderborg <peterend@axis.com> --- configure | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/configure b/configure index ac143f2fea..c0fac151f8 100755 --- a/configure +++ b/configure @@ -1279,6 +1279,20 @@ int x; EOF } +test_ioctl(){ + log test_ioctl "$@" + ctl=$1 + shift 1 + test_cc "$@" <<EOF +#include <stddef.h> +#include <sys/ioctl.h> +int f; +void x(){ +ioctl(f, $ctl, NULL); +} +EOF +} + check_cflags(){ log check_cflags "$@" test_cflags "$@" && add_cflags "$@" @@ -2536,6 +2550,7 @@ HAVE_LIST=" $TOOLCHAIN_FEATURES $TYPES_LIST gzip + ioctl_gifindex ioctl_posix libdrm_getfb2 makeinfo @@ -2668,6 +2683,7 @@ CONFIG_EXTRA=" vvc_sei wma_freqs wmv2dsp + " CMDLINE_SELECT=" @@ -6010,6 +6026,7 @@ case $target_os in linux) enable section_data_rel_ro enabled_any arm aarch64 && enable_weak linux_perf + ;; irix*) target_os=irix @@ -6615,6 +6632,7 @@ if ! disabled network; then else disable network fi + test_ioctl SIOCGIFINDEX && enable ioctl_gifindex fi check_builtin MemoryBarrier windows.h "MemoryBarrier()" -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
* [FFmpeg-devel] [PATCH v2 2/2] avformat/udp: Select output interfaces for ipv6 multicast 2025-08-05 7:56 [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg ` (2 preceding siblings ...) 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 1/2] configure: Add test_ioctl and test for SIOCGIFINDEX Peter Enderborg @ 2025-08-07 10:49 ` Peter Enderborg 3 siblings, 0 replies; 6+ messages in thread From: Peter Enderborg @ 2025-08-07 10:49 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Peter Enderborg 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. Signed-off-by: Peter Enderborg <peterend@axis.com> --- libavformat/udp.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/libavformat/udp.c b/libavformat/udp.c index 035db785c2..d5d77fdfb8 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,79 @@ 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_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)))) { + struct ifreq if_req; + strncpy(if_req.ifr_name, ife->ifa_name, IFNAMSIZ); + if (ioctl(iindex_fd, SIOCGIFINDEX, &if_req) != -1) { + struct ipv6_mreq mreq6; + memcpy(&mreq6.ipv6mr_multiaddr, &addr->sin6_addr, 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) { + membership_changed = 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)"); + goto error; + } + } + break; + case IPV6_LEAVE_GROUP: + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) == 0) { + membership_changed = 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)"); + goto error; + } + } + break; + default: + av_log(logctx, AV_LOG_ERROR, "unknown multicast operation\n"); + goto error; + } + } + } + } + } + } + freeifaddrs(ifal); + close(iindex_fd); + + if (!membership_changed) { + av_log(logctx, AV_LOG_ERROR, "no valid interfaces found\n"); + return AVERROR(EINVAL); + } + return 0; +error: + freeifaddrs(ifal); + close(iindex_fd); + av_log(logctx, AV_LOG_ERROR, "udp_ipv6_multicast_iterate failed\n"); + return AVERROR(EINVAL); +} +#endif static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx) @@ -226,15 +306,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 +347,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 https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-08-12 10:59 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2025-08-05 7:56 [FFmpeg-devel] [PATCH] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg 2025-08-06 15:28 ` Rémi Denis-Courmont 2025-08-12 10:59 ` Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 0/2] " Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 1/2] configure: Add test_ioctl and test for SIOCGIFINDEX Peter Enderborg 2025-08-07 10:49 ` [FFmpeg-devel] [PATCH v2 2/2] avformat/udp: Select output interfaces for ipv6 multicast Peter Enderborg
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