2 * Copyright (C) 2006 Mike McCauley (mikem@open.com.au)
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "tnccs_reason_strings_msg.h"
17 #include "tnccs_error_msg.h"
19 #include <utils/debug.h>
21 typedef struct private_tnccs_reason_strings_msg_t private_tnccs_reason_strings_msg_t
;
24 * Private data of a tnccs_reason_strings_msg_t object.
27 struct private_tnccs_reason_strings_msg_t
{
29 * Public tnccs_reason_strings_msg_t interface.
31 tnccs_reason_strings_msg_t
public;
36 tnccs_msg_type_t type
;
39 * XML-encoded message node
54 METHOD(tnccs_msg_t
, get_type
, tnccs_msg_type_t
,
55 private_tnccs_reason_strings_msg_t
*this)
60 METHOD(tnccs_msg_t
, get_node
, xmlNodePtr
,
61 private_tnccs_reason_strings_msg_t
*this)
66 METHOD(tnccs_msg_t
, destroy
, void,
67 private_tnccs_reason_strings_msg_t
*this)
69 free(this->reason
.ptr
);
70 free(this->language
.ptr
);
74 METHOD(tnccs_reason_strings_msg_t
, get_reason
, chunk_t
,
75 private_tnccs_reason_strings_msg_t
*this, chunk_t
*language
)
77 *language
= this->language
;
85 tnccs_msg_t
*tnccs_reason_strings_msg_create_from_node(xmlNodePtr node
,
86 linked_list_t
*errors
)
88 private_tnccs_reason_strings_msg_t
*this;
89 char *error_msg
, *lang_string
, *reason_string
;
90 tnccs_error_type_t error_type
= TNCCS_ERROR_MALFORMED_BATCH
;
96 .tnccs_msg_interface
= {
97 .get_type
= _get_type
,
98 .get_node
= _get_node
,
101 .get_reason
= _get_reason
,
103 .type
= TNCCS_MSG_REASON_STRINGS
,
107 if (xmlStrcmp(node
->name
, (const xmlChar
*)"TNCCS-ReasonStrings"))
109 error_msg
= "TNCCS-ReasonStrings tag expected";
113 child
= node
->xmlChildrenNode
;
116 if (xmlIsBlankNode(child
))
121 if (xmlStrcmp(child
->name
, (const xmlChar
*)"ReasonString"))
123 error_msg
= "ReasonString tag expected";
129 lang_string
= (char*)xmlGetProp(child
, (const xmlChar
*)"lang");
134 this->language
= chunk_create(strdup(lang_string
), strlen(lang_string
));
136 reason_string
= (char*)xmlNodeGetContent(child
);
137 this->reason
= chunk_create(strdup(reason_string
), strlen(reason_string
));
139 return &this->public.tnccs_msg_interface
;
142 msg
= tnccs_error_msg_create(error_type
, error_msg
);
143 errors
->insert_last(errors
, msg
);
151 tnccs_msg_t
*tnccs_reason_strings_msg_create(chunk_t reason
, chunk_t language
)
153 private_tnccs_reason_strings_msg_t
*this;
154 xmlNodePtr n
, n2
, n3
;
158 .tnccs_msg_interface
= {
159 .get_type
= _get_type
,
160 .get_node
= _get_node
,
163 .get_reason
= _get_reason
,
165 .type
= TNCCS_MSG_REASON_STRINGS
,
166 .node
= xmlNewNode(NULL
, BAD_CAST
"TNCC-TNCS-Message"),
167 .reason
= chunk_create_clone(malloc(reason
.len
+ 1), reason
),
168 .language
= chunk_create_clone(malloc(language
.len
+ 1), language
),
171 /* add NULL termination for XML string representation */
172 this->reason
.ptr
[this->reason
.len
] = '\0';
173 this->language
.ptr
[this->language
.len
] = '\0';
175 /* add the message type number in hex */
176 n
= xmlNewNode(NULL
, BAD_CAST
"Type");
177 xmlNodeSetContent(n
, BAD_CAST
"00000004");
178 xmlAddChild(this->node
, n
);
180 n
= xmlNewNode(NULL
, BAD_CAST
"XML");
181 xmlAddChild(this->node
, n
);
183 n2
= xmlNewNode(NULL
, BAD_CAST
enum_to_name(tnccs_msg_type_names
, this->type
));
185 /* could add multiple reasons here, if we had them */
187 n3
= xmlNewNode(NULL
, BAD_CAST
"ReasonString");
188 xmlNewProp(n3
, BAD_CAST
"xml:lang", BAD_CAST
this->language
.ptr
);
189 xmlNodeSetContent(n3
, BAD_CAST
this->reason
.ptr
);
193 return &this->public.tnccs_msg_interface
;