8a5646815e6b8cddfa980dd4ba56d38fd6237490
[strongswan.git] / src / libcharon / plugins / tnccs_11 / messages / tnccs_error_msg.c
1 /*
2 * Copyright (C) 2006 Mike McCauley (mikem@open.com.au)
3 * Copyright (C) 2010 Andreas Steffen, 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 "tnccs_error_msg.h"
17
18 #include <debug.h>
19
20 ENUM(tnccs_error_type_names, TNCCS_ERROR_BATCH_TOO_LONG, TNCCS_ERROR_OTHER,
21 "batch-too-long",
22 "malformed-batch",
23 "invalid-batch-id",
24 "invalid-recipient-type",
25 "internal-error",
26 "other"
27 );
28
29 typedef struct private_tnccs_error_msg_t private_tnccs_error_msg_t;
30
31 /**
32 * Private data of a tnccs_error_msg_t object.
33 *
34 */
35 struct private_tnccs_error_msg_t {
36 /**
37 * Public tnccs_error_msg_t interface.
38 */
39 tnccs_error_msg_t public;
40
41 /**
42 * TNCCS message type
43 */
44 tnccs_msg_type_t type;
45
46 /**
47 * XML-encoded message node
48 */
49 xmlNodePtr node;
50
51 /**
52 * Error type
53 */
54 tnccs_error_type_t error_type;
55
56 /**
57 * Error message
58 */
59 char *error_msg;
60 };
61
62 METHOD(tnccs_msg_t, get_type, tnccs_msg_type_t,
63 private_tnccs_error_msg_t *this)
64 {
65 return this->type;
66 }
67
68 METHOD(tnccs_msg_t, get_node, xmlNodePtr,
69 private_tnccs_error_msg_t *this)
70 {
71 return this->node;
72 }
73
74 METHOD(tnccs_msg_t, process, status_t,
75 private_tnccs_error_msg_t *this)
76 {
77 return SUCCESS;
78 }
79
80 METHOD(tnccs_msg_t, destroy, void,
81 private_tnccs_error_msg_t *this)
82 {
83 free(this->error_msg);
84 free(this);
85 }
86
87 METHOD(tnccs_error_msg_t, get_message, char*,
88 private_tnccs_error_msg_t *this, tnccs_error_type_t *type)
89 {
90 *type = this->error_type;
91
92 return this->error_msg;
93 }
94
95 /**
96 * See header
97 */
98 tnccs_msg_t *tnccs_error_msg_create_from_node(xmlNodePtr node)
99 {
100 private_tnccs_error_msg_t *this;
101
102 INIT(this,
103 .public = {
104 .tnccs_msg_interface = {
105 .get_type = _get_type,
106 .get_node = _get_node,
107 .process = _process,
108 .destroy = _destroy,
109 },
110 .get_message = _get_message,
111 },
112 .type = TNCCS_MSG_ERROR,
113 .node = node,
114 );
115
116 return &this->public.tnccs_msg_interface;
117 }
118
119 /**
120 * See header
121 */
122 tnccs_msg_t *tnccs_error_msg_create(tnccs_error_type_t type, char *msg)
123 {
124 private_tnccs_error_msg_t *this;
125 xmlNodePtr n, n2;
126
127 INIT(this,
128 .public = {
129 .tnccs_msg_interface = {
130 .get_type = _get_type,
131 .get_node = _get_node,
132 .process = _process,
133 .destroy = _destroy,
134 },
135 .get_message = _get_message,
136 },
137 .type = TNCCS_MSG_ERROR,
138 .node = xmlNewNode(NULL, BAD_CAST "TNCC-TNCS-Message"),
139 .error_type = type,
140 .error_msg = strdup(msg),
141 );
142
143 n = xmlNewNode(NULL, BAD_CAST "Type");
144 xmlNodeSetContent(n, BAD_CAST "00000002");
145 xmlAddChild(this->node, n);
146
147 n = xmlNewNode(NULL, BAD_CAST "XML");
148 xmlAddChild(this->node, n);
149
150 n2 = xmlNewNode(NULL, BAD_CAST enum_to_name(tnccs_msg_type_names, this->type));
151 xmlNewProp(n2, BAD_CAST "type",
152 BAD_CAST enum_to_name(tnccs_error_type_names, type));
153 xmlNodeSetContent(n2, BAD_CAST msg);
154 xmlAddChild(n, n2);
155
156 return &this->public.tnccs_msg_interface;
157 }