From 8e05bad0c9dbd118b76411e96c61cb073aedfd53 Mon Sep 17 00:00:00 2001 From: Alaric Senat <dev.asenat@posteo.net> Date: Tue, 5 Apr 2022 13:15:43 +0200 Subject: [PATCH] upnp: exclusively use `UpnpInit2` `UpnpInit` is deprecated and unmaintained since 1.6. It is known to be vulnerable (CVE-2020-12695). Dropping its support simplifies a lot the network code as solely a net interface name is required by `UpnpInit2` now. --- modules/services_discovery/upnp-wrapper.cpp | 25 ++-- modules/services_discovery/upnp-wrapper.hpp | 140 -------------------- 2 files changed, 11 insertions(+), 154 deletions(-) diff --git a/modules/services_discovery/upnp-wrapper.cpp b/modules/services_discovery/upnp-wrapper.cpp index ab9da0838590..01c0dcb6e02d 100644 --- a/modules/services_discovery/upnp-wrapper.cpp +++ b/modules/services_discovery/upnp-wrapper.cpp @@ -57,20 +57,17 @@ UpnpInstanceWrapper *UpnpInstanceWrapper::get(vlc_object_t *p_obj) return NULL; } - /* libupnp 1.8 deprecate `UpnpInit` and introduce `UpnpInit2` as a replacement. */ - #if UPNP_VERSION >= 10800 - char* psz_miface = var_InheritString( p_obj, "miface" ); - if (psz_miface == NULL) - psz_miface = getPreferedAdapter(); - msg_Info( p_obj, "Initializing libupnp on '%s' interface", psz_miface ? psz_miface : "default" ); - int i_res = UpnpInit2( psz_miface, 0 ); - free( psz_miface ); - #else - /* If UpnpInit2 isn't available, initialize on first IPv4-capable interface */ - char *psz_hostip = getIpv4ForMulticast(); - int i_res = UpnpInit( psz_hostip, 0 ); - free(psz_hostip); - #endif /* UPNP_VERSION >= 10800 */ + char *net_iface = var_InheritString( p_obj, "miface" ); + if ( net_iface == NULL ) + { + // No forced multicast network interface, select one by default. + net_iface = getPreferedAdapter(); + } + msg_Info( p_obj, "Initializing libupnp on '%s' interface", + net_iface ? net_iface : "default" ); + int i_res = UpnpInit2( net_iface, 0 ); + free( net_iface ); + if( i_res != UPNP_E_SUCCESS ) { msg_Err( p_obj, "Initialization failed: %s", UpnpGetErrorMessage( i_res ) ); diff --git a/modules/services_discovery/upnp-wrapper.hpp b/modules/services_discovery/upnp-wrapper.hpp index b999078297c8..96b448a86722 100644 --- a/modules/services_discovery/upnp-wrapper.hpp +++ b/modules/services_discovery/upnp-wrapper.hpp @@ -236,93 +236,6 @@ inline char* getPreferedAdapter() free(addresses); return NULL; } - -inline char *getIpv4ForMulticast() -{ - IP_ADAPTER_UNICAST_ADDRESS *p_best_ip = NULL; - wchar_t psz_uri[32]; - DWORD strSize; - IP_ADAPTER_ADDRESSES *p_adapter, *addresses; - - addresses = ListAdapters(); - if (addresses == NULL) - return NULL; - - /* find one with multicast capabilities */ - p_adapter = addresses; - while (p_adapter != NULL) - { - if (isAdapterSuitable( p_adapter, false )) - { - /* make sure it supports 239.255.255.250 */ - IP_ADAPTER_MULTICAST_ADDRESS *p_multicast = getMulticastAddress( p_adapter ); - if (p_multicast != NULL) - { - /* get an IPv4 address */ - IP_ADAPTER_UNICAST_ADDRESS *p_unicast = p_adapter->FirstUnicastAddress; - while (p_unicast != NULL) - { - strSize = sizeof( psz_uri ) / sizeof( wchar_t ); - if( WSAAddressToString( p_unicast->Address.lpSockaddr, - p_unicast->Address.iSockaddrLength, - NULL, psz_uri, &strSize ) == 0 ) - { - if ( p_best_ip == NULL || - p_best_ip->ValidLifetime > p_unicast->ValidLifetime ) - { - p_best_ip = p_unicast; - } - } - p_unicast = p_unicast->Next; - } - } - } - p_adapter = p_adapter->Next; - } - - if ( p_best_ip != NULL ) - goto done; - - /* find any with IPv4 */ - p_adapter = addresses; - while (p_adapter != NULL) - { - if (isAdapterSuitable(p_adapter, false)) - { - /* get an IPv4 address */ - IP_ADAPTER_UNICAST_ADDRESS *p_unicast = p_adapter->FirstUnicastAddress; - while (p_unicast != NULL) - { - strSize = sizeof( psz_uri ) / sizeof( wchar_t ); - if( WSAAddressToString( p_unicast->Address.lpSockaddr, - p_unicast->Address.iSockaddrLength, - NULL, psz_uri, &strSize ) == 0 ) - { - if ( p_best_ip == NULL || - p_best_ip->ValidLifetime > p_unicast->ValidLifetime ) - { - p_best_ip = p_unicast; - } - } - p_unicast = p_unicast->Next; - } - } - p_adapter = p_adapter->Next; - } - -done: - if (p_best_ip != NULL) - { - strSize = sizeof( psz_uri ) / sizeof( wchar_t ); - WSAAddressToString( p_best_ip->Address.lpSockaddr, - p_best_ip->Address.iSockaddrLength, - NULL, psz_uri, &strSize ); - free(addresses); - return FromWide( psz_uri ); - } - free(addresses); - return NULL; -} #else /* _WIN32 */ #ifdef __APPLE__ @@ -400,59 +313,6 @@ inline char *getPreferedAdapter() #endif -#ifdef __APPLE__ - -inline bool necessaryFlagsSetOnInterface(struct ifaddrs *anInterface) -{ - unsigned int flags = anInterface->ifa_flags; - if( (flags & IFF_UP) && (flags & IFF_RUNNING) && !(flags & IFF_LOOPBACK) && !(flags & IFF_POINTOPOINT) ) { - return true; - } - return false; -} - -static char *getIpv4ForMulticast() -{ - struct ifaddrs *listOfInterfaces; - struct ifaddrs *anInterface; - int ret = getifaddrs(&listOfInterfaces); - char *bestIP = NULL; - - if (ret != 0) { - return NULL; - } - - anInterface = listOfInterfaces; - while (anInterface != NULL) { - if (anInterface->ifa_addr->sa_family == AF_INET) { - bool ret = necessaryFlagsSetOnInterface(anInterface); - if (ret) { - /* ignore sockets connecting to the touchbar on MacBooks */ - if (strncmp(anInterface->ifa_name, "bridge", 6) != 0) { - if (bestIP) { - FREENULL(bestIP); - } - bestIP = strdup(inet_ntoa(((struct sockaddr_in *)anInterface->ifa_addr)->sin_addr)); - } - } - } - - anInterface = anInterface->ifa_next; - } - freeifaddrs(listOfInterfaces); - - return bestIP; -} - -#else - -static char *getIpv4ForMulticast() -{ - return NULL; -} - -#endif - #endif /* _WIN32 */ #endif -- GitLab