GNUnet  0.11.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 = {
145  .post_conv = &post_extract_varsize_blob,
146  .cleaner = &cleanup_varsize_blob,
147  .dst = (void *) (dst),
148  .result_size = ptr_size,
149  .num_fields = 1
150  };
151 
152  return res;
153 }
154 
155 
168 static int
170  struct GNUNET_MY_ResultSpec *rs,
171  MYSQL_STMT *stmt,
172  unsigned int column,
173  MYSQL_BIND *results)
174 {
175  results[0].buffer = rs->dst;
176  results[0].buffer_length = rs->dst_size;
177  results[0].length = &rs->mysql_bind_output_length;
178  results[0].buffer_type = MYSQL_TYPE_BLOB;
179  results[0].is_null = &rs->is_null;
180  rs->is_null = 0;
181 
182  return GNUNET_OK;
183 }
184 
185 
199 static int
201  struct GNUNET_MY_ResultSpec *rs,
202  MYSQL_STMT *stmt,
203  unsigned int column,
204  MYSQL_BIND *results)
205 {
206  if (*results->is_null)
207  return GNUNET_SYSERR;
208  if (rs->dst_size != rs->mysql_bind_output_length)
209  return GNUNET_SYSERR;
210  return GNUNET_OK;
211 }
212 
213 
224  size_t ptr_size)
225 {
226  struct GNUNET_MY_ResultSpec res = {
228  .post_conv = &post_extract_fixed_blob,
229  .cleaner = NULL,
230  .dst = (void *) (ptr),
231  .dst_size = ptr_size,
232  .num_fields = 1
233  };
234 
235  return res;
236 }
237 
238 
251 static int
253  struct GNUNET_MY_ResultSpec *rs,
254  MYSQL_STMT *stmt,
255  unsigned int column,
256  MYSQL_BIND *results)
257 {
258  results[0].buffer = NULL;
259  results[0].buffer_length = 0;
260  results[0].length = &rs->mysql_bind_output_length;
261  results[0].buffer_type = MYSQL_TYPE_BLOB;
262  results[0].is_null = &rs->is_null;
263  rs->is_null = 0;
264 
265  return GNUNET_OK;
266 }
267 
268 
282 static int
284  struct GNUNET_MY_ResultSpec *rs,
285  MYSQL_STMT *stmt,
286  unsigned int column,
287  MYSQL_BIND *results)
288 
289 {
290  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
291  void *buf;
292  size_t size;
293 
294  if (*results->is_null)
295  return GNUNET_SYSERR;
296  size = (size_t) rs->mysql_bind_output_length;
297 
298  if (rs->mysql_bind_output_length != size)
299  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
300  buf = GNUNET_malloc (size);
301 
302  results[0].buffer = buf;
303  results[0].buffer_length = size;
304  results[0].buffer_type = MYSQL_TYPE_BLOB;
305  if (0 !=
306  mysql_stmt_fetch_column (stmt,
307  results,
308  column,
309  0))
310  {
311  GNUNET_free (buf);
312  return GNUNET_SYSERR;
313  }
314 
316  size);
317  GNUNET_free (buf);
318  if (NULL == *pk)
319  {
321  "Results contains bogus public key value (fail to decode)\n");
322  return GNUNET_SYSERR;
323  }
324 
325  return GNUNET_OK;
326 }
327 
328 
336 static void
338  struct GNUNET_MY_ResultSpec *rs)
339 {
340  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
341 
342  if (NULL != *pk)
343  {
345  *pk = NULL;
346  }
347 }
348 
349 
359 {
360  struct GNUNET_MY_ResultSpec res = {
362  .post_conv = &post_extract_rsa_public_key,
363  .cleaner = &clean_rsa_public_key,
364  .dst = (void *) rsa,
365  .dst_size = 0,
366  .num_fields = 1
367  };
368 
369  return res;
370 }
371 
372 
385 static int
387  struct GNUNET_MY_ResultSpec *rs,
388  MYSQL_STMT *stmt,
389  unsigned int column,
390  MYSQL_BIND *results)
391 {
392  results[0].buffer = 0;
393  results[0].buffer_length = 0;
394  results[0].length = &rs->mysql_bind_output_length;
395  results[0].buffer_type = MYSQL_TYPE_BLOB;
396  results[0].is_null = &rs->is_null;
397  rs->is_null = 0;
398 
399  return GNUNET_OK;
400 }
401 
402 
415 static int
417  struct GNUNET_MY_ResultSpec *rs,
418  MYSQL_STMT *stmt,
419  unsigned int column,
420  MYSQL_BIND *results)
421 {
422  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
423  void *buf;
424  size_t size;
425 
426  if (*results->is_null)
427  return GNUNET_SYSERR;
428  size = (size_t) rs->mysql_bind_output_length;
429 
430  if (rs->mysql_bind_output_length != size)
431  return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
432  buf = GNUNET_malloc (size);
433 
434  results[0].buffer = buf;
435  results[0].buffer_length = size;
436  results[0].buffer_type = MYSQL_TYPE_BLOB;
437  if (0 !=
438  mysql_stmt_fetch_column (stmt,
439  results,
440  column,
441  0))
442  {
443  GNUNET_free (buf);
444  return GNUNET_SYSERR;
445  }
446 
448  size);
449  GNUNET_free (buf);
450  if (NULL == *sig)
451  {
453  "Resuls contains bogus signature value (fails to decode)\n");
454  return GNUNET_SYSERR;
455  }
456  return GNUNET_OK;
457 }
458 
459 
467 static void
469  struct GNUNET_MY_ResultSpec *rs)
470 {
471  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
472 
473  if (NULL != *sig)
474  {
476  *sig = NULL;
477  }
478 }
479 
480 
489 {
490  struct GNUNET_MY_ResultSpec res = {
492  .post_conv = &post_extract_rsa_signature,
493  .cleaner = &clean_rsa_signature,
494  .dst = (void *) sig,
495  .dst_size = 0,
496  .num_fields = 1
497  };
498 
499  return res;
500 }
501 
502 
515 static int
517  struct GNUNET_MY_ResultSpec *rs,
518  MYSQL_STMT *stmt,
519  unsigned int column,
520  MYSQL_BIND *results)
521 {
522  results[0].buffer = NULL;
523  results[0].buffer_length = 0;
524  results[0].length = &rs->mysql_bind_output_length;
525  results[0].buffer_type = MYSQL_TYPE_BLOB;
526  results[0].is_null = &rs->is_null;
527  rs->is_null = 0;
528 
529  return GNUNET_OK;
530 }
531 
532 
545 static int
547  struct GNUNET_MY_ResultSpec *rs,
548  MYSQL_STMT *stmt,
549  unsigned int column,
550  MYSQL_BIND *results)
551 {
552  size_t size = (size_t) rs->mysql_bind_output_length;
553  char *buf;
554 
555  if (rs->mysql_bind_output_length != size)
556  return GNUNET_SYSERR;
557  if (*results->is_null)
558  {
559  *(void **) rs->dst = NULL;
560  return GNUNET_OK;
561  }
562 
563  buf = GNUNET_malloc (size);
564  results[0].buffer = buf;
565  results[0].buffer_length = size;
566  results[0].buffer_type = MYSQL_TYPE_BLOB;
567 
568  if (0 !=
569  mysql_stmt_fetch_column (stmt,
570  results,
571  column,
572  0))
573  {
574  GNUNET_free (buf);
575  return GNUNET_SYSERR;
576  }
577  buf[size] = '\0';
578  *(void **) rs->dst = buf;
579  return GNUNET_OK;
580 }
581 
582 
591 {
592  struct GNUNET_MY_ResultSpec res = {
594  .post_conv = &post_extract_string,
595  .cleaner = NULL,
596  .dst = (void *) dst,
597  .dst_size = 0,
598  .num_fields = 1
599  };
600 
601  return res;
602 }
603 
604 
614 {
615  return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
616 }
617 
618 
627 {
628  struct GNUNET_MY_ResultSpec res =
629  GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
630 
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 
710  return res;
711 }
712 
713 
727 static int
729  struct GNUNET_MY_ResultSpec *rs,
730  MYSQL_STMT *stmt,
731  unsigned int column,
732  MYSQL_BIND *results)
733 {
734  results[0].buffer = rs->dst;
735  results[0].buffer_length = rs->dst_size;
736  results[0].length = &rs->mysql_bind_output_length;
737  results[0].buffer_type = MYSQL_TYPE_LONG;
738  results[0].is_null = &rs->is_null;
739  rs->is_null = 0;
740 
741  return GNUNET_OK;
742 }
743 
744 
758 static int
760  struct GNUNET_MY_ResultSpec *rs,
761  MYSQL_STMT *stmt,
762  unsigned int column,
763  MYSQL_BIND *results)
764 {
765  if (rs->dst_size != rs->mysql_bind_output_length)
766  return GNUNET_SYSERR;
767  if (*results->is_null)
768  return GNUNET_SYSERR;
769  return GNUNET_OK;
770 }
771 
772 
781 {
782  struct GNUNET_MY_ResultSpec res = {
784  .post_conv = &post_extract_uint32,
785  .cleaner = NULL,
786  .dst = (void *) u32,
787  .dst_size = sizeof(*u32),
788  .num_fields = 1
789  };
790 
791  return res;
792 }
793 
794 
807 static int
809  struct GNUNET_MY_ResultSpec *rs,
810  MYSQL_STMT *stmt,
811  unsigned int column,
812  MYSQL_BIND *results)
813 {
814  if (sizeof(uint64_t) != rs->dst_size)
815  return GNUNET_SYSERR;
816  results[0].buffer = rs->dst;
817  results[0].buffer_length = rs->dst_size;
818  results[0].length = &rs->mysql_bind_output_length;
819  results[0].buffer_type = MYSQL_TYPE_LONGLONG;
820  results[0].is_null = &rs->is_null;
821  rs->is_null = 0;
822 
823  return GNUNET_OK;
824 }
825 
826 
839 static int
841  struct GNUNET_MY_ResultSpec *rs,
842  MYSQL_STMT *stmt,
843  unsigned int column,
844  MYSQL_BIND *results)
845 {
846  if (sizeof(uint64_t) != rs->dst_size)
847  return GNUNET_SYSERR;
848  if (*results->is_null)
849  return GNUNET_SYSERR;
850  return GNUNET_OK;
851 }
852 
853 
862 {
863  struct GNUNET_MY_ResultSpec res = {
865  .post_conv = &post_extract_uint64,
866  .cleaner = NULL,
867  .dst = (void *) u64,
868  .dst_size = sizeof(*u64),
869  .num_fields = 1
870  };
871 
872  return res;
873 }
874 
875 
876 /* 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:65
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:1112
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:53
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:465
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: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:1192
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:307
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.