Minor code cleanup / refactoring

This commit is contained in:
Jeremy Lakeman 2013-07-12 10:56:24 +09:30
parent add895fd97
commit 2f32635b87
2 changed files with 35 additions and 27 deletions

View File

@ -29,24 +29,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "crypto.h"
#include "log.h"
int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
int rhizome_mdp_send_block(struct subscriber *dest, unsigned char *id, uint64_t version, uint64_t fileOffset, uint32_t bitmap, uint16_t blockLength)
{
IN();
uint64_t version=
read_uint64(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES]);
uint64_t fileOffset=
read_uint64(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8]);
uint32_t bitmap=
read_uint32(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8+8]);
uint16_t blockLength=
read_uint16(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8+8+4]);
if (blockLength>1024) RETURN(-1);
struct subscriber *source = find_subscriber(mdp->out.src.sid, SID_SIZE, 0);
char *id_str = alloca_tohex_bid(id);
if (config.debug.rhizome_tx)
DEBUGF("Requested blocks for %s @%llx", alloca_tohex_bid(&mdp->out.payload[0]), fileOffset);
DEBUGF("Requested blocks for %s @%llx", id_str, fileOffset);
/* Find manifest that corresponds to BID and version.
If we don't have this combination, then do nothing.
@ -58,7 +51,7 @@ int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
*/
char filehash[SHA512_DIGEST_STRING_LENGTH];
if (rhizome_database_filehash_from_id(alloca_tohex_bid(mdp->out.payload), version, filehash)<=0)
if (rhizome_database_filehash_from_id(id_str, version, filehash)<=0)
RETURN(-1);
struct rhizome_read read;
@ -83,10 +76,10 @@ int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
reply.out.src.port=MDP_PORT_RHIZOME_RESPONSE;
int send_broadcast=1;
if (source){
if (!(source->reachable&REACHABLE_DIRECT))
if (dest){
if (!(dest->reachable&REACHABLE_DIRECT))
send_broadcast=0;
if (source->reachable&REACHABLE_UNICAST && source->interface && source->interface->prefer_unicast)
if (dest->reachable&REACHABLE_UNICAST && dest->interface && dest->interface->prefer_unicast)
send_broadcast=0;
}
@ -97,18 +90,16 @@ int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
reply.out.ttl=1;
}else{
// if we get a request from a peer that we can only talk to via unicast, send data via unicast too.
bcopy(mdp->out.src.sid,reply.out.dst.sid,SID_SIZE);
reply.out.ttl=64;
bcopy(dest->sid, reply.out.dst.sid, SID_SIZE);
}
reply.out.dst.port=MDP_PORT_RHIZOME_RESPONSE;
reply.out.queue=OQ_OPPORTUNISTIC;
reply.out.payload[0]='B'; // reply contains blocks
// include 16 bytes of BID prefix for identification
bcopy(&mdp->out.payload[0],&reply.out.payload[1],16);
bcopy(id, &reply.out.payload[1], 16);
// and version of manifest
bcopy(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES],
&reply.out.payload[1+16],8);
bcopy(&version, &reply.out.payload[1+16], sizeof(uint64_t));
int i;
for(i=0;i<32;i++){
@ -149,6 +140,22 @@ int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
OUT();
}
int overlay_mdp_service_rhizomerequest(overlay_mdp_frame *mdp)
{
uint64_t version=
read_uint64(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES]);
uint64_t fileOffset=
read_uint64(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8]);
uint32_t bitmap=
read_uint32(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8+8]);
uint16_t blockLength=
read_uint16(&mdp->out.payload[RHIZOME_MANIFEST_ID_BYTES+8+8+4]);
struct subscriber *source = find_subscriber(mdp->out.src.sid, SID_SIZE, 0);
return rhizome_mdp_send_block(source, &mdp->out.payload[0], version, fileOffset, bitmap, blockLength);
}
int overlay_mdp_service_rhizomeresponse(overlay_mdp_frame *mdp)
{
IN();

View File

@ -233,6 +233,9 @@ end:
schedule(alarm);
}
#define HAS_PORT (1<<1)
#define HAS_MANIFESTS (1<<0)
/* Queue an advertisment for a single manifest */
int rhizome_advertise_manifest(rhizome_manifest *m){
struct overlay_frame *frame = malloc(sizeof(struct overlay_frame));
@ -244,7 +247,7 @@ int rhizome_advertise_manifest(rhizome_manifest *m){
frame->payload = ob_new();
ob_limitsize(frame->payload, 800);
if (ob_append_byte(frame->payload, 3)) goto error;
if (ob_append_byte(frame->payload, HAS_PORT|HAS_MANIFESTS)) goto error;
if (ob_append_ui16(frame->payload, is_rhizome_http_enabled()?rhizome_http_server_port:0)) goto error;
if (ob_append_ui16(frame->payload, m->manifest_all_bytes)) goto error;
if (ob_append_bytes(frame->payload, m->manifestdata, m->manifest_all_bytes)) goto error;
@ -273,15 +276,14 @@ int overlay_rhizome_saw_advertisements(int i, struct overlay_frame *f, long long
httpaddr.sin_port = htons(RHIZOME_HTTP_PORT);
int manifest_length;
rhizome_manifest *m=NULL;
char httpaddrtxt[INET_ADDRSTRLEN];
int (*oldfunc)() = sqlite_set_tracefunc(is_debug_rhizome_ads);
if (ad_frame_type & 2){
if (ad_frame_type & HAS_PORT){
httpaddr.sin_port = htons(ob_get_ui16(f->payload));
}
if (ad_frame_type & 1){
if (ad_frame_type & HAS_MANIFESTS){
/* Extract whole manifests */
while(f->payload->position < f->payload->sizeLimit) {
if (ob_getbyte(f->payload, f->payload->position)==0xff){
@ -294,7 +296,6 @@ int overlay_rhizome_saw_advertisements(int i, struct overlay_frame *f, long long
unsigned char *data = ob_get_bytes_ptr(f->payload, manifest_length);
if (!data) {
assert(inet_ntop(AF_INET, &httpaddr.sin_addr, httpaddrtxt, sizeof(httpaddrtxt)) != NULL);
WHYF("Illegal manifest length field in rhizome advertisement frame %d vs %d.",
manifest_length, f->payload->sizeLimit - f->payload->position);
break;