Procházet zdrojové kódy

Cleanup: Refactor out the multicast address base calculation

The multicast adddress base calculation was implemented three times in each
platform's networkconfig.c file. Create a common implementation in
ciptcpipinterface.c and use only this one.

Signed-off-by: Stefan Mätje <stefan.maetje@esd.eu>
Stefan Mätje před 6 roky
rodič
revize
9229d654d2

+ 23 - 1
source/src/cip/ciptcpipinterface.c

@@ -279,7 +279,7 @@ EipStatus GetAttributeSingleTcpIpInterface(
         &(g_tcpip.mcast_config.number_of_allocated_multicast_addresses),
         &message);
 
-      EipUint32 multicast_address = ntohl(
+      CipUdint multicast_address = ntohl(
         g_tcpip.mcast_config.starting_multicast_address);
 
       message_router_response->data_length += EncodeData(kCipUdint,
@@ -351,6 +351,28 @@ EipStatus GetAttributeAllTcpIpInterface(
   return kEipStatusOkSend;
 }
 
+/**
+ *  This function calculates the multicast base address to be used for CIP
+ *  connections from the current IP setting. The algorithm is implemented
+ *  according to CIP spec Volume 2,
+ *  section 3-5.3 "Multicast Address Allocation for EtherNet/IP"
+ */
+void CipTcpIpCalculateMulticastIp(CipTcpIpObject *p_tcpip)
+{
+  /* Multicast base address according to spec: 239.192.1.0 */
+  static const CipUdint cip_mcast_base_addr = 0xEFC00100;
+
+  /* Calculate the CIP multicast address. The multicast address is calculated, not input */
+  CipUdint host_id = ntohl(p_tcpip->interface_configuration.ip_address) &
+                    ~ntohl(p_tcpip->interface_configuration.network_mask);
+  host_id -= 1;
+  host_id &= 0x3ff;
+
+  g_tcpip.mcast_config.starting_multicast_address =
+    htonl(cip_mcast_base_addr + (host_id << 5));
+}
+
+
 EipUint16 GetEncapsulationInactivityTimeout(CipInstance *instance) {
   CipAttributeStruct *attribute = GetCipAttribute(instance, 13);
   OPENER_ASSERT(NULL != attribute)

+ 6 - 0
source/src/cip/ciptcpipinterface.h

@@ -63,6 +63,12 @@ EipStatus CipTcpIpInterfaceInit(void);
  */
 void ShutdownTcpIpInterface(void);
 
+/** @brief Calculate Multicast address base from current IP setting
+ *
+ *  @param  p_tcpip pointer to TCP/IP object
+ */
+void CipTcpIpCalculateMulticastIp(CipTcpIpObject *p_tcpip);
+
 /** @brief Public Method to get Encapsulation Inactivity Timeout Value
  *
  *

+ 3 - 7
source/src/ports/MINGW/networkconfig.c

@@ -65,13 +65,9 @@ EipStatus ConfigureNetworkInterface(const char *const network_interface) {
         g_tcpip.interface_configuration.gateway = inet_addr(
           pAdapter->GatewayList.IpAddress.String);
 
-        CipUdint host_id = ntohl(g_tcpip.interface_configuration.ip_address)
-                           & ~ntohl(g_tcpip.interface_configuration.network_mask);              /* see CIP spec 3-5.3 for multicast address algorithm*/
-        host_id -= 1;
-        host_id &= 0x3ff;
-
-        g_tcpip.mcast_config.starting_multicast_address = htonl(
-          ntohl(inet_addr("239.192.1.0") ) + (host_id << 5) );
+        /* Calculate the CIP multicast address. The multicast address is
+         * derived from the current IP address. */
+        CipTcpIpCalculateMulticastIp(&g_tcpip);
       }
       pAdapter = pAdapter->Next;
     }

+ 3 - 9
source/src/ports/POSIX/networkconfig.c

@@ -118,15 +118,9 @@ EipStatus ConfigureNetworkInterface(const char *const network_interface) {
     g_tcpip.interface_configuration.gateway = 0;
   }
 
-  /* calculate the CIP multicast address. The multicast address is calculated, not input*/
-  EipUint32 host_id = ntohl(g_tcpip.interface_configuration.ip_address) &
-                      ~ntohl(
-    g_tcpip.interface_configuration.network_mask);                                                                       /* see CIP spec 3-5.3 for multicast address algorithm*/
-  host_id -= 1;
-  host_id &= 0x3ff;
-
-  g_tcpip.mcast_config.starting_multicast_address =
-    htonl(ntohl(inet_addr("239.192.1.0") ) + (host_id << 5) );
+  /* Calculate the CIP multicast address. The multicast address is
+   * derived from the current IP address. */
+  CipTcpIpCalculateMulticastIp(&g_tcpip);
 
   fclose(file_handle);
 

+ 3 - 9
source/src/ports/WIN32/networkconfig.c

@@ -72,15 +72,9 @@ void ConfigureIpMacAddress(const CipUint interface_index) {
         inet_pton(AF_INET, pAdapter->GatewayList.IpAddress.String,
                   &g_tcpip.interface_configuration.gateway);
 
-        CipUdint host_id = ntohl(g_tcpip.interface_configuration.ip_address)
-                           & ~ntohl(g_tcpip.interface_configuration.network_mask);              /* see CIP spec 3-5.3 for multicast address algorithm*/
-        host_id -= 1;
-        host_id &= 0x3ff;
-
-        CipUdint multicast_base;
-        inet_pton(AF_INET, "239.192.1.0", &multicast_base);
-        g_tcpip.mcast_config.starting_multicast_address = htonl(
-          ntohl(multicast_base) + (host_id << 5) );
+        /* Calculate the CIP multicast address. The multicast address is
+         * derived from the current IP address. */
+        CipTcpIpCalculateMulticastIp(&g_tcpip);
       }
       pAdapter = pAdapter->Next;
     }