root / src / charon / encoding / payloads / cp_payload.c @ b5a2055fb1b88ea4abb97334d89e311c9ceaa7d4
History | View | Annotate | Download (7.4 KB)
| 1 | /*
|
|---|---|
| 2 | * Copyright (C) 2005-2009 Martin Willi |
| 3 | * Copyright (C) 2005 Jan Hutter |
| 4 | * Hochschule fuer Technik Rapperswil |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <stddef.h> |
| 18 | |
| 19 | #include "cp_payload.h" |
| 20 | |
| 21 | #include <encoding/payloads/encodings.h> |
| 22 | #include <utils/linked_list.h> |
| 23 | |
| 24 | ENUM(config_type_names, CFG_REQUEST, CFG_ACK, |
| 25 | "CFG_REQUEST",
|
| 26 | "CFG_REPLY",
|
| 27 | "CFG_SET",
|
| 28 | "CFG_ACK",
|
| 29 | ); |
| 30 | |
| 31 | typedef struct private_cp_payload_t private_cp_payload_t; |
| 32 | |
| 33 | /**
|
| 34 | * Private data of an cp_payload_t object. |
| 35 | * |
| 36 | */ |
| 37 | struct private_cp_payload_t {
|
| 38 | /**
|
| 39 | * Public cp_payload_t interface. |
| 40 | */ |
| 41 | cp_payload_t public; |
| 42 | |
| 43 | /**
|
| 44 | * Next payload type. |
| 45 | */ |
| 46 | u_int8_t next_payload; |
| 47 | |
| 48 | /**
|
| 49 | * Critical flag. |
| 50 | */ |
| 51 | bool critical;
|
| 52 | |
| 53 | /**
|
| 54 | * Length of this payload. |
| 55 | */ |
| 56 | u_int16_t payload_length; |
| 57 | |
| 58 | /**
|
| 59 | * List of attributes, as configuration_attribute_t |
| 60 | */ |
| 61 | linked_list_t *attributes; |
| 62 | |
| 63 | /**
|
| 64 | * Config Type. |
| 65 | */ |
| 66 | u_int8_t type; |
| 67 | }; |
| 68 | |
| 69 | /**
|
| 70 | * Encoding rules to parse or generate a IKEv2-CP Payload |
| 71 | * |
| 72 | * The defined offsets are the positions in a object of type |
| 73 | * private_cp_payload_t. |
| 74 | * |
| 75 | */ |
| 76 | encoding_rule_t cp_payload_encodings[] = {
|
| 77 | /* 1 Byte next payload type, stored in the field next_payload */
|
| 78 | { U_INT_8, offsetof(private_cp_payload_t, next_payload) },
|
| 79 | /* the critical bit */
|
| 80 | { FLAG, offsetof(private_cp_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 CP payload*/
|
| 90 | { PAYLOAD_LENGTH, offsetof(private_cp_payload_t, payload_length) },
|
| 91 | /* Proposals are stored in a proposal substructure,
|
| 92 | offset points to a linked_list_t pointer */ |
| 93 | { U_INT_8, offsetof(private_cp_payload_t, type) },
|
| 94 | { RESERVED_BYTE,0 },
|
| 95 | { RESERVED_BYTE,0 },
|
| 96 | { RESERVED_BYTE,0 },
|
| 97 | { CONFIGURATION_ATTRIBUTES, offsetof(private_cp_payload_t, attributes) }
|
| 98 | }; |
| 99 | |
| 100 | /*
|
| 101 | 1 2 3 |
| 102 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 103 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 104 | ! Next Payload !C! RESERVED ! Payload Length ! |
| 105 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 106 | ! CFG Type ! RESERVED ! |
| 107 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 108 | ! ! |
| 109 | ~ Configuration Attributes ~ |
| 110 | ! ! |
| 111 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 112 | */ |
| 113 | |
| 114 | /**
|
| 115 | * Implementation of payload_t.verify. |
| 116 | */ |
| 117 | static status_t verify(private_cp_payload_t *this)
|
| 118 | {
|
| 119 | status_t status = SUCCESS; |
| 120 | enumerator_t *enumerator; |
| 121 | payload_t *attribute; |
| 122 | |
| 123 | enumerator = this->attributes->create_enumerator(this->attributes); |
| 124 | while (enumerator->enumerate(enumerator, &attribute))
|
| 125 | {
|
| 126 | status = attribute->verify(attribute); |
| 127 | if (status != SUCCESS)
|
| 128 | {
|
| 129 | break;
|
| 130 | } |
| 131 | } |
| 132 | enumerator->destroy(enumerator); |
| 133 | return status;
|
| 134 | } |
| 135 | |
| 136 | /**
|
| 137 | * Implementation of payload_t.get_encoding_rules. |
| 138 | */ |
| 139 | static void get_encoding_rules(private_cp_payload_t *this, |
| 140 | encoding_rule_t **rules, size_t *rule_count) |
| 141 | {
|
| 142 | *rules = cp_payload_encodings; |
| 143 | *rule_count = sizeof(cp_payload_encodings) / sizeof(encoding_rule_t); |
| 144 | } |
| 145 | |
| 146 | /**
|
| 147 | * Implementation of payload_t.get_type. |
| 148 | */ |
| 149 | static payload_type_t get_type(private_cp_payload_t *this)
|
| 150 | {
|
| 151 | return CONFIGURATION;
|
| 152 | } |
| 153 | |
| 154 | /**
|
| 155 | * Implementation of payload_t.get_next_type. |
| 156 | */ |
| 157 | static payload_type_t get_next_type(private_cp_payload_t *this)
|
| 158 | {
|
| 159 | return this->next_payload;
|
| 160 | } |
| 161 | |
| 162 | /**
|
| 163 | * Implementation of payload_t.set_next_type. |
| 164 | */ |
| 165 | static void set_next_type(private_cp_payload_t *this,payload_type_t type) |
| 166 | {
|
| 167 | this->next_payload = type; |
| 168 | } |
| 169 | |
| 170 | /**
|
| 171 | * recompute the length of the payload. |
| 172 | */ |
| 173 | static void compute_length(private_cp_payload_t *this) |
| 174 | {
|
| 175 | enumerator_t *enumerator; |
| 176 | payload_t *attribute; |
| 177 | |
| 178 | this->payload_length = CP_PAYLOAD_HEADER_LENGTH; |
| 179 | |
| 180 | enumerator = this->attributes->create_enumerator(this->attributes); |
| 181 | while (enumerator->enumerate(enumerator, &attribute))
|
| 182 | {
|
| 183 | this->payload_length += attribute->get_length(attribute); |
| 184 | } |
| 185 | enumerator->destroy(enumerator); |
| 186 | } |
| 187 | |
| 188 | /**
|
| 189 | * Implementation of payload_t.get_length. |
| 190 | */ |
| 191 | static size_t get_length(private_cp_payload_t *this)
|
| 192 | {
|
| 193 | return this->payload_length;
|
| 194 | } |
| 195 | |
| 196 | /**
|
| 197 | * Implementation of cp_payload_t.create_attribute_enumerator. |
| 198 | */ |
| 199 | static enumerator_t *create_attribute_enumerator(private_cp_payload_t *this)
|
| 200 | {
|
| 201 | return this->attributes->create_enumerator(this->attributes);
|
| 202 | } |
| 203 | |
| 204 | /**
|
| 205 | * Implementation of cp_payload_t.add_attribute. |
| 206 | */ |
| 207 | static void add_attribute(private_cp_payload_t *this, |
| 208 | configuration_attribute_t *attribute) |
| 209 | {
|
| 210 | this->attributes->insert_last(this->attributes, attribute); |
| 211 | compute_length(this); |
| 212 | } |
| 213 | |
| 214 | /**
|
| 215 | * Implementation of cp_payload_t.get_type. |
| 216 | */ |
| 217 | static config_type_t get_config_type(private_cp_payload_t *this)
|
| 218 | {
|
| 219 | return this->type;
|
| 220 | } |
| 221 | |
| 222 | /**
|
| 223 | * Implementation of payload_t.destroy and cp_payload_t.destroy. |
| 224 | */ |
| 225 | static void destroy(private_cp_payload_t *this) |
| 226 | {
|
| 227 | this->attributes->destroy_offset(this->attributes, |
| 228 | offsetof(configuration_attribute_t, destroy)); |
| 229 | free(this); |
| 230 | } |
| 231 | |
| 232 | /*
|
| 233 | * Described in header. |
| 234 | */ |
| 235 | cp_payload_t *cp_payload_create() |
| 236 | {
|
| 237 | private_cp_payload_t *this = malloc_thing(private_cp_payload_t); |
| 238 | |
| 239 | this->public.payload_interface.verify = (status_t (*) (payload_t *))verify; |
| 240 | this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
|
| 241 | this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length; |
| 242 | this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type; |
| 243 | this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
|
| 244 | this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type; |
| 245 | this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
|
| 246 | |
| 247 | this->public.create_attribute_enumerator = (enumerator_t*(*)(cp_payload_t *))create_attribute_enumerator; |
| 248 | this->public.add_attribute = (void (*) (cp_payload_t *,configuration_attribute_t*))add_attribute;
|
| 249 | this->public.get_type = (config_type_t (*) (cp_payload_t *))get_config_type; |
| 250 | this->public.destroy = (void (*)(cp_payload_t *))destroy;
|
| 251 | |
| 252 | /* set default values of the fields */
|
| 253 | this->critical = FALSE; |
| 254 | this->next_payload = NO_PAYLOAD; |
| 255 | this->payload_length = CP_PAYLOAD_HEADER_LENGTH; |
| 256 | this->attributes = linked_list_create(); |
| 257 | this->type = CFG_REQUEST; |
| 258 | |
| 259 | return &this->public;
|
| 260 | } |
| 261 | |
| 262 | /*
|
| 263 | * Described in header. |
| 264 | */ |
| 265 | cp_payload_t *cp_payload_create_type(config_type_t type) |
| 266 | {
|
| 267 | private_cp_payload_t *this = (private_cp_payload_t*)cp_payload_create(); |
| 268 | |
| 269 | this->type = type; |
| 270 | |
| 271 | return &this->public;
|
| 272 | } |
| 273 |