PR #26065 opened by dangowrt URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26065 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26065.patch
# Summary of changes This adds a `local_dev` option to the tcp and udp protocols. It binds the socket to a named network device with `SO_BINDTODEVICE`. For udp the option also selects the device that multicast groups, including source-specific ones, are joined on, and rtp passes it on to its UDP sockets. Children of a segmented format inherit it the way they inherit `local_addr`. The option exists only where `SO_BINDTODEVICE` is defined. `local_addr` pins a connection to the address a device holds when the URL is opened. A DHCP renewal or an IPv6 privacy or prefix change takes that address away during a long session, and an IPv4 address leaves no way to reach the same uplink over IPv6. Binding to the device lets each connection use the address of its family that the device holds at the time, which is what a player on a multi-homed host needs to keep a stream on one uplink. This builds on #24565: the first two commits here are that pull request rebased onto current master, and the tcp commit adds `local_dev` to the option list `ffio_copy_url_options()` copies to child connections. Only the last two commits are new. Tested on Linux x86_64 with a build limited to the network protocols and the hls and mpegts demuxers, under strace against a loopback HTTP server: `-local_dev lo` issues `setsockopt(SO_BINDTODEVICE, "lo")` before `connect()` on the playlist connection and again on the segment connection of an HLS playlist; a device that does not exist fails the open with "No such device" before any `connect()`; `udp://127.0.0.1:<port>?local_dev=lo` binds the device before `bind()`. `make fate-url` passes. The multicast join on the device index and the rtp path were not exercised in this run, and no other FATE target was run. >From fe73ac40d037b3cb2b7c62761f0d985d611c487c Mon Sep 17 00:00:00 2001 From: Daniel Golle <[email protected]> Date: Thu, 17 Sep 2026 23:11:18 +0100 Subject: [PATCH 1/4] avformat: let a protocol expose the one it runs over Options of a nested protocol cannot be read back from the enclosing one. A URLContext offers its own private context as an AVOptions child, so a lookup on an http connection reaches the HTTP options and stops there, while the TCP options underneath, among them the local bind address, stay out of reach. Exposing the nested context as another AVOptions child would not do: children are searched before the object itself, so tcp would shadow the http options that share a name with it, listen and timeout among them. Add url_get_nested() and implement it for http and tls, which lets a caller walk the chain deliberately. It has no user yet. Signed-off-by: Daniel Golle <[email protected]> --- libavformat/avio.c | 7 +++++++ libavformat/http.c | 8 ++++++++ libavformat/tls.c | 7 +++++++ libavformat/tls.h | 2 ++ libavformat/tls_gnutls.c | 2 ++ libavformat/tls_libtls.c | 1 + libavformat/tls_mbedtls.c | 2 ++ libavformat/tls_openssl.c | 2 ++ libavformat/tls_schannel.c | 2 ++ libavformat/tls_securetransport.c | 1 + libavformat/url.h | 12 ++++++++++++ 11 files changed, 46 insertions(+) diff --git a/libavformat/avio.c b/libavformat/avio.c index f60bc0eae3a..5ee55cb5017 100644 --- a/libavformat/avio.c +++ b/libavformat/avio.c @@ -925,6 +925,13 @@ int ffurl_get_short_seek(void *urlcontext) return h->prot->url_get_short_seek(h); } +URLContext *ffurl_get_nested(URLContext *h) +{ + if (!h || !h->prot || !h->prot->url_get_nested) + return NULL; + return h->prot->url_get_nested(h); +} + int ffurl_shutdown(URLContext *h, int flags) { if (!h || !h->prot || !h->prot->url_shutdown) diff --git a/libavformat/http.c b/libavformat/http.c index 2a4481da591..3f4cb5b00ed 100644 --- a/libavformat/http.c +++ b/libavformat/http.c @@ -2484,6 +2484,12 @@ static int http_get_short_seek(URLContext *h) return ffurl_get_short_seek(s->hd); } +static URLContext *http_get_nested(URLContext *h) +{ + HTTPContext *s = h->priv_data; + return s->hd; +} + #define HTTP_CLASS(flavor) \ static const AVClass flavor ## _context_class = { \ .class_name = # flavor, \ @@ -2506,6 +2512,7 @@ const URLProtocol ff_http_protocol = { .url_close = http_close, .url_get_file_handle = http_get_file_handle, .url_get_short_seek = http_get_short_seek, + .url_get_nested = http_get_nested, .url_shutdown = http_shutdown, .priv_data_size = sizeof(HTTPContext), .priv_data_class = &http_context_class, @@ -2526,6 +2533,7 @@ const URLProtocol ff_https_protocol = { .url_close = http_close, .url_get_file_handle = http_get_file_handle, .url_get_short_seek = http_get_short_seek, + .url_get_nested = http_get_nested, .url_shutdown = http_shutdown, .priv_data_size = sizeof(HTTPContext), .priv_data_class = &https_context_class, diff --git a/libavformat/tls.c b/libavformat/tls.c index 3eab305f568..81aa9b43620 100644 --- a/libavformat/tls.c +++ b/libavformat/tls.c @@ -51,6 +51,13 @@ int ff_tls_parse_host(TLSShared *s, char *hostname, int hostname_size, int *port return 0; } +URLContext *ff_tls_get_nested(URLContext *h) +{ + TLSShared *c = h->priv_data; + + return c->is_dtls ? c->udp : c->tcp; +} + int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options) { int port; diff --git a/libavformat/tls.h b/libavformat/tls.h index 570c245ff44..79cb1b65262 100644 --- a/libavformat/tls.h +++ b/libavformat/tls.h @@ -112,6 +112,8 @@ int ff_tls_parse_host(TLSShared *s, char *hostname, int hostname_size, int *port int ff_tls_open_underlying(TLSShared *c, URLContext *parent, const char *uri, AVDictionary **options); +URLContext *ff_tls_get_nested(URLContext *h); + int ff_url_read_all(const char *url, AVBPrint *bp); int ff_tls_set_external_socket(URLContext *h, URLContext *sock); diff --git a/libavformat/tls_gnutls.c b/libavformat/tls_gnutls.c index c40c2cd7f35..dd9648d3dc6 100644 --- a/libavformat/tls_gnutls.c +++ b/libavformat/tls_gnutls.c @@ -777,6 +777,7 @@ const URLProtocol ff_tls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, @@ -798,6 +799,7 @@ const URLProtocol ff_dtls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &dtls_class, diff --git a/libavformat/tls_libtls.c b/libavformat/tls_libtls.c index 7210f6cd14c..fdba7ed6598 100644 --- a/libavformat/tls_libtls.c +++ b/libavformat/tls_libtls.c @@ -211,6 +211,7 @@ const URLProtocol ff_tls_protocol = { .url_close = ff_tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, diff --git a/libavformat/tls_mbedtls.c b/libavformat/tls_mbedtls.c index 211652d38af..8afd39067fb 100644 --- a/libavformat/tls_mbedtls.c +++ b/libavformat/tls_mbedtls.c @@ -810,6 +810,7 @@ const URLProtocol ff_tls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, @@ -831,6 +832,7 @@ const URLProtocol ff_dtls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &dtls_class, diff --git a/libavformat/tls_openssl.c b/libavformat/tls_openssl.c index 3bcd1e74dd8..2e517182d06 100644 --- a/libavformat/tls_openssl.c +++ b/libavformat/tls_openssl.c @@ -1030,6 +1030,7 @@ const URLProtocol ff_tls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, @@ -1051,6 +1052,7 @@ const URLProtocol ff_dtls_protocol = { .url_write = tls_write, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &dtls_class, diff --git a/libavformat/tls_schannel.c b/libavformat/tls_schannel.c index a789f500fb2..1ada3e922b7 100644 --- a/libavformat/tls_schannel.c +++ b/libavformat/tls_schannel.c @@ -1482,6 +1482,7 @@ const URLProtocol ff_tls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, @@ -1505,6 +1506,7 @@ const URLProtocol ff_dtls_protocol = { .url_write = tls_write, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &dtls_class, diff --git a/libavformat/tls_securetransport.c b/libavformat/tls_securetransport.c index 01965439e93..2b980a5e290 100644 --- a/libavformat/tls_securetransport.c +++ b/libavformat/tls_securetransport.c @@ -424,6 +424,7 @@ const URLProtocol ff_tls_protocol = { .url_close = tls_close, .url_get_file_handle = tls_get_file_handle, .url_get_short_seek = tls_get_short_seek, + .url_get_nested = ff_tls_get_nested, .priv_data_size = sizeof(TLSContext), .flags = URL_PROTOCOL_FLAG_NETWORK, .priv_data_class = &tls_class, diff --git a/libavformat/url.h b/libavformat/url.h index 1075014333c..b22fed6e61b 100644 --- a/libavformat/url.h +++ b/libavformat/url.h @@ -86,6 +86,10 @@ typedef struct URLProtocol { int (*url_get_multi_file_handle)(URLContext *h, int **handles, int *numhandles); int (*url_get_short_seek)(URLContext *h); + /** + * Return the protocol this one runs over, NULL while not connected. + */ + URLContext *(*url_get_nested)(URLContext *h); int (*url_shutdown)(URLContext *h, int flags); const AVClass *priv_data_class; int priv_data_size; @@ -266,6 +270,14 @@ int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles); */ int ffurl_get_short_seek(void *urlcontext); +/** + * Return the protocol this URL runs over, for example the TCP connection + * carrying an HTTP request. + * + * @return the nested resource, or NULL if there is none. + */ +URLContext *ffurl_get_nested(URLContext *h); + /** * Signal the URLContext that we are done reading or writing the stream. * -- 2.54.0 >From 7877c2e687ac5929b64465999edf61e42e05d22b Mon Sep 17 00:00:00 2001 From: Daniel Golle <[email protected]> Date: Thu, 17 Sep 2026 23:11:25 +0100 Subject: [PATCH 2/4] avformat/aviobuf: inherit the bind address in child connections A segmented format opens the playlist and then a connection per segment, and ffio_copy_url_options() decides what those children inherit. It carries the HTTP identity options but not local_addr, so on a multi-homed host the playlist leaves through the interface the caller asked for while every segment goes out of whichever one the route table picks. A service that geolocates then serves the playlist and refuses the segments. Walk the protocol chain with ffurl_get_nested() so an option living below the outermost protocol is found, and carry local_addr and local_port. Signed-off-by: Daniel Golle <[email protected]> --- libavformat/aviobuf.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 43431381b9b..070c2d8aee7 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -31,6 +31,7 @@ #include "avio.h" #include "avio_internal.h" #include "internal.h" +#include "url.h" #include <stdarg.h> #define IO_BUFFER_SIZE 32768 @@ -959,23 +960,40 @@ void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, } } +static uint8_t *url_option_find(AVIOContext *pb, const char *name) +{ + URLContext *h = ffio_geturlcontext(pb); + uint8_t *buf = NULL; + + if (av_opt_get(pb, name, AV_OPT_SEARCH_CHILDREN, &buf) >= 0 && buf[0] != '\0') + return buf; + + av_freep(&buf); + + for (h = ffurl_get_nested(h); h; h = ffurl_get_nested(h)) { + if (av_opt_get(h, name, AV_OPT_SEARCH_CHILDREN, &buf) >= 0 && buf[0] != '\0') + return buf; + av_freep(&buf); + } + + return NULL; +} + int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts) { const char *opts[] = { - "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", "prefer_libcurl", NULL }; + "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", + "prefer_libcurl", "local_addr", "local_port", NULL }; const char **opt = opts; uint8_t *buf = NULL; int ret = 0; while (*opt) { - if (av_opt_get(pb, *opt, AV_OPT_SEARCH_CHILDREN, &buf) >= 0) { - if (buf[0] != '\0') { - ret = av_dict_set(avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL); - if (ret < 0) - return ret; - } else { - av_freep(&buf); - } + buf = url_option_find(pb, *opt); + if (buf) { + ret = av_dict_set(avio_opts, *opt, buf, AV_DICT_DONT_STRDUP_VAL); + if (ret < 0) + return ret; } opt++; } -- 2.54.0 >From 635f7239bf4545fc48a62bf539bbe567ca887ede Mon Sep 17 00:00:00 2001 From: Daniel Golle <[email protected]> Date: Sat, 24 Oct 2026 20:29:23 +0100 Subject: [PATCH 3/4] avformat/tcp: add local_dev to bind the socket to a device local_addr pins a connection to the address an interface holds when the URL is opened. A DHCP renewal or an IPv6 privacy or prefix change takes that address away during a long session, and an IPv4 address leaves no way to reach the same uplink over IPv6. local_dev binds the socket to a named device with SO_BINDTODEVICE, so each connection uses the address of its family the device holds at the time, and children of a segmented format inherit it like local_addr. Signed-off-by: Daniel Golle <[email protected]> --- doc/protocols.texi | 5 +++++ libavformat/aviobuf.c | 2 +- libavformat/tcp.c | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/protocols.texi b/doc/protocols.texi index e802c9121b5..d116056b944 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -2269,6 +2269,11 @@ Local IP address of a network interface used for tcp socket connect. @item local_port=@var{port} Local port used for tcp socket connect. +@item local_dev=@var{device} +Network device the tcp socket is bound to before it connects, so the +connection leaves through that device and takes whichever address it holds +at the time. Linux only. + @item timeout=@var{microseconds} Set raise error timeout, expressed in microseconds. diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c index 070c2d8aee7..284124938f3 100644 --- a/libavformat/aviobuf.c +++ b/libavformat/aviobuf.c @@ -983,7 +983,7 @@ int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts) { const char *opts[] = { "headers", "user_agent", "cookies", "http_proxy", "referer", "rw_timeout", "icy", - "prefer_libcurl", "local_addr", "local_port", NULL }; + "prefer_libcurl", "local_addr", "local_port", "local_dev", NULL }; const char **opt = opts; uint8_t *buf = NULL; int ret = 0; diff --git a/libavformat/tcp.c b/libavformat/tcp.c index 09dc8a96796..b19100a5f28 100644 --- a/libavformat/tcp.c +++ b/libavformat/tcp.c @@ -18,6 +18,9 @@ * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#define _DEFAULT_SOURCE /* SO_BINDTODEVICE with glibc */ + #include "avformat.h" #include "libavutil/avassert.h" #include "libavutil/mem.h" @@ -32,6 +35,7 @@ #if HAVE_POLL_H #include <poll.h> #endif +#include <string.h> typedef struct TCPContext { const AVClass *class; @@ -39,6 +43,7 @@ typedef struct TCPContext { int listen; char *local_port; char *local_addr; + char *local_dev; int open_timeout; int rw_timeout; int listen_timeout; @@ -58,6 +63,9 @@ static const AVOption options[] = { { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E }, { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, { "local_addr", "Local address", OFFSET(local_addr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, +#ifdef SO_BINDTODEVICE + { "local_dev", "Local network device", OFFSET(local_dev), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, +#endif { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, @@ -80,6 +88,16 @@ static const AVClass tcp_class = { static int customize_fd(void *ctx, int fd, int family) { TCPContext *s = ctx; +#ifdef SO_BINDTODEVICE + int err; + + if (s->local_dev && + setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, s->local_dev, strlen(s->local_dev))) { + err = ff_neterrno(); + ff_log_net_error(ctx, AV_LOG_ERROR, "setsockopt(SO_BINDTODEVICE)"); + return err; + } +#endif if (s->local_addr || s->local_port) { struct addrinfo hints = { 0 }, *ai, *cur_ai; -- 2.54.0 >From 8b1913fb6acb14670852dbead6561b1a6b0e5f39 Mon Sep 17 00:00:00 2001 From: Daniel Golle <[email protected]> Date: Sat, 24 Oct 2026 23:08:09 +0100 Subject: [PATCH 4/4] avformat/udp: add local_dev to bind the socket to a device A UDP or RTP stream can only be tied to an uplink by its local address, which goes stale when the interface's address changes, and a multicast group is joined on whatever interface the route to it names. local_dev binds the socket to a named device with SO_BINDTODEVICE and joins multicast groups, and source-specific ones, on that device's index. RTP passes it on to its UDP sockets. Signed-off-by: Daniel Golle <[email protected]> --- doc/protocols.texi | 8 +++++ libavformat/rtpproto.c | 8 +++++ libavformat/udp.c | 79 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/doc/protocols.texi b/doc/protocols.texi index d116056b944..3703fa6a480 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -1426,6 +1426,10 @@ set to 1) or to a default remote address (if set to 0). Local IP address of a network interface used for sending packets or joining multicast groups. +@item local_dev=@var{device} +Network device the RTP and RTCP sockets are bound to, passed on to the +underlying UDP protocol. Linux only. + @item timeout=@var{n} Set timeout (in microseconds) of socket I/O operations to @var{n}. @end table @@ -2537,6 +2541,10 @@ Override the local UDP port to bind with. Local IP address of a network interface used for sending packets or joining multicast groups. +@item local_dev=@var{device} +Network device the socket is bound to, so packets leave and arrive through +that device and multicast groups are joined on it. Linux only. + @item pkt_size=@var{size} Set the size in bytes of UDP packets. diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c index c4309c19e46..15c2994175d 100644 --- a/libavformat/rtpproto.c +++ b/libavformat/rtpproto.c @@ -24,6 +24,8 @@ * RTP protocol */ +#define _DEFAULT_SOURCE /* SO_BINDTODEVICE with glibc */ + #include "libavutil/mem.h" #include "libavutil/parseutils.h" #include "libavutil/avstring.h" @@ -62,6 +64,7 @@ typedef struct RTPContext { char *fec_options_str; int64_t rw_timeout; char *localaddr; + char *local_dev; } RTPContext; #define OFFSET(x) offsetof(RTPContext, x) @@ -87,6 +90,9 @@ static const AVOption options[] = { { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, { "fec", "FEC", OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = E }, { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, +#ifdef SO_BINDTODEVICE + { "local_dev", "Local network device", OFFSET(local_dev), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, +#endif { NULL } }; @@ -205,6 +211,8 @@ static void build_udp_url(RTPContext *s, url_add_option(buf, buf_size, "block=%s", exclude_sources); if (localaddr && localaddr[0]) url_add_option(buf, buf_size, "localaddr=%s", localaddr); + if (s->local_dev && s->local_dev[0]) + url_add_option(buf, buf_size, "local_dev=%s", s->local_dev); } /** diff --git a/libavformat/udp.c b/libavformat/udp.c index 74f3f3674cd..f2bda5d30d8 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -46,6 +46,10 @@ #include "TargetConditionals.h" #endif +#ifdef SO_BINDTODEVICE +#include <net/if.h> +#endif + #if HAVE_UDPLITE_H #include "udplite.h" #else @@ -118,6 +122,7 @@ typedef struct UDPContext { uint8_t tmp[UDP_MAX_PKT_SIZE + sizeof(UDPQueuedPacketHeader)]; int remaining_in_dg; char *localaddr; + char *local_dev; int timeout; int dscp; struct sockaddr_storage local_addr_storage; @@ -138,6 +143,9 @@ static const AVOption options[] = { { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E }, { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, +#ifdef SO_BINDTODEVICE + { "local_dev", "Local network device", OFFSET(local_dev), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, +#endif { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E }, { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E }, { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E }, @@ -209,9 +217,41 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, return 0; } -static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, - struct sockaddr *local_addr, void *logctx) +static unsigned udp_local_ifindex(const UDPContext *s) { +#ifdef SO_BINDTODEVICE + if (s->local_dev) + return if_nametoindex(s->local_dev); +#endif + return 0; +} + +#ifdef SO_BINDTODEVICE +static int udp_membership_ifindex(int sockfd, int cmd, struct sockaddr *addr, + unsigned ifindex, void *logctx, + const char *name) +{ + struct ip_mreqn mreqn = { 0 }; + + mreqn.imr_multiaddr = ((struct sockaddr_in *)addr)->sin_addr; + mreqn.imr_ifindex = ifindex; + if (setsockopt(sockfd, IPPROTO_IP, cmd, &mreqn, sizeof(mreqn)) < 0) { + ff_log_net_error(logctx, AV_LOG_ERROR, name); + return ff_neterrno(); + } + return 0; +} +#endif + +static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, + struct sockaddr *local_addr, + unsigned ifindex, void *logctx) +{ +#if defined(SO_BINDTODEVICE) && defined(IP_ADD_MEMBERSHIP) + if (addr->sa_family == AF_INET && ifindex) + return udp_membership_ifindex(sockfd, IP_ADD_MEMBERSHIP, addr, ifindex, + logctx, "setsockopt(IP_ADD_MEMBERSHIP)"); +#endif #ifdef IP_ADD_MEMBERSHIP if (addr->sa_family == AF_INET) { struct ip_mreq mreq; @@ -233,7 +273,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, 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; + mreq6.ipv6mr_interface = ifindex; 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(); @@ -244,8 +284,14 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, } static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, - struct sockaddr *local_addr, void *logctx) + struct sockaddr *local_addr, + unsigned ifindex, void *logctx) { +#if defined(SO_BINDTODEVICE) && defined(IP_DROP_MEMBERSHIP) + if (addr->sa_family == AF_INET && ifindex) + return udp_membership_ifindex(sockfd, IP_DROP_MEMBERSHIP, addr, ifindex, + logctx, "setsockopt(IP_DROP_MEMBERSHIP)"); +#endif #ifdef IP_DROP_MEMBERSHIP if (addr->sa_family == AF_INET) { struct ip_mreq mreq; @@ -267,7 +313,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, 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; + mreq6.ipv6mr_interface = ifindex; 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; @@ -283,17 +329,19 @@ static int udp_set_multicast_sources(URLContext *h, struct sockaddr_storage *sources, int nb_sources, int include) { - if (addr->sa_family != AF_INET) { + unsigned ifindex = udp_local_ifindex(h->priv_data); + + if (addr->sa_family != AF_INET || ifindex) { #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) /* For IPv4 prefer the old approach, as that alone works reliably on * Windows and it also supports supplying the interface based on its - * address. */ + * address. Only this one takes an interface index for a device. */ for (int i = 0; i < nb_sources; i++) { struct group_source_req mreqs; int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; //TODO: Interface index should be looked up from local_addr - mreqs.gsr_interface = 0; + mreqs.gsr_interface = ifindex; memcpy(&mreqs.gsr_group, addr, addr_len); memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources)); @@ -387,6 +435,15 @@ static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr, if (udp_fd < 0) goto fail; +#ifdef SO_BINDTODEVICE + if (s->local_dev && + setsockopt(udp_fd, SOL_SOCKET, SO_BINDTODEVICE, s->local_dev, + strlen(s->local_dev))) { + ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_BINDTODEVICE)"); + goto fail; + } +#endif + memcpy(addr, res->ai_addr, res->ai_addrlen); *addr_len = res->ai_addrlen; @@ -857,7 +914,8 @@ static int udp_open(URLContext *h, const char *uri, int flags) goto fail; } else { if ((ret = udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr, - (struct sockaddr *)&s->local_addr_storage, h)) < 0) + (struct sockaddr *)&s->local_addr_storage, + udp_local_ifindex(s), h)) < 0) goto fail; } if (s->filters.nb_exclude_addrs) { @@ -1125,7 +1183,8 @@ static int udp_close(URLContext *h) if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr, - (struct sockaddr *)&s->local_addr_storage, h); + (struct sockaddr *)&s->local_addr_storage, + udp_local_ifindex(s), h); #if HAVE_THREADS if (s->thread_started) { int ret; -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
