Skip to content

Commit a711abc

Browse files
committed
[WPE][GTK][WebRTC] Support for UDP ports range configuration
https://bugs.webkit.org/show_bug.cgi?id=280330 Reviewed by Xabier Rodriguez-Calvar. In some constrained environments where a firewall blocks UDP network traffic excepted on a specific port range, this settings can be used to give hints to the WebRTC backend regarding which ports to allocate. The format is min-port:max-port, so for instance 20000:30000. The default value is 0:0 which means the OS will use no hints from the WebRTC backend. * Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: * Source/WebCore/platform/mediastream/WebRTCProvider.cpp: (WebCore::WebRTCProvider::setPortAllocatorRange): * Source/WebCore/platform/mediastream/WebRTCProvider.h: * Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp: (WebCore::LibWebRTCProvider::createPeerConnection): * Source/WebKit/UIProcess/API/glib/WebKitSettings.cpp: (_WebKitSettingsPrivate::_WebKitSettingsPrivate): (webKitSettingsSetProperty): (webKitSettingsGetProperty): (webkit_settings_class_init): (webkit_settings_get_webrtc_udp_ports_range): (webkit_settings_set_webrtc_udp_ports_range): * Source/WebKit/UIProcess/API/glib/WebKitSettings.h.in: * Source/WebKit/WebProcess/Network/webrtc/LibWebRTCProvider.cpp: (WebKit::LibWebRTCProvider::LibWebRTCProvider): * Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp: (testWebKitSettings): Canonical link: https://commits.webkit.org/285190@main
1 parent b13b132 commit a711abc

9 files changed

Lines changed: 162 additions & 0 deletions

File tree

Source/WTF/Scripts/Preferences/WebPreferences.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,6 +2588,21 @@ WebMParserEnabled:
25882588
WebKit:
25892589
default: true
25902590

2591+
WebRTCUDPPortRange:
2592+
type: String
2593+
status: embedder
2594+
category: media
2595+
humanReadableName: "WebRTC UDP Port Range"
2596+
humanReadableDescription: "Set a UDP port range for WebRTC. If set to 0:0, the port range is determined by the OS"
2597+
condition: ENABLE(WEB_RTC)
2598+
defaultValue:
2599+
WebKitLegacy:
2600+
default: '"0:0"_str'
2601+
WebKit:
2602+
default: '"0:0"_str'
2603+
WebCore:
2604+
default: '"0:0"_str'
2605+
25912606
WebSecurityEnabled:
25922607
type: bool
25932608
inspectorOverride: true

Source/WebCore/platform/mediastream/WebRTCProvider.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include <wtf/Function.h>
3636
#include <wtf/NeverDestroyed.h>
37+
#include <wtf/text/StringToIntegerConversion.h>
3738

3839
namespace WebCore {
3940

@@ -307,4 +308,29 @@ std::optional<MediaCapabilitiesEncodingInfo> WebRTCProvider::videoEncodingCapabi
307308
return { };
308309
}
309310

311+
void WebRTCProvider::setPortAllocatorRange(StringView range)
312+
{
313+
auto components = range.toStringWithoutCopying().split(':');
314+
if (UNLIKELY(components.size() != 2)) {
315+
WTFLogAlways("Invalid format for UDP port range. Should be \"min-port:max-port\"");
316+
ASSERT_NOT_REACHED();
317+
return;
318+
}
319+
320+
auto minPort = WTF::parseInteger<int>(components[0]).value_or(0);
321+
auto maxPort = WTF::parseInteger<int>(components[1]).value_or(0);
322+
if (!minPort || !maxPort) {
323+
WTFLogAlways("Invalid format for UDP port range. Should be \"min-port:max-port\"");
324+
ASSERT_NOT_REACHED();
325+
return;
326+
}
327+
328+
m_portAllocatorRange = { { minPort, maxPort } };
329+
}
330+
331+
std::optional<std::pair<int, int>> WebRTCProvider::portAllocatorRange() const
332+
{
333+
return m_portAllocatorRange;
334+
}
335+
310336
} // namespace WebCore

