GNUnet  0.19.5
my_result_helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 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 "gnunet_util_lib.h"
28 #include "gnunet_mysql_compat.h"
29 #include "gnunet_my_lib.h"
30 
31 
44 static int
46  struct GNUNET_MY_ResultSpec *rs,
47  MYSQL_STMT *stmt,
48  unsigned int column,
49  MYSQL_BIND *results)
50 {
51  results[0].buffer = NULL;
52  results[0].buffer_length = 0;
53  results[0].length = &rs->mysql_bind_output_length;
54  results[0].is_null = &rs->is_null;
55  rs->is_null = 0;
56 
57  return GNUNET_OK;
58 }
59 
60 
73 static int
75  struct GNUNET_MY_ResultSpec *rs,
76  MYSQL_STMT *stmt,
77  unsigned int column,
78  MYSQL_BIND *results)
79 {
80  void *buf;
81  size_t size;
82 
83  if (*results->is_null)
84  return GNUNET_SYSERR;
85  size = (size_t) rs->mysql_bind_output_length;
86 
87  if (rs->mysql_bind_output_length != size)
88  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
89 
91 
92  results[0].buffer = buf;
93  results[0].buffer_length = size;
94  results[0].buffer_type = MYSQL_TYPE_BLOB;
95 
96  if (0 !=
97  mysql_stmt_fetch_column (stmt,
98  results,
99  column,
100  0))
101  {
102  GNUNET_free (buf);
103  return GNUNET_SYSERR;
104  }
105 
106  *(void **) rs->dst = buf;
107  *rs->result_size = size;
108 
109  return GNUNET_OK;
110 }
111 
112 
119 static void
121  struct GNUNET_MY_ResultSpec *rs)
122 {
123  void **ptr = (void **) rs->dst;
124 
125  if (NULL != *ptr)
126  {
127  GNUNET_free (*ptr);
128  *ptr = NULL;
129  }
130 }
131 
132 
135  size_t *ptr_size)
136 {
137  struct GNUNET_MY_ResultSpec res = {
138  .pre_conv = &pre_extract_varsize_blob,
139  .post_conv = &post_extract_varsize_blob,
140  .cleaner = &cleanup_varsize_blob,
141  .dst = (void *) (dst),
142  .result_size = ptr_size,
143  .num_fields = 1
144  };
145 
146  return res;
147 }
148 
149 
162 static int
164  struct GNUNET_MY_ResultSpec *rs,
165  MYSQL_STMT *stmt,
166  unsigned int column,
167  MYSQL_BIND *results)
168 {
169  results[0].buffer = rs->dst;
170  results[0].buffer_length = rs->dst_size;
171  results[0].length = &rs->mysql_bind_output_length;
172  results[0].buffer_type = MYSQL_TYPE_BLOB;
173  results[0].is_null = &rs->is_null;
174  rs->is_null = 0;
175 
176  return GNUNET_OK;
177 }
178 
179 
193 static int
195  struct GNUNET_MY_ResultSpec *rs,
196  MYSQL_STMT *stmt,
197  unsigned int column,
198  MYSQL_BIND *results)
199 {
200  if (*results->is_null)
201  return GNUNET_SYSERR;
202  if (rs->dst_size != rs->mysql_bind_output_length)
203  return GNUNET_SYSERR;
204  return GNUNET_OK;
205 }
206 
207 
218  size_t ptr_size)
219 {
220  struct GNUNET_MY_ResultSpec res = {
221  .pre_conv = &pre_extract_fixed_blob,
222  .post_conv = &post_extract_fixed_blob,
223  .cleaner = NULL,
224  .dst = (void *) (ptr),
225  .dst_size = ptr_size,
226  .num_fields = 1
227  };
228 
229  return res;
230 }
231 
232 
245 static int
247  struct GNUNET_MY_ResultSpec *rs,
248  MYSQL_STMT *stmt,
249  unsigned int column,
250  MYSQL_BIND *results)
251 {
252  results[0].buffer = NULL;
253  results[0].buffer_length = 0;
254  results[0].length = &rs->mysql_bind_output_length;
255  results[0].buffer_type = MYSQL_TYPE_BLOB;
256  results[0].is_null = &rs->is_null;
257  rs->is_null = 0;
258 
259  return GNUNET_OK;
260 }
261 
262 
276 static int
278  struct GNUNET_MY_ResultSpec *rs,
279  MYSQL_STMT *stmt,
280  unsigned int column,
281  MYSQL_BIND *results)
282 
283 {
284  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
285  void *buf;
286  size_t size;
287 
288  if (*results->is_null)
289  return GNUNET_SYSERR;
290  size = (size_t) rs->mysql_bind_output_length;
291 
292  if (rs->mysql_bind_output_length != size)
293  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
294  buf = GNUNET_malloc (size);
295 
296  results[0].buffer = buf;
297  results[0].buffer_length = size;
298  results[0].buffer_type = MYSQL_TYPE_BLOB;
299  if (0 !=
300  mysql_stmt_fetch_column (stmt,
301  results,
302  column,
303  0))
304  {
305  GNUNET_free (buf);
306  return GNUNET_SYSERR;
307  }
308 
310  size);
311  GNUNET_free (buf);
312  if (NULL == *pk)
313  {
315  "Results contains bogus public key value (fail to decode)\n");
316  return GNUNET_SYSERR;
317  }
318 
319  return GNUNET_OK;
320 }
321 
322 
330 static void
332  struct GNUNET_MY_ResultSpec *rs)
333 {
334  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
335 
336  if (NULL != *pk)
337  {
339  *pk = NULL;
340  }
341 }
342 
343 
353 {
354  struct GNUNET_MY_ResultSpec res = {
355  .pre_conv = &pre_extract_rsa_public_key,
356  .post_conv = &post_extract_rsa_public_key,
357  .cleaner = &clean_rsa_public_key,
358  .dst = (void *) rsa,
359  .dst_size = 0,
360  .num_fields = 1
361  };
362 
363  return res;
364 }
365 
366 
379 static int
381  struct GNUNET_MY_ResultSpec *rs,
382  MYSQL_STMT *stmt,
383  unsigned int column,
384  MYSQL_BIND *results)
385 {
386  results[0].buffer = 0;
387  results[0].buffer_length = 0;
388  results[0].length = &rs->mysql_bind_output_length;
389  results[0].buffer_type = MYSQL_TYPE_BLOB;
390  results[0].is_null = &rs->is_null;
391  rs->is_null = 0;
392 
393  return GNUNET_OK;
394 }
395 
396 
409 static int
411  struct GNUNET_MY_ResultSpec *rs,
412  MYSQL_STMT *stmt,
413  unsigned int column,
414  MYSQL_BIND *results)
415 {
416  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
417  void *buf;
418  size_t size;
419 
420  if (*results->is_null)
421  return GNUNET_SYSERR;
422  size = (size_t) rs->mysql_bind_output_length;
423 
424  if (rs->mysql_bind_output_length != size)
425  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
426  buf = GNUNET_malloc (size);
427 
428  results[0].buffer = buf;
429  results[0].buffer_length = size;
430  results[0].buffer_type = MYSQL_TYPE_BLOB;
431  if (0 !=
432  mysql_stmt_fetch_column (stmt,
433  results,
434  column,
435  0))
436  {
437  GNUNET_free (buf);
438  return GNUNET_SYSERR;
439  }
440 
442  size);
443  GNUNET_free (buf);
444  if (NULL == *sig)
445  {
447  "Resuls contains bogus signature value (fails to decode)\n");
448  return GNUNET_SYSERR;
449  }
450  return GNUNET_OK;
451 }
452 
453 
461 static void
463  struct GNUNET_MY_ResultSpec *rs)
464 {
465  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
466 
467  if (NULL != *sig)
468  {
470  *sig = NULL;
471  }
472 }
473 
474 
483 {
484  struct GNUNET_MY_ResultSpec res = {
485  .pre_conv = &pre_extract_rsa_signature,
486  .post_conv = &post_extract_rsa_signature,
487  .cleaner = &clean_rsa_signature,
488  .dst = (void *) sig,
489  .dst_size = 0,
490  .num_fields = 1
491  };
492 
493  return res;
494 }
495 
496 
509 static int
511  struct GNUNET_MY_ResultSpec *rs,
512  MYSQL_STMT *stmt,
513  unsigned int column,
514  MYSQL_BIND *results)
515 {
516  results[0].buffer = NULL;
517  results[0].buffer_length = 0;
518  results[0].length = &rs->mysql_bind_output_length;
519  results[0].buffer_type = MYSQL_TYPE_BLOB;
520  results[0].is_null = &rs->is_null;
521  rs->is_null = 0;
522 
523  return GNUNET_OK;
524 }
525 
526 
539 static int
541  struct GNUNET_MY_ResultSpec *rs,
542  MYSQL_STMT *stmt,
543  unsigned int column,
544  MYSQL_BIND *results)
545 {
546  size_t size = (size_t) rs->mysql_bind_output_length;
547  char *buf;
548 
549  if (rs->mysql_bind_output_length != size)
550  return GNUNET_SYSERR;
551  if (*results->is_null)
552  {
553  *(void **) rs->dst = NULL;
554  return GNUNET_OK;
555  }
556 
557  buf = GNUNET_malloc (size);
558  results[0].buffer = buf;
559  results[0].buffer_length = size;
560  results[0].buffer_type = MYSQL_TYPE_BLOB;
561 
562  if (0 !=
563  mysql_stmt_fetch_column (stmt,
564  results,
565  column,
566  0))
567  {
568  GNUNET_free (buf);
569  return GNUNET_SYSERR;
570  }
571  buf[size] = '\0';
572  *(void **) rs->dst = buf;
573  return GNUNET_OK;
574 }
575 
576 
585 {
586  struct GNUNET_MY_ResultSpec res = {
587  .pre_conv = &pre_extract_string,
588  .post_conv = &post_extract_string,
589  .cleaner = NULL,
590  .dst = (void *) dst,
591  .dst_size = 0,
592  .num_fields = 1
593  };
594 
595  return res;
596 }
597 
598 
608 {
609  return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
610 }
611 
612 
621 {
622  struct GNUNET_MY_ResultSpec res =
623  GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
624 
625  return res;
626 }
627 
628 
641 static int
643  struct GNUNET_MY_ResultSpec *rs,
644  MYSQL_STMT *stmt,
645  unsigned int column,
646  MYSQL_BIND *results)
647 {
648  results[0].buffer = rs->dst;
649  results[0].buffer_length = rs->dst_size;
650  results[0].length = &rs->mysql_bind_output_length;
651  results[0].buffer_type = MYSQL_TYPE_SHORT;
652  results[0].is_null = &rs->is_null;
653  rs->is_null = 0;
654 
655  return GNUNET_OK;
656 }
657 
658 
671 static int
673  struct GNUNET_MY_ResultSpec *rs,
674  MYSQL_STMT *stmt,
675  unsigned int column,
676  MYSQL_BIND *results)
677 {
678  if (rs->dst_size != rs->mysql_bind_output_length)
679  return GNUNET_SYSERR;
680  if (*results->is_null)
681  return GNUNET_SYSERR;
682  return GNUNET_OK;
683 }
684 
685 
693 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
694 {
695  struct GNUNET_MY_ResultSpec res = {
696  .pre_conv = &pre_extract_uint16,
697  .post_conv = &post_extract_uint16,
698  .cleaner = NULL,
699  .dst = (void *) u16,
700  .dst_size = sizeof(*u16),
701  .num_fields = 1
702  };
703 
704  return res;
705 }
706 
707 
720 static int
722  struct GNUNET_MY_ResultSpec *rs,
723  MYSQL_STMT *stmt,
724  unsigned int column,
725  MYSQL_BIND *results)
726 {
727  results[0].buffer = rs->dst;
728  results[0].buffer_length = rs->dst_size;
729  results[0].length = &rs->mysql_bind_output_length;
730  results[0].buffer_type = MYSQL_TYPE_LONG;
731  results[0].is_null = &rs->is_null;
732  rs->is_null = 0;
733 
734  return GNUNET_OK;
735 }
736 
737 
750 static int
752  struct GNUNET_MY_ResultSpec *rs,
753  MYSQL_STMT *stmt,
754  unsigned int column,
755  MYSQL_BIND *results)
756 {
757  if (rs->dst_size != rs->mysql_bind_output_length)
758  return GNUNET_SYSERR;
759  if (*results->is_null)
760  return GNUNET_SYSERR;
761  return GNUNET_OK;
762 }
763 
764 
772 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
773 {
774  struct GNUNET_MY_ResultSpec res = {
775  .pre_conv = &pre_extract_uint32,
776  .post_conv = &post_extract_uint32,
777  .cleaner = NULL,
778  .dst = (void *) u32,
779  .dst_size = sizeof(*u32),
780  .num_fields = 1
781  };
782 
783  return res;
784 }
785 
786 
799 static int
801  struct GNUNET_MY_ResultSpec *rs,
802  MYSQL_STMT *stmt,
803  unsigned int column,
804  MYSQL_BIND *results)
805 {
806  if (sizeof(uint64_t) != rs->dst_size)
807  return GNUNET_SYSERR;
808  results[0].buffer = rs->dst;
809  results[0].buffer_length = rs->dst_size;
810  results[0].length = &rs->mysql_bind_output_length;
811  results[0].buffer_type = MYSQL_TYPE_LONGLONG;
812  results[0].is_null = &rs->is_null;
813  rs->is_null = 0;
814 
815  return GNUNET_OK;
816 }
817 
818 
831 static int
833  struct GNUNET_MY_ResultSpec *rs,
834  MYSQL_STMT *stmt,
835  unsigned int column,
836  MYSQL_BIND *results)
837 {
838  if (sizeof(uint64_t) != rs->dst_size)
839  return GNUNET_SYSERR;
840  if (*results->is_null)
841  return GNUNET_SYSERR;
842  return GNUNET_OK;
843 }
844 
845 
853 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
854 {
855  struct GNUNET_MY_ResultSpec res = {
856  .pre_conv = &pre_extract_uint64,
857  .post_conv = &post_extract_uint64,
858  .cleaner = NULL,
859  .dst = (void *) u64,
860  .dst_size = sizeof(*u64),
861  .num_fields = 1
862  };
863 
864  return res;
865 }
866 
867 
868 /* end of my_result_helper.c */
static int res
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static unsigned int results
static char buf[2048]
Helper library to access a MySQL database.
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:1039
#define GNUNET_log(kind,...)
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:991
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:268
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:423
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time in network byte order expected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint16(uint16_t *u16)
uint16_t expected
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_variable_size(void **dst, size_t *ptr_size)
Variable-size result expected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_string(char **dst)
0- terminated string exprected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_rsa_signature(struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_rsa_public_key(struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_fixed_size(void *ptr, size_t ptr_size)
Fixed-size result expected.
#define GNUNET_MY_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint32(uint32_t *u32)
uint32_t expected
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint64(uint64_t *u64)
uint64_t expected.
static int pre_extract_rsa_public_key(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Mysql database result at row row.
static int post_extract_rsa_signature(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Mysql database result at row row.
static int pre_extract_varsize_blob(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
extract data from a Mysql database result at row row
static int post_extract_fixed_blob(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Check size of extracted fixed size data from a Mysql database result at row row.
static int post_extract_uint64(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Check size of extracted fixed-size data from a Mysql database.
static int post_extract_string(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Check size of extracted fixed size data from a Mysql database.
static int pre_extract_uint16(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Postgres database result at row row.
static void clean_rsa_public_key(void *cls, struct GNUNET_MY_ResultSpec *rs)
Function called to clean up memory allocated by a GNUNET_MY_ResultConverter.
static int post_extract_rsa_public_key(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Check size of extracted fixed size data from a Mysql database result at row row.
static int post_extract_uint16(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Check size of extracted fixed size data from a Mysql database.
static int pre_extract_string(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Mysql database result at row row.
static int pre_extract_fixed_blob(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Mysql database result at row row.
static int post_extract_varsize_blob(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
extract data from a Mysql database result at row row
static int pre_extract_uint32(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a MYSQL database result at row row.
static void clean_rsa_signature(void *cls, struct GNUNET_MY_ResultSpec *rs)
Function called to clean up memory allocated by a GNUNET_MY_ResultConverter.
static void cleanup_varsize_blob(void *cls, struct GNUNET_MY_ResultSpec *rs)
extract data from a Mysql database result at row row
static int pre_extract_rsa_signature(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a Mysql database result at row row.
static int post_extract_uint32(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a MYSQL database result at row row.
static int pre_extract_uint64(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extract data from a MYSQL database result at row row.
static unsigned int size
Size of the "table".
Definition: peer.c:68
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_extract_result() to initialize the arguments of the prepared stateme...
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...
MYSQL_BOOL is_null
Memory for MySQL to notify us about NULL values.
unsigned int num_fields
How many fields does this result specification occupy in the result returned by MySQL.
void * dst
Destination for the data.
size_t * result_size
Where to store actual size of the result.
unsigned long mysql_bind_output_length
Location where we temporarily store the output buffer length from MySQL.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.