Ver código fonte

Add DUMP_HEX_PKG function to allow easier fuzz debugging

Stefan Profanter 7 anos atrás
pai
commit
0e6d5a9a56

+ 6 - 0
CMakeLists.txt

@@ -116,6 +116,8 @@ option(UA_BUILD_UNIT_TESTS "Build the unit tests" OFF)
 option(UA_BUILD_FUZZING "Build the fuzzing executables" OFF)
 option(UA_BUILD_OSS_FUZZ "Special build switch used in oss-fuzz" OFF)
 mark_as_advanced(UA_BUILD_OSS_FUZZ)
+option(UA_DEBUG_DUMP_PKGS "Dump every package received by the server as hexdump format" OFF)
+mark_as_advanced(UA_DEBUG_DUMP_PKGS)
 option(UA_BUILD_EXAMPLES_NODESET_COMPILER "Generate an OPC UA information model from a nodeset XML (experimental)" OFF)
 
 # Advanced Build Targets
@@ -334,6 +336,10 @@ set(default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.c
                            ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
                            ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
                            ${PROJECT_SOURCE_DIR}/plugins/ua_config_standard.c)
+if(UA_DEBUG_DUMP_PKGS)
+    list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/plugins/ua_debug_dump_pkgs.c)
+endif()
+
 if(UA_ENABLE_EMBEDDED_LIBC)
   list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/deps/libc_string.c)
 endif()

+ 1 - 0
include/ua_config.h.in

@@ -40,6 +40,7 @@ extern "C" {
 #cmakedefine UA_ENABLE_DISCOVERY
 #cmakedefine UA_ENABLE_DISCOVERY_MULTICAST
 #cmakedefine UA_ENABLE_DISCOVERY_SEMAPHORE
+#cmakedefine UA_DEBUG_DUMP_PKGS
 
 /**
  * Standard Includes

+ 31 - 0
plugins/ua_debug_dump_pkgs.c

@@ -0,0 +1,31 @@
+/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
+ * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
+
+#include "ua_util.h"
+
+#include <ctype.h>
+#include <stdio.h>
+void UA_dump_hex_pkg(UA_Byte* buffer, size_t bufferLen) {
+    printf("--------------- HEX Package Start ---------------\n");
+    char ascii[17];
+    memset(ascii,0,17);
+    for (size_t i = 0; i < bufferLen; i++)
+    {
+        if (i == 0)
+            printf("%08zx ", i);
+        else if (i%16 == 0)
+            printf("|%s|\n%08zx ", ascii, i);
+        if (isprint((int)(buffer[i])))
+            ascii[i%16] = (char)buffer[i];
+        else
+            ascii[i%16] = '.';
+        printf("%02X ", (unsigned char)buffer[i]);
+    }
+    size_t fillPos = bufferLen %16;
+    ascii[fillPos] = 0;
+    for (size_t i=fillPos; i<16; i++) {
+        printf("   ");
+    }
+    printf("|%s|\n%08zx\n", ascii, bufferLen);
+    printf("--------------- HEX Package END ---------------\n");
+}

+ 5 - 0
src/server/ua_server_binary.c

@@ -614,6 +614,11 @@ processBinaryMessage(UA_Server *server, UA_Connection *connection,
                      UA_ByteString *message) {
     UA_LOG_TRACE(server->config.logger, UA_LOGCATEGORY_NETWORK,
                  "Connection %i | Received a packet.", connection->sockfd);
+
+    #ifdef UA_DEBUG_DUMP_PKGS
+    UA_dump_hex_pkg(message->data, message->length);
+    #endif
+
     UA_Boolean realloced = UA_FALSE;
     UA_StatusCode retval = UA_Connection_completeChunks(connection, message, &realloced);
 

+ 4 - 0
src/ua_util.h

@@ -128,6 +128,10 @@ size_t UA_readNumber(u8 *buf, size_t buflen, u32 *number);
 # define UA_TYPENAME(name)
 #endif
 
+#ifdef UA_DEBUG_DUMP_PKGS
+void UA_EXPORT UA_dump_hex_pkg(UA_Byte* buffer, size_t bufferLen);
+#endif
+
 #ifdef __cplusplus
 } // extern "C"
 #endif