41e2e67fb47d1045760da498e6bd64604a8a0410
[strongswan.git] / src / libcharon / plugins / tnccs_20 / messages / pb_assessment_result_message.c
1 /*
2 * Copyright (C) 2010 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pb_assessment_result_message.h"
17
18 #include <tls_writer.h>
19 #include <tls_reader.h>
20 #include <debug.h>
21
22 typedef struct private_pb_assessment_result_message_t private_pb_assessment_result_message_t;
23
24 /**
25 * PB-Assessment-Result message (see section 4.6 of RFC 5793)
26 *
27 * 1 2 3
28 * 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
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * | Assessment Result |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 */
33
34 #define ASSESSMENT_RESULT_MESSAGE_SIZE 4
35
36 /**
37 * Private data of a pb_assessment_result_message_t object.
38 *
39 */
40 struct private_pb_assessment_result_message_t {
41 /**
42 * Public pb_assessment_result_message_t interface.
43 */
44 pb_assessment_result_message_t public;
45
46 /**
47 * PB-TNC message type
48 */
49 pb_tnc_msg_type_t type;
50
51 /**
52 * Assessment result code
53 */
54 u_int32_t assessment_result;
55
56 /**
57 * Encoded message
58 */
59 chunk_t encoding;
60 };
61
62 METHOD(pb_tnc_message_t, get_type, pb_tnc_msg_type_t,
63 private_pb_assessment_result_message_t *this)
64 {
65 return this->type;
66 }
67
68 METHOD(pb_tnc_message_t, get_encoding, chunk_t,
69 private_pb_assessment_result_message_t *this)
70 {
71 return this->encoding;
72 }
73
74 METHOD(pb_tnc_message_t, build, void,
75 private_pb_assessment_result_message_t *this)
76 {
77 tls_writer_t *writer;
78
79 /* build message */
80 writer = tls_writer_create(ASSESSMENT_RESULT_MESSAGE_SIZE);
81 writer->write_uint32(writer, this->assessment_result);
82 free(this->encoding.ptr);
83 this->encoding = writer->get_buf(writer);
84 this->encoding = chunk_clone(this->encoding);
85 writer->destroy(writer);
86 }
87
88 METHOD(pb_tnc_message_t, process, status_t,
89 private_pb_assessment_result_message_t *this)
90 {
91 tls_reader_t *reader;
92
93 if (this->encoding.len < ASSESSMENT_RESULT_MESSAGE_SIZE)
94 {
95 DBG1(DBG_TNC,"%N message is shorter than message size of %u bytes",
96 pb_tnc_msg_type_names, PB_MSG_ASSESSMENT_RESULT,
97 ASSESSMENT_RESULT_MESSAGE_SIZE);
98 return FAILED;
99 }
100
101 /* process message */
102 reader = tls_reader_create(this->encoding);
103 reader->read_uint32(reader, &this->assessment_result);
104
105 reader->destroy(reader);
106 return SUCCESS;
107 }
108
109 METHOD(pb_tnc_message_t, destroy, void,
110 private_pb_assessment_result_message_t *this)
111 {
112 free(this->encoding.ptr);
113 free(this);
114 }
115
116 METHOD(pb_assessment_result_message_t, get_assessment_result, u_int32_t,
117 private_pb_assessment_result_message_t *this)
118 {
119 return this->assessment_result;
120 }
121
122 /**
123 * See header
124 */
125 pb_tnc_message_t *pb_assessment_result_message_create_from_data(chunk_t data)
126 {
127 private_pb_assessment_result_message_t *this;
128
129 INIT(this,
130 .public = {
131 .pb_interface = {
132 .get_type = _get_type,
133 .get_encoding = _get_encoding,
134 .build = _build,
135 .process = _process,
136 .destroy = _destroy,
137 },
138 .get_assessment_result = _get_assessment_result,
139 },
140 .type = PB_MSG_ASSESSMENT_RESULT,
141 .encoding = chunk_clone(data),
142 );
143
144 return &this->public.pb_interface;
145 }
146
147 /**
148 * See header
149 */
150 pb_tnc_message_t *pb_assessment_result_message_create(u_int32_t assessment_result)
151 {
152 private_pb_assessment_result_message_t *this;
153
154 INIT(this,
155 .public = {
156 .pb_interface = {
157 .get_type = _get_type,
158 .get_encoding = _get_encoding,
159 .build = _build,
160 .process = _process,
161 .destroy = _destroy,
162 },
163 .get_assessment_result = _get_assessment_result,
164 },
165 .type = PB_MSG_ASSESSMENT_RESULT,
166 .assessment_result = assessment_result,
167 );
168
169 return &this->public.pb_interface;
170 }