af1621b4bb54a03000d2fffb12731ee72f856d50
[strongswan.git] / src / libcharon / plugins / tnccs_11 / messages / tnccs_reason_strings_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_reason_strings_msg.h"
17
18 #include <debug.h>
19
20 typedef struct private_tnccs_reason_strings_msg_t private_tnccs_reason_strings_msg_t;
21
22 /**
23 * Private data of a tnccs_reason_strings_msg_t object.
24 *
25 */
26 struct private_tnccs_reason_strings_msg_t {
27 /**
28 * Public tnccs_reason_strings_msg_t interface.
29 */
30 tnccs_reason_strings_msg_t public;
31
32 /**
33 * TNCCS message type
34 */
35 tnccs_msg_type_t type;
36
37 /**
38 * XML-encoded message node
39 */
40 xmlNodePtr node;
41
42 /**
43 * Reason Language
44 */
45 char* language;
46
47 /**
48 * Reason String
49 */
50 char* reason;
51 };
52
53 METHOD(tnccs_msg_t, get_type, tnccs_msg_type_t,
54 private_tnccs_reason_strings_msg_t *this)
55 {
56 return this->type;
57 }
58
59 METHOD(tnccs_msg_t, get_node, xmlNodePtr,
60 private_tnccs_reason_strings_msg_t *this)
61 {
62 return this->node;
63 }
64
65 METHOD(tnccs_msg_t, process, status_t,
66 private_tnccs_reason_strings_msg_t *this)
67 {
68 return SUCCESS;
69 }
70
71 METHOD(tnccs_msg_t, destroy, void,
72 private_tnccs_reason_strings_msg_t *this)
73 {
74 free(this->language);
75 free(this->reason);
76 free(this);
77 }
78
79 METHOD(tnccs_reason_strings_msg_t, get_reason, char*,
80 private_tnccs_reason_strings_msg_t *this, char **language)
81 {
82 *language = this->language;
83
84 return this->reason;
85 }
86
87 /**
88 * See header
89 */
90 tnccs_msg_t *tnccs_reason_strings_msg_create_from_node(xmlNodePtr node)
91 {
92 private_tnccs_reason_strings_msg_t *this;
93
94 INIT(this,
95 .public = {
96 .tnccs_msg_interface = {
97 .get_type = _get_type,
98 .get_node = _get_node,
99 .process = _process,
100 .destroy = _destroy,
101 },
102 .get_reason = _get_reason,
103 },
104 .type = TNCCS_MSG_REASON_STRINGS,
105 .node = node,
106 );
107
108 return &this->public.tnccs_msg_interface;
109 }
110
111 /**
112 * See header
113 */
114 tnccs_msg_t *tnccs_reason_strings_msg_create(char *language, char *reason)
115 {
116 private_tnccs_reason_strings_msg_t *this;
117 xmlNodePtr n, n2, n3;
118
119 INIT(this,
120 .public = {
121 .tnccs_msg_interface = {
122 .get_type = _get_type,
123 .get_node = _get_node,
124 .process = _process,
125 .destroy = _destroy,
126 },
127 .get_reason = _get_reason,
128 },
129 .type = TNCCS_MSG_REASON_STRINGS,
130 .node = xmlNewNode(NULL, BAD_CAST "TNCC-TNCS-Message"),
131 .language = strdup(language),
132 .reason = strdup(reason),
133 );
134
135 /* add the message type number in hex */
136 n = xmlNewNode(NULL, BAD_CAST "Type");
137 xmlNodeSetContent(n, BAD_CAST "00000004");
138 xmlAddChild(this->node, n);
139
140 n = xmlNewNode(NULL, BAD_CAST "XML");
141 xmlAddChild(this->node, n);
142
143 n2 = xmlNewNode(NULL, BAD_CAST enum_to_name(tnccs_msg_type_names, this->type));
144 xmlNodeSetContent(n2, BAD_CAST language);
145 xmlAddChild(n, n2);
146
147 /* could add multiple reasons here, if we had them */
148 n3 = xmlNewNode(NULL, BAD_CAST "ReasonString");
149 xmlNewProp(n3, BAD_CAST "xml:lang", BAD_CAST language);
150 xmlNodeSetContent(n3, BAD_CAST reason);
151 xmlAddChild(n2, n3);
152
153 return &this->public.tnccs_msg_interface;
154 }