apps/dtnrespond/dtnrespond.c
changeset 66 e1101c5d54a1
parent 65 333724f2f7cf
child 67 cfe2328ba1c6
--- a/apps/dtnrespond/dtnrespond.c	Wed Oct 26 13:33:11 2011 +0100
+++ b/apps/dtnrespond/dtnrespond.c	Fri Jan 06 17:28:36 2012 +0000
@@ -60,7 +60,6 @@
     fprintf(stderr, "options:\n");
     fprintf(stderr, " -n  < count > exit after count bundles received\n");
     fprintf(stderr, " -r  < eid > reply to endpoint\n");
-    fprintf(stderr, " -e  < seconds > bundle expiry time\n");
     fprintf(stderr, " -i  < regid > existing registration id\n");
     fprintf(stderr, " -E  < seconds > registration expiry time\n");
     fprintf(stderr, " -A  < defer | drop | exec > failure action\n");
@@ -93,7 +92,6 @@
     char * reply_eid_name,              // r
     char * matching_filename,           // f
     int * count,                        // n
-    dtn_timeval_t * bundle_expiry,      // e
     dtn_reg_id_t * regid,               // i
     dtn_timeval_t * reg_expiry,         // E
     int * reg_fail_action,              // A
@@ -114,7 +112,7 @@
 
     while( !done )
     {
-        c = getopt(argc, argv, "l:f:n:r:e:i:E:A:S:P:DXFRcC1NWvhH");
+        c = getopt(argc, argv, "l:f:n:r:i:E:A:S:P:DXFRcC1NWvhH");
         switch(c)
         {
         case 'l':
@@ -129,9 +127,6 @@
         case 'n':
             *count = atoi(optarg);
             break;
-        case 'e':
-            *bundle_expiry = atoi(optarg);
-            break;
         case 'i':
             *regid = atoi(optarg);
             break;
@@ -217,7 +212,7 @@
     }
 
     // if no reply-to eid set, use the local eid
-    if (*reply_eid_name == NULL)
+    if (reply_eid_name == NULL)
         strncpy(reply_eid_name, local_eid_name, PATH_MAX);
 
     return DTN_SUCCESS;
@@ -385,7 +380,8 @@
 
     // move past any leading whitespace
     // by testing if the current char is in the whitespace string
-    while ( i<strlen(in) && strchr(whitespace, in[i]) != NULL )
+    in_len = strlen(in);
+    while ( i<in_len && strchr(whitespace, in[i]) != NULL )
     	++i;
 
     in_len = strlen(&(in[i]));
@@ -413,9 +409,9 @@
 * @return 0 on success or -1 if input not completely read
 *******************************************************************************/
 int
-escape_spaces(const char * in, char * out, int out_len)
+escape_spaces(const char * in, char * out, size_t out_len)
 {
-    int i=0, j=0;
+    u_int i=0, j=0;
     char escape = '\\';
     char space = ' ';
 
@@ -480,7 +476,7 @@
     u_int * response_kind,
     char  * response_path,
     int     response_path_len,
-    int   * response_expiry,
+    dtn_timeval_t * response_expiry,
     int   * found)
 {
     char line[PATH_MAX];
@@ -508,11 +504,12 @@
     	if (strncmp(line, "#", 1) == 0)
     		continue;
 
-    	matching_rule 	= strtok(line, ",");
-		query 			= strtok(NULL, ",");
-		path 	        = strtok(NULL, ",");
-		kind  			= strtok(NULL, ",");
-		expiry			= strtok(NULL, ",");
+    	// if a null pointer is returned, loop again to get to the next line
+    	if ((matching_rule 	= strtok(line, ",")) == NULL) continue;
+    	if ((query 			= strtok(NULL, ",")) == NULL) continue;
+    	if ((path 	        = strtok(NULL, ",")) == NULL) continue;
+    	if ((kind  			= strtok(NULL, ",")) == NULL) continue;
+    	if ((expiry			= strtok(NULL, ",")) == NULL) continue;
 
 
     	// match query
@@ -532,7 +529,7 @@
         if (response_path_exists(trim_path)) {
         	*found = 1;
         	*response_kind = (u_int) atoi(kind);
-        	*response_expiry = atoi(response_expiry);
+        	*response_expiry = (dtn_timeval_t) atoi(expiry);
         	strncpy(response_path, trim_path, response_path_len);
 
 			break;
@@ -569,12 +566,13 @@
 int
 bpq_to_char_array(const dtn_bpq_extension_block_data_t * bpq,
     char* buf,
-    size_t buf_len,
+    int buf_len,
     int verbose)
 {
-    int i=0, j=0, k=0;
+    int i=0, j=0;
+	u_int k=0;
     int q_encoding_len, f_encoding_len, encoding_len;
-    char encoding[PATH_MAX];
+    u_char encoding[PATH_MAX];
 
     memset(buf, 0, buf_len);
 
@@ -615,7 +613,7 @@
 	}
 
     // Source EID n-bytes
-    for (j=0; i<buf_len && j<bpq->original_id.source_len; ++j)
+    for (j=0; i<buf_len && j<(int)bpq->original_id.source_len; ++j)
         buf[i++] = bpq->original_id.source.uri[j];
 
 
@@ -631,7 +629,7 @@
     }
 
     // Query value n-bytes
-    for (j=0; i<buf_len && j<bpq->query.query_len; ++j)
+    for (j=0; i<buf_len && j<(int)bpq->query.query_len; ++j)
         buf[i++] = bpq->query.query_val[j];
 
 
@@ -681,7 +679,7 @@
         fprintf (stdout, "   source eid len: %d\n",
         		 (int) bpq->original_id.source_len);
         fprintf (stdout, "       source eid: %s\n",
-        		 (int) bpq->original_id.source.uri);
+        		 bpq->original_id.source.uri);
 
         fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
         fprintf (stdout, "   q_encoding_len: %d\n", q_encoding_len);
@@ -716,8 +714,8 @@
 * @return The number of bytes or -1 on error
 *******************************************************************************/
 int
-char_array_to_bpq(const char* buf,
-    size_t buf_len,
+char_array_to_bpq(const u_char* buf,
+    int buf_len,
     dtn_bpq_extension_block_data_t * bpq,
     int verbose)
 {
@@ -753,7 +751,7 @@
     // Source EID length     SDNV
     if ( (q_decoding_len = sdnv_decode (&(buf[i]),
     									buf_len - i,
-    									&(bpq->original_id.source_len))) == -1 ) {
+    									(u_int64_t*)&(bpq->original_id.source_len))) == -1 ) {
         fprintf (stderr, "Error decoding source EID length\n");
         return -1;
     }
@@ -761,7 +759,7 @@
 
     // Source EID            n-bytes
     if (i<buf_len && bpq->original_id.source_len <= DTN_MAX_ENDPOINT_ID) {
-    	strncpy(bpq->original_id.source.uri, &(buf[i]), bpq->original_id.source_len);
+    	strncpy(bpq->original_id.source.uri, (char*)&(buf[i]), bpq->original_id.source_len);
     	i += bpq->original_id.source_len;
     } else {
     	fprintf (stderr, "Error copying source EID\n");
@@ -773,32 +771,32 @@
     // BPQ-value-length     SDNV
     if ( (q_decoding_len = sdnv_decode (&(buf[i]),
     									buf_len - i,
-    									&(bpq->query.query_len))) == -1 ) {
+    									(u_int64_t*)&(bpq->query.query_len))) == -1 ) {
         fprintf (stderr, "Error decoding BPQ-value-length\n");
         return -1;
     }
     i += q_decoding_len;
 
     // BPQ-value            n-bytes
