Prechádzať zdrojové kódy

changing session id to random GUID, updated logging of session ids

Stasik0 8 rokov pred
rodič
commit
3fd2bffe2d

+ 12 - 0
include/ua_log.h

@@ -60,6 +60,18 @@ typedef enum {
  * the minimum log-level defined in ua_config.h into account. */
 typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category, const char *msg, ...);
 
+/**
+ * Signatures for printing complex types
+ */
+
+#define PRINTF_GUID_FORMAT "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"
+#define PRINTF_GUID_DATA(PREFIX) PREFIX.identifier.guid.data1, PREFIX.identifier.guid.data2, \
+                                 PREFIX.identifier.guid.data3, PREFIX.identifier.guid.data4[0], \
+                                 PREFIX.identifier.guid.data4[1], PREFIX.identifier.guid.data4[2], \
+                                 PREFIX.identifier.guid.data4[3], PREFIX.identifier.guid.data4[4], \
+                                 PREFIX.identifier.guid.data4[5], PREFIX.identifier.guid.data4[6], \
+                                 PREFIX.identifier.guid.data4[7]\
+
 #if UA_LOGLEVEL <= 100
 #define UA_LOG_TRACE(LOGGER, CATEGORY, ...) do { \
         if(LOGGER) LOGGER(UA_LOGLEVEL_TRACE, CATEGORY, __VA_ARGS__); } while(0)

+ 2 - 2
src/server/ua_services_session.c

@@ -38,8 +38,8 @@ void Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
         UA_SessionManager_removeSession(&server->sessionManager, &newSession->authenticationToken);
          return;
     }
-    UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Session %i created",
-                         newSession->sessionId.identifier.numeric);
+    UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Session " PRINTF_GUID_FORMAT " created",
+                         PRINTF_GUID_DATA(newSession->sessionId));
 }
 
 void

+ 1 - 4
src/server/ua_session_manager.c

@@ -1,12 +1,9 @@
 #include "ua_session_manager.h"
 #include "ua_server_internal.h"
 
-#define STARTSESSIONID 1
-
 UA_StatusCode
 UA_SessionManager_init(UA_SessionManager *sm, UA_Server *server) {
     LIST_INIT(&sm->sessions);
-    sm->lastSessionId = STARTSESSIONID;
     sm->currentSessionCount = 0;
     sm->server = server;
     return UA_STATUSCODE_GOOD;
@@ -72,7 +69,7 @@ UA_SessionManager_createSession(UA_SessionManager *sm, UA_SecureChannel *channel
 
     sm->currentSessionCount++;
     UA_Session_init(&newentry->session);
-    newentry->session.sessionId = UA_NODEID_NUMERIC(1, sm->lastSessionId++);
+    newentry->session.sessionId = UA_NODEID_GUID(1, UA_Guid_random());
     newentry->session.authenticationToken = UA_NODEID_GUID(1, UA_Guid_random());
 
     if(request->requestedSessionTimeout <= sm->server->config.maxSessionTimeout &&

+ 0 - 1
src/server/ua_session_manager.h

@@ -13,7 +13,6 @@ typedef struct session_list_entry {
 
 typedef struct UA_SessionManager {
     LIST_HEAD(session_list, session_list_entry) sessions; // doubly-linked list of sessions
-    UA_UInt32 lastSessionId;
     UA_UInt32 currentSessionCount;
     UA_Server *server;
 } UA_SessionManager;

+ 8 - 9
src/server/ua_subscription.c

@@ -45,17 +45,17 @@ void MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem) {
 
 static void SampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem) {
     if(monitoredItem->monitoredItemType != UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
-        UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session %i | MonitoredItem %i | "
+        UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session " PRINTF_GUID_FORMAT " | MonitoredItem %i | "
                      "Cannot process a monitoreditem that is not a data change notification",
-                     monitoredItem->subscription->session->sessionId, monitoredItem->itemId);
+                     PRINTF_GUID_DATA(monitoredItem->subscription->session->sessionId), monitoredItem->itemId);
         return;
     }
 
     MonitoredItem_queuedValue *newvalue = UA_malloc(sizeof(MonitoredItem_queuedValue));
     if(!newvalue) {
         UA_LOG_INFO(server->config.logger, UA_LOGCATEGORY_SERVER,
-                    "Session %i | MonitoredItem %i | Skipped a sample due to lack of memory",
-                    monitoredItem->subscription->session->sessionId, monitoredItem->itemId);
+                    "Session " PRINTF_GUID_FORMAT " | MonitoredItem %i | Skipped a sample due to lack of memory",
+                    PRINTF_GUID_DATA(monitoredItem->subscription->session->sessionId), monitoredItem->itemId);
         return;
     }
     UA_DataValue_init(&newvalue->value);
@@ -90,16 +90,15 @@ static void SampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem) {
         UA_ByteString_deleteMembers(&newValueAsByteString);
         UA_DataValue_deleteMembers(&newvalue->value);
         UA_free(newvalue);
-        UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session %u | Subscription %u | "
+        UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session " PRINTF_GUID_FORMAT " | Subscription %u | "
                      "MonitoredItem %u | Do not sample an unchanged value",
-                     monitoredItem->subscription->session->sessionId.identifier.numeric,
-                     monitoredItem->subscription->subscriptionID, monitoredItem->itemId);
+                     PRINTF_GUID_DATA(monitoredItem->subscription->session->sessionId), monitoredItem->subscription->subscriptionID, monitoredItem->itemId);
         return;
     }
 
