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-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2011-12-18 21:40:02 +00:00
|
|
|
#include "rhizome.h"
|
2012-01-03 06:05:02 +00:00
|
|
|
#include <stdlib.h>
|
2011-12-13 09:04:12 +00:00
|
|
|
|
2012-07-11 07:20:50 +00:00
|
|
|
static int rhizome_enabled_flag = -1; // unknown
|
|
|
|
int rhizome_fetch_interval_ms = -1;
|
|
|
|
|
|
|
|
/* Configure rhizome.
|
|
|
|
@author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int rhizome_configure()
|
|
|
|
{
|
|
|
|
rhizome_enabled_flag = confValueGetBoolean("rhizome.enable", 1);
|
|
|
|
rhizome_fetch_interval_ms = (int) confValueGetInt64Range("rhizome.fetch_interval_ms", 3000, 1, 3600000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_enabled()
|
|
|
|
{
|
|
|
|
if (rhizome_enabled_flag < 0)
|
|
|
|
rhizome_configure();
|
|
|
|
return rhizome_enabled_flag;
|
|
|
|
}
|
|
|
|
|
2012-04-11 09:10:10 +00:00
|
|
|
/* Import a bundle from the inbox folder. The bundle is contained a pair of files, one containing
|
|
|
|
the manifest and the optional other containing the payload.
|
|
|
|
|
|
|
|
The logic is all in rhizome_add_manifest(). This function just wraps that function and manages
|
|
|
|
file and object buffers and lifetimes.
|
2011-12-13 09:04:12 +00:00
|
|
|
*/
|
2012-04-02 08:12:40 +00:00
|
|
|
|
2012-05-22 07:25:59 +00:00
|
|
|
int rhizome_bundle_import(rhizome_manifest *m_in, rhizome_manifest **m_out,
|
2012-05-25 15:20:48 +00:00
|
|
|
const char *bundle, int ttl)
|
2011-12-13 09:04:12 +00:00
|
|
|
{
|
2012-07-13 01:36:10 +00:00
|
|
|
if (debug & DEBUG_RHIZOME)
|
2012-05-25 15:20:48 +00:00
|
|
|
DEBUGF("rhizome_bundle_import(m_in=%p, m_out=%p, bundle=%s, ttl=%d)",
|
|
|
|
m_in, m_out, bundle ? bundle : "(null)", ttl);
|
2012-04-13 08:17:20 +00:00
|
|
|
if (m_out) *m_out = NULL;
|
|
|
|
|
2011-12-13 09:04:12 +00:00
|
|
|
char filename[1024];
|
|
|
|
char manifestname[1024];
|
2012-05-22 00:30:26 +00:00
|
|
|
|
|
|
|
/* make sure import path exists */
|
2012-05-24 03:30:31 +00:00
|
|
|
if (create_rhizome_import_dir() == -1)
|
2012-05-24 01:58:32 +00:00
|
|
|
return -1;
|
2012-05-22 00:30:26 +00:00
|
|
|
|
2012-05-24 01:58:32 +00:00
|
|
|
if (!FORM_RHIZOME_IMPORT_PATH(filename, "file.%s", bundle)
|
|
|
|
|| !FORM_RHIZOME_IMPORT_PATH(manifestname, "manifest.%s", bundle))
|
2012-04-02 08:12:40 +00:00
|
|
|
return WHY("Manifest bundle name too long");
|
2012-05-21 13:01:35 +00:00
|
|
|
|
2012-04-02 08:12:40 +00:00
|
|
|
/* Read manifest file if no manifest was given */
|
|
|
|
rhizome_manifest *m = m_in;
|
2012-05-25 06:08:13 +00:00
|
|
|
if (!m_in) {
|
|
|
|
m = rhizome_new_manifest();
|
2012-04-02 08:12:40 +00:00
|
|
|
if (!m)
|
2012-05-25 06:08:13 +00:00
|
|
|
return WHY("Out of manifests.");
|
2012-05-25 23:54:47 +00:00
|
|
|
if (rhizome_read_manifest_file(m, manifestname, 0 /* file not buffer */) == -1) {
|
2012-05-25 06:08:13 +00:00
|
|
|
rhizome_manifest_free(m);
|
2012-04-02 08:12:40 +00:00
|
|
|
return WHY("Could not read manifest file.");
|
2012-05-25 23:54:47 +00:00
|
|
|
} else if (rhizome_manifest_verify(m)) {
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
return WHY("Could not verify manifest file.");
|
2012-05-25 06:08:13 +00:00
|
|
|
}
|
2012-04-02 08:12:40 +00:00
|
|
|
}
|
2011-12-13 09:04:12 +00:00
|
|
|
|
2012-04-02 08:12:40 +00:00
|
|
|
/* Add the manifest and its associated file to the Rhizome database. */
|
2012-07-16 09:01:00 +00:00
|
|
|
m->dataFileName = strdup(filename);
|
2012-05-25 15:20:48 +00:00
|
|
|
if (rhizome_manifest_check_file(m))
|
|
|
|
return WHY("File does not belong to manifest");
|
|
|
|
int ret=rhizome_manifest_check_duplicate(m,NULL);
|
|
|
|
if (!ret) rhizome_add_manifest(m, ttl);
|
2012-04-02 08:12:40 +00:00
|
|
|
unlink(filename);
|
|
|
|
if (ret == -1) {
|
2012-05-22 00:30:26 +00:00
|
|
|
WHY("rhizome_add_manifest() failed");
|
2012-04-02 08:12:40 +00:00
|
|
|
unlink(manifestname);
|
|
|
|
} else {
|
|
|
|
/* >>> For testing, write manifest file back to disk and leave it there */
|
|
|
|
// unlink(manifestname);
|
|
|
|
if (rhizome_write_manifest_file(m, manifestname))
|
|
|
|
ret = WHY("Could not write manifest file.");
|
|
|
|
}
|
2012-01-27 05:51:48 +00:00
|
|
|
|
2012-04-13 08:17:20 +00:00
|
|
|
/* If the manifest structure was allocated in this function, and it is not being returned to the
|
|
|
|
caller, then this function is responsible for freeing it */
|
|
|
|
if (m_out)
|
|
|
|
*m_out = m;
|
|
|
|
else if (!m_in)
|
2012-04-02 08:12:40 +00:00
|
|
|
rhizome_manifest_free(m);
|
2011-12-13 09:04:12 +00:00
|
|
|
|
2012-04-02 08:12:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-25 23:06:29 +00:00
|
|
|
int rhizome_manifest_check_sanity(rhizome_manifest *m_in)
|
2012-04-02 08:12:40 +00:00
|
|
|
{
|
2012-04-12 09:00:52 +00:00
|
|
|
/* Ensure manifest meets basic sanity checks. */
|
2012-05-17 06:39:57 +00:00
|
|
|
const char *service = rhizome_manifest_get(m_in, "service", NULL, 0);
|
2012-05-25 15:20:48 +00:00
|
|
|
const char *sender = rhizome_manifest_get(m_in, "sender", NULL, 0);
|
|
|
|
const char *recipient = rhizome_manifest_get(m_in, "recipient", NULL, 0);
|
|
|
|
|
2012-05-17 06:39:57 +00:00
|
|
|
if (service == NULL || !service[0])
|
|
|
|
return WHY("Manifest missing 'service' field");
|
2012-04-13 08:17:20 +00:00
|
|
|
if (rhizome_manifest_get_ll(m_in, "date") == -1)
|
2012-04-12 09:00:52 +00:00
|
|
|
return WHY("Manifest missing 'date' field");
|
2012-05-20 06:37:22 +00:00
|
|
|
if (strcasecmp(service, RHIZOME_SERVICE_FILE) == 0) {
|
2012-05-19 04:39:50 +00:00
|
|
|
const char *name = rhizome_manifest_get(m_in, "name", NULL, 0);
|
2012-06-05 06:15:53 +00:00
|
|
|
if (name == NULL)
|
2012-05-21 01:07:24 +00:00
|
|
|
return WHY("Manifest missing 'name' field");
|
2012-05-20 06:37:22 +00:00
|
|
|
} else if (strcasecmp(service, RHIZOME_SERVICE_MESHMS) == 0) {
|
2012-05-20 14:39:14 +00:00
|
|
|
if (sender == NULL || !sender[0])
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("MeshMS Manifest missing 'sender' field");
|
2012-07-12 06:44:25 +00:00
|
|
|
if (!str_is_subscriber_id(sender))
|
|
|
|
return WHYF("MeshMS Manifest contains invalid 'sender' field: %s", sender);
|
2012-05-20 14:39:14 +00:00
|
|
|
if (recipient == NULL || !recipient[0])
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("MeshMS Manifest missing 'recipient' field");
|
2012-07-12 06:44:25 +00:00
|
|
|
if (!str_is_subscriber_id(recipient))
|
|
|
|
return WHYF("MeshMS Manifest contains invalid 'recipient' field: %s", recipient);
|
2012-05-25 23:06:29 +00:00
|
|
|
} else {
|
|
|
|
return WHY("Invalid service type");
|
2012-05-25 15:20:48 +00:00
|
|
|
}
|
2012-07-12 06:44:25 +00:00
|
|
|
if (debug & DEBUG_RHIZOME)
|
|
|
|
DEBUGF("sender='%s'", sender ? sender : "(null)");
|
2012-05-25 15:20:48 +00:00
|
|
|
|
|
|
|
/* passes all sanity checks */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
A bundle can either be an ordinary manifest-payload pair, or a group description.
|
|
|
|
|
|
|
|
- Group descriptions are manifests with no payload that have the "isagroup" variable set. They
|
|
|
|
get stored in the manifests table AND a reference is added to the grouplist table. Any
|
|
|
|
manifest, including any group manifest, may be a member of zero or one group. This allows a
|
|
|
|
nested, i.e., multi-level group hierarchy where sub-groups will only typically be discovered
|
|
|
|
by joining the parent group.
|
|
|
|
*/
|
|
|
|
|
2012-06-08 05:57:15 +00:00
|
|
|
int rhizome_manifest_bind_id(rhizome_manifest *m_in, const unsigned char *authorSid)
|
2012-05-25 15:20:48 +00:00
|
|
|
{
|
|
|
|
rhizome_manifest_createid(m_in);
|
|
|
|
/* The ID is implicit in transit, but we need to store it in the file, so that reimporting
|
|
|
|
manifests on receiver nodes works easily. We might implement something that strips the id
|
|
|
|
variable out of the manifest when sending it, or some other scheme to avoid sending all the
|
|
|
|
extra bytes. */
|
2012-06-08 05:57:15 +00:00
|
|
|
char id[RHIZOME_MANIFEST_ID_STRLEN + 1];
|
|
|
|
rhizome_bytes_to_hex_upper(m_in->cryptoSignPublic, id, RHIZOME_MANIFEST_ID_BYTES);
|
2012-05-25 15:20:48 +00:00
|
|
|
rhizome_manifest_set(m_in, "id", id);
|
2012-06-08 05:57:15 +00:00
|
|
|
if (authorSid) {
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Set the BK using the provided authorship information.
|
|
|
|
Serval Security Framework defines BK as being:
|
|
|
|
BK = privateKey XOR sha512(RS##BID), where BID = cryptoSignPublic,
|
|
|
|
and RS is the rhizome secret for the specified author.
|
|
|
|
The nice thing about this specification is that:
|
|
|
|
privateKey = BK XOR sha512(RS##BID), so the same function can be used
|
|
|
|
to encrypt and decrypt the BK field. */
|
|
|
|
unsigned char bkbytes[RHIZOME_BUNDLE_KEY_BYTES];
|
2012-06-08 05:57:15 +00:00
|
|
|
if (rhizome_bk_xor(authorSid, m_in->cryptoSignPublic, m_in->cryptoSignSecret, bkbytes) == 0) {
|
2012-05-25 15:20:48 +00:00
|
|
|
char bkhex[RHIZOME_BUNDLE_KEY_STRLEN + 1];
|
2012-06-08 05:57:15 +00:00
|
|
|
(void) tohex(bkhex, bkbytes, RHIZOME_BUNDLE_KEY_BYTES);
|
2012-05-25 15:20:48 +00:00
|
|
|
if (debug&DEBUG_RHIZOME) DEBUGF("set BK=%s", bkhex);
|
|
|
|
rhizome_manifest_set(m_in, "BK", bkhex);
|
|
|
|
} else {
|
|
|
|
return WHY("Failed to set BK");
|
|
|
|
}
|
2012-05-19 04:39:50 +00:00
|
|
|
}
|
2012-05-25 15:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-12 09:00:52 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_manifest_bind_file(rhizome_manifest *m_in,const char *filename,int encryptP)
|
|
|
|
{
|
2012-04-11 09:10:10 +00:00
|
|
|
/* Keep payload file name handy for later */
|
2012-04-13 08:17:20 +00:00
|
|
|
m_in->dataFileName = strdup(filename);
|
2012-04-02 08:12:40 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Keep note as to whether we are supposed to be encrypting this file or not */
|
|
|
|
m_in->payloadEncryption=encryptP;
|
|
|
|
if (encryptP) rhizome_manifest_set_ll(m_in,"crypt",1);
|
|
|
|
else rhizome_manifest_set_ll(m_in,"crypt",0);
|
|
|
|
|
2012-06-05 06:15:53 +00:00
|
|
|
/* Get length of payload. An empty filename means empty payload. */
|
|
|
|
if (filename[0]) {
|
|
|
|
struct stat stat;
|
|
|
|
if (lstat(filename,&stat))
|
|
|
|
return WHYF("Could not stat() payload file '%s'",filename);
|
|
|
|
m_in->fileLength = stat.st_size;
|
|
|
|
} else
|
|
|
|
m_in->fileLength = 0;
|
2012-05-25 15:20:48 +00:00
|
|
|
if (debug & DEBUG_RHIZOME)
|
|
|
|
DEBUGF("filename=%s, fileLength=%lld", filename, m_in->fileLength);
|
|
|
|
rhizome_manifest_set_ll(m_in,"filesize",m_in->fileLength);
|
|
|
|
|
2012-07-16 09:01:00 +00:00
|
|
|
/* Compute hash of non-empty payload */
|
|
|
|
if (m_in->fileLength != 0) {
|
|
|
|
char hexhashbuf[RHIZOME_FILEHASH_STRLEN + 1];
|
|
|
|
if (rhizome_hash_file(m_in,filename, hexhashbuf))
|
|
|
|
return WHY("Could not hash file.");
|
|
|
|
memcpy(&m_in->fileHexHash[0], &hexhashbuf[0], sizeof hexhashbuf);
|
|
|
|
rhizome_manifest_set(m_in, "filehash", m_in->fileHexHash);
|
|
|
|
m_in->fileHashedP = 1;
|
|
|
|
} else {
|
|
|
|
m_in->fileHexHash[0] = '\0';
|
|
|
|
rhizome_manifest_del(m_in, "filehash");
|
|
|
|
m_in->fileHashedP = 0;
|
|
|
|
}
|
2012-05-25 15:20:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-02 08:12:40 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
int rhizome_manifest_check_file(rhizome_manifest *m_in)
|
|
|
|
{
|
2012-06-08 03:43:26 +00:00
|
|
|
long long gotfile = 0;
|
|
|
|
if (sqlite_exec_int64(&gotfile, "SELECT COUNT(*) FROM FILES WHERE ID='%s' and datavalid=1;", m_in->fileHexHash) != 1) {
|
|
|
|
WHYF("Failed to count files");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (gotfile) {
|
|
|
|
DEBUGF("Skipping file checks for bundle, as file is already in the database");
|
2012-05-27 15:29:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Find out whether the payload is expected to be encrypted or not */
|
|
|
|
m_in->payloadEncryption=rhizome_manifest_get_ll(m_in, "crypt");
|
|
|
|
|
2012-04-11 09:10:10 +00:00
|
|
|
/* Check payload file is accessible and discover its length, then check that it matches
|
|
|
|
the file size stored in the manifest */
|
2012-07-16 09:01:00 +00:00
|
|
|
long long mfilesize = rhizome_manifest_get_ll(m_in, "filesize");
|
|
|
|
m_in->fileLength = 0;
|
2012-06-05 06:15:53 +00:00
|
|
|
if (m_in->dataFileName[0]) {
|
|
|
|
struct stat stat;
|
2012-07-16 09:01:00 +00:00
|
|
|
if (lstat(m_in->dataFileName,&stat) == -1) {
|
|
|
|
if (errno != ENOENT || mfilesize != 0)
|
|
|
|
return WHYF_perror("stat(%s)", m_in->dataFileName);
|
|
|
|
} else {
|
|
|
|
m_in->fileLength = stat.st_size;
|
|
|
|
}
|
|
|
|
}
|
2012-05-25 15:20:48 +00:00
|
|
|
if (debug & DEBUG_RHIZOME)
|
|
|
|
DEBUGF("filename=%s, fileLength=%lld", m_in->dataFileName, m_in->fileLength);
|
|
|
|
if (mfilesize != -1 && mfilesize != m_in->fileLength) {
|
|
|
|
WHYF("Manifest.filesize (%lld) != actual file size (%lld)", mfilesize, m_in->fileLength);
|
|
|
|
return -1;
|
2011-12-13 09:04:12 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 09:01:00 +00:00
|
|
|
/* If payload is empty, ensure manifest has not file hash, otherwis compute the hash of the
|
|
|
|
payload and check that it matches manifest. */
|
2012-05-25 15:20:48 +00:00
|
|
|
const char *mhexhash = rhizome_manifest_get(m_in, "filehash", NULL, 0);
|
2012-07-16 09:01:00 +00:00
|
|
|
if (m_in->fileLength != 0) {
|
|
|
|
char hexhashbuf[RHIZOME_FILEHASH_STRLEN + 1];
|
|
|
|
if (rhizome_hash_file(m_in,m_in->dataFileName, hexhashbuf))
|
|
|
|
return WHY("Could not hash file.");
|
|
|
|
memcpy(&m_in->fileHexHash[0], &hexhashbuf[0], sizeof hexhashbuf);
|
|
|
|
m_in->fileHashedP = 1;
|
|
|
|
if (!mhexhash) return WHY("manifest contains no file hash");
|
|
|
|
if (mhexhash && strcmp(m_in->fileHexHash, mhexhash)) {
|
|
|
|
WHYF("Manifest.filehash (%s) does not match payload hash (%s)", mhexhash, m_in->fileHexHash);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mhexhash != NULL) {
|
|
|
|
WHYF("Manifest.filehash (%s) should be absent for empty payload", mhexhash);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-13 09:04:12 +00:00
|
|
|
}
|
2012-04-11 09:10:10 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-16 02:16:58 +00:00
|
|
|
|
2012-05-25 15:20:48 +00:00
|
|
|
/* Check if a manifest is already stored for the same payload with the same details.
|
|
|
|
This catches the case of "dna rhizome add file <filename>" on the same file more than once.
|
|
|
|
(Debounce!) */
|
|
|
|
int rhizome_manifest_check_duplicate(rhizome_manifest *m_in,rhizome_manifest **m_out)
|
|
|
|
{
|
2012-06-08 03:43:26 +00:00
|
|
|
if (debug & DEBUG_RHIZOME) DEBUG("Checking for duplicate");
|
2012-05-25 15:20:48 +00:00
|
|
|
if (m_out) *m_out = NULL;
|
2012-04-12 09:00:52 +00:00
|
|
|
rhizome_manifest *dupm = NULL;
|
2012-05-25 23:02:17 +00:00
|
|
|
if (rhizome_find_duplicate(m_in, &dupm,0 /* version doesn't matter */) == -1)
|
2012-04-12 09:00:52 +00:00
|
|
|
return WHY("Errors encountered searching for duplicate manifest");
|
|
|
|
if (dupm) {
|
2012-04-16 02:16:58 +00:00
|
|
|
/* If the caller wants the duplicate manifest, it must be finalised, otherwise discarded. */
|
2012-04-13 08:17:20 +00:00
|
|
|
if (m_out) {
|
|
|
|
*m_out = dupm;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rhizome_manifest_free(dupm);
|
2012-06-08 03:43:26 +00:00
|
|
|
if (debug & DEBUG_RHIZOME) DEBUG("Found a duplicate");
|
2012-04-16 02:16:58 +00:00
|
|
|
return 2;
|
2012-04-12 09:00:52 +00:00
|
|
|
}
|
2012-06-08 03:43:26 +00:00
|
|
|
if (debug & DEBUG_RHIZOME) DEBUG("No duplicate found");
|
2012-05-25 15:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_add_manifest(rhizome_manifest *m_in,int ttl)
|
|
|
|
{
|
2012-07-13 01:36:10 +00:00
|
|
|
if (debug & DEBUG_RHIZOME)
|
2012-05-25 15:20:48 +00:00
|
|
|
DEBUGF("rhizome_add_manifest(m_in=%p, ttl=%d)",m_in, ttl);
|
|
|
|
|
|
|
|
if (m_in->finalised==0)
|
|
|
|
return WHY("Manifest must be finalised before being stored");
|
|
|
|
|
|
|
|
/* Store time to live, clamped to within legal range */
|
|
|
|
m_in->ttl = ttl < 0 ? 0 : ttl > 254 ? 254 : ttl;
|
|
|
|
|
2012-05-25 23:06:29 +00:00
|
|
|
if (rhizome_manifest_check_sanity(m_in))
|
2012-05-25 15:20:48 +00:00
|
|
|
return WHY("Sanity checks on manifest failed");
|
|
|
|
|
|
|
|
if (rhizome_manifest_check_file(m_in))
|
|
|
|
return WHY("File does not belong to this manifest");
|
|
|
|
|
|
|
|
/* Get manifest version number. */
|
|
|
|
m_in->version = rhizome_manifest_get_ll(m_in, "version");
|
|
|
|
if (m_in->version==-1)
|
|
|
|
return WHY("Manifest must have a version number");
|
2012-04-12 09:00:52 +00:00
|
|
|
|
2012-04-11 09:10:10 +00:00
|
|
|
/* Supply manifest version number if missing, so we can do the version check below */
|
2012-04-16 02:16:58 +00:00
|
|
|
if (m_in->version == -1) {
|
2012-05-11 01:08:46 +00:00
|
|
|
m_in->version = gettime_ms();
|
2012-04-16 02:16:58 +00:00
|
|
|
rhizome_manifest_set_ll(m_in, "version", m_in->version);
|
2012-04-11 09:10:10 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 08:17:20 +00:00
|
|
|
/* If the manifest already has an ID */
|
2012-05-23 06:34:00 +00:00
|
|
|
char id[SID_STRLEN + 1];
|
|
|
|
if (rhizome_manifest_get(m_in, "id", id, SID_STRLEN + 1)) {
|
|
|
|
str_toupper_inplace(id);
|
2012-04-16 02:16:58 +00:00
|
|
|
/* Discard the new manifest unless it is newer than the most recent known version with the same ID */
|
2012-06-08 03:43:26 +00:00
|
|
|
long long storedversion = -1;
|
|
|
|
switch (sqlite_exec_int64(&storedversion, "SELECT version from manifests where id='%s';", id)) {
|
|
|
|
case -1:
|
|
|
|
return WHY("Select failed");
|
|
|
|
case 0:
|
|
|
|
if (debug & DEBUG_RHIZOME) DEBUG("No existing manifest");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (debug & DEBUG_RHIZOME) DEBUGF("Found existing version=%lld, new version=%lld", storedversion, m_in->version);
|
|
|
|
if (m_in->version < storedversion)
|
|
|
|
return WHY("Newer version exists");
|
|
|
|
if (m_in->version == storedversion)
|
|
|
|
return WHY("Same version of manifest exists, not adding");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return WHY("Select found too many rows!");
|
2012-04-11 09:10:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-05-25 15:20:48 +00:00
|
|
|
/* no manifest ID */
|
|
|
|
return WHY("Manifest does not have an ID");
|
2011-12-20 00:55:52 +00:00
|
|
|
}
|
2011-12-13 09:04:12 +00:00
|
|
|
|
|
|
|
/* Okay, it is written, and can be put directly into the rhizome database now */
|
2012-05-25 15:20:48 +00:00
|
|
|
if (rhizome_store_bundle(m_in) == -1)
|
|
|
|
return WHY("rhizome_store_bundle() failed.");
|
2011-12-13 09:04:12 +00:00
|
|
|
|
2012-07-11 05:21:25 +00:00
|
|
|
// This message used in tests; do not modify or remove.
|
|
|
|
const char *service = rhizome_manifest_get(m_in, "service", NULL, 0);
|
|
|
|
INFOF("RHIZOME ADD MANIFEST service=%s bid=%s version=%lld",
|
|
|
|
service ? service : "NULL",
|
|
|
|
alloca_tohex_sid(m_in->cryptoSignPublic),
|
|
|
|
m_in->version
|
|
|
|
);
|
2012-05-11 21:54:52 +00:00
|
|
|
monitor_announce_bundle(m_in);
|
2012-04-02 08:12:40 +00:00
|
|
|
return 0;
|
2011-12-13 09:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update an existing Rhizome bundle */
|
|
|
|
int rhizome_bundle_push_update(char *id,long long version,unsigned char *data,int appendP)
|
|
|
|
{
|
|
|
|
return WHY("Not implemented");
|
|
|
|
}
|
|
|
|
|
2012-05-22 03:35:29 +00:00
|
|
|
/* When voice traffic is being carried, we need to throttle Rhizome down
|
|
|
|
to a more sensible level. Or possibly even supress it entirely.
|
|
|
|
*/
|
|
|
|
long long rhizome_voice_timeout=0;
|
|
|
|
int rhizome_saw_voice_traffic()
|
|
|
|
{
|
|
|
|
/* We are in "voice mode" for a second after sending a voice frame */
|
2012-07-30 07:52:38 +00:00
|
|
|
if (rhizome_voice_timeout<gettime_ms()) {
|
2012-05-22 04:45:35 +00:00
|
|
|
WHY("Suppressing Rhizome due to voice traffic");
|
2012-07-30 07:52:38 +00:00
|
|
|
rhizome_voice_timeout=gettime_ms()+1000;
|
2012-05-22 04:45:35 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 03:35:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|