aboutsummaryrefslogtreecommitdiff
path: root/src/my
diff options
context:
space:
mode:
Diffstat (limited to 'src/my')
-rw-r--r--src/my/.gitignore1
-rw-r--r--src/my/Makefile.am41
-rw-r--r--src/my/my.c269
-rw-r--r--src/my/my_query_helper.c400
-rw-r--r--src/my/my_result_helper.c876
-rw-r--r--src/my/test_my.c300
-rw-r--r--src/my/test_my.conf0
7 files changed, 0 insertions, 1887 deletions
diff --git a/src/my/.gitignore b/src/my/.gitignore
deleted file mode 100644
index 3338ba2ea..000000000
--- a/src/my/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
1test_my
diff --git a/src/my/Makefile.am b/src/my/Makefile.am
deleted file mode 100644
index 51e20e6bf..000000000
--- a/src/my/Makefile.am
+++ /dev/null
@@ -1,41 +0,0 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4if USE_COVERAGE
5 AM_CFLAGS = --coverage
6endif
7
8if HAVE_MYSQL
9lib_LTLIBRARIES = libgnunetmy.la
10endif
11
12libgnunetmy_la_SOURCES = \
13 my.c \
14 my_query_helper.c \
15 my_result_helper.c
16
17libgnunetmy_la_LIBADD = $(MYSQL_LDFLAGS) -lmysqlclient \
18 $(top_builddir)/src/mysql/libgnunetmysql.la \
19 $(top_builddir)/src/util/libgnunetutil.la
20libgnunetmy_la_LDFLAGS = \
21 $(GN_LIB_LDFLAGS) \
22 -version-info 0:0:0
23
24if ENABLE_TEST_RUN
25TESTS = \
26 test_my
27endif
28
29EXTRA_DIST = \
30 test_my.conf
31
32check_PROGRAMS= \
33 test_my
34
35test_my_SOURCES = \
36 test_my.c
37test_my_LDADD = \
38 libgnunetmy.la \
39 $(top_builddir)/src/mysql/libgnunetmysql.la \
40 $(top_builddir)/src/util/libgnunetutil.la \
41 -lmysqlclient $(XLIB)
diff --git a/src/my/my.c b/src/my/my.c
deleted file mode 100644
index 468a4a47c..000000000
--- a/src/my/my.c
+++ /dev/null
@@ -1,269 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2016, 2018 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file my/my.c
22 * @brief library to help with access to a MySQL database
23 * @author Christophe Genevey
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <mysql/mysql.h>
28#include "gnunet_my_lib.h"
29
30
31/**
32 * Run a prepared SELECT statement.
33 *
34 * @param mc mysql context
35 * @param sh handle to SELECT statement
36 * @param params parameters to the statement
37 * @return
38 #GNUNET_YES if we can prepare all statement
39 #GNUNET_SYSERR if we can't prepare all statement
40 */
41int
42GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
43 struct GNUNET_MYSQL_StatementHandle *sh,
44 struct GNUNET_MY_QueryParam *params)
45{
46 const struct GNUNET_MY_QueryParam *p;
47 unsigned int num;
48 MYSQL_STMT *stmt;
49
50 num = 0;
51 for (unsigned int i = 0; NULL != params[i].conv; i++)
52 num += params[i].num_params;
53 {
54 MYSQL_BIND qbind[num];
55 unsigned int off;
56
57 memset (qbind,
58 0,
59 sizeof(qbind));
60 off = 0;
61 for (unsigned int i = 0; NULL != (p = &params[i])->conv; i++)
62 {
63 if (GNUNET_OK !=
64 p->conv (p->conv_cls,
65 p,
66 &qbind[off]))
67 {
68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
69 "Conversion for MySQL query failed at offset %u\n",
70 i);
71 return GNUNET_SYSERR;
72 }
73 off += p->num_params;
74 }
75 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
76 if (mysql_stmt_bind_param (stmt,
77 qbind))
78 {
79 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
80 "my",
81 _ ("`%s' failed at %s:%d with error: %s\n"),
82 "mysql_stmt_bind_param",
83 __FILE__, __LINE__,
84 mysql_stmt_error (stmt));
85 GNUNET_MYSQL_statements_invalidate (mc);
86 return GNUNET_SYSERR;
87 }
88
89 if (mysql_stmt_execute (stmt))
90 {
91 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
92 "my",
93 _ ("`%s' failed at %s:%d with error: %s\n"),
94 "mysql_stmt_execute", __FILE__, __LINE__,
95 mysql_stmt_error (stmt));
96 GNUNET_MYSQL_statements_invalidate (mc);
97 return GNUNET_SYSERR;
98 }
99 GNUNET_MY_cleanup_query (params,
100 qbind);
101 }
102 return GNUNET_OK;
103}
104
105
106/**
107 * Free all memory that was allocated in @a qp during
108 * #GNUNET_MY_exec_prepared().
109 *
110 * @param qp query specification to clean up
111 * @param qbind array of parameter to clean up
112 */
113void
114GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
115 MYSQL_BIND *qbind)
116{
117 for (unsigned int i = 0; NULL != qp[i].conv; i++)
118 if (NULL != qp[i].cleaner)
119 qp[i].cleaner (qp[i].conv_cls,
120 &qbind[i]);
121}
122
123
124/**
125 * Extract results from a query result according to the given
126 * specification. Always fetches the next row.
127 *
128 * @param sh statement that returned results
129 * @param rs specification to extract for
130 * @return
131 * #GNUNET_YES if all results could be extracted
132 * #GNUNET_NO if there is no more data in the result set
133 * #GNUNET_SYSERR if a result was invalid
134 */
135int
136GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
137 struct GNUNET_MY_ResultSpec *rs)
138{
139 unsigned int num_fields;
140 int ret;
141 MYSQL_STMT *stmt;
142
143 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
144 if (NULL == stmt)
145 {
146 GNUNET_break (0);
147 return GNUNET_SYSERR;
148 }
149 if (NULL == rs)
150 {
151 mysql_stmt_free_result (stmt);
152 return GNUNET_NO;
153 }
154
155 num_fields = 0;
156 for (unsigned int i = 0; NULL != rs[i].pre_conv; i++)
157 num_fields += rs[i].num_fields;
158
159 if (mysql_stmt_field_count (stmt) != num_fields)
160 {
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 "Number of fields mismatch between SQL result and result specification\n");
163 return GNUNET_SYSERR;
164 }
165
166 {
167 MYSQL_BIND result[num_fields];
168 unsigned int field_off;
169
170 memset (result, 0, sizeof(MYSQL_BIND) * num_fields);
171 field_off = 0;
172 for (unsigned int i = 0; NULL != rs[i].pre_conv; i++)
173 {
174 struct GNUNET_MY_ResultSpec *rp = &rs[i];
175
176 if (GNUNET_OK !=
177 rp->pre_conv (rp->conv_cls,
178 rp,
179 stmt,
180 field_off,
181 &result[field_off]))
182
183 {
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Pre-conversion for MySQL result failed at offset %u\n",
186 i);
187 return GNUNET_SYSERR;
188 }
189 field_off += rp->num_fields;
190 }
191
192 if (mysql_stmt_bind_result (stmt, result))
193 {
194 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
195 "my",
196 _ ("%s failed at %s:%d with error: %s\n"),
197 "mysql_stmt_bind_result",
198 __FILE__, __LINE__,
199 mysql_stmt_error (stmt));
200 return GNUNET_SYSERR;
201 }
202#if TEST_OPTIMIZATION
203 (void) mysql_stmt_store_result (stmt);
204#endif
205 ret = mysql_stmt_fetch (stmt);
206 if (MYSQL_NO_DATA == ret)
207 {
208 mysql_stmt_free_result (stmt);
209 return GNUNET_NO;
210 }
211 if (1 == ret)
212 {
213 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
214 "my",
215 _ ("%s failed at %s:%d with error: %s\n"),
216 "mysql_stmt_fetch",
217 __FILE__, __LINE__,
218 mysql_stmt_error (stmt));
219 GNUNET_MY_cleanup_result (rs);
220 mysql_stmt_free_result (stmt);
221 return GNUNET_SYSERR;
222 }
223 field_off = 0;
224 for (unsigned int i = 0; NULL != rs[i].post_conv; i++)
225 {
226 struct GNUNET_MY_ResultSpec *rp = &rs[i];
227
228 if (NULL != rp->post_conv)
229 if (GNUNET_OK !=
230 rp->post_conv (rp->conv_cls,
231 rp,
232 stmt,
233 field_off,
234 &result[field_off]))
235 {
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Post-conversion for MySQL result failed at offset %u\n",
238 i);
239 mysql_stmt_free_result (stmt);
240 for (unsigned int j = 0; j < i; j++)
241 if (NULL != rs[j].cleaner)
242 rs[j].cleaner (rs[j].conv_cls,
243 rs[j].dst);
244 return GNUNET_SYSERR;
245 }
246 field_off += rp->num_fields;
247 }
248 }
249 return GNUNET_OK;
250}
251
252
253/**
254 * Free all memory that was allocated in @a rs during
255 * #GNUNET_MY_extract_result().
256 *
257 * @param rs result specification to clean up
258 */
259void
260GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs)
261{
262 for (unsigned int i = 0; NULL != rs[i].post_conv; i++)
263 if (NULL != rs[i].cleaner)
264 rs[i].cleaner (rs[i].conv_cls,
265 &rs[i]);
266}
267
268
269/* end of my.c */
diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c
deleted file mode 100644
index 97ea04fd1..000000000
--- a/src/my/my_query_helper.c
+++ /dev/null
@@ -1,400 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file my/my_query_helper.c
22 * @brief library to help with access to a MySQL database
23 * @author Christian Grothoff
24 * @author Christophe Genevey
25 */
26#include "platform.h"
27#include <mysql/mysql.h>
28#include "gnunet_my_lib.h"
29
30
31/**
32 * Function called to clean up memory allocated
33 * by a #GNUNET_MY_QueryConverter.
34 *
35 * @param cls closure
36 * @param qbind array of parameter to clean up
37 */
38static void
39my_clean_query (void *cls,
40 MYSQL_BIND *qbind)
41{
42 (void) cls;
43 GNUNET_free (qbind[0].buffer);
44}
45
46
47/**
48 * Function called to convert input argument into SQL parameters.
49 *
50 * @param cls closure
51 * @param pq data about the query
52 * @param qbind array of parameters to initialize
53 * @return -1 on error
54 */
55static int
56my_conv_fixed_size (void *cls,
57 const struct GNUNET_MY_QueryParam *qp,
58 MYSQL_BIND *qbind)
59{
60 (void) cls;
61 GNUNET_assert (1 == qp->num_params);
62 qbind->buffer = (void *) qp->data;
63 qbind->buffer_length = qp->data_len;
64 qbind->buffer_type = MYSQL_TYPE_BLOB;
65
66 return 1;
67}
68
69
70/**
71 * Generate query parameter for a buffer @a ptr of
72 * @a ptr_size bytes.
73 *
74 * @param ptr pointer to the query parameter to pass
75 * @param ptr_size number of bytes in @a ptr
76 */
77struct GNUNET_MY_QueryParam
78GNUNET_MY_query_param_fixed_size (const void *ptr,
79 size_t ptr_size)
80{
81 struct GNUNET_MY_QueryParam qp = {
82 .conv = &my_conv_fixed_size,
83 .cleaner = NULL,
84 .conv_cls = NULL,
85 .num_params = 1,
86 .data = ptr,
87 .data_len = (unsigned long) ptr_size
88 };
89
90 return qp;
91}
92
93
94/**
95 * Function called to convert input argument into SQL parameters.
96 *
97 * @param cls closure
98 * @param pq data about the query
99 * @param qbind array of parameters to initialize
100 * @return -1 on error
101 */
102static int
103my_conv_string (void *cls,
104 const struct GNUNET_MY_QueryParam *qp,
105 MYSQL_BIND *qbind)
106{
107 (void) cls;
108 GNUNET_assert (1 == qp->num_params);
109 qbind->buffer = (void *) qp->data;
110 qbind->buffer_length = qp->data_len;
111 qbind->buffer_type = MYSQL_TYPE_STRING;
112 return 1;
113}
114
115
116/**
117 * Generate query parameter for a string
118 *
119 * @param ptr pointer to the string query parameter to pass
120 */
121struct GNUNET_MY_QueryParam
122GNUNET_MY_query_param_string (const char *ptr)
123{
124 struct GNUNET_MY_QueryParam qp = {
125 .conv = &my_conv_string,
126 .cleaner = NULL,
127 .conv_cls = NULL,
128 .num_params = 1,
129 .data = ptr,
130 .data_len = strlen (ptr)
131 };
132
133 return qp;
134}
135
136
137/**
138 * Function called to convert input argument into SQL parameters
139 *
140 * @param cls closure
141 * @param pq data about the query
142 * @param qbind array of parameters to initialize
143 * @return -1 on error
144 */
145static int
146my_conv_uint16 (void *cls,
147 const struct GNUNET_MY_QueryParam *qp,
148 MYSQL_BIND *qbind)
149{
150 (void) cls;
151 GNUNET_assert (1 == qp->num_params);
152 qbind->buffer = (void *) qp->data;
153 qbind->buffer_length = sizeof(uint16_t);
154 qbind->buffer_type = MYSQL_TYPE_SHORT;
155 qbind->is_unsigned = 1;
156 return 1;
157}
158
159
160/**
161 * Generate query parameter for an uint16_t in host byte order.
162 *
163 * @param x pointer to the query parameter to pass
164 */
165struct GNUNET_MY_QueryParam
166GNUNET_MY_query_param_uint16 (const uint16_t *x)
167{
168 struct GNUNET_MY_QueryParam res = {
169 .conv = &my_conv_uint16,
170 .cleaner = NULL,
171 .conv_cls = NULL,
172 .num_params = 1,
173 .data = x,
174 .data_len = sizeof(*x)
175 };
176
177 return res;
178}
179
180
181/**
182 * Function called to convert input argument into SQL parameters
183 *
184 * @param cls closure
185 * @param pq data about the query
186 * @param qbind array of parameters to initialize
187 * @return -1 on error
188 */
189static int
190my_conv_uint32 (void *cls,
191 const struct GNUNET_MY_QueryParam *qp,
192 MYSQL_BIND *qbind)
193{
194 (void) cls;
195 GNUNET_assert (1 == qp->num_params);
196 qbind->buffer = (void *) qp->data;
197 qbind->buffer_length = sizeof(uint32_t);
198 qbind->buffer_type = MYSQL_TYPE_LONG;
199 qbind->is_unsigned = 1;
200 return 1;
201}
202
203
204/**
205 * Generate query parameter for an uint32_t in host byte order
206 *
207 * @param x pointer to the query parameter to pass
208 */
209struct GNUNET_MY_QueryParam
210GNUNET_MY_query_param_uint32 (const uint32_t *x)
211{
212 struct GNUNET_MY_QueryParam res = {
213 .conv = &my_conv_uint32,
214 .cleaner = NULL,
215 .conv_cls = NULL,
216 .num_params = 1,
217 .data = x,
218 .data_len = sizeof(*x)
219 };
220
221 return res;
222}
223
224
225/**
226 * Function called to convert input argument into SQL parameters
227 *
228 * @param cls closure
229 * @param pq data about the query
230 * @param qbind array of parameters to initialize
231 * @return -1 on error
232 */
233static int
234my_conv_uint64 (void *cls,
235 const struct GNUNET_MY_QueryParam *qp,
236 MYSQL_BIND *qbind)
237{
238 (void) cls;
239 GNUNET_assert (1 == qp->num_params);
240 qbind->buffer = (void *) qp->data;
241 qbind->buffer_length = sizeof(uint64_t);
242 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
243 qbind->is_unsigned = 1;
244 return 1;
245}
246
247
248/**
249 * Generate query parameter for an uint64_t in host byte order
250 *
251 * @param x pointer to the query parameter to pass
252 */
253struct GNUNET_MY_QueryParam
254GNUNET_MY_query_param_uint64 (const uint64_t *x)
255{
256 struct GNUNET_MY_QueryParam res = {
257 .conv = &my_conv_uint64,
258 .cleaner = NULL,
259 .conv_cls = NULL,
260 .num_params = 1,
261 .data = x,
262 .data_len = sizeof(*x)
263 };
264
265 return res;
266}
267
268
269/**
270 * Function called to convert input argument into SQL parameters
271 *
272 * @param cls closure
273 * @param pq data about the query
274 * @param qbind array of parameters to initialize
275 * @return -1 on error
276 */
277static int
278my_conv_rsa_public_key (void *cls,
279 const struct GNUNET_MY_QueryParam *qp,
280 MYSQL_BIND *qbind)
281{
282 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
283 void *buf;
284 size_t buf_size;
285
286 (void) cls;
287 GNUNET_assert (1 == qp->num_params);
288 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
289 &buf);
290 qbind->buffer = buf;
291 qbind->buffer_length = buf_size;
292 qbind->buffer_type = MYSQL_TYPE_BLOB;
293 return 1;
294}
295
296
297/**
298 * Generate query parameter for an RSA public key. The
299 * database must contain a BLOB type in the respective position.
300 *
301 * @param x the query parameter to pass
302 * @return array entry for the query parameters to use
303 */
304struct GNUNET_MY_QueryParam
305GNUNET_MY_query_param_rsa_public_key (const struct
306 GNUNET_CRYPTO_RsaPublicKey *x)
307{
308 struct GNUNET_MY_QueryParam res = {
309 .conv = &my_conv_rsa_public_key,
310 .cleaner = &my_clean_query,
311 .conv_cls = NULL,
312 .num_params = 1,
313 .data = x,
314 .data_len = 0
315 };
316
317 return res;
318}
319
320
321/**
322 * Function called to convert input argument into SQL parameters
323 *
324 *@param cls closure
325 *@param pq data about the query
326 *@param qbind array of parameters to initialize
327 *@return -1 on error
328 */
329static int
330my_conv_rsa_signature (void *cls,
331 const struct GNUNET_MY_QueryParam *qp,
332 MYSQL_BIND *qbind)
333{
334 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
335 void *buf;
336 size_t buf_size;
337
338 (void) cls;
339 GNUNET_assert (1 == qp->num_params);
340 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
341 &buf);
342 qbind->buffer = buf;
343 qbind->buffer_length = buf_size;
344 qbind->buffer_type = MYSQL_TYPE_BLOB;
345 return 1;
346}
347
348
349/**
350 * Generate query parameter for an RSA signature. The
351 * database must contain a BLOB type in the respective position
352 *
353 * @param x the query parameter to pass
354 * @return array entry for the query parameters to use
355 */
356struct GNUNET_MY_QueryParam
357GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
358{
359 struct GNUNET_MY_QueryParam res = {
360 .conv = &my_conv_rsa_signature,
361 .cleaner = &my_clean_query,
362 .conv_cls = NULL,
363 .num_params = 1,
364 .data = (x),
365 .data_len = 0
366 };
367
368 return res;
369}
370
371
372/**
373 * Generate query parameter for an absolute time value.
374 * The database must store a 64-bit integer.
375 *
376 * @param x pointer to the query parameter to pass
377 * @return array entry for the query parameters to use
378 */
379struct GNUNET_MY_QueryParam
380GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
381{
382 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
383}
384
385
386/**
387 * Generate query parameter for an absolute time value.
388 * The database must store a 64-bit integer.
389 *
390 * @param x pointer to the query parameter to pass
391 */
392struct GNUNET_MY_QueryParam
393GNUNET_MY_query_param_absolute_time_nbo (const struct
394 GNUNET_TIME_AbsoluteNBO *x)
395{
396 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
397}
398
399
400/* end of my_query_helper.c */
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c
deleted file mode 100644
index 99b4229a4..000000000
--- a/src/my/my_result_helper.c
+++ /dev/null
@@ -1,876 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file my/my_result_helper.c
22 * @brief functions to extract result values
23 * @author Christophe Genevey
24 */
25
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_my_lib.h"
29
30
31/**
32 * extract data from a Mysql database @a result at row @a row
33 *
34 * @param cls closure
35 * @param[in,out] rs
36 * @param stmt the mysql statement that is being run
37 * @param column the column that is being processed
38 * @param[out] result mysql result
39 * @return
40 * #GNUNET_OK if all results could be extracted
41 * #GNUNET_SYSERR if a result was invalid
42 */
43static int
44pre_extract_varsize_blob (void *cls,
45 struct GNUNET_MY_ResultSpec *rs,
46 MYSQL_STMT *stmt,
47 unsigned int column,
48 MYSQL_BIND *results)
49{
50 results[0].buffer = NULL;
51 results[0].buffer_length = 0;
52 results[0].length = &rs->mysql_bind_output_length;
53 results[0].is_null = &rs->is_null;
54 rs->is_null = 0;
55
56 return GNUNET_OK;
57}
58
59
60/**
61 * extract data from a Mysql database @a result at row @a row
62 *
63 * @param cls closure
64 * @param[in,out] rs
65 * @param stmt the mysql statement that is being run
66 * @param column the column that is being processed
67 * @param[out] results
68 * @return
69 * #GNUNET_OK if all results could be extracted
70 * #GNUNET_SYSERR if a result was invalid
71 */
72static int
73post_extract_varsize_blob (void *cls,
74 struct GNUNET_MY_ResultSpec *rs,
75 MYSQL_STMT *stmt,
76 unsigned int column,
77 MYSQL_BIND *results)
78{
79 void *buf;
80 size_t size;
81
82 if (*results->is_null)
83 return GNUNET_SYSERR;
84 size = (size_t) rs->mysql_bind_output_length;
85
86 if (rs->mysql_bind_output_length != size)
87 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
88
89 buf = GNUNET_malloc (size);
90
91 results[0].buffer = buf;
92 results[0].buffer_length = size;
93 results[0].buffer_type = MYSQL_TYPE_BLOB;
94
95 if (0 !=
96 mysql_stmt_fetch_column (stmt,
97 results,
98 column,
99 0))
100 {
101 GNUNET_free (buf);
102 return GNUNET_SYSERR;
103 }
104
105 *(void **) rs->dst = buf;
106 *rs->result_size = size;
107
108 return GNUNET_OK;
109}
110
111
112/**
113 * extract data from a Mysql database @a result at row @a row
114 *
115 * @param cls closure
116 * @param[in,out] rs
117 */
118static void
119cleanup_varsize_blob (void *cls,
120 struct GNUNET_MY_ResultSpec *rs)
121{
122 void **ptr = (void **) rs->dst;
123
124 if (NULL != *ptr)
125 {
126 GNUNET_free (*ptr);
127 *ptr = NULL;
128 }
129}
130
131
132/**
133 * Variable-size result expected
134 *
135 * @param[out] dst where to store the result, allocated
136 * @param[out] ptr_size where to store the size of @a dst
137 * @return array entru for the result specification to use
138 */
139struct GNUNET_MY_ResultSpec
140GNUNET_MY_result_spec_variable_size (void **dst,
141 size_t *ptr_size)
142{
143 struct GNUNET_MY_ResultSpec res = {
144 .pre_conv = &pre_extract_varsize_blob,
145 .post_conv = &post_extract_varsize_blob,
146 .cleaner = &cleanup_varsize_blob,
147 .dst = (void *) (dst),
148 .result_size = ptr_size,
149 .num_fields = 1
150 };
151
152 return res;
153}
154
155
156/**
157 * Extract data from a Mysql database @a result at row @a row
158 *
159 * @param cls closure
160 * @param[in,out] rs
161 * @param stmt the mysql statement that is being run
162 * @param column the column that is being processed
163 * @param[out] results
164 * @return
165 * #GNUNET_OK if all results could be extracted
166 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
167 */
168static int
169pre_extract_fixed_blob (void *cls,
170 struct GNUNET_MY_ResultSpec *rs,
171 MYSQL_STMT *stmt,
172 unsigned int column,
173 MYSQL_BIND *results)
174{
175 results[0].buffer = rs->dst;
176 results[0].buffer_length = rs->dst_size;
177 results[0].length = &rs->mysql_bind_output_length;
178 results[0].buffer_type = MYSQL_TYPE_BLOB;
179 results[0].is_null = &rs->is_null;
180 rs->is_null = 0;
181
182 return GNUNET_OK;
183}
184
185
186/**
187 * Check size of extracted fixed size data from a Mysql database @a
188 * result at row @a row
189 *
190 * @param cls closure
191 * @param[in,out] rs
192 * @param stmt the mysql statement that is being run
193 * @param column the column that is being processed
194 * @param[out] results
195 * @return
196 * #GNUNET_OK if all results could be extracted
197 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
198 */
199static int
200post_extract_fixed_blob (void *cls,
201 struct GNUNET_MY_ResultSpec *rs,
202 MYSQL_STMT *stmt,
203 unsigned int column,
204 MYSQL_BIND *results)
205{
206 if (*results->is_null)
207 return GNUNET_SYSERR;
208 if (rs->dst_size != rs->mysql_bind_output_length)
209 return GNUNET_SYSERR;
210 return GNUNET_OK;
211}
212
213
214/**
215 * Fixed-size result expected.
216 *
217 * @param name name of the field in the table
218 * @param[out] dst where to store the result
219 * @param ptr_size number of bytes in @a dst
220 * @return array entry for the result specification to use
221 */
222struct GNUNET_MY_ResultSpec
223GNUNET_MY_result_spec_fixed_size (void *ptr,
224 size_t ptr_size)
225{
226 struct GNUNET_MY_ResultSpec res = {
227 .pre_conv = &pre_extract_fixed_blob,
228 .post_conv = &post_extract_fixed_blob,
229 .cleaner = NULL,
230 .dst = (void *) (ptr),
231 .dst_size = ptr_size,
232 .num_fields = 1
233 };
234
235 return res;
236}
237
238
239/**
240 * Extract data from a Mysql database @a result at row @a row
241 *
242 * @param cls closure
243 * @param[in,out] rs
244 * @param stmt the mysql statement that is being run
245 * @param column the column that is being processed
246 * @param[out] results
247 * @return
248 * #GNUNET_OK if all results could be extracted
249 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
250 */
251static int
252pre_extract_rsa_public_key (void *cls,
253 struct GNUNET_MY_ResultSpec *rs,
254 MYSQL_STMT *stmt,
255 unsigned int column,
256 MYSQL_BIND *results)
257{
258 results[0].buffer = NULL;
259 results[0].buffer_length = 0;
260 results[0].length = &rs->mysql_bind_output_length;
261 results[0].buffer_type = MYSQL_TYPE_BLOB;
262 results[0].is_null = &rs->is_null;
263 rs->is_null = 0;
264
265 return GNUNET_OK;
266}
267
268
269/**
270 * Check size of extracted fixed size data from a Mysql database @a
271 * result at row @a row
272 *
273 * @param cls closure
274 * @param[in,out] rs
275 * @param stmt the mysql statement that is being run
276 * @param column the column that is being processed
277 * @param[out] results
278 * @return
279 * #GNUNET_OK if all results could be extracted
280 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
281 */
282static int
283post_extract_rsa_public_key (void *cls,
284 struct GNUNET_MY_ResultSpec *rs,
285 MYSQL_STMT *stmt,
286 unsigned int column,
287 MYSQL_BIND *results)
288
289{
290 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
291 void *buf;
292 size_t size;
293
294 if (*results->is_null)
295 return GNUNET_SYSERR;
296 size = (size_t) rs->mysql_bind_output_length;
297
298 if (rs->mysql_bind_output_length != size)
299 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
300 buf = GNUNET_malloc (size);
301
302 results[0].buffer = buf;
303 results[0].buffer_length = size;
304 results[0].buffer_type = MYSQL_TYPE_BLOB;
305 if (0 !=
306 mysql_stmt_fetch_column (stmt,
307 results,
308 column,
309 0))
310 {
311 GNUNET_free (buf);
312 return GNUNET_SYSERR;
313 }
314
315 *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
316 size);
317 GNUNET_free (buf);
318 if (NULL == *pk)
319 {
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "Results contains bogus public key value (fail to decode)\n");
322 return GNUNET_SYSERR;
323 }
324
325 return GNUNET_OK;
326}
327
328
329/**
330 * Function called to clean up memory allocated
331 * by a #GNUNET_MY_ResultConverter.
332 *
333 * @param cls closure
334 * @param rs result data to clean up
335 */
336static void
337clean_rsa_public_key (void *cls,
338 struct GNUNET_MY_ResultSpec *rs)
339{
340 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
341
342 if (NULL != *pk)
343 {
344 GNUNET_CRYPTO_rsa_public_key_free (*pk);
345 *pk = NULL;
346 }
347}
348
349
350/**
351 * RSA public key expected
352 *
353 * @param name name of the field in the table
354 * @param[out] rsa where to store the result
355 * @return array entry for the result specification to use
356 */
357struct GNUNET_MY_ResultSpec
358GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
359{
360 struct GNUNET_MY_ResultSpec res = {
361 .pre_conv = &pre_extract_rsa_public_key,
362 .post_conv = &post_extract_rsa_public_key,
363 .cleaner = &clean_rsa_public_key,
364 .dst = (void *) rsa,
365 .dst_size = 0,
366 .num_fields = 1
367 };
368
369 return res;
370}
371
372
373/**
374 * Extract data from a Mysql database @a result at row @a row.
375 *
376 * @param cls closure
377 * @param[in,out] rs
378 * @param stmt the mysql statement that is being run
379 * @param column the column that is being processed
380 * @param[out] results
381 * @return
382 * #GNUNET_OK if all results could be extracted
383 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
384 */
385static int
386pre_extract_rsa_signature (void *cls,
387 struct GNUNET_MY_ResultSpec *rs,
388 MYSQL_STMT *stmt,
389 unsigned int column,
390 MYSQL_BIND *results)
391{
392 results[0].buffer = 0;
393 results[0].buffer_length = 0;
394 results[0].length = &rs->mysql_bind_output_length;
395 results[0].buffer_type = MYSQL_TYPE_BLOB;
396 results[0].is_null = &rs->is_null;
397 rs->is_null = 0;
398
399 return GNUNET_OK;
400}
401
402
403/**
404 * Extract data from a Mysql database @a result at row @a row.
405 *
406 * @param cls closure
407 * @param[in,out] rs
408 * @param stmt the mysql statement that is being run
409 * @param column the column that is being processed
410 * @param[out] results
411 * @return
412 * #GNUNET_OK if all results could be extracted
413 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
414 */
415static int
416post_extract_rsa_signature (void *cls,
417 struct GNUNET_MY_ResultSpec *rs,
418 MYSQL_STMT *stmt,
419 unsigned int column,
420 MYSQL_BIND *results)
421{
422 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
423 void *buf;
424 size_t size;
425
426 if (*results->is_null)
427 return GNUNET_SYSERR;
428 size = (size_t) rs->mysql_bind_output_length;
429
430 if (rs->mysql_bind_output_length != size)
431 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
432 buf = GNUNET_malloc (size);
433
434 results[0].buffer = buf;
435 results[0].buffer_length = size;
436 results[0].buffer_type = MYSQL_TYPE_BLOB;
437 if (0 !=
438 mysql_stmt_fetch_column (stmt,
439 results,
440 column,
441 0))
442 {
443 GNUNET_free (buf);
444 return GNUNET_SYSERR;
445 }
446
447 *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
448 size);
449 GNUNET_free (buf);
450 if (NULL == *sig)
451 {
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Resuls contains bogus signature value (fails to decode)\n");
454 return GNUNET_SYSERR;
455 }
456 return GNUNET_OK;
457}
458
459
460/**
461 * Function called to clean up memory allocated
462 * by a #GNUNET_MY_ResultConverter.
463 *
464 * @param cls closure
465 * @param rd result data to clean up
466 */
467static void
468clean_rsa_signature (void *cls,
469 struct GNUNET_MY_ResultSpec *rs)
470{
471 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
472
473 if (NULL != *sig)
474 {
475 GNUNET_CRYPTO_rsa_signature_free (*sig);
476 *sig = NULL;
477 }
478}
479
480
481/**
482 * RSA signature expected.
483 *
484 * @param[out] sig where to store the result;
485 * @return array entry for the result specification to use
486 */
487struct GNUNET_MY_ResultSpec
488GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
489{
490 struct GNUNET_MY_ResultSpec res = {
491 .pre_conv = &pre_extract_rsa_signature,
492 .post_conv = &post_extract_rsa_signature,
493 .cleaner = &clean_rsa_signature,
494 .dst = (void *) sig,
495 .dst_size = 0,
496 .num_fields = 1
497 };
498
499 return res;
500}
501
502
503/**
504 * Extract data from a Mysql database @a result at row @a row
505 *
506 * @param cls closure
507 * @param[in,out] rs
508 * @param stmt the mysql statement that is being run
509 * @param column the column that is being processed
510 * @param[out] results
511 * @return
512 * #GNUNET_OK if all results could be extracted
513 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
514 */
515static int
516pre_extract_string (void *cls,
517 struct GNUNET_MY_ResultSpec *rs,
518 MYSQL_STMT *stmt,
519 unsigned int column,
520 MYSQL_BIND *results)
521{
522 results[0].buffer = NULL;
523 results[0].buffer_length = 0;
524 results[0].length = &rs->mysql_bind_output_length;
525 results[0].buffer_type = MYSQL_TYPE_BLOB;
526 results[0].is_null = &rs->is_null;
527 rs->is_null = 0;
528
529 return GNUNET_OK;
530}
531
532
533/**
534 * Check size of extracted fixed size data from a Mysql database @a
535 *
536 * @param cls closure
537 * @param[in,out] rs
538 * @param stmt the mysql statement that is being run
539 * @param column the column that is being processed
540 * @param[out] results
541 * @return
542 * #GNUNET_OK if all results could be extracted
543 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
544 */
545static int
546post_extract_string (void *cls,
547 struct GNUNET_MY_ResultSpec *rs,
548 MYSQL_STMT *stmt,
549 unsigned int column,
550 MYSQL_BIND *results)
551{
552 size_t size = (size_t) rs->mysql_bind_output_length;
553 char *buf;
554
555 if (rs->mysql_bind_output_length != size)
556 return GNUNET_SYSERR;
557 if (*results->is_null)
558 {
559 *(void **) rs->dst = NULL;
560 return GNUNET_OK;
561 }
562
563 buf = GNUNET_malloc (size);
564 results[0].buffer = buf;
565 results[0].buffer_length = size;
566 results[0].buffer_type = MYSQL_TYPE_BLOB;
567
568 if (0 !=
569 mysql_stmt_fetch_column (stmt,
570 results,
571 column,
572 0))
573 {
574 GNUNET_free (buf);
575 return GNUNET_SYSERR;
576 }
577 buf[size] = '\0';
578 *(void **) rs->dst = buf;
579 return GNUNET_OK;
580}
581
582
583/**
584 * 0- terminated string exprected.
585 *
586 * @param[out] dst where to store the result, allocated
587 * @return array entry for the result specification to use
588 */
589struct GNUNET_MY_ResultSpec
590GNUNET_MY_result_spec_string (char **dst)
591{
592 struct GNUNET_MY_ResultSpec res = {
593 .pre_conv = &pre_extract_string,
594 .post_conv = &post_extract_string,
595 .cleaner = NULL,
596 .dst = (void *) dst,
597 .dst_size = 0,
598 .num_fields = 1
599 };
600
601 return res;
602}
603
604
605/**
606 * Absolute time expected
607 *
608 * @param name name of the field in the table
609 * @param[out] at where to store the result
610 * @return array entry for the result specification to use
611 */
612struct GNUNET_MY_ResultSpec
613GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
614{
615 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
616}
617
618
619/**
620 * Absolute time in network byte order expected
621 *
622 * @param[out] at where to store the result
623 * @return array entry for the result specification to use
624 */
625struct GNUNET_MY_ResultSpec
626GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
627{
628 struct GNUNET_MY_ResultSpec res =
629 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
630
631 return res;
632}
633
634
635/**
636 * Extract data from a Postgres database @a result at row @a row.
637 *
638 * @param cls closure
639 * @param[in,out] rs
640 * @param stmt the mysql statement that is being run
641 * @param column the column that is being processed
642 * @param[out] results
643 * @return
644 * #GNUNET_YES if all results could be extracted
645 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
646 */
647static int
648pre_extract_uint16 (void *cls,
649 struct GNUNET_MY_ResultSpec *rs,
650 MYSQL_STMT *stmt,
651 unsigned int column,
652 MYSQL_BIND *results)
653{
654 results[0].buffer = rs->dst;
655 results[0].buffer_length = rs->dst_size;
656 results[0].length = &rs->mysql_bind_output_length;
657 results[0].buffer_type = MYSQL_TYPE_SHORT;
658 results[0].is_null = &rs->is_null;
659 rs->is_null = 0;
660
661 return GNUNET_OK;
662}
663
664
665/**
666 * Check size of extracted fixed size data from a Mysql database.
667 *
668 * @param cls closure
669 * @param[in,out] rs
670 * @param stmt the mysql statement that is being run
671 * @param column the column that is being processed
672 * @param[out] results
673 * @return
674 * #GNUNET_YES if all results could be extracted
675 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
676 */
677static int
678post_extract_uint16 (void *cls,
679 struct GNUNET_MY_ResultSpec *rs,
680 MYSQL_STMT *stmt,
681 unsigned int column,
682 MYSQL_BIND *results)
683{
684 if (rs->dst_size != rs->mysql_bind_output_length)
685 return GNUNET_SYSERR;
686 if (*results->is_null)
687 return GNUNET_SYSERR;
688 return GNUNET_OK;
689}
690
691
692/**
693 * uint16_t expected
694 *
695 * @param[out] u16 where to store the result
696 * @return array entry for the result specification to use
697 */
698struct GNUNET_MY_ResultSpec
699GNUNET_MY_result_spec_uint16 (uint16_t *u16)
700{
701 struct GNUNET_MY_ResultSpec res = {
702 .pre_conv = &pre_extract_uint16,
703 .post_conv = &post_extract_uint16,
704 .cleaner = NULL,
705 .dst = (void *) u16,
706 .dst_size = sizeof(*u16),
707 .num_fields = 1
708 };
709
710 return res;
711}
712
713
714/**
715 * Extract data from a MYSQL database @a result at row @a row
716 *
717 * @param cls closure
718 * @param cls closure
719 * @param[in,out] rs
720 * @param stmt the mysql statement that is being run
721 * @param column the column that is being processed
722 * @param[out] results
723 * @return
724 * #GNUNET_OK if all results could be extracted
725 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
726 */
727static int
728pre_extract_uint32 (void *cls,
729 struct GNUNET_MY_ResultSpec *rs,
730 MYSQL_STMT *stmt,
731 unsigned int column,
732 MYSQL_BIND *results)
733{
734 results[0].buffer = rs->dst;
735 results[0].buffer_length = rs->dst_size;
736 results[0].length = &rs->mysql_bind_output_length;
737 results[0].buffer_type = MYSQL_TYPE_LONG;
738 results[0].is_null = &rs->is_null;
739 rs->is_null = 0;
740
741 return GNUNET_OK;
742}
743
744
745/**
746 * Extract data from a MYSQL database @a result at row @a row
747 *
748 * @param cls closure
749 * @param cls closure
750 * @param[in,out] rs
751 * @param stmt the mysql statement that is being run
752 * @param column the column that is being processed
753 * @param[out] results
754 * @return
755 * #GNUNET_OK if all results could be extracted
756 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
757 */
758static int
759post_extract_uint32 (void *cls,
760 struct GNUNET_MY_ResultSpec *rs,
761 MYSQL_STMT *stmt,
762 unsigned int column,
763 MYSQL_BIND *results)
764{
765 if (rs->dst_size != rs->mysql_bind_output_length)
766 return GNUNET_SYSERR;
767 if (*results->is_null)
768 return GNUNET_SYSERR;
769 return GNUNET_OK;
770}
771
772
773/**
774 * uint32_t expected
775 *
776 * @param[out] u32 where to store the result
777 * @return array entry for the result specification to use
778 */
779struct GNUNET_MY_ResultSpec
780GNUNET_MY_result_spec_uint32 (uint32_t *u32)
781{
782 struct GNUNET_MY_ResultSpec res = {
783 .pre_conv = &pre_extract_uint32,
784 .post_conv = &post_extract_uint32,
785 .cleaner = NULL,
786 .dst = (void *) u32,
787 .dst_size = sizeof(*u32),
788 .num_fields = 1
789 };
790
791 return res;
792}
793
794
795/**
796 * Extract data from a MYSQL database @a result at row @a row
797 *
798 * @param cls closure
799 * @param[in,out] rs
800 * @param stmt the mysql statement that is being run
801 * @param column the column that is being processed
802 * @param[out] results
803 * @return
804 * #GNUNET_OK if all results could be extracted
805 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
806 */
807static int
808pre_extract_uint64 (void *cls,
809 struct GNUNET_MY_ResultSpec *rs,
810 MYSQL_STMT *stmt,
811 unsigned int column,
812 MYSQL_BIND *results)
813{
814 if (sizeof(uint64_t) != rs->dst_size)
815 return GNUNET_SYSERR;
816 results[0].buffer = rs->dst;
817 results[0].buffer_length = rs->dst_size;
818 results[0].length = &rs->mysql_bind_output_length;
819 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
820 results[0].is_null = &rs->is_null;
821 rs->is_null = 0;
822
823 return GNUNET_OK;
824}
825
826
827/**
828 * Check size of extracted fixed-size data from a Mysql database
829 *
830 * @param cls closure
831 * @param[in,out] rs
832 * @param stmt the mysql statement that is being run
833 * @param column the column that is being processed
834 * @param[out] results
835 * @return
836 * #GNUNET_OK if all results could be extracted
837 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
838 */
839static int
840post_extract_uint64 (void *cls,
841 struct GNUNET_MY_ResultSpec *rs,
842 MYSQL_STMT *stmt,
843 unsigned int column,
844 MYSQL_BIND *results)
845{
846 if (sizeof(uint64_t) != rs->dst_size)
847 return GNUNET_SYSERR;
848 if (*results->is_null)
849 return GNUNET_SYSERR;
850 return GNUNET_OK;
851}
852
853
854/**
855 * uint64_t expected.
856 *
857 * @param[out] u64 where to store the result
858 * @return array entry for the result specification to use
859 */
860struct GNUNET_MY_ResultSpec
861GNUNET_MY_result_spec_uint64 (uint64_t *u64)
862{
863 struct GNUNET_MY_ResultSpec res = {
864 .pre_conv = &pre_extract_uint64,
865 .post_conv = &post_extract_uint64,
866 .cleaner = NULL,
867 .dst = (void *) u64,
868 .dst_size = sizeof(*u64),
869 .num_fields = 1
870 };
871
872 return res;
873}
874
875
876/* end of my_result_helper.c */
diff --git a/src/my/test_my.c b/src/my/test_my.c
deleted file mode 100644
index ffb5a5f13..000000000
--- a/src/my/test_my.c
+++ /dev/null
@@ -1,300 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file my/test_my.c
22 * @brief Tests for convenience MySQL database
23 * @author Christophe Genevey
24 */
25#include "platform.h"
26#include <mysql/mysql.h>
27#include "gnunet_my_lib.h"
28#include "gnunet_mysql_lib.h"
29#include "gnunet_util_lib.h"
30
31
32/**
33 * Run actual test queries.
34 *
35 * @param contexte the current context of mysql
36 * @return 0 on success
37 */
38static int
39run_queries (struct GNUNET_MYSQL_Context *context)
40{
41 struct GNUNET_CRYPTO_RsaPublicKey *pub = NULL;
42 struct GNUNET_CRYPTO_RsaPublicKey *pub2 = NULL;
43 struct GNUNET_CRYPTO_RsaSignature *sig = NULL;;
44 struct GNUNET_CRYPTO_RsaSignature *sig2 = NULL;
45 struct GNUNET_TIME_Absolute abs_time = GNUNET_TIME_absolute_get ();
46 struct GNUNET_TIME_Absolute abs_time2;
47 struct GNUNET_TIME_Absolute forever = GNUNET_TIME_UNIT_FOREVER_ABS;
48 struct GNUNET_TIME_Absolute forever2;
49 const struct GNUNET_TIME_AbsoluteNBO abs_time_nbo =
50 GNUNET_TIME_absolute_hton (abs_time);
51 struct GNUNET_HashCode hc;
52 struct GNUNET_HashCode hc2;
53 const char msg[] = "hello";
54 void *msg2 = NULL;
55 size_t msg2_len;
56
57 const char msg3[] = "world";
58 char *msg4 = "";
59
60 uint16_t u16;
61 uint16_t u162;
62 uint32_t u32;
63 uint32_t u322;
64 uint64_t u64;
65 uint64_t u642;
66
67 int ret;
68
69 struct GNUNET_MYSQL_StatementHandle *statements_handle_insert = NULL;
70 struct GNUNET_MYSQL_StatementHandle *statements_handle_select = NULL;
71
72 struct GNUNET_CRYPTO_RsaPrivateKey *priv = NULL;
73 struct GNUNET_HashCode hmsg;
74
75 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
76 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
77 memset (&hmsg, 42, sizeof(hmsg));
78 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv,
79 &hmsg);
80 u16 = 16;
81 u32 = 32;
82 u64 = UINT64_MAX;
83
84 memset (&hc, 0, sizeof(hc));
85 memset (&hc2, 0, sizeof(hc2));
86
87 statements_handle_insert
88 = GNUNET_MYSQL_statement_prepare (context,
89 "INSERT INTO test_my2 ("
90 " pub"
91 ",sig"
92 ",abs_time"
93 ",forever"
94 ",abs_time_nbo"
95 ",hash"
96 ",vsize"
97 ",str"
98 ",u16"
99 ",u32"
100 ",u64"
101 ") VALUES "
102 "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
103
104 if (NULL == statements_handle_insert)
105 {
106 fprintf (stderr, "Failed to prepared statement INSERT\n");
107 GNUNET_CRYPTO_rsa_signature_free (sig);
108 GNUNET_CRYPTO_rsa_private_key_free (priv);
109 GNUNET_CRYPTO_rsa_public_key_free (pub);
110 return 1;
111 }
112
113 struct GNUNET_MY_QueryParam params_insert[] = {
114 GNUNET_MY_query_param_rsa_public_key (pub),
115 GNUNET_MY_query_param_rsa_signature (sig),
116 GNUNET_MY_query_param_absolute_time (&abs_time),
117 GNUNET_MY_query_param_absolute_time (&forever),
118 GNUNET_MY_query_param_absolute_time_nbo (&abs_time_nbo),
119 GNUNET_MY_query_param_auto_from_type (&hc),
120 GNUNET_MY_query_param_fixed_size (msg, strlen (msg)),
121 GNUNET_MY_query_param_string (msg3),
122 GNUNET_MY_query_param_uint16 (&u16),
123 GNUNET_MY_query_param_uint32 (&u32),
124 GNUNET_MY_query_param_uint64 (&u64),
125 GNUNET_MY_query_param_end
126 };
127
128 if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
129 statements_handle_insert,
130 params_insert))
131 {
132 fprintf (stderr, "Failed to execute prepared statement INSERT\n");
133 GNUNET_CRYPTO_rsa_signature_free (sig);
134 GNUNET_CRYPTO_rsa_private_key_free (priv);
135 GNUNET_CRYPTO_rsa_public_key_free (pub);
136 return 1;
137 }
138
139 statements_handle_select
140 = GNUNET_MYSQL_statement_prepare (context,
141 "SELECT"
142 " pub"
143 ",sig"
144 ",abs_time"
145 ",forever"
146 ",hash"
147 ",vsize"
148 ",str"
149 ",u16"
150 ",u32"
151 ",u64"
152 " FROM test_my2");
153
154 if (NULL == statements_handle_select)
155 {
156 fprintf (stderr, "Failed to prepared statement SELECT\n");
157 GNUNET_CRYPTO_rsa_signature_free (sig);
158 GNUNET_CRYPTO_rsa_private_key_free (priv);
159 GNUNET_CRYPTO_rsa_public_key_free (pub);
160 return 1;
161 }
162
163 struct GNUNET_MY_QueryParam params_select[] = {
164 GNUNET_MY_query_param_end
165 };
166
167 if (GNUNET_OK != GNUNET_MY_exec_prepared (context,
168 statements_handle_select,
169 params_select))
170 {
171 fprintf (stderr, "Failed to execute prepared statement SELECT\n");
172 GNUNET_CRYPTO_rsa_signature_free (sig);
173 GNUNET_CRYPTO_rsa_private_key_free (priv);
174 GNUNET_CRYPTO_rsa_public_key_free (pub);
175 return 1;
176 }
177
178 struct GNUNET_MY_ResultSpec results_select[] = {
179 GNUNET_MY_result_spec_rsa_public_key (&pub2),
180 GNUNET_MY_result_spec_rsa_signature (&sig2),
181 GNUNET_MY_result_spec_absolute_time (&abs_time2),
182 GNUNET_MY_result_spec_absolute_time (&forever2),
183 GNUNET_MY_result_spec_auto_from_type (&hc2),
184 GNUNET_MY_result_spec_variable_size (&msg2, &msg2_len),
185 GNUNET_MY_result_spec_string (&msg4),
186 GNUNET_MY_result_spec_uint16 (&u162),
187 GNUNET_MY_result_spec_uint32 (&u322),
188 GNUNET_MY_result_spec_uint64 (&u642),
189 GNUNET_MY_result_spec_end
190 };
191
192 ret = GNUNET_MY_extract_result (statements_handle_select,
193 results_select);
194
195 GNUNET_assert (GNUNET_YES == ret);
196 GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us);
197 GNUNET_break (forever.abs_value_us == forever2.abs_value_us);
198 GNUNET_break (0 ==
199 memcmp (&hc,
200 &hc2,
201 sizeof(struct GNUNET_HashCode)));
202
203 GNUNET_assert (NULL != sig2);
204 GNUNET_assert (NULL != pub2);
205 GNUNET_break (0 ==
206 GNUNET_CRYPTO_rsa_signature_cmp (sig,
207 sig2));
208 GNUNET_break (0 ==
209 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
210 pub2));
211
212 GNUNET_break (strlen (msg) == msg2_len);
213 GNUNET_break (0 ==
214 strncmp (msg,
215 msg2,
216 msg2_len));
217
218 GNUNET_break (strlen (msg3) == strlen (msg4));
219 GNUNET_break (0 ==
220 strcmp (msg3,
221 msg4));
222
223 GNUNET_break (16 == u162);
224 GNUNET_break (32 == u322);
225 GNUNET_break (UINT64_MAX == u642);
226
227 GNUNET_MY_cleanup_result (results_select);
228
229 GNUNET_CRYPTO_rsa_signature_free (sig);
230 GNUNET_CRYPTO_rsa_private_key_free (priv);
231 GNUNET_CRYPTO_rsa_public_key_free (pub);
232
233 if (GNUNET_OK != ret)
234 return 1;
235
236 return 0;
237}
238
239
240int
241main (int argc, const char *const argv[])
242{
243 struct GNUNET_CONFIGURATION_Handle *config;
244 struct GNUNET_MYSQL_Context *context;
245 int ret;
246
247 GNUNET_log_setup ("test-my",
248 "WARNING",
249 NULL);
250
251 config = GNUNET_CONFIGURATION_create ();
252 if (GNUNET_OK !=
253 GNUNET_CONFIGURATION_parse (config, "test_my.conf"))
254 {
255 fprintf (stderr, "Failed to parse configuration\n");
256 return 1;
257 }
258
259 context = GNUNET_MYSQL_context_create (config,
260 "datastore-mysql");
261 if (NULL == context)
262 {
263 fprintf (stderr, "Failed to connect to database\n");
264 return 77;
265 }
266
267 (void) GNUNET_MYSQL_statement_run (context,
268 "DROP TABLE test_my2;");
269
270 if (GNUNET_OK !=
271 GNUNET_MYSQL_statement_run (context,
272 "CREATE TABLE IF NOT EXISTS test_my2("
273 " pub BLOB NOT NULL"
274 ",sig BLOB NOT NULL"
275 ",abs_time BIGINT NOT NULL"
276 ",forever BIGINT NOT NULL"
277 ",abs_time_nbo BIGINT NOT NULL"
278 ",hash BLOB NOT NULL CHECK(LENGTH(hash)=64)"
279 ",vsize BLOB NOT NULL"
280 ",str BLOB NOT NULL"
281 ",u16 SMALLINT NOT NULL"
282 ",u32 INT NOT NULL"
283 ",u64 BIGINT NOT NULL"
284 ")"))
285 {
286 fprintf (stderr,
287 "Failed to create table. Database likely not setup correctly.\n");
288 GNUNET_MYSQL_statements_invalidate (context);
289 GNUNET_MYSQL_context_destroy (context);
290
291 return 77;
292 }
293
294 ret = run_queries (context);
295
296 GNUNET_MYSQL_context_destroy (context);
297 GNUNET_free (config);
298
299 return ret;
300}
diff --git a/src/my/test_my.conf b/src/my/test_my.conf
deleted file mode 100644
index e69de29bb..000000000
--- a/src/my/test_my.conf
+++ /dev/null