Skip to content

Commit

Permalink
feat: add callback for unhandled STUN requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain committed Jun 14, 2024
1 parent b756b5a commit 4de4a6d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/rtc/global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ RTC_CPP_EXPORT void InitLogger(LogLevel level, LogCallback callback = nullptr);
RTC_CPP_EXPORT void Preload();
RTC_CPP_EXPORT std::shared_future<void> Cleanup();

RTC_CPP_EXPORT struct UnhandledStunRequest {
optional<std::string> ufrag;
optional<std::string> pwd;
uint8_t family;
std::string address;
uint16_t port;
};

RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request)> UnhandledStunRequestCallback;

RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr);

struct SctpSettings {
// For the following settings, not set means optimized default
optional<size_t> recvBufferSize; // in bytes
Expand Down
14 changes: 14 additions & 0 deletions include/rtc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ typedef void(RTC_API *rtcAvailableCallbackFunc)(int id, void *ptr);
typedef void(RTC_API *rtcPliHandlerCallbackFunc)(int tr, void *ptr);
typedef void(RTC_API *rtcRembHandlerCallbackFunc)(int tr, unsigned int bitrate, void *ptr);

// Handle STUN requests with unexpected ufrags

typedef struct {
const char * ufrag;
const char * pwd;
uint8_t family;
const char * address;
uint16_t port;
} rtcUnhandledStunRequest;

typedef void(RTC_API *rtcUnhandledStunRequestCallbackFunc)(rtcUnhandledStunRequest request);

RTC_C_EXPORT void rtcOnUnhandledStunRequest(const char *host, int port, rtcUnhandledStunRequestCallbackFunc callback);

// Log

// NULL cb on the first call will log to stdout
Expand Down
49 changes: 49 additions & 0 deletions src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include "impl/init.hpp"

#include <mutex>
#include <map>

#if !USE_NICE
#include <juice/juice.h>
#endif

namespace {

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

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

UnhandledStunRequestCallback unboundStunCallback;

void InvokeUnhandledStunRequestCallback (const juice_stun_binding_t *info, void *user_ptr) {
PLOG_DEBUG << "Invoking Unbind STUN listener";
auto callback = static_cast<UnhandledStunRequestCallback *>(user_ptr);

(*callback)({
.ufrag = std::string(info->ufrag),
.pwd = std::string(info->pwd),
.family = info->family,
.address = std::string(info->address),
.port = info->port
});
}

void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, UnhandledStunRequestCallback callback) {
#if USE_NICE
PLOG_WARNING << "BindStunListener is not supported with libnice, please use libjuice";
#else
if (callback == NULL) {
PLOG_DEBUG << "Removing unhandled STUN request listener";

if (juice_unbind_stun() < 0) {
throw std::runtime_error("Could not unbind STUN listener");
}
unboundStunCallback = NULL;

return;
}

PLOG_DEBUG << "Adding listener for unhandled STUN requests";

if (unboundStunCallback != NULL) {
throw std::runtime_error("Unhandled STUN request handler already present");
}

unboundStunCallback = std::move(callback);

if (juice_bind_stun(host.c_str(), port, &InvokeUnhandledStunRequestCallback, &unboundStunCallback) < 0) {
throw std::invalid_argument("Could add listener for unhandled STUN requests");
}
#endif
}

RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, LogLevel level) {
switch (level) {
case LogLevel::Fatal:
Expand Down

0 comments on commit 4de4a6d

Please sign in to comment.