Introduced payload types sa_payload and proposal_substructure
[strongswan.git] / Source / charon / payloads / sa_payload.c
1 /**
2 * @file sa_payload.h
3 *
4 * @brief Declaration of the class sa_payload_t.
5 *
6 * An object of this type represents an IKEv2 SA-Payload and contains proposal
7 * substructures.
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 "sa_payload.h"
30
31 #include "encodings.h"
32 #include "../utils/allocator.h"
33 #include "../utils/linked_list.h"
34
35
36 /**
37 * Private data of an sa_payload_t' Object
38 *
39 */
40 typedef struct private_sa_payload_s private_sa_payload_t;
41
42 struct private_sa_payload_s {
43 /**
44 * public sa_payload_t interface
45 */
46 sa_payload_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53 /**
54 * Critical flag
55 */
56 bool critical;
57
58 /**
59 * Length of this payload
60 */
61 u_int16_t payload_length;
62
63 /**
64 * Proposals in this payload are stored in a linked_list_t
65 */
66 linked_list_t * proposals;
67 };
68
69 /**
70 * Encoding rules to parse or generate a IKEv2-Header
71 *
72 * The defined offsets are the positions in a object of type
73 * private_sa_payload_t.
74 *
75 */
76 encoding_rule_t sa_payload_encodings[] = {
77 /* 1 Byte next payload type, stored in the field next_payload */
78 { U_INT_8, offsetof(private_sa_payload_t, next_payload) },
79 /* the critical bit */
80 { FLAG, offsetof(private_sa_payload_t, critical) },
81 /* 7 Bit reserved bits, nowhere stored */
82 { RESERVED_BIT, 0 },
83 { RESERVED_BIT, 0 },
84 { RESERVED_BIT, 0 },
85 { RESERVED_BIT, 0 },
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 /* Length of the whole SA payload*/
90 { PAYLOAD_LENGTH, offsetof(private_sa_payload_t, payload_length) },
91 /* Proposals are stored in a proposal substructure,
92 offset points to a linked_list_t pointer */
93 { PROPOSALS, offsetof(private_sa_payload_t, proposals) }
94 };
95
96 /**
97 * Implements payload_t's and sa_payload_t's destroy function.
98 * See #payload_s.destroy or sa_payload_s.destroy for description.
99 */
100 static status_t destroy(private_sa_payload_t *this)
101 {
102 /* all proposals are getting destroyed */
103 while (this->proposals->get_count(this->proposals) > 0)
104 {
105 proposal_substructure_t *current_proposal;
106 if (this->proposals->remove_last(this->proposals,(void **)&current_proposal) != SUCCESS)
107 {
108 break;
109 }
110 current_proposal->destroy(current_proposal);
111 }
112 this->proposals->destroy(this->proposals);
113
114 allocator_free(this);
115
116 return SUCCESS;
117 }
118
119 /**
120 * Implements payload_t's get_encoding_rules function.
121 * See #payload_s.get_encoding_rules for description.
122 */
123 static status_t get_encoding_rules(private_sa_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
124 {
125 *rules = sa_payload_encodings;
126 *rule_count = sizeof(sa_payload_encodings) / sizeof(encoding_rule_t);
127
128 return SUCCESS;
129 }
130
131 /**
132 * Implements payload_t's get_type function.
133 * See #payload_s.get_type for description.
134 */
135 static payload_type_t get_type(private_sa_payload_t *this)
136 {
137 return SECURITY_ASSOCIATION;
138 }
139
140 /**
141 * Implements payload_t's get_next_type function.
142 * See #payload_s.get_next_type for description.
143 */
144 static payload_type_t get_next_type(private_sa_payload_t *this)
145 {
146 return (this->next_payload);
147 }
148
149 /**
150 * Implements payload_t's get_length function.
151 * See #payload_s.get_length for description.
152 */
153 static size_t get_length(private_sa_payload_t *this)
154 {
155 return this->payload_length;
156 }
157
158 /*
159 * Described in header
160 */
161 sa_payload_t *sa_payload_create()
162 {
163 private_sa_payload_t *this = allocator_alloc_thing(private_sa_payload_t);
164 if (this == NULL)
165 {
166 return NULL;
167 }
168
169 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
170 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
171 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
172 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
173 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
174 this->public.destroy = (status_t (*) (sa_payload_t *)) destroy;
175
176 /* set default values of the fields */
177 this->critical = SA_PAYLOAD_CRITICAL_FLAG;
178 this->next_payload = NO_PAYLOAD;
179 this->payload_length = SA_PAYLOAD_HEADER_LENGTH;
180
181 this->proposals = linked_list_create();
182
183 if (this->proposals == NULL)
184 {
185 allocator_free(this);
186 return NULL;
187 }
188 return (&(this->public));
189 }
190
191