-    if (i<buf_len) bpq->query.query_val = &(buf[i]);
+    if (i<buf_len) bpq->query.query_val = (char*)&(buf[i]);
     	i += bpq->query.query_len;
 
 
     // number of fragments  SDNV
     if ( (f_decoding_len = sdnv_decode (&(buf[i]),
     									buf_len - i,
-    									&(bpq->fragments.num_frag_returned))) == -1 ) {
+    									(u_int64_t*)&(bpq->fragments.num_frag_returned))) == -1 ) {
         fprintf (stderr, "Error decoding number of fragments\n");
         return -1;
     }
     i += f_decoding_len;
 
-    for (j=0; i<buf_len && j<bpq->fragments.num_frag_returned; ++j) {
+    for (j=0; i<buf_len && j<(int)bpq->fragments.num_frag_returned; ++j) {
 
         // fragment offsets     SDNV
         if ( (decoding_len = sdnv_decode (&(buf[i]),
         								  buf_len - i,
-        								  &(bpq->fragments.frag_offsets[j]))) == -1 ) {
+        								  (u_int64_t*)&(bpq->fragments.frag_offsets[j]))) == -1 ) {
             fprintf (stderr, "Error decoding fragment[%d] offset\n", j);
             return -1;
         }
@@ -807,7 +805,7 @@
         // fragment lengths     SDNV
         if ( (decoding_len = sdnv_decode (&(buf[i]),
         								  buf_len - i,
-        								  &(bpq->fragments.frag_lenghts[j]))) == -1 ) {
+        								  (u_int64_t*)&(bpq->fragments.frag_lenghts[j]))) == -1 ) {
             fprintf (stderr, "Error decoding fragment[%d] length\n", j);
             return -1;
         }
