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_error_msg.h"
18 #include <utils/debug.h>
20 ENUM(tnccs_error_type_names
, TNCCS_ERROR_BATCH_TOO_LONG
, TNCCS_ERROR_OTHER
,
24 "invalid-recipient-type",
29 typedef struct private_tnccs_error_msg_t private_tnccs_error_msg_t
;
32 * Private data of a tnccs_error_msg_t object.
35 struct private_tnccs_error_msg_t
{
37 * Public tnccs_error_msg_t interface.
39 tnccs_error_msg_t
public;
44 tnccs_msg_type_t type
;
47 * XML-encoded message node
54 tnccs_error_type_t error_type
;
67 METHOD(tnccs_msg_t
, get_type
, tnccs_msg_type_t
,
68 private_tnccs_error_msg_t
*this)
73 METHOD(tnccs_msg_t
, get_node
, xmlNodePtr
,
74 private_tnccs_error_msg_t
*this)
79 METHOD(tnccs_msg_t
, get_ref
, tnccs_msg_t
*,
80 private_tnccs_error_msg_t
*this)
83 return &this->public.tnccs_msg_interface
;
86 METHOD(tnccs_msg_t
, destroy
, void,
87 private_tnccs_error_msg_t
*this)
89 if (ref_put(&this->ref
))
91 free(this->error_msg
);
96 METHOD(tnccs_error_msg_t
, get_message
, char*,
97 private_tnccs_error_msg_t
*this, tnccs_error_type_t
*type
)
99 *type
= this->error_type
;
101 return this->error_msg
;
107 tnccs_msg_t
*tnccs_error_msg_create_from_node(xmlNodePtr node
)
109 private_tnccs_error_msg_t
*this;
110 xmlChar
*error_type_name
, *error_msg
;
114 .tnccs_msg_interface
= {
115 .get_type
= _get_type
,
116 .get_node
= _get_node
,
120 .get_message
= _get_message
,
122 .type
= TNCCS_MSG_ERROR
,
125 .error_type
= TNCCS_ERROR_OTHER
,
128 error_type_name
= xmlGetProp(node
, (const xmlChar
*)"type");
131 this->error_type
= enum_from_name(tnccs_error_type_names
,
132 (char*)error_type_name
);
133 if (this->error_type
== -1)
135 this->error_type
= TNCCS_ERROR_OTHER
;
137 xmlFree(error_type_name
);
140 error_msg
= xmlNodeGetContent(node
);
143 this->error_msg
= strdup((char*)error_msg
);
147 return &this->public.tnccs_msg_interface
;
153 tnccs_msg_t
*tnccs_error_msg_create(tnccs_error_type_t type
, char *msg
)
155 private_tnccs_error_msg_t
*this;
160 .tnccs_msg_interface
= {
161 .get_type
= _get_type
,
162 .get_node
= _get_node
,
166 .get_message
= _get_message
,
168 .type
= TNCCS_MSG_ERROR
,
170 .node
= xmlNewNode(NULL
, BAD_CAST
"TNCC-TNCS-Message"),
172 .error_msg
= strdup(msg
),
175 DBG1(DBG_TNC
, "%s", msg
);
177 n
= xmlNewNode(NULL
, BAD_CAST
"Type");
178 xmlNodeSetContent(n
, BAD_CAST
"00000002");
179 xmlAddChild(this->node
, n
);
181 n
= xmlNewNode(NULL
, BAD_CAST
"XML");
182 xmlAddChild(this->node
, n
);
184 n2
= xmlNewNode(NULL
, BAD_CAST
enum_to_name(tnccs_msg_type_names
, this->type
));
185 xmlNewProp(n2
, BAD_CAST
"type",
186 BAD_CAST
enum_to_name(tnccs_error_type_names
, type
));
187 xmlNodeSetContent(n2
, BAD_CAST msg
);
190 return &this->public.tnccs_msg_interface
;