Forráskód Böngészése

moved tcp handling connection request in own function on order to make main network handling code more easier to understand,
fixed issue in different port handling between list identity and cpf data encapsulation

Alois Zoitl 14 éve
szülő
commit
38b67191c4

+ 1 - 1
src/enet_encap/cpf.c

@@ -344,7 +344,7 @@ assembleLinearMsg(S_CIP_MR_Response * pa_MRResponse,
           htols(pa_CPFDataItem->AddrInfo[j].TypeID, &pa_msg);
           htols(pa_CPFDataItem->AddrInfo[j].Length, &pa_msg);
 
-          encapsulateIPAdress(pa_CPFDataItem->AddrInfo[j].nsin_port,
+          encapsulateIPAdressCPF(pa_CPFDataItem->AddrInfo[j].nsin_port,
               pa_CPFDataItem->AddrInfo[j].nsin_addr, pa_msg);
           pa_msg += 8;
 

+ 2 - 1
src/enet_encap/encap.c

@@ -300,6 +300,7 @@ handleReceivedListIdentityCmd(int pa_nSocket,
       if (-1 == g_stDelayedEncapsulationMessages[i].m_nSocket)
         {
           pstDelayedMessageBuffer = &(g_stDelayedEncapsulationMessages[i]);
+          break;
         }
     }
 
@@ -371,7 +372,7 @@ determineDelayTime(EIP_BYTE *pa_acBufferStart,
     }
 
   pa_pstDelayedMessageBuffer->m_unTimeOut = (unMaxDelayTime * rand())
-      / RAND_MAX;
+      / RAND_MAX + 1;
 }
 
 /*   void RegisterSession(struct S_Encapsulation_Data *pa_S_ReceiveData)

+ 30 - 0
src/enet_encap/endianconv.c

@@ -105,7 +105,37 @@ void
 encapsulateIPAdress(EIP_UINT16 pa_unPort, EIP_UINT32 pa_unAddr,
     EIP_BYTE *pa_acCommBuf)
 {
+  if (OPENER_LITTLE_ENDIAN_PLATFORM == g_nOpENerPlatformEndianess)
+    {
+      htols(htons(AF_INET), &pa_acCommBuf);
+      htols(htons(pa_unPort), &pa_acCommBuf);
+      htoll(pa_unAddr, &pa_acCommBuf);
+
+    }
+  else
+    {
+      if (OPENER_BIG_ENDIAN_PLATFORM == g_nOpENerPlatformEndianess)
+        {
+          pa_acCommBuf[0] = (unsigned char) (AF_INET >> 8);
+          pa_acCommBuf[1] = (unsigned char) AF_INET;
+          pa_acCommBuf += 2;
+
+          pa_acCommBuf[0] = (unsigned char) (pa_unPort >> 8);
+          pa_acCommBuf[1] = (unsigned char) pa_unPort;
+          pa_acCommBuf += 2;
+
+          pa_acCommBuf[3] = (unsigned char) pa_unAddr;
+          pa_acCommBuf[2] = (unsigned char) (pa_unAddr >> 8);
+          pa_acCommBuf[1] = (unsigned char) (pa_unAddr >> 16);
+          pa_acCommBuf[0] = (unsigned char) (pa_unAddr >> 24);
+        }
+    }
+}
 
+void
+encapsulateIPAdressCPF(EIP_UINT16 pa_unPort, EIP_UINT32 pa_unAddr,
+    EIP_BYTE *pa_acCommBuf)
+{
   if (OPENER_LITTLE_ENDIAN_PLATFORM == g_nOpENerPlatformEndianess)
     {
       htols(htons(AF_INET), &pa_acCommBuf);

+ 7 - 0
src/enet_encap/endianconv.h

@@ -53,6 +53,13 @@ void
 encapsulateIPAdress(EIP_UINT16 pa_unPort, EIP_UINT32 pa_unAddr,
     EIP_BYTE *pa_acCommBuf);
 
+/*!\brief Encapsulate the sockaddr information as necessary for the CPF data items
+ */
+void
+encapsulateIPAdressCPF(EIP_UINT16 pa_unPort, EIP_UINT32 pa_unAddr,
+    EIP_BYTE *pa_acCommBuf);
+
+
 
 /** Identify if we are running on a big or little endian system and set
  * variable.

+ 40 - 20
src/ports/platform-pc/networkhandler.c

@@ -42,9 +42,22 @@ int g_nCurrentActiveTCPSocket;
 static struct timeval tv;
 static MILLISECONDS actualtime, lasttime;
 
+/*!\brief handle any connection request coming in the TCP server socket.
+ *
+ */
+void
+handleTCPConnectionRequest();
+
+
+/*!
+ *
+ */
 EIP_STATUS
 handleDataOnTCPSocket(int pa_nSocket);
 
+
+
+
 static MILLISECONDS
 getmilliseconds(void)
 {
@@ -206,15 +219,13 @@ NetworkHandler_ProcessOnce(void)
   int nRemainingBytes;
   int replylen;
   S_CIP_ConnectionObject *pstConnection;
-  struct sockaddr_in remote_addr;
 
 #ifndef WIN32
-  socklen_t addrlen;
   socklen_t fromlen;
 #else
   unsigned long fromlen;
-  unsigned long addrlen
 #endif
+
   read_fds = master;
   fromlen = sizeof(from);
 
@@ -255,23 +266,7 @@ NetworkHandler_ProcessOnce(void)
             /* see if this is a connection request to the TCP listener*/
             if (fd == TheNetworkStatus.nTCPListener) /* handle new TCP connection */
               {
-                OPENER_TRACE_INFO("networkhandler: new TCP connection\n");
-                addrlen = sizeof(remote_addr);
-                newfd = accept(TheNetworkStatus.nTCPListener,
-                    (struct sockaddr *) &remote_addr, &addrlen); /* remote_addr does not seem to be used*/
-                if (newfd == -1)
-                  {
-                    OPENER_TRACE_ERR("networkhandler: error on accept: %s\n", strerror(errno));
-                    continue;
-                  }
-
-                FD_SET(newfd, &master);
-                /* add newfd to master set */
-                if (newfd > fdmax)
-                  fdmax = newfd;
-                OPENER_TRACE_STATE(
-                    "networkhandler: opened new TCP connection on fd %d\n",
-                    newfd);
+                handleTCPConnectionRequest();
                 continue;
               }
 
@@ -354,6 +349,7 @@ NetworkHandler_ProcessOnce(void)
             /* if not registered UDP, handle as a TCP receive */
             if (EIP_ERROR == handleDataOnTCPSocket(fd)) /* if error */
               {
+                IApp_CloseSocket(fd);
                 closeSession(fd); /* clean up session and close the socket */
               }
           }
@@ -610,3 +606,27 @@ IApp_CloseSocket(int pa_nSockFd)
 #endif
     }
 }
+
+void
+handleTCPConnectionRequest()
+{
+  OPENER_TRACE_INFO("networkhandler: new TCP connection\n");
+
+  newfd = accept(TheNetworkStatus.nTCPListener, NULL, NULL);
+  if (newfd == -1)
+    {
+      OPENER_TRACE_ERR("networkhandler: error on accept: %s\n", strerror(errno));
+      return;
+    }
+
+  FD_SET(newfd, &master);
+  /* add newfd to master set */
+  if (newfd > fdmax)
+    {
+      fdmax = newfd;
+    }
+
+  OPENER_TRACE_STATE(
+      "networkhandler: opened new TCP connection on fd %d\n",
+      newfd);
+}