Просмотр исходного кода

Fix: Add more checks to SetAttribute of TCP/IP interface configuration

The Conformance Test Tool requires also to test if the IP address and the
gateway address are neither the network address or the broadcast address
of the device's network interface when the SetAttribute service tries to
set the TCP/IP object's interface configuration.

Signed-off-by: Stefan Mätje <stefan.maetje@esd.eu>
Stefan Mätje 6 лет назад
Родитель
Сommit
7d68036670
1 измененных файлов с 27 добавлено и 5 удалено
  1. 27 5
      source/src/cip/ciptcpipinterface.c

+ 27 - 5
source/src/cip/ciptcpipinterface.c

@@ -216,6 +216,21 @@ static bool IsOnLoopbackNetwork(in_addr_t ip_addr)
   return (ip & IN_CLASSA_NET) == (INADDR_LOOPBACK & IN_CLASSA_NET);
 }
 
+/** Check if an IP address is either the network or the broadcast address
+ *
+ *  @param  ip_addr   IP address in network byte order
+ *  @param  net_mask  network mask in network byte order
+ *  @return           status
+ *
+ * Check if an IP address is either the network or the broadcast address.
+ *  In this case it is not a valid IP address for a host.
+ *  This check is endian agnostic.
+ */
+static bool IsNetworkOrBroadcastIp(in_addr_t ip_addr, in_addr_t net_mask) {
+  return  ((ip_addr & net_mask) == ip_addr) ||  /* is network address */
+          ((ip_addr | ~net_mask) == ip_addr);   /* is broadcast address */
+}
+
 /** Check the Interface configuration being valid according to EIP specification
  *
  * In Vol. 2 the "Table 5-4.3 Instance Attributes" provides some information
@@ -228,13 +243,14 @@ static bool IsOnLoopbackNetwork(in_addr_t ip_addr)
  *  - MASK: IP is a valid network mask
  *  -  ABC: IP is in class A, B or C
  *  - NLCL: IP is not localhost aka. INADDR_LOOPBACK
+ *  -   NB: IP is neither network or broadcast address (using network_mask)
  *
  * This is the table which checks are applied to what IP:
- *  N0 | MASK | ABC | NLCL | IP address
- *   + |   -  |  +  |   +  | ip_address
- *   - |   +  |  -  |   -  | network_mask
- *   - |   -  |  +  |   +  | gateway
- *   - |   -  |  +  |   -  | name_server / name_server_2
+ *  N0 | MASK | ABC | NLCL | NB | IP address
+ *   + |   -  |  +  |   +  |  + | ip_address
+ *   - |   +  |  -  |   -  |  - | network_mask
+ *   - |   -  |  +  |   +  |  + | gateway
+ *   - |   -  |  +  |   -  |  - | name_server / name_server_2
  * A configured gateway must be reachable according to the network mask.
  */
 static bool IsValidNetworkConfig(const CipTcpIpInterfaceConfiguration *if_cfg)
@@ -256,6 +272,12 @@ static bool IsValidNetworkConfig(const CipTcpIpInterfaceConfiguration *if_cfg)
       IsOnLoopbackNetwork(if_cfg->gateway)) {
     return false;
   }
+  /* Check NB */
+  if (IsNetworkOrBroadcastIp(if_cfg->ip_address, if_cfg->network_mask) ||
+      (INADDR_ANY != ntohl(if_cfg->gateway) &&
+       IsNetworkOrBroadcastIp(if_cfg->gateway, if_cfg->network_mask))) {
+    return false;
+  }
   if (INADDR_ANY != ntohl(if_cfg->gateway) &&
       INADDR_ANY != ntohl(if_cfg->network_mask)) {
     /* gateway is configured. Check if it is reachable. */