]> git.ipfire.org Git - thirdparty/strongswan.git/blame - programs/charon/charon/encoding/payloads/eap_payload.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / charon / encoding / payloads / eap_payload.c
CommitLineData
c81eb6e7
JH
1/**
2 * @file eap_payload.c
3 *
4 * @brief Implementation of eap_payload_t.
5 *
6 */
7
8/*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
5113680f 23#include <stddef.h>
c81eb6e7 24
5113680f 25#include "eap_payload.h"
c81eb6e7
JH
26
27
28typedef struct private_eap_payload_t private_eap_payload_t;
29
30/**
31 * Private data of an eap_payload_t object.
32 *
33 */
34struct private_eap_payload_t {
35 /**
36 * Public eap_payload_t interface.
37 */
38 eap_payload_t public;
39
40 /**
41 * Next payload type.
42 */
43 u_int8_t next_payload;
44
45 /**
46 * Critical flag.
47 */
48 bool critical;
49
50 /**
51 * Length of this payload.
52 */
53 u_int16_t payload_length;
54
55 /**
56 * The contained message.
57 */
58 chunk_t message;
59};
60
61/**
62 * Encoding rules to parse or generate a EAP payload.
63 *
64 * The defined offsets are the positions in a object of type
65 * private_eap_payload_t.
66 *
67 */
68encoding_rule_t eap_payload_encodings[] = {
69 /* 1 Byte next payload type, stored in the field next_payload */
70 { U_INT_8, offsetof(private_eap_payload_t, next_payload) },
71 /* the critical bit */
72 { FLAG, offsetof(private_eap_payload_t, critical) },
73 /* 7 Bit reserved bits, nowhere stored */
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 /* Length of the whole payload*/
82 { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length)},
83 /* some eap data bytes, length is defined in PAYLOAD_LENGTH */
84 { EAP_MESSAGE, offsetof(private_eap_payload_t, message) }
85};
86
87/*
88 1 2 3
89 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
90 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
91 ! Next Payload !C! RESERVED ! Payload Length !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 ! !
94 ~ EAP Message ~
95 ! !
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97*/
98
99/**
100 * Implementation of payload_t.verify.
101 */
102static status_t verify(private_eap_payload_t *this)
103{
c81eb6e7
JH
104 return SUCCESS;
105}
106
107/**
108 * Implementation of eap_payload_t.get_encoding_rules.
109 */
110static void get_encoding_rules(private_eap_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
111{
112 *rules = eap_payload_encodings;
113 *rule_count = sizeof(eap_payload_encodings) / sizeof(encoding_rule_t);
114}
115
116/**
117 * Implementation of payload_t.get_type.
118 */
119static payload_type_t get_payload_type(private_eap_payload_t *this)
120{
121 return EXTENSIBLE_AUTHENTICATION;
122}
123
124/**
125 * Implementation of payload_t.get_next_type.
126 */
127static payload_type_t get_next_type(private_eap_payload_t *this)
128{
129 return (this->next_payload);
130}
131
132/**
133 * Implementation of payload_t.set_next_type.
134 */
135static void set_next_type(private_eap_payload_t *this,payload_type_t type)
136{
137 this->next_payload = type;
138}
139
140/**
141 * Implementation of payload_t.get_length.
142 */
143static size_t get_length(private_eap_payload_t *this)
144{
145 return this->payload_length;
146}
147
148/**
149 * Implementation of eap_payload_t.set_message.
150 */
151static void set_message (private_eap_payload_t *this, chunk_t message)
152{
153 if (this->message.ptr != NULL)
154 {
5113680f 155 chunk_free(&(this->message));
c81eb6e7 156 }
5113680f 157 this->message.ptr = clalloc(message.ptr,message.len);
c81eb6e7
JH
158 this->message.len = message.len;
159 this->payload_length = EAP_PAYLOAD_HEADER_LENGTH + this->message.len;
160}
161
162/**
163 * Implementation of eap_payload_t.get_message.
164 */
165static chunk_t get_message (private_eap_payload_t *this)
166{
167 return (this->message);
168}
169
170/**
171 * Implementation of eap_payload_t.get_data_clone.
172 */
173static chunk_t get_message_clone (private_eap_payload_t *this)
174{
175 chunk_t cloned_message;
176 if (this->message.ptr == NULL)
177 {
178 return (this->message);
179 }
5113680f 180 cloned_message.ptr = clalloc(this->message.ptr,this->message.len);
c81eb6e7
JH
181 cloned_message.len = this->message.len;
182 return cloned_message;
183}
184
185/**
186 * Implementation of payload_t.destroy and eap_payload_t.destroy.
187 */
188static void destroy(private_eap_payload_t *this)
189{
190 if (this->message.ptr != NULL)
191 {
5113680f 192 chunk_free(&(this->message));
c81eb6e7
JH
193 }
194
5113680f 195 free(this);
c81eb6e7
JH
196}
197
198/*
199 * Described in header
200 */
201eap_payload_t *eap_payload_create()
202{
5113680f 203 private_eap_payload_t *this = malloc_thing(private_eap_payload_t);
c81eb6e7
JH
204
205 /* interface functions */
206 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
207 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
208 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
209 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
210 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
211 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
212 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
213
214 /* public functions */
215 this->public.destroy = (void (*) (eap_payload_t *)) destroy;
216 this->public.set_message = (void (*) (eap_payload_t *,chunk_t)) set_message;
217 this->public.get_message_clone = (chunk_t (*) (eap_payload_t *)) get_message_clone;
218 this->public.get_message = (chunk_t (*) (eap_payload_t *)) get_message;
219
220 /* private variables */
221 this->critical = FALSE;
222 this->next_payload = NO_PAYLOAD;
223 this->payload_length = EAP_PAYLOAD_HEADER_LENGTH;
224 this->message = CHUNK_INITIALIZER;
225
226 return (&(this->public));
227}