-    UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session %u | Subscription %u | "
+    UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SERVER, "Session " PRINTF_GUID_FORMAT " | Subscription %u | "
                  "MonitoredItem %u | Sampling the value",
-                 monitoredItem->subscription->session->sessionId.identifier.numeric,
+                 PRINTF_GUID_DATA(monitoredItem->subscription->session->sessionId),
                  monitoredItem->subscription->subscriptionID, monitoredItem->itemId);
 
     /* do we have space in the queue? */

+ 19 - 13
src/ua_session.h

@@ -72,41 +72,47 @@ UA_Session_getUniqueSubscriptionID(UA_Session *session);
 /**
  * Log Helper
  * ---------- */
+
 #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, MSG, ...)                 \
-    UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_TRACE(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                  (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                  (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                 SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
+                 PRINTF_GUID_DATA(SESSION->sessionId), \
+                 ##__VA_ARGS__);
 
 #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, MSG, ...)                 \
-    UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_DEBUG(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                  (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                  (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                 SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
+                 PRINTF_GUID_DATA(SESSION->sessionId), \
+                 ##__VA_ARGS__);
 
 #define UA_LOG_INFO_SESSION(LOGGER, SESSION, MSG, ...)                  \
-    UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_INFO(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                  (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                  (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                 SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
+                 PRINTF_GUID_DATA(SESSION->sessionId), \
+                 ##__VA_ARGS__);
 
 #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, MSG, ...)               \
-    UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_WARNING(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                    (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                    (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                   SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
+                   PRINTF_GUID_DATA(SESSION->sessionId), \
+                   ##__VA_ARGS__);
 
 #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, MSG, ...)                 \
-    UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_ERROR(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                  (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                  (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                 SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
+                 PRINTF_GUID_DATA(SESSION->sessionId), \
+                 ##__VA_ARGS__);
 
 #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, MSG, ...)                 \
-    UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session %i | " MSG, \
+    UA_LOG_FATAL(LOGGER, UA_LOGCATEGORY_SESSION, "Connection %i | SecureChannel %i | Session " PRINTF_GUID_FORMAT " | " MSG, \
                  (SESSION->channel ? (SESSION->channel->connection ? SESSION->channel->connection->sockfd : 0) : 0), \
                  (SESSION->channel ? SESSION->channel->securityToken.channelId : 0), \
-                 SESSION->sessionId.identifier.numeric, ##__VA_ARGS__);
-
+                 PRINTF_GUID_DATA(SESSION->sessionId), \
+                 ##__VA_ARGS__);
 
 #endif /* UA_SESSION_H_ */