GNUnet  0.10.x
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_my_lib.h"
29 
30 
43 static int
45  struct GNUNET_MY_ResultSpec *rs,
46  MYSQL_STMT *stmt,
47  unsigned int column,
48  MYSQL_BIND *results)
49 {
50  results[0].buffer = NULL;
51  results[0].buffer_length = 0;
52  results[0].length = &rs->mysql_bind_output_length;
53  results[0].is_null = &rs->is_null;
54  rs->is_null = 0;
55 
56  return GNUNET_OK;
57 }
58 
59 
72 static int
74  struct GNUNET_MY_ResultSpec *rs,
75  MYSQL_STMT *stmt,
76  unsigned int column,
77  MYSQL_BIND *results)
78 {
79  void *buf;
80  size_t size;
81 
82  if (*results->is_null)
83  return GNUNET_SYSERR;
84  size = (size_t) rs->mysql_bind_output_length;
85 
86  if (rs->mysql_bind_output_length != size)
87  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
88 
89  buf = GNUNET_malloc (size);
90 
91  results[0].buffer = buf;
92  results[0].buffer_length = size;
93  results[0].buffer_type = MYSQL_TYPE_BLOB;
94 
95  if (0 !=
96  mysql_stmt_fetch_column (stmt,
97  results,
98  column,
99  0))
100  {
101  GNUNET_free (buf);
102  return GNUNET_SYSERR;
103  }
104 
105  *(void **) rs->dst = buf;
106  *rs->result_size = size;
107 
108  return GNUNET_OK;
109 }
110 
111 
118 static void
120  struct GNUNET_MY_ResultSpec *rs)
121 {
122  void **ptr = (void **)rs->dst;
123 
124  if (NULL != *ptr)
125  {
126  GNUNET_free (*ptr);
127  *ptr = NULL;
128  }
129 }
130 
131 
141  size_t *ptr_size)
142 {
143  struct GNUNET_MY_ResultSpec res =
144  {
146  .post_conv = &post_extract_varsize_blob,
147  .cleaner = &cleanup_varsize_blob,
148  .dst = (void *)(dst),
149  .result_size = ptr_size,
150  .num_fields = 1
151  };
152 
153  return res;
154 }
155 
156 
169 static int
171  struct GNUNET_MY_ResultSpec *rs,
172  MYSQL_STMT *stmt,
173  unsigned int column,
174  MYSQL_BIND *results)
175 {
176  results[0].buffer = rs->dst;
177  results[0].buffer_length = rs->dst_size;
178  results[0].length = &rs->mysql_bind_output_length;
179  results[0].buffer_type = MYSQL_TYPE_BLOB;
180  results[0].is_null = &rs->is_null;
181  rs->is_null = 0;
182 
183  return GNUNET_OK;
184 }
185 
186 
200 static int
202  struct GNUNET_MY_ResultSpec *rs,
203  MYSQL_STMT *stmt,
204  unsigned int column,
205  MYSQL_BIND *results)
206 {
207  if (*results->is_null)
208  return GNUNET_SYSERR;
209  if (rs->dst_size != rs->mysql_bind_output_length)
210  return GNUNET_SYSERR;
211  return GNUNET_OK;
212 }
213 
214 
225  size_t ptr_size)
226 {
227  struct GNUNET_MY_ResultSpec res =
228  {
230  .post_conv = &post_extract_fixed_blob,
231  .cleaner = NULL,
232  .dst = (void *)(ptr),
233  .dst_size = ptr_size,
234  .num_fields = 1
235  };
236 
237  return res;
238 }
239 
240 
253 static int
255  struct GNUNET_MY_ResultSpec *rs,
256  MYSQL_STMT *stmt,
257  unsigned int column,
258  MYSQL_BIND *results)
259 {
260  results[0].buffer = NULL;
261  results[0].buffer_length = 0;
262  results[0].length = &rs->mysql_bind_output_length;
263  results[0].buffer_type = MYSQL_TYPE_BLOB;
264  results[0].is_null = &rs->is_null;
265  rs->is_null = 0;
266 
267  return GNUNET_OK;
268 }
269 
270 
284 static int
286  struct GNUNET_MY_ResultSpec *rs,
287  MYSQL_STMT *stmt,
288  unsigned int column,
289  MYSQL_BIND *results)
290 
291 {
292  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
293  void *buf;
294  size_t size;
295 
296  if (*results->is_null)
297  return GNUNET_SYSERR;
298  size = (size_t) rs->mysql_bind_output_length;
299 
300  if (rs->mysql_bind_output_length != size)
301  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
302  buf = GNUNET_malloc (size);
303 
304  results[0].buffer = buf;
305  results[0].buffer_length = size;
306  results[0].buffer_type = MYSQL_TYPE_BLOB;
307  if (0 !=
308  mysql_stmt_fetch_column (stmt,
309  results,
310  column,
311  0))
312  {
313  GNUNET_free (buf);
314  return GNUNET_SYSERR;
315  }
316 
318  size);
319  GNUNET_free (buf);
320  if (NULL == *pk)
321  {
323  "Results contains bogus public key value (fail to decode)\n");
324  return GNUNET_SYSERR;
325  }
326 
327  return GNUNET_OK;
328 }
329 
330 
338 static void
340  struct GNUNET_MY_ResultSpec *rs)
341 {
342  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
343 
344  if (NULL != *pk)
345  {
347  *pk = NULL;
348  }
349 }
350 
351 
361 {
362  struct GNUNET_MY_ResultSpec res = {
364  .post_conv = &post_extract_rsa_public_key,
365  .cleaner = &clean_rsa_public_key,
366  .dst = (void *) rsa,
367  .dst_size = 0,
368  .num_fields = 1
369  };
370 
371  return res;
372 }
373 
374 
387 static int
389  struct GNUNET_MY_ResultSpec *rs,
390  MYSQL_STMT *stmt,
391  unsigned int column,
392  MYSQL_BIND *results)
393 {
394  results[0].buffer = 0;
395  results[0].buffer_length = 0;
396  results[0].length = &rs->mysql_bind_output_length;
397  results[0].buffer_type = MYSQL_TYPE_BLOB;
398  results[0].is_null = &rs->is_null;
399  rs->is_null = 0;
400 
401  return GNUNET_OK;
402 }
403 
404 
417 static int
419  struct GNUNET_MY_ResultSpec *rs,
420  MYSQL_STMT *stmt,
421  unsigned int column,
422  MYSQL_BIND *results)
423 {
424  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
425  void *buf;
426  size_t size;
427 
428  if (*results->is_null)
429  return GNUNET_SYSERR;
430  size = (size_t) rs->mysql_bind_output_length;
431 
432  if (rs->mysql_bind_output_length != size)
433  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
434  buf = GNUNET_malloc (size);
435 
436  results[0].buffer = buf;
437  results[0].buffer_length = size;
438  results[0].buffer_type = MYSQL_TYPE_BLOB;
439  if (0 !=
440  mysql_stmt_fetch_column (stmt,
441  results,
442  column,
443  0))
444  {
445  GNUNET_free (buf);
446  return GNUNET_SYSERR;
447  }
448 
450  size);
451  GNUNET_free (buf);
452  if (NULL == *sig)
453  {
455  "Resuls contains bogus signature value (fails to decode)\n");
456  return GNUNET_SYSERR;
457  }
458  return GNUNET_OK;
459 }
460 
461 
469 static void
471  struct GNUNET_MY_ResultSpec *rs)
472 {
473  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
474 
475  if (NULL != *sig)
476  {
478  *sig = NULL;
479  }
480 }
481 
482 
491 {
492  struct GNUNET_MY_ResultSpec res =
493  {
495  .post_conv = &post_extract_rsa_signature,
496  .cleaner = &clean_rsa_signature,
497  .dst = (void *)sig,
498  .dst_size = 0,
499  .num_fields = 1
500  };
501  return res;
502 }
503 
504 
517 static int
518 pre_extract_string (void * cls,
519  struct GNUNET_MY_ResultSpec *rs,
520  MYSQL_STMT *stmt,
521  unsigned int column,
522  MYSQL_BIND *results)
523 {
524  results[0].buffer = NULL;
525  results[0].buffer_length = 0;
526  results[0].length = &rs->mysql_bind_output_length;
527  results[0].buffer_type = MYSQL_TYPE_BLOB;
528  results[0].is_null = &rs->is_null;
529  rs->is_null = 0;
530 
531  return GNUNET_OK;
532 }
533 
534 
547 static int
549  struct GNUNET_MY_ResultSpec *rs,
550  MYSQL_STMT *stmt,
551  unsigned int column,
552  MYSQL_BIND *results)
553 {
554  size_t size = (size_t) rs->mysql_bind_output_length;
555  char *buf;
556 
557  if (rs->mysql_bind_output_length != size)
558  return GNUNET_SYSERR;
559  if (*results->is_null)
560  {
561  *(void **) rs->dst = NULL;
562  return GNUNET_OK;
563  }
564 
565  buf = GNUNET_malloc (size);
566  results[0].buffer = buf;
567  results[0].buffer_length = size;
568  results[0].buffer_type = MYSQL_TYPE_BLOB;
569 
570  if (0 !=
571  mysql_stmt_fetch_column (stmt,
572  results,
573  column,
574  0))
575  {
576  GNUNET_free (buf);
577  return GNUNET_SYSERR;
578  }
579  buf[size] = '\0';
580  *(void **) rs->dst = buf;
581  return GNUNET_OK;
582 }
583 
584 
593 {
594  struct GNUNET_MY_ResultSpec res = {
596  .post_conv = &post_extract_string,
597  .cleaner = NULL,
598  .dst = (void *) dst,
599  .dst_size = 0,
600  .num_fields = 1
601  };
602  return res;
603 }
604 
605 
615 {
616  return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
617 }
618 
619 
628 {
629  struct GNUNET_MY_ResultSpec res =
630  GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
631  return res;
632 }
633 
634 
647 static int
649  struct GNUNET_MY_ResultSpec *rs,
650  MYSQL_STMT *stmt,
651  unsigned int column,
652  MYSQL_BIND *results)
653 {
654  results[0].buffer = rs->dst;
655  results[0].buffer_length = rs->dst_size;
656  results[0].length = &rs->mysql_bind_output_length;
657  results[0].buffer_type = MYSQL_TYPE_SHORT;
658  results[0].is_null = &rs->is_null;
659  rs->is_null = 0;
660 
661  return GNUNET_OK;
662 }
663 
664 
677 static int
679  struct GNUNET_MY_ResultSpec *rs,
680  MYSQL_STMT *stmt,
681  unsigned int column,
682  MYSQL_BIND *results)
683 {
684  if (rs->dst_size != rs->mysql_bind_output_length)
685  return GNUNET_SYSERR;
686  if (*results->is_null)
687  return GNUNET_SYSERR;
688  return GNUNET_OK;
689 }
690 
691 
700 {
701  struct GNUNET_MY_ResultSpec res = {
703  .post_conv = &post_extract_uint16,
704  .cleaner = NULL,
705  .dst = (void *) u16,
706  .dst_size = sizeof (*u16),
707  .num_fields = 1
708  };
709  return res;
710 }
711 
712 
726 static int
728  struct GNUNET_MY_ResultSpec *rs,
729  MYSQL_STMT *stmt,
730  unsigned int column,
731  MYSQL_BIND *results)
732 {
733  results[0].buffer = rs->dst;
734  results[0].buffer_length = rs->dst_size;
735  results[0].length = &rs->mysql_bind_output_length;
736  results[0].buffer_type = MYSQL_TYPE_LONG;
737  results[0].is_null = &rs->is_null;
738  rs->is_null = 0;
739 
740  return GNUNET_OK;
741 }
742 
743 
757 static int
759  struct GNUNET_MY_ResultSpec *rs,
760  MYSQL_STMT * stmt,
761  unsigned int column,
762  MYSQL_BIND *results)
763 {
764  if (rs->dst_size != rs->mysql_bind_output_length)
765  return GNUNET_SYSERR;
766  if (*results->is_null)
767  return GNUNET_SYSERR;
768  return GNUNET_OK;
769 }
770 
771 
780 {
781  struct GNUNET_MY_ResultSpec res = {
783  .post_conv = &post_extract_uint32,
784  .cleaner = NULL,
785  .dst = (void *) u32,
786  .dst_size = sizeof (*u32),
787  .num_fields = 1
788  };
789  return res;
790 }
791 
792 
805 static int
807  struct GNUNET_MY_ResultSpec *rs,
808  MYSQL_STMT *stmt,
809  unsigned int column,
810  MYSQL_BIND *results)
811 {
812  if (sizeof (uint64_t) != rs->dst_size)
813  return GNUNET_SYSERR;
814  results[0].buffer = rs->dst;
815  results[0].buffer_length = rs->dst_size;
816  results[0].length = &rs->mysql_bind_output_length;
817  results[0].buffer_type = MYSQL_TYPE_LONGLONG;
818  results[0].is_null = &rs->is_null;
819  rs->is_null = 0;
820 
821  return GNUNET_OK;
822 }
823 
824 
837 static int
839  struct GNUNET_MY_ResultSpec *rs,
840  MYSQL_STMT *stmt,
841  unsigned int column,
842  MYSQL_BIND *results)
843 {
844  if (sizeof (uint64_t) != rs->dst_size)
845  return GNUNET_SYSERR;
846  if (*results->is_null)
847  return GNUNET_SYSERR;
848  return GNUNET_OK;
849 }
850 
851 
860 {
861  struct GNUNET_MY_ResultSpec res = {
863  .post_conv = &post_extract_uint64,
864  .cleaner = NULL,
865  .dst = (void *) u64,
866  .dst_size = sizeof (*u64),
867  .num_fields = 1
868  };
869  return res;
870 }
871 
872 
873 /* end of my_result_helper.c */
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 struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
static void cleanup_varsize_blob(void *cls, struct GNUNET_MY_ResultSpec *rs)
extract data from a Mysql database result at row row
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:951
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint64(uint64_t *u64)
uint64_t expected.
size_t * result_size
Where to store actual size of the result.
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 datbase.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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.
void * dst
Destination for the data.
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. ...
The public information of an RSA key pair.
Definition: crypto_rsa.c:51
my_bool is_null
Memory for MySQL to notify us about NULL values.
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.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time in network byte order expected.
Information we pass to GNUNET_MY_extract_result() to initialize the arguments of the prepared stateme...
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 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_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 .
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:370
static char buf[2048]
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_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 res
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_fixed_size(void *ptr, size_t ptr_size)
Fixed-size result expected.
static int pre_extract_uint32(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extrac data from a MYSQL database result at row row.
#define GNUNET_MY_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
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.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_string(char **dst)
0- terminated string exprected.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint32(uint32_t *u32)
uint32_t expected
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.
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
GNUNET_MY_ResultConverter pre_conv
Function to call to initialize the MYSQL_BIND array.
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 fixe size data from a Mysql database.
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_signature_decode(const char *buf, size_t len)
Decode the signature from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:996
static unsigned int results
Definition: gnunet-search.c:52
#define GNUNET_log(kind,...)
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 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_uint32(void *cls, struct GNUNET_MY_ResultSpec *rs, MYSQL_STMT *stmt, unsigned int column, MYSQL_BIND *results)
Extrac data from a MYSQL database result at row row.
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:302
Time for absolute times used by GNUnet, in microseconds.
unsigned int num_fields
How many fields does this result specification occupy in the result returned by MySQL.
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 void clean_rsa_public_key(void *cls, struct GNUNET_MY_ResultSpec *rs)
Function called to clean up memory allocated by a GNUNET_MY_ResultConverter.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_uint16(uint16_t *u16)
uint16_t expected
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.
struct GNUNET_MY_ResultSpec GNUNET_MY_result_spec_variable_size(void **dst, size_t *ptr_size)
Variable-size result expected.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.