mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-20 05:37:57 +00:00
Introduce typedef for handling BAR values
This commit is contained in:
parent
dabd482ad8
commit
f63e7fee19
17
rhizome.h
17
rhizome.h
@ -61,13 +61,7 @@ typedef struct rhizome_signature {
|
||||
size_t signatureLength;
|
||||
} rhizome_signature;
|
||||
|
||||
#define RHIZOME_BAR_BYTES 32
|
||||
#define RHIZOME_BAR_COMPARE_BYTES 31
|
||||
#define RHIZOME_BAR_PREFIX_BYTES 15
|
||||
#define RHIZOME_BAR_PREFIX_OFFSET 0
|
||||
#define RHIZOME_BAR_FILESIZE_OFFSET 15
|
||||
#define RHIZOME_BAR_VERSION_OFFSET 16
|
||||
#define RHIZOME_BAR_GEOBOX_OFFSET 23
|
||||
#define RHIZOME_BAR_TTL_OFFSET 31
|
||||
|
||||
#define MAX_MANIFEST_VARS 256
|
||||
@ -456,6 +450,7 @@ enum sqlbind_type {
|
||||
ZEROBLOB, // int bytes
|
||||
SID_T, // const sid_t *sidp
|
||||
RHIZOME_BID_T, // const rhizome_bid_t *bidp
|
||||
RHIZOME_BAR_T, // const rhizome_bar_t *barp
|
||||
RHIZOME_FILEHASH_T, // const rhizome_filehash_t *hashp
|
||||
TOHEX, // const unsigned char *binary, unsigned bytes
|
||||
TEXT_TOUPPER, // const char *text,
|
||||
@ -536,10 +531,8 @@ double rhizome_manifest_get_double(rhizome_manifest *m,char *var,double default_
|
||||
int rhizome_manifest_extract_signature(rhizome_manifest *m, unsigned *ofs);
|
||||
int rhizome_update_file_priority(const char *fileid);
|
||||
enum rhizome_bundle_status rhizome_find_duplicate(const rhizome_manifest *m, rhizome_manifest **found);
|
||||
int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar);
|
||||
uint64_t rhizome_bar_version(const unsigned char *bar);
|
||||
uint64_t rhizome_bar_bidprefix_ll(const unsigned char *bar);
|
||||
int rhizome_is_bar_interesting(const unsigned char *bar);
|
||||
int rhizome_manifest_to_bar(rhizome_manifest *m, rhizome_bar_t *bar);
|
||||
int rhizome_is_bar_interesting(const rhizome_bar_t *bar);
|
||||
int rhizome_is_manifest_interesting(rhizome_manifest *m);
|
||||
int rhizome_retrieve_manifest(const rhizome_bid_t *bid, rhizome_manifest *m);
|
||||
int rhizome_retrieve_manifest_by_prefix(const unsigned char *prefix, unsigned prefix_len, rhizome_manifest *m);
|
||||
@ -583,8 +576,8 @@ int rhizome_secret2bk(
|
||||
int rhizome_sign_hash_with_key(rhizome_manifest *m,const unsigned char *sk,
|
||||
const unsigned char *pk,rhizome_signature *out);
|
||||
int rhizome_verify_bundle_privatekey(const unsigned char *sk, const unsigned char *pk);
|
||||
int rhizome_queue_ignore_manifest(unsigned char *bid_prefix, int prefix_len, int timeout);
|
||||
int rhizome_ignore_manifest_check(unsigned char *bid_prefix, int prefix_len);
|
||||
int rhizome_queue_ignore_manifest(const unsigned char *bid_prefix, int prefix_len, int timeout);
|
||||
int rhizome_ignore_manifest_check(const unsigned char *bid_prefix, int prefix_len);
|
||||
|
||||
/* Rhizome list cursor for iterating over all or a subset of manifests in the store.
|
||||
*/
|
||||
|
@ -707,6 +707,17 @@ int _sqlite_vbind(struct __sourceloc __whence, int log_level, sqlite_retry_state
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RHIZOME_BAR_T: {
|
||||
const rhizome_bar_t *barp = va_arg(ap, const rhizome_bar_t *);
|
||||
++argnum;
|
||||
if (barp == NULL) {
|
||||
BIND_NULL(RHIZOME_BAR_T);
|
||||
} else {
|
||||
BIND_DEBUG(STATIC_BLOB, sqlite3_bind_blob, "%s,%d,SQLITE_STATIC", alloca_toprint(20, barp->binary, RHIZOME_BAR_BYTES), RHIZOME_BAR_BYTES);
|
||||
BIND_RETRY(sqlite3_bind_blob, barp->binary, RHIZOME_BAR_BYTES, SQLITE_STATIC);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RHIZOME_FILEHASH_T: {
|
||||
const rhizome_filehash_t *hashp = va_arg(ap, const rhizome_filehash_t *);
|
||||
++argnum;
|
||||
@ -1412,8 +1423,8 @@ int rhizome_store_manifest(rhizome_manifest *m)
|
||||
return WHY("Manifest is not signed, and I don't have the key. Manifest might be forged or corrupt.");
|
||||
|
||||
/* Bind BAR to data field */
|
||||
unsigned char bar[RHIZOME_BAR_BYTES];
|
||||
rhizome_manifest_to_bar(m,bar);
|
||||
rhizome_bar_t bar;
|
||||
rhizome_manifest_to_bar(m, &bar);
|
||||
|
||||
/* Store the file (but not if it is already in the database) */
|
||||
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
||||
@ -1453,7 +1464,7 @@ int rhizome_store_manifest(rhizome_manifest *m)
|
||||
STATIC_BLOB, m->manifestdata, m->manifest_all_bytes,
|
||||
INT64, m->version,
|
||||
INT64, (int64_t) now,
|
||||
STATIC_BLOB, bar, RHIZOME_BAR_BYTES,
|
||||
RHIZOME_BAR_T, &bar,
|
||||
INT64, m->filesize,
|
||||
RHIZOME_FILEHASH_T|NUL, m->filesize > 0 ? &m->filehash : NULL,
|
||||
// Only store the author if it is known to be authentic.
|
||||
@ -2062,13 +2073,9 @@ static int is_interesting(const char *id_hex, uint64_t version)
|
||||
OUT();
|
||||
}
|
||||
|
||||
int rhizome_is_bar_interesting(const unsigned char *bar)
|
||||
int rhizome_is_bar_interesting(const rhizome_bar_t *bar)
|
||||
{
|
||||
uint64_t version = rhizome_bar_version(bar);
|
||||
char id_hex[RHIZOME_BAR_PREFIX_BYTES *2 + 2];
|
||||
tohex(id_hex, RHIZOME_BAR_PREFIX_BYTES * 2, &bar[RHIZOME_BAR_PREFIX_OFFSET]);
|
||||
strcat(id_hex, "%");
|
||||
return is_interesting(id_hex, version);
|
||||
return is_interesting(alloca_tohex_rhizome_bar_prefix(bar), rhizome_bar_version(bar));
|
||||
}
|
||||
|
||||
int rhizome_is_manifest_interesting(rhizome_manifest *m)
|
||||
|
@ -308,14 +308,12 @@ rhizome_direct_bundle_cursor *rhizome_direct_get_fill_response(unsigned char *bu
|
||||
while(them<them_count||us<us_count)
|
||||
{
|
||||
DEBUGF("them=%d, us=%d",them,us);
|
||||
unsigned char *them_bar=&buffer[10+them*RHIZOME_BAR_BYTES];
|
||||
unsigned char *us_bar=&usbuffer[10+us*RHIZOME_BAR_BYTES];
|
||||
const rhizome_bar_t *their_bar = (const rhizome_bar_t *)&buffer[10+them*RHIZOME_BAR_BYTES];
|
||||
const rhizome_bar_t *our_bar = (const rhizome_bar_t *)&usbuffer[10+us*RHIZOME_BAR_BYTES];
|
||||
int relation=0;
|
||||
if (them<them_count&&us<us_count) {
|
||||
relation=memcmp(them_bar,us_bar,RHIZOME_BAR_COMPARE_BYTES);
|
||||
relation=memcmp(their_bar->binary,our_bar->binary,RHIZOME_BAR_COMPARE_BYTES);
|
||||
DEBUGF("relation = %d",relation);
|
||||
dump("them BAR",them_bar,RHIZOME_BAR_BYTES);
|
||||
dump("us BAR",us_bar,RHIZOME_BAR_BYTES);
|
||||
}
|
||||
else if (us==us_count) relation=-1; /* they have a bundle we don't have */
|
||||
else if (them==them_count) relation=+1; /* we have a bundle they don't have */
|
||||
@ -329,54 +327,54 @@ rhizome_direct_bundle_cursor *rhizome_direct_get_fill_response(unsigned char *bu
|
||||
Append 16-byte "please send" record consisting of 0x01 followed
|
||||
by the eight-byte BID prefix from the BAR. */
|
||||
c->buffer[c->buffer_offset_bytes+c->buffer_used]=0x01; /* Please send */
|
||||
bcopy(&buffer[10+them*RHIZOME_BAR_BYTES+RHIZOME_BAR_PREFIX_OFFSET],
|
||||
bcopy(rhizome_bar_prefix(their_bar),
|
||||
&c->buffer[c->buffer_offset_bytes+c->buffer_used+1],
|
||||
RHIZOME_BAR_PREFIX_BYTES);
|
||||
c->buffer_used+=1+RHIZOME_BAR_PREFIX_BYTES;
|
||||
who=-1;
|
||||
DEBUGF("They have previously unseen bundle %016"PRIx64"*",
|
||||
rhizome_bar_bidprefix_ll(&buffer[10+them*RHIZOME_BAR_BYTES]));
|
||||
rhizome_bar_bidprefix_ll(their_bar));
|
||||
} else if (relation>0) {
|
||||
/* We have a bundle that they don't have any version of
|
||||
Append 16-byte "I have [newer]" record consisting of 0x02 followed
|
||||
by the eight-byte BID prefix from the BAR. */
|
||||
c->buffer[c->buffer_offset_bytes+c->buffer_used]=0x02; /* I have [newer] */
|
||||
bcopy(&usbuffer[10+us*RHIZOME_BAR_BYTES+RHIZOME_BAR_PREFIX_OFFSET],
|
||||
bcopy(rhizome_bar_prefix(our_bar),
|
||||
&c->buffer[c->buffer_offset_bytes+c->buffer_used+1],
|
||||
RHIZOME_BAR_PREFIX_BYTES);
|
||||
c->buffer_used+=1+RHIZOME_BAR_PREFIX_BYTES;
|
||||
who=+1;
|
||||
DEBUGF("We have previously unseen bundle %016"PRIx64"*",
|
||||
rhizome_bar_bidprefix_ll(&usbuffer[10+us*RHIZOME_BAR_BYTES]));
|
||||
rhizome_bar_bidprefix_ll(our_bar));
|
||||
} else {
|
||||
/* We each have a version of this bundle, so see whose is newer */
|
||||
uint64_t them_version = rhizome_bar_version(&buffer[10+them*RHIZOME_BAR_BYTES]);
|
||||
uint64_t us_version = rhizome_bar_version(&usbuffer[10+us*RHIZOME_BAR_BYTES]);
|
||||
uint64_t them_version = rhizome_bar_version(their_bar);
|
||||
uint64_t us_version = rhizome_bar_version(our_bar);
|
||||
if (them_version>us_version) {
|
||||
/* They have the newer version of the bundle */
|
||||
c->buffer[c->buffer_offset_bytes+c->buffer_used]=0x01; /* Please send */
|
||||
bcopy(&buffer[10+them*RHIZOME_BAR_BYTES+RHIZOME_BAR_PREFIX_OFFSET],
|
||||
bcopy(rhizome_bar_prefix(their_bar),
|
||||
&c->buffer[c->buffer_offset_bytes+c->buffer_used+1],
|
||||
RHIZOME_BAR_PREFIX_BYTES);
|
||||
c->buffer_used+=1+RHIZOME_BAR_PREFIX_BYTES;
|
||||
DEBUGF("They have newer version of bundle %016"PRIx64"* (%"PRIu64" versus %"PRIu64")",
|
||||
rhizome_bar_bidprefix_ll(&usbuffer[10+us*RHIZOME_BAR_BYTES]),
|
||||
rhizome_bar_bidprefix_ll(their_bar),
|
||||
them_version,
|
||||
us_version);
|
||||
} else if (them_version<us_version) {
|
||||
/* We have the newer version of the bundle */
|
||||
c->buffer[c->buffer_offset_bytes+c->buffer_used]=0x02; /* I have [newer] */
|
||||
bcopy(&usbuffer[10+us*RHIZOME_BAR_BYTES+RHIZOME_BAR_PREFIX_OFFSET],
|
||||
bcopy(rhizome_bar_prefix(our_bar),
|
||||
&c->buffer[c->buffer_offset_bytes+c->buffer_used+1],
|
||||
RHIZOME_BAR_PREFIX_BYTES);
|
||||
c->buffer_used+=1+RHIZOME_BAR_PREFIX_BYTES;
|
||||
DEBUGF("We have newer version of bundle %016"PRIx64"* (%"PRIu64" versus %"PRIu64")",
|
||||
rhizome_bar_bidprefix_ll(&usbuffer[10+us*RHIZOME_BAR_BYTES]),
|
||||
rhizome_bar_bidprefix_ll(our_bar),
|
||||
us_version,
|
||||
them_version);
|
||||
} else {
|
||||
DEBUGF("We both have the same version of %016"PRIx64"*",
|
||||
rhizome_bar_bidprefix_ll(&buffer[10+them*RHIZOME_BAR_BYTES]));
|
||||
rhizome_bar_bidprefix_ll(their_bar));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -421,7 +421,7 @@ typedef struct ignored_manifest_cache {
|
||||
a collision is exceedingly remote */
|
||||
ignored_manifest_cache ignored;
|
||||
|
||||
int rhizome_ignore_manifest_check(unsigned char *bid_prefix, int prefix_len)
|
||||
int rhizome_ignore_manifest_check(const unsigned char *bid_prefix, int prefix_len)
|
||||
{
|
||||
if (prefix_len < RHIZOME_BAR_PREFIX_BYTES)
|
||||
FATAL("Prefix length is too short");
|
||||
@ -443,7 +443,7 @@ int rhizome_ignore_manifest_check(unsigned char *bid_prefix, int prefix_len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rhizome_queue_ignore_manifest(unsigned char *bid_prefix, int prefix_len, int timeout)
|
||||
int rhizome_queue_ignore_manifest(const unsigned char *bid_prefix, int prefix_len, int timeout)
|
||||
{
|
||||
if (prefix_len < RHIZOME_BAR_PREFIX_BYTES)
|
||||
FATAL("Prefix length is too short");
|
||||
|
@ -45,7 +45,7 @@ int log2ll(uint64_t x)
|
||||
}
|
||||
|
||||
|
||||
int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar)
|
||||
int rhizome_manifest_to_bar(rhizome_manifest *m, rhizome_bar_t *bar)
|
||||
{
|
||||
IN();
|
||||
/* BAR = Bundle Advertisement Record.
|
||||
@ -81,13 +81,14 @@ int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar)
|
||||
|
||||
/* Manifest prefix */
|
||||
unsigned i;
|
||||
for(i=0;i<RHIZOME_BAR_PREFIX_BYTES;i++)
|
||||
bar[RHIZOME_BAR_PREFIX_OFFSET+i]=m->cryptoSignPublic.binary[i];
|
||||
for(i=0;i<RHIZOME_BAR_PREFIX_BYTES;i++)
|
||||
bar->binary[RHIZOME_BAR_PREFIX_OFFSET+i]=m->cryptoSignPublic.binary[i];
|
||||
/* file length */
|
||||
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
||||
bar[RHIZOME_BAR_FILESIZE_OFFSET]=log2ll(m->filesize);
|
||||
bar->binary[RHIZOME_BAR_FILESIZE_OFFSET]=log2ll(m->filesize);
|
||||
/* Version */
|
||||
for(i=0;i<7;i++) bar[RHIZOME_BAR_VERSION_OFFSET+6-i]=(m->version>>(8*i))&0xff;
|
||||
for(i=0;i<7;i++)
|
||||
bar->binary[RHIZOME_BAR_VERSION_OFFSET+6-i]=(m->version>>(8*i))&0xff;
|
||||
|
||||
#if 0
|
||||
/* geo bounding box TODO: replace with bounding circle!!! */
|
||||
@ -107,34 +108,34 @@ int rhizome_manifest_to_bar(rhizome_manifest *m,unsigned char *bar)
|
||||
#endif
|
||||
unsigned short v;
|
||||
int o=RHIZOME_BAR_GEOBOX_OFFSET;
|
||||
v=(minLat+90)*(65535/180); bar[o++]=(v>>8)&0xff; bar[o++]=(v>>0)&0xff;
|
||||
v=(minLong+180)*(65535/360); bar[o++]=(v>>8)&0xff; bar[o++]=(v>>0)&0xff;
|
||||
v=(maxLat+90)*(65535/180); bar[o++]=(v>>8)&0xff; bar[o++]=(v>>0)&0xff;
|
||||
v=(maxLong+180)*(65535/360); bar[o++]=(v>>8)&0xff; bar[o++]=(v>>0)&0xff;
|
||||
v=(minLat+90)*(65535/180); bar->binary[o++]=(v>>8)&0xff; bar->binary[o++]=(v>>0)&0xff;
|
||||
v=(minLong+180)*(65535/360); bar->binary[o++]=(v>>8)&0xff; bar->binary[o++]=(v>>0)&0xff;
|
||||
v=(maxLat+90)*(65535/180); bar->binary[o++]=(v>>8)&0xff; bar->binary[o++]=(v>>0)&0xff;
|
||||
v=(maxLong+180)*(65535/360); bar->binary[o++]=(v>>8)&0xff; bar->binary[o++]=(v>>0)&0xff;
|
||||
|
||||
bar[RHIZOME_BAR_TTL_OFFSET]=0;
|
||||
bar->binary[RHIZOME_BAR_TTL_OFFSET]=0;
|
||||
|
||||
RETURN(0);
|
||||
OUT();
|
||||
}
|
||||
|
||||
uint64_t rhizome_bar_version(const unsigned char *bar)
|
||||
uint64_t rhizome_bar_version(const rhizome_bar_t *bar)
|
||||
{
|
||||
uint64_t version=0;
|
||||
int i;
|
||||
for(i=0;i<7;i++)
|
||||
version|=((uint64_t)(bar[RHIZOME_BAR_VERSION_OFFSET+6-i]))<<(8LL*i);
|
||||
version|=((uint64_t)(bar->binary[RHIZOME_BAR_VERSION_OFFSET+6-i]))<<(8LL*i);
|
||||
return version;
|
||||
}
|
||||
|
||||
/* This function only displays the first 8 bytes, and should not be used
|
||||
for comparison. */
|
||||
uint64_t rhizome_bar_bidprefix_ll(const unsigned char *bar)
|
||||
uint64_t rhizome_bar_bidprefix_ll(const rhizome_bar_t *bar)
|
||||
{
|
||||
uint64_t bidprefix=0;
|
||||
int i;
|
||||
for(i=0;i<8;i++)
|
||||
bidprefix|=((uint64_t)bar[RHIZOME_BAR_PREFIX_OFFSET+7-i])<<(8*i);
|
||||
bidprefix|=((uint64_t)bar->binary[RHIZOME_BAR_PREFIX_OFFSET+7-i])<<(8*i);
|
||||
return bidprefix;
|
||||
}
|
||||
|
||||
@ -306,28 +307,28 @@ next:
|
||||
struct overlay_buffer *payload = NULL;
|
||||
|
||||
// parse BAR's
|
||||
unsigned char *bars[50];
|
||||
const rhizome_bar_t *bars[50];
|
||||
int bar_count=0;
|
||||
while(ob_remaining(f->payload)>0 && bar_count<50){
|
||||
unsigned char *bar;
|
||||
bars[bar_count]=bar=ob_get_bytes_ptr(f->payload, RHIZOME_BAR_BYTES);
|
||||
const rhizome_bar_t *bar;
|
||||
bars[bar_count]=bar=(const rhizome_bar_t *)ob_get_bytes_ptr(f->payload, RHIZOME_BAR_BYTES);
|
||||
if (!bar){
|
||||
WARNF("Expected whole BAR @%zx (only %zd bytes remain)", ob_position(f->payload), ob_remaining(f->payload));
|
||||
break;
|
||||
}
|
||||
|
||||
// are we ignoring this manifest?
|
||||
if (rhizome_ignore_manifest_check(&bar[RHIZOME_BAR_PREFIX_OFFSET], RHIZOME_BAR_PREFIX_BYTES))
|
||||
if (rhizome_ignore_manifest_check(rhizome_bar_prefix(bar), RHIZOME_BAR_PREFIX_BYTES))
|
||||
continue;
|
||||
|
||||
// do we have free space in a fetch queue?
|
||||
unsigned char log2_size = bar[RHIZOME_BAR_FILESIZE_OFFSET];
|
||||
unsigned char log2_size = rhizome_bar_log_size(bar);
|
||||
if (log2_size!=0xFF && rhizome_fetch_has_queue_space(log2_size)!=1)
|
||||
continue;
|
||||
|
||||
uint64_t version = rhizome_bar_version(bar);
|
||||
// are we already fetching this bundle [or later]?
|
||||
rhizome_manifest *m=rhizome_fetch_search(&bar[RHIZOME_BAR_PREFIX_OFFSET], RHIZOME_BAR_PREFIX_BYTES);
|
||||
rhizome_manifest *m=rhizome_fetch_search(rhizome_bar_prefix(bar), RHIZOME_BAR_PREFIX_BYTES);
|
||||
if (m && m->version >= version)
|
||||
continue;
|
||||
|
||||
@ -367,8 +368,8 @@ next:
|
||||
payload = ob_new();
|
||||
}
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("Requesting manifest for BAR %s", alloca_tohex(bars[index], RHIZOME_BAR_BYTES));
|
||||
ob_append_bytes(payload, bars[index], RHIZOME_BAR_BYTES);
|
||||
DEBUGF("Requesting manifest for BAR %s", alloca_tohex_rhizome_bar_t(bars[index]));
|
||||
ob_append_bytes(payload, bars[index]->binary, RHIZOME_BAR_BYTES);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
struct bar_entry
|
||||
{
|
||||
unsigned char bar[RHIZOME_BAR_BYTES];
|
||||
rhizome_bar_t bar;
|
||||
unsigned tries;
|
||||
time_ms_t next_request;
|
||||
};
|
||||
@ -115,17 +115,17 @@ static void rhizome_sync_send_requests(struct subscriber *subscriber, struct rhi
|
||||
if (state->bars[i].next_request > now)
|
||||
continue;
|
||||
|
||||
unsigned char *prefix = &state->bars[i].bar[RHIZOME_BAR_PREFIX_OFFSET];
|
||||
unsigned char *prefix = rhizome_bar_prefix(&state->bars[i].bar);
|
||||
|
||||
if (rhizome_ignore_manifest_check(prefix, RHIZOME_BAR_PREFIX_BYTES))
|
||||
continue;
|
||||
|
||||
// do we have free space now in the appropriate fetch queue?
|
||||
unsigned char log2_size = state->bars[i].bar[RHIZOME_BAR_FILESIZE_OFFSET];
|
||||
unsigned char log2_size = rhizome_bar_log_size(&state->bars[i].bar);
|
||||
if (log2_size!=0xFF && rhizome_fetch_has_queue_space(log2_size)!=1)
|
||||
continue;
|
||||
|
||||
uint64_t version = rhizome_bar_version(state->bars[i].bar);
|
||||
uint64_t version = rhizome_bar_version(&state->bars[i].bar);
|
||||
// are we already fetching this bundle [or later]?
|
||||
rhizome_manifest *m=rhizome_fetch_search(prefix, RHIZOME_BAR_PREFIX_BYTES);
|
||||
if (m && m->version >= version){
|
||||
@ -147,16 +147,16 @@ static void rhizome_sync_send_requests(struct subscriber *subscriber, struct rhi
|
||||
break;
|
||||
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("Requesting manifest for BAR %s", alloca_tohex(state->bars[i].bar, RHIZOME_BAR_BYTES));
|
||||
DEBUGF("Requesting manifest for BAR %s", alloca_tohex_rhizome_bar_t(&state->bars[i].bar));
|
||||
|
||||
ob_append_bytes(payload, state->bars[i].bar, RHIZOME_BAR_BYTES);
|
||||
ob_append_bytes(payload, state->bars[i].bar.binary, RHIZOME_BAR_BYTES);
|
||||
|
||||
state->bars[i].tries--;
|
||||
state->bars[i].next_request = now+5000;
|
||||
if (!state->bars[i].tries){
|
||||
// remove this BAR and shift the last BAR down to this position if required.
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("Giving up on fetching BAR %s", alloca_tohex(state->bars[i].bar, RHIZOME_BAR_BYTES));
|
||||
DEBUGF("Giving up on fetching BAR %s", alloca_tohex_rhizome_bar_t(&state->bars[i].bar));
|
||||
state->bar_count --;
|
||||
if (i<state->bar_count)
|
||||
state->bars[i] = state->bars[state->bar_count];
|
||||
@ -196,23 +196,23 @@ static void rhizome_sync_send_requests(struct subscriber *subscriber, struct rhi
|
||||
|
||||
static int sync_bundle_inserted(struct subscriber *subscriber, void *context)
|
||||
{
|
||||
const unsigned char *bar = context;
|
||||
const rhizome_bar_t *bar = context;
|
||||
if (!subscriber->sync_state)
|
||||
return 0;
|
||||
|
||||
const unsigned char *id = &bar[RHIZOME_BAR_PREFIX_OFFSET];
|
||||
const unsigned char *id = rhizome_bar_prefix(bar);
|
||||
uint64_t version = rhizome_bar_version(bar);
|
||||
|
||||
struct rhizome_sync *state = subscriber->sync_state;
|
||||
int i;
|
||||
for (i=state->bar_count -1;i>=0;i--){
|
||||
unsigned char *this_bar = state->bars[i].bar;
|
||||
unsigned char *this_id = &this_bar[RHIZOME_BAR_PREFIX_OFFSET];
|
||||
rhizome_bar_t *this_bar = &state->bars[i].bar;
|
||||
unsigned char *this_id = rhizome_bar_prefix(this_bar);
|
||||
uint64_t this_version = rhizome_bar_version(this_bar);
|
||||
if (memcmp(this_id, id, RHIZOME_BAR_PREFIX_BYTES)==0 && version >= this_version){
|
||||
// remove this BAR and shift the last BAR down to this position if required.
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("Removing BAR %s from queue", alloca_tohex(this_bar, RHIZOME_BAR_BYTES));
|
||||
DEBUGF("Removing BAR %s from queue", alloca_tohex_rhizome_bar_t(this_bar));
|
||||
state->bar_count --;
|
||||
if (i<state->bar_count)
|
||||
state->bars[i] = state->bars[state->bar_count];
|
||||
@ -228,14 +228,14 @@ int rhizome_sync_bundle_inserted(const unsigned char *bar)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sync_cache_bar(struct rhizome_sync *state, unsigned char *bar, uint64_t token)
|
||||
static int sync_cache_bar(struct rhizome_sync *state, const rhizome_bar_t *bar, uint64_t token)
|
||||
{
|
||||
int ret=0;
|
||||
if (state->bar_count>=CACHE_BARS)
|
||||
return 0;
|
||||
// check the database before adding the BAR to the list
|
||||
if (token!=0 && rhizome_is_bar_interesting(bar)!=0){
|
||||
bcopy(bar, state->bars[state->bar_count].bar, RHIZOME_BAR_BYTES);
|
||||
state->bars[state->bar_count].bar = *bar;
|
||||
state->bars[state->bar_count].next_request = gettime_ms();
|
||||
state->bars[state->bar_count].tries = MAX_TRIES;
|
||||
state->bar_count++;
|
||||
@ -262,7 +262,7 @@ static void sync_process_bar_list(struct subscriber *subscriber, struct rhizome_
|
||||
{
|
||||
// find all interesting BARs in the payload and extend our sync range
|
||||
|
||||
unsigned char *bars[BARS_PER_RESPONSE];
|
||||
const rhizome_bar_t *bars[BARS_PER_RESPONSE];
|
||||
uint64_t bar_tokens[BARS_PER_RESPONSE];
|
||||
int bar_count = 0;
|
||||
int has_before=0, has_after=0;
|
||||
@ -278,12 +278,12 @@ static void sync_process_bar_list(struct subscriber *subscriber, struct rhizome_
|
||||
|
||||
while(ob_remaining(b)>0 && bar_count < BARS_PER_RESPONSE){
|
||||
bar_tokens[bar_count]=ob_get_packed_ui64(b);
|
||||
bars[bar_count]=ob_get_bytes_ptr(b, RHIZOME_BAR_BYTES);
|
||||
bars[bar_count]=(const rhizome_bar_t *)ob_get_bytes_ptr(b, RHIZOME_BAR_BYTES);
|
||||
if (!bars[bar_count])
|
||||
break;
|
||||
// allow the sender to identify the edge of the range this packet represents
|
||||
// even if there is no manifest that falls exactly on the boundary (eg deleted manifest or zero lower bound)
|
||||
if (is_all_matching(bars[bar_count], RHIZOME_BAR_BYTES, 0))
|
||||
if (rhizome_is_bar_none(bars[bar_count]))
|
||||
bars[bar_count]=NULL;
|
||||
|
||||
// track the highest BAR we've seen, even if we can't sync it yet, so we know what BARs to request.
|
||||
|
@ -44,6 +44,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#define RHIZOME_CRYPT_KEY_BYTES crypto_stream_xsalsa20_ref_KEYBYTES
|
||||
#define RHIZOME_CRYPT_KEY_STRLEN (RHIZOME_CRYPT_KEY_BYTES * 2)
|
||||
|
||||
#define RHIZOME_BAR_BYTES 32
|
||||
#define RHIZOME_BAR_PREFIX_BYTES 15
|
||||
#define RHIZOME_BAR_PREFIX_OFFSET 0
|
||||
#define RHIZOME_BAR_FILESIZE_OFFSET 15
|
||||
#define RHIZOME_BAR_VERSION_OFFSET 16
|
||||
#define RHIZOME_BAR_GEOBOX_OFFSET 23
|
||||
|
||||
// TODO Rename MANIFEST_ID to BUNDLE_ID
|
||||
// The following constants are deprecated, use the BUNDLE_ID forms instead
|
||||
#define RHIZOME_MANIFEST_ID_BYTES RHIZOME_BUNDLE_ID_BYTES
|
||||
@ -104,6 +111,18 @@ int cmp_rhizome_bk_t(const rhizome_bk_t *a, const rhizome_bk_t *b);
|
||||
int str_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex);
|
||||
int strn_to_rhizome_bk_t(rhizome_bk_t *bk, const char *hex, const char **endp);
|
||||
|
||||
typedef struct rhizome_bar_binary {
|
||||
unsigned char binary[RHIZOME_BAR_BYTES];
|
||||
} rhizome_bar_t;
|
||||
|
||||
#define rhizome_bar_prefix(X) (&(X)->binary[RHIZOME_BAR_PREFIX_OFFSET])
|
||||
#define alloca_tohex_rhizome_bar_prefix(X) alloca_tohex(&(X)->binary[RHIZOME_BAR_PREFIX_OFFSET], RHIZOME_BAR_PREFIX_BYTES)
|
||||
#define alloca_tohex_rhizome_bar_t(X) alloca_tohex((X)->binary, RHIZOME_BAR_BYTES)
|
||||
#define rhizome_bar_log_size(X) ((unsigned char)(X)->binary[RHIZOME_BAR_FILESIZE_OFFSET])
|
||||
#define rhizome_is_bar_none(X) is_all_matching((X)->binary, RHIZOME_BAR_BYTES, 0)
|
||||
uint64_t rhizome_bar_version(const rhizome_bar_t *bar);
|
||||
uint64_t rhizome_bar_bidprefix_ll(const rhizome_bar_t *bar);
|
||||
|
||||
/* Fundamental data type: Rhizome payload size
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
|
Loading…
Reference in New Issue
Block a user