]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
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 2843d60c1d974891c5ffe7423697edeaf35a164a..052a4be08321450d61e95c741e72d30ca1e0f4f2 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 4736873a74a8affb1a07423e033f97583d7ee5d1..719c942549acef1cbaf0018a615fb7a1f768008f 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 3825244e26e31b7c292788ad058250cb79a0a987..57dc58f2a02b2e4184f440595279ebb042cf10d3 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 e4c4e6ae394aa277ee32fd6524396d0869a48627..521fe1d1962f7e19fec09df8d2bd5ed62023c5af 100644 (file)
@@ -28,11 +28,6 @@ typedef struct auth_payload_t auth_payload_t;
 #include <encoding/payloads/payload.h>
 #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.
  *
index 8f6deb89c4c04a2cc35e2afc6f5e5ac0607a0734..565be7f34836124e9ce8da162ab9f8e952846289 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 21b503a402fa3beb0afe7327d48ecb369c5fe717..6eb79c8ed0b81e86a9a72c33b7ec2199e630fb97 100644 (file)
@@ -30,11 +30,6 @@ typedef enum cert_encoding_t cert_encoding_t;
 #include <credentials/certificates/certificate.h>
 #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
  */
index ec46e92777d6bcb5065a84533f183be2c698be4b..dc685e651513c1fde093dccdb42da44ec6ca4f5a 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 914063628fdbd2971c36c4bf15a413bc9d5d28a8..421ad6d58d0cc55dd031dfa4185a36f3db66353a 100644 (file)
@@ -28,11 +28,6 @@ typedef struct certreq_payload_t certreq_payload_t;
 #include <encoding/payloads/payload.h>
 #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.
  *
index a657ef13a3c424939d82336003e00484e48845a2..8746b9b9e929727791a50d4ebe20857d5b59e764 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 6e4b018bb8f263c30f96a35971ab2e3a50928ed3..e216ee366074f10ad7e53f79138342c80221330b 100644 (file)
@@ -28,11 +28,6 @@ typedef struct configuration_attribute_t configuration_attribute_t;
 #include <attributes/attributes.h>
 #include <encoding/payloads/payload.h>
 
-/**
- * Configuration attribute header length in bytes.
- */
-#define CONFIGURATION_ATTRIBUTE_HEADER_LENGTH 4
-
 /**
  * Class representing an IKEv2-CONFIGURATION Attribute.
  *
index 4e782dd8816954e6513521844d70eaf380cb65e6..05f16c33f365b67695443715e297f1e51afd0cba 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 afae6091a619cba9b2f83bc96f45a8c23e8e4a02..bc6bdee50a08d358da225994ac34584f93a9ddf3 100644 (file)
@@ -30,11 +30,6 @@ typedef struct cp_payload_t cp_payload_t;
 #include <encoding/payloads/configuration_attribute.h>
 #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.
  */
index b396135be619be4a08b6b29e224401606ef353ac..5f0af8c861288b043f84c70ca1972b9990a1cde7 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 026829f9714ac33a6321ba505b4444190d225a2d..ebafe04fbf895675cfb2f35c19a6c5d29fa815fa 100644 (file)
@@ -28,11 +28,6 @@ typedef struct delete_payload_t delete_payload_t;
 #include <encoding/payloads/payload.h>
 #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.
  *
index 581547499e2cf77a994d7692d16f1f9f8117d744..47a9b16738a4ef1d5bfdde61222fb33cb49db049 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 60d9c99d2b81424a3d038a0a0ecf9d4e90790e00..e63db7d880e234c07d8b2f5bf05ae4f7fee7599e 100644 (file)
@@ -28,11 +28,6 @@ typedef struct eap_payload_t eap_payload_t;
 #include <encoding/payloads/payload.h>
 #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.
  *
index b0b863b36e06d6da71c5602445c0c18a43f0bc59..08c64fb51e6c67d435590ac14eca90b1525392ef 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 e99c42fb7a622916f9432b15ef818002048bbf7a..3cdc54b37520f9f17eb6d495026839a7346bc9d3 100644 (file)
@@ -29,11 +29,6 @@ typedef struct encryption_payload_t encryption_payload_t;
 #include <crypto/aead.h>
 #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.
  */
index 1d42f1595f550e1f7f6f861868cc31fcc3aabd9b..b94c72b602012e554e99387d9909f3a1a755ab67 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 99831f85fe16d31bb67f1dcd57f9e17fc3e49265..c24b8ffc6fa0fd088bede1edca51546ee829e03f 100644 (file)
@@ -29,11 +29,6 @@ typedef struct id_payload_t id_payload_t;
 #include <utils/identification.h>
 #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.
  *
