pb_tnc_batch_t class implements parsing and building of PB-TNC batches
[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 #define PB_LANG_PREFIX "Accept-Language: "
35 #define PB_LANG_PREFIX_LEN strlen(PB_LANG_PREFIX)
36
37 /**
38 * Private data of a private_pb_language_preference_message_t object.
39 *
40 */
41 struct private_pb_language_preference_message_t {
42 /**
43 * Public pb_access_recommendation_message_t interface.
44 */
45 pb_language_preference_message_t public;
46
47 /**
48 * PB-TNC message type
49 */
50 pb_tnc_msg_type_t type;
51
52 /**
53 * Language preference
54 */
55 chunk_t language_preference;
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_language_preference_message_t *this)
65 {
66 return this->type;
67 }
68
69 METHOD(pb_tnc_message_t, get_encoding, chunk_t,
70 private_pb_language_preference_message_t *this)
71 {
72 return this->encoding;
73 }
74
75 METHOD(pb_tnc_message_t, build, void,
76 private_pb_language_preference_message_t *this)
77 {
78 this->encoding = chunk_cat("cc",
79 chunk_create(PB_LANG_PREFIX, PB_LANG_PREFIX_LEN),
80 this->language_preference);
81 }
82
83 METHOD(pb_tnc_message_t, process, status_t,
84 private_pb_language_preference_message_t *this)
85 {
86 chunk_t lang;
87
88 if (this->encoding.len >= PB_LANG_PREFIX_LEN &&
89 memeq(this->encoding.ptr, PB_LANG_PREFIX, PB_LANG_PREFIX_LEN))
90 {
91 lang = chunk_skip(this->encoding, PB_LANG_PREFIX_LEN);
92 this->language_preference = lang.len ? chunk_clone(lang) : chunk_empty;
93 return SUCCESS;
94 }
95 else
96 {
97 /* TODO generate non-fatal PB-TNC error msg */
98 return VERIFY_ERROR;
99 }
100 }
101
102 METHOD(pb_tnc_message_t, destroy, void,
103 private_pb_language_preference_message_t *this)
104 {
105 free(this->encoding.ptr);
106 free(this->language_preference.ptr);
107 free(this);
108 }
109
110 METHOD(pb_language_preference_message_t, get_language_preference, chunk_t,
111 private_pb_language_preference_message_t *this)
112 {
113 return this->language_preference;
114 }
115
116 /**
117 * See header
118 */
119 pb_tnc_message_t *pb_language_preference_message_create_from_data(chunk_t data)
120 {
121 private_pb_language_preference_message_t *this;
122
123 INIT(this,
124 .public = {
125 .pb_interface = {
126 .get_type = _get_type,
127 .get_encoding = _get_encoding,
128 .build = _build,
129 .process = _process,
130 .destroy = _destroy,
131 },
132 .get_language_preference = _get_language_preference,
133 },
134 .type = PB_MSG_LANGUAGE_PREFERENCE,
135 .encoding = chunk_clone(data),
136 );
137
138 return &this->public.pb_interface;
139 }
140
141 /**
142 * See header
143 */
144 pb_tnc_message_t *pb_language_preference_message_create(chunk_t language_preference)
145 {
146 private_pb_language_preference_message_t *this;
147
148 INIT(this,
149 .public = {
150 .pb_interface = {
151 .get_type = _get_type,
152 .get_encoding = _get_encoding,
153 .build = _build,
154 .process = _process,
155 .destroy = _destroy,
156 },
157 .get_language_preference = _get_language_preference,
158 },
159 .type = PB_MSG_LANGUAGE_PREFERENCE,
160 .language_preference = chunk_clone(language_preference),
161 );
162
163 return &this->public.pb_interface;
164 }