Source/WebCore/platform/mediastream/WebRTCProvider.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class WEBCORE_EXPORT WebRTCProvider {
8181
virtual void setLoggingLevel(WTFLogLevel);
8282
virtual void clearFactory();
8383

84+
void setPortAllocatorRange(StringView);
85+
std::optional<std::pair<int, int>> portAllocatorRange() const;
86+
8487
protected:
8588
#if ENABLE(WEB_RTC)
8689
std::optional<RTCRtpCapabilities>& audioDecodingCapabilities();
@@ -107,6 +110,8 @@ class WEBCORE_EXPORT WebRTCProvider {
107110
bool m_supportsVP9Profile2 { false };
108111
bool m_supportsMDNS { false };
109112

113+
std::optional<std::pair<int, int>> m_portAllocatorRange;
114+
110115
private:
111116
virtual void initializeAudioDecodingCapabilities();
112117
virtual void initializeVideoDecodingCapabilities();

Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPee
399399
if (!factory)
400400
return nullptr;
401401

402+
if (auto portRange = portAllocatorRange())
403+
portAllocator->SetPortRange(portRange->first, portRange->second);
404+
402405
webrtc::PeerConnectionDependencies dependencies { &observer };
403406
dependencies.allocator = WTFMove(portAllocator);
404407
dependencies.async_resolver_factory = WTFMove(asyncResolveFactory);

Source/WebKit/UIProcess/API/glib/WebKitSettings.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ struct _WebKitSettingsPrivate {
6767
fantasyFontFamily = preferences->fantasyFontFamily().utf8();
6868
pictographFontFamily = preferences->pictographFontFamily().utf8();
6969
defaultCharset = preferences->defaultTextEncodingName().utf8();
70+
#if ENABLE(WEB_RTC)
71+
webrtcUDPPortsRange = preferences->webRTCUDPPortRange().utf8();
72+
#endif
7073
}
7174

7275
RefPtr<WebPreferences> preferences;
@@ -80,6 +83,9 @@ struct _WebKitSettingsPrivate {
8083
CString defaultCharset;
8184
CString userAgent;
8285
CString mediaContentTypesRequiringHardwareSupport;
86+
#if ENABLE(WEB_RTC)
87+
CString webrtcUDPPortsRange;
88+
#endif
8389
bool allowModalDialogs { false };
8490
bool zoomTextOnly { false };
8591
#if PLATFORM(GTK)
@@ -181,6 +187,7 @@ enum {
181187
PROP_ENABLE_DIRECTORY_UPLOAD,
182188
PROP_ENABLE_SERVICE_WORKER,
183189
PROP_ENABLE_ICE_CANDIDATE_FILTERING,
190+
PROP_WEBRTC_UDP_PORTS_RANGE,
184191
N_PROPERTIES,
185192
};
186193

@@ -438,6 +445,9 @@ ALLOW_DEPRECATED_DECLARATIONS_END
438445
case PROP_ENABLE_ICE_CANDIDATE_FILTERING:
439446
webkit_settings_set_enable_ice_candidate_filtering(settings, g_value_get_boolean(value));
440447
break;
448+
case PROP_WEBRTC_UDP_PORTS_RANGE:
449+
webkit_settings_set_webrtc_udp_ports_range(settings, g_value_get_string(value));
450+
break;
441451
default:
442452
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
443453
break;
@@ -666,6 +676,9 @@ ALLOW_DEPRECATED_DECLARATIONS_END
666676
case PROP_ENABLE_ICE_CANDIDATE_FILTERING:
667677
g_value_set_boolean(value, webkit_settings_get_enable_ice_candidate_filtering(settings));
668678
break;
679+
case PROP_WEBRTC_UDP_PORTS_RANGE:
680+
g_value_set_string(value, webkit_settings_get_webrtc_udp_ports_range(settings));
681+
break;
669682
default:
670683
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
671684
break;
@@ -1774,6 +1787,25 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
17741787
TRUE,
17751788
readWriteConstructParamFlags);
17761789

1790+
/**
1791+
* WebKitSettings:webrtc-udp-ports-range:
1792+
*
1793+
* Allow customization of the WebRTC UDP ports range.
1794+
*
1795+
* In some constrained environments where a firewall blocks UDP network traffic excepted on a
1796+
* specific port range, this settings can be used to give hints to the WebRTC backend regarding
1797+
* which ports to allocate. The format is min-port:max-port, so for instance 20000:30000. The
1798+
* default value is 0:0 which means the OS will use no hints from the WebRTC backend.
1799+
*
1800+
* Since: 2.48
1801+
*/
1802+
sObjProperties[PROP_WEBRTC_UDP_PORTS_RANGE] = g_param_spec_string(
1803+
"webrtc-udp-ports-range",
1804+
_("WebRTC UDP ports range"),
1805+
_("WebRTC UDP ports range, the format is min-port:max-port"),
1806+
nullptr, // A null string forces the default value.
1807+
readWriteConstructParamFlags);
1808+
17771809
g_object_class_install_properties(gObjectClass, N_PROPERTIES, sObjProperties);
17781810
}
17791811

@@ -4466,3 +4498,51 @@ void webkit_settings_set_enable_ice_candidate_filtering(WebKitSettings* settings
44664498
priv->preferences->setICECandidateFilteringEnabled(enabled);
44674499
g_object_notify(G_OBJECT(settings), "enable-ice-candidate-filtering");
44684500
}
4501+
4502+
/**
4503+
* webkit_settings_get_webrtc_udp_ports_range:
4504+
* @settings: a #WebKitSettings
4505+
*
4506+
* Get the [property@Settings:webrtc-udp-ports-range] property.
4507+
*
4508+
* Returns: The WebRTC UDP ports range, or %NULL if un-set.
4509+
*
4510+
* Since: 2.48
4511+
*/
4512+
const gchar*
4513+
webkit_settings_get_webrtc_udp_ports_range(WebKitSettings* settings)
4514+
{
4515+
g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), FALSE);
4516+
#if ENABLE(WEB_RTC)
4517+
return settings->priv->webrtcUDPPortsRange.data();
4518+
#else
4519+
return nullptr;
4520+
#endif
4521+
}
4522+
4523+
/**
4524+
* webkit_settings_set_webrtc_udp_ports_range:
4525+
* @settings: a #WebKitSettings
4526+
* @udp_port_range: Value to be set
4527+
*
4528+
* Set the [property@Settings:webrtc-udp-ports-range] property.
4529+
*
4530+
* Since: 2.48
4531+
*/
4532+
void
4533+
webkit_settings_set_webrtc_udp_ports_range(WebKitSettings* settings, const gchar* udpPortsRange)
4534+
{
4535+
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
4536+
#if ENABLE(WEB_RTC)
4537+
WebKitSettingsPrivate* priv = settings->priv;
4538+
if (!g_strcmp0(priv->webrtcUDPPortsRange.data(), udpPortsRange))
4539+
return;
4540+
4541+
auto portRange = String::fromLatin1(udpPortsRange);
4542+
priv->preferences->setWebRTCUDPPortRange(portRange);
4543+
priv->webrtcUDPPortsRange = portRange.utf8();
4544+
g_object_notify_by_pspec(G_OBJECT(settings), sObjProperties[PROP_WEBRTC_UDP_PORTS_RANGE]);
4545+
#else
4546+
UNUSED_PARAM(udpPortsRange);
4547+
#endif
4548+
}

