This file is part of GNUnet
Copyright (C) 2014, 2015, 2016 GNUnet e.V.
- GNUnet is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
*/
/**
* @file gnunet_json_lib.h
#ifndef GNUNET_JSON_LIB_H
#define GNUNET_JSON_LIB_H
-#include <gnunet/gnunet_util_lib.h>
+#include "gnunet_util_lib.h"
+#include "gnunet_gnsrecord_lib.h"
#include <jansson.h>
GNUNET_JSON_spec_uint64 (const char *name,
uint64_t *u64);
+/**
+ * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
+ *
+ * @param name name of the JSON field
+ * @param[out] boolean where to store the boolean found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_boolean (const char *name,
+ int *boolean);
+
/* ************ GNUnet-specific parser specifications ******************* */
struct GNUNET_TIME_Absolute *at);
+/**
+ * Absolute time in network byte order.
+ *
+ * @param name name of the JSON field
+ * @param[out] at where to store the absolute time found under @a name
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_absolute_time_nbo (const char *name,
+ struct GNUNET_TIME_AbsoluteNBO *at);
+
+
/**
* Relative time.
*
*/
struct GNUNET_JSON_Specification
GNUNET_JSON_spec_rsa_public_key (const char *name,
- struct GNUNET_CRYPTO_rsa_PublicKey **pk);
+ struct GNUNET_CRYPTO_RsaPublicKey **pk);
/**
*/
struct GNUNET_JSON_Specification
GNUNET_JSON_spec_rsa_signature (const char *name,
- struct GNUNET_CRYPTO_rsa_Signature **sig);
+ struct GNUNET_CRYPTO_RsaSignature **sig);
+
+
+
+/**
+ * JSON Specification for GNS Records.
+ *
+ * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill
+ * @return JSON Specification
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object);
/* ****************** Generic generator interface ******************* */
size_t size);
+/**
+ * Convert binary data to a JSON string with the base32crockford
+ * encoding.
+ *
+ * @param ptr binary data, sizeof (*ptr) must yield correct size
+ * @return json string that encodes @a data
+ */
+#define GNUNET_JSON_from_data_auto(ptr) GNUNET_JSON_from_data(ptr, sizeof (*ptr))
+
+
/**
* Convert absolute timestamp to a json string.
*
GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp);
+/**
+ * Convert absolute timestamp to a json string.
+ *
+ * @param stamp the time stamp
+ * @return a json string with the timestamp in @a stamp
+ */
+json_t *
+GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp);
+
+
/**
* Convert relative timestamp to a json string.
*
* @return corresponding JSON encoding
*/
json_t *
-GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_rsa_PublicKey *pk);
+GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk);
/**
* @return corresponding JSON encoding
*/
json_t *
-GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_rsa_Signature *sig);
+GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig);
+
+/**
+ * Convert Gns record to JSON.
+ *
+ * @param rname name of record
+ * @param rd record data
+ * @return corresponding JSON encoding
+ */
+json_t *
+GNUNET_JSON_from_gns_record (const char* rname,
+ const struct GNUNET_GNSRECORD_Data *rd);
+
+/* ******************* Helpers for MHD upload handling ******************* */
+
+/**
+ * Return codes from #GNUNET_JSON_post_parser().
+ */
+enum GNUNET_JSON_PostResult {
+ /**
+ * Parsing successful, JSON result is in `*json`.
+ */
+ GNUNET_JSON_PR_SUCCESS,
+ /**
+ * Parsing continues, call again soon!
+ */
+ GNUNET_JSON_PR_CONTINUE,
+
+ /**
+ * Sorry, memory allocation (malloc()) failed.
+ */
+ GNUNET_JSON_PR_OUT_OF_MEMORY,
+
+ /**
+ * Request size exceeded `buffer_max` argument.
+ */
+ GNUNET_JSON_PR_REQUEST_TOO_LARGE,
+
+ /**
+ * JSON parsing failed. This was not a JSON upload.
+ */
+ GNUNET_JSON_PR_JSON_INVALID
+};
+
+
+/**
+ * Process a POST request containing a JSON object. This function
+ * realizes an MHD POST processor that will (incrementally) process
+ * JSON data uploaded to the HTTP server. It will store the required
+ * state in the @a con_cls, which must be cleaned up using
+ * #GNUNET_JSON_post_parser_callback().
+ *
+ * @param buffer_max maximum allowed size for the buffer
+ * @param con_cls the closure (will point to a `struct Buffer *`)
+ * @param upload_data the POST data
+ * @param upload_data_size number of bytes in @a upload_data
+ * @param json the JSON object for a completed request
+ * @return result code indicating the status of the operation
+ */
+enum GNUNET_JSON_PostResult
+GNUNET_JSON_post_parser (size_t buffer_max,
+ void **con_cls,
+ const char *upload_data,
+ size_t *upload_data_size,
+ json_t **json);
+
+
+/**
+ * Function called whenever we are done with a request
+ * to clean up our state.
+ *
+ * @param con_cls value as it was left by
+ * #GNUNET_JSON_post_parser(), to be cleaned up
+ */
+void
+GNUNET_JSON_post_parser_cleanup (void *con_cls);
+
+
+/* ****************** GETOPT JSON helper ******************* */
+
+
+/**
+ * Allow user to specify a JSON input value.
+ *
+ * @param shortName short name of the option
+ * @param name long name of the option
+ * @param argumentHelp help text for the option argument
+ * @param description long help text for the option
+ * @param[out] val set to the JSON specified at the command line
+ */
+struct GNUNET_GETOPT_CommandLineOption
+GNUNET_JSON_getopt (char shortName,
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ json_t **json);
#endif