added reply-to option to dtnrespond
authoraidan
Wed, 29 Jun 2011 12:23:07 +0100
changeset 19 ce4cfc1cb1b6
parent 18 3d1ea31e2a19
child 20 7fd5f0de6c59
added reply-to option to dtnrespond
apps/dtnrespond/dtnrespond.1
apps/dtnrespond/dtnrespond.c
--- a/apps/dtnrespond/dtnrespond.1	Wed Jun 29 11:54:26 2011 +0100
+++ b/apps/dtnrespond/dtnrespond.1	Wed Jun 29 12:23:07 2011 +0100
@@ -24,6 +24,8 @@
 .IR eid
 .RB -f
 .IR matching_file
+.RB [ -r
+.IR eid ]
 .RB [ -n
 .IR num ]
 .RB [ -e
@@ -65,6 +67,9 @@
 information on the files to match against. See 
 .B dtnmatch.
 .TP
+.B \-\^r
+The reply-to EID for the bundle. If not set the src EID will be used.
+.TP
 .B \-\^n
 The number of queries to listen for before closing. Default: 0
 indicates
--- a/apps/dtnrespond/dtnrespond.c	Wed Jun 29 11:54:26 2011 +0100
+++ b/apps/dtnrespond/dtnrespond.c	Wed Jun 29 12:23:07 2011 +0100
@@ -65,6 +65,7 @@
                     "[opts]\n", progname);
     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");
@@ -95,6 +96,7 @@
 int
 parse_options(int argc, char** argv,
     char * local_eid_name,              // l
+    char * reply_eid_name,              // r
     char * matching_filename,           // f
     int * count,                        // n
     dtn_timeval_t * bundle_expiry,      // e
@@ -112,6 +114,7 @@
 
     //initialize strings
     memset(local_eid_name,    0, sizeof(char) * PATH_MAX);
+    memset(reply_eid_name,    0, sizeof(char) * PATH_MAX);
     memset(matching_filename, 0, sizeof(char) * PATH_MAX);
     memset(reg_fail_script,   0, sizeof(char) * PATH_MAX);
 
@@ -123,6 +126,9 @@
         case 'l':
             strncpy(local_eid_name, optarg, PATH_MAX);
             break;
+        case 'r':
+            strncpy(reply_eid_name, optarg, PATH_MAX);
+            break;
         case 'f':
             strncpy(matching_filename, optarg, PATH_MAX);
             break;
@@ -215,6 +221,11 @@
             exit(1);
         }             
     }
+
+    // if no reply-to eid set, use the local eid
+    if (reply_eid_name == NULL)
+        strncpy(reply_eid_name, local_eid_name, PATH_MAX);
+
     return 0;
 }
 
@@ -223,7 +234,9 @@
 * returns success or exits on failure 
 *******************************************************************************/
 int
