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