2011-12-21 09:55:05 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
This program 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 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
#ifndef __SERVALDNA__RHIZOME_H
|
|
|
|
#define __SERVALDNA__RHIZOME_H
|
|
|
|
|
2012-02-24 06:51:22 +00:00
|
|
|
#include <sqlite3.h>
|
2011-12-18 21:40:02 +00:00
|
|
|
#include "sha2.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "str.h"
|
2012-05-25 04:59:55 +00:00
|
|
|
#include "strbuf.h"
|
2012-10-19 04:24:41 +00:00
|
|
|
#include "nacl.h"
|
2011-12-18 21:40:02 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-08-24 05:56:25 +00:00
|
|
|
#ifndef __RHIZOME_INLINE
|
|
|
|
# if __GNUC__ && !__GNUC_STDC_INLINE__
|
|
|
|
# define __RHIZOME_INLINE extern inline
|
|
|
|
# else
|
|
|
|
# define __RHIZOME_INLINE inline
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
// TODO Rename MANIFEST_ID to BUNDLE_ID
|
2012-05-23 06:34:00 +00:00
|
|
|
#define RHIZOME_MANIFEST_ID_BYTES crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES
|
|
|
|
#define RHIZOME_MANIFEST_ID_STRLEN (RHIZOME_MANIFEST_ID_BYTES * 2)
|
2012-10-09 16:37:49 +00:00
|
|
|
#define RHIZOME_BUNDLE_KEY_BYTES (crypto_sign_edwards25519sha512batch_SECRETKEYBYTES-crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES)
|
2012-05-23 06:34:00 +00:00
|
|
|
#define RHIZOME_BUNDLE_KEY_STRLEN (RHIZOME_BUNDLE_KEY_BYTES * 2)
|
|
|
|
#define RHIZOME_FILEHASH_BYTES SHA512_DIGEST_LENGTH
|
|
|
|
#define RHIZOME_FILEHASH_STRLEN (RHIZOME_FILEHASH_BYTES * 2)
|
|
|
|
|
2012-06-01 08:42:59 +00:00
|
|
|
#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES
|
|
|
|
#define RHIZOME_CRYPT_KEY_STRLEN (RHIZOME_CRYPT_KEY_BYTES * 2)
|
2012-12-21 00:23:47 +00:00
|
|
|
|
|
|
|
// assumed to always be 2^n
|
2012-06-01 08:42:59 +00:00
|
|
|
#define RHIZOME_CRYPT_PAGE_SIZE 4096
|
2012-05-26 04:12:33 +00:00
|
|
|
|
2011-12-21 05:58:08 +00:00
|
|
|
#define RHIZOME_HTTP_PORT 4110
|
2012-07-02 06:54:07 +00:00
|
|
|
#define RHIZOME_HTTP_PORT_MAX 4150
|
2011-12-21 05:58:08 +00:00
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
/* Fundamental data type: Rhizome Bundle ID
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
typedef struct rhizome_bid_binary {
|
|
|
|
unsigned char binary[RHIZOME_MANIFEST_ID_BYTES];
|
|
|
|
} rhizome_bid_t;
|
|
|
|
|
|
|
|
#define RHIZOME_BID_ZERO ((rhizome_bid_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}})
|
|
|
|
#define RHIZOME_BID_MAX ((rhizome_bid_t){{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}})
|
|
|
|
#define rhizome_bid_t_is_zero(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0)
|
|
|
|
#define rhizome_bid_t_is_max(bid) is_all_matching((bid).binary, sizeof (*(rhizome_bid_t*)0).binary, 0xff)
|
|
|
|
#define alloca_tohex_rhizome_bid_t(bid) alloca_tohex((bid).binary, sizeof (*(rhizome_bid_t*)0).binary)
|
|
|
|
int cmp_rhizome_bid_t(const rhizome_bid_t *a, const rhizome_bid_t *b);
|
|
|
|
int str_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex);
|
|
|
|
int strn_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, const char **endp);
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
/* Fundamental data type: Rhizome File Hash
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct rhizome_filehash_binary {
|
|
|
|
unsigned char binary[RHIZOME_FILEHASH_BYTES];
|
|
|
|
} rhizome_filehash_t;
|
|
|
|
|
|
|
|
#define RHIZOME_FILEHASH_NONE ((rhizome_filehash_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}})
|
|
|
|
#define rhizome_filehash_t_is_zero(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0)
|
|
|
|
#define rhizome_filehash_t_is_max(fh) is_all_matching((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary, 0xff)
|
|
|
|
#define alloca_tohex_rhizome_filehash_t(fh) alloca_tohex((fh).binary, sizeof (*(rhizome_filehash_t*)0).binary)
|
|
|
|
int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t *b);
|
|
|
|
int str_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex);
|
|
|
|
int strn_to_rhizome_filehash_t(rhizome_filehash_t *fh, const char *hex, const char **endp);
|
|
|
|
|
|
|
|
/* Fundamental data type: Rhizome Bundle Key
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
typedef struct rhizome_bk_binary {
|
|
|
|
unsigned char binary[RHIZOME_BUNDLE_KEY_BYTES];
|
|
|
|
} rhizome_bk_t;
|
|
|
|
|
|
|
|
#define RHIZOME_BK_NONE ((rhizome_bk_t){{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}})
|
|
|
|
|
|
|
|
__RHIZOME_INLINE int rhizome_is_bk_none(const rhizome_bk_t *bk) {
|
|
|
|
return is_all_matching(bk->binary, sizeof bk->binary, 0);
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:39:08 +00:00
|
|
|
#define alloca_tohex_rhizome_bk_t(bk) alloca_tohex((bk).binary, sizeof (*(rhizome_bk_t*)0).binary)
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
|
2012-08-09 02:44:32 +00:00
|
|
|
extern time_ms_t rhizome_voice_timeout;
|
2012-05-22 03:35:29 +00:00
|
|
|
|
2011-12-20 11:39:49 +00:00
|
|
|
#define RHIZOME_PRIORITY_HIGHEST RHIZOME_PRIORITY_SERVAL_CORE
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_CORE 5
|
|
|
|
#define RHIZOME_PRIORITY_SUBSCRIBED 4
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_OPTIONAL 3
|
|
|
|
#define RHIZOME_PRIORITY_DEFAULT 2
|
|
|
|
#define RHIZOME_PRIORITY_SERVAL_BULK 1
|
|
|
|
#define RHIZOME_PRIORITY_NOTINTERESTED 0
|
|
|
|
|
2013-09-17 02:37:20 +00:00
|
|
|
#define RHIZOME_IDLE_TIMEOUT 20000
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2013-01-03 00:48:30 +00:00
|
|
|
#define EXISTING_BUNDLE_ID 1
|
|
|
|
#define NEW_BUNDLE_ID 2
|
|
|
|
|
2011-12-20 06:57:24 +00:00
|
|
|
typedef struct rhizome_signature {
|
|
|
|
unsigned char signature[crypto_sign_edwards25519sha512batch_BYTES
|
2011-12-20 21:16:12 +00:00
|
|
|
+crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES+1];
|
2011-12-20 06:57:24 +00:00
|
|
|
int signatureLength;
|
|
|
|
} rhizome_signature;
|
|
|
|
|
2012-01-03 06:05:02 +00:00
|
|
|
#define RHIZOME_BAR_BYTES 32
|
2012-10-02 09:54:35 +00:00
|
|
|
#define RHIZOME_BAR_COMPARE_BYTES 31
|
|
|
|
#define RHIZOME_BAR_PREFIX_BYTES 15
|
|
|
|
#define RHIZOME_BAR_PREFIX_OFFSET 0
|
|
|
|
#define RHIZOME_BAR_FILESIZE_OFFSET 15
|
|
|
|
#define RHIZOME_BAR_VERSION_OFFSET 16
|
|
|
|
#define RHIZOME_BAR_GEOBOX_OFFSET 23
|
|
|
|
#define RHIZOME_BAR_TTL_OFFSET 31
|
2012-01-03 06:05:02 +00:00
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
#define MAX_MANIFEST_VARS 256
|
|
|
|
#define MAX_MANIFEST_BYTES 8192
|
|
|
|
typedef struct rhizome_manifest {
|
2012-01-28 01:15:45 +00:00
|
|
|
int manifest_record_number;
|
2011-12-18 21:34:31 +00:00
|
|
|
int manifest_bytes;
|
2012-01-13 10:43:17 +00:00
|
|
|
int manifest_all_bytes;
|
2011-12-18 21:34:31 +00:00
|
|
|
unsigned char manifestdata[MAX_MANIFEST_BYTES];
|
2011-12-18 21:52:34 +00:00
|
|
|
unsigned char manifesthash[crypto_hash_sha512_BYTES];
|
2011-12-18 21:34:31 +00:00
|
|
|
|
|
|
|
/* CryptoSign key pair for this manifest.
|
|
|
|
The filename as distributed on Rhizome will be the public key
|
|
|
|
of this pair, thus ensuring that noone can tamper with a bundle
|
|
|
|
except the creator. */
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t cryptoSignPublic;
|
2011-12-18 21:52:34 +00:00
|
|
|
unsigned char cryptoSignSecret[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES];
|
2013-08-01 02:07:35 +00:00
|
|
|
/* Whether we have the secret for this manifest on hand */
|
|
|
|
int haveSecret;
|
2011-12-18 21:34:31 +00:00
|
|
|
|
|
|
|
int var_count;
|
|
|
|
char *vars[MAX_MANIFEST_VARS];
|
|
|
|
char *values[MAX_MANIFEST_VARS];
|
|
|
|
|
|
|
|
int sig_count;
|
2011-12-20 21:16:12 +00:00
|
|
|
/* Parties who have signed this manifest (raw byte format) */
|
|
|
|
unsigned char *signatories[MAX_MANIFEST_VARS];
|
|
|
|
/*
|
2012-10-29 05:37:42 +00:00
|
|
|
0x17 = crypto_sign_edwards25519sha512batch()
|
2011-12-20 21:16:12 +00:00
|
|
|
*/
|
2011-12-18 21:34:31 +00:00
|
|
|
unsigned char signatureTypes[MAX_MANIFEST_VARS];
|
2011-12-20 21:16:12 +00:00
|
|
|
|
2013-08-01 02:07:35 +00:00
|
|
|
// errors only involve the correctness of fields that are mandatory for
|
|
|
|
// proper operation of the transport and storage layer
|
|
|
|
int errors;
|
|
|
|
// a warning indicates that the manifest cannot be perfectly understood by this version of rhizome
|
|
|
|
// during add, the manifest should not be finalised and imported
|
|
|
|
// during extract an error should be displayed.
|
|
|
|
int warnings;
|
2013-01-02 00:42:15 +00:00
|
|
|
time_ms_t inserttime;
|
2013-01-03 05:42:24 +00:00
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
/* Set non-zero after variables have been packed and
|
|
|
|
signature blocks appended.
|
2011-12-20 00:55:52 +00:00
|
|
|
All fields below may not be valid until the manifest has been finalised */
|
2011-12-18 21:34:31 +00:00
|
|
|
int finalised;
|
|
|
|
|
2012-01-03 06:05:02 +00:00
|
|
|
/* time-to-live in hops of this manifest. */
|
|
|
|
int ttl;
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
/* When finalised, we keep the filehash and maximum priority due to any
|
|
|
|
group membership handy */
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t fileLength;
|
|
|
|
int64_t journalTail;
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t filehash;
|
2013-07-08 05:27:47 +00:00
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
int fileHighestPriority;
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Absolute path of the file associated with the manifest */
|
|
|
|
char *dataFileName;
|
2012-10-18 07:00:22 +00:00
|
|
|
/* If set, unlink(2) the associated file when freeing the manifest */
|
|
|
|
int dataFileUnlinkOnFree;
|
2013-01-03 05:42:24 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Whether the paylaod is encrypted or not */
|
2013-01-03 05:42:24 +00:00
|
|
|
int payloadEncryption;
|
|
|
|
unsigned char payloadKey[RHIZOME_CRYPT_KEY_BYTES];
|
|
|
|
unsigned char payloadNonce[crypto_stream_xsalsa20_NONCEBYTES];
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-20 05:18:26 +00:00
|
|
|
/* Whether the manifest contains a signature that corresponds to the
|
|
|
|
manifest id (ie public key) */
|
|
|
|
int selfSigned;
|
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
/* Version of the manifest. Typically the number of milliseconds since 1970. */
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t version;
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-20 02:54:09 +00:00
|
|
|
int group_count;
|
|
|
|
char *groups[MAX_MANIFEST_VARS];
|
|
|
|
|
2012-10-09 07:13:34 +00:00
|
|
|
/* Author of the manifest. A reference to a local keyring entry. Manifests
|
|
|
|
* not authored locally will have the ANY author (all zeros).
|
|
|
|
*/
|
2013-10-09 08:24:21 +00:00
|
|
|
sid_t author;
|
2012-10-09 07:13:34 +00:00
|
|
|
|
2011-12-18 21:34:31 +00:00
|
|
|
} rhizome_manifest;
|
|
|
|
|
2012-05-20 06:37:22 +00:00
|
|
|
/* Supported service identifiers. These go in the 'service' field of every
|
|
|
|
* manifest, and indicate which application must be used to process the bundle
|
|
|
|
* after it is received by Rhizome.
|
|
|
|
*/
|
|
|
|
#define RHIZOME_SERVICE_FILE "file"
|
|
|
|
#define RHIZOME_SERVICE_MESHMS "MeshMS1"
|
2013-08-01 02:07:35 +00:00
|
|
|
#define RHIZOME_SERVICE_MESHMS2 "MeshMS2"
|
2012-05-20 06:37:22 +00:00
|
|
|
|
2013-07-13 05:17:06 +00:00
|
|
|
extern int64_t rhizome_space;
|
2013-10-07 13:07:37 +00:00
|
|
|
extern uint16_t rhizome_http_server_port;
|
2012-07-11 07:20:50 +00:00
|
|
|
|
2013-07-13 05:17:06 +00:00
|
|
|
int log2ll(uint64_t x);
|
2012-07-11 07:20:50 +00:00
|
|
|
int rhizome_configure();
|
2012-10-24 04:43:50 +00:00
|
|
|
int rhizome_enabled();
|
|
|
|
int rhizome_fetch_delay_ms();
|
2012-05-24 01:58:32 +00:00
|
|
|
|
2012-05-15 03:26:10 +00:00
|
|
|
int rhizome_set_datastore_path(const char *path);
|
2012-05-24 01:58:32 +00:00
|
|
|
|
|
|
|
const char *rhizome_datastore_path();
|
2012-05-14 06:02:28 +00:00
|
|
|
int form_rhizome_datastore_path(char * buf, size_t bufsiz, const char *fmt, ...);
|
|
|
|
int create_rhizome_datastore_dir();
|
|
|
|
|
|
|
|
/* Handy statement for forming the path of a rhizome store file in a char buffer whose declaration
|
|
|
|
* is in scope (so that sizeof(buf) will work). Evaluates to true if the pathname fitted into
|
2012-05-24 01:58:32 +00:00
|
|
|
* the provided buffer, false (0) otherwise (after logging an error). */
|
2012-05-14 06:02:28 +00:00
|
|
|
#define FORM_RHIZOME_DATASTORE_PATH(buf,fmt,...) (form_rhizome_datastore_path((buf), sizeof(buf), (fmt), ##__VA_ARGS__))
|
2012-05-24 01:58:32 +00:00
|
|
|
#define FORM_RHIZOME_IMPORT_PATH(buf,fmt,...) (form_rhizome_import_path((buf), sizeof(buf), (fmt), ##__VA_ARGS__))
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-18 21:40:02 +00:00
|
|
|
extern sqlite3 *rhizome_db;
|
2011-12-18 21:34:31 +00:00
|
|
|
|
2011-12-20 00:55:52 +00:00
|
|
|
int rhizome_opendb();
|
2012-10-29 01:25:14 +00:00
|
|
|
int rhizome_close_db();
|
2013-06-14 02:28:19 +00:00
|
|
|
void verify_bundles();
|
2012-07-12 07:09:01 +00:00
|
|
|
|
2013-02-20 04:14:29 +00:00
|
|
|
struct rhizome_cleanup_report {
|
2013-10-06 17:12:17 +00:00
|
|
|
unsigned deleted_stale_incoming_files;
|
|
|
|
unsigned deleted_orphan_files;
|
|
|
|
unsigned deleted_orphan_fileblobs;
|
2013-02-20 04:14:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int rhizome_cleanup(struct rhizome_cleanup_report *report);
|
|
|
|
|
|
|
|
int rhizome_manifest_createid(rhizome_manifest *m);
|
2013-07-25 05:16:34 +00:00
|
|
|
int rhizome_get_bundle_from_seed(rhizome_manifest *m, const char *seed);
|
2012-05-02 08:27:35 +00:00
|
|
|
int rhizome_strn_is_manifest_id(const char *text);
|
|
|
|
int rhizome_str_is_manifest_id(const char *text);
|
2012-06-05 04:28:59 +00:00
|
|
|
int rhizome_strn_is_bundle_key(const char *text);
|
|
|
|
int rhizome_str_is_bundle_key(const char *text);
|
2012-06-01 08:42:59 +00:00
|
|
|
int rhizome_strn_is_bundle_crypt_key(const char *text);
|
|
|
|
int rhizome_str_is_bundle_crypt_key(const char *text);
|
2012-05-02 08:27:35 +00:00
|
|
|
int rhizome_strn_is_file_hash(const char *text);
|
|
|
|
int rhizome_str_is_file_hash(const char *text);
|
2012-07-12 07:09:01 +00:00
|
|
|
|
2012-10-04 08:41:16 +00:00
|
|
|
int http_header_complete(const char *buf, size_t len, size_t read_since_last_call);
|
2012-07-13 08:36:55 +00:00
|
|
|
|
2012-08-23 08:13:35 +00:00
|
|
|
typedef struct sqlite_retry_state {
|
|
|
|
unsigned int limit; // do not retry once elapsed >= limit
|
|
|
|
unsigned int sleep; // number of milliseconds to sleep between retries
|
|
|
|
unsigned int elapsed; // the total number of milliseconds elapsed doing retries
|
2012-08-24 05:56:25 +00:00
|
|
|
time_ms_t start; // the gettime_ms() value just after the current SQL query first returned BUSY
|
|
|
|
unsigned int busytries; // the number of times the current SQL query has returned BUSY
|
2012-08-23 08:13:35 +00:00
|
|
|
}
|
|
|
|
sqlite_retry_state;
|
|
|
|
|
|
|
|
sqlite_retry_state sqlite_retry_state_init(int serverLimit, int serverSleep, int otherLimit, int otherSleep);
|
|
|
|
|
|
|
|
#define SQLITE_RETRY_STATE_DEFAULT sqlite_retry_state_init(-1,-1,-1,-1)
|
|
|
|
|
2013-01-11 03:49:26 +00:00
|
|
|
int rhizome_write_manifest_file(rhizome_manifest *m, const char *filename, char append);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_manifest_selfsign(rhizome_manifest *m);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_drop_stored_file(const rhizome_filehash_t *hashp, int maximum_priority);
|
2013-10-03 19:20:37 +00:00
|
|
|
int rhizome_manifest_priority(sqlite_retry_state *retry, const rhizome_bid_t *bidp);
|
2012-05-25 23:54:47 +00:00
|
|
|
int rhizome_read_manifest_file(rhizome_manifest *m, const char *filename, int bufferPAndSize);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_hash_file(rhizome_manifest *m, const char *path, rhizome_filehash_t *hash_out, uint64_t *size_out);
|
2012-04-12 09:00:52 +00:00
|
|
|
char *rhizome_manifest_get(const rhizome_manifest *m, const char *var, char *out, int maxlen);
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t rhizome_manifest_get_ll(rhizome_manifest *m, const char *var);
|
|
|
|
int rhizome_manifest_set_ll(rhizome_manifest *m,char *var, int64_t value);
|
2012-04-02 08:12:40 +00:00
|
|
|
int rhizome_manifest_set(rhizome_manifest *m, const char *var, const char *value);
|
2012-07-16 09:01:00 +00:00
|
|
|
int rhizome_manifest_del(rhizome_manifest *m, const char *var);
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t rhizome_file_size(char *filename);
|
2012-10-16 06:16:52 +00:00
|
|
|
void _rhizome_manifest_free(struct __sourceloc __whence, rhizome_manifest *m);
|
|
|
|
#define rhizome_manifest_free(m) _rhizome_manifest_free(__WHENCE__,m)
|
|
|
|
rhizome_manifest *_rhizome_new_manifest(struct __sourceloc __whence);
|
|
|
|
#define rhizome_new_manifest() _rhizome_new_manifest(__WHENCE__)
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_pack_variables(rhizome_manifest *m);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_store_bundle(rhizome_manifest *m);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_remove_file_datainvalid(sqlite_retry_state *retry, const rhizome_filehash_t *hashp);
|
2011-12-18 21:34:31 +00:00
|
|
|
int rhizome_manifest_add_group(rhizome_manifest *m,char *groupid);
|
2012-05-25 04:59:55 +00:00
|
|
|
int rhizome_clean_payload(const char *fileidhex);
|
2012-05-26 04:19:13 +00:00
|
|
|
int rhizome_store_file(rhizome_manifest *m,const unsigned char *key);
|
2012-12-20 04:48:59 +00:00
|
|
|
int rhizome_bundle_import_files(rhizome_manifest *m, const char *manifest_path, const char *filepath);
|
2013-10-09 08:24:21 +00:00
|
|
|
int rhizome_fill_manifest(rhizome_manifest *m, const char *filepath, const sid_t *authorSidp, rhizome_bk_t *bsk);
|
2012-05-25 15:20:48 +00:00
|
|
|
|
2012-05-27 06:30:51 +00:00
|
|
|
int rhizome_manifest_verify(rhizome_manifest *m);
|
2012-05-25 23:06:29 +00:00
|
|
|
int rhizome_manifest_check_sanity(rhizome_manifest *m_in);
|
2012-05-25 15:20:48 +00:00
|
|
|
|
2012-10-09 07:13:34 +00:00
|
|
|
int rhizome_manifest_bind_id(rhizome_manifest *m_in);
|
2013-09-30 07:12:25 +00:00
|
|
|
int rhizome_manifest_finalise(rhizome_manifest *m, rhizome_manifest **mout, int deduplicate);
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_add_manifest(rhizome_manifest *m_in,int ttl);
|
|
|
|
|
2012-05-23 06:34:00 +00:00
|
|
|
void rhizome_bytes_to_hex_upper(unsigned const char *in, char *out, int byteCount);
|
2012-05-15 07:54:25 +00:00
|
|
|
int rhizome_find_privatekey(rhizome_manifest *m);
|
2012-10-11 07:28:24 +00:00
|
|
|
int rhizome_sign_hash(rhizome_manifest *m, rhizome_signature *out);
|
2012-08-23 08:13:35 +00:00
|
|
|
|
2012-08-24 05:56:25 +00:00
|
|
|
__RHIZOME_INLINE int sqlite_code_ok(int code)
|
|
|
|
{
|
|
|
|
return code == SQLITE_OK || code == SQLITE_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
__RHIZOME_INLINE int sqlite_code_busy(int code)
|
|
|
|
{
|
|
|
|
return code == SQLITE_BUSY || code == SQLITE_LOCKED;
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
int (*sqlite_set_tracefunc(int (*newfunc)()))();
|
|
|
|
int is_debug_rhizome();
|
|
|
|
int is_debug_rhizome_ads();
|
2012-10-10 02:52:30 +00:00
|
|
|
|
2013-10-02 15:46:10 +00:00
|
|
|
enum sqlbind_type {
|
|
|
|
END = 0xbabecafe,
|
2013-10-10 07:53:06 +00:00
|
|
|
INT = 1, // int value
|
|
|
|
INT_TOSTR, // int value
|
|
|
|
UINT_TOSTR, // unsigned value
|
|
|
|
INT64, // int64_t value
|
|
|
|
INT64_TOSTR, // int64_t value
|
|
|
|
UINT64_TOSTR, // uint64_t value
|
|
|
|
TEXT, // const char *text,
|
|
|
|
TEXT_LEN, // const char *text, int bytes
|
|
|
|
STATIC_TEXT, // const char *text,
|
|
|
|
STATIC_TEXT_LEN, // const char *text, int bytes
|
|
|
|
STATIC_BLOB, // const void *blob, int bytes
|
|
|
|
ZEROBLOB, // int bytes
|
|
|
|
SID_T, // const sid_t *sidp
|
|
|
|
RHIZOME_BID_T, // const rhizome_bid_t *bidp
|
|
|
|
RHIZOME_FILEHASH_T, // const rhizome_filehash_t *hashp
|
|
|
|
TOHEX, // const unsigned char *binary, unsigned bytes
|
|
|
|
TEXT_TOUPPER, // const char *text,
|
|
|
|
TEXT_LEN_TOUPPER, // const char *text, unsigned bytes
|
|
|
|
NUL = 1 << 15, // NUL (no arg) ; NUL|INT, ...
|
2013-10-02 15:46:10 +00:00
|
|
|
INDEX = 0xfade0000, // INDEX|INT, int index, ...
|
|
|
|
NAMED = 0xdead0000 // NAMED|INT, const char *label, ...
|
|
|
|
};
|
|
|
|
|
2013-10-03 05:45:30 +00:00
|
|
|
sqlite3_stmt *_sqlite_prepare(struct __sourceloc, int log_level, sqlite_retry_state *retry, const char *sqltext);
|
2013-10-02 15:46:10 +00:00
|
|
|
int _sqlite_bind(struct __sourceloc __whence, int log_level, sqlite_retry_state *retry, sqlite3_stmt *statement, ...);
|
|
|
|
int _sqlite_vbind(struct __sourceloc __whence, int log_level, sqlite_retry_state *retry, sqlite3_stmt *statement, va_list ap);
|
2013-10-03 05:45:30 +00:00
|
|
|
sqlite3_stmt *_sqlite_prepare_bind(struct __sourceloc, int log_level, sqlite_retry_state *retry, const char *sqltext, ...);
|
2013-02-20 04:14:29 +00:00
|
|
|
int _sqlite_retry(struct __sourceloc, sqlite_retry_state *retry, const char *action);
|
|
|
|
void _sqlite_retry_done(struct __sourceloc, sqlite_retry_state *retry, const char *action);
|
2013-10-03 05:45:30 +00:00
|
|
|
int _sqlite_step(struct __sourceloc, int log_level, sqlite_retry_state *retry, sqlite3_stmt *statement);
|
|
|
|
int _sqlite_exec_void(struct __sourceloc, int log_level, const char *sqltext, ...);
|
|
|
|
int _sqlite_exec_void_retry(struct __sourceloc, int log_level, sqlite_retry_state *retry, const char *sqltext, ...);
|
2013-10-02 15:46:10 +00:00
|
|
|
int _sqlite_exec_int64(struct __sourceloc, int64_t *result, const char *sqltext, ...);
|
|
|
|
int _sqlite_exec_int64_retry(struct __sourceloc, sqlite_retry_state *retry, int64_t *result, const char *sqltext, ...);
|
|
|
|
int _sqlite_exec_strbuf(struct __sourceloc, strbuf sb, const char *sqltext, ...);
|
|
|
|
int _sqlite_exec_strbuf_retry(struct __sourceloc, sqlite_retry_state *retry, strbuf sb, const char *sqltext, ...);
|
|
|
|
int _sqlite_vexec_strbuf_retry(struct __sourceloc, sqlite_retry_state *retry, strbuf sb, const char *sqltext, va_list ap);
|
|
|
|
|
|
|
|
// The 'arg' arguments in the following macros appear to be unnecessary, but
|
|
|
|
// they serve a very useful purpose, so don't remove them! They ensure that
|
|
|
|
// programmers do not forget the bind args, of which there must be at least
|
|
|
|
// one, even if it is only 'END' to make no bindings at all.
|
2013-10-03 05:45:30 +00:00
|
|
|
#define sqlite_prepare(rs,sql) _sqlite_prepare(__WHENCE__, LOG_LEVEL_ERROR, (rs), (sql))
|
|
|
|
#define sqlite_prepare_loglevel(ll,rs,sql) _sqlite_prepare(__WHENCE__, (ll), (rs), (sql))
|
|
|
|
#define sqlite_prepare_bind(rs,sql,arg,...) _sqlite_prepare_bind(__WHENCE__, LOG_LEVEL_ERROR, (rs), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_prepare_bind_loglevel(ll,rs,sql,arg,...) _sqlite_prepare_bind(__WHENCE__, (ll), (rs), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_bind(rs,stmt,arg,...) _sqlite_bind(__WHENCE__, LOG_LEVEL_ERROR, (rs), (stmt), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_bind_loglevel(ll,rs,stmt,arg,...) _sqlite_bind(__WHENCE__, (ll), (rs), (stmt), arg, ##__VA_ARGS__)
|
2013-10-02 15:46:10 +00:00
|
|
|
#define sqlite_retry(rs,action) _sqlite_retry(__WHENCE__, (rs), (action))
|
|
|
|
#define sqlite_retry_done(rs,action) _sqlite_retry_done(__WHENCE__, (rs), (action))
|
2013-10-03 07:14:06 +00:00
|
|
|
#define sqlite_exec(stmt) _sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, NULL, (stmt))
|
|
|
|
#define sqlite_exec_retry(rs,stmt) _sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, (rs), (stmt))
|
|
|
|
#define sqlite_exec_retry_loglevel(ll,rs,stmt) _sqlite_exec(__WHENCE__, (ll), (rs), (stmt))
|
2013-10-03 05:45:30 +00:00
|
|
|
#define sqlite_step(stmt) _sqlite_step(__WHENCE__, LOG_LEVEL_ERROR, NULL, (stmt))
|
|
|
|
#define sqlite_step_retry(rs,stmt) _sqlite_step(__WHENCE__, LOG_LEVEL_ERROR, (rs), (stmt))
|
|
|
|
#define sqlite_exec_void(sql,arg,...) _sqlite_exec_void(__WHENCE__, LOG_LEVEL_ERROR, (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_void_loglevel(ll,sql,arg,...) _sqlite_exec_void(__WHENCE__, (ll), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_void_retry(rs,sql,arg,...) _sqlite_exec_void_retry(__WHENCE__, LOG_LEVEL_ERROR, (rs), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_void_retry_loglevel(ll,rs,sql,arg,...) _sqlite_exec_void_retry(__WHENCE__, (ll), (rs), (sql), arg, ##__VA_ARGS__)
|
2013-10-02 15:46:10 +00:00
|
|
|
#define sqlite_exec_int64(res,sql,arg,...) _sqlite_exec_int64(__WHENCE__, (res), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_int64_retry(rs,res,sql,arg,...) _sqlite_exec_int64_retry(__WHENCE__, (rs), (res), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_strbuf(sb,sql,arg,...) _sqlite_exec_strbuf(__WHENCE__, (sb), (sql), arg, ##__VA_ARGS__)
|
|
|
|
#define sqlite_exec_strbuf_retry(rs,sb,sql,arg,...) _sqlite_exec_strbuf_retry(__WHENCE__, (rs), (sb), (sql), arg, ##__VA_ARGS__)
|
2012-08-23 08:13:35 +00:00
|
|
|
|
2012-01-03 06:05:02 +00:00
|
|
|
double rhizome_manifest_get_double(rhizome_manifest *m,char *var,double default_value);
|
|
|
|
int rhizome_manifest_extract_signature(rhizome_manifest *m,int *ofs);
|
2012-05-26 04:12:33 +00:00
|
|
|
int rhizome_update_file_priority(const char *fileid);
|
2013-08-01 02:07:35 +00:00
|
|
|
int rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found);
|
2012-01-12 03:35:05 +00:00
|
|
|
int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar);
|
2013-06-18 03:57:26 +00:00
|
|
|
int64_t rhizome_bar_version(const unsigned char *bar);
|
2013-07-13 05:17:06 +00:00
|
|
|
uint64_t rhizome_bar_bidprefix_ll(unsigned char *bar);
|
2013-02-12 00:04:04 +00:00
|
|
|
int rhizome_is_bar_interesting(unsigned char *bar);
|
2013-08-22 05:44:21 +00:00
|
|
|
int rhizome_is_manifest_interesting(rhizome_manifest *m);
|
2013-07-03 07:21:27 +00:00
|
|
|
int rhizome_list_manifests(struct cli_context *context, const char *service, const char *name,
|
2013-01-17 01:13:31 +00:00
|
|
|
const char *sender_sid, const char *recipient_sid,
|
2013-01-22 04:56:40 +00:00
|
|
|
int limit, int offset, char count_rows);
|
2013-10-03 13:46:45 +00:00
|
|
|
int rhizome_retrieve_manifest(const rhizome_bid_t *bid, rhizome_manifest *m);
|
|
|
|
int rhizome_retrieve_manifest_by_prefix(const unsigned char *prefix, unsigned prefix_len, rhizome_manifest *m);
|
2013-08-20 23:51:40 +00:00
|
|
|
int rhizome_advertise_manifest(struct subscriber *dest, rhizome_manifest *m);
|
2013-10-03 13:46:45 +00:00
|
|
|
int rhizome_delete_bundle(const rhizome_bid_t *bidp);
|
|
|
|
int rhizome_delete_manifest(const rhizome_bid_t *bidp);
|
|
|
|
int rhizome_delete_payload(const rhizome_bid_t *bidp);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_delete_file(const rhizome_filehash_t *hashp);
|
2012-01-12 03:35:05 +00:00
|
|
|
|
|
|
|
#define RHIZOME_DONTVERIFY 0
|
|
|
|
#define RHIZOME_VERIFY 1
|
2012-01-13 06:51:06 +00:00
|
|
|
|
|
|
|
int rhizome_fetching_get_fds(struct pollfd *fds,int *fdcount,int fdmax);
|
2012-05-11 21:54:52 +00:00
|
|
|
int monitor_announce_bundle(rhizome_manifest *m);
|
2013-10-09 08:24:21 +00:00
|
|
|
int rhizome_find_secret(const sid_t *authorSidp, int *rs_len, const unsigned char **rs);
|
2012-10-09 16:37:49 +00:00
|
|
|
int rhizome_bk_xor_stream(
|
2013-10-03 13:46:45 +00:00
|
|
|
const rhizome_bid_t *bidp,
|
2012-10-09 16:37:49 +00:00
|
|
|
const unsigned char *rs,
|
|
|
|
const size_t rs_len,
|
|
|
|
unsigned char *xor_stream,
|
|
|
|
int xor_stream_byte_count);
|
|
|
|
int rhizome_bk2secret(rhizome_manifest *m,
|
2013-10-03 13:46:45 +00:00
|
|
|
const rhizome_bid_t *bidp,
|
2012-10-09 16:37:49 +00:00
|
|
|
const unsigned char *rs, const size_t rs_len,
|
|
|
|
/* The BK need only be the length of the secret half of the secret key */
|
|
|
|
const unsigned char bkin[RHIZOME_BUNDLE_KEY_BYTES],
|
|
|
|
unsigned char secret[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES]
|
|
|
|
);
|
|
|
|
int rhizome_secret2bk(
|
2013-10-03 13:46:45 +00:00
|
|
|
const rhizome_bid_t *bidp,
|
2012-10-09 16:37:49 +00:00
|
|
|
const unsigned char *rs, const size_t rs_len,
|
|
|
|
/* The BK need only be the length of the secret half of the secret key */
|
|
|
|
unsigned char bkout[RHIZOME_BUNDLE_KEY_BYTES],
|
|
|
|
const unsigned char secret[crypto_sign_edwards25519sha512batch_SECRETKEYBYTES]
|
2013-10-03 13:46:45 +00:00
|
|
|
);
|
2012-05-28 05:15:54 +00:00
|
|
|
unsigned char *rhizome_bundle_shared_secret(rhizome_manifest *m);
|
2013-01-03 01:44:13 +00:00
|
|
|
int rhizome_extract_privatekey(rhizome_manifest *m, rhizome_bk_t *bsk);
|
2013-01-03 05:42:24 +00:00
|
|
|
int rhizome_extract_privatekey_required(rhizome_manifest *m, rhizome_bk_t *bsk);
|
2012-10-09 16:37:49 +00:00
|
|
|
int rhizome_sign_hash_with_key(rhizome_manifest *m,const unsigned char *sk,
|
|
|
|
const unsigned char *pk,rhizome_signature *out);
|
|
|
|
int rhizome_verify_bundle_privatekey(rhizome_manifest *m, const unsigned char *sk,
|
|
|
|
const unsigned char *pk);
|
2012-10-11 07:28:24 +00:00
|
|
|
int rhizome_find_bundle_author(rhizome_manifest *m);
|
2013-02-12 00:04:04 +00:00
|
|
|
int rhizome_queue_ignore_manifest(unsigned char *bid_prefix, int prefix_len, int timeout);
|
|
|
|
int rhizome_ignore_manifest_check(unsigned char *bid_prefix, int prefix_len);
|
2012-05-22 05:32:28 +00:00
|
|
|
|
|
|
|
/* one manifest is required per candidate, plus a few spare.
|
|
|
|
so MAX_RHIZOME_MANIFESTS must be > MAX_CANDIDATES.
|
|
|
|
*/
|
2013-04-11 05:54:13 +00:00
|
|
|
#define MAX_RHIZOME_MANIFESTS 40
|
|
|
|
#define MAX_CANDIDATES 32
|
2012-05-22 06:06:21 +00:00
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
int rhizome_suggest_queue_manifest_import(rhizome_manifest *m, const struct sockaddr_in *peerip, const sid_t *peersidp);
|
2013-09-13 04:32:35 +00:00
|
|
|
rhizome_manifest * rhizome_fetch_search(const unsigned char *id, int prefix_length);
|
2012-08-28 05:02:12 +00:00
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
/* Rhizome file storage api */
|
2013-10-10 07:53:06 +00:00
|
|
|
struct rhizome_write_buffer
|
|
|
|
{
|
2013-07-17 06:17:35 +00:00
|
|
|
struct rhizome_write_buffer *_next;
|
|
|
|
int64_t offset;
|
|
|
|
int buffer_size;
|
|
|
|
int data_size;
|
|
|
|
unsigned char data[0];
|
|
|
|
};
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
struct rhizome_write
|
|
|
|
{
|
|
|
|
rhizome_filehash_t id;
|
2013-08-27 06:45:51 +00:00
|
|
|
uint64_t temp_id;
|
2013-02-20 03:59:08 +00:00
|
|
|
char id_known;
|
|
|
|
|
2013-07-22 05:34:02 +00:00
|
|
|
int64_t tail;
|
2013-02-20 03:59:08 +00:00
|
|
|
int64_t file_offset;
|
2013-07-18 07:34:12 +00:00
|
|
|
int64_t written_offset;
|
2013-02-20 03:59:08 +00:00
|
|
|
int64_t file_length;
|
2013-07-18 07:34:12 +00:00
|
|
|
struct rhizome_write_buffer *buffer_list;
|
|
|
|
int buffer_size;
|
2013-02-20 03:59:08 +00:00
|
|
|
|
|
|
|
int crypt;
|
|
|
|
unsigned char key[RHIZOME_CRYPT_KEY_BYTES];
|
|
|
|
unsigned char nonce[crypto_stream_xsalsa20_NONCEBYTES];
|
|
|
|
|
|
|
|
SHA512_CTX sha512_context;
|
|
|
|
int64_t blob_rowid;
|
|
|
|
int blob_fd;
|
2013-07-18 04:22:42 +00:00
|
|
|
sqlite3_blob *sql_blob;
|
2013-02-20 03:59:08 +00:00
|
|
|
};
|
|
|
|
|
2013-07-25 05:12:30 +00:00
|
|
|
struct rhizome_read_buffer{
|
|
|
|
uint64_t offset;
|
|
|
|
unsigned char data[RHIZOME_CRYPT_PAGE_SIZE];
|
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
struct rhizome_read
|
|
|
|
{
|
|
|
|
rhizome_filehash_t id;
|
2013-02-20 03:59:08 +00:00
|
|
|
|
|
|
|
int crypt;
|
|
|
|
unsigned char key[RHIZOME_CRYPT_KEY_BYTES];
|
|
|
|
unsigned char nonce[crypto_stream_xsalsa20_NONCEBYTES];
|
|
|
|
|
2013-08-21 06:15:18 +00:00
|
|
|
int64_t hash_offset;
|
2013-02-20 03:59:08 +00:00
|
|
|
SHA512_CTX sha512_context;
|
2013-08-21 06:15:18 +00:00
|
|
|
char invalid;
|
2013-02-20 03:59:08 +00:00
|
|
|
|
|
|
|
int64_t blob_rowid;
|
|
|
|
int blob_fd;
|
|
|
|
|
2013-07-22 05:34:02 +00:00
|
|
|
int64_t tail;
|
2013-02-20 03:59:08 +00:00
|
|
|
int64_t offset;
|
|
|
|
int64_t length;
|
|
|
|
};
|
|
|
|
|
2012-08-28 05:02:12 +00:00
|
|
|
typedef struct rhizome_http_request {
|
|
|
|
struct sched_ent alarm;
|
2013-07-13 05:17:06 +00:00
|
|
|
time_ms_t initiate_time; /* time connection was initiated */
|
2012-08-28 05:02:12 +00:00
|
|
|
|
2012-10-02 23:25:04 +00:00
|
|
|
struct sockaddr_in requestor;
|
|
|
|
|
2012-10-02 09:02:48 +00:00
|
|
|
/* identify request from others being run.
|
|
|
|
Monotonic counter feeds it. Only used for debugging when we write
|
|
|
|
post-<uuid>.log files for multi-part form requests. */
|
|
|
|
unsigned int uuid;
|
|
|
|
|
2012-08-28 05:02:12 +00:00
|
|
|
/* The HTTP request as currently received */
|
|
|
|
int request_length;
|
2013-08-15 06:17:39 +00:00
|
|
|
int header_length;
|
2012-10-04 08:08:33 +00:00
|
|
|
char request[1024];
|
2012-08-28 05:02:12 +00:00
|
|
|
|
|
|
|
/* Nature of the request */
|
|
|
|
int request_type;
|
2012-08-31 03:45:05 +00:00
|
|
|
/* All of the below are receiving data */
|
2012-08-28 05:02:12 +00:00
|
|
|
#define RHIZOME_HTTP_REQUEST_RECEIVING -1
|
2012-08-31 03:45:05 +00:00
|
|
|
#define RHIZOME_HTTP_REQUEST_RECEIVING_MULTIPART -2
|
2013-09-24 00:20:45 +00:00
|
|
|
|
|
|
|
// callback function to fill the response buffer
|
|
|
|
int (*generator)(struct rhizome_http_request *r);
|
2012-08-28 05:02:12 +00:00
|
|
|
|
|
|
|
/* Local buffer of data to be sent.
|
|
|
|
If a RHIZOME_HTTP_REQUEST_FROMBUFFER, then the buffer is sent, and when empty
|
|
|
|
the request is closed.
|
|
|
|
Else emptying the buffer triggers a request to fetch more data. Only if no
|
|
|
|
more data is provided do we then close the request. */
|
|
|
|
unsigned char *buffer;
|
|
|
|
int buffer_size; // size
|
|
|
|
int buffer_length; // number of bytes loaded into buffer
|
|
|
|
int buffer_offset; // where we are between [0,buffer_length)
|
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
struct rhizome_read read_state;
|
|
|
|
|
2012-08-31 03:45:05 +00:00
|
|
|
/* Path of request (used by POST multipart form requests where
|
|
|
|
the actual processing of the request does not occur while the
|
|
|
|
request headers are still available. */
|
|
|
|
char path[1024];
|
2012-09-01 04:41:23 +00:00
|
|
|
/* Boundary string for POST multipart form requests */
|
2012-09-05 19:45:25 +00:00
|
|
|
char boundary_string[1024];
|
|
|
|
int boundary_string_length;
|
2012-09-05 20:59:05 +00:00
|
|
|
/* File currently being written to while decoding POST multipart form */
|
|
|
|
FILE *field_file;
|
2012-10-02 23:25:04 +00:00
|
|
|
/* Name of data file supplied */
|
|
|
|
char data_file_name[1024];
|
2012-09-05 20:59:05 +00:00
|
|
|
/* Which fields have been seen in POST multipart form */
|
|
|
|
int fields_seen;
|
|
|
|
/* The seen fields bitmap above shares values with the actual Rhizome Direct
|
|
|
|
state machine. The state numbers (and thus bitmap values for the various
|
|
|
|
fields) are listed here.
|
|
|
|
|
|
|
|
To avoid confusion, we should not use single bit values for states that do
|
|
|
|
not correspond directly to a particular field.
|
|
|
|
Doesn't really matter what they are apart from not having exactly one bit set.
|
|
|
|
In fact, the only reason to not have exactly one bit set is so that we keep as
|
|
|
|
many bits available for field types as possible.
|
|
|
|
*/
|
|
|
|
#define RD_MIME_STATE_MANIFESTHEADERS (1<<0)
|
|
|
|
#define RD_MIME_STATE_DATAHEADERS (1<<1)
|
|
|
|
#define RD_MIME_STATE_INITIAL 0
|
|
|
|
#define RD_MIME_STATE_PARTHEADERS 0xffff0000
|
|
|
|
#define RD_MIME_STATE_BODY 0xffff0001
|
2012-08-31 03:45:05 +00:00
|
|
|
|
2012-08-28 05:02:12 +00:00
|
|
|
/* The source specification data which are used in different ways by different
|
|
|
|
request types */
|
|
|
|
char source[1024];
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t source_index;
|
|
|
|
int64_t source_count;
|
2012-08-28 05:02:12 +00:00
|
|
|
int source_record_size;
|
|
|
|
unsigned int source_flags;
|
|
|
|
|
2013-01-06 09:20:56 +00:00
|
|
|
const char *sql_table;
|
|
|
|
const char *sql_row;
|
|
|
|
int64_t rowid;
|
2012-08-28 05:02:12 +00:00
|
|
|
/* source_index used for offset in blob */
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t blob_end;
|
2012-08-28 05:02:12 +00:00
|
|
|
|
|
|
|
} rhizome_http_request;
|
|
|
|
|
2012-09-11 03:39:40 +00:00
|
|
|
struct http_response {
|
|
|
|
unsigned int result_code;
|
|
|
|
const char * content_type;
|
2013-08-15 06:17:39 +00:00
|
|
|
uint64_t content_start;
|
|
|
|
uint64_t content_end;
|
2013-07-13 05:17:06 +00:00
|
|
|
uint64_t content_length;
|
2012-09-11 03:39:40 +00:00
|
|
|
const char * body;
|
|
|
|
};
|
2012-11-30 11:56:31 +00:00
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
int rhizome_received_content(const unsigned char *bidprefix,uint64_t version,
|
2012-11-30 12:12:28 +00:00
|
|
|
uint64_t offset,int count,unsigned char *bytes,
|
|
|
|
int type);
|
2013-02-16 09:55:26 +00:00
|
|
|
int64_t rhizome_database_create_blob_for(const char *filehashhex_or_tempid,
|
|
|
|
int64_t fileLength,int priority);
|
2012-09-11 05:04:58 +00:00
|
|
|
int rhizome_server_set_response(rhizome_http_request *r, const struct http_response *h);
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_server_free_http_request(rhizome_http_request *r);
|
|
|
|
int rhizome_server_http_send_bytes(rhizome_http_request *r);
|
|
|
|
int rhizome_server_parse_http_request(rhizome_http_request *r);
|
|
|
|
int rhizome_server_simple_http_response(rhizome_http_request *r, int result, const char *response);
|
2013-09-24 00:20:45 +00:00
|
|
|
int rhizome_server_http_response(rhizome_http_request *r, int result,
|
|
|
|
const char *mime_type, const char *body, uint64_t bytes);
|
2013-07-13 05:17:06 +00:00
|
|
|
int rhizome_server_http_response_header(rhizome_http_request *r, int result, const char *mime_type, uint64_t bytes);
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_http_server_start(int (*http_parse_func)(rhizome_http_request *),
|
|
|
|
const char *http_parse_func_description,
|
2013-10-07 13:07:37 +00:00
|
|
|
uint16_t port_low, uint16_t port_high);
|
2012-09-08 10:56:18 +00:00
|
|
|
|
2012-11-30 04:17:27 +00:00
|
|
|
int is_rhizome_enabled();
|
|
|
|
int is_rhizome_mdp_enabled();
|
|
|
|
int is_rhizome_http_enabled();
|
|
|
|
int is_rhizome_advertise_enabled();
|
|
|
|
int is_rhizome_mdp_server_running();
|
|
|
|
int is_rhizome_http_server_running();
|
|
|
|
|
2012-09-08 10:56:18 +00:00
|
|
|
typedef struct rhizome_direct_bundle_cursor {
|
2012-09-08 23:00:38 +00:00
|
|
|
/* Where the current fill started */
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t start_size_high;
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t start_bid_low;
|
2012-09-08 23:00:38 +00:00
|
|
|
|
|
|
|
/* Limit of where this cursor may traverse */
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t limit_size_high;
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t limit_bid_high;
|
2012-09-08 23:00:38 +00:00
|
|
|
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t size_low;
|
|
|
|
int64_t size_high;
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t bid_low;
|
|
|
|
rhizome_bid_t bid_high;
|
2012-09-08 10:56:18 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
int buffer_size;
|
|
|
|
int buffer_used;
|
2012-09-08 23:28:47 +00:00
|
|
|
int buffer_offset_bytes;
|
2012-09-08 10:56:18 +00:00
|
|
|
} rhizome_direct_bundle_cursor;
|
|
|
|
|
|
|
|
rhizome_direct_bundle_cursor *rhizome_direct_bundle_iterator(int buffer_size);
|
2012-09-08 23:00:38 +00:00
|
|
|
void rhizome_direct_bundle_iterator_unlimit(rhizome_direct_bundle_cursor *r);
|
|
|
|
int rhizome_direct_bundle_iterator_pickle_range(rhizome_direct_bundle_cursor *r,
|
|
|
|
unsigned char *pickled,
|
|
|
|
int pickle_buffer_size);
|
2012-09-22 05:51:02 +00:00
|
|
|
rhizome_manifest *rhizome_direct_get_manifest(unsigned char *bid_prefix,int prefix_length);
|
2012-09-08 23:00:38 +00:00
|
|
|
int rhizome_direct_bundle_iterator_unpickle_range(rhizome_direct_bundle_cursor *r,
|
|
|
|
const unsigned char *pickled,
|
|
|
|
int pickle_buffer_size);
|
2012-09-08 10:56:18 +00:00
|
|
|
int rhizome_direct_bundle_iterator_fill(rhizome_direct_bundle_cursor *c,
|
|
|
|
int max_bars);
|
|
|
|
void rhizome_direct_bundle_iterator_free(rhizome_direct_bundle_cursor **c);
|
2013-10-03 13:46:45 +00:00
|
|
|
int rhizome_direct_get_bars(const rhizome_bid_t *bid_low,
|
|
|
|
rhizome_bid_t *bid_high,
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t size_low, int64_t size_high,
|
2013-10-03 13:46:45 +00:00
|
|
|
const rhizome_bid_t *bid_max,
|
2012-09-07 22:25:57 +00:00
|
|
|
unsigned char *bars_out,
|
|
|
|
int bars_requested);
|
2012-09-08 10:56:18 +00:00
|
|
|
int rhizome_direct_process_post_multipart_bytes
|
|
|
|
(rhizome_http_request *r,const char *bytes,int count);
|
2012-08-28 05:10:17 +00:00
|
|
|
|
2012-09-09 23:46:27 +00:00
|
|
|
typedef struct rhizome_direct_sync_request {
|
|
|
|
struct sched_ent alarm;
|
|
|
|
rhizome_direct_bundle_cursor *cursor;
|
|
|
|
|
|
|
|
int pushP;
|
|
|
|
int pullP;
|
|
|
|
|
|
|
|
/* Sync interval in seconds. zero = sync only once */
|
|
|
|
int interval;
|
|
|
|
|
|
|
|
/* The dispatch function will be called each time a sync request can
|
|
|
|
be sent off, i.e., one cursor->buffer full of data.
|
|
|
|
Will differ based on underlying transport. HTTP is the initial
|
|
|
|
supported transport, but deLorme inReach will likely follow soon after.
|
|
|
|
*/
|
|
|
|
void (*dispatch_function)(struct rhizome_direct_sync_request *);
|
|
|
|
|
|
|
|
/* General purpose pointer for transport-dependent state */
|
|
|
|
void *transport_specific_state;
|
|
|
|
|
|
|
|
/* Statistics.
|
|
|
|
Each sync will consist of one or more "fills" of the cursor buffer, which
|
|
|
|
will then be dispatched by the transport-specific dispatch function.
|
|
|
|
Each of those dispatches may then result in zero or
|
|
|
|
*/
|
|
|
|
int syncs_started;
|
|
|
|
int syncs_completed;
|
|
|
|
int fills_sent;
|
|
|
|
int fill_responses_processed;
|
|
|
|
int bundles_pushed;
|
|
|
|
int bundles_pulled;
|
|
|
|
int bundle_transfers_in_progress;
|
|
|
|
|
|
|
|
} rhizome_direct_sync_request;
|
|
|
|
|
|
|
|
#define RHIZOME_DIRECT_MAX_SYNC_HANDLES 16
|
|
|
|
extern rhizome_direct_sync_request *rd_sync_handles[RHIZOME_DIRECT_MAX_SYNC_HANDLES];
|
|
|
|
extern int rd_sync_handle_count;
|
|
|
|
|
|
|
|
rhizome_direct_sync_request
|
|
|
|
*rhizome_direct_new_sync_request(
|
|
|
|
void (*transport_specific_dispatch_function)
|
|
|
|
(struct rhizome_direct_sync_request *),
|
|
|
|
int buffer_size,int interval, int mode,
|
|
|
|
void *transport_specific_state);
|
|
|
|
int rhizome_direct_continue_sync_request(rhizome_direct_sync_request *r);
|
|
|
|
int rhizome_direct_conclude_sync_request(rhizome_direct_sync_request *r);
|
2012-09-11 03:39:40 +00:00
|
|
|
rhizome_direct_bundle_cursor *rhizome_direct_get_fill_response
|
2012-09-21 12:04:53 +00:00
|
|
|
(unsigned char *buffer,int size,int max_response_bytes);
|
2012-09-09 23:46:27 +00:00
|
|
|
|
2012-09-10 10:52:14 +00:00
|
|
|
typedef struct rhizome_direct_transport_state_http {
|
|
|
|
int port;
|
|
|
|
char host[1024];
|
|
|
|
} rhizome_direct_transport_state_http;
|
|
|
|
|
2012-09-09 23:46:27 +00:00
|
|
|
void rhizome_direct_http_dispatch(rhizome_direct_sync_request *);
|
|
|
|
|
2012-08-28 05:10:17 +00:00
|
|
|
extern unsigned char favicon_bytes[];
|
|
|
|
extern int favicon_len;
|
2012-10-02 10:53:44 +00:00
|
|
|
|
|
|
|
int rhizome_import_from_files(const char *manifestpath,const char *filepath);
|
2012-10-24 01:38:41 +00:00
|
|
|
|
|
|
|
enum rhizome_start_fetch_result {
|
|
|
|
STARTED = 0,
|
|
|
|
SAMEBUNDLE,
|
|
|
|
SAMEPAYLOAD,
|
|
|
|
SUPERSEDED,
|
|
|
|
OLDERBUNDLE,
|
|
|
|
NEWERBUNDLE,
|
|
|
|
IMPORTED,
|
|
|
|
SLOTBUSY
|
|
|
|
};
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
enum rhizome_start_fetch_result rhizome_fetch_request_manifest_by_prefix(const struct sockaddr_in *peerip, const sid_t *sidp, const unsigned char *prefix, size_t prefix_length);
|
2012-10-23 07:40:20 +00:00
|
|
|
int rhizome_any_fetch_active();
|
2012-11-12 05:51:38 +00:00
|
|
|
int rhizome_any_fetch_queued();
|
2013-04-11 05:54:13 +00:00
|
|
|
int rhizome_fetch_queue_bytes();
|
2013-07-11 03:02:06 +00:00
|
|
|
int rhizome_fetch_status_html(struct strbuf *b);
|
2013-04-11 05:54:13 +00:00
|
|
|
int rhizome_fetch_has_queue_space(unsigned char log2_size);
|
2012-10-04 08:08:33 +00:00
|
|
|
|
|
|
|
struct http_response_parts {
|
|
|
|
int code;
|
|
|
|
char *reason;
|
2013-08-15 07:30:43 +00:00
|
|
|
int64_t range_start;
|
2013-07-13 05:17:06 +00:00
|
|
|
int64_t content_length;
|
2012-10-04 08:08:33 +00:00
|
|
|
char *content_start;
|
|
|
|
};
|
|
|
|
|
|
|
|
int unpack_http_response(char *response, struct http_response_parts *parts);
|
2012-12-04 03:42:28 +00:00
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
/* rhizome storage methods */
|
2012-12-17 05:11:27 +00:00
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_exists(const rhizome_filehash_t *hashp);
|
|
|
|
int rhizome_open_write(struct rhizome_write *write, const rhizome_filehash_t *expectedHashp, int64_t file_length, int priority);
|
2013-07-17 06:17:35 +00:00
|
|
|
int rhizome_write_buffer(struct rhizome_write *write_state, unsigned char *buffer, int data_size);
|
|
|
|
int rhizome_random_write(struct rhizome_write *write_state, int64_t offset, unsigned char *buffer, int data_size);
|
2013-07-25 05:12:30 +00:00
|
|
|
int rhizome_write_open_manifest(struct rhizome_write *write, rhizome_manifest *m);
|
2012-12-18 00:21:12 +00:00
|
|
|
int rhizome_write_file(struct rhizome_write *write, const char *filename);
|
2012-12-17 05:11:27 +00:00
|
|
|
int rhizome_fail_write(struct rhizome_write *write);
|
|
|
|
int rhizome_finish_write(struct rhizome_write *write);
|
2012-12-19 05:46:49 +00:00
|
|
|
int rhizome_import_file(rhizome_manifest *m, const char *filepath);
|
2013-08-20 23:51:40 +00:00
|
|
|
int rhizome_import_buffer(rhizome_manifest *m, unsigned char *buffer, int length);
|
2012-12-20 04:48:59 +00:00
|
|
|
int rhizome_stat_file(rhizome_manifest *m, const char *filepath);
|
2012-12-19 05:46:49 +00:00
|
|
|
int rhizome_add_file(rhizome_manifest *m, const char *filepath);
|
2013-01-03 05:42:24 +00:00
|
|
|
int rhizome_derive_key(rhizome_manifest *m, rhizome_bk_t *bsk);
|
2013-07-05 06:54:50 +00:00
|
|
|
|
|
|
|
int rhizome_open_write_journal(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, uint64_t new_size);
|
|
|
|
int rhizome_append_journal_buffer(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, unsigned char *buffer, int len);
|
|
|
|
int rhizome_append_journal_file(rhizome_manifest *m, rhizome_bk_t *bsk, uint64_t advance_by, const char *filename);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_journal_pipe(struct rhizome_write *write, const rhizome_filehash_t *hashp, uint64_t start_offset, uint64_t length);
|
2013-07-05 06:54:50 +00:00
|
|
|
|
2012-12-21 00:23:47 +00:00
|
|
|
int rhizome_crypt_xor_block(unsigned char *buffer, int buffer_size, int64_t stream_offset,
|
2013-01-02 04:35:22 +00:00
|
|
|
const unsigned char *key, const unsigned char *nonce);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_open_read(struct rhizome_read *read, const rhizome_filehash_t *hashp);
|
2012-12-28 01:04:22 +00:00
|
|
|
int rhizome_read(struct rhizome_read *read, unsigned char *buffer, int buffer_length);
|
2013-07-25 05:12:30 +00:00
|
|
|
int rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, int len);
|
2013-02-20 03:59:08 +00:00
|
|
|
int rhizome_read_close(struct rhizome_read *read);
|
2013-08-21 06:15:18 +00:00
|
|
|
int rhizome_open_decrypt_read(rhizome_manifest *m, rhizome_bk_t *bsk, struct rhizome_read *read_state);
|
2013-01-03 05:42:24 +00:00
|
|
|
int rhizome_extract_file(rhizome_manifest *m, const char *filepath, rhizome_bk_t *bsk);
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_dump_file(const rhizome_filehash_t *hashp, const char *filepath, int64_t *length);
|
2013-10-03 13:46:45 +00:00
|
|
|
int rhizome_read_cached(const rhizome_bid_t *bid, uint64_t version, time_ms_t timeout,
|
2013-08-16 05:27:28 +00:00
|
|
|
uint64_t fileOffset, unsigned char *buffer, int length);
|
|
|
|
int rhizome_cache_close();
|
2013-02-20 03:59:08 +00:00
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_database_filehash_from_id(const rhizome_bid_t *bidp, uint64_t version, rhizome_filehash_t *hashp);
|
2013-06-18 03:57:26 +00:00
|
|
|
|
|
|
|
int overlay_mdp_service_rhizome_sync(struct overlay_frame *frame, overlay_mdp_frame *mdp);
|
|
|
|
int rhizome_sync_announce();
|
|
|
|
int rhizome_sync_bundle_inserted(const unsigned char *bar);
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
#endif //__SERVALDNA__RHIZOME_H
|