Kaynağa Gözat

Rename UA_Connection_completeMessages to UA_Connection_completeChunks;
Don't lookup the client hostname for a new connection

Julius Pfrommer 7 yıl önce
ebeveyn
işleme
b4f69cf923

+ 21 - 9
plugins/ua_network_tcp.c

@@ -234,10 +234,19 @@ ServerNetworkLayerTCP_freeConnection(UA_Connection *connection) {
 static UA_StatusCode
 ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd,
                           struct sockaddr_storage *remote) {
+    /* Set nonblocking */
+    socket_set_nonblocking(newsockfd);
+
+    /* Do not merge packets on the socket (disable Nagle's algorithm) */
+    int dummy = 1;
+    setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY,
+               (const char *)&dummy, sizeof(dummy));
+
     /* Get the peer name for logging */
     char remote_name[100];
-    if(getnameinfo((struct sockaddr*)remote, sizeof(struct sockaddr_storage), remote_name,
-                   sizeof(remote_name), NULL, 0, 0) == 0) {
+    int res = getnameinfo((struct sockaddr*)remote, sizeof(struct sockaddr_storage),
+                          remote_name, sizeof(remote_name), NULL, 0, NI_NUMERICHOST);
+    if(res == 0) {
         UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK,
                     "Connection %i | New connection over TCP from %s",
                     newsockfd, remote_name);
@@ -432,11 +441,10 @@ ServerNetworkLayerTCP_listen(UA_ServerNetworkLayer *nl, UA_Server *server,
 #endif
             continue;
 
-        socket_set_nonblocking(newsockfd);
-        /* Do not merge packets on the socket (disable Nagle's algorithm) */
-        int dummy = 1;
-        setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY,
-                   (const char *)&dummy, sizeof(dummy));
+        UA_LOG_TRACE(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK,
+                    "Connection %i | New connection over TCP on server socket %i",
+                    newsockfd, layer->serverSockets[i]);
+
         ServerNetworkLayerTCP_add(layer, (UA_Int32)newsockfd, &remote);
     }
 
@@ -447,6 +455,10 @@ ServerNetworkLayerTCP_listen(UA_ServerNetworkLayer *nl, UA_Server *server,
            !UA_fd_isset(e->connection.sockfd, &fdset))
           continue;
 
+        UA_LOG_TRACE(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK,
+                    "Connection %i | Activity on the socket",
+                    e->connection.sockfd);
+
         UA_ByteString buf = UA_BYTESTRING_NULL;
         UA_StatusCode retval = connection_recv(&e->connection, &buf, 0);
 
@@ -457,11 +469,11 @@ ServerNetworkLayerTCP_listen(UA_ServerNetworkLayer *nl, UA_Server *server,
             /* The socket is shutdown but not closed */
             if(e->connection.state != UA_CONNECTION_CLOSED) {
                 UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK,
-                            "Connection %d was closed by the client",
+                            "Connection %i | Closed by the client",
                             e->connection.sockfd);
             } else {
                 UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_NETWORK,
-                            "Connection %d was closed by the server",
+                            "Connection %i | Closed by the server",
                             e->connection.sockfd);
             }
             LIST_REMOVE(e, pointers);

+ 10 - 2
src/server/ua_server_binary.c

@@ -612,12 +612,17 @@ UA_Server_processSecureChannelMessage(UA_Server *server, UA_SecureChannel *chann
 static void
 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);
     UA_Boolean realloced = UA_FALSE;
-    UA_StatusCode retval = UA_Connection_completeMessages(connection, message, &realloced);
+    UA_StatusCode retval = UA_Connection_completeChunks(connection, message, &realloced);
 
     /* No failure, but no chunk ready */
