From patchwork Thu Apr 11 13:01:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SWduamF0b3ZpxIcsIExhemFyIChSUyk=?= X-Patchwork-Id: 48010 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c90a:b0:1a7:a0dc:8de5 with SMTP id gx10csp269468pzb; Thu, 11 Apr 2024 06:01:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXgesSbQZlw8Ge06TVepyiGIBowG0uxqw/pYyJXTZq6dmieD8SAAlYvC6fWjZR3jNsa1dOvowyIJ1lkTenXVaSvXAMAEQE9iyLfFQ== X-Google-Smtp-Source: AGHT+IGGThbjX4grZOL2Yfv+nGq/6Wr/c4TrYsZejmS+SPlBdOEsYRH6D3kzPQQzXGpOXE9VEYNr X-Received: by 2002:a50:c046:0:b0:56e:cde:897c with SMTP id u6-20020a50c046000000b0056e0cde897cmr4186497edd.7.1712840509662; Thu, 11 Apr 2024 06:01:49 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ev21-20020a056402541500b0056c49ebbb14si767297edb.275.2024.04.11.06.01.41; Thu, 11 Apr 2024 06:01:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cubic.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 795F368D109; Thu, 11 Apr 2024 16:01:37 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mx0a-00172501.pphosted.com (mx0b-00172501.pphosted.com [67.231.156.187]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 87C1068C463 for ; Thu, 11 Apr 2024 16:01:30 +0300 (EEST) Received: from pps.filterd (m0043387.ppops.net [127.0.0.1]) by mx0b-00172501.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 43B98Vh6027605 for ; Thu, 11 Apr 2024 06:01:27 -0700 Received: from eur05-db8-obe.outbound.protection.outlook.com (mail-db8eur05lp2105.outbound.protection.outlook.com [104.47.17.105]) by mx0b-00172501.pphosted.com (PPS) with ESMTPS id 3xdhnecbgj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 11 Apr 2024 06:01:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ULXut/X5UgC0tEtVakt/Fey8npdXComJO1QMlFdS5/uDgkGZYOnw0FgzML2i5WZSR9ciSpZMK/A/MnPnW6C7+9cgg7E1x/s1ieREmoeiZzxV83pdpqT48nLqutxfkqqLYd09znLObOL8Cm6d5QlUbWtm/SY13KzwLPDuoxMR8FCb3Y+4tuIwP4i3a15ZF0lcJr+qkZ0aQEg1Nxo2mIAm8ulHClXOjWH2HSgHWEgVLXGE4nUZJuXSB6Iv1We3s/XcH+WlbMNphWTKa3ErsZRVYagTox2Nzzmb330CsXpUwx36VhBm6h28L4tKToNt1E1dlYbx5nsRo3lmtvC2zuCNJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+vipposwrCFd+Ssv2ABMcM6tXOEXBdeoHyOe7gsHfag=; b=PDEOFYXYdGrcnrRGVzMfj50WoAbYBiC8W+0ogbx1oO+1mPIF5vENLaPB1Bw4b5RXmiAX4kCzLZCOtnFluI/KHPK0xKs1c88pvm8Lx1o3fL9G02x7igu24URMBR8FeaOqnWGfO7zC0abcFEhxrgVO/8caEs/KDZTaAaVItmIZbhKznVlDywxf4wUsgpO+SWXG6HkukytPW+nwLZgDlUeUnkuSzn8nAjVbn+5Fj3DiNkqURMRtuYCvoH+X3w4lyM+Ztp+ao7IRFDJ+MiVp2zgw5s64INInbkhVnUx9dUdeu8wpk6sKEB3X7yULZ2pDspdR6ngoSnpfgSlyLRvIHaIgXA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=cubic.com; dmarc=pass action=none header.from=cubic.com; dkim=pass header.d=cubic.com; arc=none Received: from AS8P193MB1997.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:40d::10) by AM8P193MB1297.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:355::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Thu, 11 Apr 2024 13:01:22 +0000 Received: from AS8P193MB1997.EURP193.PROD.OUTLOOK.COM ([fe80::7a08:e115:4e52:5521]) by AS8P193MB1997.EURP193.PROD.OUTLOOK.COM ([fe80::7a08:e115:4e52:5521%3]) with mapi id 15.20.7409.053; Thu, 11 Apr 2024 13:01:22 +0000 From: =?iso-8859-2?q?Ignjatovi=E6=2C_Lazar_=28RS=29?= To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v3] avformat: enable UDP IPv6 multicast interface selection using zone index Thread-Index: AQHajBBaBGDRWdqQ4UKGd/UFOo+JRg== Date: Thu, 11 Apr 2024 13:01:22 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_ActionId=0b50350d-4bdc-46ad-a029-33b783f04297; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_ContentBits=0; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_Enabled=true; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_Method=Privileged; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_Name=Public; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_SetDate=2024-04-11T13:01:53Z; MSIP_Label_878f443b-b434-4a74-8e06-267f84c31196_SiteId=10d6de58-a709-4821-a02c-4c46747e0059; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: AS8P193MB1997:EE_|AM8P193MB1297:EE_ x-ms-office365-filtering-correlation-id: 1dbe941e-1eef-48bd-836f-08dc5a277ca1 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: nZWxYnJhipTVV+gX6Ov8azeTofYuI3uYzxkJE7obhfN6Q26cujPwPXBoIhKfu4JXlvrD1horuIoGWWYmEC6BxS1zFlHsWI4nv2Qx1i2KiUb5ZOat7mv4+J3Zsgl7F/mYi1ibKEqIdtvkEmrETlkfrHT36a6kQhgG/EjihUq5IdBP8exAd+DD3Xj6g8+ntyMcY+lW0IEFFc/fbhOqyRfeMLchZjrvP95FXj0NGjUFHVlk0+LysuNN9UOewzW2YCcXYy3NxkU3R+jXzOKLrXMg4Vwyq0C41YNUsTlKGK5TmltyTaO8tN2WKsxBRIRulhSZYRLqznpjUPPfc2Dx6ZPgr0wMcPRljVaN3/x1TvsNMY5DqiJsIyh19IG8TRRtplawtSDmJ8JiZe2Webljweo2Q9N1Y5epUikxLd0s7OGdphH1h6IUtVrsyyLCFHsD0iqU7ROxFhT6Y0Pt3crse5OGkBY/jWNa+9GMcalpwyS8LGdTaIzyE1bsFFpw9IeVTvAJ2miOWeuu7LQyg2ETkiIcy3eP0jGtwBBCS32nqzZ8Ypmn9juaegxiXrIPKm/HbGZFcReUWLENg1jrmlZJiAyNqzoerURrHxBGKiIzeM3grdg11pXoWaPgaemtJnf2f8XHK4DoOCOjJeCO9OpPbb1wzeZTeGOPiZlNxSA6FPN/Cs29cbd+D9PUxgYT0kIH6TjlVZV0RgAYYdooaJ1Ss87PIChk1nY/Vnos5DDOrHM7nj8= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AS8P193MB1997.EURP193.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230031)(376005)(1800799015)(366007)(38070700009); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-2?q?lF8BGQgiJ+tvGzQoz5ut5kW?= =?iso-8859-2?q?vcVi1SCuMUXmsi5N/iWoIOFORZGAPcZ/QUIN13/syi+DuMwcIFkMbJL5g4NQ?= =?iso-8859-2?q?f1FKDnRglyYI4Z3pxdTrPYlx8FhDmH+nx/6A40HvxeMGoKBmmbeLABy2eG6j?= =?iso-8859-2?q?9VHX662ZRVL7MFPDoE8gfWVNZ6Ons5bgcQffF7v+LjEADFo6qYHteJq+a0LR?= =?iso-8859-2?q?fUb61Lc++iKYhkguBqDH3AyXO1eKSIbx71EkFKitNEKMSbgFDbiY/9oKBiAo?= =?iso-8859-2?q?HUtd4ZFWxiEyeXkxjC/L+bPpEnlYh0al7K3dyIpuPVlEUIVH0gCh/Sb/BwP5?= =?iso-8859-2?q?l/q7prxjbkSlO7wg/ulbUUGXgl10aFcIFRgSYYiqKkQ04hSUhxD+AJyErkKX?= =?iso-8859-2?q?ZpwmvLcdn0nX7EqCErO6VxaOyO9OK8kLU49FbSJVvIbDx7Khkq8XWcPOFCfj?= =?iso-8859-2?q?8d3yy6JYlD1/qUb9aYdcVlaE+gUCwmxWE7SuEuxdhkd9RBsEE0wK0oz6pAe6?= =?iso-8859-2?q?RkqsewWvDeOFxIIui7pzsY9DfBHkpJn85l7BnaGa73n8zkzEh//vxS4TFVw8?= =?iso-8859-2?q?c9sH7Z31cmiby8QRoG/AXz6jiaKzF3U7TF3CmqDy1629984rMYAlgEPU25bv?= =?iso-8859-2?q?l0SVAjICStX9/g9mpbnL4Iv3I8Yw4fZ1gQMocR7g5e2vpLrRFvqlc5MHgOHC?= =?iso-8859-2?q?x+4ieK6g/NwOcWMc9LlE4phABsloqlwcypgcYo1QuIYCPPUTdZO0dPBG/8cK?= =?iso-8859-2?q?5W5xmfp1Gykka9pxk0x8YsjYCJRG7o5iJ+qoZqvM9sDkVsRzFqZiKUNZTPVP?= =?iso-8859-2?q?o4KmeIrgrcgiMGgqhXkjDTftdcwfDn2O7HBuYcXVD46Vc+nUmkL6tlAuZ5mX?= =?iso-8859-2?q?Vz3IdJECy8/JOPFoqDJ2+3LwvzkrKaHOQaq2W21bKlcLv16ad6D3H5rszyFH?= =?iso-8859-2?q?I0aXvtPaVZsvXCSe0mya6KvX6xH9R6sMtRnr2umsGbqEVNHyHRXxa+Q0Y5Q/?= =?iso-8859-2?q?7+z8DaJaqljeRmCD7WpHKD0WjZvuJXcNAs0kMw1RtUVJpP/W40wC2XJ55Jy/?= =?iso-8859-2?q?u8DBcfvMWgYy3QnCfc9B+Cu8McnMLpA1BpLRLQtKT2nhyQGYOaWbQwXcGXAG?= =?iso-8859-2?q?8Sp2/6Z7DNZmZAsuUP9o3AGxg8IM/HiK2rt/Qqrni5M3alX6E21IyphwdhE5?= =?iso-8859-2?q?nYotYKTi4YnMvdK9WddVsTK2OaUkzQCYL51/poYeoFhtIhMoEnEcByDUN/ZA?= =?iso-8859-2?q?5iCAX/jLQpbbXNLd0dgk+zZVjAIY+4boi3usEkTEIsZe2WecUzwJ4yROyRxZ?= =?iso-8859-2?q?sh+a9fi8cVB9YkgerGEbrF+MwZA8pqoVljJ2azkbMxhmR+NIayf0qDZC75dx?= =?iso-8859-2?q?ehgBD7Ts4u94wPiu+WrrjalWrdEGeaSbXx304YzM/6GZeNpj4j8tOWcCrNVf?= =?iso-8859-2?q?FtC37Lqme8cwFBaU1lxfeQlYYJAATE4P0cEAPYSsZ8LsaX/fTj2AU6bLqfqT?= =?iso-8859-2?q?g7LIwg+sTSbutsTDNfr3SaViKdet8PFXaiMdQLz3nZ6vjxQXDC4m6Qvs9KON?= =?iso-8859-2?q?NAQveb+KqncaRufRwyr3+qP/WPLYIkI4hgVH1O8XDmzQnRbTgLta01wM=3D?= MIME-Version: 1.0 X-OriginatorOrg: cubic.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: AS8P193MB1997.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 1dbe941e-1eef-48bd-836f-08dc5a277ca1 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Apr 2024 13:01:22.0906 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 10d6de58-a709-4821-a02c-4c46747e0059 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: AEvoJZvWJpkw7VqgYBzMb3vJbo/C4eObKxjOsaDJxCBp6thQHftnIZUCdaQZsS+myPajbj3RKsnvnZNZLXzmbw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P193MB1297 X-Proofpoint-GUID: s1hcTnfvI69jRqf3EQwHxrOxHWxBh3WT X-Proofpoint-ORIG-GUID: s1hcTnfvI69jRqf3EQwHxrOxHWxBh3WT X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-11_06,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=0 malwarescore=0 bulkscore=0 impostorscore=0 adultscore=0 spamscore=0 lowpriorityscore=0 priorityscore=1501 mlxlogscore=999 clxscore=1015 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404110094 Subject: [FFmpeg-devel] [PATCH v3] avformat: enable UDP IPv6 multicast interface selection using zone index X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ljiyjFbZlb2/ avformat: enable UDP IPv6 multicast interface selection using zone index Enabled IPv6 interface selection using zone index. Properly resolved interface index in places where default 0 interface index is used (marked with TODO: within udp.c). Adjusted binding for multicast sockets that are used for reading from the network. For mcast addresses, bind to mcast address is attempted as before. In case that this fails, which will happen on Windows, socket is bound to INADDR_ANY/IN6ADDR_ANY_INIT depending on address family. Actual interface selection is performed using udp_set_multicast_interface to point to the desired interface for sending. Closes: #368 Signed-off-by: Lazar Ignjatovic --- NOTE: Due to comments, this patch is proposed as one of two alternatives The other alternative uses `localaddr` for defining interfaces. V1 -> V2 reverted iface resolution for IPv4 MCAST_JOIN_SOURCE_GROUP V2 -> V3 resolved conflicts configure | 3 ++ doc/protocols.texi | 2 +- libavformat/network.h | 6 +++ libavformat/udp.c | 85 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 88 insertions(+), 8 deletions(-) -- 2.41.0.windows.2 This message has been marked as Public on 04/11/2024 13:01Z. diff --git a/configure b/configure index 55f1fc354d..96183e58b3 100755 --- a/configure +++ b/configure @@ -2311,6 +2311,7 @@ HEADERS_LIST=" valgrind_valgrind_h windows_h winsock2_h + iphlpapi_h " INTRINSICS_LIST=" @@ -6483,6 +6484,8 @@ if ! disabled network; then check_struct winsock2.h "struct sockaddr" sa_len check_type ws2tcpip.h "struct sockaddr_in6" check_type ws2tcpip.h "struct sockaddr_storage" + check_headers iphlpapi.h -liphlpapi && network_extralibs="$network_extralibs -liphlpapi" || disable iphlpapi_h + check_func_headers iphlpapi.h GetBestInterfaceEx $network_extralibs else disable network fi diff --git a/doc/protocols.texi b/doc/protocols.texi index f54600b846..a8892845d3 100644 --- a/doc/protocols.texi +++ b/doc/protocols.texi @@ -2027,7 +2027,7 @@ packet bursts. Override the local UDP port to bind with. @item localaddr=@var{addr} -Local IP address of a network interface used for sending packets or joining +Local IPv4 address of a network interface used for sending packets or joining multicast groups. @item pkt_size=@var{size} diff --git a/libavformat/network.h b/libavformat/network.h index ca214087fc..2461b651d4 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -38,6 +38,10 @@ #include #include +#if HAVE_IPHLPAPI_H +#include +#endif + #ifndef EPROTONOSUPPORT #define EPROTONOSUPPORT WSAEPROTONOSUPPORT #endif @@ -64,6 +68,8 @@ int ff_neterrno(void); #include #include #include +#include +#include #define ff_neterrno() AVERROR(errno) #endif /* HAVE_WINSOCK2_H */ diff --git a/libavformat/udp.c b/libavformat/udp.c index c1ebdd1222..fa2f62b0b7 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -36,6 +36,7 @@ #include "libavutil/opt.h" #include "libavutil/log.h" #include "libavutil/time.h" +#include "libavutil/avstring.h" #include "network.h" #include "os_support.h" #include "url.h" @@ -220,8 +221,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, 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; + mreq6.ipv6mr_interface = ((struct sockaddr_in6 *)addr)->sin6_scope_id; 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(); @@ -231,6 +231,39 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr, return 0; } +static int udp_set_multicast_interface(int sockfd, struct sockaddr *addr, + struct sockaddr *local_addr, void *logctx) +{ +#ifdef IP_MULTICAST_IF + if (addr->sa_family == AF_INET) { + struct ip_mreq mreq; + + mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; + if (local_addr) + mreq.imr_interface = ((struct sockaddr_in *)local_addr)->sin_addr; + else + mreq.imr_interface.s_addr = INADDR_ANY; + + if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, (const void *)&mreq, sizeof(mreq)) < 0) { + ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_IF)"); + return ff_neterrno(); + } + } +#endif +#if defined(IPV6_MULTICAST_IF) && defined(IPPROTO_IPV6) + if (addr->sa_family == AF_INET6) { + unsigned int iface; + iface = ((struct sockaddr_in6 *)addr)->sin6_scope_id; + + if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &iface, sizeof(unsigned int)) < 0) { + ff_log_net_error(logctx, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_IF)"); + return ff_neterrno(); + } + } +#endif + return 0; +} + static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, struct sockaddr *local_addr, void *logctx) { @@ -254,8 +287,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr, 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; + mreq6.ipv6mr_interface = ((struct sockaddr_in6 *)addr)->sin6_scope_id; 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; @@ -284,6 +316,9 @@ static int udp_set_multicast_sources(URLContext *h, //TODO: Interface index should be looked up from local_addr mreqs.gsr_interface = 0; + if (level == IPPROTO_IPV6) + mreqs.gsr_interface = ((struct sockaddr_in6 *)addr)->sin6_scope_id; + memcpy(&mreqs.gsr_group, addr, addr_len); memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources)); @@ -340,9 +375,24 @@ static int udp_set_url(URLContext *h, { struct addrinfo *res0; int addr_len; + const char *host, *sc; + char address[1024], scope[1024]; - res0 = ff_ip_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0); + if (sc = strchr(hostname, '%')) { + av_strlcpy(address, hostname, FFMIN(1024, sc - hostname + 1)); + av_strlcpy(scope, sc + 1, FFMIN(1024, strlen(hostname) - (sc - hostname))); + host = address; + } else { + host = hostname; + } + + res0 = ff_ip_resolve_host(h, host, port, SOCK_DGRAM, AF_UNSPEC, 0); if (!res0) return AVERROR(EIO); +#if HAVE_IPHLPAPI_H || !HAVE_WINSOCK2_H + if (res0->ai_family== AF_INET6 && strlen(scope) > 0) { + ((struct sockaddr_in6*)res0->ai_addr)->sin6_scope_id = if_nametoindex(scope); + } +#endif memcpy(addr, res0->ai_addr, res0->ai_addrlen); addr_len = res0->ai_addrlen; freeaddrinfo(res0); @@ -841,8 +891,23 @@ static int udp_open(URLContext *h, const char *uri, int flags) if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) { bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len); } - /* bind to the local address if not multicast or if the multicast - * bind failed */ + + /* bind to ADDR_ANY if the multicast bind failed */ + if (s->is_multicast && bind_ret < 0) { + struct addrinfo *res; + + if (s->dest_addr.ss_family == AF_INET) + res = ff_ip_resolve_host(h, "0.0.0.0", 0, SOCK_DGRAM, AF_UNSPEC, 0); + else if (s->dest_addr.ss_family == AF_INET6) + res = ff_ip_resolve_host(h, "::", 0, SOCK_DGRAM, AF_UNSPEC, 0); + + if (res && res->ai_addr) { + bind_ret = bind(udp_fd, res->ai_addr, res->ai_addrlen); + } + + freeaddrinfo(res); + } + /* the bind is needed to give a port to the socket now */ if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) { ff_log_net_error(h, AV_LOG_ERROR, "bind failed"); @@ -855,6 +920,12 @@ static int udp_open(URLContext *h, const char *uri, int flags) s->local_port = udp_port(&my_addr, len); if (s->is_multicast) { + if ((ret = udp_set_multicast_interface(udp_fd, + (struct sockaddr *)&s->dest_addr, + (struct sockaddr *)&s->local_addr_storage, + h)) < 0) + goto fail; + if (h->flags & AVIO_FLAG_WRITE) { /* output */ if ((ret = udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr, h)) < 0)