GNUnet  0.11.x
pq_query_helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 2016, 2020 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  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
28 
29 
44 static int
45 qconv_null (void *cls,
46  const void *data,
47  size_t data_len,
48  void *param_values[],
49  int param_lengths[],
50  int param_formats[],
51  unsigned int param_length,
52  void *scratch[],
53  unsigned int scratch_length)
54 {
55  (void) scratch;
56  (void) scratch_length;
57  (void) data;
58  (void) data_len;
59  GNUNET_break (NULL == cls);
60  if (1 != param_length)
61  return -1;
62  param_values[0] = NULL;
63  param_lengths[0] = 0;
64  param_formats[0] = 1;
65  return 0;
66 }
67 
68 
71 {
72  struct GNUNET_PQ_QueryParam res = {
73  .conv = &qconv_null,
74  .num_params = 1
75  };
76 
77  return res;
78 }
79 
80 
95 static int
96 qconv_fixed (void *cls,
97  const void *data,
98  size_t data_len,
99  void *param_values[],
100  int param_lengths[],
101  int param_formats[],
102  unsigned int param_length,
103  void *scratch[],
104  unsigned int scratch_length)
105 {
106  (void) scratch;
107  (void) scratch_length;
108  GNUNET_break (NULL == cls);
109  if (1 != param_length)
110  return -1;
111  param_values[0] = (void *) data;
112  param_lengths[0] = data_len;
113  param_formats[0] = 1;
114  return 0;
115 }
116 
117 
119 GNUNET_PQ_query_param_fixed_size (const void *ptr,
120  size_t ptr_size)
121 {
122  struct GNUNET_PQ_QueryParam res = {
123  &qconv_fixed, NULL, ptr, ptr_size, 1
124  };
125 
126  return res;
127 }
128 
129 
131 GNUNET_PQ_query_param_string (const char *ptr)
132 {
134  strlen (ptr));
135 }
136 
137 
140 {
141  static uint8_t bt = 1;
142  static uint8_t bf = 0;
143 
144  return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
145  sizeof (uint8_t));
146 }
147 
148 
163 static int
164 qconv_uint16 (void *cls,
165  const void *data,
166  size_t data_len,
167  void *param_values[],
168  int param_lengths[],
169  int param_formats[],
170  unsigned int param_length,
171  void *scratch[],
172  unsigned int scratch_length)
173 {
174  const uint16_t *u_hbo = data;
175  uint16_t *u_nbo;
176 
177  (void) scratch;
178  (void) scratch_length;
179  GNUNET_break (NULL == cls);
180  if (1 != param_length)
181  return -1;
182  u_nbo = GNUNET_new (uint16_t);
183  scratch[0] = u_nbo;
184  *u_nbo = htons (*u_hbo);
185  param_values[0] = (void *) u_nbo;
186  param_lengths[0] = sizeof(uint16_t);
187  param_formats[0] = 1;
188  return 1;
189 }
190 
191 
193 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
194 {
195  struct GNUNET_PQ_QueryParam res = {
196  .conv = &qconv_uint16,
197  .data = x,
198  .size = sizeof(*x),
199  .num_params = 1
200  };
201 
202  return res;
203 }
204 
205 
220 static int
221 qconv_uint32 (void *cls,
222  const void *data,
223  size_t data_len,
224  void *param_values[],
225  int param_lengths[],
226  int param_formats[],
227  unsigned int param_length,
228  void *scratch[],
229  unsigned int scratch_length)
230 {
231  const uint32_t *u_hbo = data;
232  uint32_t *u_nbo;
233 
234  (void) scratch;
235  (void) scratch_length;
236  GNUNET_break (NULL == cls);
237  if (1 != param_length)
238  return -1;
239  u_nbo = GNUNET_new (uint32_t);
240  scratch[0] = u_nbo;
241  *u_nbo = htonl (*u_hbo);
242  param_values[0] = (void *) u_nbo;
243  param_lengths[0] = sizeof(uint32_t);
244  param_formats[0] = 1;
245  return 1;
246 }
247 
248 
250 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
251 {
252  struct GNUNET_PQ_QueryParam res = {
253  .conv = &qconv_uint32,
254  .data = x,
255  .size = sizeof(*x),
256  .num_params = 1
257  };
258 
259  return res;
260 }
261 
262 
277 static int
278 qconv_uint64 (void *cls,
279  const void *data,
280  size_t data_len,
281  void *param_values[],
282  int param_lengths[],
283  int param_formats[],
284  unsigned int param_length,
285  void *scratch[],
286  unsigned int scratch_length)
287 {
288  const uint64_t *u_hbo = data;
289  uint64_t *u_nbo;
290 
291  (void) scratch;
292  (void) scratch_length;
293  GNUNET_break (NULL == cls);
294  if (1 != param_length)
295  return -1;
296  u_nbo = GNUNET_new (uint64_t);
297  scratch[0] = u_nbo;
298  *u_nbo = GNUNET_htonll (*u_hbo);
299  param_values[0] = (void *) u_nbo;
300  param_lengths[0] = sizeof(uint64_t);
301  param_formats[0] = 1;
302  return 1;
303 }
304 
305 
307 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
308 {
309  struct GNUNET_PQ_QueryParam res = {
310  .conv = &qconv_uint64,
311  .data = x,
312  .size = sizeof(*x),
313  .num_params = 1
314  };
315 
316  return res;
317 }
318 
319 
334 static int
336  const void *data,
337  size_t data_len,
338  void *param_values[],
339  int param_lengths[],
340  int param_formats[],
341  unsigned int param_length,
342  void *scratch[],
343  unsigned int scratch_length)
344 {
345  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
346  void *buf;
347  size_t buf_size;
348 
349  GNUNET_break (NULL == cls);
350  if (1 != param_length)
351  return -1;
352  buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
353  &buf);
354  scratch[0] = buf;
355  param_values[0] = (void *) buf;
356  param_lengths[0] = buf_size;
357  param_formats[0] = 1;
358  return 1;
359 }
360 
361 
364  const struct GNUNET_CRYPTO_RsaPublicKey *x)
365 {
366  struct GNUNET_PQ_QueryParam res = {
367  .conv = &qconv_rsa_public_key,
368  .data = x,
369  .num_params = 1
370  };
371 
372  return res;
373 }
374 
375 
390 static int
392  const void *data,
393  size_t data_len,
394  void *param_values[],
395  int param_lengths[],
396  int param_formats[],
397  unsigned int param_length,
398  void *scratch[],
399  unsigned int scratch_length)
400 {
401  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
402  void *buf;
403  size_t buf_size;
404 
405  GNUNET_break (NULL == cls);
406  if (1 != param_length)
407  return -1;
408  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
409  &buf);
410  scratch[0] = buf;
411  param_values[0] = (void *) buf;
412  param_lengths[0] = buf_size;
413  param_formats[0] = 1;
414  return 1;
415 }
416 
417 
420 {
421  struct GNUNET_PQ_QueryParam res = {
422  .conv = &qconv_rsa_signature,
423  .data = x,
424  .num_params = 1
425  };
426 
427  return res;
428 }
429 
430 
445 static int
446 qconv_rel_time (void *cls,
447  const void *data,
448  size_t data_len,
449  void *param_values[],
450  int param_lengths[],
451  int param_formats[],
452  unsigned int param_length,
453  void *scratch[],
454  unsigned int scratch_length)
455 {
456  const struct GNUNET_TIME_Relative *u = data;
457  struct GNUNET_TIME_Relative rel;
458  uint64_t *u_nbo;
459 
460  GNUNET_break (NULL == cls);
461  if (1 != param_length)
462  return -1;
463  rel = *u;
464  if (rel.rel_value_us > INT64_MAX)
465  rel.rel_value_us = INT64_MAX;
466  u_nbo = GNUNET_new (uint64_t);
467  scratch[0] = u_nbo;
468  *u_nbo = GNUNET_htonll (rel.rel_value_us);
469  param_values[0] = (void *) u_nbo;
470  param_lengths[0] = sizeof(uint64_t);
471  param_formats[0] = 1;
472  return 1;
473 }
474 
475 
478 {
479  struct GNUNET_PQ_QueryParam res = {
480  .conv = &qconv_rel_time,
481  .data = x,
482  .size = sizeof(*x),
483  .num_params = 1
484  };
485 
486  return res;
487 }
488 
489 
504 static int
505 qconv_abs_time (void *cls,
506  const void *data,
507  size_t data_len,
508  void *param_values[],
509  int param_lengths[],
510  int param_formats[],
511  unsigned int param_length,
512  void *scratch[],
513  unsigned int scratch_length)
514 {
515  const struct GNUNET_TIME_Absolute *u = data;
516  struct GNUNET_TIME_Absolute abs;
517  uint64_t *u_nbo;
518 
519  GNUNET_break (NULL == cls);
520  if (1 != param_length)
521  return -1;
522  abs = *u;
523  if (abs.abs_value_us > INT64_MAX)
524  abs.abs_value_us = INT64_MAX;
525  u_nbo = GNUNET_new (uint64_t);
526  scratch[0] = u_nbo;
527  *u_nbo = GNUNET_htonll (abs.abs_value_us);
528  param_values[0] = (void *) u_nbo;
529  param_lengths[0] = sizeof(uint64_t);
530  param_formats[0] = 1;
531  return 1;
532 }
533 
534 
537 {
538  struct GNUNET_PQ_QueryParam res = {
539  .conv = &qconv_abs_time,
540  .data = x,
541  .size = sizeof(*x),
542  .num_params = 1
543  };
544 
545  return res;
546 }
547 
548 
551  const struct GNUNET_TIME_AbsoluteNBO *x)
552 {
553  return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
554 }
555 
556 
559 {
560  return GNUNET_PQ_query_param_absolute_time (&x->abs_time);
561 }
562 
563 
566  const struct GNUNET_TIME_TimestampNBO *x)
567 {
568  return GNUNET_PQ_query_param_absolute_time_nbo (&x->abs_time_nbo);
569 }
570 
571 
572 /* end of pq_query_helper.c */
static int res
uint32_t data
The data value.
static char buf[2048]
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:36
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:324
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:998
helper functions for Postgres DB interactions
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
static int qconv_fixed(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
static int qconv_rel_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null(void)
Generate query parameter to create a NULL value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_relative_time(const struct GNUNET_TIME_Relative *x)
Generate query parameter for a relative time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
static int qconv_null(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_bool(bool b)
Pass a boolean into a query.
static int qconv_uint64(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_uint32(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_abs_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_rsa_signature(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
static int qconv_uint16(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp(const struct GNUNET_TIME_Timestamp *x)
Generate query parameter for a timestamp.
static int qconv_rsa_public_key(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp_nbo(const struct GNUNET_TIME_TimestampNBO *x)
Generate query parameter for a timestamp in NBO.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
The public information of an RSA key pair.
Definition: crypto_rsa.c:52
an RSA signature
Definition: crypto_rsa.c:64
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:65
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Rounded time for timestamps used by GNUnet, in seconds.