updates to BPQ parsing
authoraidan
Wed, 26 Oct 2011 13:33:11 +0100
changeset 65 333724f2f7cf
parent 64 1296a0283271
child 66 e1101c5d54a1
updates to BPQ parsing
applib/dtn_types.h
apps/dtnquery/dtnquery.c
apps/dtnrespond/dtnrespond.c
servlib/bundling/BPQBlock.cc
servlib/bundling/BPQBlock.h
--- a/applib/dtn_types.h	Mon Oct 24 18:28:33 2011 +0100
+++ b/applib/dtn_types.h	Wed Oct 26 13:33:11 2011 +0100
@@ -263,7 +263,7 @@
  */
 
 enum dtn_bpq_extension_block_matching_rule_t {
-    BPQ_MATCHING_RULE_EXACT = 1, 
+    BPQ_MATCHING_RULE_EXACT = 0x00,
 };
 typedef enum dtn_bpq_extension_block_matching_rule_t dtn_bpq_extension_block_matching_rule_t;
 
--- a/apps/dtnquery/dtnquery.c	Mon Oct 24 18:28:33 2011 +0100
+++ b/apps/dtnquery/dtnquery.c	Wed Oct 26 13:33:11 2011 +0100
@@ -94,7 +94,7 @@
     fprintf(stderr, " -h  help\n");
     fprintf(stderr, " -v  verbose\n");
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -161,7 +161,7 @@
     token = strtok (NULL, ",");
     if (token != NULL)  return 1;
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -374,10 +374,10 @@
     }
 
     // if no reply-to eid set, use the src eid
-    if (reply_eid_name == NULL)
+    if (*reply_eid_name == NULL)
         strncpy(reply_eid_name, src_eid_name, PATH_MAX);
     
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -436,9 +436,9 @@
     REQUIRE(matching_rule == BPQ_MATCHING_RULE_EXACT,
          "-u <matching rule> invalid rule\n");
     REQUIRE(bundle_expiry > 0, "-e <expiry> must be a positive integer\n");
+
+    return DTN_SUCCESS;
 #undef REQUIRE
-//todo: check this is ok
-    return 0;
 }
 
 /*******************************************************************************
@@ -587,7 +587,7 @@
     if (verbose) printf("%d byte file from [%s]: transit time=%d ms, written to '%s'\n",
            (int)fileinfo.st_size, bundle_spec.source.uri, 0, destination);
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -1240,6 +1240,6 @@
     dtn_close(handle);
     if (verbose) fprintf(stdout, "closed connection to dtn router...\n");
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
--- a/apps/dtnrespond/dtnrespond.c	Mon Oct 24 18:28:33 2011 +0100
+++ b/apps/dtnrespond/dtnrespond.c	Wed Oct 26 13:33:11 2011 +0100
@@ -78,7 +78,7 @@
     fprintf(stderr, " -h  help\n");
     fprintf(stderr, " -v  verbose\n");
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -220,7 +220,7 @@
     if (*reply_eid_name == NULL)
         strncpy(reply_eid_name, local_eid_name, PATH_MAX);
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -403,8 +403,7 @@
     for (j = strlen(out)-1; strchr(whitespace, in[i]) != NULL; --j)
     	out[j--] = 0;
 
-    return 0;
-
+    return DTN_SUCCESS;
 }   
 
 /*******************************************************************************
@@ -464,7 +463,7 @@
 		return -1;
     } else {
     	fclose (file);
-		return 0;
+		return DTN_SUCCESS;
     }
 }
 
@@ -504,14 +503,18 @@
     	// TODO: handle malformed input from matching file
     	if (fgets(line, PATH_MAX, file) == NULL) {
     		break;
-    	} else {
-    		matching_rule 	= strtok(line, ",");
-    		query 			= strtok(NULL, ",");
-    		path 	        = strtok(NULL, ",");
-    		kind  			= strtok(NULL, ",");
-    		expiry			= strtok(NULL, ",");
     	}
 
+    	if (strncmp(line, "#", 1) == 0)
+    		continue;
+
+    	matching_rule 	= strtok(line, ",");
+		query 			= strtok(NULL, ",");
+		path 	        = strtok(NULL, ",");
+		kind  			= strtok(NULL, ",");
+		expiry			= strtok(NULL, ",");
+
+
     	// match query
         if (atoi(matching_rule) != (int)bpq->matching_rule		||
 			BPQ_MATCHING_RULE_EXACT != bpq->matching_rule		||
@@ -539,7 +542,7 @@
     }
     fclose (file);
 
-    return 0;
+    return DTN_SUCCESS;
 }
 
 /*******************************************************************************
@@ -1120,7 +1123,7 @@
     dtn_close(handle);
     if (verbose) fprintf(stdout, "closed connection to dtn router...\n");
 
-    return 0;
+    return DTN_SUCCESS;
 
 // UNREACHABLE CODE if count = 0 //////////////////////////////////////////////
 }
--- 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);
--- a/servlib/bundling/BPQBlock.h	Mon Oct 24 18:28:33 2011 +0100
+++ b/servlib/bundling/BPQBlock.h	Wed Oct 26 13:33:11 2011 +0100
@@ -90,8 +90,8 @@
 
     int extract_kind			(const u_char* buf, u_int* buf_index, u_int buf_length);
     int extract_matching_rule	(const u_char* buf, u_int* buf_index, u_int buf_length);
-    int extract_creation_ts		(const u_char* buf, u_int* buf_index, u_int buf_length);
-    int extract_source			(const u_char* buf, u_int* buf_index, u_int buf_length);
+    int extract_creation_ts		(const u_char* buf, u_int* buf_index, u_int buf_length, bool local, const Bundle* bundle);
+    int extract_source			(const u_char* buf, u_int* buf_index, u_int buf_length, bool local, const Bundle* bundle);
     int extract_query			(const u_char* buf, u_int* buf_index, u_int buf_length);
     int extract_fragments		(const u_char* buf, u_int* buf_index, u_int buf_length);