-validate_options(const char * local_eid_name, const char * matching_filename)
+validate_options(const char * local_eid_name,
+    const char * reply_eid_name,
+    const char * matching_filename)
 {
 #define REQUIRE(test, err_msg) \
     if(!test) { \
@@ -233,6 +246,7 @@
     }
 
     REQUIRE(strlen(local_eid_name) > 0, "-l <local eid> required\n");
+    REQUIRE(strlen(reply_eid_name) > 0, "-r <reply eid> required\n");
     REQUIRE(strlen(matching_filename) > 0, "-f <matching filename> required\n");
     
     return 0;
@@ -325,6 +339,36 @@
 }
 
 /*******************************************************************************
+* parse eid:
+*  
+* code lifted from dtnsend
+* todo: check this
+*******************************************************************************/
+dtn_endpoint_id_t *
+parse_eid(dtn_handle_t handle,
+    dtn_endpoint_id_t * eid,
+    const char * str,
+    int verbose)
+{
+    // try the string as an actual dtn eid
+    if (dtn_parse_eid_string(eid, str) == DTN_SUCCESS) {
+        if (verbose) fprintf(stdout, "%s (literal)\n", str);
+        return eid;
+    }
+
+    // build a local eid based on the configuration of our dtn
+    // router plus the str as demux string
+    else if (dtn_build_local_eid(handle, eid, str) == DTN_SUCCESS) {
+        if (verbose) fprintf(stdout, "%s (local)\n", str);
+        return eid;
+    }
+    else {
+        fprintf(stderr, "invalid eid string '%s'\n", str);
+        exit(1);
+    }
+}
+
+/*******************************************************************************
 * trim whitespace:
 * first move past any leading whitespace 
 * after the first non-whitespace char bef=gin copying to output
@@ -654,6 +698,7 @@
 send_response_bpq(dtn_handle_t * handle,
     dtn_reg_id_t regid,
     dtn_bundle_spec_t * query_bundle_spec,
+    const dtn_endpoint_id_t * reply_eid,
     dtn_bpq_extension_block_data_t * query_bpq_block_data,
     const char * pathname,
     int bundle_expiry,
@@ -707,9 +752,9 @@
     response_bpq_block.data.data_val = buf;
 
     // copy dest src
-    dtn_copy_eid(&response_bundle_spec.dest,    &(query_bundle_spec->source));
+    dtn_copy_eid(&response_bundle_spec.dest,    &(query_bundle_spec->replyto));
     dtn_copy_eid(&response_bundle_spec.source,  &(query_bundle_spec->dest));
-    dtn_copy_eid(&response_bundle_spec.replyto, &(query_bundle_spec->dest));
+    dtn_copy_eid(&response_bundle_spec.replyto, reply_eid);
 
     // set the bundle spec dtn options
     response_bundle_spec.expiration = bundle_expiry;
@@ -746,6 +791,7 @@
 int
 receive_bpq(dtn_handle_t * handle,
     dtn_reg_id_t regid,
+    const dtn_endpoint_id_t * reply_eid,
     const char * matching_filename,
     int  count,
     int bundle_expiry,
@@ -819,6 +865,7 @@
             ret = send_response_bpq(handle,
                                     regid,
                                     &bundle_spec,
+                                    reply_eid,
                                     &bpq_block_data,
                                     pathname,
                                     bundle_expiry,
@@ -845,7 +892,9 @@
 main(int argc, char** argv)
 {
     dtn_endpoint_id_t local_eid;
+    dtn_endpoint_id_t reply_eid;
     char local_eid_name[PATH_MAX];
+    char reply_eid_name[PATH_MAX];
     char matching_filename[PATH_MAX];
     int count = -1;                                     //forever
     dtn_timeval_t bundle_expiry = 3600;                 //one hour
@@ -861,6 +910,7 @@
 
     parse_options(argc, argv,
         local_eid_name,
+        reply_eid_name,
         matching_filename,
         &count,
         &bundle_expiry,
@@ -872,7 +922,7 @@
         &delivery_options,
         &verbose);
 
-    validate_options(local_eid_name, matching_filename);
+    validate_options(local_eid_name, reply_eid_name, matching_filename);
 
     // open the ipc handle
     if (verbose) fprintf(stdout, "opening connection to dtn router...\n");
@@ -883,6 +933,9 @@
     }
     if (verbose) fprintf(stdout, "opened connection to dtn router...\n");
 
+    parse_eid(handle, &reply_eid, reply_eid_name, verbose);
+    if (verbose) fprintf(stdout, "parsed reply_eid: %s\n", reply_eid.uri);
+
     // get dtn registration
     if (verbose) fprintf(stdout, "registering with dtn...\n");
     register_with_dtn(handle,
@@ -900,6 +953,7 @@
     // this fn will likely never exit so the handle won't be closed...
     receive_bpq(&handle,
         regid,
+        &reply_eid,
         matching_filename,
         count,
         bundle_expiry,