GNUnet  0.11.x
pq_result_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 
32  bool *is_null)
33 {
34  struct GNUNET_PQ_ResultSpec rsr;
35 
36  rsr = rs;
37  rsr.is_nullable = true;
38  rsr.is_null = is_null;
39  return rsr;
40 }
41 
42 
50 static void
52  void *rd)
53 {
54  void **dst = rd;
55 
56  (void) cls;
57  if (NULL != *dst)
58  {
59  GNUNET_free (*dst);
60  *dst = NULL;
61  }
62 }
63 
64 
78 static enum GNUNET_GenericReturnValue
80  PGresult *result,
81  int row,
82  const char *fname,
83  size_t *dst_size,
84  void *dst)
85 {
86  size_t len;
87  const char *res;
88  void *idst;
89  int fnum;
90 
91  (void) cls;
92  *dst_size = 0;
93  *((void **) dst) = NULL;
94 
95  fnum = PQfnumber (result,
96  fname);
97  if (fnum < 0)
98  {
99  GNUNET_break (0);
100  return GNUNET_SYSERR;
101  }
102  if (PQgetisnull (result,
103  row,
104  fnum))
105  return GNUNET_NO;
106  /* if a field is null, continue but
107  * remember that we now return a different result */
108  len = PQgetlength (result,
109  row,
110  fnum);
111  res = PQgetvalue (result,
112  row,
113  fnum);
114  GNUNET_assert (NULL != res);
115  *dst_size = len;
116  idst = GNUNET_malloc (len);
117  *((void **) dst) = idst;
118  GNUNET_memcpy (idst,
119  res,
120  len);
121  return GNUNET_OK;
122 }
123 
124 
127  void **dst,
128  size_t *sptr)
129 {
130  struct GNUNET_PQ_ResultSpec res =
132  &clean_varsize_blob, NULL,
133  (void *) (dst), 0, name, sptr };
134 
135  return res;
136 }
137 
138 
152 static enum GNUNET_GenericReturnValue
154  PGresult *result,
155  int row,
156  const char *fname,
157  size_t *dst_size,
158  void *dst)
159 {
160  size_t len;
161  const char *res;
162  int fnum;
163 
164  (void) cls;
165  fnum = PQfnumber (result,
166  fname);
167  if (fnum < 0)
168  {
169  GNUNET_break (0);
171  "Result does not have field %s\n",
172  fname);
173  return GNUNET_SYSERR;
174  }
175  if (PQgetisnull (result,
176  row,
177  fnum))
178  return GNUNET_NO;
179  /* if a field is null, continue but
180  * remember that we now return a different result */
181  len = PQgetlength (result,
182  row,
183  fnum);
184  if (*dst_size != len)
185  {
187  "Expected %u bytes for field `%s', got %u\n",
188  (unsigned int) *dst_size,
189  fname,
190  (unsigned int) len);
191  GNUNET_break (0);
192  return GNUNET_SYSERR;
193  }
194  res = PQgetvalue (result,
195  row,
196  fnum);
197  GNUNET_assert (NULL != res);
198  GNUNET_memcpy (dst,
199  res,
200  len);
201  return GNUNET_OK;
202 }
203 
204 
207  void *dst,
208  size_t dst_size)
209 {
210  struct GNUNET_PQ_ResultSpec res =
212  NULL, NULL,
213  (dst), dst_size, name, NULL };
214 
215  return res;
216 }
217 
218 
232 static enum GNUNET_GenericReturnValue
234  PGresult *result,
235  int row,
236  const char *fname,
237  size_t *dst_size,
238  void *dst)
239 {
240  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
241  size_t len;
242  const char *res;
243  int fnum;
244 
245  (void) cls;
246  *pk = NULL;
247  fnum = PQfnumber (result,
248  fname);
249  if (fnum < 0)
250  {
251  GNUNET_break (0);
252  return GNUNET_SYSERR;
253  }
254  if (PQgetisnull (result,
255  row,
256  fnum))
257  return GNUNET_NO;
258 
259  /* if a field is null, continue but
260  * remember that we now return a different result */
261  len = PQgetlength (result,
262  row,
263  fnum);
264  res = PQgetvalue (result,
265  row,
266  fnum);
268  len);
269  if (NULL == *pk)
270  {
271  GNUNET_break (0);
272  return GNUNET_SYSERR;
273  }
274  return GNUNET_OK;
275 }
276 
277 
285 static void
287  void *rd)
288 {
289  struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
290 
291  (void) cls;
292  if (NULL != *pk)
293  {
295  *pk = NULL;
296  }
297 }
298 
299 
302  struct GNUNET_CRYPTO_RsaPublicKey **rsa)
303 {
304  struct GNUNET_PQ_ResultSpec res =
307  NULL,
308  (void *) rsa, 0, name, NULL };
309 
310  return res;
311 }
312 
313 
327 static enum GNUNET_GenericReturnValue
329  PGresult *result,
330  int row,
331  const char *fname,
332  size_t *dst_size,
333  void *dst)
334 {
335  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
336  size_t len;
337  const void *res;
338  int fnum;
339 
340  (void) cls;
341  *sig = NULL;
342  fnum = PQfnumber (result,
343  fname);
344  if (fnum < 0)
345  {
346  GNUNET_break (0);
347  return GNUNET_SYSERR;
348  }
349  if (PQgetisnull (result,
350  row,
351  fnum))
352  return GNUNET_NO;
353  /* if a field is null, continue but
354  * remember that we now return a different result */
355  len = PQgetlength (result,
356  row,
357  fnum);
358  res = PQgetvalue (result,
359  row,
360  fnum);
362  len);
363  if (NULL == *sig)
364  {
365  GNUNET_break (0);
366  return GNUNET_SYSERR;
367  }
368  return GNUNET_OK;
369 }
370 
371 
379 static void
381  void *rd)
382 {
383  struct GNUNET_CRYPTO_RsaSignature **sig = rd;
384 
385  (void) cls;
386  if (NULL != *sig)
387  {
389  *sig = NULL;
390  }
391 }
392 
393 
396  struct GNUNET_CRYPTO_RsaSignature **sig)
397 {
398  struct GNUNET_PQ_ResultSpec res =
401  NULL,
402  (void *) sig, 0, (name), NULL };
403 
404  return res;
405 }
406 
407 
421 static enum GNUNET_GenericReturnValue
423  PGresult *result,
424  int row,
425  const char *fname,
426  size_t *dst_size,
427  void *dst)
428 {
429  char **str = dst;
430  size_t len;
431  const char *res;
432  int fnum;
433 
434  (void) cls;
435  *str = NULL;
436  fnum = PQfnumber (result,
437  fname);
438  if (fnum < 0)
439  {
440  GNUNET_break (0);
441  return GNUNET_SYSERR;
442  }
443  if (PQgetisnull (result,
444  row,
445  fnum))
446  return GNUNET_NO;
447  /* if a field is null, continue but
448  * remember that we now return a different result */
449  len = PQgetlength (result,
450  row,
451  fnum);
452  res = PQgetvalue (result,
453  row,
454  fnum);
455  *str = GNUNET_strndup (res,
456  len);
457  if (NULL == *str)
458  {
459  GNUNET_break (0);
460  return GNUNET_SYSERR;
461  }
462  return GNUNET_OK;
463 }
464 
465 
473 static void
475  void *rd)
476 {
477  char **str = rd;
478 
479  (void) cls;
480  if (NULL != *str)
481  {
482  GNUNET_free (*str);
483  *str = NULL;
484  }
485 }
486 
487 
490  char **dst)
491 {
492  struct GNUNET_PQ_ResultSpec res =
493  { &extract_string,
494  &clean_string,
495  NULL,
496  (void *) dst, 0, (name), NULL };
497 
498  return res;
499 }
500 
501 
515 static enum GNUNET_GenericReturnValue
517  PGresult *result,
518  int row,
519  const char *fname,
520  size_t *dst_size,
521  void *dst)
522 {
523  struct GNUNET_TIME_Relative *udst = dst;
524  const int64_t *res;
525  int fnum;
526 
527  (void) cls;
528  fnum = PQfnumber (result,
529  fname);
530  if (fnum < 0)
531  {
532  GNUNET_break (0);
533  return GNUNET_SYSERR;
534  }
535  if (PQgetisnull (result,
536  row,
537  fnum))
538  return GNUNET_NO;
539  GNUNET_assert (NULL != dst);
540  if (sizeof(struct GNUNET_TIME_Relative) != *dst_size)
541  {
542  GNUNET_break (0);
543  return GNUNET_SYSERR;
544  }
545  if (sizeof(int64_t) !=
546  PQgetlength (result,
547  row,
548  fnum))
549  {
550  GNUNET_break (0);
551  return GNUNET_SYSERR;
552  }
553  res = (int64_t *) PQgetvalue (result,
554  row,
555  fnum);
556  if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
558  else
559  udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res);
560  return GNUNET_OK;
561 }
562 
563 
566  struct GNUNET_TIME_Relative *rt)
567 {
568  struct GNUNET_PQ_ResultSpec res = {
570  NULL,
571  NULL,
572  (void *) rt,
573  sizeof(*rt),
574  name,
575  NULL
576  };
577 
578  return res;
579 }
580 
581 
595 static enum GNUNET_GenericReturnValue
597  PGresult *result,
598  int row,
599  const char *fname,
600  size_t *dst_size,
601  void *dst)
602 {
603  struct GNUNET_TIME_Absolute *udst = dst;
604  const int64_t *res;
605  int fnum;
606 
607  (void) cls;
608  fnum = PQfnumber (result,
609  fname);
610  if (fnum < 0)
611  {
612  GNUNET_break (0);
613  return GNUNET_SYSERR;
614  }
615  if (PQgetisnull (result,
616  row,
617  fnum))
618  return GNUNET_NO;
619  GNUNET_assert (NULL != dst);
620  if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size)
621  {
622  GNUNET_break (0);
623  return GNUNET_SYSERR;
624  }
625  if (sizeof(int64_t) !=
626  PQgetlength (result,
627  row,
628  fnum))
629  {
630  GNUNET_break (0);
631  return GNUNET_SYSERR;
632  }
633  res = (int64_t *) PQgetvalue (result,
634  row,
635  fnum);
636  if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
638  else
639  udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res);
640  return GNUNET_OK;
641 }
642 
643 
646  struct GNUNET_TIME_Absolute *at)
647 {
648  struct GNUNET_PQ_ResultSpec res =
649  { &extract_abs_time,
650  NULL,
651  NULL,
652  (void *) at, sizeof(*at), (name), NULL };
653 
654  return res;
655 }
656 
657 
660  struct GNUNET_TIME_AbsoluteNBO *at)
661 {
662  struct GNUNET_PQ_ResultSpec res =
663  GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__);
664 
665  return res;
666 }
667 
668 
682 static enum GNUNET_GenericReturnValue
684  PGresult *result,
685  int row,
686  const char *fname,
687  size_t *dst_size,
688  void *dst)
689 {
690  uint16_t *udst = dst;
691  const uint16_t *res;
692  int fnum;
693 
694  (void) cls;
695  fnum = PQfnumber (result,
696  fname);
697  if (fnum < 0)
698  {
699  GNUNET_break (0);
700  return GNUNET_SYSERR;
701  }
702  if (PQgetisnull (result,
703  row,
704  fnum))
705  return GNUNET_NO;
706  GNUNET_assert (NULL != dst);
707  if (sizeof(uint16_t) != *dst_size)
708  {
709  GNUNET_break (0);
710  return GNUNET_SYSERR;
711  }
712  if (sizeof(uint16_t) !=
713  PQgetlength (result,
714  row,
715  fnum))
716  {
717  GNUNET_break (0);
718  return GNUNET_SYSERR;
719  }
720  res = (uint16_t *) PQgetvalue (result,
721  row,
722  fnum);
723  *udst = ntohs (*res);
724  return GNUNET_OK;
725 }
726 
727 
730  uint16_t *u16)
731 {
732  struct GNUNET_PQ_ResultSpec res =
733  { &extract_uint16,
734  NULL,
735  NULL,
736  (void *) u16, sizeof(*u16), (name), NULL };
737 
738  return res;
739 }
740 
741 
755 static enum GNUNET_GenericReturnValue
757  PGresult *result,
758  int row,
759  const char *fname,
760  size_t *dst_size,
761  void *dst)
762 {
763  uint32_t *udst = dst;
764  const uint32_t *res;
765  int fnum;
766 
767  (void) cls;
768  fnum = PQfnumber (result,
769  fname);
770  if (fnum < 0)
771  {
772  GNUNET_break (0);
773  return GNUNET_SYSERR;
774  }
775  if (PQgetisnull (result,
776  row,
777  fnum))
778  return GNUNET_NO;
779  GNUNET_assert (NULL != dst);
780  if (sizeof(uint32_t) != *dst_size)
781  {
782  GNUNET_break (0);
783  return GNUNET_SYSERR;
784  }
785  if (sizeof(uint32_t) !=
786  PQgetlength (result,
787  row,
788  fnum))
789  {
790  GNUNET_break (0);
791  return GNUNET_SYSERR;
792  }
793  res = (uint32_t *) PQgetvalue (result,
794  row,
795  fnum);
796  *udst = ntohl (*res);
797  return GNUNET_OK;
798 }
799 
800 
803  uint32_t *u32)
804 {
805  struct GNUNET_PQ_ResultSpec res = {
807  NULL,
808  NULL,
809  (void *) u32,
810  sizeof(*u32),
811  (name),
812  NULL
813  };
814 
815  return res;
816 }
817 
818 
832 static enum GNUNET_GenericReturnValue
834  PGresult *result,
835  int row,
836  const char *fname,
837  size_t *dst_size,
838  void *dst)
839 {
840  uint64_t *udst = dst;
841  const uint64_t *res;
842  int fnum;
843 
844  (void) cls;
845  fnum = PQfnumber (result,
846  fname);
847  if (fnum < 0)
848  {
850  "Field %s missing in result\n",
851  fname);
852  GNUNET_break (0);
853  return GNUNET_SYSERR;
854  }
855  if (PQgetisnull (result,
856  row,
857  fnum))
858  return GNUNET_NO;
859 
860  GNUNET_assert (NULL != dst);
861  if (sizeof(uint64_t) != *dst_size)
862  {
863  GNUNET_break (0);
864  return GNUNET_SYSERR;
865  }
866  if (sizeof(uint64_t) !=
867  PQgetlength (result,
868  row,
869  fnum))
870  {
871  GNUNET_break (0);
872  return GNUNET_SYSERR;
873  }
874  res = (uint64_t *) PQgetvalue (result,
875  row,
876  fnum);
877  *udst = GNUNET_ntohll (*res);
878  return GNUNET_OK;
879 }
880 
881 
884  uint64_t *u64)
885 {
886  struct GNUNET_PQ_ResultSpec res = {
888  NULL,
889  NULL,
890  (void *) u64,
891  sizeof(*u64),
892  (name),
893  NULL
894  };
895 
896  return res;
897 }
898 
899 
900 /* end of pq_result_helper.c */
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_public_key(const char *name, struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
static enum GNUNET_GenericReturnValue extract_string(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_string(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
uint64_t rel_value_us
The actual value.
an RSA signature
Definition: crypto_rsa.c:63
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:945
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint16(const char *name, uint16_t *u16)
uint16_t expected.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static enum GNUNET_GenericReturnValue extract_varsize_blob(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time_nbo(const char *name, struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
const char * fname
Field name of the desired result.
Description of a DB result cell.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static enum GNUNET_GenericReturnValue extract_fixed_blob(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
bool is_nullable
True if NULL is allowed for a value in the database.
static void clean_rsa_signature(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
static enum GNUNET_GenericReturnValue extract_rel_time(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static enum GNUNET_GenericReturnValue extract_uint64(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
uint64_t abs_value_us
The actual value.
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static enum GNUNET_GenericReturnValue extract_uint16(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_signature_decode(const void *buf, size_t buf_size)
Decode the signature from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:993
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_rsa_signature(const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.
void * cls
Closure for conv and cleaner.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_fixed_size(const char *name, void *dst, size_t dst_size)
Fixed-size result expected.
struct GNUNET_CRYPTO_RsaPublicKey * GNUNET_CRYPTO_rsa_public_key_decode(const char *buf, size_t len)
Decode the public key from the data-format back to the "normal", internal format. ...
Definition: crypto_rsa.c:383
static int result
Global testing status.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...
static int res
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
bool * is_null
Points to a location where we should store "true" if the result found is NULL, and otherwise "false"...
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_allow_null(struct GNUNET_PQ_ResultSpec rs, bool *is_null)
Allow NULL value to be found in the database for the given value.
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
helper functions for Postgres DB interactions
static void clean_rsa_public_key(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static enum GNUNET_GenericReturnValue extract_abs_time(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static enum GNUNET_GenericReturnValue extract_rsa_signature(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_varsize_blob(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
static enum GNUNET_GenericReturnValue extract_rsa_public_key(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
const char * name
#define GNUNET_log(kind,...)
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:267
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
Time for absolute times used by GNUnet, in microseconds.
void * dst
Destination for the data.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_relative_time(const char *name, struct GNUNET_TIME_Relative *rt)
Relative time expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
static enum GNUNET_GenericReturnValue extract_uint32(void *cls, PGresult *result, int row, const char *fname, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:53
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...