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 int
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  {
106  /* Let's allow this for varsize */
107  return GNUNET_OK;
108  }
109  /* if a field is null, continue but
110  * remember that we now return a different result */
111  len = PQgetlength (result,
112  row,
113  fnum);
114  res = PQgetvalue (result,
115  row,
116  fnum);
117  GNUNET_assert (NULL != res);
118  *dst_size = len;
119  idst = GNUNET_malloc (len);
120  *((void **) dst) = idst;
121  GNUNET_memcpy (idst,
122  res,
123  len);
124  return GNUNET_OK;
125 }
126 
127 
130  void **dst,
131  size_t *sptr)
132 {
133  struct GNUNET_PQ_ResultSpec res =
135  &clean_varsize_blob, NULL,
136  (void *) (dst), 0, name, sptr };
137 
138  return res;
139 }
140 
141 
155 static int
157  PGresult *result,
158  int row,
159  const char *fname,
160  size_t *dst_size,
161  void *dst)
162 {
163  size_t len;
164  const char *res;
165  int fnum;
166 
167  (void) cls;
168  fnum = PQfnumber (result,
169  fname);
170  if (fnum < 0)
171  {
172  GNUNET_break (0);
174  "Result does not have field %s\n",
175  fname);
176  return GNUNET_SYSERR;
177  }
178  if (PQgetisnull (result,
179  row,
180  fnum))
181  {
182  GNUNET_break (0);
183  return GNUNET_SYSERR;
184  }
185 
186  /* if a field is null, continue but
187  * remember that we now return a different result */
188  len = PQgetlength (result,
189  row,
190  fnum);
191  if (*dst_size != len)
192  {
194  "Expected %u bytes for field `%s', got %u\n",
195  (unsigned int) *dst_size,
196  fname,
197  (unsigned int) len);
198  GNUNET_break (0);
199  return GNUNET_SYSERR;
200  }
201  res = PQgetvalue (result,
202  row,
203  fnum);
204  GNUNET_assert (NULL != res);
205  GNUNET_memcpy (dst,
206  res,
207  len);
208  return GNUNET_OK;
209 }
210 
211 
214  void *dst,
215  size_t dst_size)
216 {
217  struct GNUNET_PQ_ResultSpec res =
219  NULL, NULL,
220  (dst), dst_size, name, NULL };
221 
222  return res;
223 }
224 
225 
239 static int
241  PGresult *result,
242  int row,
243  const char *fname,
244  size_t *dst_size,
245  void *dst)
246 {
247  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
248  size_t len;
249  const char *res;
250  int fnum;
251 
252  (void) cls;
253  *pk = NULL;
254  fnum = PQfnumber (result,
255  fname);
256  if (fnum < 0)
257  {
258  GNUNET_break (0);
259  return GNUNET_SYSERR;
260  }
261  if (PQgetisnull (result,
262  row,
263  fnum))
264  {
265  GNUNET_break (0);
266  return GNUNET_SYSERR;
267  }
268  /* if a field is null, continue but
269  * remember that we now return a different result */
270  len = PQgetlength (result,
271  row,
272  fnum);
273  res = PQgetvalue (result,
274  row,
275  fnum);
277  len);
278  if (NULL == *pk)
279  {
280  GNUNET_break (0);
281  return GNUNET_SYSERR;
282  }
283  return GNUNET_OK;
284 }
285 
286 
294 static void
296  void *rd)
297 {
298  struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
299 
300  (void) cls;
301  if (NULL != *pk)
302  {
304  *pk = NULL;
305  }
306 }
307 
308 
311  struct GNUNET_CRYPTO_RsaPublicKey **rsa)
312 {
313  struct GNUNET_PQ_ResultSpec res =
316  NULL,
317  (void *) rsa, 0, name, NULL };
318 
319  return res;
320 }
321 
322 
336 static int
338  PGresult *result,
339  int row,
340  const char *fname,
341  size_t *dst_size,
342  void *dst)
343 {
344  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
345  size_t len;
346  const void *res;
347  int fnum;
348 
349  (void) cls;
350  *sig = NULL;
351  fnum = PQfnumber (result,
352  fname);
353  if (fnum < 0)
354  {
355  GNUNET_break (0);
356  return GNUNET_SYSERR;
357  }
358  if (PQgetisnull (result,
359  row,
360  fnum))
361  {
362  GNUNET_break (0);
363  return GNUNET_SYSERR;
364  }
365  /* if a field is null, continue but
366  * remember that we now return a different result */
367  len = PQgetlength (result,
368  row,
369  fnum);
370  res = PQgetvalue (result,
371  row,
372  fnum);
374  len);
375  if (NULL == *sig)
376  {
377  GNUNET_break (0);
378  return GNUNET_SYSERR;
379  }
380  return GNUNET_OK;
381 }
382 
383 
391 static void
393  void *rd)
394 {
395  struct GNUNET_CRYPTO_RsaSignature **sig = rd;
396 
397  (void) cls;
398  if (NULL != *sig)
399  {
401  *sig = NULL;
402  }
403 }
404 
405 
408  struct GNUNET_CRYPTO_RsaSignature **sig)
409 {
410  struct GNUNET_PQ_ResultSpec res =
413  NULL,
414  (void *) sig, 0, (name), NULL };
415 
416  return res;
417 }
418 
419 
433 static int
435  PGresult *result,
436  int row,
437  const char *fname,
438  size_t *dst_size,
439  void *dst)
440 {
441  char **str = dst;
442  size_t len;
443  const char *res;
444  int fnum;
445 
446  (void) cls;
447  *str = NULL;
448  fnum = PQfnumber (result,
449  fname);
450  if (fnum < 0)
451  {
452  GNUNET_break (0);
453  return GNUNET_SYSERR;
454  }
455  if (PQgetisnull (result,
456  row,
457  fnum))
458  {
459  GNUNET_break (0);
460  return GNUNET_SYSERR;
461  }
462  /* if a field is null, continue but
463  * remember that we now return a different result */
464  len = PQgetlength (result,
465  row,
466  fnum);
467  res = PQgetvalue (result,
468  row,
469  fnum);
470  *str = GNUNET_strndup (res,
471  len);
472  if (NULL == *str)
473  {
474  GNUNET_break (0);
475  return GNUNET_SYSERR;
476  }
477  return GNUNET_OK;
478 }
479 
480 
488 static void
490  void *rd)
491 {
492  char **str = rd;
493 
494  (void) cls;
495  if (NULL != *str)
496  {
497  GNUNET_free (*str);
498  *str = NULL;
499  }
500 }
501 
502 
505  char **dst)
506 {
507  struct GNUNET_PQ_ResultSpec res =
508  { &extract_string,
509  &clean_string,
510  NULL,
511  (void *) dst, 0, (name), NULL };
512 
513  return res;
514 }
515 
516 
530 static int
532  PGresult *result,
533  int row,
534  const char *fname,
535  size_t *dst_size,
536  void *dst)
537 {
538  struct GNUNET_TIME_Relative *udst = dst;
539  const int64_t *res;
540  int fnum;
541 
542  (void) cls;
543  fnum = PQfnumber (result,
544  fname);
545  if (fnum < 0)
546  {
547  GNUNET_break (0);
548  return GNUNET_SYSERR;
549  }
550  if (PQgetisnull (result,
551  row,
552  fnum))
553  {
554  GNUNET_break (0);
555  return GNUNET_SYSERR;
556  }
557  GNUNET_assert (NULL != dst);
558  if (sizeof(struct GNUNET_TIME_Relative) != *dst_size)
559  {
560  GNUNET_break (0);
561  return GNUNET_SYSERR;
562  }
563  if (sizeof(int64_t) !=
564  PQgetlength (result,
565  row,
566  fnum))
567  {
568  GNUNET_break (0);
569  return GNUNET_SYSERR;
570  }
571  res = (int64_t *) PQgetvalue (result,
572  row,
573  fnum);
574  if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
576  else
577  udst->rel_value_us = GNUNET_ntohll ((uint64_t) *res);
578  return GNUNET_OK;
579 }
580 
581 
584  struct GNUNET_TIME_Relative *rt)
585 {
586  struct GNUNET_PQ_ResultSpec res = {
588  NULL,
589  NULL,
590  (void *) rt,
591  sizeof(*rt),
592  name,
593  NULL
594  };
595 
596  return res;
597 }
598 
599 
613 static int
615  PGresult *result,
616  int row,
617  const char *fname,
618  size_t *dst_size,
619  void *dst)
620 {
621  struct GNUNET_TIME_Absolute *udst = dst;
622  const int64_t *res;
623  int fnum;
624 
625  (void) cls;
626  fnum = PQfnumber (result,
627  fname);
628  if (fnum < 0)
629  {
630  GNUNET_break (0);
631  return GNUNET_SYSERR;
632  }
633  if (PQgetisnull (result,
634  row,
635  fnum))
636  {
637  GNUNET_break (0);
638  return GNUNET_SYSERR;
639  }
640  GNUNET_assert (NULL != dst);
641  if (sizeof(struct GNUNET_TIME_Absolute) != *dst_size)
642  {
643  GNUNET_break (0);
644  return GNUNET_SYSERR;
645  }
646  if (sizeof(int64_t) !=
647  PQgetlength (result,
648  row,
649  fnum))
650  {
651  GNUNET_break (0);
652  return GNUNET_SYSERR;
653  }
654  res = (int64_t *) PQgetvalue (result,
655  row,
656  fnum);
657  if (INT64_MAX == GNUNET_ntohll ((uint64_t) *res))
659  else
660  udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res);
661  return GNUNET_OK;
662 }
663 
664 
667  struct GNUNET_TIME_Absolute *at)
668 {
669  struct GNUNET_PQ_ResultSpec res =
670  { &extract_abs_time,
671  NULL,
672  NULL,
673  (void *) at, sizeof(*at), (name), NULL };
674 
675  return res;
676 }
677 
678 
681  struct GNUNET_TIME_AbsoluteNBO *at)
682 {
683  struct GNUNET_PQ_ResultSpec res =
684  GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__);
685 
686  return res;
687 }
688 
689 
703 static int
705  PGresult *result,
706  int row,
707  const char *fname,
708  size_t *dst_size,
709  void *dst)
710 {
711  uint16_t *udst = dst;
712  const uint16_t *res;
713  int fnum;
714 
715  (void) cls;
716  fnum = PQfnumber (result,
717  fname);
718  if (fnum < 0)
719  {
720  GNUNET_break (0);
721  return GNUNET_SYSERR;
722  }
723  if (PQgetisnull (result,
724  row,
725  fnum))
726  {
727  GNUNET_break (0);
728  return GNUNET_SYSERR;
729  }
730  GNUNET_assert (NULL != dst);
731  if (sizeof(uint16_t) != *dst_size)
732  {
733  GNUNET_break (0);
734  return GNUNET_SYSERR;
735  }
736  if (sizeof(uint16_t) !=
737  PQgetlength (result,
738  row,
739  fnum))
740  {
741  GNUNET_break (0);
742  return GNUNET_SYSERR;
743  }
744  res = (uint16_t *) PQgetvalue (result,
745  row,
746  fnum);
747  *udst = ntohs (*res);
748  return GNUNET_OK;
749 }
750 
751 
754  uint16_t *u16)
755 {
756  struct GNUNET_PQ_ResultSpec res =
757  { &extract_uint16,
758  NULL,
759  NULL,
760  (void *) u16, sizeof(*u16), (name), NULL };
761 
762  return res;
763 }
764 
765 
779 static int
781  PGresult *result,
782  int row,
783  const char *fname,
784  size_t *dst_size,
785  void *dst)
786 {
787  uint32_t *udst = dst;
788  const uint32_t *res;
789  int fnum;
790 
791  (void) cls;
792  fnum = PQfnumber (result,
793  fname);
794  if (fnum < 0)
795  {
796  GNUNET_break (0);
797  return GNUNET_SYSERR;
798  }
799  if (PQgetisnull (result,
800  row,
801  fnum))
802  {
803  GNUNET_break (0);
804  return GNUNET_SYSERR;
805  }
806  GNUNET_assert (NULL != dst);
807  if (sizeof(uint32_t) != *dst_size)
808  {
809  GNUNET_break (0);
810  return GNUNET_SYSERR;
811  }
812  if (sizeof(uint32_t) !=
813  PQgetlength (result,
814  row,
815  fnum))
816  {
817  GNUNET_break (0);
818  return GNUNET_SYSERR;
819  }
820  res = (uint32_t *) PQgetvalue (result,
821  row,
822  fnum);
823  *udst = ntohl (*res);
824  return GNUNET_OK;
825 }
826 
827 
830  uint32_t *u32)
831 {
832  struct GNUNET_PQ_ResultSpec res =
833  { &extract_uint32,
834  NULL,
835  NULL,
836  (void *) u32, sizeof(*u32), (name), NULL };
837 
838  return res;
839 }
840 
841 
855 static int
857  PGresult *result,
858  int row,
859  const char *fname,
860  size_t *dst_size,
861  void *dst)
862 {
863  uint64_t *udst = dst;
864  const uint64_t *res;
865  int fnum;
866 
867  (void) cls;
868  fnum = PQfnumber (result,
869  fname);
870  if (fnum < 0)
871  {
873  "Field %s missing in result\n",
874  fname);
875  GNUNET_break (0);
876  return GNUNET_SYSERR;
877  }
878  if (PQgetisnull (result,
879  row,
880  fnum))
881  {
882  GNUNET_break (0);
883  return GNUNET_SYSERR;
884  }
885  GNUNET_assert (NULL != dst);
886  if (sizeof(uint64_t) != *dst_size)
887  {
888  GNUNET_break (0);
889  return GNUNET_SYSERR;
890  }
891  if (sizeof(uint64_t) !=
892  PQgetlength (result,
893  row,
894  fnum))
895  {
896  GNUNET_break (0);
897  return GNUNET_SYSERR;
898  }
899  res = (uint64_t *) PQgetvalue (result,
900  row,
901  fnum);
902  *udst = GNUNET_ntohll (*res);
903  return GNUNET_OK;
904 }
905 
906 
909  uint64_t *u64)
910 {
911  struct GNUNET_PQ_ResultSpec res = {
913  NULL,
914  NULL,
915  (void *) u64, sizeof(*u64), (name), NULL
916  };
917 
918  return res;
919 }
920 
921 
922 /* 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 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:944
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.
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 int 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.
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. ...
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...
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:992
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
static int 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.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
static int 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_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.
static int 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.
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 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.
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
static int 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.
helper functions for Postgres DB interactions
static int 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_rsa_public_key(void *cls, void *rd)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
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 int 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
static int 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.
#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 int 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.
#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...