4 * @brief Implementation of eap_payload_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "eap_payload.h"
25 #include <utils/allocator.h>
28 typedef struct private_eap_payload_t private_eap_payload_t
;
31 * Private data of an eap_payload_t object.
34 struct private_eap_payload_t
{
36 * Public eap_payload_t interface.
43 u_int8_t next_payload
;
51 * Length of this payload.
53 u_int16_t payload_length
;
56 * The contained message.
62 * Encoding rules to parse or generate a EAP payload.
64 * The defined offsets are the positions in a object of type
65 * private_eap_payload_t.
68 encoding_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 */
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
) }
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 * Implementation of payload_t.verify.
102 static status_t
verify(private_eap_payload_t
*this)
106 /* critical bit is set! */
113 * Implementation of eap_payload_t.get_encoding_rules.
115 static void get_encoding_rules(private_eap_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
117 *rules
= eap_payload_encodings
;
118 *rule_count
= sizeof(eap_payload_encodings
) / sizeof(encoding_rule_t
);
122 * Implementation of payload_t.get_type.
124 static payload_type_t
get_payload_type(private_eap_payload_t
*this)
126 return EXTENSIBLE_AUTHENTICATION
;
130 * Implementation of payload_t.get_next_type.
132 static payload_type_t
get_next_type(private_eap_payload_t
*this)
134 return (this->next_payload
);
138 * Implementation of payload_t.set_next_type.
140 static void set_next_type(private_eap_payload_t
*this,payload_type_t type
)
142 this->next_payload
= type
;
146 * Implementation of payload_t.get_length.
148 static size_t get_length(private_eap_payload_t
*this)
150 return this->payload_length
;
154 * Implementation of eap_payload_t.set_message.
156 static void set_message (private_eap_payload_t
*this, chunk_t message
)
158 if (this->message
.ptr
!= NULL
)
160 allocator_free_chunk(&(this->message
));
162 this->message
.ptr
= allocator_clone_bytes(message
.ptr
,message
.len
);
163 this->message
.len
= message
.len
;
164 this->payload_length
= EAP_PAYLOAD_HEADER_LENGTH
+ this->message
.len
;
168 * Implementation of eap_payload_t.get_message.
170 static chunk_t
get_message (private_eap_payload_t
*this)
172 return (this->message
);
176 * Implementation of eap_payload_t.get_data_clone.
178 static chunk_t
get_message_clone (private_eap_payload_t
*this)
180 chunk_t cloned_message
;
181 if (this->message
.ptr
== NULL
)
183 return (this->message
);
185 cloned_message
.ptr
= allocator_clone_bytes(this->message
.ptr
,this->message
.len
);
186 cloned_message
.len
= this->message
.len
;
187 return cloned_message
;
191 * Implementation of payload_t.destroy and eap_payload_t.destroy.
193 static void destroy(private_eap_payload_t
*this)
195 if (this->message
.ptr
!= NULL
)
197 allocator_free_chunk(&(this->message
));
200 allocator_free(this);
204 * Described in header
206 eap_payload_t
*eap_payload_create()
208 private_eap_payload_t
*this = allocator_alloc_thing(private_eap_payload_t
);
210 /* interface functions */
211 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
212 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
213 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
214 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
215 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
216 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_payload_type
;
217 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
219 /* public functions */
220 this->public.destroy
= (void (*) (eap_payload_t
*)) destroy
;
221 this->public.set_message
= (void (*) (eap_payload_t
*,chunk_t
)) set_message
;
222 this->public.get_message_clone
= (chunk_t (*) (eap_payload_t
*)) get_message_clone
;
223 this->public.get_message
= (chunk_t (*) (eap_payload_t
*)) get_message
;
225 /* private variables */
226 this->critical
= FALSE
;
227 this->next_payload
= NO_PAYLOAD
;
228 this->payload_length
= EAP_PAYLOAD_HEADER_LENGTH
;
229 this->message
= CHUNK_INITIALIZER
;
231 return (&(this->public));