Parcourir la source

Add SCTP_DEBUG option to enable usrsctp debug output in verbose log

Paul-Louis Ageneau il y a 4 ans
Parent
commit
c806f10527
3 fichiers modifiés avec 29 ajouts et 3 suppressions
  1. 4 0
      CMakeLists.txt
  2. 24 3
      src/impl/sctptransport.cpp
  3. 1 0
      src/impl/sctptransport.hpp

+ 4 - 0
CMakeLists.txt

@@ -15,6 +15,7 @@ option(NO_EXAMPLES "Disable examples" OFF)
 option(NO_TESTS "Disable tests build" OFF)
 option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
 option(CAPI_STDCALL "Set calling convention of C API callbacks stdcall" OFF)
+option(SCTP_DEBUG "Enable SCTP debugging output to verbose log" OFF)
 
 if(USE_GNUTLS)
 	option(USE_NETTLE "Use Nettle in libjuice" ON)
@@ -199,6 +200,9 @@ find_package(Threads REQUIRED)
 set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
 add_subdirectory(deps/plog EXCLUDE_FROM_ALL)
 
+if(SCTP_DEBUG)
+	add_definitions(-DSCTP_DEBUG)
+endif()
 option(sctp_build_programs 0)
 option(sctp_build_shared_lib 0)
 set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

+ 24 - 3
src/impl/sctptransport.cpp

@@ -22,6 +22,8 @@
 #include "logcounter.hpp"
 
 #include <chrono>
+#include <cstdarg>
+#include <cstdio>
 #include <exception>
 #include <iostream>
 #include <limits>
@@ -113,9 +115,12 @@ private:
 SctpTransport::InstancesSet *SctpTransport::Instances = new InstancesSet;
 
 void SctpTransport::Init() {
-	usrsctp_init(0, &SctpTransport::WriteCallback, nullptr);
+	usrsctp_init(0, SctpTransport::WriteCallback, SctpTransport::DebugCallback);
 	usrsctp_sysctl_set_sctp_pr_enable(1);  // Enable Partial Reliability Extension (RFC 3758)
 	usrsctp_sysctl_set_sctp_ecn_enable(0); // Disable Explicit Congestion Notification
+#ifdef SCTP_DEBUG
+	usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
+#endif
 }
 
 void SctpTransport::SetSettings(const SctpSettings &s) {
@@ -882,7 +887,7 @@ optional<milliseconds> SctpTransport::rtt() {
 void SctpTransport::UpcallCallback(struct socket *, void *arg, int /* flags */) {
 	auto *transport = static_cast<SctpTransport *>(arg);
 
-	if (auto lock = Instances->lock(transport))
+	if (auto locked = Instances->lock(transport))
 		transport->handleUpcall();
 }
 
@@ -891,10 +896,26 @@ int SctpTransport::WriteCallback(void *ptr, void *data, size_t len, uint8_t tos,
 
 	// Workaround for sctplab/usrsctp#405: Send callback is invoked on already closed socket
 	// https://github.com/sctplab/usrsctp/issues/405
-	if (auto lock = Instances->lock(transport))
+	if (auto locked = Instances->lock(transport))
 		return transport->handleWrite(static_cast<byte *>(data), len, tos, set_df);
 	else
 		return -1;
 }
 
+void SctpTransport::DebugCallback(const char *format, ...) {
+	const size_t bufferSize = 1024;
+	char buffer[bufferSize];
+	va_list va;
+	va_start(va, format);
+	int len = std::vsnprintf(buffer, bufferSize, format, va);
+	va_end(va);
+	if (len <= 0)
+		return;
+
+	len = std::min(len, int(bufferSize - 1));
+	buffer[len - 1] = '\0'; // remove newline
+
+	PLOG_VERBOSE << "usrsctp: " << buffer; // usrsctp debug as verbose
+}
+
 } // namespace rtc::impl

+ 1 - 0
src/impl/sctptransport.hpp

@@ -117,6 +117,7 @@ private:
 
 	static void UpcallCallback(struct socket *sock, void *arg, int flags);
 	static int WriteCallback(void *sctp_ptr, void *data, size_t len, uint8_t tos, uint8_t set_df);
+	static void DebugCallback(const char *format, ...);
 
 	class InstancesSet;
 	static InstancesSet *Instances;