Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add callback for unhandled STUN requests #1211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading