Added some more logging to the BPQ apps
authoraidan
Tue, 31 May 2011 18:20:22 +0100
changeset 8 ce3cb8a86959
parent 7 0a3c1a78bf75
child 9 b72ba16e5586
Added some more logging to the BPQ apps
apps/dtnquery/dtnquery.c
apps/dtnrespond/dtnrespond.c
--- a/apps/dtnquery/dtnquery.c	Tue May 31 17:35:55 2011 +0100
+++ b/apps/dtnquery/dtnquery.c	Tue May 31 18:20:22 2011 +0100
@@ -600,10 +600,13 @@
 * @return The number of bytes or -1 on error
 *******************************************************************************/
 int
-bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq, char* buf, size_t buf_len)
+bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq,
+    char* buf,
+    size_t buf_len,
+    int verbose)
 {
     int i=0, j=0, k=0;
-    int encoding_len;    
+    int q_encoding_len, f_encoding_len, encoding_len;    
     char encoding[PATH_MAX];
 
     memset(buf, 0, buf_len);
@@ -615,9 +618,9 @@
     if (i < buf_len)    buf[i++] = (char) bpq->matching_rule;
 
     // BPQ-value-length     SDNV
-    if ( (encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 )
+    if ( (q_encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 )
         return -1;
-    for (j=0; i<buf_len && j<encoding_len; ++j)
+    for (j=0; i<buf_len && j<q_encoding_len; ++j)
         buf[i++] = encoding[j];
 
     // BPQ-value            n-bytes
@@ -625,11 +628,10 @@
         buf[i++] = bpq->query.query_val[j];
 
     // number of fragments  SDNV
-    if ( (encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 )
+    if ( (f_encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 )
         return -1;
-    for (j=0; i<buf_len && j<encoding_len; ++j)
+    for (j=0; i<buf_len && j<f_encoding_len; ++j)
         buf[i++] = encoding[j];
-
     
     for (k=0; k<bpq->fragments.num_frag_returned; ++k) {
 
@@ -646,6 +648,16 @@
             buf[i++] = encoding[j];
     }
 
+    if (verbose) {
+        fprintf (stdout, "\nbpq_to_char_array (buf_len:%d, i:%d):\n",buf_len,i);
+        fprintf (stdout, "             kind: %d\n", (int) buf[0]);
+        fprintf (stdout, "    matching rule: %d\n", (int) buf[1]);
+        fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
+        fprintf (stdout, "   q_encoding_len: %d\n", q_encoding_len);
+        fprintf (stdout, "        query val: %s\n", bpq->query.query_val);
+        fprintf (stdout, "     fragment len: %d\n", bpq->fragments.num_frag_returned);
+        fprintf (stdout, "   f_encoding_len: %d\n\n", f_encoding_len);
+    }
     
     return i;
 }
@@ -663,10 +675,13 @@
 * @return DTN_SUCCESS or -1 on error
 *******************************************************************************/
 int
-char_array_to_bpq(const char* buf, size_t buf_len, dtn_bpq_extension_block_data_t * bpq) 
+char_array_to_bpq(const char* buf,
+    size_t buf_len,
+    dtn_bpq_extension_block_data_t * bpq,
+    int verbose) 
 {
     int i=0, j=0;
-    int decoding_len=0;
+    int q_decoding_len, f_decoding_len, decoding_len;
 
     // BPQ-kind             1-byte
     if (i<buf_len) bpq->kind = (u_int) buf[i++];
@@ -675,18 +690,18 @@
     if (i<buf_len) bpq->matching_rule = (u_int) buf[i++];
 
     // BPQ-value-length     SDNV
-    if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->query.query_len))) == -1 )
+    if ( (q_decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->query.query_len))) == -1 )
         return -1;
-    i += decoding_len;
+    i += q_decoding_len;
 
     // BPQ-value            n-bytes
     if (i<buf_len) bpq->query.query_val = &(buf[i]);
     i += bpq->query.query_len;
 
     // number of fragments  SDNV
