Skip to content

Commit 311fea0

Browse files
committed
feat: add callback for unhandled STUN requests
Calls the functions added to libjuice in paullouisageneau/libjuice#248 Exports a `OnUnhandledStunRequest` function that can be passed a callback that will be invoked when an incoming STUN message is received that has no corresponding agent for the ICE ufrag. Closes #1166
1 parent 054cd65 commit 311fea0

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

include/rtc/global.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ RTC_CPP_EXPORT void InitLogger(LogLevel level, LogCallback callback = nullptr);
3434
RTC_CPP_EXPORT void Preload();
3535
RTC_CPP_EXPORT std::shared_future<void> Cleanup();
3636

37+
struct UnhandledStunRequest {
38+
optional<std::string> localUfrag;
39+
optional<std::string> remoteUfrag;
40+
std::string address;
41+
uint16_t port;
42+
};
43+
44+
RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request)> UnhandledStunRequestCallback;
45+
46+
RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr);
47+
3748
struct SctpSettings {
3849
// For the following settings, not set means optimized default
3950
optional<size_t> recvBufferSize; // in bytes

include/rtc/rtc.h

+14
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
172172
typedef void(RTC_API *rtcPliHandlerCallbackFunc)(int tr, void *ptr);
173173
typedef void(RTC_API *rtcRembHandlerCallbackFunc)(int tr, unsigned int bitrate, void *ptr);
174174

175+
// Handle STUN requests with unexpected ufrags
176+
177+
typedef struct {
178+
const char * ufrag;
179+
const char * pwd;
180+
uint8_t family;
181+
const char * address;
182+
uint16_t port;
183+
} rtcUnhandledStunRequest;
184+
185+
typedef void(RTC_API *rtcUnhandledStunRequestCallbackFunc)(rtcUnhandledStunRequest request);
186+
187+
RTC_C_EXPORT void rtcOnUnhandledStunRequest(const char *host, int port, rtcUnhandledStunRequestCallbackFunc callback);
188+
175189
// Log
176190

177191
// NULL cb on the first call will log to stdout

src/global.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "impl/init.hpp"
2020

2121
#include <mutex>
22+
#include <map>
23+
24+
#if !USE_NICE
25+
#include <juice/juice.h>
26+
#endif
2227

2328
namespace {
2429

@@ -88,6 +93,54 @@ std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
8893

8994
void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
9095

96+
#if !USE_NICE
97+
98+
UnhandledStunRequestCallback unboundStunCallback;
99+
100+
void InvokeUnhandledStunRequestCallback (const juice_mux_binding_request *info, void *user_ptr) {
101+
PLOG_DEBUG << "Invoking Unbind STUN listener";
102+
auto callback = static_cast<UnhandledStunRequestCallback *>(user_ptr);
103+
104+
(*callback)({
105+
std::string(info->local_ufrag),
106+
std::string(info->remote_ufrag),
107+
std::string(info->address),
108+
info->port
109+
});
110+
}
111+
112+
#endif
113+
114+
void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, [[maybe_unused]] UnhandledStunRequestCallback callback) {
115+
#if USE_NICE
116+
PLOG_WARNING << "BindStunListener is not supported with libnice, please use libjuice";
117+
#else
118+
if (callback == NULL) {
119+
PLOG_DEBUG << "Removing unhandled STUN request listener";
120+
121+
// call with NULL callback to unbind
122+
if (juice_mux_listen(host.c_str(), port, NULL, NULL) < 0) {
123+
throw std::runtime_error("Could not unbind STUN listener");
124+
}
125+
unboundStunCallback = NULL;
126+
127+
return;
128+
}
129+
130+
PLOG_DEBUG << "Adding listener for unhandled STUN requests";
131+
132+
if (unboundStunCallback != NULL) {
133+
throw std::runtime_error("Unhandled STUN request handler already present");
134+
}
135+
136+
unboundStunCallback = std::move(callback);
137+
138+
if (juice_mux_listen(host.c_str(), port, &InvokeUnhandledStunRequestCallback, &unboundStunCallback) < 0) {
139+
throw std::invalid_argument("Could add listener for unhandled STUN requests");
140+
}
141+
#endif
142+
}
143+
91144
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, LogLevel level) {
92145
switch (level) {
93146
case LogLevel::Fatal:

0 commit comments

Comments
 (0)