Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
#include <assert.h>
|
2013-07-22 08:25:02 +00:00
|
|
|
#include "serval.h"
|
|
|
|
#include "rhizome.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "conf.h"
|
2013-07-25 05:19:06 +00:00
|
|
|
#include "crypto.h"
|
2013-07-26 05:06:11 +00:00
|
|
|
#include "strlcpy.h"
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
#define MESHMS_BLOCK_TYPE_ACK 0x01
|
|
|
|
#define MESHMS_BLOCK_TYPE_MESSAGE 0x02
|
|
|
|
#define MESHMS_BLOCK_TYPE_BID_REFERENCE 0x03
|
|
|
|
|
2013-07-26 05:06:11 +00:00
|
|
|
// the manifest details for one half of a conversation
|
2013-07-22 08:25:02 +00:00
|
|
|
struct ply{
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t bundle_id;
|
2013-07-22 08:25:02 +00:00
|
|
|
uint64_t version;
|
|
|
|
uint64_t tail;
|
|
|
|
uint64_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct conversations{
|
2013-07-26 05:06:11 +00:00
|
|
|
// binary tree
|
2013-07-22 08:25:02 +00:00
|
|
|
struct conversations *_left;
|
|
|
|
struct conversations *_right;
|
2013-07-26 05:06:11 +00:00
|
|
|
|
|
|
|
// who are we talking to?
|
2013-07-30 01:36:12 +00:00
|
|
|
sid_t them;
|
2013-07-26 05:06:11 +00:00
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
char found_my_ply;
|
|
|
|
struct ply my_ply;
|
2013-07-26 05:06:11 +00:00
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
char found_their_ply;
|
|
|
|
struct ply their_ply;
|
2013-07-25 05:19:06 +00:00
|
|
|
|
|
|
|
// what is the offset of their last message
|
|
|
|
uint64_t their_last_message;
|
|
|
|
// what is the last message we marked as read
|
|
|
|
uint64_t read_offset;
|
|
|
|
// our cached value for the last known size of their ply
|
|
|
|
uint64_t their_size;
|
2013-07-22 08:25:02 +00:00
|
|
|
};
|
|
|
|
|
2013-07-26 05:06:11 +00:00
|
|
|
// cursor state for reading one half of a conversation
|
2013-07-22 08:25:02 +00:00
|
|
|
struct ply_read{
|
2013-07-26 05:06:11 +00:00
|
|
|
// rhizome payload
|
2013-07-22 08:25:02 +00:00
|
|
|
struct rhizome_read read;
|
2013-07-26 05:06:11 +00:00
|
|
|
// block buffer
|
2013-07-25 05:19:06 +00:00
|
|
|
struct rhizome_read_buffer buff;
|
2013-07-26 05:06:11 +00:00
|
|
|
|
|
|
|
// details of the current record
|
2013-07-22 08:25:02 +00:00
|
|
|
uint64_t record_end_offset;
|
|
|
|
uint16_t record_length;
|
2013-10-17 12:47:41 +00:00
|
|
|
size_t buffer_size;
|
2013-07-23 04:51:46 +00:00
|
|
|
char type;
|
2013-07-26 05:06:11 +00:00
|
|
|
// raw record data
|
2013-07-22 08:25:02 +00:00
|
|
|
unsigned char *buffer;
|
|
|
|
};
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
static int meshms_conversations_list(const sid_t *my_sid, const sid_t *their_sid, struct conversations **conv);
|
2013-07-25 05:19:06 +00:00
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
static void free_conversations(struct conversations *conv){
|
|
|
|
if (!conv)
|
|
|
|
return;
|
|
|
|
free_conversations(conv->_left);
|
|
|
|
free_conversations(conv->_right);
|
|
|
|
free(conv);
|
|
|
|
}
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
static int get_my_conversation_bundle(const sid_t *my_sidp, rhizome_manifest *m)
|
2013-07-25 05:19:06 +00:00
|
|
|
{
|
|
|
|
/* Find our private key */
|
|
|
|
int cn=0,in=0,kp=0;
|
2013-10-09 08:24:21 +00:00
|
|
|
if (!keyring_find_sid(keyring,&cn,&in,&kp,my_sidp))
|
|
|
|
return WHYF("SID was not found in keyring: %s", alloca_tohex_sid_t(*my_sidp));
|
2013-07-25 05:19:06 +00:00
|
|
|
|
|
|
|
char seed[1024];
|
|
|
|
snprintf(seed, sizeof(seed),
|
|
|
|
"incorrection%sconcentrativeness",
|
|
|
|
alloca_tohex(keyring->contexts[cn]->identities[in]
|
|
|
|
->keypairs[kp]->private_key, crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES));
|
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
if (rhizome_get_bundle_from_seed(m, seed) == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
// always consider the content encrypted, we don't need to rely on the manifest itself.
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
rhizome_manifest_set_crypt(m, PAYLOAD_ENCRYPTED);
|
|
|
|
assert(m->haveSecret);
|
2013-10-11 05:09:10 +00:00
|
|
|
if (m->haveSecret == NEW_BUNDLE_ID) {
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
rhizome_manifest_set_service(m, RHIZOME_SERVICE_FILE);
|
2013-11-05 07:28:03 +00:00
|
|
|
if (rhizome_fill_manifest(m, NULL, my_sidp) == -1)
|
2013-10-11 05:09:10 +00:00
|
|
|
return WHY("Invalid manifest");
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (config.debug.meshms) {
|
|
|
|
char secret[RHIZOME_BUNDLE_KEY_STRLEN + 1];
|
|
|
|
rhizome_bytes_to_hex_upper(m->cryptoSignSecret, secret, RHIZOME_BUNDLE_KEY_BYTES);
|
|
|
|
// The 'meshms' automated test depends on this message; do not alter.
|
|
|
|
DEBUGF("MESHMS CONVERSATION BUNDLE bid=%s secret=%s",
|
|
|
|
alloca_tohex_rhizome_bid_t(m->cryptoSignPublic),
|
|
|
|
secret
|
|
|
|
);
|
|
|
|
}
|
2013-10-11 05:09:10 +00:00
|
|
|
} else {
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (strcmp(m->service, RHIZOME_SERVICE_FILE) != 0)
|
|
|
|
return WHYF("Invalid manifest, service=%s but should be %s", m->service, RHIZOME_SERVICE_MESHMS2);
|
2013-07-26 05:06:11 +00:00
|
|
|
}
|
2013-07-25 05:19:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-03 07:14:06 +00:00
|
|
|
static struct conversations *add_conv(struct conversations **conv, const sid_t *them)
|
|
|
|
{
|
|
|
|
struct conversations **ptr = conv;
|
2013-07-25 05:19:06 +00:00
|
|
|
while(*ptr){
|
2013-10-03 07:14:06 +00:00
|
|
|
int cmp = cmp_sid_t(&(*ptr)->them, them);
|
|
|
|
if (cmp == 0)
|
2013-07-25 05:19:06 +00:00
|
|
|
break;
|
2013-10-03 07:14:06 +00:00
|
|
|
if (cmp < 0)
|
2013-07-25 05:19:06 +00:00
|
|
|
ptr = &(*ptr)->_left;
|
|
|
|
else
|
|
|
|
ptr = &(*ptr)->_right;
|
|
|
|
}
|
|
|
|
if (!*ptr){
|
|
|
|
*ptr = emalloc_zero(sizeof(struct conversations));
|
|
|
|
if (*ptr)
|
2013-10-03 07:14:06 +00:00
|
|
|
(*ptr)->them = *them;
|
2013-07-25 05:19:06 +00:00
|
|
|
}
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
// find matching conversations
|
2013-10-03 07:14:06 +00:00
|
|
|
// if their_sid == my_sid, return all conversations with any recipient
|
|
|
|
static int get_database_conversations(const sid_t *my_sid, const sid_t *their_sid, struct conversations **conv)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
2013-10-03 07:14:06 +00:00
|
|
|
sqlite3_stmt *statement = sqlite_prepare_bind(&retry,
|
|
|
|
"SELECT id, version, filesize, tail, sender, recipient"
|
|
|
|
" FROM manifests"
|
|
|
|
" WHERE service = ?3"
|
|
|
|
" AND (sender=?1 or recipient=?1)"
|
|
|
|
" AND (sender=?2 or recipient=?2)",
|
|
|
|
SID_T, my_sid,
|
|
|
|
SID_T, their_sid ? their_sid : my_sid,
|
|
|
|
STATIC_TEXT, RHIZOME_SERVICE_MESHMS2,
|
|
|
|
END
|
|
|
|
);
|
2013-07-22 08:25:02 +00:00
|
|
|
if (!statement)
|
|
|
|
return -1;
|
2013-10-03 07:14:06 +00:00
|
|
|
if (config.debug.meshms) {
|
|
|
|
const char *my_sid_hex = alloca_tohex_sid_t(*my_sid);
|
|
|
|
const char *their_sid_hex = alloca_tohex_sid_t(*(their_sid ? their_sid : my_sid));
|
2013-07-22 08:25:02 +00:00
|
|
|
DEBUGF("Looking for conversations for %s, %s", my_sid_hex, their_sid_hex);
|
2013-10-03 07:14:06 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) {
|
2013-10-03 13:46:45 +00:00
|
|
|
const char *id_hex = (const char *)sqlite3_column_text(statement, 0);
|
2013-10-06 19:24:46 +00:00
|
|
|
int64_t version = sqlite3_column_int64(statement, 1);
|
|
|
|
int64_t size = sqlite3_column_int64(statement, 2);
|
|
|
|
int64_t tail = sqlite3_column_int64(statement, 3);
|
2013-07-22 08:25:02 +00:00
|
|
|
const char *sender = (const char *)sqlite3_column_text(statement, 4);
|
|
|
|
const char *recipient = (const char *)sqlite3_column_text(statement, 5);
|
|
|
|
if (config.debug.meshms)
|
2013-10-03 13:46:45 +00:00
|
|
|
DEBUGF("found id %s, sender %s, recipient %s", id_hex, sender, recipient);
|
|
|
|
rhizome_bid_t bid;
|
|
|
|
if (str_to_rhizome_bid_t(&bid, id_hex) == -1) {
|
|
|
|
WHYF("invalid Bundle ID hex: %s -- skipping", alloca_str_toprint(id_hex));
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-03 07:14:06 +00:00
|
|
|
const char *them = recipient;
|
|
|
|
sid_t their_sid;
|
|
|
|
if (str_to_sid_t(&their_sid, them) == -1) {
|
|
|
|
WHYF("invalid SID hex: %s -- skipping", alloca_str_toprint(them));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cmp_sid_t(&their_sid, my_sid) == 0) {
|
|
|
|
them = sender;
|
|
|
|
if (str_to_sid_t(&their_sid, them) == -1) {
|
|
|
|
WHYF("invalid SID hex: %s -- skipping", alloca_str_toprint(them));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
struct conversations *ptr = add_conv(conv, &their_sid);
|
2013-07-25 05:19:06 +00:00
|
|
|
if (!ptr)
|
2013-10-03 07:14:06 +00:00
|
|
|
break;
|
2013-07-22 08:25:02 +00:00
|
|
|
struct ply *p;
|
|
|
|
if (them==sender){
|
2013-07-25 05:19:06 +00:00
|
|
|
ptr->found_their_ply=1;
|
|
|
|
p=&ptr->their_ply;
|
2013-07-22 08:25:02 +00:00
|
|
|
}else{
|
2013-07-25 05:19:06 +00:00
|
|
|
ptr->found_my_ply=1;
|
|
|
|
p=&ptr->my_ply;
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
2013-10-03 13:46:45 +00:00
|
|
|
p->bundle_id = bid;
|
2013-07-22 08:25:02 +00:00
|
|
|
p->version = version;
|
|
|
|
p->tail = tail;
|
|
|
|
p->size = size;
|
|
|
|
}
|
|
|
|
sqlite3_finalize(statement);
|
2013-10-03 07:14:06 +00:00
|
|
|
return 0;
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static struct conversations * find_or_create_conv(const sid_t *my_sid, const sid_t *their_sid)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
struct conversations *conv=NULL;
|
|
|
|
if (meshms_conversations_list(my_sid, their_sid, &conv))
|
|
|
|
return NULL;
|
|
|
|
if (!conv){
|
|
|
|
conv = emalloc_zero(sizeof(struct conversations));
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
conv->them = *their_sid;
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static int create_ply(const sid_t *my_sid, struct conversations *conv, rhizome_manifest *m)
|
|
|
|
{
|
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("Creating ply for my_sid=%s them=%s",
|
|
|
|
alloca_tohex_sid_t(conv->them),
|
|
|
|
alloca_tohex_sid_t(*my_sid));
|
|
|
|
rhizome_manifest_set_service(m, RHIZOME_SERVICE_MESHMS2);
|
|
|
|
rhizome_manifest_set_sender(m, my_sid);
|
|
|
|
rhizome_manifest_set_recipient(m, &conv->them);
|
|
|
|
rhizome_manifest_set_filesize(m, 0);
|
|
|
|
rhizome_manifest_set_tail(m, 0);
|
2013-11-05 07:28:03 +00:00
|
|
|
if (rhizome_fill_manifest(m, NULL, my_sid))
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-05 07:28:03 +00:00
|
|
|
assert(m->haveSecret);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->payloadEncryption == PAYLOAD_ENCRYPTED);
|
2013-10-03 13:46:45 +00:00
|
|
|
conv->my_ply.bundle_id = m->cryptoSignPublic;
|
|
|
|
conv->found_my_ply = 1;
|
2013-07-22 08:25:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static int append_footer(unsigned char *buffer, char type, int payload_len)
|
|
|
|
{
|
2013-07-23 04:51:46 +00:00
|
|
|
payload_len = (payload_len << 4) | (type&0xF);
|
|
|
|
write_uint16(buffer, payload_len);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
static int ply_read_open(struct ply_read *ply, const rhizome_bid_t *bid, rhizome_manifest *m)
|
|
|
|
{
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
2013-10-03 13:46:45 +00:00
|
|
|
DEBUGF("Opening ply %s", alloca_tohex_rhizome_bid_t(*bid));
|
|
|
|
if (rhizome_retrieve_manifest(bid, m))
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-05 07:28:03 +00:00
|
|
|
int ret = rhizome_open_decrypt_read(m, &ply->read);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ret == 1)
|
2013-10-03 13:46:45 +00:00
|
|
|
WARNF("Payload was not found for manifest %s, %"PRId64, alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ret != 0)
|
2013-08-26 03:46:18 +00:00
|
|
|
return ret;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
|
|
|
ply->read.offset = ply->read.length = m->filesize;
|
2013-07-22 08:25:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static int ply_read_close(struct ply_read *ply)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
if (ply->buffer){
|
|
|
|
free(ply->buffer);
|
|
|
|
ply->buffer=NULL;
|
|
|
|
}
|
2013-07-23 04:51:46 +00:00
|
|
|
ply->buffer_size=0;
|
2013-07-25 05:19:06 +00:00
|
|
|
ply->buff.len=0;
|
2013-07-22 08:25:02 +00:00
|
|
|
return rhizome_read_close(&ply->read);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the next record from the ply (backwards)
|
|
|
|
// returns 1 on EOF, -1 on failure
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static int ply_read_next(struct ply_read *ply)
|
|
|
|
{
|
|
|
|
ply->record_end_offset = ply->read.offset;
|
2013-07-23 04:51:46 +00:00
|
|
|
unsigned char footer[2];
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (ply->read.offset <= sizeof footer) {
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("EOF");
|
2013-07-22 08:25:02 +00:00
|
|
|
return 1;
|
2013-07-23 04:51:46 +00:00
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ply->read.offset -= sizeof footer;
|
|
|
|
if (rhizome_read_buffered(&ply->read, &ply->buff, footer, sizeof footer) != sizeof footer)
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
|
|
|
// (rhizome_read automatically advances the offset by the number of bytes read)
|
2013-07-23 04:51:46 +00:00
|
|
|
ply->record_length=read_uint16(footer);
|
|
|
|
ply->type = ply->record_length & 0xF;
|
|
|
|
ply->record_length = ply->record_length>>4;
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
if (config.debug.meshms)
|
2013-07-25 05:19:06 +00:00
|
|
|
DEBUGF("Found record %d, length %d @%"PRId64, ply->type, ply->record_length, ply->record_end_offset);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
// need to allow for advancing the tail and cutting a message in half.
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (ply->record_length + sizeof footer > ply->read.offset){
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("EOF");
|
2013-07-22 08:25:02 +00:00
|
|
|
return 1;
|
2013-07-23 04:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ply->read.offset -= ply->record_length + sizeof(footer);
|
|
|
|
uint64_t record_start = ply->read.offset;
|
|
|
|
|
|
|
|
if (ply->buffer_size < ply->record_length){
|
|
|
|
ply->buffer_size = ply->record_length;
|
2013-07-22 08:25:02 +00:00
|
|
|
unsigned char *b=realloc(ply->buffer, ply->buffer_size);
|
|
|
|
if (!b)
|
|
|
|
return WHY("realloc() failed");
|
|
|
|
ply->buffer = b;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ssize_t read = rhizome_read_buffered(&ply->read, &ply->buff, ply->buffer, ply->record_length);
|
|
|
|
if (read != ply->record_length)
|
|
|
|
return WHYF("Expected %u bytes read, got %zd", ply->record_length, read);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
ply->read.offset = record_start;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-25 05:19:06 +00:00
|
|
|
// keep reading past messages until you find this type.
|
2013-07-22 08:25:02 +00:00
|
|
|
static int ply_find_next(struct ply_read *ply, char type){
|
|
|
|
while(1){
|
|
|
|
int ret = ply_read_next(ply);
|
2013-07-23 04:51:46 +00:00
|
|
|
if (ret || ply->type==type)
|
2013-07-22 08:25:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 07:28:03 +00:00
|
|
|
static int append_meshms_buffer(const sid_t *my_sid, struct conversations *conv, unsigned char *buffer, int len)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
int ret=-1;
|
|
|
|
rhizome_manifest *mout = NULL;
|
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
|
|
|
if (!m)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (conv->found_my_ply){
|
2013-10-03 13:46:45 +00:00
|
|
|
if (rhizome_retrieve_manifest(&conv->my_ply.bundle_id, m))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
2013-11-05 07:28:03 +00:00
|
|
|
rhizome_authenticate_author(m);
|
|
|
|
if (!m->haveSecret || m->authorship != AUTHOR_AUTHENTIC)
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
}else{
|
2013-07-30 01:36:12 +00:00
|
|
|
if (create_ply(my_sid, conv, m))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2013-11-05 07:28:03 +00:00
|
|
|
assert(m->haveSecret);
|
|
|
|
assert(m->authorship == AUTHOR_AUTHENTIC);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
2013-11-05 07:28:03 +00:00
|
|
|
if (rhizome_append_journal_buffer(m, 0, buffer, len))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-09-30 07:12:25 +00:00
|
|
|
if (rhizome_manifest_finalise(m, &mout, 1))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret=0;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (mout && mout!=m)
|
|
|
|
rhizome_manifest_free(mout);
|
|
|
|
if (m)
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update if any conversations are unread or need to be acked.
|
2013-07-25 05:19:06 +00:00
|
|
|
// return -1 for failure, 1 if the conversation index needs to be saved.
|
2013-07-30 01:36:12 +00:00
|
|
|
static int update_conversation(const sid_t *my_sid, struct conversations *conv){
|
2013-07-22 08:25:02 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUG("Checking if conversation needs to be acked");
|
|
|
|
|
|
|
|
// Nothing to be done if they have never sent us anything
|
|
|
|
if (!conv->found_their_ply)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rhizome_manifest *m_ours = NULL;
|
|
|
|
rhizome_manifest *m_theirs = rhizome_new_manifest();
|
|
|
|
if (!m_theirs)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct ply_read ply;
|
|
|
|
bzero(&ply, sizeof(ply));
|
|
|
|
int ret=-1;
|
|
|
|
|
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUG("Locating their last message");
|
|
|
|
|
|
|
|
// find the offset of their last message
|
2013-10-03 13:46:45 +00:00
|
|
|
if (rhizome_retrieve_manifest(&conv->their_ply.bundle_id, m_theirs))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
if (ply_read_open(&ply, &conv->their_ply.bundle_id, m_theirs))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret = ply_find_next(&ply, MESHMS_BLOCK_TYPE_MESSAGE);
|
2013-07-25 05:19:06 +00:00
|
|
|
if (ret!=0){
|
|
|
|
// no messages indicates that we didn't do anthing
|
|
|
|
if (ret>0)
|
|
|
|
ret=0;
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
2013-07-25 05:19:06 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
|
2013-07-25 05:19:06 +00:00
|
|
|
if (conv->their_last_message == ply.record_end_offset){
|
|
|
|
// nothing has changed since last time
|
|
|
|
ret=0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
conv->their_last_message = ply.record_end_offset;
|
2013-07-22 08:25:02 +00:00
|
|
|
if (config.debug.meshms)
|
2013-07-25 05:19:06 +00:00
|
|
|
DEBUGF("Found last message @%"PRId64, conv->their_last_message);
|
2013-07-22 08:25:02 +00:00
|
|
|
ply_read_close(&ply);
|
|
|
|
|
2013-07-23 04:51:46 +00:00
|
|
|
// find our previous ack
|
|
|
|
uint64_t previous_ack = 0;
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
if (conv->found_my_ply){
|
|
|
|
if (config.debug.meshms)
|
2013-07-23 04:51:46 +00:00
|
|
|
DEBUG("Locating our previous ack");
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
m_ours = rhizome_new_manifest();
|
|
|
|
if (!m_ours)
|
|
|
|
goto end;
|
2013-10-03 13:46:45 +00:00
|
|
|
if (rhizome_retrieve_manifest(&conv->my_ply.bundle_id, m_ours))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
if (ply_read_open(&ply, &conv->my_ply.bundle_id, m_ours))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret = ply_find_next(&ply, MESHMS_BLOCK_TYPE_ACK);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ret == -1)
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
2013-07-23 04:51:46 +00:00
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
if (ret==0){
|
2013-10-17 12:47:41 +00:00
|
|
|
if (unpack_uint(ply.buffer, ply.record_length, &previous_ack) == -1)
|
2013-07-23 04:51:46 +00:00
|
|
|
previous_ack=0;
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("Previous ack is %"PRId64, previous_ack);
|
2013-07-22 08:25:02 +00:00
|
|
|
ply_read_close(&ply);
|
|
|
|
}else{
|
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("No outgoing ply");
|
|
|
|
}
|
|
|
|
|
2013-07-25 05:19:06 +00:00
|
|
|
if (previous_ack >= conv->their_last_message){
|
2013-07-22 08:25:02 +00:00
|
|
|
// their last message has already been acked
|
2013-07-25 05:19:06 +00:00
|
|
|
ret=1;
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append an ack for their message
|
|
|
|
if (config.debug.meshms)
|
2013-07-25 05:19:06 +00:00
|
|
|
DEBUGF("Creating ACK for %"PRId64" - %"PRId64, previous_ack, conv->their_last_message);
|
2013-07-23 04:51:46 +00:00
|
|
|
|
|
|
|
unsigned char buffer[24];
|
|
|
|
int ofs=0;
|
2013-07-25 05:19:06 +00:00
|
|
|
ofs+=pack_uint(&buffer[ofs], conv->their_last_message);
|
2013-07-23 04:51:46 +00:00
|
|
|
if (previous_ack)
|
2013-07-25 05:19:06 +00:00
|
|
|
ofs+=pack_uint(&buffer[ofs], conv->their_last_message - previous_ack);
|
2013-07-23 04:51:46 +00:00
|
|
|
ofs+=append_footer(buffer+ofs, MESHMS_BLOCK_TYPE_ACK, ofs);
|
2013-07-30 01:36:12 +00:00
|
|
|
ret = append_meshms_buffer(my_sid, conv, buffer, ofs);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
ply_read_close(&ply);
|
|
|
|
if (m_ours)
|
|
|
|
rhizome_manifest_free(m_ours);
|
|
|
|
if (m_theirs)
|
|
|
|
rhizome_manifest_free(m_theirs);
|
2013-07-25 05:19:06 +00:00
|
|
|
|
|
|
|
// if it's all good, remember the size of their ply at the time we examined it.
|
|
|
|
if (ret>=0)
|
|
|
|
conv->their_size = conv->their_ply.size;
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-25 05:19:06 +00:00
|
|
|
// update conversations, and return 1 if the conversation index should be saved
|
2013-07-30 01:36:12 +00:00
|
|
|
static int update_conversations(const sid_t *my_sid, struct conversations *conv){
|
2013-07-22 08:25:02 +00:00
|
|
|
if (!conv)
|
|
|
|
return 0;
|
2013-07-25 05:19:06 +00:00
|
|
|
int ret = 0;
|
2013-07-30 01:36:12 +00:00
|
|
|
if (update_conversations(my_sid, conv->_left))
|
2013-07-25 05:19:06 +00:00
|
|
|
ret=1;
|
|
|
|
|
|
|
|
if (conv->their_size != conv->their_ply.size){
|
2013-07-30 01:36:12 +00:00
|
|
|
if (update_conversation(my_sid, conv)>0)
|
2013-07-25 05:19:06 +00:00
|
|
|
ret=1;
|
|
|
|
}
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
if (update_conversations(my_sid, conv->_right))
|
2013-07-25 05:19:06 +00:00
|
|
|
ret=1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read our cached conversation list from our rhizome payload
|
2013-07-31 01:03:46 +00:00
|
|
|
// if we can't load the existing data correctly, just ignore it.
|
2013-10-03 07:14:06 +00:00
|
|
|
static int read_known_conversations(rhizome_manifest *m, const sid_t *their_sid, struct conversations **conv)
|
|
|
|
{
|
2013-07-31 01:03:46 +00:00
|
|
|
if (m->haveSecret==NEW_BUNDLE_ID)
|
2013-07-25 05:19:06 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct rhizome_read read;
|
|
|
|
bzero(&read, sizeof(read));
|
|
|
|
struct rhizome_read_buffer buff;
|
|
|
|
bzero(&buff, sizeof(buff));
|
|
|
|
|
2013-11-05 07:28:03 +00:00
|
|
|
int ret = rhizome_open_decrypt_read(m, &read);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ret == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
2013-07-30 01:36:12 +00:00
|
|
|
|
2013-07-31 01:03:46 +00:00
|
|
|
unsigned char version=0xFF;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ssize_t r = rhizome_read_buffered(&read, &buff, &version, 1);
|
|
|
|
ret = -1;
|
|
|
|
if (r == -1)
|
|
|
|
goto end;
|
|
|
|
if (version != 1) {
|
|
|
|
WARNF("Expected version 1 (got 0x%02x)", version);
|
2013-07-31 01:03:46 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
while (1) {
|
2013-07-30 01:36:12 +00:00
|
|
|
sid_t sid;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
r = rhizome_read_buffered(&read, &buff, sid.binary, sizeof sid.binary);
|
|
|
|
if (r != sizeof sid.binary)
|
2013-07-25 05:19:06 +00:00
|
|
|
break;
|
2013-07-26 05:06:11 +00:00
|
|
|
if (config.debug.meshms)
|
2013-10-09 08:24:21 +00:00
|
|
|
DEBUGF("Reading existing conversation for %s", alloca_tohex_sid_t(sid));
|
2013-10-03 07:14:06 +00:00
|
|
|
if (their_sid && cmp_sid_t(&sid, their_sid) != 0)
|
2013-07-25 05:19:06 +00:00
|
|
|
continue;
|
2013-07-30 01:36:12 +00:00
|
|
|
struct conversations *ptr = add_conv(conv, &sid);
|
2013-07-25 05:19:06 +00:00
|
|
|
if (!ptr)
|
2013-07-31 01:03:46 +00:00
|
|
|
goto end;
|
2013-07-25 05:19:06 +00:00
|
|
|
unsigned char details[8*3];
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
r = rhizome_read_buffered(&read, &buff, details, sizeof details);
|
|
|
|
if (r == -1)
|
2013-07-31 01:03:46 +00:00
|
|
|
break;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int bytes = r;
|
|
|
|
int ofs = 0;
|
|
|
|
int unpacked = unpack_uint(details, bytes, &ptr->their_last_message);
|
|
|
|
if (unpacked == -1)
|
2013-07-31 01:03:46 +00:00
|
|
|
break;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ofs += unpacked;
|
|
|
|
unpacked = unpack_uint(details+ofs, bytes-ofs, &ptr->read_offset);
|
|
|
|
if (unpacked == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
break;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ofs += unpacked;
|
|
|
|
unpacked = unpack_uint(details+ofs, bytes-ofs, &ptr->their_size);
|
|
|
|
if (unpacked == -1)
|
2013-07-31 01:03:46 +00:00
|
|
|
break;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ofs += unpacked;
|
2013-07-31 01:03:46 +00:00
|
|
|
read.offset += ofs - bytes;
|
2013-07-25 05:19:06 +00:00
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ret = 0;
|
2013-07-25 05:19:06 +00:00
|
|
|
end:
|
|
|
|
rhizome_read_close(&read);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return ret;
|
2013-07-25 05:19:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
static ssize_t write_conversation(struct rhizome_write *write, struct conversations *conv)
|
|
|
|
{
|
|
|
|
size_t len=0;
|
2013-07-26 05:06:11 +00:00
|
|
|
if (!conv)
|
|
|
|
return len;
|
2013-07-25 05:19:06 +00:00
|
|
|
{
|
2013-07-30 01:36:12 +00:00
|
|
|
unsigned char buffer[sizeof(conv->them) + (8*3)];
|
2013-07-25 05:19:06 +00:00
|
|
|
if (write)
|
2013-07-30 01:36:12 +00:00
|
|
|
bcopy(conv->them.binary, buffer, sizeof(conv->them));
|
2013-07-25 05:19:06 +00:00
|
|
|
len+=sizeof(conv->them);
|
2013-07-26 05:06:11 +00:00
|
|
|
if (write){
|
2013-07-31 01:03:46 +00:00
|
|
|
len+=pack_uint(&buffer[len], conv->their_last_message);
|
|
|
|
len+=pack_uint(&buffer[len], conv->read_offset);
|
|
|
|
len+=pack_uint(&buffer[len], conv->their_size);
|
2013-07-26 05:06:11 +00:00
|
|
|
int ret=rhizome_write_buffer(write, buffer, len);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ret == -1)
|
2013-07-26 05:06:11 +00:00
|
|
|
return ret;
|
2013-07-31 01:03:46 +00:00
|
|
|
}else{
|
|
|
|
len+=measure_packed_uint(conv->their_last_message);
|
|
|
|
len+=measure_packed_uint(conv->read_offset);
|
|
|
|
len+=measure_packed_uint(conv->their_size);
|
2013-07-26 05:06:11 +00:00
|
|
|
}
|
2013-10-17 12:47:41 +00:00
|
|
|
DEBUGF("len %s, %"PRId64", %"PRId64", %"PRId64" = %zu",
|
2013-10-09 08:24:21 +00:00
|
|
|
alloca_tohex_sid_t(conv->them),
|
2013-07-31 01:03:46 +00:00
|
|
|
conv->their_last_message,
|
|
|
|
conv->read_offset,
|
|
|
|
conv->their_size,
|
|
|
|
len);
|
2013-07-25 05:19:06 +00:00
|
|
|
}
|
|
|
|
// write the two child nodes
|
2013-10-17 12:47:41 +00:00
|
|
|
ssize_t ret = write_conversation(write, conv->_left);
|
|
|
|
if (ret == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
return ret;
|
2013-10-17 12:47:41 +00:00
|
|
|
len += (size_t) ret;
|
|
|
|
ret = write_conversation(write, conv->_right);
|
|
|
|
if (ret == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
return ret;
|
2013-10-17 12:47:41 +00:00
|
|
|
len += (size_t) ret;
|
2013-07-25 05:19:06 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
static int write_known_conversations(rhizome_manifest *m, struct conversations *conv)
|
|
|
|
{
|
2013-07-25 05:19:06 +00:00
|
|
|
rhizome_manifest *mout=NULL;
|
|
|
|
|
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof(write));
|
|
|
|
int ret=-1;
|
|
|
|
|
|
|
|
// TODO rebalance tree...
|
|
|
|
|
|
|
|
// measure the final payload first
|
2013-10-17 12:47:41 +00:00
|
|
|
ssize_t len=write_conversation(NULL, conv);
|
|
|
|
if (len == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
// then write it
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
rhizome_manifest_set_version(m, m->version + 1);
|
|
|
|
rhizome_manifest_set_filesize(m, (size_t)len + 1);
|
2013-07-26 05:06:11 +00:00
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
if (rhizome_write_open_manifest(&write, m) == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
2013-07-31 01:03:46 +00:00
|
|
|
unsigned char version=1;
|
2013-10-17 12:47:41 +00:00
|
|
|
if (rhizome_write_buffer(&write, &version, 1) == -1)
|
2013-07-31 01:03:46 +00:00
|
|
|
goto end;
|
2013-10-17 12:47:41 +00:00
|
|
|
if (write_conversation(&write, conv) == -1)
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
if (rhizome_finish_write(&write))
|
|
|
|
goto end;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
rhizome_manifest_set_filehash(m, &write.id);
|
2013-09-30 07:12:25 +00:00
|
|
|
if (rhizome_manifest_finalise(m, &mout, 1))
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret=0;
|
|
|
|
end:
|
|
|
|
if (ret)
|
|
|
|
rhizome_fail_write(&write);
|
|
|
|
if (mout && m!=mout)
|
|
|
|
rhizome_manifest_free(mout);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read information about existing conversations from a rhizome payload
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
static int meshms_conversations_list(const sid_t *my_sid, const sid_t *their_sid, struct conversations **conv)
|
|
|
|
{
|
2013-07-25 05:19:06 +00:00
|
|
|
int ret=-1;
|
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
|
|
|
if (!m)
|
|
|
|
goto end;
|
2013-07-30 01:36:12 +00:00
|
|
|
if (get_my_conversation_bundle(my_sid, m))
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
// read conversations payload
|
2013-07-30 01:36:12 +00:00
|
|
|
if (read_known_conversations(m, their_sid, conv))
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
if (get_database_conversations(my_sid, their_sid, conv))
|
2013-07-25 05:19:06 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
if (update_conversations(my_sid, *conv) && !their_sid){
|
2013-07-25 05:19:06 +00:00
|
|
|
if (write_known_conversations(m, *conv))
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret=0;
|
|
|
|
|
|
|
|
end:
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
return ret;
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// recursively traverse the conversation tree in sorted order and output the details of each conversation
|
|
|
|
static int output_conversations(struct cli_context *context, struct conversations *conv,
|
|
|
|
int output, int offset, int count){
|
|
|
|
if (!conv)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int traverse_count = output_conversations(context, conv->_left, output, offset, count);
|
|
|
|
if (count <0 || output + traverse_count < offset + count){
|
|
|
|
if (output + traverse_count >= offset){
|
2013-07-30 01:36:12 +00:00
|
|
|
cli_put_long(context, output + traverse_count, ":");
|
|
|
|
cli_put_hexvalue(context, conv->them.binary, sizeof(conv->them), ":");
|
2013-07-30 06:56:01 +00:00
|
|
|
cli_put_string(context, conv->read_offset < conv->their_last_message ? "unread":"", ":");
|
|
|
|
cli_put_long(context, conv->their_last_message, ":");
|
|
|
|
cli_put_long(context, conv->read_offset, "\n");
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
|
|
|
traverse_count++;
|
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
traverse_count += output_conversations(context, conv->_right, output + traverse_count, offset, count);
|
2013-07-22 08:25:02 +00:00
|
|
|
return traverse_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// output the list of existing conversations for a given local identity
|
|
|
|
int app_meshms_conversations(const struct cli_parsed *parsed, struct cli_context *context){
|
|
|
|
const char *sidhex, *offset_str, *count_str;
|
|
|
|
if (cli_arg(parsed, "sid", &sidhex, str_is_subscriber_id, "") == -1
|
|
|
|
|| cli_arg(parsed, "offset", &offset_str, NULL, "0")==-1
|
|
|
|
|| cli_arg(parsed, "count", &count_str, NULL, "-1")==-1)
|
|
|
|
return -1;
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
sid_t sid;
|
|
|
|
fromhex(sid.binary, sidhex, sizeof(sid.binary));
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
int offset=atoi(offset_str);
|
|
|
|
int count=atoi(count_str);
|
|
|
|
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
if (!(keyring = keyring_open_instance_cli(parsed)))
|
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (rhizome_opendb() == -1){
|
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
struct conversations *conv=NULL;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (meshms_conversations_list(&sid, NULL, &conv)){
|
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
const char *names[]={
|
2013-07-30 06:56:01 +00:00
|
|
|
"_id","recipient","read", "last_message", "read_offset"
|
2013-07-22 08:25:02 +00:00
|
|
|
};
|
|
|
|
|
2013-07-30 06:56:01 +00:00
|
|
|
cli_columns(context, 5, names);
|
2013-07-30 01:36:12 +00:00
|
|
|
int rows = output_conversations(context, conv, 0, offset, count);
|
|
|
|
cli_row_count(context, rows);
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
free_conversations(conv);
|
2013-11-06 05:05:44 +00:00
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 07:28:03 +00:00
|
|
|
int app_meshms_send_message(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
const char *my_sidhex, *their_sidhex, *message;
|
|
|
|
if (cli_arg(parsed, "sender_sid", &my_sidhex, str_is_subscriber_id, "") == -1
|
|
|
|
|| cli_arg(parsed, "recipient_sid", &their_sidhex, str_is_subscriber_id, "") == -1
|
|
|
|
|| cli_arg(parsed, "payload", &message, NULL, "") == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
if (!(keyring = keyring_open_instance_cli(parsed)))
|
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (rhizome_opendb() == -1){
|
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
|
|
|
|
sid_t my_sid, their_sid;
|
2013-11-05 07:28:03 +00:00
|
|
|
if (str_to_sid_t(&my_sid, my_sidhex) == -1)
|
|
|
|
return WHY("invalid sender SID");
|
|
|
|
if (str_to_sid_t(&their_sid, their_sidhex) == -1)
|
|
|
|
return WHY("invalid recipient SID");
|
|
|
|
struct conversations *conv = find_or_create_conv(&my_sid, &their_sid);
|
2013-11-06 14:12:42 +00:00
|
|
|
if (!conv) {
|
2013-11-06 05:05:44 +00:00
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
// construct a message payload
|
|
|
|
int message_len = strlen(message)+1;
|
|
|
|
|
|
|
|
// TODO, new format here.
|
2013-07-23 04:51:46 +00:00
|
|
|
unsigned char buffer[message_len+3];
|
|
|
|
strcpy((char*)buffer, message); // message
|
|
|
|
message_len+=append_footer(buffer+message_len, MESHMS_BLOCK_TYPE_MESSAGE, message_len);
|
2013-07-30 01:36:12 +00:00
|
|
|
int ret = append_meshms_buffer(&my_sid, conv, buffer, message_len);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
free_conversations(conv);
|
2013-11-06 05:05:44 +00:00
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int app_meshms_list_messages(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
2013-07-22 08:25:02 +00:00
|
|
|
const char *my_sidhex, *their_sidhex;
|
|
|
|
if (cli_arg(parsed, "sender_sid", &my_sidhex, str_is_subscriber_id, "") == -1
|
|
|
|
|| cli_arg(parsed, "recipient_sid", &their_sidhex, str_is_subscriber_id, "") == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
if (!(keyring = keyring_open_instance_cli(parsed)))
|
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (rhizome_opendb() == -1){
|
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
sid_t my_sid, their_sid;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (str_to_sid_t(&my_sid, my_sidhex) == -1){
|
|
|
|
keyring_free(keyring);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return WHY("invalid sender SID");
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
|
|
|
if (str_to_sid_t(&their_sid, their_sidhex) == -1){
|
|
|
|
keyring_free(keyring);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return WHY("invalid recipient SID");
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
struct conversations *conv=find_or_create_conv(&my_sid, &their_sid);
|
2013-11-06 05:05:44 +00:00
|
|
|
if (!conv){
|
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
int ret=-1;
|
|
|
|
|
|
|
|
const char *names[]={
|
2013-08-02 04:32:56 +00:00
|
|
|
"_id","offset","type","message"
|
2013-07-22 08:25:02 +00:00
|
|
|
};
|
|
|
|
|
2013-08-02 04:32:56 +00:00
|
|
|
cli_columns(context, 4, names);
|
2013-07-22 08:25:02 +00:00
|
|
|
|
2013-07-26 05:06:11 +00:00
|
|
|
rhizome_manifest *m_ours=NULL, *m_theirs=NULL;
|
|
|
|
struct ply_read read_ours, read_theirs;
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
// if we've never sent a message, (or acked theirs), there is nothing to show
|
|
|
|
if (!conv->found_my_ply){
|
|
|
|
ret=0;
|
2013-07-30 01:36:12 +00:00
|
|
|
cli_row_count(context, 0);
|
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("Did not find my ply");
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start reading messages from both ply's in reverse order
|
|
|
|
bzero(&read_ours, sizeof(read_ours));
|
|
|
|
bzero(&read_theirs, sizeof(read_theirs));
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
m_ours = rhizome_new_manifest();
|
|
|
|
if (!m_ours)
|
|
|
|
goto end;
|
2013-10-03 13:46:45 +00:00
|
|
|
if (ply_read_open(&read_ours, &conv->my_ply.bundle_id, m_ours))
|
2013-07-30 01:36:12 +00:00
|
|
|
goto end;
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
uint64_t their_last_ack=0;
|
2013-08-02 04:32:56 +00:00
|
|
|
uint64_t their_ack_offset=0;
|
|
|
|
int64_t unread_mark=conv->read_offset;
|
2013-07-22 08:25:02 +00:00
|
|
|
|
|
|
|
if (conv->found_their_ply){
|
2013-09-20 07:17:39 +00:00
|
|
|
m_theirs = rhizome_new_manifest();
|
2013-07-22 08:25:02 +00:00
|
|
|
if (!m_theirs)
|
|
|
|
goto end;
|
2013-10-03 13:46:45 +00:00
|
|
|
if (ply_read_open(&read_theirs, &conv->their_ply.bundle_id, m_theirs))
|
2013-07-22 08:25:02 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
// find their last ACK so we know if messages have been received
|
|
|
|
int r = ply_find_next(&read_theirs, MESHMS_BLOCK_TYPE_ACK);
|
2013-07-23 04:51:46 +00:00
|
|
|
if (r==0){
|
2013-10-17 12:47:41 +00:00
|
|
|
if (unpack_uint(read_theirs.buffer, read_theirs.record_length, &their_last_ack) == -1)
|
2013-07-23 04:51:46 +00:00
|
|
|
their_last_ack=0;
|
2013-08-02 04:32:56 +00:00
|
|
|
else
|
|
|
|
their_ack_offset = read_theirs.record_end_offset;
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("Found their last ack @%"PRId64, their_last_ack);
|
|
|
|
}
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
2013-08-02 04:32:56 +00:00
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
int id=0;
|
|
|
|
while(ply_read_next(&read_ours)==0){
|
|
|
|
if (config.debug.meshms)
|
2013-07-26 05:06:11 +00:00
|
|
|
DEBUGF("Offset %"PRId64", type %d, read_offset %"PRId64, read_ours.read.offset, read_ours.type, conv->read_offset);
|
2013-08-02 04:32:56 +00:00
|
|
|
|
|
|
|
if (their_last_ack && their_last_ack >= read_ours.record_end_offset){
|
|
|
|
cli_put_long(context, id++, ":");
|
|
|
|
cli_put_long(context, their_ack_offset, ":");
|
|
|
|
cli_put_string(context, "ACK", ":");
|
|
|
|
cli_put_string(context, "delivered", "\n");
|
|
|
|
their_last_ack = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-23 04:51:46 +00:00
|
|
|
switch(read_ours.type){
|
2013-07-22 08:25:02 +00:00
|
|
|
case MESHMS_BLOCK_TYPE_ACK:
|
|
|
|
// read their message list, and insert all messages that are included in the ack range
|
|
|
|
if (conv->found_their_ply){
|
2013-07-23 04:51:46 +00:00
|
|
|
int ofs=unpack_uint(read_ours.buffer, read_ours.record_length, (uint64_t*)&read_theirs.read.offset);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (ofs == -1)
|
2013-07-23 04:51:46 +00:00
|
|
|
break;
|
|
|
|
uint64_t end_range;
|
|
|
|
int x = unpack_uint(read_ours.buffer+ofs, read_ours.record_length - ofs, &end_range);
|
2013-10-17 12:47:41 +00:00
|
|
|
if (x == -1)
|
2013-07-23 04:51:46 +00:00
|
|
|
end_range=0;
|
|
|
|
else
|
|
|
|
end_range = read_theirs.read.offset - end_range;
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
// TODO tail
|
|
|
|
// just incase we don't have the full bundle anymore
|
|
|
|
if (read_theirs.read.offset > read_theirs.read.length)
|
|
|
|
read_theirs.read.offset = read_theirs.read.length;
|
2013-08-02 04:32:56 +00:00
|
|
|
|
2013-07-23 04:51:46 +00:00
|
|
|
if (config.debug.meshms)
|
|
|
|
DEBUGF("Reading other log from %"PRId64", to %"PRId64, read_theirs.read.offset, end_range);
|
2013-07-22 08:25:02 +00:00
|
|
|
while(ply_find_next(&read_theirs, MESHMS_BLOCK_TYPE_MESSAGE)==0){
|
|
|
|
if (read_theirs.read.offset < end_range)
|
|
|
|
break;
|
2013-08-02 04:32:56 +00:00
|
|
|
|
|
|
|
if (unread_mark >= (int64_t)read_theirs.record_end_offset){
|
|
|
|
cli_put_long(context, id++, ":");
|
|
|
|
cli_put_long(context, unread_mark, ":");
|
|
|
|
cli_put_string(context, "MARK", ":");
|
|
|
|
cli_put_string(context, "read", "\n");
|
|
|
|
unread_mark = -1;
|
|
|
|
}
|
|
|
|
|
2013-07-22 08:25:02 +00:00
|
|
|
cli_put_long(context, id++, ":");
|
2013-07-26 05:06:11 +00:00
|
|
|
cli_put_long(context, read_theirs.record_end_offset, ":");
|
2013-08-02 04:32:56 +00:00
|
|
|
cli_put_string(context, "<", ":");
|
2013-07-23 04:51:46 +00:00
|
|
|
cli_put_string(context, (char *)read_theirs.buffer, "\n");
|
2013-07-22 08:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MESHMS_BLOCK_TYPE_MESSAGE:
|
|
|
|
// TODO new message format here
|
|
|
|
cli_put_long(context, id++, ":");
|
2013-07-26 05:06:11 +00:00
|
|
|
cli_put_long(context, read_ours.record_end_offset, ":");
|
2013-08-02 04:32:56 +00:00
|
|
|
cli_put_string(context, ">", ":");
|
2013-07-23 04:51:46 +00:00
|
|
|
cli_put_string(context, (char *)read_ours.buffer, "\n");
|
2013-07-22 08:25:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
|
|
|
|
cli_row_count(context, id);
|
2013-07-22 08:25:02 +00:00
|
|
|
ret=0;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (m_ours){
|
|
|
|
rhizome_manifest_free(m_ours);
|
|
|
|
ply_read_close(&read_ours);
|
|
|
|
}
|
|
|
|
if (m_theirs){
|
|
|
|
rhizome_manifest_free(m_theirs);
|
|
|
|
ply_read_close(&read_theirs);
|
|
|
|
}
|
|
|
|
free_conversations(conv);
|
2013-11-06 05:05:44 +00:00
|
|
|
keyring_free(keyring);
|
2013-07-22 08:25:02 +00:00
|
|
|
return ret;
|
2013-07-26 05:06:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 01:36:12 +00:00
|
|
|
static int mark_read(struct conversations *conv, const sid_t *their_sid, const char *offset_str){
|
2013-07-26 05:06:11 +00:00
|
|
|
int ret=0;
|
|
|
|
if (conv){
|
2013-10-03 07:14:06 +00:00
|
|
|
int cmp = their_sid ? cmp_sid_t(&conv->them, their_sid) : 0;
|
2013-07-26 05:06:11 +00:00
|
|
|
if (!their_sid || cmp<0){
|
|
|
|
ret+=mark_read(conv->_left, their_sid, offset_str);
|
|
|
|
}
|
|
|
|
if (!their_sid || cmp==0){
|
|
|
|
// update read offset
|
|
|
|
// - never rewind
|
|
|
|
// - never past their last message
|
|
|
|
uint64_t offset = conv->their_last_message;
|
|
|
|
if (offset_str){
|
|
|
|
uint64_t x = atol(offset_str);
|
|
|
|
if (x<offset)
|
|
|
|
offset=x;
|
|
|
|
}
|
|
|
|
if (offset > conv->read_offset){
|
|
|
|
if (config.debug.meshms)
|
2013-07-30 01:36:12 +00:00
|
|
|
DEBUGF("Moving read marker for %s, from %"PRId64" to %"PRId64,
|
2013-10-09 08:24:21 +00:00
|
|
|
alloca_tohex_sid_t(conv->them), conv->read_offset, offset);
|
2013-07-26 05:06:11 +00:00
|
|
|
conv->read_offset = offset;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!their_sid || cmp>0){
|
|
|
|
ret+=mark_read(conv->_right, their_sid, offset_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int app_meshms_mark_read(const struct cli_parsed *parsed, struct cli_context *context)
|
|
|
|
{
|
2013-07-26 05:06:11 +00:00
|
|
|
const char *my_sidhex, *their_sidhex, *offset_str;
|
|
|
|
if (cli_arg(parsed, "sender_sid", &my_sidhex, str_is_subscriber_id, "") == -1
|
|
|
|
|| cli_arg(parsed, "recipient_sid", &their_sidhex, str_is_subscriber_id, NULL) == -1
|
|
|
|
|| cli_arg(parsed, "offset", &offset_str, NULL, NULL)==-1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (create_serval_instance_dir() == -1)
|
|
|
|
return -1;
|
|
|
|
if (!(keyring = keyring_open_instance_cli(parsed)))
|
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
if (rhizome_opendb() == -1){
|
|
|
|
keyring_free(keyring);
|
2013-07-26 05:06:11 +00:00
|
|
|
return -1;
|
2013-11-06 05:05:44 +00:00
|
|
|
}
|
2013-07-30 01:36:12 +00:00
|
|
|
sid_t my_sid, their_sid;
|
|
|
|
fromhex(my_sid.binary, my_sidhex, sizeof(my_sid.binary));
|
|
|
|
if (their_sidhex)
|
|
|
|
fromhex(their_sid.binary, their_sidhex, sizeof(their_sid.binary));
|
|
|
|
|
2013-07-26 05:06:11 +00:00
|
|
|
int ret=-1;
|
|
|
|
struct conversations *conv=NULL;
|
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
|
|
|
if (!m)
|
|
|
|
goto end;
|
2013-07-30 01:36:12 +00:00
|
|
|
if (get_my_conversation_bundle(&my_sid, m))
|
2013-07-26 05:06:11 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
// read all conversations, so we can write them again
|
|
|
|
if (read_known_conversations(m, NULL, &conv))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
// read the full list of conversations from the database too
|
2013-07-30 01:36:12 +00:00
|
|
|
if (get_database_conversations(&my_sid, NULL, &conv))
|
2013-07-26 05:06:11 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
// check if any incoming conversations need to be acked or have new messages and update the read offset
|
2013-07-30 01:36:12 +00:00
|
|
|
int changed = update_conversations(&my_sid, conv);
|
|
|
|
if (mark_read(conv, their_sidhex?&their_sid:NULL, offset_str))
|
2013-07-26 05:06:11 +00:00
|
|
|
changed =1;
|
|
|
|
if (changed){
|
|
|
|
// save the conversation list
|
|
|
|
if (write_known_conversations(m, conv))
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret=0;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (m)
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
free_conversations(conv);
|
2013-11-06 05:05:44 +00:00
|
|
|
keyring_free(keyring);
|
2013-07-26 05:06:11 +00:00
|
|
|
return ret;
|
2013-09-30 07:12:25 +00:00
|
|
|
}
|