-    if ( (decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 )
+    if ( (f_decoding_len = sdnv_decode (buf[i], buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 )
         return -1;
-    i += decoding_len;
+    i += f_decoding_len;
 
     for (j=0; i<buf_len && j<bpq->fragments.num_frag_returned; ++j) {
 
@@ -704,6 +719,17 @@
     if (i != buf_len)
         return -1;
 
+    if (verbose) {
+        fprintf (stdout, "\nchar_array_to_bpq (buf_len:%d, i:%d):\n",buf_len, i);
+        fprintf (stdout, "             kind: %d\n", (int) buf[0]);
+        fprintf (stdout, "    matching rule: %d\n", (int) buf[1]);
+        fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
+        fprintf (stdout, "   q_decoding_len: %d\n", q_decoding_len);
+        fprintf (stdout, "        query val: %s\n", bpq->query.query_val);
+        fprintf (stdout, "     fragment len: %d\n", bpq->fragments.num_frag_returned);
+        fprintf (stdout, "   f_decoding_len: %d\n\n", f_decoding_len);
+    }
+
     return DTN_SUCCESS;
 }
 
@@ -749,7 +775,7 @@
     bpq_block_data.fragments.frag_offsets = NULL;
     bpq_block_data.fragments.frag_lenghts = NULL;
 
-    buf_len = bpq_to_char_array(&bpq_block_data, buf, PATH_MAX);
+    buf_len = bpq_to_char_array(&bpq_block_data, buf, PATH_MAX, verbose);
 
     // set the bpq block
     bpq_block.type = DTN_BPQ_BLOCK_TYPE;
@@ -802,11 +828,14 @@
     const char * filename,
     int verbose)
 {
-    int ret = 0;
-    dtn_bundle_spec_t bundle_spec;
-    dtn_bundle_payload_t payload;
+    int ret = 0, num_blocks, i;
+    dtn_bundle_spec_t               bundle_spec;
+    dtn_extension_block_t*          bpq_blocks;
+    dtn_bpq_extension_block_data_t  bpq_block_data;
+    dtn_bundle_payload_t            payload;
 
     memset(&bundle_spec, 0, sizeof(bundle_spec));
+    memset(&bpq_block_data, 0, sizeof(dtn_bpq_extension_block_data_t));
     memset(&payload, 0, sizeof(payload));
 
     // recv the bpq bundle
@@ -823,6 +852,40 @@
                              bundle_spec.creation_ts.seqno);
     }
 
+    // extract the bpq
+    num_blocks = bundle_spec.blocks.blocks_len;
+    bpq_blocks = bundle_spec.blocks.blocks_val;
+
+    for (i = 0; i < num_blocks; ++i) {
+        if (bpq_blocks[i].type == DTN_BPQ_BLOCK_TYPE) {
+
+            if (verbose) fprintf(stdout, "bundle contains a "
+                                         "BPQ extension block\n");
+            
+            if (verbose) fprintf(stdout, "BPQ query_len: %d)\n", (int)  bpq_blocks[i].data.data_len);
+            if (verbose) fprintf(stdout, "BPQ query_val: %s)\n", (char*)bpq_blocks[i].data.data_val);
+
+            if ( bpq_blocks[i].data.data_len <= 0 || bpq_blocks[i].data.data_val == NULL) {
+                fprintf(stderr, "error decoding query bundle: %d\n", ret);
+                return -1;
+            }
+                
+            ret = char_array_to_bpq(bpq_blocks[i].data.data_val,
+                                    bpq_blocks[i].data.data_len,
+                                    &bpq_block_data,
+                                    verbose);
+            if (ret != DTN_SUCCESS) {
+                fprintf(stderr, "error decoding query bundle: %d\n", ret);
+                return ret;
+            }
+
+//           if (verbose) fprintf(stdout, "BPQ query(%s)\n", bpq_block_data.query.query_val);
+
+            break;
+        }
+    }
+
+
     // handle the payload file
     ret = handle_file_transfer(bundle_spec, payload, filename, verbose);
     if (ret != DTN_SUCCESS) {
--- a/apps/dtnrespond/dtnrespond.c	Tue May 31 17:35:55 2011 +0100
+++ b/apps/dtnrespond/dtnrespond.c	Tue May 31 18:20:22 2011 +0100
@@ -514,10 +514,11 @@
 int
 bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq,
     char* buf,
-    size_t buf_len)
+    size_t buf_len,
+    int verbose)
 {
     int i=0, j=0, k=0;
-    int encoding_len;
+    int q_encoding_len, f_encoding_len, encoding_len;
     char encoding[PATH_MAX];
 
     // BPQ-kind             1-byte
@@ -527,9 +528,9 @@
     if (i < buf_len)    buf[i++] = (char) bpq->matching_rule;
 
     // BPQ-value-length     SDNV
-    if ( (encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 )
+    if ( (q_encoding_len = sdnv_encode (bpq->query.query_len, encoding, PATH_MAX)) == -1 )
         return -1;
-    for (j=0; i<buf_len && j<encoding_len; ++j)
+    for (j=0; i<buf_len && j<q_encoding_len; ++j)
         buf[i++] = encoding[j];
 
     // BPQ-value            n-bytes
@@ -537,9 +538,9 @@
         buf[i++] = bpq->query.query_val[j];
 
     // number of fragments  SDNV
-    if ( (encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 )
+    if ( (f_encoding_len = sdnv_encode (bpq->fragments.num_frag_returned, encoding, PATH_MAX)) == -1 )
         return -1;
-    for (j=0; i<buf_len && j<encoding_len; ++j)
+    for (j=0; i<buf_len && j<f_encoding_len; ++j)
         buf[i++] = encoding[j];
 
     for (k=0; k<bpq->fragments.num_frag_returned; ++k) {
@@ -556,7 +557,16 @@
         for (j=0; i<buf_len && j<encoding_len; ++j)
             buf[i++] = encoding[j];
     }
-
+    if (verbose) {
+        fprintf (stdout, "\nbpq_to_char_array (buf_len:%d, i:%d):\n",buf_len,i);
+        fprintf (stdout, "             kind: %d\n", (int) buf[0]);
+        fprintf (stdout, "    matching rule: %d\n", (int) buf[1]);
+        fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
+        fprintf (stdout, "   q_encoding_len: %d\n", q_encoding_len);
+        fprintf (stdout, "        query val: %s\n", bpq->query.query_val);
+        fprintf (stdout, "     fragment len: %d\n", bpq->fragments.num_frag_returned);
+        fprintf (stdout, "   f_encoding_len: %d\n\n", f_encoding_len);
+    }
 
     return i;
 }
@@ -577,10 +587,11 @@
 int
 char_array_to_bpq(const char* buf,
     size_t buf_len,
-    dtn_bpq_extension_block_data_t * bpq)
+    dtn_bpq_extension_block_data_t * bpq,
+    int verbose)
 {
     int i=0, j=0;
-    int decoding_len=0;
+    int q_decoding_len, f_decoding_len, decoding_len;
 
     // BPQ-kind             1-byte
     if (i<buf_len) bpq->kind = (u_int) buf[i++];
@@ -589,18 +600,18 @@
     if (i<buf_len) bpq->matching_rule = (u_int) buf[i++];
 
     // BPQ-value-length     SDNV
-    if ( (decoding_len = sdnv_decode (&(buf[i]), buf_len - i, &(bpq->query.query_len))) == -1 )
+    if ( (q_decoding_len = sdnv_decode (&(buf[i]), buf_len - i, &(bpq->query.query_len))) == -1 )
         return -1;
-    i += decoding_len;
+    i += q_decoding_len;
 
     // BPQ-value            n-bytes
     if (i<buf_len) bpq->query.query_val = &(buf[i]);
     i += bpq->query.query_len;
 
     // number of fragments  SDNV
-    if ( (decoding_len = sdnv_decode (&(buf[i]), buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 )
+    if ( (f_decoding_len = sdnv_decode (&(buf[i]), buf_len - i, &(bpq->fragments.num_frag_returned))) == -1 )
         return -1;
-    i += decoding_len;
+    i += f_decoding_len;
 
     for (j=0; i<buf_len && j<bpq->fragments.num_frag_returned; ++j) {
 
@@ -618,6 +629,17 @@
     if (i != buf_len)
         return -1;
 
+    if (verbose) {
+        fprintf (stdout, "\nchar_array_to_bpq (buf_len:%d, i:%d):\n",buf_len, i);
+        fprintf (stdout, "             kind: %d\n", (int) buf[0]);
+        fprintf (stdout, "    matching rule: %d\n", (int) buf[1]);
+        fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
+        fprintf (stdout, "   q_decoding_len: %d\n", q_decoding_len);
+        fprintf (stdout, "        query val: %s\n", bpq->query.query_val);
+        fprintf (stdout, "     fragment len: %d\n", bpq->fragments.num_frag_returned);
+        fprintf (stdout, "   f_decoding_len: %d\n\n", f_decoding_len);
+    }
+
     return DTN_SUCCESS;
 }
 
@@ -668,11 +690,12 @@
     offsets[0] = 0;
     lengths[0] = 100;   //todo: add payload length here
 
+/*  REMOVING TODO - CHECK THE RFC TO MAKE SURE THIS IS OK
     response_bpq_block_data.fragments.num_frag_returned = 1;
     response_bpq_block_data.fragments.frag_offsets = offsets;
     response_bpq_block_data.fragments.frag_lenghts = lengths; 
-
-    if ( (buf_len = bpq_to_char_array(&response_bpq_block_data, buf, PATH_MAX)) == -1 ) {
+*/
+    if ( (buf_len = bpq_to_char_array(&response_bpq_block_data, buf, PATH_MAX, verbose)) == -1 ) {
         fprintf (stderr, "error encoding bpq: %d", buf_len);
         return -1;
     }
@@ -776,7 +799,8 @@
 
                 ret = char_array_to_bpq(bpq_blocks[j].data.data_val, 
                                         bpq_blocks[j].data.data_len, 
-                                        &bpq_block_data);
+                                        &bpq_block_data,
+					verbose);
                 if (ret != DTN_SUCCESS) {
                     fprintf(stderr, "error decoding query bundle: %d\n", ret);
                     return ret;