GNUnet 0.24.1-16-gbc519cf4b
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, 2024 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#include "gnunet_util_lib.h"
31#include <jansson.h>
32
33/* ****************** Generic parser interface ******************* */
34
39
40
51(*GNUNET_JSON_Parser)(void *cls,
52 json_t *root,
53 struct GNUNET_JSON_Specification *spec);
54
55
62typedef void
63(*GNUNET_JSON_Cleaner) (void *cls,
64 struct GNUNET_JSON_Specification *spec);
65
66
71{
76
81
85 void *cls;
86
91 const char *field;
92
96 void *ptr;
97
102 bool *missing;
103
107 size_t *size_ptr;
108
112 size_t ptr_size;
113
118};
119
120
135GNUNET_JSON_parse (const json_t *root,
136 struct GNUNET_JSON_Specification *spec,
137 const char **error_json_name,
138 unsigned int *error_line);
139
140
153void
155
156
157/* ****************** Canonical parser specifications ******************* */
158
159
165
166
176 bool *missing);
177
178
188GNUNET_JSON_spec_fixed (const char *name,
189 void *obj,
190 size_t size);
191
192
200#define GNUNET_JSON_spec_fixed_auto(name, obj) \
201 GNUNET_JSON_spec_fixed (name, (obj), sizeof(*(obj)))
202
203
212GNUNET_JSON_spec_fixed64 (const char *name,
213 void *obj,
214 size_t size);
215
216
223#define GNUNET_JSON_spec_fixed64_auto(name, obj) \
224 GNUNET_JSON_spec_fixed (name, (obj), sizeof(*(obj)))
225
226
236GNUNET_JSON_spec_varsize (const char *name,
237 void **obj,
238 size_t *size);
239
240
248GNUNET_JSON_spec_string (const char *name,
249 const char **strptr);
250
251
260 char **strptr);
261
262
271GNUNET_JSON_spec_json (const char *name,
272 json_t **jsonp);
273
274
283 json_t **jsonp);
284
285
294 json_t **jsonp);
295
296
305 const json_t **jsonp);
306
307
316 const json_t **jsonp);
317
318
326GNUNET_JSON_spec_bool (const char *name,
327 bool *b);
328
329
337GNUNET_JSON_spec_double (const char *name,
338 double *f);
339
340
348GNUNET_JSON_spec_uint8 (const char *name,
349 uint8_t *u8);
350
351
359GNUNET_JSON_spec_uint16 (const char *name,
360 uint16_t *u16);
361
362
370GNUNET_JSON_spec_uint32 (const char *name,
371 uint32_t *u32);
372
373
381GNUNET_JSON_spec_uint (const char *name,
382 unsigned int *ui);
383
391GNUNET_JSON_spec_ull (const char *name,
392 unsigned long long *ull);
393
394
402GNUNET_JSON_spec_uint64 (const char *name,
403 uint64_t *u64);
404
405
413GNUNET_JSON_spec_int16 (const char *name,
414 int16_t *i16);
415
423GNUNET_JSON_spec_int64 (const char *name,
424 int64_t *i64);
425
426
434GNUNET_JSON_spec_boolean (const char *name,
435 int *boolean);
436
437
438/* ************ GNUnet-specific parser specifications ******************* */
439
448 struct GNUNET_TIME_Timestamp *t);
449
450
459 struct GNUNET_TIME_TimestampNBO *tn);
460
461
470 struct GNUNET_TIME_Relative *rt);
471
472
482
483
492 struct GNUNET_CRYPTO_RsaSignature **sig);
493
494
504
505
514 const char *field,
515 struct GNUNET_CRYPTO_BlindedSignature **b_sig);
516
517
526 const char *field,
527 struct GNUNET_CRYPTO_UnblindedSignature **ub_sig);
528
529
530/* ****************** Generic generator interface ******************* */
531
532
541json_t *
542GNUNET_JSON_from_data (const void *data,
543 size_t size);
544
545
554json_t *
555GNUNET_JSON_from_data64 (const void *data,
556 size_t size);
557
558
566#define GNUNET_JSON_from_data_auto(ptr) \
567 GNUNET_JSON_from_data (ptr, sizeof(*(ptr)))
568
569
577#define GNUNET_JSON_from_data64_auto(ptr) \
578 GNUNET_JSON_from_data64 (ptr, sizeof(*(ptr)))
579
580
587json_t *
589
590
597json_t *
599
600
607json_t *
609
610
617json_t *
619
620
627json_t *
629
630/* ****************** GETOPT JSON helper ******************* */
631
632
644 const char *name,
645 const char *argumentHelp,
646 const char *description,
647 json_t **json);
648
649
650/* ****************** JSON PACK helper ******************* */
651
652
657
658
666typedef json_t *
667(*GNUNET_JSON_PackCallback)(const struct GNUNET_JSON_PackSpec *se);
668
669
674{
680 const char *field_name;
681
685 json_t *object;
686
694
698 bool final;
699};
700
701
709json_t *
711
712
720#define GNUNET_JSON_PACK(...) \
721 GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, \
722 GNUNET_JSON_pack_end_ ()})
723
724
732
733
742
743
753GNUNET_JSON_pack_bool (const char *name,
754 bool b);
755
756
766GNUNET_JSON_pack_double (const char *name,
767 double f);
768
769
779GNUNET_JSON_pack_string (const char *name,
780 const char *s);
781
782
793GNUNET_JSON_pack_uint64 (const char *name,
794 uint64_t num);
795
796
806GNUNET_JSON_pack_int64 (const char *name,
807 int64_t num);
808
809
822 json_t *o);
823
824
837 json_t *o);
838
839
851 json_t *a);
852
853
865 json_t *a);
866
867
879 const void *blob,
880 size_t blob_size);
881
882
891#define GNUNET_JSON_pack_data_auto(name,blob) \
892 GNUNET_JSON_pack_data_varsize (name, (blob), sizeof (*(blob)))
893
894
908 const void *blob,
909 size_t blob_size);
910
911
922#define GNUNET_JSON_pack_data64_auto(name,blob) \
923 GNUNET_JSON_pack_data64_varsize (name, (blob), sizeof (*(blob)))
924
925
937 struct GNUNET_TIME_Timestamp at);
938
939
951 struct GNUNET_TIME_TimestampNBO at);
952
953
964 struct GNUNET_TIME_Relative rt);
965
966
977 struct GNUNET_TIME_RelativeNBO rt);
978
979
990 const struct GNUNET_CRYPTO_RsaPublicKey *pk);
991
992
1003 const struct GNUNET_CRYPTO_RsaSignature *sig);
1004
1005
1016 const char *name,
1017 const struct GNUNET_CRYPTO_UnblindedSignature *sig);
1018
1019
1030 const char *name,
1031 const struct GNUNET_CRYPTO_BlindedMessage *msg);
1032
1033
1044 const struct GNUNET_CRYPTO_BlindedSignature *sig);
1045
1046#endif
1047
1048/* 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:335
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:437
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string_copy(const char *name, char **strptr)
The expected field stores a string.
Definition: json_helper.c:338
struct GNUNET_JSON_PackSpec GNUNET_JSON_pack_end_(void)
Do not use directly.
Definition: json_pack.c:75
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:1552
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:250
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:115
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:221
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:304
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:100
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:268
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:396
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int16(const char *name, int16_t *i16)
16-bit signed integer.
Definition: json_helper.c:969
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:1687
void(* GNUNET_JSON_Cleaner)(void *cls, struct GNUNET_JSON_Specification *spec)
Function called to clean up data from earlier parsing.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_ull(const char *name, unsigned long long *ull)
Unsigned long long.
Definition: json_helper.c:879
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:94
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:1415
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:1286
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:738
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:176
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:244
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:1132
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:86
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint32(const char *name, uint32_t *u32)
32-bit integer.
Definition: json_helper.c:785
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:384
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:108
struct GNUNET_JSON_Specification GNUNET_JSON_spec_string(const char *name, const char **strptr)
The expected field stores a string.
Definition: json_helper.c:296
struct GNUNET_JSON_Specification GNUNET_JSON_spec_array_copy(const char *name, json_t **jsonp)
JSON array, reference counter not incremented.
Definition: json_helper.c:558
struct GNUNET_JSON_Specification GNUNET_JSON_spec_double(const char *name, double *f)
double.
Definition: json_helper.c:644
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:422
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:326
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:464
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:88
struct GNUNET_JSON_Specification GNUNET_JSON_spec_int64(const char *name, int64_t *i64)
64-bit signed integer.
Definition: json_helper.c:1011
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:1371
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:136
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint(const char *name, unsigned int *ui)
Unsigned integer.
Definition: json_helper.c:832
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:198
struct GNUNET_JSON_Specification GNUNET_JSON_spec_timestamp(const char *name, struct GNUNET_TIME_Timestamp *t)
Timestamp.
Definition: json_helper.c:1087
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 an unblinded signature.
Definition: json_helper.c:1820
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:518
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:371
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:147
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:286
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:358
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:176
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:155
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:349
struct GNUNET_JSON_Specification GNUNET_JSON_spec_bool(const char *name, bool *b)
boolean.
Definition: json_helper.c:604
struct GNUNET_JSON_Specification GNUNET_JSON_spec_relative_time(const char *name, struct GNUNET_TIME_Relative *rt)
Relative time.
Definition: json_helper.c:1202
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint8(const char *name, uint8_t *u8)
8-bit integer.
Definition: json_helper.c:691
struct GNUNET_JSON_Specification GNUNET_JSON_spec_uint64(const char *name, uint64_t *u64)
64-bit integer.
Definition: json_helper.c:921
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_object_copy(const char *name, json_t **jsonp)
JSON object, reference counter not incremented.
Definition: json_helper.c:477
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:122
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.
Time for timestamps used by GNUnet, in microseconds rounded to seconds.