created libpts
[strongswan.git] / src / libpts / pts / pts_file_meas.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * 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 "pts_file_meas.h"
17
18 #include <utils/linked_list.h>
19 #include <debug.h>
20
21 typedef struct private_pts_file_meas_t private_pts_file_meas_t;
22
23 /**
24 * Private data of a pts_file_meas_t object.
25 *
26 */
27 struct private_pts_file_meas_t {
28
29 /**
30 * Public pts_file_meas_t interface.
31 */
32 pts_file_meas_t public;
33
34 /**
35 * ID of PTS File Measurement Request
36 */
37 u_int16_t request_id;
38
39 /**
40 * List of File Measurements
41 */
42 linked_list_t *list;
43 };
44
45 typedef struct entry_t entry_t;
46
47 /**
48 * PTS File Measurement entry
49 */
50 struct entry_t {
51 char *filename;
52 chunk_t measurement;
53 };
54
55 /**
56 * Free an entry_t object
57 */
58 static void free_entry(entry_t *entry)
59 {
60 if (entry)
61 {
62 free(entry->filename);
63 free(entry->measurement.ptr);
64 free(entry);
65 }
66 }
67
68 METHOD(pts_file_meas_t, get_request_id, u_int16_t,
69 private_pts_file_meas_t *this)
70 {
71 return this->request_id;
72 }
73
74 METHOD(pts_file_meas_t, get_file_count, int,
75 private_pts_file_meas_t *this)
76 {
77 return this->list->get_count(this->list);
78 }
79
80 METHOD(pts_file_meas_t, add, void,
81 private_pts_file_meas_t *this, char *filename, chunk_t measurement)
82 {
83 entry_t *entry;
84
85 entry = malloc_thing(entry_t);
86 entry->filename = strdup(filename);
87 entry->measurement = chunk_clone(measurement);
88
89 this->list->insert_last(this->list, entry);
90 }
91
92 /**
93 * Enumerate file measurement entries
94 */
95 static bool entry_filter(void *null, entry_t **entry, char **filename,
96 void *i2, chunk_t *measurement)
97 {
98 *filename = (*entry)->filename;
99 *measurement = (*entry)->measurement;
100 return TRUE;
101 }
102
103 METHOD(pts_file_meas_t, create_enumerator, enumerator_t*,
104 private_pts_file_meas_t *this)
105 {
106 return enumerator_create_filter(this->list->create_enumerator(this->list),
107 (void*)entry_filter, NULL, NULL);
108 }
109
110 METHOD(pts_file_meas_t, verify, bool,
111 private_pts_file_meas_t *this, enumerator_t *e_hash, bool is_dir)
112 {
113 char *filename;
114 chunk_t measurement;
115 entry_t *entry;
116 enumerator_t *enumerator;
117 bool found, success = TRUE;
118
119 while (e_hash->enumerate(e_hash, &filename, &measurement))
120 {
121 found = FALSE;
122
123 enumerator = this->list->create_enumerator(this->list);
124 while (enumerator->enumerate(enumerator, &entry))
125 {
126 if (!is_dir || streq(filename, entry->filename))
127 {
128 found = TRUE;
129 break;
130 }
131 }
132 enumerator->destroy(enumerator);
133
134 if (!found)
135 {
136 DBG1(DBG_TNC, " no measurement found for '%s'", filename);
137 success = FALSE;
138 continue;
139 }
140 if (chunk_equals(measurement, entry->measurement))
141 {
142 DBG2(DBG_TNC, " %#B for '%s' is ok", &measurement, filename);
143 }
144 else
145 {
146 DBG1(DBG_TNC, " %#B for '%s' is incorrect", &measurement, filename);
147 success = FALSE;
148 }
149 if (!is_dir)
150 {
151 break;
152 }
153 }
154 return success;
155 }
156
157 METHOD(pts_file_meas_t, destroy, void,
158 private_pts_file_meas_t *this)
159 {
160 this->list->destroy_function(this->list, (void *)free_entry);
161 free(this);
162 }
163
164 /**
165 * See header
166 */
167 pts_file_meas_t *pts_file_meas_create(u_int16_t request_id)
168 {
169 private_pts_file_meas_t *this;
170
171 INIT(this,
172 .public = {
173 .get_request_id = _get_request_id,
174 .get_file_count = _get_file_count,
175 .add = _add,
176 .create_enumerator = _create_enumerator,
177 .verify = _verify,
178 .destroy = _destroy,
179 },
180 .request_id = request_id,
181 .list = linked_list_create(),
182 );
183
184 return &this->public;
185 }
186