Browse Source

chore: pr comments

achingbrain 7 months ago
parent
commit
4e0a5d80da
3 changed files with 22 additions and 49 deletions
  1. 3 8
      include/rtc/global.hpp
  2. 0 14
      include/rtc/rtc.h
  3. 19 27
      src/global.cpp

+ 3 - 8
include/rtc/global.hpp

@@ -34,21 +34,16 @@ RTC_CPP_EXPORT void InitLogger(LogLevel level, LogCallback callback = nullptr);
 RTC_CPP_EXPORT void Preload();
 RTC_CPP_EXPORT std::shared_future<void> Cleanup();
 
-struct UnhandledStunRequest {
+struct IceUdpMuxRequest {
 	std::string localUfrag;
 	std::string remoteUfrag;
 	std::string remoteHost;
 	uint16_t remotePort;
 };
 
-RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request, void* userPtr)> UnhandledStunRequestCallback;
+RTC_CPP_EXPORT typedef std::function<void(IceUdpMuxRequest request)> IceUdpMuxCallback;
 
-struct UnhandledStunRequestHandler {
-	UnhandledStunRequestCallback callback;
-	void *userPtr;
-};
-
-RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr, void *userPtr = nullptr);
+RTC_CPP_EXPORT void ListenIceUdpMux (int port, IceUdpMuxCallback *callback, std::optional<std::string> bindAddress = nullptr);
 
 struct SctpSettings {
 	// For the following settings, not set means optimized default

+ 0 - 14
include/rtc/rtc.h

@@ -172,20 +172,6 @@ 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

+ 19 - 27
src/global.cpp

@@ -93,57 +93,49 @@ std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
 
 void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
 
-std::map<int, UnhandledStunRequestHandler *> unboundStunCallbacks;
-
 #if !USE_NICE
 
-void InvokeUnhandledStunRequestCallback (const juice_mux_binding_request *info, void *user_ptr) {
-	PLOG_DEBUG << "Invoking unhandled STUN request callback";
+void InvokeIceUdpMuxCallback (const juice_mux_binding_request *info, void *user_ptr) {
+	PLOG_DEBUG << "Invoking ICE UDP mux callback";
 
-	UnhandledStunRequestHandler *handler = (struct UnhandledStunRequestHandler*)user_ptr;
+	IceUdpMuxCallback *callback = (IceUdpMuxCallback*)user_ptr;
 
-	if (handler->callback) {
-		handler->callback({
+	if (callback) {
+		(*callback)({
 			std::string(info->local_ufrag),
 			std::string(info->remote_ufrag),
 			std::string(info->address),
 			info->port
-		}, handler->userPtr);
+		});
 	} else {
-		PLOG_DEBUG << "No unhandled STUN request callback configured for port " << info->port;
+		PLOG_DEBUG << "No ICE UDP mux callback configured for port " << info->port;
 	}
 }
 
 #endif
 
-void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, [[maybe_unused]] UnhandledStunRequestCallback callback, [[maybe_unused]] void *userPtr) {
+void ListenIceUdpMux ([[maybe_unused]] int port, [[maybe_unused]] IceUdpMuxCallback *callback, [[maybe_unused]] std::optional<std::string> bindAddress) {
 	#if USE_NICE
-		PLOG_WARNING << "BindStunListener is not supported with libnice, please use libjuice";
+		PLOG_WARNING << "ListenIceUdpMux is not supported with libnice, please use libjuice";
 	#else
-	if (callback == NULL) {
-		PLOG_DEBUG << "Removing unhandled STUN request listener";
+	// NULL host binds to all available addresses
+	const char *host = bindAddress.has_value() ? bindAddress.value().c_str() : NULL;
 
-		free(unboundStunCallbacks.at(port));
-		unboundStunCallbacks.erase(port);
+	if (callback == NULL) {
+		PLOG_DEBUG << "Removing ICE UDP mux callback for port " << port;
 
-		// call with NULL callback to unbind
-		if (juice_mux_listen(host.c_str(), port, NULL, NULL) < 0) {
-			throw std::runtime_error("Could not unbind STUN listener");
+		// call with NULL callback to remove the listener for the host/port
+		if (juice_mux_listen(host, port, NULL, NULL) < 0) {
+			throw std::runtime_error("Could not remove ICE UDP mux callback");
 		}
 
 		return;
 	}
 
-	PLOG_DEBUG << "Adding listener for unhandled STUN requests";
-
-	UnhandledStunRequestHandler *handler = (UnhandledStunRequestHandler*)calloc(1, sizeof(UnhandledStunRequestHandler));
-	handler->callback = std::move(callback);
-	handler->userPtr = userPtr;
-
-	unboundStunCallbacks[port] = handler;
+	PLOG_DEBUG << "Adding ICE UDP mux callback for port " << port;
 
-	if (juice_mux_listen(host.c_str(), port, &InvokeUnhandledStunRequestCallback, handler) < 0) {
-		throw std::invalid_argument("Could not add listener for unhandled STUN requests");
+	if (juice_mux_listen(host, port, &InvokeIceUdpMuxCallback, callback) < 0) {
+		throw std::invalid_argument("Could not add ICE UDP mux callback");
 	}
 	#endif
 }