2 * @file nonce_payload.h
4 * @brief Implementation of nonce_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
26 #include "nonce_payload.h"
28 #include <encoding/payloads/encodings.h>
31 typedef struct private_nonce_payload_t private_nonce_payload_t
;
34 * Private data of an nonce_payload_t object.
37 struct private_nonce_payload_t
{
39 * Public nonce_payload_t interface.
41 nonce_payload_t
public;
46 u_int8_t next_payload
;
54 * Length of this payload.
56 u_int16_t payload_length
;
59 * The contained nonce value.
64 * @brief Computes the length of this payload.
66 * @param this calling private_nonce_payload_t object
68 void (*compute_length
) (private_nonce_payload_t
*this);
72 * Encoding rules to parse or generate a nonce payload
74 * The defined offsets are the positions in a object of type
75 * private_nonce_payload_t.
78 encoding_rule_t nonce_payload_encodings
[] = {
79 /* 1 Byte next payload type, stored in the field next_payload */
80 { U_INT_8
, offsetof(private_nonce_payload_t
, next_payload
) },
81 /* the critical bit */
82 { FLAG
, offsetof(private_nonce_payload_t
, critical
) },
83 /* 7 Bit reserved bits, nowhere stored */
91 /* Length of the whole nonce payload*/
92 { PAYLOAD_LENGTH
, offsetof(private_nonce_payload_t
, payload_length
) },
93 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
94 { NONCE_DATA
, offsetof(private_nonce_payload_t
, nonce
) }
98 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
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 ! Next Payload !C! RESERVED ! Payload Length !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * Implementation of payload_t.verify.
111 static status_t
verify(private_nonce_payload_t
*this)
113 if ((this->nonce
.len
< 16) || ((this->nonce
.len
> 256)))
115 /* nonce length is wrong */
123 * Implementation of nonce_payload_t.set_nonce.
125 static status_t
set_nonce(private_nonce_payload_t
*this, chunk_t nonce
)
127 this->nonce
.ptr
= clalloc(nonce
.ptr
, nonce
.len
);
128 this->nonce
.len
= nonce
.len
;
129 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
+ nonce
.len
;
134 * Implementation of nonce_payload_t.get_nonce.
136 static chunk_t
get_nonce(private_nonce_payload_t
*this)
139 nonce
.ptr
= clalloc(this->nonce
.ptr
,this->nonce
.len
);
140 nonce
.len
= this->nonce
.len
;
145 * Implementation of nonce_payload_t.get_encoding_rules.
147 static void get_encoding_rules(private_nonce_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
149 *rules
= nonce_payload_encodings
;
150 *rule_count
= sizeof(nonce_payload_encodings
) / sizeof(encoding_rule_t
);
154 * Implementation of payload_t.get_type.
156 static payload_type_t
get_type(private_nonce_payload_t
*this)
162 * Implementation of payload_t.get_next_type.
164 static payload_type_t
get_next_type(private_nonce_payload_t
*this)
166 return (this->next_payload
);
170 * Implementation of payload_t.set_next_type.
172 static void set_next_type(private_nonce_payload_t
*this,payload_type_t type
)
174 this->next_payload
= type
;
178 * Implementation of payload_t.get_length.
180 static size_t get_length(private_nonce_payload_t
*this)
182 this->compute_length(this);
183 return this->payload_length
;
187 * Implementation of private_id_payload_t.compute_length.
189 static void compute_length(private_nonce_payload_t
*this)
191 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
+ this->nonce
.len
;
195 * Implementation of payload_t.destroy and nonce_payload_t.destroy.
197 static void destroy(private_nonce_payload_t
*this)
199 if (this->nonce
.ptr
!= NULL
)
201 free(this->nonce
.ptr
);
208 * Described in header
210 nonce_payload_t
*nonce_payload_create()
212 private_nonce_payload_t
*this = malloc_thing(private_nonce_payload_t
);
214 /* interface functions */
215 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
216 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
217 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
218 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
219 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
220 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
221 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
223 /* public functions */
224 this->public.destroy
= (void (*) (nonce_payload_t
*)) destroy
;
225 this->public.set_nonce
= (void (*) (nonce_payload_t
*,chunk_t
)) set_nonce
;
226 this->public.get_nonce
= (chunk_t (*) (nonce_payload_t
*)) get_nonce
;
228 /* private functions */
229 this->compute_length
= compute_length
;
231 /* private variables */
232 this->critical
= FALSE
;
233 this->next_payload
= NO_PAYLOAD
;
234 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
;
235 this->nonce
.ptr
= NULL
;
238 return (&(this->public));