diff --git a/rhizome_packetformats.c b/rhizome_packetformats.c index 48204f11..972a78f1 100644 --- a/rhizome_packetformats.c +++ b/rhizome_packetformats.c @@ -157,24 +157,9 @@ int overlay_rhizome_add_advertisements(struct decode_context *context, int inter if (slots<1) { RETURN(WHY("No room for node advertisements")); } - if (overlay_frame_build_header(context, e, - 0, OF_TYPE_RHIZOME_ADVERT, 0, 1, - NULL, NULL, - NULL, my_subscriber)) - return -1; - /* Randomly choose whether to advertise manifests or BARs first. */ int skipmanifests=random()&1; - /* Version of rhizome advert block (1 byte): - 1 = manifests then BARs, - 2 = BARs only, - 3 = HTTP port then manifests then BARs, - 4 = HTTP port then BARs only - */ - ob_append_byte(e,3+skipmanifests); - /* Rhizome HTTP server port number (2 bytes) */ - ob_append_ui16(e, rhizome_http_server_port); - + /* XXX Should add priority bundles here. XXX Should prioritise bundles for subscribed groups, Serval-authorised files etc over common bundles. @@ -211,6 +196,32 @@ int overlay_rhizome_add_advertisements(struct decode_context *context, int inter sqlite3_stmt *statement=NULL; sqlite3_blob *blob=NULL; + ob_checkpoint(e); + + if (overlay_frame_build_header(context, e, + 0, OF_TYPE_RHIZOME_ADVERT, 0, 1, + NULL, NULL, + NULL, my_subscriber)){ + ob_rewind(e); + return -1; + } + + /* Version of rhizome advert block (1 byte): + 1 = manifests then BARs, + 2 = BARs only, + 3 = HTTP port then manifests then BARs, + 4 = HTTP port then BARs only + */ + if (ob_append_byte(e,3+skipmanifests)){ + ob_rewind(e); + return -1; + } + /* Rhizome HTTP server port number (2 bytes) */ + if (ob_append_ui16(e, rhizome_http_server_port)){ + ob_rewind(e); + return -1; + } + for(pass=skipmanifests;pass<2;pass++) { ob_checkpoint(e); switch(pass) { @@ -223,8 +234,10 @@ int overlay_rhizome_add_advertisements(struct decode_context *context, int inter } if (!statement) { sqlite_set_debugmask(oldmask); - RETURN(WHY("Could not prepare sql statement for fetching BARs for advertisement")); + WHY("Could not prepare sql statement for fetching BARs for advertisement"); + goto stopStuffing; } + while( sqlite_step_retry(&retry, statement) == SQLITE_ROW && e->position+RHIZOME_BAR_BYTES<=e->sizeLimit ) {