implemented the RFC 5792 PA-TNC protocol and an example IMC/IMV pair
[strongswan.git] / src / libimcv / ita / ita_attr_command.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "ita_attr_command.h"
16
17 #include <tnc/pen/pen.h>
18 #include <debug.h>
19
20 typedef struct private_ita_attr_command_t private_ita_attr_command_t;
21
22 /**
23 * Private data of an ita_attr_command_t object.
24 */
25 struct private_ita_attr_command_t {
26
27 /**
28 * Public members of ita_attr_command_t
29 */
30 ita_attr_command_t public;
31
32 /**
33 * Attribute vendor ID
34 */
35 pen_t vendor_id;
36
37 /**
38 * Attribute type
39 */
40 u_int32_t type;
41
42 /**
43 * Attribute value
44 */
45 chunk_t value;
46
47 /**
48 * Noskip flag
49 */
50 bool noskip_flag;
51
52 /**
53 * Command string
54 */
55 char *command;
56 };
57
58 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
59 private_ita_attr_command_t *this)
60 {
61 return this->vendor_id;
62 }
63
64 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
65 private_ita_attr_command_t *this)
66 {
67 return this->type;
68 }
69
70 METHOD(pa_tnc_attr_t, get_value, chunk_t,
71 private_ita_attr_command_t *this)
72 {
73 return this->value;
74 }
75
76 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
77 private_ita_attr_command_t *this)
78 {
79 return this->noskip_flag;
80 }
81
82 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
83 private_ita_attr_command_t *this, bool noskip)
84 {
85 this->noskip_flag = noskip;
86 }
87
88 METHOD(pa_tnc_attr_t, build, void,
89 private_ita_attr_command_t *this)
90 {
91 this->value = chunk_create(this->command, strlen(this->command));
92 this->value = chunk_clone(this->value);
93 }
94
95 METHOD(pa_tnc_attr_t, process, status_t,
96 private_ita_attr_command_t *this)
97 {
98 this->command = malloc(this->value.len + 1);
99 memcpy(this->command, this->value.ptr, this->value.len);
100 this->command[this->value.len] = '\0';
101
102 return SUCCESS;
103 }
104
105 METHOD(pa_tnc_attr_t, destroy, void,
106 private_ita_attr_command_t *this)
107 {
108 free(this->value.ptr);
109 free(this->command);
110 free(this);
111 }
112
113 METHOD(ita_attr_command_t, get_command, char*,
114 private_ita_attr_command_t *this)
115 {
116 return this->command;
117 }
118
119 /**
120 * Described in header.
121 */
122 pa_tnc_attr_t *ita_attr_command_create(char *command)
123 {
124 private_ita_attr_command_t *this;
125
126 INIT(this,
127 .public = {
128 .pa_tnc_attribute = {
129 .get_vendor_id = _get_vendor_id,
130 .get_type = _get_type,
131 .get_value = _get_value,
132 .get_noskip_flag = _get_noskip_flag,
133 .set_noskip_flag = _set_noskip_flag,
134 .build = _build,
135 .process = _process,
136 .destroy = _destroy,
137 },
138 .get_command = _get_command,
139 },
140 .vendor_id = PEN_ITA,
141 .type = ITA_ATTR_COMMAND,
142 .command = strdup(command),
143 );
144
145 return &this->public.pa_tnc_attribute;
146 }
147
148 /**
149 * Described in header.
150 */
151 pa_tnc_attr_t *ita_attr_command_create_from_data(chunk_t data)
152 {
153 private_ita_attr_command_t *this;
154
155 INIT(this,
156 .public = {
157 .pa_tnc_attribute = {
158 .get_vendor_id = _get_vendor_id,
159 .get_type = _get_type,
160 .get_value = _get_value,
161 .build = _build,
162 .process = _process,
163 .destroy = _destroy,
164 },
165 .get_command = _get_command,
166 },
167 .vendor_id = PEN_ITA,
168 .type = ITA_ATTR_COMMAND,
169 .value = chunk_clone(data),
170 );
171
172 return &this->public.pa_tnc_attribute;
173 }
174
175