servlib/bundling/BPQBlock.cc
changeset 65 333724f2f7cf
parent 64 1296a0283271
child 66 e1101c5d54a1
--- a/servlib/bundling/BPQBlock.cc	Mon Oct 24 18:28:33 2011 +0100
+++ b/servlib/bundling/BPQBlock.cc	Wed Oct 26 13:33:11 2011 +0100
@@ -207,7 +207,7 @@
 BPQBlock::initialise(BlockInfo* block, bool created_locally, const Bundle* bundle)
 {
 #define TRY(fn) 		\
-    if(!fn) { 			\
+    if(fn != BP_SUCCESS) { 			\
         return BP_FAIL; \
     }
 
@@ -221,14 +221,8 @@
 	TRY (extract_kind(buf, &buf_index, buf_length));
 	TRY (extract_matching_rule(buf, &buf_index, buf_length));
 
-	if (created_locally) {
-		creation_ts_.seconds_ 	= bundle->creation_ts().seconds_;
-		creation_ts_.seqno_		= bundle->creation_ts().seqno_;
-		source_.assign(bundle->source());
-	} else {
-		TRY (extract_creation_ts(buf, &buf_index, buf_length));
-		TRY (extract_source(buf, &buf_index, buf_length));
-	}
+	TRY (extract_creation_ts(buf, &buf_index, buf_length, created_locally, bundle));
+	TRY (extract_source(buf, &buf_index, buf_length, created_locally, bundle));
 
 	TRY (extract_query(buf, &buf_index, buf_length));
 	TRY (extract_fragments(buf, &buf_index, buf_length));
@@ -276,10 +270,10 @@
             block->writable_contents()->buf_len());
     }
 