Source/WebKit/UIProcess/API/gtk/WebKitSettings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,20 @@ WEBKIT_API void
522522
webkit_settings_set_enable_webrtc (WebKitSettings *settings,
523523
gboolean enabled);
524524

525+
WEBKIT_API gboolean
526+
webkit_settings_get_disable_web_security (WebKitSettings *settings);
527+
528+
WEBKIT_API void
529+
webkit_settings_set_disable_web_security (WebKitSettings *settings,
530+
gboolean disabled);
531+
532+
WEBKIT_API const gchar*
533+
webkit_settings_get_webrtc_udp_ports_range (WebKitSettings *settings);
534+
535+
WEBKIT_API void
536+
webkit_settings_set_webrtc_udp_ports_range (WebKitSettings *settings,
537+
const gchar *udp_port_range);
538+
525539
G_END_DECLS
526540

527541
#endif /* WebKitSettings_h */

Source/WebKit/UIProcess/API/wpe/WebKitSettings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,13 @@ WEBKIT_API void
540540
webkit_settings_set_enable_ice_candidate_filtering (WebKitSettings *settings,
541541
gboolean enabled);
542542

543+
WEBKIT_API const gchar*
544+
webkit_settings_get_webrtc_udp_ports_range (WebKitSettings *settings);
545+
546+
WEBKIT_API void
547+
webkit_settings_set_webrtc_udp_ports_range (WebKitSettings *settings,
548+
const gchar *udp_port_range);
549+
543550
G_END_DECLS
544551

545552
#endif /* WebKitSettings_h */

Source/WebKit/WebProcess/Network/webrtc/LibWebRTCProvider.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ LibWebRTCProvider::LibWebRTCProvider(WebPage& webPage)
7373
#else
7474
m_supportsMDNS = true;
7575
#endif
76+
77+
auto* page = webPage.corePage();
78+
if (!page || !page->settings().webRTCUDPPortRange())
79+
return;
80+
81+
setPortAllocatorRange(page->settings().webRTCUDPPortRange());
7682
}
7783

7884
rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPeerConnection(ScriptExecutionContextIdentifier identifier, webrtc::PeerConnectionObserver& observer, rtc::PacketSocketFactory* socketFactory, webrtc::PeerConnectionInterface::RTCConfiguration&& configuration)

Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ ALLOW_DEPRECATED_DECLARATIONS_END
383383
webkit_settings_set_disable_web_security(settings, TRUE);
384384
g_assert_true(webkit_settings_get_disable_web_security(settings));
385385

386+
#if ENABLE(WEB_RTC)
387+
g_assert_cmpstr("", ==, webkit_settings_get_webrtc_udp_ports_range(settings));
388+
webkit_settings_set_webrtc_udp_ports_range(settings, "20000:30000");
389+
g_assert_cmpstr("20000:30000", ==, webkit_settings_get_webrtc_udp_ports_range(settings));
390+
#endif
391+
386392
g_object_unref(G_OBJECT(settings));
387393
}
388394

0 commit comments

Comments
 (0)