trying again to fix test_service timeout on v6 failure
[oweals/gnunet.git] / src / my / my_query_helper.c
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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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  */
38 static void
39 my_clean_query (void *cls,
40                 MYSQL_BIND *qbind)
41 {
42   GNUNET_free (qbind[0].buffer);
43 }
44
45
46 /**
47  * Function called to convert input argument into SQL parameters.
48  *
49  * @param cls closure
50  * @param pq data about the query
51  * @param qbind array of parameters to initialize
52  * @return -1 on error
53  */
54 static int
55 my_conv_fixed_size (void *cls,
56                     const struct GNUNET_MY_QueryParam *qp,
57                     MYSQL_BIND *qbind)
58 {
59   GNUNET_assert (1 == qp->num_params);
60   qbind->buffer = (void *) qp->data;
61   qbind->buffer_length = qp->data_len;
62   qbind->buffer_type = MYSQL_TYPE_BLOB;
63
64   return 1;
65 }
66
67
68 /**
69  * Generate query parameter for a buffer @a ptr of
70  * @a ptr_size bytes.
71  *
72  * @param ptr pointer to the query parameter to pass
73  * @param ptr_size number of bytes in @a ptr
74  */
75 struct GNUNET_MY_QueryParam
76 GNUNET_MY_query_param_fixed_size (const void *ptr,
77                                   size_t ptr_size)
78 {
79   struct GNUNET_MY_QueryParam qp = {
80     .conv = &my_conv_fixed_size,
81     .cleaner = NULL,
82     .conv_cls = NULL,
83     .num_params = 1,
84     .data = ptr,
85     .data_len = (unsigned long) ptr_size
86   };
87   return qp;
88 }
89
90
91 /**
92  * Function called to convert input argument into SQL parameters.
93  *
94  * @param cls closure
95  * @param pq data about the query
96  * @param qbind array of parameters to initialize
97  * @return -1 on error
98  */
99 static int
100 my_conv_string (void *cls,
101                 const struct GNUNET_MY_QueryParam *qp,
102                 MYSQL_BIND *qbind)
103 {
104   GNUNET_assert (1 == qp->num_params);
105
106   qbind->buffer = (void *) qp->data;
107   qbind->buffer_length = qp->data_len;
108   qbind->buffer_type = MYSQL_TYPE_STRING;
109
110   return 1;
111 }
112
113
114 /**
115  * Generate query parameter for a string
116  *
117  * @param ptr pointer to the string query parameter to pass
118  */
119 struct GNUNET_MY_QueryParam
120 GNUNET_MY_query_param_string (const char *ptr)
121 {
122   struct GNUNET_MY_QueryParam qp = {
123     .conv = &my_conv_string,
124     .cleaner = NULL,
125     .conv_cls = NULL,
126     .num_params = 1,
127     .data = ptr,
128     .data_len = strlen (ptr)
129   };
130   return qp;
131 }
132
133
134 /**
135  * Function called to convert input argument into SQL parameters
136  *
137  * @param cls closure
138  * @param pq data about the query
139  * @param qbind array of parameters to initialize
140  * @return -1 on error
141  */
142 static int
143 my_conv_uint16 (void *cls,
144                 const struct GNUNET_MY_QueryParam *qp,
145                 MYSQL_BIND *qbind)
146 {
147   GNUNET_assert (1 == qp->num_params);
148   qbind->buffer = (void *) qp->data;
149   qbind->buffer_length = sizeof (uint16_t);
150   qbind->buffer_type = MYSQL_TYPE_SHORT;
151   return 1;
152 }
153
154
155 /**
156  * Generate query parameter for an uint16_t in host byte order.
157  *
158  * @param x pointer to the query parameter to pass
159  */
160 struct GNUNET_MY_QueryParam
161 GNUNET_MY_query_param_uint16 (const uint16_t *x)
162 {
163   struct GNUNET_MY_QueryParam res = {
164     .conv = &my_conv_uint16,
165     .cleaner = NULL,
166     .conv_cls = NULL,
167     .num_params = 1,
168     .data = x,
169     .data_len = sizeof (*x)
170   };
171
172   return res;
173 }
174
175
176 /**
177  * Function called to convert input argument into SQL parameters
178  *
179  * @param cls closure
180  * @param pq data about the query
181  * @param qbind array of parameters to initialize
182  * @return -1 on error
183  */
184 static int
185 my_conv_uint32 (void *cls,
186                 const struct GNUNET_MY_QueryParam *qp,
187                 MYSQL_BIND *qbind)
188 {
189   GNUNET_assert (1 == qp->num_params);
190   qbind->buffer = (void *) qp->data;
191   qbind->buffer_length = sizeof(uint32_t);
192   qbind->buffer_type = MYSQL_TYPE_LONG;
193
194   return 1;
195 }
196
197
198 /**
199  * Generate query parameter for an uint32_t in host byte order
200  *
201  * @param x pointer to the query parameter to pass
202  */
203 struct GNUNET_MY_QueryParam
204 GNUNET_MY_query_param_uint32 (const uint32_t *x)
205 {
206   struct GNUNET_MY_QueryParam res = {
207     .conv = &my_conv_uint32,
208     .cleaner = NULL,
209     .conv_cls = NULL,
210     .num_params = 1,
211     .data = x,
212     .data_len = sizeof (*x)
213   };
214
215   return res;
216 }
217
218
219 /**
220  * Function called to convert input argument into SQL parameters
221  *
222  * @param cls closure
223  * @param pq data about the query
224  * @param qbind array of parameters to initialize
225  * @return -1 on error
226  */
227 static int
228 my_conv_uint64 (void *cls,
229                 const struct GNUNET_MY_QueryParam *qp,
230                 MYSQL_BIND * qbind)
231 {
232   GNUNET_assert (1 == qp->num_params);
233   qbind->buffer = (void *) qp->data;
234   qbind->buffer_length = sizeof (uint64_t);
235   qbind->buffer_type = MYSQL_TYPE_LONGLONG;
236   return 1;
237 }
238
239
240 /**
241  * Generate query parameter for an uint64_t in host byte order
242  *
243  * @param x pointer to the query parameter to pass
244  */
245 struct GNUNET_MY_QueryParam
246 GNUNET_MY_query_param_uint64 (const uint64_t *x)
247 {
248   struct GNUNET_MY_QueryParam res = {
249     .conv = &my_conv_uint64,
250     .cleaner = NULL,
251     .conv_cls = NULL,
252     .num_params = 1,
253     .data = x,
254     .data_len = sizeof(*x)
255   };
256
257   return res;
258 }
259
260
261 /**
262  * Function called to convert input argument into SQL parameters
263  *
264  * @param cls closure
265  * @param pq data about the query
266  * @param qbind array of parameters to initialize
267  * @return -1 on error
268  */
269 static int
270 my_conv_rsa_public_key (void *cls,
271                         const struct GNUNET_MY_QueryParam *qp,
272                         MYSQL_BIND * qbind)
273 {
274   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
275   char *buf;
276   size_t buf_size;
277
278   GNUNET_assert(1 == qp->num_params);
279
280   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
281
282   qbind->buffer = (void *) buf;
283   qbind->buffer_length = buf_size;
284   qbind->buffer_type = MYSQL_TYPE_BLOB;
285
286   return 1;
287 }
288
289
290 /**
291  * Generate query parameter for an RSA public key. The
292  * database must contain a BLOB type in the respective position.
293  *
294  * @param x the query parameter to pass
295  * @return array entry for the query parameters to use
296  */
297 struct GNUNET_MY_QueryParam
298 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
299 {
300   struct GNUNET_MY_QueryParam res = {
301     .conv = &my_conv_rsa_public_key,
302     .cleaner = &my_clean_query,
303     .conv_cls = NULL,
304     .num_params = 1,
305     .data = x,
306     .data_len = 0
307   };
308
309   return res;
310 }
311
312
313 /**
314   * Function called to convert input argument into SQL parameters
315   *
316   *@param cls closure
317   *@param pq data about the query
318   *@param qbind array of parameters to initialize
319   *@return -1 on error
320   */
321 static int
322 my_conv_rsa_signature (void *cls,
323                        const struct GNUNET_MY_QueryParam *qp,
324                        MYSQL_BIND *qbind)
325 {
326   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
327   char *buf;
328   size_t buf_size;
329
330   GNUNET_assert(1 == qp->num_params);
331
332   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
333                                                  &buf);
334   qbind->buffer = (void *) buf;
335   qbind->buffer_length = buf_size;
336   qbind->buffer_type = MYSQL_TYPE_BLOB;
337
338   return 1;
339 }
340
341
342 /**
343  * Generate query parameter for an RSA signature. The
344  * database must contain a BLOB type in the respective position
345  *
346  * @param x the query parameter to pass
347  * @return array entry for the query parameters to use
348  */
349 struct GNUNET_MY_QueryParam
350 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
351 {
352   struct GNUNET_MY_QueryParam res = {
353     .conv = &my_conv_rsa_signature,
354     .cleaner = &my_clean_query,
355     .conv_cls = NULL,
356     .num_params = 1,
357     .data = (x),
358     .data_len = 0
359   };
360   return res;
361 }
362
363
364 /**
365  * Generate query parameter for an absolute time value.
366  * The database must store a 64-bit integer.
367  *
368  * @param x pointer to the query parameter to pass
369  * @return array entry for the query parameters to use
370  */
371 struct GNUNET_MY_QueryParam
372 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
373 {
374   return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
375 }
376
377
378 /**
379  * Generate query parameter for an absolute time value.
380  * The database must store a 64-bit integer.
381  *
382  * @param x pointer to the query parameter to pass
383  */
384 struct GNUNET_MY_QueryParam
385 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
386 {
387   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
388 }
389
390
391 /* end of my_query_helper.c */