GNUnet 0.21.2
gnunet_json_lib.h
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 */
27#ifndef GNUNET_JSON_LIB_H
28#define GNUNET_JSON_LIB_H
29
30
31#include "gnunet_util_lib.h"
32#include <jansson.h>
33#include <microhttpd.h>
34
35/* ****************** Generic parser interface ******************* */
36
41
42
53(*GNUNET_JSON_Parser)(void *cls,
54 json_t *root,
55 struct GNUNET_JSON_Specification *spec);
56
57
64typedef void
65(*GNUNET_JSON_Cleaner) (void *cls,
66 struct GNUNET_JSON_Specification *spec);
67
68
73{
78
83
87 void *cls;
88
93 const char *field;
94
98 void *ptr;
99
104 bool *missing;
105
109 size_t *size_ptr;
110
114 size_t ptr_size;
115
120};
121
122
137GNUNET_JSON_parse (const json_t *root,
138 struct GNUNET_JSON_Specification *spec,
139 const char **error_json_name,
140 unsigned int *error_line);
141
142
155void
157
158
159/* ****************** Canonical parser specifications ******************* */
160
161
167
168
178 bool *missing);
179
180
190GNUNET_JSON_spec_fixed (const char *name,
191 void *obj,
192 size_t size);
193
194
202#define GNUNET_JSON_spec_fixed_auto(name, obj) \
203 GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
204
205
214GNUNET_JSON_spec_fixed64 (const char *name,
215 void *obj,
216 size_t size);
217
218
225#define GNUNET_JSON_spec_fixed64_auto(name, obj) \
226 GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
227
228
238GNUNET_JSON_spec_varsize (const char *name,
239 void **obj,
240 size_t *size);
241
242
250GNUNET_JSON_spec_string (const char *name,
251 const char **strptr);
252
253
262GNUNET_JSON_spec_json (const char *name,
263 json_t **jsonp);
264
265
274 const json_t **jsonp);
275
276
285 const json_t **jsonp);
286
287
295GNUNET_JSON_spec_bool (const char *name,
296 bool *b);
297
298
306GNUNET_JSON_spec_double (const char *name,
307 double *f);
308
309
317GNUNET_JSON_spec_uint8 (const char *name,
318 uint8_t *u8);
319
320
328GNUNET_JSON_spec_uint16 (const char *name,
329 uint16_t *u16);
330
331
339GNUNET_JSON_spec_uint32 (const char *name,
340 uint32_t *u32);
341
342
350GNUNET_JSON_spec_uint64 (const char *name,
351 uint64_t *u64);
352
353
361GNUNET_JSON_spec_int64 (const char *name,
362 int64_t *i64);
363
364
372GNUNET_JSON_spec_boolean (const char *name,
373 int *boolean);
374
375
376/* ************ GNUnet-specific parser specifications ******************* */
377
386 struct GNUNET_TIME_Timestamp *t);
387
388
397 struct GNUNET_TIME_TimestampNBO *tn);
398
399
408 struct GNUNET_TIME_Relative *rt);
409
410
420
421
430 struct GNUNET_CRYPTO_RsaSignature **sig);
431
432
442
443
452 struct GNUNET_CRYPTO_BlindedSignature **b_sig);
453
454
463 struct GNUNET_CRYPTO_UnblindedSignature **ub_sig);
464
465
466/* ****************** Generic generator interface ******************* */
467
468
477json_t *
478GNUNET_JSON_from_data (const void *data,
479 size_t size);
480
481
490json_t *
491GNUNET_JSON_from_data64 (const void *data,
492 size_t size);
493
494
502#define GNUNET_JSON_from_data_auto(ptr) \
503 GNUNET_JSON_from_data (ptr, sizeof(*ptr))
504
505
513#define GNUNET_JSON_from_data64_auto(ptr) \
514 GNUNET_JSON_from_data64 (ptr, sizeof(*ptr))
515
516
523json_t *
525
526
533json_t *
535
536
543json_t *
545
546
553json_t *
555
556
563json_t *
565
566/* ******************* Helpers for MHD upload handling ******************* */
567
572{
577
582
587
592
598
599
616GNUNET_JSON_post_parser (size_t buffer_max,
617 struct MHD_Connection *connection,
618 void **con_cls,
619 const char *upload_data,
620 size_t *upload_data_size,
621 json_t **json);
622
623
631void
632GNUNET_JSON_post_parser_cleanup (void *con_cls);
633
634
635/* ****************** GETOPT JSON helper ******************* */
636
637
649 const char *name,
650 const char *argumentHelp,
651 const char *description,
652 json_t **json);
653
654
655/* ****************** JSON PACK helper ******************* */
656
657
662
663
671typedef json_t *
672(*GNUNET_JSON_PackCallback)(const struct GNUNET_JSON_PackSpec *se);
673
674
679{
683 const char *field_name;
684
688 json_t *object;
689
697};
698
699
707json_t *
709
710
718#define GNUNET_JSON_PACK(...) \
719 GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, \
720 GNUNET_JSON_pack_end_ ()})
721
722
730
731
740
741
751GNUNET_JSON_pack_bool (const char *name,
752 bool b);
753
754
764GNUNET_JSON_pack_double (const char *name,
765 double f);
766
767
777GNUNET_JSON_pack_string (const char *name,
778 const char *s);
779
780
791GNUNET_JSON_pack_uint64 (const char *name,
792 uint64_t num);
793
794
804GNUNET_JSON_pack_int64 (const char *name,
805 int64_t num);
806
807
819 json_t *o);
820
821
833 json_t *o);
834
835
847 json_t *a);
848
849
861 json_t *a);
862
863
875 const void *blob,
876 size_t blob_size);
877
878
887#define GNUNET_JSON_pack_data_auto(name,blob) \
888 GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob))
889
890
904 const void *blob,
905 size_t blob_size);
906
907
918#define GNUNET_JSON_pack_data64_auto(name,blob) \
919 GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob))
920
921
933 struct GNUNET_TIME_Timestamp at);
934
935
947 struct GNUNET_TIME_TimestampNBO at);
948
949
960 struct GNUNET_TIME_Relative rt);
961
962
973 struct GNUNET_TIME_RelativeNBO rt);
974
975
986 const struct GNUNET_CRYPTO_RsaPublicKey *pk);
987
988
999 const struct GNUNET_CRYPTO_RsaSignature *sig);
1000
1001
1012 const struct GNUNET_CRYPTO_UnblindedSignature *sig);
1013
1014
1025 const struct GNUNET_CRYPTO_BlindedMessage *msg);
1026
1027
1038 const struct GNUNET_CRYPTO_BlindedSignature *sig);
1039
1040#endif
1041
1042/* end of gnunet_json_lib.h */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static char * data
The data to insert into the dht.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
static char * name
Name (label) of the records to list.
static struct GNUNET_SCHEDULER_Task * t
Main task.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel(const char *name, struct GNUNET_TIME_Relative rt)
Generate packer instruction for a JSON field of type relative time.
Definition: json_pack.c:319
struct GNUNET_JSON_Specification GNUNET_JSON_spec_object_const(const char *name, const json_t **jsonp)
JSON object, reference counter not incremented.
Definition: json_helper.c:409
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:69
struct GNUNET_JSON_Specification GNUNET_JSON_spec_blinded_message(const char *name, struct GNUNET_CRYPTO_BlindedMessage **msg)
Specification for parsing a blinded message.
Definition: json_helper.c:1329
enum GNUNET_GenericReturnValue(* GNUNET_JSON_Parser)(void *cls, json_t *root, struct GNUNET_JSON_Specification *spec)
Function called to parse JSON argument.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_varsize(const char *name, void **obj, size_t *size)
Variable size object (in network byte order, encoded using Crockford Base32hex encoding).
Definition: json_helper.c:258
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed(const char *name, void *obj, size_t size)
Variable size object (in network byte order, encoded using Crockford Base32hex encoding).
Definition: json_helper.c:117
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_steal(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference is taken over by ...
Definition: json_pack.c:210
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp(const char *name, struct GNUNET_TIME_Timestamp at)
Generate packer instruction for a JSON field of type timestamp.
Definition: json_pack.c:289
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:94
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:255
json_t * GNUNET_JSON_from_timestamp(struct GNUNET_TIME_Timestamp stamp)
Convert timestamp to a json string.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_json(const char *name, json_t **jsonp)
JSON object or array.
Definition: json_helper.c:365
struct GNUNET_JSON_Specification GNUNET_JSON_spec_blinded_signature(const char *field, struct GNUNET_CRYPTO_BlindedSignature **b_sig)
Specification for parsing a blinded signature.
Definition: json_helper.c:1464
void(* GNUNET_JSON_Cleaner)(void *cls, struct GNUNET_JSON_Specification *spec)
Function called to clean up data from earlier parsing.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_bool(const char *name, bool b)
Generate packer instruction for a JSON field of type bool.
Definition: json_pack.c:88
struct GNUNET_JSON_Specification GNUNET_JSON_spec_boolean(const char *name, int *boolean)
Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
Definition: json_helper.c:1190
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_public_key(const char *name, struct GNUNET_CRYPTO_RsaPublicKey **pk)
Specification for parsing an RSA public key.
Definition: json_helper.c:1061
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint16(const char *name, uint16_t *u16)
16-bit integer.
Definition: json_helper.c:643
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_steal(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference is taken over by...
Definition: json_pack.c:165
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_array_incref(const char *name, json_t *a)
Generate packer instruction for a JSON field of type JSON array where the reference counter is increm...
Definition: json_pack.c:232
json_t * GNUNET_JSON_from_timestamp_nbo(struct GNUNET_TIME_TimestampNBO stamp)
Convert timestamp to a json string.
json_t * GNUNET_JSON_from_data64(const void *data, size_t size)
Convert binary data to a JSON string with base64 encoding.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp_nbo(const char *name, struct GNUNET_TIME_TimestampNBO *tn)
Timestamp in network byte order.
Definition: json_helper.c:907
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_allow_null(struct GNUNET_JSON_PackSpec in)
Modify packer instruction to allow NULL as a value.
Definition: json_pack.c:80
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32(const char *name, uint32_t *u32)
32-bit integer.
Definition: json_helper.c:693
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_unblinded_signature(const char *name, const struct GNUNET_CRYPTO_UnblindedSignature *sig)
Generate packer instruction for a JSON field of type unblinded signature.
Definition: json_pack.c:367
json_t * GNUNET_JSON_pack_(struct GNUNET_JSON_PackSpec spec[])
Pack a JSON object from a spec.
Definition: json_pack.c:29
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_double(const char *name, double f)
Generate packer instruction for a JSON field of type double.
Definition: json_pack.c:101
GNUNET_JSON_PostResult
Return codes from GNUNET_JSON_post_parser().
@ GNUNET_JSON_PR_CONTINUE
Parsing continues, call again soon!
@ GNUNET_JSON_PR_OUT_OF_MEMORY
Sorry, memory allocation (malloc()) failed.
@ GNUNET_JSON_PR_JSON_INVALID
JSON parsing failed.
@ GNUNET_JSON_PR_REQUEST_TOO_LARGE
Request size exceeded buffer_max argument.
@ GNUNET_JSON_PR_SUCCESS
Parsing successful, JSON result is in *json.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string(const char *name, const char **strptr)
The expected field stores a string.
Definition: json_helper.c:306
struct GNUNET_JSON_Specification GNUNET_JSON_spec_double(const char *name, double *f)
double.
Definition: json_helper.c:543
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_message(const char *name, const struct GNUNET_CRYPTO_BlindedMessage *msg)
Generate packer instruction for a JSON field of type blinded message.
Definition: json_pack.c:404
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_timestamp_nbo(const char *name, struct GNUNET_TIME_TimestampNBO at)
Generate packer instruction for a JSON field of type timestamp in network byte order.
Definition: json_pack.c:310
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_blinded_sig(const char *name, const struct GNUNET_CRYPTO_BlindedSignature *sig)
Generate packer instruction for a JSON field of type blinded signature.
Definition: json_pack.c:445
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec, bool *missing)
Set the "optional" flag for a parser specification entry.
Definition: json.c:82
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64(const char *name, int64_t *i64)
64-bit signed integer.
Definition: json_helper.c:783
struct GNUNET_JSON_Specification GNUNET_JSON_spec_rsa_signature(const char *name, struct GNUNET_CRYPTO_RsaSignature **sig)
Specification for parsing an RSA signature.
Definition: json_helper.c:1146
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_uint64(const char *name, uint64_t num)
Generate packer instruction for a JSON field of type unsigned integer.
Definition: json_pack.c:127
enum GNUNET_JSON_PostResult GNUNET_JSON_post_parser(size_t buffer_max, struct MHD_Connection *connection, void **con_cls, const char *upload_data, size_t *upload_data_size, json_t **json)
Process a POST request containing a JSON object.
Definition: json_mhd.c:265
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_object_incref(const char *name, json_t *o)
Generate packer instruction for a JSON field of type JSON object where the reference counter is incre...
Definition: json_pack.c:187
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp(const char *name, struct GNUNET_TIME_Timestamp *t)
Timestamp.
Definition: json_helper.c:862
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_unblinded_signature(const char *field, struct GNUNET_CRYPTO_UnblindedSignature **ub_sig)
Specification for parsing a unblinded signature.
Definition: json_helper.c:1595
struct GNUNET_JSON_Specification GNUNET_JSON_spec_array_const(const char *name, const json_t **jsonp)
JSON array, reference counter not incremented.
Definition: json_helper.c:452
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_signature(const char *name, const struct GNUNET_CRYPTO_RsaSignature *sig)
Generate packer instruction for a JSON field of type RSA signature.
Definition: json_pack.c:354
struct GNUNET_GETOPT_CommandLineOption GNUNET_JSON_getopt(char shortName, const char *name, const char *argumentHelp, const char *description, json_t **json)
Allow user to specify a JSON input value.
Definition: json.c:141
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_data64_varsize(const char *name, const void *blob, size_t blob_size)
Generate packer instruction for a JSON field of type variable size binary blob.
Definition: json_pack.c:272
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_rsa_public_key(const char *name, const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Generate packer instruction for a JSON field of type RSA public key.
Definition: json_pack.c:341
struct GNUNET_JSON_Specification GNUNET_JSON_spec_fixed64(const char *name, void *obj, size_t size)
Variable size object (in network byte order, encoded using base64 encoding).
Definition: json_helper.c:181
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_int64(const char *name, int64_t num)
Generate packer instruction for a JSON field of type signed integer.
Definition: json_pack.c:145
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_time_rel_nbo(const char *name, struct GNUNET_TIME_RelativeNBO rt)
Generate packer instruction for a JSON field of type relative time in network byte order.
Definition: json_pack.c:332
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool(const char *name, bool *b)
boolean.
Definition: json_helper.c:500
void GNUNET_JSON_post_parser_cleanup(void *con_cls)
Function called whenever we are done with a request to clean up our state.
Definition: json_mhd.c:367
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time(const char *name, struct GNUNET_TIME_Relative *rt)
Relative time.
Definition: json_helper.c:977
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8(const char *name, uint8_t *u8)
8-bit integer.
Definition: json_helper.c:593
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64(const char *name, uint64_t *u64)
64-bit integer.
Definition: json_helper.c:738
enum GNUNET_GenericReturnValue GNUNET_JSON_parse(const json_t *root, struct GNUNET_JSON_Specification *spec, const char **error_json_name, unsigned int *error_line)
Navigate and parse data in a JSON tree.
Definition: json.c:32
struct GNUNET_JSON_Specification GNUNET_JSON_spec_end(void)
End of a parser specification.
Definition: json_helper.c:33
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_string(const char *name, const char *s)
Generate packer instruction for a JSON field of type string.
Definition: json_pack.c:114
GNUNET_GenericReturnValue
Named constants for return values.
static unsigned int size
Size of the "table".
Definition: peer.c:68
Blinded message ready for blind signing.
Type for blinded signatures.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Type of (unblinded) signatures.
Definition of a command line option.
const char * description
Help text for the option (description)
const char * argumentHelp
Name of the argument for the user in help text.
const char shortName
Short name of the option.
Element in the array to give to the packer.
bool allow_null
True if a NULL (or 0) argument is allowed.
json_t * object
Object to pack.
const char * field_name
Name of the field to pack.
Entry in parser specification for GNUNET_JSON_parse().
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
bool * missing
Pointer to set to true if this argument is indeed missing.
void * ptr
Pointer, details specific to the parser.
const char * field
Name of the field to parse, use NULL to get the JSON of the main object instead of the JSON of an ind...
size_t ptr_size
Number of bytes available in ptr.
size_t * size_ptr
Where should we store the final size of ptr.
bool is_optional
Set to true if this component is optional.
void * cls
Closure for parser and cleaner.
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
Time for relative time used by GNUnet, in microseconds and in network byte order.
Time for relative time used by GNUnet, in microseconds.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Rounded time for timestamps used by GNUnet, in seconds.