- skeleton implementation of ike_header
[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 struct 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 status_t destroy(payload_t *this)
72 {
73 allocator_free(this);
74
75 return SUCCESS;
76 }
77
78 status_t get_encoding_rules(payload_t *this, encoding_rule_t **rules, size_t *rule_count)
79 {
80 *rules = ike_header_encodings;
81 *rule_count = sizeof(ike_header_encodings) / sizeof(encoding_rule_t);
82
83 return SUCCESS;
84 }
85
86 payload_type_t get_type(payload_t *this)
87 {
88 return HEADER;
89 }
90
91 payload_type_t get_next_type(payload_t *this)
92 {
93 return (((ike_header_t*)this)->next_payload);
94 }
95
96 size_t get_length(payload_t *this)
97 {
98 return sizeof(ike_header_t);
99 }
100
101
102 ike_header_t *ike_header_create()
103 {
104 ike_header_t *this = allocator_alloc_thing(ike_header_t);
105 if (this == NULL)
106 {
107 return NULL;
108 }
109
110 this->payload_interface.get_encoding_rules = get_encoding_rules;
111 this->payload_interface.get_length = get_length;
112 this->payload_interface.get_next_type = get_next_type;
113 this->payload_interface.get_type = get_type;
114 this->payload_interface.destroy = destroy;
115
116 return this;
117 }
118
119