Add a payload.get_header_length() method, remove header length definitions
authorMartin Willi <martin@revosec.ch>
Thu, 17 Nov 2011 11:27:46 +0000 (11:27 +0000)
committerMartin Willi <martin@revosec.ch>
Tue, 20 Mar 2012 16:30:42 +0000 (17:30 +0100)
44 files changed:
src/conftest/hooks/ike_auth_fill.c
src/libcharon/encoding/parser.c
src/libcharon/encoding/payloads/auth_payload.c
src/libcharon/encoding/payloads/auth_payload.h
src/libcharon/encoding/payloads/cert_payload.c
src/libcharon/encoding/payloads/cert_payload.h
src/libcharon/encoding/payloads/certreq_payload.c
src/libcharon/encoding/payloads/certreq_payload.h
src/libcharon/encoding/payloads/configuration_attribute.c
src/libcharon/encoding/payloads/configuration_attribute.h
src/libcharon/encoding/payloads/cp_payload.c
src/libcharon/encoding/payloads/cp_payload.h
src/libcharon/encoding/payloads/delete_payload.c
src/libcharon/encoding/payloads/delete_payload.h
src/libcharon/encoding/payloads/eap_payload.c
src/libcharon/encoding/payloads/eap_payload.h
src/libcharon/encoding/payloads/encryption_payload.c
src/libcharon/encoding/payloads/encryption_payload.h
src/libcharon/encoding/payloads/id_payload.c
src/libcharon/encoding/payloads/id_payload.h
src/libcharon/encoding/payloads/ike_header.c
src/libcharon/encoding/payloads/ke_payload.c
src/libcharon/encoding/payloads/ke_payload.h
src/libcharon/encoding/payloads/nonce_payload.c
src/libcharon/encoding/payloads/nonce_payload.h
src/libcharon/encoding/payloads/notify_payload.c
src/libcharon/encoding/payloads/notify_payload.h
src/libcharon/encoding/payloads/payload.h
src/libcharon/encoding/payloads/proposal_substructure.c
src/libcharon/encoding/payloads/proposal_substructure.h
src/libcharon/encoding/payloads/sa_payload.c
src/libcharon/encoding/payloads/sa_payload.h
src/libcharon/encoding/payloads/traffic_selector_substructure.c
src/libcharon/encoding/payloads/traffic_selector_substructure.h
src/libcharon/encoding/payloads/transform_attribute.c
src/libcharon/encoding/payloads/transform_substructure.c
src/libcharon/encoding/payloads/transform_substructure.h
src/libcharon/encoding/payloads/ts_payload.c
src/libcharon/encoding/payloads/ts_payload.h
src/libcharon/encoding/payloads/unknown_payload.c
src/libcharon/encoding/payloads/unknown_payload.h
src/libcharon/encoding/payloads/vendor_id_payload.c
src/libcharon/encoding/payloads/vendor_id_payload.h
src/libcharon/network/receiver.c

