- payload ike_header cleaned
[strongswan.git] / Source / charon / payloads / ike_header.c
1 /**
2 * @file ike_header.c
3 *
4 * @brief Definition of the encoding rules used when parsing or generating
5 * an IKEv2-Header
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 /* offsetof macro */
25 #include <stddef.h>
26
27 #include "ike_header.h"
28
29 #include "encodings.h"
30 #include "../utils/allocator.h"
31
32 /**
33 * Encoding rules to parse or generate a IKEv2-Header
34 *
35 * The defined offsets are the positions in a object of type
36 * ike_header_t.
37 *
38 */
39 encoding_rule_t ike_header_encodings[] = {
40 /* 8 Byte SPI, stored in the field initiator_spi */
41 { U_INT_64, offsetof(ike_header_t, initiator_spi) },
42 /* 8 Byte SPI, stored in the field responder_spi */
43 { U_INT_64, offsetof(ike_header_t, responder_spi) },
44 /* 1 Byte next payload type, stored in the field next_payload */
45 { U_INT_8, offsetof(ike_header_t, next_payload) },
46 /* 4 Bit major version, stored in the field maj_version */
47 { U_INT_4, offsetof(ike_header_t, maj_version) },
48 /* 4 Bit minor version, stored in the field min_version */
49 { U_INT_4, offsetof(ike_header_t, min_version) },
50 /* 8 Bit for the exchange type */
51 { U_INT_8, offsetof(ike_header_t, exchange_type) },
52 /* 2 Bit reserved bits, nowhere stored */
53 { RESERVED_BIT, 0 },
54 { RESERVED_BIT, 0 },
55 /* 3 Bit flags, stored in the fields response, version and initiator */
56 { FLAG, offsetof(ike_header_t, flags.response) },
57 { FLAG, offsetof(ike_header_t, flags.version) },
58 { FLAG, offsetof(ike_header_t, flags.initiator) },
59 /* 3 Bit reserved bits, nowhere stored */
60 { RESERVED_BIT, 0 },
61 { RESERVED_BIT, 0 },
62 { RESERVED_BIT, 0 },
63 /* 4 Byte message id, stored in the field message_id */
64 { U_INT_32, offsetof(ike_header_t, message_id) },
65 /* 4 Byte length fied, stored in the field length */
66 { LENGTH, offsetof(ike_header_t, length) }
67 };
68
69
70 /**
71 * Implements payload_t's and ike_header_t's destroy function.
72 * See #payload_s.destroy or ike_header_s.destroy for description.
73 */
74 static status_t destroy(ike_header_t *this)
75 {
76 allocator_free(this);
77
78 return SUCCESS;
79 }
80
81 /**
82 * Implements payload_t's get_encoding_rules function.
83 * See #payload_s.get_encoding_rules for description.
84 */
85 static status_t get_encoding_rules(payload_t *this, encoding_rule_t **rules, size_t *rule_count)
86 {
87 *rules = ike_header_encodings;
88 *rule_count = sizeof(ike_header_encodings) / sizeof(encoding_rule_t);
89
90 return SUCCESS;
91 }
92
93 /**
94 * Implements payload_t's get_type function.
95 * See #payload_s.get_type for description.
96 */
97 static payload_type_t get_type(payload_t *this)
98 {
99 return HEADER;
100 }
101
102 /**
103 * Implements payload_t's get_next_type function.
104 * See #payload_s.get_next_type for description.
105 */
106 static payload_type_t get_next_type(payload_t *this)
107 {
108 return (((ike_header_t*)this)->next_payload);
109 }
110
111 /**
112 * Implements payload_t's get_length function.
113 * See #payload_s.get_length for description.
114 */
115 static size_t get_length(payload_t *this)
116 {
117 return sizeof(ike_header_t);
118 }
119
120 /*
121 * Described in header
122 */
123 ike_header_t *ike_header_create()
124 {
125 ike_header_t *this = allocator_alloc_thing(ike_header_t);
126 if (this == NULL)
127 {
128 return NULL;
129 }
130
131 this->payload_interface.get_encoding_rules = get_encoding_rules;
132 this->payload_interface.get_length = get_length;
133 this->payload_interface.get_next_type = get_next_type;
134 this->payload_interface.get_type = get_type;
135 this->payload_interface.destroy = (status_t (*) (payload_t *))destroy;
136 this->destroy = destroy;
137
138 /* set default values of the fields */
139 this->initiator_spi = 0;
140 this->responder_spi = 0;
141 this->next_payload = 0;
142 this->maj_version = IKE_MAJOR_VERSION;
143 this->min_version = IKE_MINOR_VERSION;
144 this->exchange_type = NOT_SET;
145 this->flags.initiator = TRUE;
146 this->flags.version = HIGHER_VERSION_SUPPORTED_FLAG;
147 this->flags.response = FALSE;
148 this->message_id = 0;
149 this->length = IKE_HEADER_LENGTH;
150
151
152 return this;
153 }
154
155