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 
502  return res;
503 }
504 
505 
518 static int
520  struct GNUNET_MY_ResultSpec *rs,
521  MYSQL_STMT *stmt,
522  unsigned int column,
523  MYSQL_BIND *results)
524 {
525  results[0].buffer = NULL;
526  results[0].buffer_length = 0;
527  results[0].length = &rs->mysql_bind_output_length;
528  results[0].buffer_type = MYSQL_TYPE_BLOB;
529  results[0].is_null = &rs->is_null;
530  rs->is_null = 0;
531 
532  return GNUNET_OK;
533 }
534 
535 
548 static int
550  struct GNUNET_MY_ResultSpec *rs,
551  MYSQL_STMT *stmt,
552  unsigned int column,
553  MYSQL_BIND *results)
554 {
555  size_t size = (size_t)rs->mysql_bind_output_length;
556  char *buf;
557 
558  if (rs->mysql_bind_output_length != size)
559  return GNUNET_SYSERR;
560  if (*results->is_null)
561  {
562  *(void **)rs->dst = NULL;
563  return GNUNET_OK;
564  }
565 
566  buf = GNUNET_malloc(size);
567  results[0].buffer = buf;
568  results[0].buffer_length = size;
569  results[0].buffer_type = MYSQL_TYPE_BLOB;
570 
571  if (0 !=
572  mysql_stmt_fetch_column(stmt,
573  results,
574  column,
575  0))
576  {
577  GNUNET_free(buf);
578  return GNUNET_SYSERR;
579  }
580  buf[size] = '\0';
581  *(void **)rs->dst = buf;
582  return GNUNET_OK;
583 }
584 
585 
594 {
595  struct GNUNET_MY_ResultSpec res = {
597  .post_conv = &post_extract_string,
598  .cleaner = NULL,
599  .dst = (void *)dst,
600  .dst_size = 0,
601  .num_fields = 1
602  };
603 
604  return res;
605 }
606 
607 
617 {
618  return GNUNET_MY_result_spec_uint64(&at->abs_value_us);
619 }
620 
621 
630 {
631  struct GNUNET_MY_ResultSpec res =
632  GNUNET_MY_result_spec_auto_from_type(&at->abs_value_us__);
633 
634  return res;
635 }
636 
637 
650 static int
652  struct GNUNET_MY_ResultSpec *rs,
653  MYSQL_STMT *stmt,
654  unsigned int column,
655  MYSQL_BIND *results)
656 {
657  results[0].buffer = rs->dst;
658  results[0].buffer_length = rs->dst_size;
659  results[0].length = &rs->mysql_bind_output_length;
660  results[0].buffer_type = MYSQL_TYPE_SHORT;
661  results[0].is_null = &rs->is_null;
662  rs->is_null = 0;
663 
664  return GNUNET_OK;
665 }
666 
667 
680 static int
682  struct GNUNET_MY_ResultSpec *rs,
683  MYSQL_STMT *stmt,
684  unsigned int column,
685  MYSQL_BIND *results)
686 {
687  if (rs->dst_size != rs->mysql_bind_output_length)
688  return GNUNET_SYSERR;
689  if (*results->is_null)
690  return GNUNET_SYSERR;
691  return GNUNET_OK;
692 }
693 
694 
703 {
704  struct GNUNET_MY_ResultSpec res = {
706  .post_conv = &post_extract_uint16,
707  .cleaner = NULL,
708  .dst = (void *)u16,
709  .dst_size = sizeof(*u16),
710  .num_fields = 1
711  };
712 
713  return res;
714 }
715 
716 
730 static int
732  struct GNUNET_MY_ResultSpec *rs,
733  MYSQL_STMT *stmt,
734  unsigned int column,
735  MYSQL_BIND *results)
736 {
737  results[0].buffer = rs->dst;
738  results[0].buffer_length = rs->dst_size;
739  results[0].length = &rs->mysql_bind_output_length;
740  results[0].buffer_type = MYSQL_TYPE_LONG;
741  results[0].is_null = &rs->is_null;
742  rs->is_null = 0;
743 
744  return GNUNET_OK;
745 }
746 
747 
761 static int
763  struct GNUNET_MY_ResultSpec *rs,
764  MYSQL_STMT * stmt,
765  unsigned int column,
766  MYSQL_BIND *results)
767 {
768  if (rs->dst_size != rs->mysql_bind_output_length)
769  return GNUNET_SYSERR;
770  if (*results->is_null)
771  return GNUNET_SYSERR;
772  return GNUNET_OK;
773 }
774 
775 
784 {
785  struct GNUNET_MY_ResultSpec res = {
787  .post_conv = &post_extract_uint32,
788  .cleaner = NULL,
789  .dst = (void *)u32,
790  .dst_size = sizeof(*u32),
791  .num_fields = 1
792  };
793 
794  return res;
795 }
796 
797 
810 static int
812  struct GNUNET_MY_ResultSpec *rs,
813  MYSQL_STMT *stmt,
814  unsigned int column,
815  MYSQL_BIND *results)
816 {
817  if (sizeof(uint64_t) != rs->dst_size)
818  return GNUNET_SYSERR;
819  results[0].buffer = rs->dst;
820  results[0].buffer_length = rs->dst_size;
821  results[0].length = &rs->mysql_bind_output_length;
822  results[0].buffer_type = MYSQL_TYPE_LONGLONG;
823  results[0].is_null = &rs->is_null;
824  rs->is_null = 0;
825 
826  return GNUNET_OK;
827 }
828 
829 
842 static int
844  struct GNUNET_MY_ResultSpec *rs,
845  MYSQL_STMT *stmt,
846  unsigned int column,
847  MYSQL_BIND *results)
848 {
849  if (sizeof(uint64_t) != rs->dst_size)
850  return GNUNET_SYSERR;
851  if (*results->is_null)
852  return GNUNET_SYSERR;
853  return GNUNET_OK;
854 }
855 
856 
865 {
866  struct GNUNET_MY_ResultSpec res = {
868  .post_conv = &post_extract_uint64,
869  .cleaner = NULL,
870  .dst = (void *)u64,
871  .dst_size = sizeof(*u64),
872  .num_fields = 1
873  };
874 
875  return res;
876 }
877 
878 
879 /* 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:61
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:950
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:75
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:50
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:367
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:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
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:995
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:299
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.