GNUnet  0.19.3
my_query_helper.c
Go to the documentation of this file.
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  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_mysql_compat.h"
29 #include "gnunet_my_lib.h"
30 
31 
39 static void
40 my_clean_query (void *cls,
41  MYSQL_BIND *qbind)
42 {
43  (void) cls;
44  GNUNET_free (qbind[0].buffer);
45 }
46 
47 
56 static int
57 my_conv_fixed_size (void *cls,
58  const struct GNUNET_MY_QueryParam *qp,
59  MYSQL_BIND *qbind)
60 {
61  (void) cls;
62  GNUNET_assert (1 == qp->num_params);
63  qbind->buffer = (void *) qp->data;
64  qbind->buffer_length = qp->data_len;
65  qbind->buffer_type = MYSQL_TYPE_BLOB;
66 
67  return 1;
68 }
69 
70 
79 GNUNET_MY_query_param_fixed_size (const void *ptr,
80  size_t ptr_size)
81 {
82  struct GNUNET_MY_QueryParam qp = {
84  .cleaner = NULL,
85  .conv_cls = NULL,
86  .num_params = 1,
87  .data = ptr,
88  .data_len = (unsigned long) ptr_size
89  };
90 
91  return qp;
92 }
93 
94 
103 static int
104 my_conv_string (void *cls,
105  const struct GNUNET_MY_QueryParam *qp,
106  MYSQL_BIND *qbind)
107 {
108  (void) cls;
109  GNUNET_assert (1 == qp->num_params);
110  qbind->buffer = (void *) qp->data;
111  qbind->buffer_length = qp->data_len;
112  qbind->buffer_type = MYSQL_TYPE_STRING;
113  return 1;
114 }
115 
116 
123 GNUNET_MY_query_param_string (const char *ptr)
124 {
125  struct GNUNET_MY_QueryParam qp = {
126  .conv = &my_conv_string,
127  .cleaner = NULL,
128  .conv_cls = NULL,
129  .num_params = 1,
130  .data = ptr,
131  .data_len = strlen (ptr)
132  };
133 
134  return qp;
135 }
136 
137 
146 static int
147 my_conv_uint16 (void *cls,
148  const struct GNUNET_MY_QueryParam *qp,
149  MYSQL_BIND *qbind)
150 {
151  (void) cls;
152  GNUNET_assert (1 == qp->num_params);
153  qbind->buffer = (void *) qp->data;
154  qbind->buffer_length = sizeof(uint16_t);
155  qbind->buffer_type = MYSQL_TYPE_SHORT;
156  qbind->is_unsigned = 1;
157  return 1;
158 }
159 
160 
167 GNUNET_MY_query_param_uint16 (const uint16_t *x)
168 {
169  struct GNUNET_MY_QueryParam res = {
170  .conv = &my_conv_uint16,
171  .cleaner = NULL,
172  .conv_cls = NULL,
173  .num_params = 1,
174  .data = x,
175  .data_len = sizeof(*x)
176  };
177 
178  return res;
179 }
180 
181 
190 static int
191 my_conv_uint32 (void *cls,
192  const struct GNUNET_MY_QueryParam *qp,
193  MYSQL_BIND *qbind)
194 {
195  (void) cls;
196  GNUNET_assert (1 == qp->num_params);
197  qbind->buffer = (void *) qp->data;
198  qbind->buffer_length = sizeof(uint32_t);
199  qbind->buffer_type = MYSQL_TYPE_LONG;
200  qbind->is_unsigned = 1;
201  return 1;
202 }
203 
204 
211 GNUNET_MY_query_param_uint32 (const uint32_t *x)
212 {
213  struct GNUNET_MY_QueryParam res = {
214  .conv = &my_conv_uint32,
215  .cleaner = NULL,
216  .conv_cls = NULL,
217  .num_params = 1,
218  .data = x,
219  .data_len = sizeof(*x)
220  };
221 
222  return res;
223 }
224 
225 
234 static int
235 my_conv_uint64 (void *cls,
236  const struct GNUNET_MY_QueryParam *qp,
237  MYSQL_BIND *qbind)
238 {
239  (void) cls;
240  GNUNET_assert (1 == qp->num_params);
241  qbind->buffer = (void *) qp->data;
242  qbind->buffer_length = sizeof(uint64_t);
243  qbind->buffer_type = MYSQL_TYPE_LONGLONG;
244  qbind->is_unsigned = 1;
245  return 1;
246 }
247 
248 
255 GNUNET_MY_query_param_uint64 (const uint64_t *x)
256 {
257  struct GNUNET_MY_QueryParam res = {
258  .conv = &my_conv_uint64,
259  .cleaner = NULL,
260  .conv_cls = NULL,
261  .num_params = 1,
262  .data = x,
263  .data_len = sizeof(*x)
264  };
265 
266  return res;
267 }
268 
269 
278 static int
280  const struct GNUNET_MY_QueryParam *qp,
281  MYSQL_BIND *qbind)
282 {
283  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
284  void *buf;
285  size_t buf_size;
286 
287  (void) cls;
288  GNUNET_assert (1 == qp->num_params);
289  buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
290  &buf);
291  qbind->buffer = buf;
292  qbind->buffer_length = buf_size;
293  qbind->buffer_type = MYSQL_TYPE_BLOB;
294  return 1;
295 }
296 
297 
308 {
309  struct GNUNET_MY_QueryParam res = {
310  .conv = &my_conv_rsa_public_key,
311  .cleaner = &my_clean_query,
312  .conv_cls = NULL,
313  .num_params = 1,
314  .data = x,
315  .data_len = 0
316  };
317 
318  return res;
319 }
320 
321 
330 static int
332  const struct GNUNET_MY_QueryParam *qp,
333  MYSQL_BIND *qbind)
334 {
335  const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
336  void *buf;
337  size_t buf_size;
338 
339  (void) cls;
340  GNUNET_assert (1 == qp->num_params);
341  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
342  &buf);
343  qbind->buffer = buf;
344  qbind->buffer_length = buf_size;
345  qbind->buffer_type = MYSQL_TYPE_BLOB;
346  return 1;
347 }
348 
349 
359 {
360  struct GNUNET_MY_QueryParam res = {
361  .conv = &my_conv_rsa_signature,
362  .cleaner = &my_clean_query,
363  .conv_cls = NULL,
364  .num_params = 1,
365  .data = (x),
366  .data_len = 0
367  };
368 
369  return res;
370 }
371 
372 
382 {
383  return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
384 }
385 
386 
396 {
397  return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
398 }
399 
400 
401 /* end of my_query_helper.c */
static int res
static char buf[2048]
Helper library to access a MySQL database.
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:325
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, void **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:999
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
#define GNUNET_MY_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_string(const char *ptr)
Generate query parameter for a string.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
static int my_conv_string(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static void my_clean_query(void *cls, MYSQL_BIND *qbind)
Function called to clean up memory allocated by a GNUNET_MY_QueryConverter.
static int my_conv_fixed_size(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_rsa_signature(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_uint64(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_uint32(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_uint16(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_rsa_public_key(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Information we pass to GNUNET_MY_exec_prepared() to initialize the arguments of the prepared statemen...
Definition: gnunet_my_lib.h:93
unsigned long data_len
Information to pass to conv.
const void * data
Information to pass to conv.
unsigned int num_params
Number of arguments the conv converter expects to initialize.
GNUNET_MY_QueryConverter conv
Function to call for the type conversion.
Definition: gnunet_my_lib.h:97
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.