dc43e29e3525482886ae921c6cf60146df9d0261
[strongswan.git] / src / libcharon / plugins / tnccs_20 / messages / pb_access_recommendation_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_access_recommendation_message.h"
17
18 #include <tls_writer.h>
19 #include <tls_reader.h>
20 #include <debug.h>
21
22 typedef struct private_pb_access_recommendation_message_t private_pb_access_recommendation_message_t;
23
24 /**
25 * PB-Access-Recommendation message (see section 4.7 of RFC 5793)
26 *
27 * 0 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 * | Reserved | Access Recommendation Code |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 */
33
34 #define ACCESS_RECOMMENDATION_RESERVED 0x0000
35 #define ACCESS_RECOMMENDATION_MESSAGE_SIZE 4
36
37 /**
38 * Private data of a private_pb_access_recommendation_message_t object.
39 *
40 */
41 struct private_pb_access_recommendation_message_t {
42 /**
43 * Public pb_access_recommendation_message_t interface.
44 */
45 pb_access_recommendation_message_t public;
46
47 /**
48 * PB-TNC message type
49 */
50 pb_tnc_msg_type_t type;
51
52 /**
53 * Access recommendation code
54 */
55 u_int16_t recommendation;
56
57 /**
58 * Encoded message
59 */
60 chunk_t encoding;
61 };
62
63 METHOD(pb_tnc_message_t, get_type, pb_tnc_msg_type_t,
64 private_pb_access_recommendation_message_t *this)
65 {
66 return this->type;
67 }
68
69 METHOD(pb_tnc_message_t, get_encoding, chunk_t,
70 private_pb_access_recommendation_message_t *this)
71 {
72 return this->encoding;
73 }
74
75 METHOD(pb_tnc_message_t, build, void,
76 private_pb_access_recommendation_message_t *this)
77 {
78 tls_writer_t *writer;
79
80 /* build message */
81 writer = tls_writer_create(ACCESS_RECOMMENDATION_MESSAGE_SIZE);
82 writer->write_uint16(writer, ACCESS_RECOMMENDATION_RESERVED);
83 writer->write_uint16(writer, this->recommendation);
84 free(this->encoding.ptr);
85 this->encoding = writer->get_buf(writer);
86 this->encoding = chunk_clone(this->encoding);
87 writer->destroy(writer);
88 }
89
90 METHOD(pb_tnc_message_t, process, status_t,
91 private_pb_access_recommendation_message_t *this)
92 {
93 tls_reader_t *reader;
94 u_int16_t reserved;
95
96 if (this->encoding.len < ACCESS_RECOMMENDATION_MESSAGE_SIZE)
97 {
98 DBG1(DBG_TNC,"%N message is shorter than message size of %u bytes",
99 pb_tnc_msg_type_names, PB_MSG_ACCESS_RECOMMENDATION,
100 ACCESS_RECOMMENDATION_MESSAGE_SIZE);
101 return FAILED;
102 }
103
104 /* process message */
105 reader = tls_reader_create(this->encoding);
106 reader->read_uint16(reader, &reserved);
107 reader->read_uint16(reader, &this->recommendation);
108
109 reader->destroy(reader);
110 return SUCCESS;
111 }
112
113 METHOD(pb_tnc_message_t, destroy, void,
114 private_pb_access_recommendation_message_t *this)
115 {
116 free(this->encoding.ptr);
117 free(this);
118 }
119
120 METHOD(pb_access_recommendation_message_t, get_access_recommendation, u_int16_t,
121 private_pb_access_recommendation_message_t *this)
122 {
123 return this->recommendation;
124 }
125
126 /**
127 * See header
128 */
129 pb_tnc_message_t *pb_access_recommendation_message_create_from_data(chunk_t data)
130 {
131 private_pb_access_recommendation_message_t *this;
132
133 INIT(this,
134 .public = {
135 .pb_interface = {
136 .get_type = _get_type,
137 .get_encoding = _get_encoding,
138 .build = _build,
139 .process = _process,
140 .destroy = _destroy,
141 },
142 .get_access_recommendation = _get_access_recommendation,
143 },
144 .type = PB_MSG_ACCESS_RECOMMENDATION,
145 .encoding = chunk_clone(data),
146 );
147
148 return &this->public.pb_interface;
149 }
150
151 /**
152 * See header
153 */
154 pb_tnc_message_t *pb_access_recommendation_message_create(u_int16_t recommendation)
155 {
156 private_pb_access_recommendation_message_t *this;
157
158 INIT(this,
159 .public = {
160 .pb_interface = {
161 .get_type = _get_type,
162 .get_encoding = _get_encoding,
163 .build = _build,
164 .process = _process,
165 .destroy = _destroy,
166 },
167 .get_access_recommendation = _get_access_recommendation,
168 },
169 .type = PB_MSG_ACCESS_RECOMMENDATION,
170 .recommendation = recommendation,
171 );
172
173 return &this->public.pb_interface;
174 }