index 97c6d8c26795911f16b1bc44cd2d5288a5c95857..54e04658ce334b2cb3952b58b95ba7a9d8dd827f 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 183e722a2606a9e47aaa1553cd3de983c2ff5b4c..a6e5217aae8bfdddae4dff9af1b3e902cc5f1adf 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 c448ceff65b403ce317305300228b67d4f36051c..5942954d9973d26568a13f75eb13b0a3de2b69d9 100644 (file)
@@ -30,16 +30,6 @@ typedef struct ke_payload_t ke_payload_t;
 #include <utils/linked_list.h>
 #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.
  */
index 86222c13afd49425b6bbcb7bc04dd9d971f47a64..1e0cf557915b512c147c3659bd197d3112cbbdc3 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 3be2e2e13dc935e22e85d771d7650e6230012d3c..5c47f5f9f9b45cd9281bc4286ec65ff85d35b215 100644 (file)
@@ -32,11 +32,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.
  */
index d8f45b9b48952df830b22c9cc7b854d7a47a0b31..f2e1267c60a72e62daba19ec9b0e7f5cd8e1234a 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 35feb487197c3ec7d055a7b85c8c86bcee7a218e..91a3cac0f39e819ad48e14cb3d0d74a980d2d5cf 100644 (file)
@@ -32,11 +32,6 @@ typedef struct notify_payload_t notify_payload_t;
 #include <encoding/payloads/proposal_substructure.h>
 #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.
  *
index 963c5f09016ed682628daa886a97da94fd851858..0060e373095d4891430791a86eeb5c240f1fab59 100644 (file)
@@ -276,6 +276,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.
         *
index 3b663da86d4901322a347964e465faabb41317de..16d5794aec454397b6638485813f5408e7349a58 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 86ccd5b8be0cafcf2e74cc766c47b624d976d315..72bbdd64f87f92a38caeb3a0040fd797bb6100f1 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 32251e28b451fd388ecc51c46f0dbd3597bc8441..66e994b433998c733461707260df925a86ce29d7 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 11fc774e5648ddfe157c4d7fdcd030b6c37a19fa..16d462e654a7f93ba93128afab36c51f1ee544a9 100644 (file)
@@ -29,16 +29,6 @@ typedef struct sa_payload_t sa_payload_t;
 #include <encoding/payloads/proposal_substructure.h>
 #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.
  *
index c213441ab1cd9e0513c7319281af1901d4badfdb..378f5bbc305598682e765ca699a33942f8083a6f 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 0109fd7f5bf0ba7270b16d6dd35dee74f4b8c44d..1ad5fb526c963c6572125fbf6cae1877d1320e2c 100644 (file)
@@ -29,11 +29,6 @@ typedef struct traffic_selector_substructure_t traffic_selector_substructure_t;
 #include <selectors/traffic_selector.h>
 #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.
  *
index fa344019bdec259b5ec29fcdb2e4b2be301ae731..474362fca208d726db4949a6b54f6880f60b431c 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 da022ca75bf9ac0be7366a959c112aea65cbfc0c..63464e9f312b64fa8b75bb183e886756522655a0 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 7b10712751be4bdd10c8e56ffdb633f95c70b36e..947df24f9b93f6e14660de1eee0ee3f7fab5f6eb 100644 (file)
@@ -39,11 +39,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.
  */
index 8f4508a9caa7553069c4755014da80481cfcc920..df47b8892dbe26089227447a1164fdd07c28cce4 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 88ca00bc9557f1fc038cf618edc9d9fc1c06ec31..5a92655dcee1b895002aba82e3d0707beb262088 100644 (file)
@@ -30,11 +30,6 @@ typedef struct ts_payload_t ts_payload_t;
 #include <encoding/payloads/payload.h>
 #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.
  *
index 9f5c6ef9864ff5a5f410d89a962f8d9a5ea5d7d8..29dfa9da4a2a073b6fdc458bd2050a05c515d711 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 5ae85331bc8c910e4cd6c982981011b910463d1f..326b550cd872cc17f0e25e2f46accab221b2aa25 100644 (file)
@@ -27,11 +27,6 @@ typedef struct unknown_payload_t unknown_payload_t;
 #include <library.h>
 #include <encoding/payloads/payload.h>
 
-/**
- * Header length of the unknown payload.
- */
-#define UNKNOWN_PAYLOAD_HEADER_LENGTH 4
-
 /**
  * Payload which can't be processed further.
  *
index 45669879bf9e3e0e1b1f21192796d45377a0414f..8a1d9970d345016ddda653f2906c22e4ec316aa9 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 4943af436207fda0d61e73c1c8d8863102e82bfd..9a814777b134948c57306740e43bb2f9f336b8cc 100644 (file)
@@ -27,11 +27,6 @@ typedef struct vendor_id_payload_t vendor_id_payload_t;
 #include <library.h>
 #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.
  *
index 2f024900ebeed72c9873f2baf26b17bc70bfb093..9e4fddf5aa017d5f5693603dd30d662137dbcf18 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;