Ver Fonte

Merge pull request #333 from EIPStackGroup/Claroty_ParseConnectionPath_hangs

Fixes Claroty repors for CPF stack overflows and parse hangs
Martin Melik-Merkumians há 4 anos atrás
pai
commit
8eaf3ff4fa

+ 90 - 90
source/src/cip/cipcommon.c

@@ -82,20 +82,20 @@ EipStatus NotifyClass(const CipClass *RESTRICT const cip_class,
 
   /* find the instance: if instNr==0, the class is addressed, else find the instance */
   EipUint16 instance_number =
-    message_router_request->request_path.instance_number;                     /* get the instance number */
-  CipInstance *instance = GetCipInstance(cip_class, instance_number);       /* look up the instance (note that if inst==0 this will be the class itself) */
-  if (instance)       /* if instance is found */
+    message_router_request->request_path.instance_number;                           /* get the instance number */
+  CipInstance *instance = GetCipInstance(cip_class, instance_number); /* look up the instance (note that if inst==0 this will be the class itself) */
+  if(instance) /* if instance is found */
   {
     OPENER_TRACE_INFO("notify: found instance %d%s\n",
                       instance_number,
                       instance_number == 0 ? " (class object)" : "");
 
-    CipServiceStruct *service = instance->cip_class->services;         /* get pointer to array of services */
-    if (NULL != service)             /* if services are defined */
+    CipServiceStruct *service = instance->cip_class->services; /* get pointer to array of services */
+    if(NULL != service) /* if services are defined */
     {
-      for (size_t i = 0; i < instance->cip_class->number_of_services; i++)                   /* seach the services list */
+      for(size_t i = 0; i < instance->cip_class->number_of_services; i++) /* seach the services list */
       {
-        if (message_router_request->service == service->service_number)                         /* if match is found */
+        if(message_router_request->service == service->service_number) /* if match is found */
         {
           /* call the service, and return what it returns */
           OPENER_TRACE_INFO("notify: calling %s service\n", service->name);
@@ -109,23 +109,21 @@ EipStatus NotifyClass(const CipClass *RESTRICT const cip_class,
           service++;
         }
       }
-    }
-    OPENER_TRACE_WARN(
+    } OPENER_TRACE_WARN(
       "notify: service 0x%x not supported\n", message_router_request->service);
-    message_router_response->general_status = kCipErrorServiceNotSupported;                                                                                    /* if no services or service not found, return an error reply*/
+    message_router_response->general_status = kCipErrorServiceNotSupported; /* if no services or service not found, return an error reply*/
   } else {
     OPENER_TRACE_WARN("notify: instance number %d unknown\n", instance_number);
-/* if instance not found, return an error reply */
-    message_router_response->general_status =
-      kCipErrorPathDestinationUnknown;
+    /* if instance not found, return an error reply */
+    message_router_response->general_status = kCipErrorPathDestinationUnknown;
     /* according to the test tool this is the correct error flag instead of CIP_ERROR_OBJECT_DOES_NOT_EXIST */
   }
 
   /* handle error replies*/
-  message_router_response->size_of_additional_status = 0;       /* fill in the rest of the reply with not much of anything*/
+  message_router_response->size_of_additional_status = 0; /* fill in the rest of the reply with not much of anything*/
   InitializeENIPMessage(&message_router_response->message);
-  message_router_response->reply_service = (0x80
-                                            | message_router_request->service); /* except the reply code is an echo of the command + the reply flag */
+  message_router_response->reply_service =
+    (0x80 | message_router_request->service);                                        /* except the reply code is an echo of the command + the reply flag */
 
   return kEipStatusOkSend;
 }
@@ -134,51 +132,53 @@ CipInstance *AddCipInstances(CipClass *RESTRICT const cip_class,
                              const int number_of_instances) {
   CipInstance **next_instance = NULL;
   CipInstance *first_instance = NULL; /* Initialize to error result */
-  EipUint32 instance_number = 1;      /* the first instance is number 1 */
-  int new_instances   = 0;
+  EipUint32 instance_number = 1; /* the first instance is number 1 */
+  int new_instances = 0;
 
   OPENER_TRACE_INFO("adding %d instances to class %s\n",
                     number_of_instances,
                     cip_class->class_name);
 
-  next_instance = &cip_class->instances;     /* get address of pointer to head of chain */
-  while (*next_instance)       /* as long as what pp points to is not zero */
+  next_instance = &cip_class->instances; /* get address of pointer to head of chain */
+  while(*next_instance) /* as long as what pp points to is not zero */
   {
-    next_instance = &(*next_instance)->next;              /* follow the chain until pp points to pointer that contains a zero */
-    instance_number++;                                    /* keep track of what the first new instance number will be */
+    next_instance = &(*next_instance)->next; /* follow the chain until pp points to pointer that contains a zero */
+    instance_number++; /* keep track of what the first new instance number will be */
   }
 
   /* Allocate and initialize all needed instances one by one. */
-  for (new_instances = 0; new_instances < number_of_instances; new_instances++)
-  {
+  for(new_instances = 0; new_instances < number_of_instances; new_instances++) {
     CipInstance *current_instance =
       (CipInstance *) CipCalloc(1, sizeof(CipInstance) );
     OPENER_ASSERT(NULL != current_instance); /* fail if run out of memory */
-    if (NULL == current_instance) {break;}
-    if (NULL == first_instance)
-    {
-      first_instance = current_instance;                  /* remember the first allocated instance */
+    if(NULL == current_instance) {
+      break;
+    }
+    if(NULL == first_instance) {
+      first_instance = current_instance; /* remember the first allocated instance */
     }
 
-    current_instance->instance_number = instance_number;  /* assign the next sequential instance number */
-    current_instance->cip_class = cip_class;              /* point each instance to its class */
+    current_instance->instance_number = instance_number; /* assign the next sequential instance number */
+    current_instance->cip_class = cip_class; /* point each instance to its class */
 
-    if (cip_class->number_of_attributes)                  /* if the class calls for instance attributes */
-    {                                                     /* then allocate storage for the attribute array */
+    if(cip_class->number_of_attributes) /* if the class calls for instance attributes */
+    { /* then allocate storage for the attribute array */
       current_instance->attributes = (CipAttributeStruct *) CipCalloc(
-        cip_class->number_of_attributes, sizeof(CipAttributeStruct) );
+        cip_class->number_of_attributes,
+        sizeof(CipAttributeStruct) );
       OPENER_ASSERT(NULL != current_instance->attributes);/* fail if run out of memory */
-      if (NULL == current_instance->attributes) {break;}
+      if(NULL == current_instance->attributes) {
+        break;
+      }
     }
 
-    *next_instance = current_instance;        /* link the previous pointer to this new node */
-    next_instance = &current_instance->next;  /* update pp to point to the next link of the current node */
-    cip_class->number_of_instances += 1;      /* update the total number of instances recorded by the class */
-    instance_number++;                        /* update to the number of the next node*/
+    *next_instance = current_instance; /* link the previous pointer to this new node */
+    next_instance = &current_instance->next; /* update pp to point to the next link of the current node */
+    cip_class->number_of_instances += 1; /* update the total number of instances recorded by the class */
+    instance_number++; /* update to the number of the next node*/
   }
 
-  if (new_instances != number_of_instances)
-  {
+  if(new_instances != number_of_instances) {
     /* TODO: Free again all attributes and instances allocated so far in this call. */
     OPENER_TRACE_ERR(
       "ERROR: Allocated only %d instances of requested %d for class %s\n",
@@ -194,7 +194,7 @@ CipInstance *AddCipInstance(CipClass *RESTRICT const cip_class,
                             const EipUint32 instance_id) {
   CipInstance *instance = GetCipInstance(cip_class, instance_id);
 
-  if (NULL == instance) { /*we have no instance with given id*/
+  if(NULL == instance) { /*we have no instance with given id*/
     instance = AddCipInstances(cip_class, 1);
     instance->instance_number = instance_id;
   }
@@ -209,7 +209,7 @@ CipClass *CreateCipClass(const CipUdint class_code,
                          const EipUint32 highest_instance_attribute_number,
                          const int number_of_instance_services,
                          const int number_of_instances,
-                         char *name,
+                         const char *const name,
                          const EipUint16 revision,
                          InitializeCipClass initializer) {
 
@@ -217,18 +217,18 @@ CipClass *CreateCipClass(const CipUdint class_code,
                     class_code);
 
   OPENER_ASSERT(NULL == GetCipClass(class_code) ); /* check if an class with the ClassID already exists */
-/* should never try to redefine a class*/
+  /* should never try to redefine a class*/
 
-/* a metaClass is a class that holds the class attributes and services
-   CIP can talk to an instance, therefore an instance has a pointer to its class
-   CIP can talk to a class, therefore a class struct is a subclass of the instance struct,
-   and contains a pointer to a metaclass
-   CIP never explicitly addresses a metaclass*/
+  /* a metaClass is a class that holds the class attributes and services
+     CIP can talk to an instance, therefore an instance has a pointer to its class
+     CIP can talk to a class, therefore a class struct is a subclass of the instance struct,
+     and contains a pointer to a metaclass
+     CIP never explicitly addresses a metaclass*/
 
   CipClass *const cip_class = (CipClass *) CipCalloc(1, sizeof(CipClass) ); /* create the class object*/
   CipClass *const meta_class = (CipClass *) CipCalloc(1, sizeof(CipClass) ); /* create the metaclass object*/
 
-/* initialize the class-specific fields of the Class struct*/
+  /* initialize the class-specific fields of the Class struct*/
   cip_class->class_code = class_code; /* the class remembers the class ID */
   cip_class->revision = revision; /* the class remembers the class ID */
   cip_class->number_of_instances = 0; /* the number of instances initially zero (more created below) */
@@ -247,7 +247,7 @@ CipClass *CreateCipClass(const CipUdint class_code,
   meta_class->class_name = (char *) CipCalloc(1, strlen(name) + 6); /* fabricate the name "meta<classname>"*/
   snprintf(meta_class->class_name, strlen(name) + 6, "meta-%s", name);
 
-/* initialize the instance-specific fields of the Class struct*/
+  /* initialize the instance-specific fields of the Class struct*/
   cip_class->class_instance.instance_number = 0; /* the class object is instance zero of the class it describes (weird, but that's the spec)*/
   cip_class->class_instance.attributes = 0; /* this will later point to the class attibutes*/
   cip_class->class_instance.cip_class = meta_class; /* the class's class is the metaclass (like SmallTalk)*/
@@ -258,12 +258,12 @@ CipClass *CreateCipClass(const CipUdint class_code,
   meta_class->class_instance.cip_class = NULL; /* the metaclass has no class*/
   meta_class->class_instance.next = NULL; /* the next link will always be zero, since there is only one instance of any particular metaclass object*/
 
-/* further initialization of the class object*/
+  /* further initialization of the class object*/
 
   cip_class->class_instance.attributes = (CipAttributeStruct *) CipCalloc(
     meta_class->number_of_attributes,
     sizeof(CipAttributeStruct) );
-/* TODO -- check that we didn't run out of memory?*/
+  /* TODO -- check that we didn't run out of memory?*/
 
   meta_class->services = (CipServiceStruct *) CipCalloc(
     meta_class->number_of_services,
@@ -286,29 +286,30 @@ CipClass *CreateCipClass(const CipUdint class_code,
 
   if(NULL == initializer) {
     InsertAttribute( (CipInstance *) cip_class, 1, kCipUint, EncodeCipUint,
-                     (void *) &cip_class->revision, kGetableSingleAndAll );                                                /* revision */
+                     (void *) &cip_class->revision, kGetableSingleAndAll );                                                    /* revision */
     InsertAttribute( (CipInstance *) cip_class, 2, kCipUint, EncodeCipUint,
                      (void *) &cip_class->number_of_instances,
-                     kGetableSingleAndAll );                                                                                          /* #2 Max instance no. */
+                     kGetableSingleAndAll );                                                                                              /* #2 Max instance no. */
     InsertAttribute( (CipInstance *) cip_class, 3, kCipUint, EncodeCipUint,
                      (void *) &cip_class->number_of_instances,
-                     kGetableSingleAndAll );                                                                                          /* number of instances currently existing*/
+                     kGetableSingleAndAll );                                                                                              /* number of instances currently existing*/
     InsertAttribute( (CipInstance *) cip_class, 4, kCipUint, EncodeCipUint,
-                     (void *) &kCipUintZero, kGetableAllDummy );                                                /* optional attribute list - default = 0 */
+                     (void *) &kCipUintZero, kGetableAllDummy );                                                    /* optional attribute list - default = 0 */
     InsertAttribute( (CipInstance *) cip_class, 5, kCipUint, EncodeCipUint,
-                     (void *) &kCipUintZero, kNotSetOrGetable );                                                /* optional service list - default = 0 */
+                     (void *) &kCipUintZero, kNotSetOrGetable );                                                    /* optional service list - default = 0 */
     InsertAttribute( (CipInstance *) cip_class, 6, kCipUint, EncodeCipUint,
                      (void *) &meta_class->highest_attribute_number,
-                     kGetableSingle );                                                                                                /* max class attribute number*/
+                     kGetableSingle );                                                                                                    /* max class attribute number*/
     InsertAttribute( (CipInstance *) cip_class, 7, kCipUint, EncodeCipUint,
                      (void *) &cip_class->highest_attribute_number,
-                     kGetableSingle );                                                                                               /* max instance attribute number*/
+                     kGetableSingle );                                                                                                   /* max instance attribute number*/
+
     if(number_of_class_services > 0) {
       if(number_of_class_services > 1) { /*only if the mask has values add the get_attribute_all service */
         InsertService(meta_class,
                       kGetAttributeAll,
                       &GetAttributeAll,
-                      "GetAttributeAll");                                             /* bind instance services to the metaclass*/
+                      "GetAttributeAll");                                                 /* bind instance services to the metaclass*/
       }
       InsertService(meta_class,
                     kGetAttributeSingle,
@@ -319,7 +320,7 @@ CipClass *CreateCipClass(const CipUdint class_code,
     initializer(cip_class);
   }
 
-/* create the standard class services*/
+  /* create the standard class services*/
   return cip_class;
 }
 
@@ -336,7 +337,7 @@ void InsertAttribute(CipInstance *const instance,
   CipClass *cip_class = instance->cip_class;
 
   OPENER_ASSERT(NULL != attribute);
-/* adding a attribute to a class that was not declared to have any attributes is not allowed */
+  /* adding a attribute to a class that was not declared to have any attributes is not allowed */
   for(int i = 0; i < instance->cip_class->number_of_attributes; i++) {
     if(attribute->data == NULL) { /* found non set attribute */
       attribute->attribute_number = attribute_number;
@@ -366,7 +367,7 @@ void InsertAttribute(CipInstance *const instance,
     cip_class->class_name,
     instance->instance_number);
   OPENER_ASSERT(false);
-/* trying to insert too many attributes*/
+  /* trying to insert too many attributes*/
 }
 
 void InsertService(const CipClass *const cip_class,
@@ -379,11 +380,11 @@ void InsertService(const CipClass *const cip_class,
                     cip_class->class_name, cip_class->number_of_services,
                     service_number);
   OPENER_ASSERT(service != NULL);
-/* adding a service to a class that was not declared to have services is not allowed*/
+  /* adding a service to a class that was not declared to have services is not allowed*/
   for(int i = 0; i < cip_class->number_of_services; i++) /* Iterate over all service slots attached to the class */
   {
     if(service->service_number == service_number ||
-       service->service_function == NULL)                                          /* found undefined service slot*/
+       service->service_function == NULL)                                              /* found undefined service slot*/
     {
       service->service_number = service_number; /* fill in service number*/
       service->service_function = service_function; /* fill in function address*/
@@ -393,7 +394,7 @@ void InsertService(const CipClass *const cip_class,
     ++service;
   }
   OPENER_ASSERT(false);
-/* adding more services than were declared is a no-no*/
+  /* adding more services than were declared is a no-no*/
 }
 
 void InsertGetSetCallback(CipClass *const cip_class,
@@ -408,8 +409,8 @@ void InsertGetSetCallback(CipClass *const cip_class,
   if(0 != (kPreSetFunc & callbacks_to_install) ) {
     cip_class->PreSetCallback = callback_function;
   }
-/* The PostSetCallback is used for both, the after set action and the storage
- * of non volatile data. Therefore check for both flags set. */
+  /* The PostSetCallback is used for both, the after set action and the storage
+   * of non volatile data. Therefore check for both flags set. */
   if(0 != ( (kPostSetFunc | kNvDataFunc) & callbacks_to_install ) ) {
     cip_class->PostSetCallback = callback_function;
   }
@@ -459,30 +460,29 @@ EipStatus GetAttributeSingle(CipInstance *RESTRICT const instance,
   EipUint16 attribute_number =
     message_router_request->request_path.attribute_number;
 
-  if ( (NULL != attribute) && (NULL != attribute->data) ) {
+  if( (NULL != attribute) && (NULL != attribute->data) ) {
     uint8_t get_bit_mask =
-      (instance->cip_class->get_single_bit_mask[CalculateIndex(
-                                                  attribute_number)
+      (instance->cip_class->get_single_bit_mask[CalculateIndex(attribute_number)
        ]);
-    if (0 != (get_bit_mask & (1 << (attribute_number % 8) ) ) ) {
+    if(0 != (get_bit_mask & (1 << (attribute_number % 8) ) ) ) {
       OPENER_TRACE_INFO("getAttribute %d\n",
-                        message_router_request->request_path.attribute_number);                      /* create a reply message containing the data*/
+                        message_router_request->request_path.attribute_number); /* create a reply message containing the data*/
 
-/* Call the PreGetCallback if enabled for this attribute and the class provides one. */
-      if ( (attribute->attribute_flags & kPreGetFunc) &&
-           NULL != instance->cip_class->PreGetCallback ) {
+      /* Call the PreGetCallback if enabled for this attribute and the class provides one. */
+      if( (attribute->attribute_flags & kPreGetFunc) &&
+          NULL != instance->cip_class->PreGetCallback ) {
         instance->cip_class->PreGetCallback(instance,
                                             attribute,
                                             message_router_request->service);
       }
 
-      OPENER_ASSERT(NULL != attribute); attribute->encode(attribute->data,
-                                                          &message_router_response->message);
+      OPENER_ASSERT(NULL != attribute);
+      attribute->encode(attribute->data, &message_router_response->message);
       message_router_response->general_status = kCipErrorSuccess;
 
-/* Call the PostGetCallback if enabled for this attribute and the class provides one. */
-      if ( (attribute->attribute_flags & kPostGetFunc) &&
-           NULL != instance->cip_class->PostGetCallback ) {
+      /* Call the PostGetCallback if enabled for this attribute and the class provides one. */
+      if( (attribute->attribute_flags & kPostGetFunc) &&
+          NULL != instance->cip_class->PostGetCallback ) {
         instance->cip_class->PostGetCallback(instance,
                                              attribute,
                                              message_router_request->service);
@@ -594,7 +594,7 @@ void EncodeCipString(const CipString *const data,
     outgoing_message->used_message_length += string->length;
 
     if(outgoing_message->used_message_length & 0x01) {
-/* we have an odd byte count */
+      /* we have an odd byte count */
       AddSintToMessage(0, outgoing_message);
     }
   }
@@ -681,7 +681,7 @@ int DecodeData(const EipUint8 cip_data_type,
   int number_of_decoded_bytes = -1;
 
   switch(cip_data_type)
-/* check the data type of attribute */
+  /* check the data type of attribute */
   {
     case (kCipBool):
     case (kCipSint):
@@ -723,7 +723,7 @@ int DecodeData(const EipUint8 cip_data_type,
 
       number_of_decoded_bytes = string->length + 2; /* we have a two byte length field */
       if(number_of_decoded_bytes & 0x01) {
-/* we have an odd byte count */
+        /* we have an odd byte count */
         ++(*cip_message);
         number_of_decoded_bytes++;
       }
@@ -774,7 +774,7 @@ EipStatus GetAttributeAll(CipInstance *instance,
   //Missing header
 
   if(0 == instance->cip_class->number_of_attributes) {
-/*there are no attributes to be sent back*/
+    /*there are no attributes to be sent back*/
     message_router_response->reply_service =
       (0x80 | message_router_request->service);
     message_router_response->general_status = kCipErrorServiceNotSupported;
@@ -784,11 +784,11 @@ EipStatus GetAttributeAll(CipInstance *instance,
                                      message_router_response);
     message_router_response->general_status = kCipErrorSuccess;
     for(size_t j = 0; j < instance->cip_class->number_of_attributes; j++) {
-/* for each instance attribute of this class */
+      /* for each instance attribute of this class */
       EipUint16 attribute_number = attribute->attribute_number;
       if( (instance->cip_class->get_all_bit_mask[CalculateIndex(attribute_number)
            ]) & (1 << (attribute_number % 8) ) ) {
-/* only return attributes that are flagged as being part of GetAttributeAll */
+        /* only return attributes that are flagged as being part of GetAttributeAll */
         message_router_request->request_path.attribute_number =
           attribute_number;
 
@@ -844,7 +844,7 @@ void EncodeEPath(CipEpath *epath,
   }
 
   OPENER_ASSERT(
-    2 + epath->path_size * 2 == message->used_message_length - start_length);           /* path size is in 16 bit chunks according to the specification */
+    2 + epath->path_size * 2 == message->used_message_length - start_length);             /* path size is in 16 bit chunks according to the specification */
 }
 
 int DecodePaddedEPath(CipEpath *epath,
@@ -854,14 +854,14 @@ int DecodePaddedEPath(CipEpath *epath,
 
   epath->path_size = *message_runner;
   message_runner++;
-/* copy path to structure, in version 0.1 only 8 bit for Class,Instance and Attribute, need to be replaced with function */
+  /* copy path to structure, in version 0.1 only 8 bit for Class,Instance and Attribute, need to be replaced with function */
   epath->class_id = 0;
   epath->instance_number = 0;
   epath->attribute_number = 0;
 
   while(number_of_decoded_elements < epath->path_size) {
     if(kSegmentTypeReserved == ( (*message_runner) & kSegmentTypeReserved ) ) {
-/* If invalid/reserved segment type, segment type greater than 0xE0 */
+      /* If invalid/reserved segment type, segment type greater than 0xE0 */
       return kEipStatusError;
     }
 

+ 8 - 8
source/src/cip/cipconnectionmanager.c

@@ -1096,6 +1096,8 @@ EipUint8 ParseConnectionPath(CipConnectionObject *connection_object,
   const EipUint8 *message = message_router_request->data;
   const size_t connection_path_size = GetUsintFromMessage(&message); /* length in words */
   size_t remaining_path = connection_path_size;
+  OPENER_TRACE_INFO("Received connection path size: %zu \n",
+                    connection_path_size);
   CipClass *class = NULL;
 
   CipDword class_id = 0x0;
@@ -1368,7 +1370,7 @@ EipUint8 ParseConnectionPath(CipConnectionObject *connection_object,
                 break;
               default:
                 OPENER_TRACE_ERR("Not allowed in connection manager");
-                break;
+                return kCipErrorPathSegmentError;
             }
           }
           break;
@@ -1391,7 +1393,7 @@ EipUint8 ParseConnectionPath(CipConnectionObject *connection_object,
                 break;
               default:
                 OPENER_TRACE_ERR("Not allowed in connection manager");
-                break;
+                return kCipErrorPathSegmentError;
             }
           }
           break;
@@ -1416,16 +1418,14 @@ EipUint8 ParseConnectionPath(CipConnectionObject *connection_object,
 
 void CloseConnection(CipConnectionObject *RESTRICT connection_object) {
 
-  if ( kConnectionObjectTransportClassTriggerTransportClass3 !=
-       ConnectionObjectGetTransportClassTriggerTransportClass(connection_object) )
+  if(kConnectionObjectTransportClassTriggerTransportClass3 !=
+     ConnectionObjectGetTransportClassTriggerTransportClass(connection_object) )
   {
     /* only close the UDP connection for not class 3 connections */
-    CloseUdpSocket(
-      connection_object->socket[kUdpCommuncationDirectionConsuming]);
+    CloseUdpSocket(connection_object->socket[kUdpCommuncationDirectionConsuming]);
     connection_object->socket[kUdpCommuncationDirectionConsuming] =
       kEipInvalidSocket;
-    CloseUdpSocket(
-      connection_object->socket[kUdpCommuncationDirectionProducing]);
+    CloseUdpSocket(connection_object->socket[kUdpCommuncationDirectionProducing]);
     connection_object->socket[kUdpCommuncationDirectionProducing] =
       kEipInvalidSocket;
   }

+ 293 - 123
source/src/enet_encap/cpf.c

@@ -24,39 +24,58 @@ const size_t kSequencedAddressItemLength = 8;
 
 CipCommonPacketFormatData g_common_packet_format_data_item; /**< CPF global data items */
 
-static void InitializeMessageRouterResponse(CipMessageRouterResponse *const message_router_response) {
-  memset(message_router_response, 0, sizeof(*message_router_response));
+static void InitializeMessageRouterResponse(
+  CipMessageRouterResponse *const message_router_response) {
+  memset(message_router_response, 0, sizeof(*message_router_response) );
   InitializeENIPMessage(&message_router_response->message);
 }
 
-EipStatus NotifyCommonPacketFormat(const EncapsulationData *const received_data, const struct sockaddr *const originator_address,
-    ENIPMessage *const outgoing_message) {
+EipStatus NotifyCommonPacketFormat(const EncapsulationData *const received_data,
+                                   const struct sockaddr *const originator_address,
+                                   ENIPMessage *const outgoing_message) {
   EipStatus return_value = kEipStatusError;
   CipMessageRouterResponse message_router_response;
   InitializeMessageRouterResponse(&message_router_response);
 
   if(kEipStatusError
-    == (return_value = CreateCommonPacketFormatStructure(received_data->current_communication_buffer_position, received_data->data_length,
-      &g_common_packet_format_data_item))) {
+     == (return_value =
+           CreateCommonPacketFormatStructure(received_data->
+                                             current_communication_buffer_position,
+                                             received_data->data_length,
+                                             &
+                                             g_common_packet_format_data_item) ) )
+  {
     OPENER_TRACE_ERR("notifyCPF: error from createCPFstructure\n");
   } else {
     return_value = kEipStatusOkSend; /* In cases of errors we normally need to send an error response */
-    if(g_common_packet_format_data_item.address_item.type_id == kCipItemIdNullAddress) /* check if NullAddressItem received, otherwise it is no unconnected message and should not be here*/
+    if(g_common_packet_format_data_item.address_item.type_id ==
+       kCipItemIdNullAddress)                                                          /* check if NullAddressItem received, otherwise it is no unconnected message and should not be here*/
     { /* found null address item*/
-      if(g_common_packet_format_data_item.data_item.type_id == kCipItemIdUnconnectedDataItem) { /* unconnected data item received*/
-        return_value = NotifyMessageRouter(g_common_packet_format_data_item.data_item.data, g_common_packet_format_data_item.data_item.length,
-          &message_router_response, originator_address, received_data->session_handle);
+      if(g_common_packet_format_data_item.data_item.type_id ==
+         kCipItemIdUnconnectedDataItem) {                                                       /* unconnected data item received*/
+        return_value = NotifyMessageRouter(
+          g_common_packet_format_data_item.data_item.data,
+          g_common_packet_format_data_item.data_item.length,
+          &message_router_response,
+          originator_address,
+          received_data->session_handle);
         if(return_value != kEipStatusError) {
           SkipEncapsulationHeader(outgoing_message);
           /* TODO: Here we get the status. What to do? kEipStatusError from AssembleLinearMessage().
            *  Its not clear how to transport this error information to the requester. */
-          EipStatus status = AssembleLinearMessage(&message_router_response, &g_common_packet_format_data_item, outgoing_message);
+          EipStatus status = AssembleLinearMessage(&message_router_response,
+                                                   &g_common_packet_format_data_item,
+                                                   outgoing_message);
 
           /* Save pointer and move to start for Encapusulation Header */
           CipOctet *buffer = outgoing_message->current_message_position;
-          outgoing_message->current_message_position = outgoing_message->message_buffer;
-          GenerateEncapsulationHeader(received_data, outgoing_message->used_message_length, received_data->session_handle, kEncapsulationProtocolSuccess,
-            outgoing_message);
+          outgoing_message->current_message_position =
+            outgoing_message->message_buffer;
+          GenerateEncapsulationHeader(received_data,
+                                      outgoing_message->used_message_length,
+                                      received_data->session_handle,
+                                      kEncapsulationProtocolSuccess,
+                                      outgoing_message);
           /* Move pointer back to last octet */
           outgoing_message->current_message_position = buffer;
           return_value = kEipStatusOkSend;
@@ -64,97 +83,137 @@ EipStatus NotifyCommonPacketFormat(const EncapsulationData *const received_data,
       } else {
         /* wrong data item detected*/
         OPENER_TRACE_ERR(
-            "notifyCPF: got something besides the expected CIP_ITEM_ID_UNCONNECTEDMESSAGE\n");
-        GenerateEncapsulationHeader(received_data, 0, received_data->session_handle, kEncapsulationProtocolIncorrectData, outgoing_message);
+          "notifyCPF: got something besides the expected CIP_ITEM_ID_UNCONNECTEDMESSAGE\n");
+        GenerateEncapsulationHeader(received_data,
+                                    0,
+                                    received_data->session_handle,
+                                    kEncapsulationProtocolIncorrectData,
+                                    outgoing_message);
         return_value = kEipStatusOkSend;
       }
     } else {
       OPENER_TRACE_ERR(
-          "notifyCPF: got something besides the expected CIP_ITEM_ID_NULL\n");
-      GenerateEncapsulationHeader(received_data, 0, received_data->session_handle, kEncapsulationProtocolIncorrectData, outgoing_message);
+        "notifyCPF: got something besides the expected CIP_ITEM_ID_NULL\n");
+      GenerateEncapsulationHeader(received_data,
+                                  0,
+                                  received_data->session_handle,
+                                  kEncapsulationProtocolIncorrectData,
+                                  outgoing_message);
       return_value = kEipStatusOkSend;
     }
   }
   return return_value;
 }
 
-EipStatus NotifyConnectedCommonPacketFormat(const EncapsulationData *const received_data, const struct sockaddr *const originator_address,
-    ENIPMessage *const outgoing_message) {
+EipStatus NotifyConnectedCommonPacketFormat(
+  const EncapsulationData *const received_data,
+  const struct sockaddr *const originator_address,
+  ENIPMessage *const outgoing_message) {
 
-  EipStatus return_value = CreateCommonPacketFormatStructure(received_data->current_communication_buffer_position, received_data->data_length,
+  EipStatus return_value = CreateCommonPacketFormatStructure(
+    received_data->current_communication_buffer_position,
+    received_data->data_length,
     &g_common_packet_format_data_item);
 
   if(kEipStatusError == return_value) {
     OPENER_TRACE_ERR("notifyConnectedCPF: error from createCPFstructure\n");
   } else {
     return_value = kEipStatusError; /* For connected explicit messages status always has to be 0*/
-    if(g_common_packet_format_data_item.address_item.type_id == kCipItemIdConnectionAddress) /* check if ConnectedAddressItem received, otherwise it is no connected message and should not be here*/
+    if(g_common_packet_format_data_item.address_item.type_id ==
+       kCipItemIdConnectionAddress)                                                          /* check if ConnectedAddressItem received, otherwise it is no connected message and should not be here*/
     { /* ConnectedAddressItem item */
-      CipConnectionObject *connection_object = GetConnectedObject(g_common_packet_format_data_item.address_item.data.connection_identifier);
+      CipConnectionObject *connection_object = GetConnectedObject(
+        g_common_packet_format_data_item.address_item.data.connection_identifier);
       if(NULL != connection_object) {
         /* reset the watchdog timer */
         ConnectionObjectResetInactivityWatchdogTimerValue(connection_object);
 
         /*TODO check connection id  and sequence count */
-        if(g_common_packet_format_data_item.data_item.type_id == kCipItemIdConnectedDataItem) { /* connected data item received*/
+        if(g_common_packet_format_data_item.data_item.type_id ==
+           kCipItemIdConnectedDataItem) {                                                       /* connected data item received*/
           EipUint8 *buffer = g_common_packet_format_data_item.data_item.data;
-          g_common_packet_format_data_item.address_item.data.sequence_number = GetUintFromMessage((const EipUint8** const ) &buffer);
+          g_common_packet_format_data_item.address_item.data.sequence_number =
+            GetUintFromMessage( (const EipUint8 **const ) &buffer );
           OPENER_TRACE_INFO(
-              "Class 3 sequence number: %d, last sequence number: %d\n",
-              g_common_packet_format_data_item.address_item.data.sequence_number,
-              connection_object->sequence_count_consuming);
-          if(connection_object->sequence_count_consuming == g_common_packet_format_data_item.address_item.data.sequence_number) {
-            memcpy(outgoing_message, &(connection_object->last_reply_sent), sizeof(ENIPMessage));
-            outgoing_message->current_message_position = outgoing_message->message_buffer;
+            "Class 3 sequence number: %d, last sequence number: %d\n",
+            g_common_packet_format_data_item.address_item.data.sequence_number,
+            connection_object->sequence_count_consuming);
+          if(connection_object->sequence_count_consuming ==
+             g_common_packet_format_data_item.address_item.data.sequence_number)
+          {
+            memcpy(outgoing_message,
+                   &(connection_object->last_reply_sent),
+                   sizeof(ENIPMessage) );
+            outgoing_message->current_message_position =
+              outgoing_message->message_buffer;
             /* Regenerate encapsulation header for new message */
             outgoing_message->used_message_length -=
-            ENCAPSULATION_HEADER_LENGTH;
-            GenerateEncapsulationHeader(received_data, outgoing_message->used_message_length, received_data->session_handle, kEncapsulationProtocolSuccess,
-              outgoing_message);
+              ENCAPSULATION_HEADER_LENGTH;
+            GenerateEncapsulationHeader(received_data,
+                                        outgoing_message->used_message_length,
+                                        received_data->session_handle,
+                                        kEncapsulationProtocolSuccess,
+                                        outgoing_message);
             outgoing_message->current_message_position = buffer;
             /* End regenerate encapsulation header for new message */
             return kEipStatusOkSend;
           }
-          connection_object->sequence_count_consuming = g_common_packet_format_data_item.address_item.data.sequence_number;
+          connection_object->sequence_count_consuming =
+            g_common_packet_format_data_item.address_item.data.sequence_number;
 
           ConnectionObjectResetInactivityWatchdogTimerValue(connection_object);
 
           CipMessageRouterResponse message_router_response;
           InitializeMessageRouterResponse(&message_router_response);
-          return_value = NotifyMessageRouter(buffer, g_common_packet_format_data_item.data_item.length - 2, &message_router_response, originator_address,
-            received_data->session_handle);
+          return_value = NotifyMessageRouter(buffer,
+                                             g_common_packet_format_data_item.data_item.length - 2,
+                                             &message_router_response,
+                                             originator_address,
+                                             received_data->session_handle);
 
           if(return_value != kEipStatusError) {
-            g_common_packet_format_data_item.address_item.data.connection_identifier = connection_object->cip_produced_connection_id;
+            g_common_packet_format_data_item.address_item.data.
+            connection_identifier =
+              connection_object->cip_produced_connection_id;
             SkipEncapsulationHeader(outgoing_message);
             /* TODO: Here we get the status. What to do? kEipStatusError from AssembleLinearMessage().
              *  Its not clear how to transport this error information to the requester. */
-            EipStatus status = AssembleLinearMessage(&message_router_response, &g_common_packet_format_data_item, outgoing_message);
+            EipStatus status = AssembleLinearMessage(&message_router_response,
+                                                     &g_common_packet_format_data_item,
+                                                     outgoing_message);
 
             CipOctet *buffer = outgoing_message->current_message_position;
-            outgoing_message->current_message_position = outgoing_message->message_buffer;
-            GenerateEncapsulationHeader(received_data, outgoing_message->used_message_length, received_data->session_handle, kEncapsulationProtocolSuccess,
-              outgoing_message);
+            outgoing_message->current_message_position =
+              outgoing_message->message_buffer;
+            GenerateEncapsulationHeader(received_data,
+                                        outgoing_message->used_message_length,
+                                        received_data->session_handle,
+                                        kEncapsulationProtocolSuccess,
+                                        outgoing_message);
             outgoing_message->current_message_position = buffer;
-            memcpy(&connection_object->last_reply_sent, outgoing_message, sizeof(ENIPMessage));
+            memcpy(&connection_object->last_reply_sent,
+                   outgoing_message,
+                   sizeof(ENIPMessage) );
             return_value = kEipStatusOkSend;
           }
         } else {
           /* wrong data item detected*/
           OPENER_TRACE_ERR(
-              "notifyConnectedCPF: got something besides the expected CIP_ITEM_ID_UNCONNECTEDMESSAGE\n");
+            "notifyConnectedCPF: got something besides the expected CIP_ITEM_ID_UNCONNECTEDMESSAGE\n");
         }
       } else {
         OPENER_TRACE_ERR(
-            "notifyConnectedCPF: connection with given ID could not be found\n");
+          "notifyConnectedCPF: connection with given ID could not be found\n");
       }
     } else {
       OPENER_TRACE_ERR(
-          "notifyConnectedCPF: got something besides the expected CIP_ITEM_ID_NULL\n");
+        "notifyConnectedCPF: got something besides the expected CIP_ITEM_ID_NULL\n");
     }
   }
   // return outgoing_message->used_message_length;
-  return (0 != outgoing_message->used_message_length ? kEipStatusOkSend : kEipStatusOk); /* TODO: What would the right EipStatus to return? */
+  return (0 !=
+          outgoing_message->used_message_length ? kEipStatusOkSend :
+          kEipStatusOk);                                                                 /* TODO: What would the right EipStatus to return? */
 }
 
 /**
@@ -166,12 +225,15 @@ EipStatus NotifyConnectedCommonPacketFormat(const EncapsulationData *const recei
  *   @return kEipStatusOk .. success
  *             kEipStatusError .. error
  */
-EipStatus CreateCommonPacketFormatStructure(const EipUint8 *data, size_t data_length, CipCommonPacketFormatData *common_packet_format_data) {
+EipStatus CreateCommonPacketFormatStructure(const EipUint8 *data,
+                                            size_t data_length,
+                                            CipCommonPacketFormatData *common_packet_format_data)
+{
 
   common_packet_format_data->address_info_item[0].type_id = 0;
   common_packet_format_data->address_info_item[1].type_id = 0;
 
-  int length_count = 0;
+  size_t length_count = 0;
   CipUint item_count = GetUintFromMessage(&data);
   //OPENER_ASSERT(4U >= item_count);/* Sanitizing data - probably needs to be changed for productive code */
   common_packet_format_data->item_count = item_count;
@@ -181,34 +243,49 @@ EipStatus CreateCommonPacketFormatStructure(const EipUint8 *data, size_t data_le
     common_packet_format_data->address_item.length = GetUintFromMessage(&data);
     length_count += 4;
     if(common_packet_format_data->address_item.length >= 4) {
-      common_packet_format_data->address_item.data.connection_identifier = GetUdintFromMessage(&data);
+      common_packet_format_data->address_item.data.connection_identifier =
+        GetUdintFromMessage(&data);
       length_count += 4;
     }
     if(common_packet_format_data->address_item.length == 8) {
-      common_packet_format_data->address_item.data.sequence_number = GetUdintFromMessage(&data);
+      common_packet_format_data->address_item.data.sequence_number =
+        GetUdintFromMessage(&data);
       length_count += 4;
     }
   }
   if(common_packet_format_data->item_count >= 2) {
     common_packet_format_data->data_item.type_id = GetUintFromMessage(&data);
     common_packet_format_data->data_item.length = GetUintFromMessage(&data);
-    common_packet_format_data->data_item.data = (EipUint8*) data;
-    data += common_packet_format_data->data_item.length;
-    length_count += (4 + common_packet_format_data->data_item.length);
+    common_packet_format_data->data_item.data = (EipUint8 *) data;
+    if(data_length >=
+       length_count + 4 + common_packet_format_data->data_item.length) {
+      data += common_packet_format_data->data_item.length;
+      length_count += (4 + common_packet_format_data->data_item.length);
+    } else {
+      return kEipStatusError;
+    }
 
     CipUsint address_item_count = common_packet_format_data->item_count - 2;
-    for(size_t j = 0; j < (address_item_count > 2 ? 2 : address_item_count); j++) /* TODO there needs to be a limit check here???*/
+    for(size_t j = 0; j < (address_item_count > 2 ? 2 : address_item_count);
+        j++)                                                                      /* TODO there needs to be a limit check here???*/
     {
-      common_packet_format_data->address_info_item[j].type_id = GetIntFromMessage(&data);
+      common_packet_format_data->address_info_item[j].type_id =
+        GetIntFromMessage(&data);
       OPENER_TRACE_INFO("Sockaddr type id: %x\n",
-          common_packet_format_data->address_info_item[j].type_id);
+                        common_packet_format_data->address_info_item[j].type_id);
       length_count += 2;
-      if((common_packet_format_data->address_info_item[j].type_id == kCipItemIdSocketAddressInfoOriginatorToTarget)
-        || (common_packet_format_data->address_info_item[j].type_id == kCipItemIdSocketAddressInfoTargetToOriginator)) {
-        common_packet_format_data->address_info_item[j].length = GetIntFromMessage(&data);
-        common_packet_format_data->address_info_item[j].sin_family = GetIntFromMessage(&data);
-        common_packet_format_data->address_info_item[j].sin_port = GetIntFromMessage(&data);
-        common_packet_format_data->address_info_item[j].sin_addr = GetUdintFromMessage(&data);
+      if( (common_packet_format_data->address_info_item[j].type_id ==
+           kCipItemIdSocketAddressInfoOriginatorToTarget)
+          || (common_packet_format_data->address_info_item[j].type_id ==
+              kCipItemIdSocketAddressInfoTargetToOriginator) ) {
+        common_packet_format_data->address_info_item[j].length =
+          GetIntFromMessage(&data);
+        common_packet_format_data->address_info_item[j].sin_family =
+          GetIntFromMessage(&data);
+        common_packet_format_data->address_info_item[j].sin_port =
+          GetIntFromMessage(&data);
+        common_packet_format_data->address_info_item[j].sin_addr =
+          GetUdintFromMessage(&data);
         for(size_t i = 0; i < 8; i++) {
           common_packet_format_data->address_info_item[j].nasin_zero[i] = *data;
           data++;
@@ -231,7 +308,7 @@ EipStatus CreateCommonPacketFormatStructure(const EipUint8 *data, size_t data_le
     return kEipStatusOk;
   } else {
     OPENER_TRACE_WARN(
-        "something is wrong with the length in Message Router @ CreateCommonPacketFormatStructure\n");
+      "something is wrong with the length in Message Router @ CreateCommonPacketFormatStructure\n");
     if(common_packet_format_data->item_count > 2) {
       /* there is an optional packet in data stream which is not sockaddr item */
       return kEipStatusOk;
@@ -256,11 +333,15 @@ void EncodeNullAddressItem(ENIPMessage *const outgoing_message) {
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeConnectedAddressItem(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+void EncodeConnectedAddressItem(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
   /* connected data item -> address length set to 4 and copy ConnectionIdentifier */
   AddIntToMessage(kCipItemIdConnectionAddress, outgoing_message);
   AddIntToMessage(4, outgoing_message);
-  AddDintToMessage(common_packet_format_data_item->address_item.data.connection_identifier, outgoing_message);
+  AddDintToMessage(
+    common_packet_format_data_item->address_item.data.connection_identifier,
+    outgoing_message);
 }
 
 /**
@@ -269,12 +350,18 @@ void EncodeConnectedAddressItem(const CipCommonPacketFormatData *const common_pa
  * @param common_packet_format_data_item Common Packet Format item which is used in the encoding
  * @param outgoing_message The outgoing message object
  */
-void EncodeSequencedAddressItem(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+void EncodeSequencedAddressItem(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
   /* sequenced address item -> address length set to 8 and copy ConnectionIdentifier and SequenceNumber */
   AddIntToMessage(kCipItemIdSequencedAddressItem, outgoing_message);
   AddIntToMessage(kSequencedAddressItemLength, outgoing_message);
-  AddDintToMessage(common_packet_format_data_item->address_item.data.connection_identifier, outgoing_message);
-  AddDintToMessage(common_packet_format_data_item->address_item.data.sequence_number, outgoing_message);
+  AddDintToMessage(
+    common_packet_format_data_item->address_item.data.connection_identifier,
+    outgoing_message);
+  AddDintToMessage(
+    common_packet_format_data_item->address_item.data.sequence_number,
+    outgoing_message);
 }
 
 /**
@@ -283,7 +370,9 @@ void EncodeSequencedAddressItem(const CipCommonPacketFormatData *const common_pa
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeItemCount(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+void EncodeItemCount(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
   AddIntToMessage(common_packet_format_data_item->item_count, outgoing_message); /* item count */
 }
 
@@ -293,8 +382,11 @@ void EncodeItemCount(const CipCommonPacketFormatData *const common_packet_format
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeDataItemType(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
-  AddIntToMessage(common_packet_format_data_item->data_item.type_id, outgoing_message);
+void EncodeDataItemType(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
+  AddIntToMessage(common_packet_format_data_item->data_item.type_id,
+                  outgoing_message);
 }
 
 /**
@@ -303,8 +395,11 @@ void EncodeDataItemType(const CipCommonPacketFormatData *const common_packet_for
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeDataItemLength(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
-  AddIntToMessage(common_packet_format_data_item->data_item.length, outgoing_message);
+void EncodeDataItemLength(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
+  AddIntToMessage(common_packet_format_data_item->data_item.length,
+                  outgoing_message);
 }
 
 /**
@@ -313,10 +408,16 @@ void EncodeDataItemLength(const CipCommonPacketFormatData *const common_packet_f
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeDataItemData(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
-  memcpy(outgoing_message->current_message_position, common_packet_format_data_item->data_item.data, common_packet_format_data_item->data_item.length);
-  outgoing_message->current_message_position += common_packet_format_data_item->data_item.length;
-  outgoing_message->used_message_length += common_packet_format_data_item->data_item.length;
+void EncodeDataItemData(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
+  memcpy(outgoing_message->current_message_position,
+         common_packet_format_data_item->data_item.data,
+         common_packet_format_data_item->data_item.length);
+  outgoing_message->current_message_position +=
+    common_packet_format_data_item->data_item.length;
+  outgoing_message->used_message_length +=
+    common_packet_format_data_item->data_item.length;
 }
 
 /**
@@ -326,9 +427,15 @@ void EncodeDataItemData(const CipCommonPacketFormatData *const common_packet_for
  * @param outgoing_message The outgoing message object
  */
 
-void EncodeConnectedDataItemLength(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
-  AddIntToMessage((EipUint16) (message_router_response->message.used_message_length + 4 + 2 /* TODO: Magic numbers */
-  + (2 * message_router_response->size_of_additional_status)), outgoing_message);
+void EncodeConnectedDataItemLength(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
+  AddIntToMessage( (EipUint16) (message_router_response->message.
+                                used_message_length + 4 + 2                                 /* TODO: Magic numbers */
+                                + (2 *
+                                   message_router_response->
+                                   size_of_additional_status) ),
+                   outgoing_message );
 }
 
 /**
@@ -337,8 +444,12 @@ void EncodeConnectedDataItemLength(const CipMessageRouterResponse *const message
  * @param common_packet_format_data_item
  * @param outgoing_message The outgoing message object
  */
-void EncodeSequenceNumber(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
-  AddIntToMessage((EipUint16) common_packet_format_data_item->address_item.data.sequence_number, outgoing_message);
+void EncodeSequenceNumber(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
+  AddIntToMessage(
+     (EipUint16) common_packet_format_data_item->address_item.data.sequence_number,
+    outgoing_message );
 }
 
 /**
@@ -347,7 +458,9 @@ void EncodeSequenceNumber(const CipCommonPacketFormatData *const common_packet_f
  * @param message_router_response The router response message data structure to be processed
  * @param outgoing_message The outgoing message object
  */
-void EncodeReplyService(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
+void EncodeReplyService(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
   AddSintToMessage(message_router_response->reply_service, outgoing_message);
 }
 
@@ -357,7 +470,9 @@ void EncodeReplyService(const CipMessageRouterResponse *const message_router_res
  * @param message_router_response Router Response message to be processed
  * @param outgoing_message The outgoing message object
  */
-void EncodeReservedFieldOfLengthByte(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
+void EncodeReservedFieldOfLengthByte(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
   AddSintToMessage(message_router_response->reserved, outgoing_message);
 }
 
@@ -367,7 +482,9 @@ void EncodeReservedFieldOfLengthByte(const CipMessageRouterResponse *const messa
  * @param message_router_response Router Response message to be processed
  * @param outgoing_message The outgoing message object
  */
-void EncodeGeneralStatus(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
+void EncodeGeneralStatus(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
   AddSintToMessage(message_router_response->general_status, outgoing_message);
 }
 
@@ -378,8 +495,11 @@ void EncodeGeneralStatus(const CipMessageRouterResponse *const message_router_re
  * @param outgoing_message The outgoing message object
  */
 
-void EncodeExtendedStatusLength(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
-  AddSintToMessage(message_router_response->size_of_additional_status, outgoing_message);
+void EncodeExtendedStatusLength(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
+  AddSintToMessage(message_router_response->size_of_additional_status,
+                   outgoing_message);
 }
 
 /**
@@ -388,9 +508,14 @@ void EncodeExtendedStatusLength(const CipMessageRouterResponse *const message_ro
  * @param message_router_response Router Response message to be processed
  * @param outgoing_message The outgoing message object
  */
-void EncodeExtendedStatusDataItems(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
-  for(size_t i = 0; i < message_router_response->size_of_additional_status && i < MAX_SIZE_OF_ADD_STATUS; i++) {
-    AddIntToMessage(message_router_response->additional_status[i], outgoing_message);
+void EncodeExtendedStatusDataItems(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
+  for(size_t i = 0;
+      i < message_router_response->size_of_additional_status &&
+      i < MAX_SIZE_OF_ADD_STATUS; i++) {
+    AddIntToMessage(message_router_response->additional_status[i],
+                    outgoing_message);
   }
 }
 
@@ -404,7 +529,9 @@ void EncodeExtendedStatusDataItems(const CipMessageRouterResponse *const message
  * @param outgoing_message The outgoing message object
  */
 
-void EncodeExtendedStatus(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
+void EncodeExtendedStatus(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
   EncodeExtendedStatusLength(message_router_response, outgoing_message);
   EncodeExtendedStatusDataItems(message_router_response, outgoing_message);
 }
@@ -416,9 +543,15 @@ void EncodeExtendedStatus(const CipMessageRouterResponse *const message_router_r
  * @param outgoing_message The outgoing message object
  *
  */
-void EncodeUnconnectedDataItemLength(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
-  AddIntToMessage((EipUint16) (message_router_response->message.used_message_length + 4 /* TODO: Magic number */
-  + (2 * message_router_response->size_of_additional_status)), outgoing_message);
+void EncodeUnconnectedDataItemLength(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
+  AddIntToMessage( (EipUint16) (message_router_response->message.
+                                used_message_length + 4                                 /* TODO: Magic number */
+                                + (2 *
+                                   message_router_response->
+                                   size_of_additional_status) ),
+                   outgoing_message );
 }
 
 /**
@@ -427,11 +560,17 @@ void EncodeUnconnectedDataItemLength(const CipMessageRouterResponse *const messa
  * @param message_router_response Router Response message to be processed
  * @param outgoing_message The outgoing message object
  */
-void EncodeMessageRouterResponseData(const CipMessageRouterResponse *const message_router_response, ENIPMessage *const outgoing_message) {
-  memcpy(outgoing_message->current_message_position, message_router_response->message.message_buffer, message_router_response->message.used_message_length);
-
-  outgoing_message->current_message_position += message_router_response->message.used_message_length;
-  outgoing_message->used_message_length += message_router_response->message.used_message_length;
+void EncodeMessageRouterResponseData(
+  const CipMessageRouterResponse *const message_router_response,
+  ENIPMessage *const outgoing_message) {
+  memcpy(outgoing_message->current_message_position,
+         message_router_response->message.message_buffer,
+         message_router_response->message.used_message_length);
+
+  outgoing_message->current_message_position +=
+    message_router_response->message.used_message_length;
+  outgoing_message->used_message_length +=
+    message_router_response->message.used_message_length;
 }
 
 /**
@@ -441,9 +580,13 @@ void EncodeMessageRouterResponseData(const CipMessageRouterResponse *const messa
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeSockaddrInfoItemTypeId(int item_type, const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+void EncodeSockaddrInfoItemTypeId(int item_type,
+                                  const CipCommonPacketFormatData *const common_packet_format_data_item,
+                                  ENIPMessage *const outgoing_message) {
   OPENER_ASSERT(item_type == 0 || item_type == 1);
-  AddIntToMessage(common_packet_format_data_item->address_info_item[item_type].type_id, outgoing_message);
+  AddIntToMessage(
+    common_packet_format_data_item->address_info_item[item_type].type_id,
+    outgoing_message);
 }
 
 /**
@@ -453,12 +596,18 @@ void EncodeSockaddrInfoItemTypeId(int item_type, const CipCommonPacketFormatData
  * @param common_packet_format_data_item The Common Packet Format data structure from which the message is constructed
  * @param outgoing_message The outgoing message object
  */
-void EncodeSockaddrInfoLength(int item_type, const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
-  AddIntToMessage(common_packet_format_data_item->address_info_item[item_type].length, outgoing_message);
+void EncodeSockaddrInfoLength(int item_type,
+                              const CipCommonPacketFormatData *const common_packet_format_data_item,
+                              ENIPMessage *const outgoing_message) {
+  AddIntToMessage(
+    common_packet_format_data_item->address_info_item[item_type].length,
+    outgoing_message);
 }
 
-EipStatus AssembleLinearMessage(const CipMessageRouterResponse *const message_router_response,
-    const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+EipStatus AssembleLinearMessage(
+  const CipMessageRouterResponse *const message_router_response,
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
 
   if(message_router_response) {
     /* add Interface Handle and Timeout = 0 -> only for SendRRData and SendUnitData necessary */
@@ -469,17 +618,19 @@ EipStatus AssembleLinearMessage(const CipMessageRouterResponse *const message_ro
   EncodeItemCount(common_packet_format_data_item, outgoing_message);
 
   /* process Address Item */
-  switch(common_packet_format_data_item->address_item.type_id){
+  switch(common_packet_format_data_item->address_item.type_id) {
     case kCipItemIdNullAddress: {
       EncodeNullAddressItem(outgoing_message);
       break;
     }
     case kCipItemIdConnectionAddress: {
-      EncodeConnectedAddressItem(common_packet_format_data_item, outgoing_message);
+      EncodeConnectedAddressItem(common_packet_format_data_item,
+                                 outgoing_message);
       break;
     }
     case kCipItemIdSequencedAddressItem: {
-      EncodeSequencedAddressItem(common_packet_format_data_item, outgoing_message);
+      EncodeSequencedAddressItem(common_packet_format_data_item,
+                                 outgoing_message);
       break;
     }
     default:
@@ -488,26 +639,34 @@ EipStatus AssembleLinearMessage(const CipMessageRouterResponse *const message_ro
   }
 
   /* process Data Item */
-  if((common_packet_format_data_item->data_item.type_id == kCipItemIdUnconnectedDataItem)
-    || (common_packet_format_data_item->data_item.type_id == kCipItemIdConnectedDataItem)) {
+  if( (common_packet_format_data_item->data_item.type_id ==
+       kCipItemIdUnconnectedDataItem)
+      || (common_packet_format_data_item->data_item.type_id ==
+          kCipItemIdConnectedDataItem) ) {
 
     if(message_router_response) {
       EncodeDataItemType(common_packet_format_data_item, outgoing_message);
 
-      if(common_packet_format_data_item->data_item.type_id == kCipItemIdConnectedDataItem) { /* Connected Item */
-        EncodeConnectedDataItemLength(message_router_response, outgoing_message);
-        EncodeSequenceNumber(&g_common_packet_format_data_item, outgoing_message);
+      if(common_packet_format_data_item->data_item.type_id ==
+         kCipItemIdConnectedDataItem) {                                                      /* Connected Item */
+        EncodeConnectedDataItemLength(message_router_response,
+                                      outgoing_message);
+        EncodeSequenceNumber(&g_common_packet_format_data_item,
+                             outgoing_message);
 
       } else { /* Unconnected Item */
-        EncodeUnconnectedDataItemLength(message_router_response, outgoing_message);
+        EncodeUnconnectedDataItemLength(message_router_response,
+                                        outgoing_message);
       }
 
       /* write message router response into linear memory */
       EncodeReplyService(message_router_response, outgoing_message);
-      EncodeReservedFieldOfLengthByte(message_router_response, outgoing_message);
+      EncodeReservedFieldOfLengthByte(message_router_response,
+                                      outgoing_message);
       EncodeGeneralStatus(message_router_response, outgoing_message);
       EncodeExtendedStatus(message_router_response, outgoing_message);
-      EncodeMessageRouterResponseData(message_router_response, outgoing_message);
+      EncodeMessageRouterResponseData(message_router_response,
+                                      outgoing_message);
     } else { /* connected IO Message to send */
       EncodeDataItemType(common_packet_format_data_item, outgoing_message);
 
@@ -522,17 +681,26 @@ EipStatus AssembleLinearMessage(const CipMessageRouterResponse *const message_ro
    * EtherNet/IP specification doesn't demand it, but there are EIP
    * devices which depend on CPF items to appear in the order of their
    * ID number */
-  for(int type = kCipItemIdSocketAddressInfoOriginatorToTarget; type <= kCipItemIdSocketAddressInfoTargetToOriginator; type++) {
+  for(int type = kCipItemIdSocketAddressInfoOriginatorToTarget;
+      type <= kCipItemIdSocketAddressInfoTargetToOriginator; type++) {
     for(int j = 0; j < 2; j++) {
       if(common_packet_format_data_item->address_info_item[j].type_id == type) {
-        EncodeSockaddrInfoItemTypeId(j, common_packet_format_data_item, outgoing_message);
+        EncodeSockaddrInfoItemTypeId(j,
+                                     common_packet_format_data_item,
+                                     outgoing_message);
 
-        EncodeSockaddrInfoLength(j, common_packet_format_data_item, outgoing_message);
+        EncodeSockaddrInfoLength(j,
+                                 common_packet_format_data_item,
+                                 outgoing_message);
 
-        EncapsulateIpAddress(common_packet_format_data_item->address_info_item[j].sin_port, common_packet_format_data_item->address_info_item[j].sin_addr,
+        EncapsulateIpAddress(
+          common_packet_format_data_item->address_info_item[j].sin_port,
+          common_packet_format_data_item->address_info_item[j].sin_addr,
           outgoing_message);
 
-        FillNextNMessageOctetsWithValueAndMoveToNextPosition(0, 8, outgoing_message);
+        FillNextNMessageOctetsWithValueAndMoveToNextPosition(0,
+                                                             8,
+                                                             outgoing_message);
         break;
       }
     }
@@ -540,6 +708,8 @@ EipStatus AssembleLinearMessage(const CipMessageRouterResponse *const message_ro
   return kEipStatusOk;
 }
 
-void AssembleIOMessage(const CipCommonPacketFormatData *const common_packet_format_data_item, ENIPMessage *const outgoing_message) {
+void AssembleIOMessage(
+  const CipCommonPacketFormatData *const common_packet_format_data_item,
+  ENIPMessage *const outgoing_message) {
   AssembleLinearMessage(0, common_packet_format_data_item, outgoing_message);
 }

+ 1 - 1
source/src/opener_api.h

@@ -174,7 +174,7 @@ CipClass *CreateCipClass(const CipUdint class_code,
                          const EipUint32 highest_instance_attribute_number,
                          const int number_of_instance_services,
                          const int number_of_instances,
-                         char *name,
+                         const char *const name,
                          const EipUint16 revision,
                          InitializeCipClass initializer);