-    if(message->length == 0)
+    if(message->length == 0) {
+        UA_LOG_TRACE(server->config.logger, UA_LOGCATEGORY_NETWORK,
+                     "Connection %i | Not a complete chunk yet.", connection->sockfd);
         return;
+    }
 
     /* Failed to complete a chunk */
     if(retval != UA_STATUSCODE_GOOD) {
@@ -642,6 +647,9 @@ processBinaryMessage(UA_Server *server, UA_Connection *connection,
                                  "resulted in error code %s", UA_StatusCode_name(retval));
     } else {
         /* Process messages without a channel and no chunking */
+        UA_LOG_TRACE(server->config.logger, UA_LOGCATEGORY_NETWORK,
+                     "Connection %i | No channel attached to the connection. "
+                     "Process the chunk directly", connection->sockfd);
         size_t offset = 0;
         UA_TcpMessageHeader tcpMessageHeader;
         retval = UA_TcpMessageHeader_decodeBinary(message, &offset, &tcpMessageHeader);

+ 4 - 3
src/ua_connection.c

@@ -122,8 +122,9 @@ separateIncompleteChunk(UA_Connection *connection, UA_ByteString * UA_RESTRICT m
 }
 
 UA_StatusCode
-UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
-                              UA_Boolean * UA_RESTRICT realloced) {
+UA_Connection_completeChunks(UA_Connection *connection,
+                             UA_ByteString * UA_RESTRICT message,
+                             UA_Boolean * UA_RESTRICT realloced) {
     /* If we have a stored an incomplete chunk, prefix to the received message.
      * After this block, connection->incompleteMessage is always empty. The
      * message and the buffer is released if allocating the memory fails. */
@@ -166,7 +167,7 @@ UA_Connection_receiveChunksBlocking(UA_Connection *connection, UA_ByteString *ch
         retval = connection->recv(connection, chunks, timeout);
 
         /* Get complete chunks and return */
-        retval |= UA_Connection_completeMessages(connection, chunks, realloced);
+        retval |= UA_Connection_completeChunks(connection, chunks, realloced);
         if(retval != UA_STATUSCODE_GOOD || chunks->length > 0)
             break;
 

+ 4 - 4
src/ua_connection_internal.h

@@ -15,21 +15,21 @@ extern "C" {
  * protocol. Furthermore, the networklayer may operate on ringbuffers or
  * statically assigned memory.
  *
- * If an entire message is received, it is forwarded directly. But the memory
+ * If an entire chunk is received, it is forwarded directly. But the memory
  * needs to be freed with the networklayer-specific mechanism. If a half message
  * is received, we copy it into a local buffer. Then, the stack-specific free
  * needs to be used.
  *
  * @param connection The connection
- * @param message The received message. The content may be overwritten when a
- *        previsouly received buffer is completed.
+ * @param message The received packet. The content may be replaced when a chunk
+ *        is completed with previously received packets.
  * @param realloced The Boolean value is set to true if the outgoing message has
  *        been reallocated from the network layer.
  * @return Returns UA_STATUSCODE_GOOD or an error code. When an error occurs,
  *         the ingoing message and the current buffer in the connection are
  *         freed. */
 UA_StatusCode
-UA_Connection_completeMessages(UA_Connection *connection,
+UA_Connection_completeChunks(UA_Connection *connection,
                                UA_ByteString * UA_RESTRICT message,
                                UA_Boolean * UA_RESTRICT realloced);
 

+ 1 - 1
tests/check_server_binary_messages.c

@@ -41,7 +41,7 @@ START_TEST(processMessage) {
     for(size_t i = 0; i < files; i++) {
         UA_ByteString msg = readFile(filenames[i]);
         UA_Boolean reallocated;
-        UA_StatusCode retval = UA_Connection_completeMessages(&c, &msg, &reallocated);
+        UA_StatusCode retval = UA_Connection_completeChunks(&c, &msg, &reallocated);
         if(retval == UA_STATUSCODE_GOOD && msg.length > 0)
             UA_Server_processBinaryMessage(server, &c, &msg);
         UA_ByteString_deleteMembers(&msg);

+ 1 - 1
tests/fuzz/fuzz_binary_message.cc

@@ -25,7 +25,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
 
     config.logger = UA_Log_Stdout;
     UA_Boolean reallocated = UA_FALSE;
-    UA_StatusCode retval = UA_Connection_completeMessages(&c, &msg, &reallocated);
+    UA_StatusCode retval = UA_Connection_completeChunks(&c, &msg, &reallocated);
     if(retval == UA_STATUSCODE_GOOD && msg.length > 0)
         UA_Server_processBinaryMessage(server, &c, &msg);
     UA_Server_delete(server);