index 2843d60..052a4be 100644 (file)
@@ -51,7 +51,10 @@ struct private_ike_auth_fill_t {
 
 /** size of non ESP-Marker */
 #define NON_ESP_MARKER_LEN 4
-
+/** length of fixed encryption payload header */
+#define ENCRYPTION_PAYLOAD_HEADER_LENGTH 4
+/** length of fixed cert payload header */
+#define CERT_PAYLOAD_HEADER_LENGTH 5
 /**
  * Calculate packet size on wire (without ethernet/IP header)
  */
index 4736873..719c942 100644 (file)
@@ -358,12 +358,40 @@ static bool parse_chunk(private_parser_t *this, int rule_number,
        return TRUE;
 }
 
+/**
+ * Map a encoding type to a encoded payload
+ */
+static payload_type_t map_wrapped_payload(encoding_type_t type)
+{
+       switch (type)
+       {
+               case PROPOSALS:
+                       return PROPOSAL_SUBSTRUCTURE;
+               case PROPOSALS_V1:
+                       return PROPOSAL_SUBSTRUCTURE_V1;
+               case TRANSFORMS:
+                       return TRANSFORM_SUBSTRUCTURE;
+               case TRANSFORMS_V1:
+                       return TRANSFORM_SUBSTRUCTURE_V1;
+               case TRANSFORM_ATTRIBUTES:
+                       return TRANSFORM_ATTRIBUTE;
+               case TRANSFORM_ATTRIBUTES_V1:
+                       return TRANSFORM_ATTRIBUTE_V1;
+               case CONFIGURATION_ATTRIBUTES:
+                       return CONFIGURATION_ATTRIBUTE;
+               case TRAFFIC_SELECTORS:
+                       return TRAFFIC_SELECTOR_SUBSTRUCTURE;
+               default:
+                       return NO_PAYLOAD;
+       }
+}
+
 METHOD(parser_t, parse_payload, status_t,
        private_parser_t *this, payload_type_t payload_type, payload_t **payload)
 {
        payload_t *pld;
        void *output;
-       int payload_length = 0, spi_size = 0, attribute_length = 0;
+       int payload_length = 0, spi_size = 0, attribute_length = 0, header_length;
        u_int16_t ts_type = 0;
        bool attribute_format = FALSE;
        int rule_number, rule_count;
@@ -381,6 +409,7 @@ METHOD(parser_t, parse_payload, status_t,
        /* base pointer for output, avoids casting in every rule */
        output = pld;
 
+       header_length = pld->get_header_length(pld);
        /* parse the payload with its own rulse */
        rule_count = pld->get_encoding_rules(pld, &this->rules);
        for (rule_number = 0; rule_number < rule_count; rule_number++)
@@ -456,7 +485,8 @@ METHOD(parser_t, parse_payload, status_t,
                                }
                                /* parsed u_int16 should be aligned */
                                payload_length = *(u_int16_t*)(output + rule->offset);
-                               if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH)
+                               /* all payloads must have at least 4 bytes header */
+                               if (payload_length < 4)
                                {
                                        pld->destroy(pld);
                                        return PARSE_ERROR;
@@ -483,86 +513,44 @@ METHOD(parser_t, parse_payload, status_t,
                                }
                                break;
                        }
+                       /* lists */
                        case PROPOSALS:
-                       {
-                               if (payload_length < SA_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                               PROPOSAL_SUBSTRUCTURE,
-                                                               payload_length - SA_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case PROPOSALS_V1:
-                       {
-                               if (payload_length < SA_PAYLOAD_V1_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                               PROPOSAL_SUBSTRUCTURE_V1,
-                                                               payload_length - SA_PAYLOAD_V1_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case TRANSFORMS:
-                       {
-                               if (payload_length <
-                                                       spi_size + PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                       TRANSFORM_SUBSTRUCTURE, payload_length - spi_size -
-                                                                               PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case TRANSFORMS_V1:
-                       {
-                               if (payload_length <
-                                                       spi_size + PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                       TRANSFORM_SUBSTRUCTURE_V1, payload_length - spi_size -
-                                                                               PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case TRANSFORM_ATTRIBUTES:
-                       {
-                               if (payload_length < TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                               TRANSFORM_ATTRIBUTE,
-                                               payload_length - TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case TRANSFORM_ATTRIBUTES_V1:
+                       case TRAFFIC_SELECTORS:
                        {
-                               if (payload_length < TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH ||
+                               if (payload_length < header_length ||
                                        !parse_list(this, rule_number, output + rule->offset,
-                                               TRANSFORM_ATTRIBUTE_V1,
-                                               payload_length - TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH))
+                                                               map_wrapped_payload(rule->type),
+                                                               payload_length - header_length))
                                {
                                        pld->destroy(pld);
                                        return PARSE_ERROR;
                                }
                                break;
                        }
-                       case CONFIGURATION_ATTRIBUTES:
+                       /* chunks */
+                       case NONCE_DATA:
+                       case ID_DATA:
+                       case AUTH_DATA:
+                       case CERT_DATA:
+                       case CERTREQ_DATA:
+                       case EAP_DATA:
+                       case SPIS:
+                       case VID_DATA:
+                       case CONFIGURATION_ATTRIBUTE_VALUE:
+                       case KEY_EXCHANGE_DATA:
+                       case KEY_EXCHANGE_DATA_V1:
+                       case NOTIFICATION_DATA:
+                       case ENCRYPTED_DATA:
+                       case UNKNOWN_DATA:
                        {
-                               if (payload_length < CP_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                               CONFIGURATION_ATTRIBUTE,
-                                                               payload_length - CP_PAYLOAD_HEADER_LENGTH))
+                               if (payload_length < header_length ||
+                                       !parse_chunk(this, rule_number, output + rule->offset,
+                                                                payload_length - header_length))
                                {
                                        pld->destroy(pld);
                                        return PARSE_ERROR;
@@ -619,148 +607,6 @@ METHOD(parser_t, parse_payload, status_t,
                                }
                                break;
                        }
-                       case NONCE_DATA:
-                       {
-                               if (payload_length < NONCE_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - NONCE_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case ID_DATA:
-                       {
-                               if (payload_length < ID_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - ID_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case AUTH_DATA:
-                       {
-                               if (payload_length < AUTH_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - AUTH_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case CERT_DATA:
-                       {
-                               if (payload_length < CERT_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - CERT_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case CERTREQ_DATA:
-                       {
-                               if (payload_length < CERTREQ_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - CERTREQ_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case EAP_DATA:
-                       {
-                               if (payload_length < EAP_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - EAP_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case SPIS:
-                       {
-                               if (payload_length < DELETE_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - DELETE_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case VID_DATA:
-                       {
-                               if (payload_length < VENDOR_ID_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                       payload_length - VENDOR_ID_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case CONFIGURATION_ATTRIBUTE_VALUE:
-                       {
-                               if (!parse_chunk(this, rule_number, output + rule->offset,
-                                                                attribute_length))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case KEY_EXCHANGE_DATA:
-                       {
-                               if (payload_length < KE_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - KE_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case KEY_EXCHANGE_DATA_V1:
-                       {
-                               if (payload_length < KE_PAYLOAD_V1_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                                payload_length - KE_PAYLOAD_V1_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case NOTIFICATION_DATA:
-                       {
-                               if (payload_length < NOTIFY_PAYLOAD_HEADER_LENGTH + spi_size ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                               payload_length - NOTIFY_PAYLOAD_HEADER_LENGTH - spi_size))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case ENCRYPTED_DATA:
-                       {
-                               if (payload_length < ENCRYPTION_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                       payload_length - ENCRYPTION_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        case TS_TYPE:
                        {
                                if (!parse_uint8(this, rule_number, output + rule->offset))
@@ -783,29 +629,6 @@ METHOD(parser_t, parse_payload, status_t,
                                }
                                break;
                        }
-                       case TRAFFIC_SELECTORS:
-                       {
-                               if (payload_length < TS_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_list(this, rule_number, output + rule->offset,
-                                                               TRAFFIC_SELECTOR_SUBSTRUCTURE,
-                                                               payload_length - TS_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
-                       case UNKNOWN_DATA:
-                       {
-                               if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH ||
-                                       !parse_chunk(this, rule_number, output + rule->offset,
-                                                               payload_length - UNKNOWN_PAYLOAD_HEADER_LENGTH))
-                               {
-                                       pld->destroy(pld);
-                                       return PARSE_ERROR;
-                               }
-                               break;
-                       }
                        default:
                        {
                                DBG1(DBG_ENC, "  no rule to parse rule %d %N",
index 3825244..57dc58f 100644 (file)
@@ -126,6 +126,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_auth_payload_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_auth_payload_t *this)
 {
@@ -167,7 +173,7 @@ METHOD(auth_payload_t, set_data, void,
 {
        free(this->auth_data.ptr);
        this->auth_data = chunk_clone(data);
-       this->payload_length = AUTH_PAYLOAD_HEADER_LENGTH + this->auth_data.len;
+       this->payload_length = get_header_length(this) + this->auth_data.len;
 }
 
 METHOD(auth_payload_t, get_data, chunk_t,
@@ -195,6 +201,7 @@ auth_payload_t *auth_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -208,7 +215,7 @@ auth_payload_t *auth_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = AUTH_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
        );
        return &this->public;
 }
index e4c4e6a..521fe1d 100644 (file)
@@ -29,11 +29,6 @@ typedef struct auth_payload_t auth_payload_t;
 #include <sa/authenticators/authenticator.h>
 
 /**
- * Length of a auth payload without the auth data in bytes.
- */
-#define AUTH_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Class representing an IKEv2 AUTH payload.
  *
  * The AUTH payload format is described in RFC section 3.8.
index 8f6deb8..565be7f 100644 (file)
@@ -173,6 +173,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_cert_payload_t *this)
+{
+       return 5;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_cert_payload_t *this)
 {
@@ -270,6 +276,7 @@ cert_payload_t *cert_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -283,7 +290,7 @@ cert_payload_t *cert_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = CERT_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
        );
        return &this->public;
 }
@@ -312,7 +319,7 @@ cert_payload_t *cert_payload_create_from_cert(certificate_t *cert)
                free(this);
                return NULL;
        }
-       this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
+       this->payload_length = get_header_length(this) + this->data.len;
        return &this->public;
 }
 
@@ -325,7 +332,7 @@ cert_payload_t *cert_payload_create_from_hash_and_url(chunk_t hash, char *url)
 
        this->encoding = ENC_X509_HASH_AND_URL;
        this->data = chunk_cat("cc", hash, chunk_create(url, strlen(url)));
-       this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
+       this->payload_length = get_header_length(this) + this->data.len;
        return &this->public;
 }
 
@@ -338,6 +345,6 @@ cert_payload_t *cert_payload_create_custom(cert_encoding_t type, chunk_t data)
 
        this->encoding = type;
        this->data = data;
-       this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
+       this->payload_length = get_header_length(this) + this->data.len;
        return &this->public;
 }
index 21b503a..6eb79c8 100644 (file)
@@ -31,11 +31,6 @@ typedef enum cert_encoding_t cert_encoding_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Length of a cert payload without the cert data in bytes.
- */
-#define CERT_PAYLOAD_HEADER_LENGTH 5
-
-/**
  * Certifcate encodings, as in RFC4306
  */
 enum cert_encoding_t {
index ec46e92..dc685e6 100644 (file)
@@ -129,6 +129,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_certreq_payload_t *this)
+{
+       return 5;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_certreq_payload_t *this)
 {
@@ -241,6 +247,7 @@ certreq_payload_t *certreq_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -253,7 +260,7 @@ certreq_payload_t *certreq_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = CERTREQ_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
        );
        return &this->public;
 }
index 9140636..421ad6d 100644 (file)
@@ -29,11 +29,6 @@ typedef struct certreq_payload_t certreq_payload_t;
 #include <encoding/payloads/cert_payload.h>
 
 /**
- * Length of a CERTREQ payload without the CERTREQ data in bytes.
- */
-#define CERTREQ_PAYLOAD_HEADER_LENGTH 5
-
-/**
  * Class representing an IKEv2 CERTREQ payload.
  *
  * The CERTREQ payload format is described in RFC section 3.7.
index a657ef1..8746b9b 100644 (file)
@@ -161,6 +161,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_configuration_attribute_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_configuration_attribute_t *this)
 {
@@ -181,7 +187,7 @@ METHOD(payload_t, set_next_type, void,
 METHOD(payload_t, get_length, size_t,
        private_configuration_attribute_t *this)
 {
-       return this->value.len + CONFIGURATION_ATTRIBUTE_HEADER_LENGTH;
+       return get_header_length(this) + this->value.len;
 }
 
 METHOD(configuration_attribute_t, get_cattr_type, configuration_attribute_type_t,
@@ -215,6 +221,7 @@ configuration_attribute_t *configuration_attribute_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
index 6e4b018..e216ee3 100644 (file)
@@ -29,11 +29,6 @@ typedef struct configuration_attribute_t configuration_attribute_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Configuration attribute header length in bytes.
- */
-#define CONFIGURATION_ATTRIBUTE_HEADER_LENGTH 4
-
-/**
  * Class representing an IKEv2-CONFIGURATION Attribute.
  *
  * The CONFIGURATION ATTRIBUTE format is described in RFC section 3.15.1.
index 4e782dd..05f16c3 100644 (file)
@@ -149,6 +149,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_cp_payload_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_cp_payload_t *this)
 {
@@ -175,7 +181,7 @@ static void compute_length(private_cp_payload_t *this)
        enumerator_t *enumerator;
        payload_t *attribute;
 
-       this->payload_length = CP_PAYLOAD_HEADER_LENGTH;
+       this->payload_length = get_header_length(this);
 
        enumerator = this->attributes->create_enumerator(this->attributes);
        while (enumerator->enumerate(enumerator, &attribute))
@@ -230,6 +236,7 @@ cp_payload_t *cp_payload_create_type(config_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -242,7 +249,7 @@ cp_payload_t *cp_payload_create_type(config_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = CP_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .attributes = linked_list_create(),
                .type = type,
        );
index afae609..bc6bdee 100644 (file)
@@ -31,11 +31,6 @@ typedef struct cp_payload_t cp_payload_t;
 #include <utils/enumerator.h>
 
 /**
- * CP_PAYLOAD length in bytes without any proposal substructure.
- */
-#define CP_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Config Type of an Configuration Payload.
  */
 enum config_type_t {
index b396135..5f0af8c 100644 (file)
@@ -152,6 +152,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_delete_payload_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_payload_type, payload_type_t,
        private_delete_payload_t *this)
 {
@@ -258,6 +264,7 @@ delete_payload_t *delete_payload_create(protocol_id_t protocol_id)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -270,7 +277,7 @@ delete_payload_t *delete_payload_create(protocol_id_t protocol_id)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = DELETE_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .protocol_id = protocol_id,
                .spi_size = protocol_id == PROTO_AH || protocol_id == PROTO_ESP ? 4 : 0,
        );
index 026829f..ebafe04 100644 (file)
@@ -29,11 +29,6 @@ typedef struct delete_payload_t delete_payload_t;
 #include <encoding/payloads/proposal_substructure.h>
 
 /**
- * Length of a delete payload without the SPI in bytes.
- */
-#define DELETE_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Class representing an IKEv2 DELETE payload.
  *
  * The DELETE payload format is described in RFC section 3.11.
index 5815474..47a9b16 100644 (file)
@@ -150,6 +150,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_eap_payload_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_payload_type, payload_type_t,
        private_eap_payload_t *this)
 {
@@ -251,6 +257,7 @@ eap_payload_t *eap_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -265,7 +272,7 @@ eap_payload_t *eap_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = EAP_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
        );
        return &this->public;
 }
index 60d9c99..e63db7d 100644 (file)
@@ -29,11 +29,6 @@ typedef struct eap_payload_t eap_payload_t;
 #include <sa/authenticators/eap/eap_method.h>
 
 /**
- * Length of a EAP payload without the EAP Message in bytes.
- */
-#define EAP_PAYLOAD_HEADER_LENGTH 4
-
-/**
  * Class representing an IKEv2 EAP payload.
  *
  * The EAP payload format is described in RFC section 3.16.
index b0b863b..08c64fb 100644 (file)
@@ -122,6 +122,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_encryption_payload_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_encryption_payload_t *this)
 {
@@ -173,7 +179,7 @@ static void compute_length(private_encryption_payload_t *this)
                        length += this->aead->get_icv_size(this->aead);
                }
        }
-       length += ENCRYPTION_PAYLOAD_HEADER_LENGTH;
+       length += get_header_length(this);
        this->payload_length = length;
 }
 
@@ -463,6 +469,7 @@ encryption_payload_t *encryption_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -478,7 +485,7 @@ encryption_payload_t *encryption_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = ENCRYPTION_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .payloads = linked_list_create(),
        );
 
index e99c42f..3cdc54b 100644 (file)
@@ -30,11 +30,6 @@ typedef struct encryption_payload_t encryption_payload_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Encrpytion payload length in bytes without IV and following data.
- */
-#define ENCRYPTION_PAYLOAD_HEADER_LENGTH 4
-
-/**
  * The encryption payload as described in RFC section 3.14.
  */
 struct encryption_payload_t {
index 1d42f15..b94c72b 100644 (file)
@@ -141,6 +141,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_id_payload_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_id_payload_t *this)
 {
@@ -190,6 +196,7 @@ id_payload_t *id_payload_create(payload_type_t payload_type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -200,7 +207,7 @@ id_payload_t *id_payload_create(payload_type_t payload_type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = ID_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .payload_type = payload_type,
        );
        return &this->public;
index 99831f8..c24b8ff 100644 (file)
@@ -30,11 +30,6 @@ typedef struct id_payload_t id_payload_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Length of a id payload without the data in bytes.
- */
-#define ID_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Object representing an IKEv2 ID payload.
  *
  * The ID payload format is described in RFC section 3.5.
index 97c6d8c..54e0465 100644 (file)
@@ -251,6 +251,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_ike_header_t *this)
+{
+       return IKE_HEADER_LENGTH;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_ike_header_t *this)
 {
@@ -438,6 +444,7 @@ ike_header_t *ike_header_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
index 183e722..a6e5217 100644 (file)
@@ -157,6 +157,16 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings_v1);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_ke_payload_t *this)
+{
+       if (this->type == KEY_EXCHANGE)
+       {
+               return 8;
+       }
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_ke_payload_t *this)
 {
@@ -212,6 +222,7 @@ ke_payload_t *ke_payload_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -223,14 +234,10 @@ ke_payload_t *ke_payload_create(payload_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = KE_PAYLOAD_HEADER_LENGTH,
                .dh_group_number = MODP_NONE,
                .type = type,
        );
-       if (type == KEY_EXCHANGE_V1)
-       {
-               this->payload_length = KE_PAYLOAD_V1_HEADER_LENGTH;
-       }
+       this->payload_length = get_header_length(this);
        return &this->public;
 }
 
index c448cef..5942954 100644 (file)
@@ -31,16 +31,6 @@ typedef struct ke_payload_t ke_payload_t;
 #include <crypto/diffie_hellman.h>
 
 /**
- * KE payload length in bytes without any key exchange data (IKEv2).
- */
-#define KE_PAYLOAD_HEADER_LENGTH 8
-
-/**
- * KE payload length in bytes without any key exchange data (IKEv1).
- */
-#define KE_PAYLOAD_V1_HEADER_LENGTH 4
-
-/**
  * Class representing an IKEv1 or IKEv2 key exchange payload.
  */
 struct ke_payload_t {
index 86222c1..1e0cf55 100644 (file)
@@ -117,6 +117,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_nonce_payload_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_nonce_payload_t *this)
 {
@@ -145,7 +151,7 @@ METHOD(nonce_payload_t, set_nonce, void,
         private_nonce_payload_t *this, chunk_t nonce)
 {
        this->nonce = chunk_clone(nonce);
-       this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
+       this->payload_length = get_header_length(this) + nonce.len;
 }
 
 METHOD(nonce_payload_t, get_nonce, chunk_t,
@@ -173,6 +179,7 @@ nonce_payload_t *nonce_payload_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -184,7 +191,7 @@ nonce_payload_t *nonce_payload_create(payload_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = NONCE_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .type = type,
        );
        return &this->public;
index 3be2e2e..5c47f5f 100644 (file)
@@ -33,11 +33,6 @@ typedef struct nonce_payload_t nonce_payload_t;
 #define NONCE_SIZE 32
 
 /**
- * Length of a nonce payload without a nonce in bytes.
- */
-#define NONCE_PAYLOAD_HEADER_LENGTH 4
-
-/**
  * Object representing an IKEv1/IKEv2 Nonce payload.
  */
 struct nonce_payload_t {
index d8f45b9..f2e1267 100644 (file)
@@ -430,6 +430,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_notify_payload_t *this)
+{
+       return 8 + this->spi_size;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_notify_payload_t *this)
 {
@@ -451,19 +457,9 @@ METHOD(payload_t, set_next_type, void,
 /**
  * recompute the payloads length.
  */
-static void compute_length (private_notify_payload_t *this)
+static void compute_length(private_notify_payload_t *this)
 {
-       size_t length = NOTIFY_PAYLOAD_HEADER_LENGTH;
-
-       if (this->notification_data.ptr != NULL)
-       {
-               length += this->notification_data.len;
-       }
-       if (this->spi.ptr != NULL)
-       {
-               length += this->spi.len;
-       }
-       this->payload_length = length;
+       this->payload_length = get_header_length(this) + this->notification_data.len;
 }
 
 METHOD(payload_t, get_length, size_t,
@@ -565,6 +561,7 @@ notify_payload_t *notify_payload_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -582,8 +579,8 @@ notify_payload_t *notify_payload_create()
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = NOTIFY_PAYLOAD_HEADER_LENGTH,
        );
+       compute_length(this);
        return &this->public;
 }
 
index 35feb48..91a3cac 100644 (file)
@@ -33,11 +33,6 @@ typedef struct notify_payload_t notify_payload_t;
 #include <utils/linked_list.h>
 
 /**
- * Notify payload length in bytes without any spi and notification data.
- */
-#define NOTIFY_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Notify message types.
  *
  * See IKEv2 RFC 3.10.1.
index 963c5f0..0060e37 100644 (file)
@@ -277,6 +277,13 @@ struct payload_t {
        int (*get_encoding_rules) (payload_t *this, encoding_rule_t **rules);
 
        /**
+        * Get non-variable header length for a variable length payload.
+        *
+        * @return                              fixed length of the payload
+        */
+       int (*get_header_length)(payload_t *this);
+
+       /**
         * Get type of payload.
         *
         * @return                              type of this payload
index 3b663da..16d5794 100644 (file)
@@ -308,6 +308,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings_v1);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_proposal_substructure_t *this)
+{
+       return 8 + this->spi_size;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_proposal_substructure_t *this)
 {
@@ -334,7 +340,7 @@ static void compute_length(private_proposal_substructure_t *this)
        payload_t *transform;
 
        this->transforms_count = 0;
-       this->proposal_length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->spi.len;
+       this->proposal_length = get_header_length(this);
        enumerator = this->transforms->create_enumerator(this->transforms);
        while (enumerator->enumerate(enumerator, &transform))
        {
@@ -692,6 +698,7 @@ proposal_substructure_t *proposal_substructure_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -710,10 +717,10 @@ proposal_substructure_t *proposal_substructure_create(payload_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .proposal_length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH,
                .transforms = linked_list_create(),
                .type = type,
        );
+       compute_length(this);
 
        return &this->public;
 }
index 86ccd5b..72bbdd6 100644 (file)
@@ -30,12 +30,6 @@ typedef struct proposal_substructure_t proposal_substructure_t;
 #include <config/proposal.h>
 #include <utils/linked_list.h>
 
-
-/**
- * Length of the proposal substructure header (without spi).
- */
-#define PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH 8
-
 /**
  * Class representing an IKEv1/IKEv2 proposal substructure.
  */
index 32251e2..66e994b 100644 (file)
@@ -214,6 +214,16 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings_v2);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_sa_payload_t *this)
+{
+       if (this->type == SECURITY_ASSOCIATION_V1)
+       {
+               return 12;
+       }
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_sa_payload_t *this)
 {
@@ -239,21 +249,15 @@ static void compute_length(private_sa_payload_t *this)
 {
        enumerator_t *enumerator;
        payload_t *current;
-       size_t length = SA_PAYLOAD_HEADER_LENGTH;
 
-       if (this->type == SECURITY_ASSOCIATION_V1)
-       {
-               length = SA_PAYLOAD_V1_HEADER_LENGTH;
-       }
+       this->payload_length = get_header_length(this);
 
        enumerator = this->proposals->create_enumerator(this->proposals);
        while (enumerator->enumerate(enumerator, (void **)&current))
        {
-               length += current->get_length(current);
+               this->payload_length += current->get_length(current);
        }
        enumerator->destroy(enumerator);
-
-       this->payload_length = length;
 }
 
 METHOD(payload_t, get_length, size_t,
@@ -367,6 +371,7 @@ sa_payload_t *sa_payload_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
index 11fc774..16d462e 100644 (file)
@@ -30,16 +30,6 @@ typedef struct sa_payload_t sa_payload_t;
 #include <utils/linked_list.h>
 
 /**
- * SECURITY_ASSOCIATION length in bytes without any proposal substructure.
- */
-#define SA_PAYLOAD_HEADER_LENGTH 4
-
-/**
- * SECURITY_ASSOCIATION_V1 length in bytes without any proposal substructure.
- */
-#define SA_PAYLOAD_V1_HEADER_LENGTH 12
-
-/**
  * Class representing an IKEv1 or IKEv2 SA Payload.
  *
  * The SA Payload format is described in RFC section 3.3.
index c213441..378f5bb 100644 (file)
@@ -155,6 +155,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_traffic_selector_substructure_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_traffic_selector_substructure_t *this)
 {
@@ -207,6 +213,7 @@ traffic_selector_substructure_t *traffic_selector_substructure_create()
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -216,7 +223,7 @@ traffic_selector_substructure_t *traffic_selector_substructure_create()
                        .get_traffic_selector = _get_traffic_selector,
                        .destroy = _destroy,
                },
-               .payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                /* must be set to be valid */
                .ts_type = TS_IPV4_ADDR_RANGE,
        );
@@ -238,7 +245,7 @@ traffic_selector_substructure_t *traffic_selector_substructure_create_from_traff
        this->end_port = ts->get_to_port(ts);
        this->starting_address = chunk_clone(ts->get_from_address(ts));
        this->ending_address = chunk_clone(ts->get_to_address(ts));
-       this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH +
+       this->payload_length = get_header_length(this) +
                                                this->ending_address.len + this->starting_address.len;
 
        return &this->public;
index 0109fd7..1ad5fb5 100644 (file)
@@ -30,11 +30,6 @@ typedef struct traffic_selector_substructure_t traffic_selector_substructure_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Length of a TRAFFIC SELECTOR SUBSTRUCTURE without start and end address.
- */
-#define TRAFFIC_SELECTOR_HEADER_LENGTH 8
-
-/**
  * Class representing an IKEv2 TRAFFIC SELECTOR.
  *
  * The TRAFFIC SELECTOR format is described in RFC section 3.13.1.
index fa34401..474362f 100644 (file)
@@ -141,6 +141,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_transform_attribute_t *this)
+{
+       return 0;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_transform_attribute_t *this)
 {
@@ -258,6 +264,7 @@ transform_attribute_t *transform_attribute_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
index da022ca..63464e9 100644 (file)
@@ -174,6 +174,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings_v1);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_transform_substructure_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_transform_substructure_t *this)
 {
@@ -194,7 +200,7 @@ static void compute_length(private_transform_substructure_t *this)
        enumerator_t *enumerator;
        payload_t *attribute;
 
-       this->transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
+       this->transform_length = get_header_length(this);
        enumerator = this->attributes->create_enumerator(this->attributes);
        while (enumerator->enumerate(enumerator, &attribute))
        {
@@ -269,6 +275,7 @@ transform_substructure_t *transform_substructure_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -283,7 +290,7 @@ transform_substructure_t *transform_substructure_create(payload_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .transform_length = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH,
+               .transform_length = get_header_length(this),
                .attributes = linked_list_create(),
                .type = type,
        );
index 7b10712..947df24 100644 (file)
@@ -40,11 +40,6 @@ typedef struct transform_substructure_t transform_substructure_t;
 #define TRANSFORM_TYPE_VALUE 3
 
 /**
- * Length of the transform substructure header in bytes.
- */
-#define TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH 8
-
-/**
  * Class representing an IKEv1/IKEv2 transform substructure.
  */
 struct transform_substructure_t {
index 8f4508a..df47b88 100644 (file)
@@ -152,6 +152,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_ts_payload_t *this)
+{
+       return 8;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_ts_payload_t *this)
 {
@@ -182,7 +188,7 @@ static void compute_length(private_ts_payload_t *this)
        enumerator_t *enumerator;
        payload_t *subst;
 
-       this->payload_length = TS_PAYLOAD_HEADER_LENGTH;
+       this->payload_length = get_header_length(this);
        this->ts_num = 0;
        enumerator = this->substrs->create_enumerator(this->substrs);
        while (enumerator->enumerate(enumerator, &subst))
@@ -250,6 +256,7 @@ ts_payload_t *ts_payload_create(bool is_initiator)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -262,7 +269,7 @@ ts_payload_t *ts_payload_create(bool is_initiator)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = TS_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .is_initiator = is_initiator,
                .substrs = linked_list_create(),
        );
index 88ca00b..5a92655 100644 (file)
@@ -31,11 +31,6 @@ typedef struct ts_payload_t ts_payload_t;
 #include <encoding/payloads/traffic_selector_substructure.h>
 
 /**
- * Length of a TS payload without the Traffic selectors.
- */
-#define TS_PAYLOAD_HEADER_LENGTH 8
-
-/**
  * Class representing an IKEv2 TS payload.
  *
  * The TS payload format is described in RFC section 3.13.
index 9f5c6ef..29dfa9d 100644 (file)
@@ -102,10 +102,6 @@ static encoding_rule_t encodings[] = {
 METHOD(payload_t, verify, status_t,
        private_unknown_payload_t *this)
 {
-       if (this->payload_length != UNKNOWN_PAYLOAD_HEADER_LENGTH + this->data.len)
-       {
-               return FAILED;
-       }
        return SUCCESS;
 }
 
@@ -116,6 +112,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_unknown_payload_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_payload_type, payload_type_t,
        private_unknown_payload_t *this)
 {
@@ -171,6 +173,7 @@ unknown_payload_t *unknown_payload_create(payload_type_t type)
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -182,7 +185,7 @@ unknown_payload_t *unknown_payload_create(payload_type_t type)
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH,
+               .payload_length = get_header_length(this),
                .type = type,
        );
 
@@ -201,7 +204,7 @@ unknown_payload_t *unknown_payload_create_data(payload_type_t type,
        this = (private_unknown_payload_t*)unknown_payload_create(type);
        this->data = data;
        this->critical = critical;
-       this->payload_length = UNKNOWN_PAYLOAD_HEADER_LENGTH + data.len;
+       this->payload_length = get_header_length(this) + data.len;
 
        return &this->public;
 }
index 5ae8533..326b550 100644 (file)
@@ -28,11 +28,6 @@ typedef struct unknown_payload_t unknown_payload_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Header length of the unknown payload.
- */
-#define UNKNOWN_PAYLOAD_HEADER_LENGTH 4
-
-/**
  * Payload which can't be processed further.
  *
  * When the parser finds an unknown payload, he builds an instance of
index 4566987..8a1d997 100644 (file)
@@ -112,6 +112,12 @@ METHOD(payload_t, get_encoding_rules, int,
        return countof(encodings);
 }
 
+METHOD(payload_t, get_header_length, int,
+       private_vendor_id_payload_t *this)
+{
+       return 4;
+}
+
 METHOD(payload_t, get_type, payload_type_t,
        private_vendor_id_payload_t *this)
 {
@@ -162,6 +168,7 @@ vendor_id_payload_t *vendor_id_payload_create_data(payload_type_t type,
                        .payload_interface = {
                                .verify = _verify,
                                .get_encoding_rules = _get_encoding_rules,
+                               .get_header_length = _get_header_length,
                                .get_length = _get_length,
                                .get_next_type = _get_next_type,
                                .set_next_type = _set_next_type,
@@ -172,7 +179,7 @@ vendor_id_payload_t *vendor_id_payload_create_data(payload_type_t type,
                        .destroy = _destroy,
                },
                .next_payload = NO_PAYLOAD,
-               .payload_length = VENDOR_ID_PAYLOAD_HEADER_LENGTH + data.len,
+               .payload_length = get_header_length(this) + data.len,
                .data = data,
                .type = type,
        );
index 4943af4..9a81477 100644 (file)
@@ -28,11 +28,6 @@ typedef struct vendor_id_payload_t vendor_id_payload_t;
 #include <encoding/payloads/payload.h>
 
 /**
- * Length of a VENDOR ID payload without the VID data in bytes.
- */
-#define VENDOR_ID_PAYLOAD_HEADER_LENGTH 4
-
-/**
  * Class representing an IKEv1/IKEv2 VENDOR ID payload.
  *
  * The VENDOR ID payload format is described in RFC section 3.12.
index 2f02490..9e4fddf 100644 (file)
@@ -38,6 +38,8 @@
 #define BLOCK_THRESHOLD_DEFAULT 5
 /** length of the secret to use for cookie calculation */
 #define SECRET_LENGTH 16
+/** Length of a notify payload header */
+#define NOTIFY_PAYLOAD_HEADER_LENGTH 8
 
 typedef struct private_receiver_t private_receiver_t;