8dd4cd2e523e850b889f16d8406172bc85833470
[strongswan.git] / src / libcharon / plugins / tnccs_20 / messages / pb_language_preference_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_language_preference_message.h"
17
18 #include <tls_writer.h>
19 #include <tls_reader.h>
20 #include <debug.h>
21
22 typedef struct private_pb_language_preference_message_t private_pb_language_preference_message_t;
23
24 /**
25 * PB-Language-Preference message (see section 4.10 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 * | Language Preference (Variable Length) |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 */
33
34 /**
35 * Private data of a private_pb_language_preference_message_t object.
36 *
37 */
38 struct private_pb_language_preference_message_t {
39 /**
40 * Public pb_access_recommendation_message_t interface.
41 */
42 pb_language_preference_message_t public;
43
44 /**
45 * PB-TNC message type
46 */
47 pb_tnc_msg_type_t type;
48
49 /**
50 * Language preference
51 */
52 chunk_t language_preference;
53
54 /**
55 * Encoded message
56 */
57 chunk_t encoding;
58 };
59
60 METHOD(pb_tnc_message_t, get_type, pb_tnc_msg_type_t,
61 private_pb_language_preference_message_t *this)
62 {
63 return this->type;
64 }
65
66 METHOD(pb_tnc_message_t, get_encoding, chunk_t,
67 private_pb_language_preference_message_t *this)
68 {
69 return this->encoding;
70 }
71
72 METHOD(pb_tnc_message_t, build, void,
73 private_pb_language_preference_message_t *this)
74 {
75 tls_writer_t *writer;
76
77 /* build message */
78 writer = tls_writer_create(16);
79 writer->write_data(writer, this->language_preference);
80
81 free(this->encoding.ptr);
82 this->encoding = writer->get_buf(writer);
83 this->encoding = chunk_clone(this->encoding);
84 writer->destroy(writer);
85 }
86
87 METHOD(pb_tnc_message_t, process, status_t,
88 private_pb_language_preference_message_t *this)
89 {
90 tls_reader_t *reader;
91
92 if (this->encoding.len)
93 {
94 /* process message */
95 reader = tls_reader_create(this->encoding);
96 reader->read_data(reader, this->encoding.len,
97 &this->language_preference);
98 this->language_preference = chunk_clone(this->language_preference);
99 reader->destroy(reader);
100 }
101
102 return SUCCESS;
103 }
104
105 METHOD(pb_tnc_message_t, destroy, void,
106 private_pb_language_preference_message_t *this)
107 {
108 free(this->encoding.ptr);
109 free(this->language_preference.ptr);
110 free(this);
111 }
112
113 METHOD(pb_language_preference_message_t, get_language_preference, chunk_t,
114 private_pb_language_preference_message_t *this)
115 {
116 return this->language_preference;
117 }
118
119 /**
120 * See header
121 */
122 pb_tnc_message_t *pb_language_preference_message_create_from_data(chunk_t data)
123 {
124 private_pb_language_preference_message_t *this;
125
126 INIT(this,
127 .public = {
128 .pb_interface = {
129 .get_type = _get_type,
130 .get_encoding = _get_encoding,
131 .build = _build,
132 .process = _process,
133 .destroy = _destroy,
134 },
135 .get_language_preference = _get_language_preference,
136 },
137 .type = PB_MSG_LANGUAGE_PREFERENCE,
138 .encoding = chunk_clone(data),
139 );
140
141 return &this->public.pb_interface;
142 }
143
144 /**
145 * See header
146 */
147 pb_tnc_message_t *pb_language_preference_message_create(chunk_t language_preference)
148 {
149 private_pb_language_preference_message_t *this;
150
151 INIT(this,
152 .public = {
153 .pb_interface = {
154 .get_type = _get_type,
155 .get_encoding = _get_encoding,
156 .build = _build,
157 .process = _process,
158 .destroy = _destroy,
159 },
160 .get_language_preference = _get_language_preference,
161 },
162 .type = PB_MSG_LANGUAGE_PREFERENCE,
163 .language_preference = chunk_clone(language_preference),
164 );
165
166 return &this->public.pb_interface;
167 }