@@ -829,7 +827,7 @@
         fprintf (stdout, "   source eid len: %d\n",
         		 (int) bpq->original_id.source_len);
         fprintf (stdout, "       source eid: %s\n",
-        		 (int) bpq->original_id.source.uri);
+        		 bpq->original_id.source.uri);
 
         fprintf (stdout, "        query len: %d\n", bpq->query.query_len);
         fprintf (stdout, "   q_decoding_len: %d\n", q_decoding_len);
@@ -854,17 +852,17 @@
     dtn_reg_id_t regid,
     u_int response_kind,
     dtn_bundle_spec_t * query_bundle_spec,
-    const dtn_endpoint_id_t * reply_eid,
+    dtn_endpoint_id_t * reply_eid,
     dtn_bpq_extension_block_data_t * query_bpq_block_data,
-    const char * pathname,
-    int bundle_expiry,
+    char * pathname,
+    dtn_timeval_t bundle_expiry,
     dtn_bundle_priority_t  priority,
     int delivery_options,
     int verbose)
 {
     int  ret = 0;
     char buf [PATH_MAX];
-    size_t buf_len = 0;
+    int buf_len = 0;
     dtn_bundle_id_t                 response_bundle_id;
     dtn_bundle_spec_t               response_bundle_spec;
     dtn_extension_block_t           response_bpq_block;
@@ -916,7 +914,7 @@
     ret = dtn_send(*handle, regid, &response_bundle_spec, &response_payload, &response_bundle_id);
     if (ret != DTN_SUCCESS) {
         fprintf(stderr, "error sending response bundle: %d (%s)\n",
-                    ret, dtn_strerror(dtn_errno(handle)));
+                    ret, dtn_strerror(dtn_errno(*handle)));
     } else if (verbose) {
         fprintf(stdout, "bundle sent successfully: id %s,%llu.%llu\n",
                     response_bundle_id.source.uri,
@@ -937,15 +935,15 @@
 int
 receive_bpq(dtn_handle_t * handle,
     dtn_reg_id_t regid,
-    const dtn_endpoint_id_t * reply_eid,
+    dtn_endpoint_id_t * reply_eid,
     const char * matching_filename,
     int  count,
-    int bundle_expiry,
     dtn_bundle_priority_t  priority,
     int delivery_options,
     int verbose)
 {
     int i, j, num_blocks, found, ret = 0;
+    dtn_timeval_t bundle_expiry = 3600; // default one hour
     u_int response_kind;
     char pathname[PATH_MAX];
     dtn_bundle_spec_t              bundle_spec;
@@ -990,10 +988,10 @@
                 if (verbose) fprintf(stdout, "bundle %d contains a "
                                              "BPQ extension block\n", i);
 
-                ret = char_array_to_bpq(bpq_blocks[j].data.data_val, 
+                ret = char_array_to_bpq((u_char*)bpq_blocks[j].data.data_val,
                                         bpq_blocks[j].data.data_len, 
                                         &bpq_block_data,
-					verbose);
+                                        verbose);
                 if (ret != DTN_SUCCESS) {
                     fprintf(stderr, "error decoding query bundle: %d\n", ret);
                     return ret;
@@ -1052,7 +1050,6 @@
     char reply_eid_name[PATH_MAX];
     char matching_filename[PATH_MAX];
     int count = 0;                                      //forever
-    dtn_timeval_t bundle_expiry = 3600;                 //one hour
     dtn_reg_id_t regid = DTN_REGID_NONE;
     dtn_timeval_t reg_expiry = 30;
     int reg_fail_action = DTN_REG_DEFER;
@@ -1068,7 +1065,6 @@
         reply_eid_name,
         matching_filename,
         &count,
-        &bundle_expiry,
         &regid,
         &reg_expiry,
         &reg_fail_action,
@@ -1111,7 +1107,6 @@
         &reply_eid,
         matching_filename,
         count,
-        bundle_expiry,
         priority,
         delivery_options,
         verbose);