Skip to content

Commit dc78582

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 paullouisageneau#1166
1 parent 054cd65 commit dc78582

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

include/rtc/global.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ 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+
RTC_CPP_EXPORT struct UnhandledStunRequest {
38+
optional<std::string> ufrag;
39+
optional<std::string> pwd;
40+
uint8_t family;
41+
std::string address;
42+
uint16_t port;
43+
};
44+
45+
RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request)> UnhandledStunRequestCallback;
46+
47+
RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr);
48+
3749
struct SctpSettings {
3850
// For the following settings, not set means optimized default
3951
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

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

0 commit comments

Comments
 (0)