-    if ( *(block->data()) != KIND_QUERY 		||
-         *(block->data()) != KIND_RESPONSE 		||
-         *(block->data()) != KIND_RESPONSE_DO_NOT_CACHE_FRAG ) {
-        log_err("BPQBlock: block->data() = %c (should be 0|1|2)",
+    if ( *(block->data()) != KIND_QUERY 		&&
+    	 *(block->data()) != KIND_RESPONSE 		&&
+    	 *(block->data()) != KIND_RESPONSE_DO_NOT_CACHE_FRAG ) {
+        log_err("BPQBlock: block->data() = %d (should be 0|1|2)",
             *(block->data()));
     }
 }
@@ -289,7 +283,7 @@
 BPQBlock::extract_kind (const u_char* buf, u_int* buf_index, u_int buf_length)
 {
 	if ( *buf_index < buf_length ) {
-	    kind_ = (kind_t) buf[*buf_index++];
+	    kind_ = (kind_t) buf[(*buf_index)++];
 	    log_debug("BPQBlock::extract_kind: kind = %d", kind_);
 	    return BP_SUCCESS;
 
@@ -304,7 +298,7 @@
 BPQBlock::extract_matching_rule (const u_char* buf, u_int* buf_index, u_int buf_length)
 {
 	if ( *buf_index < buf_length ) {
-	    matching_rule_ = (kind_t) buf[*buf_index++];
+	    matching_rule_ = (kind_t) buf[(*buf_index)++];
 	    log_debug("BPQBlock::extract_matching_rule: matching rule = %d", matching_rule_);
 	    return BP_SUCCESS;
 
@@ -316,12 +310,53 @@
 
 //----------------------------------------------------------------------
 int
-BPQBlock::extract_creation_ts (const u_char* buf, u_int* buf_index, u_int buf_length)
+BPQBlock::extract_creation_ts (	const u_char* buf,
+								u_int* buf_index,
+								u_int buf_length,
+								bool local,
+								const Bundle* bundle)
 {
 	int decoding_len = 0;
+
+	if (local) {
+		u_int temp;
+
+		creation_ts_.seconds_ 	= bundle->creation_ts().seconds_;
+		creation_ts_.seqno_		= bundle->creation_ts().seqno_;
+
+		log_debug("BPQBlock::extract_creation_ts: bundle was locally created");
+		log_debug("\t timestamp seconds = %llu", creation_ts_.seconds_);
+		log_debug("\t timestamp sequence number = %llu", creation_ts_.seqno_);
+
+		// increment buf_index accordingly
+		if (*buf_index < buf_length &&
+			(decoding_len = SDNV::decode(	&(buf[*buf_index]),
+											buf_length - (*buf_index),
+											&temp)) >= 0) {
+			(*buf_index) += decoding_len;
+		} else {
+			log_err("Error decoding timestamp seconds");
+			return BP_FAIL;
+		}
+
+
+		// increment buf_index accordingly
+		if (*buf_index < buf_length &&
+			(decoding_len = SDNV::decode(	&(buf[*buf_index]),
+											buf_length - (*buf_index),
+											&temp)) >= 0) {
+			(*buf_index) += decoding_len;
+		} else {
+			log_err("Error decoding timestamp sequence number");
+			return BP_FAIL;
+		}
+
+		return BP_SUCCESS;
+	}
+
 	if (*buf_index < buf_length &&
 		(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-				 	 	 	 	 	 	buf_length - *buf_index,
+				 	 	 	 	 	 	buf_length - (*buf_index),
 				 	 	 	 	 	 	&(creation_ts_.seconds_)) ) >= 0 ) {
 		*buf_index += decoding_len;
 		log_debug("BPQBlock::extract_creation_ts: timestamp seconds = %llu",
@@ -334,7 +369,7 @@
 
 	if (*buf_index < buf_length &&
 		(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-										buf_length - *buf_index,
+										buf_length - (*buf_index),
 										&(creation_ts_.seqno_)) ) >= 0 ) {
 		*buf_index += decoding_len;
 		log_debug("BPQBlock::extract_creation_ts: timestamp sequence number = %llu",
@@ -349,26 +384,52 @@
 
 //----------------------------------------------------------------------
 int
-BPQBlock::extract_source (const u_char* buf, u_int* buf_index, u_int buf_length)
+BPQBlock::extract_source (	const u_char* buf,
+							u_int* buf_index,
+							u_int buf_length,
+							bool local,
+							const Bundle* bundle)
 {
 	int decoding_len = 0;
 	u_int src_eid_len = 0;
 
+	if (local) {
+		source_.assign(bundle->source());
+
+		log_debug("BPQBlock::extract_source: bundle was locally created");
+		log_debug("\t Source EID length = %u", source_.length());
+		log_debug("\t Source EID = %s", source_.c_str());
+
+		// increment buf_index accordingly
+		if (*buf_index < buf_length &&
+			(decoding_len = SDNV::decode(	&(buf[*buf_index]),
+											buf_length - (*buf_index),
+											&src_eid_len)) >= 0) {
+			(*buf_index) += decoding_len;
+			(*buf_index) += src_eid_len;
+		} else {
+			log_err("Error decoding Source EID length");
+			return BP_FAIL;
+		}
+
+		return BP_SUCCESS;
+	}
+
 	if (*buf_index < buf_length &&
 		(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-										buf_length - *buf_index,
+										buf_length - (*buf_index),
 										&src_eid_len)) >= 0 ) {
-		*buf_index += decoding_len;
+		(*buf_index) += decoding_len;
 		log_debug("BPQBlock::extract_source: Source EID length = %u", src_eid_len);
 	} else {
 		log_err("Error decoding Source EID length");
 		return BP_FAIL;
 	}
 
-    if ((*buf_index + src_eid_len) < buf_length  &&
+    if (((*buf_index) + src_eid_len) < buf_length  &&
         source_.assign((const char*) &(buf[*buf_index]), src_eid_len) ) {
 
-    	*buf_index += src_eid_len;
+    	(*buf_index) += src_eid_len;
     	log_debug("BPQBlock::extract_source: Source EID = %s", source_.c_str());
 
     } else {
@@ -387,21 +448,26 @@
 
 	if (*buf_index < buf_length &&
 		(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-										buf_length - *buf_index,
+										buf_length - (*buf_index),
 										&query_len_)) >= 0 ) {
-		*buf_index += decoding_len;
+		(*buf_index) += decoding_len;
 		log_debug("BPQBlock::extract_query: query length = %u", query_len_);
     } else {
         log_err("Error decoding BPQ query length");
         return BP_FAIL;
 	}
 
-    if ((*buf_index + query_len_) < buf_length) {
+	if (query_len_ <= 0) {
+		log_warn("BPQBlock::extract_query: No query found in block");
+	}
+
+    if (((*buf_index) + query_len_) < buf_length) {
+
     	query_val_ = (u_char*) malloc ( sizeof(u_char) * query_len_ );
 
     	memcpy(query_val_, &(buf[*buf_index]), query_len_);
 
-    	*buf_index += query_len_;
+    	(*buf_index) += query_len_;
     	log_debug("BPQBlock::extract_query: query value = %s", query_val_);
 
     } else {
@@ -424,9 +490,9 @@
 
 	if (*buf_index < buf_length &&
 		(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-										buf_length - *buf_index,
+										buf_length - (*buf_index),
 										&frag_count)) >= 0 ) {
-		*buf_index += decoding_len;
+		(*buf_index) += decoding_len;
 		log_debug("BPQBlock::extract_fragments: number of fragments = %u", frag_count);
     } else {
         log_err("Error decoding number of fragments");
@@ -437,9 +503,9 @@
 
 		if (*buf_index < buf_length &&
 			(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-											buf_length - *buf_index,
+											buf_length - (*buf_index),
 											&frag_off)) >= 0 ) {
-			*buf_index += decoding_len;
+			(*buf_index) += decoding_len;
 			log_debug("BPQBlock::extract_fragments: fragment [%u] offset = %u", i, frag_off);
 	    } else {
 	        log_err("Error decoding fragment [%u] offset", i);
@@ -448,9 +514,9 @@
 
 		if (*buf_index < buf_length &&
 			(decoding_len = SDNV::decode(	&(buf[*buf_index]),
-											buf_length - *buf_index,
+											buf_length - (*buf_index),
 											&frag_len)) >= 0 ) {
-			*buf_index += decoding_len;
+			(*buf_index) += decoding_len;
 			log_debug("BPQBlock::extract_fragments: fragment [%u] length = %u", i, frag_len);
 	    } else {
 	        log_err("Error decoding fragment [%u] length", i);