apps/dtnquery/dtnquery.c
changeset 18 3d1ea31e2a19
parent 16 dee8e37f24c2
child 20 7fd5f0de6c59
equal deleted inserted replaced
17:3d5d756acac0 18:3d1ea31e2a19
    73 usage()
    73 usage()
    74 {
    74 {
    75     fprintf(stderr, "usage: %s -s < src endpoint > -d < dest endpoint > "
    75     fprintf(stderr, "usage: %s -s < src endpoint > -d < dest endpoint > "
    76                     "[opts]\n", progname);
    76                     "[opts]\n", progname);
    77     fprintf(stderr, "options:\n");
    77     fprintf(stderr, "options:\n");
       
    78     fprintf(stderr, " -r  < eid > reply to endpoint\n");
    78     fprintf(stderr, " -f  < filename > response filename\n");
    79     fprintf(stderr, " -f  < filename > response filename\n");
    79     fprintf(stderr, " -q  < query > query or matching file\n");
    80     fprintf(stderr, " -q  < query > query or matching file\n");
    80     fprintf(stderr, " -t  < literal | base64 | file > query type\n");
    81     fprintf(stderr, " -t  < literal | base64 | file > query type\n");
    81     fprintf(stderr, " -r  < exact > matching rule\n");
    82     fprintf(stderr, " -u  < exact > matching rule\n");
    82     fprintf(stderr, " -m  < send | receive | both > mode\n");
    83     fprintf(stderr, " -m  < send | receive | both > mode\n");
    83     fprintf(stderr, " -n  < count > number of bundles to recv\n");
    84     fprintf(stderr, " -n  < count > number of bundles to recv\n");
    84     fprintf(stderr, " -o  < seconds > timeout\n");
    85     fprintf(stderr, " -o  < seconds > timeout\n");
    85     fprintf(stderr, " -e  < seconds > bundle expiry time\n");
    86     fprintf(stderr, " -e  < seconds > bundle expiry time\n");
    86     fprintf(stderr, " -i  < regid > existing registration id\n");
    87     fprintf(stderr, " -i  < regid > existing registration id\n");
   178 *******************************************************************************/
   179 *******************************************************************************/
   179 int
   180 int
   180 parse_options(int argc, char** argv,
   181 parse_options(int argc, char** argv,
   181     char * src_eid_name,                // s
   182     char * src_eid_name,                // s
   182     char * dest_eid_name,               // d
   183     char * dest_eid_name,               // d
       
   184     char * reply_eid_name,              // r
   183     char * filename,                    // f
   185     char * filename,                    // f
   184     char * query,                       // q
   186     char * query,                       // q
   185     int * query_type,                   // t
   187     int * query_type,                   // t
   186     int * matching_rule,                // r
   188     int * matching_rule,                // u
   187     int * mode,                         // m
   189     int * mode,                         // m
   188     int * count,                        // n
   190     int * count,                        // n
   189     dtn_timeval_t * timeout,            // o
   191     dtn_timeval_t * timeout,            // o
   190     dtn_timeval_t * bundle_expiry,      // e
   192     dtn_timeval_t * bundle_expiry,      // e
   191     dtn_reg_id_t  * regid,              // i
   193     dtn_reg_id_t  * regid,              // i
   202     progname = argv[0];
   204     progname = argv[0];
   203 
   205 
   204     //initialize strings
   206     //initialize strings
   205     memset(src_eid_name,    0, sizeof(char) * PATH_MAX);
   207     memset(src_eid_name,    0, sizeof(char) * PATH_MAX);
   206     memset(dest_eid_name,   0, sizeof(char) * PATH_MAX);
   208     memset(dest_eid_name,   0, sizeof(char) * PATH_MAX);
       
   209     memset(reply_eid_name,  0, sizeof(char) * PATH_MAX);
   207     memset(filename,        0, sizeof(char) * PATH_MAX);
   210     memset(filename,        0, sizeof(char) * PATH_MAX);
   208     memset(query,           0, sizeof(char) * PATH_MAX);
   211     memset(query,           0, sizeof(char) * PATH_MAX);
   209     memset(matching_file,   0, sizeof(char) * PATH_MAX);
   212     memset(matching_file,   0, sizeof(char) * PATH_MAX);
   210     memset(reg_fail_script, 0, sizeof(char) * PATH_MAX);
   213     memset(reg_fail_script, 0, sizeof(char) * PATH_MAX);
   211 
   214 
   212     while( !done )
   215     while( !done )
   213     {
   216     {
   214         c = getopt(argc, argv, "s:d:f:q:t:r:m:n:o:e:i:E:A:S:P:DXFcC1NWvhH");
   217         c = getopt(argc, argv, "s:d:f:q:t:r:u:m:n:o:e:i:E:A:S:P:DXFcC1NWvhH");
   215         switch(c)
   218         switch(c)
   216         {
   219         {
   217         case 's':            
   220         case 's':            
   218             strncpy(src_eid_name, optarg, PATH_MAX);
   221             strncpy(src_eid_name, optarg, PATH_MAX);
   219             break;
   222             break;
   220         case 'd':    
   223         case 'd':    
   221             strncpy(dest_eid_name, optarg, PATH_MAX);
   224             strncpy(dest_eid_name, optarg, PATH_MAX);
       
   225             break;
       
   226         case 'r':
       
   227             strncpy(reply_eid_name, optarg, PATH_MAX);
   222             break;
   228             break;
   223         case 'f':    
   229         case 'f':    
   224             strncpy(filename, optarg, PATH_MAX);
   230             strncpy(filename, optarg, PATH_MAX);
   225             break;
   231             break;
   226         case 'q':            
   232         case 'q':            
   239             } else {
   245             } else {
   240                 fprintf(stderr, "invalid query type '%s'\n", optarg);
   246                 fprintf(stderr, "invalid query type '%s'\n", optarg);
   241                 usage();
   247                 usage();
   242                 exit(1);
   248                 exit(1);
   243             }
   249             }
   244         case 'r':
   250         case 'u':
   245             if (!strcasecmp(optarg, "exact")) {
   251             if (!strcasecmp(optarg, "exact")) {
   246                 *matching_rule = DTN_BPQ_EXACT;
   252                 *matching_rule = DTN_BPQ_EXACT;
   247                 break;
   253                 break;
   248             } else {
   254             } else {
   249                 fprintf(stderr, "invalid query type '%s'\n", optarg);
   255                 fprintf(stderr, "invalid query type '%s'\n", optarg);
   356             // getopt already prints error message for unknown option characters
   362             // getopt already prints error message for unknown option characters
   357             usage();
   363             usage();
   358             exit(1);
   364             exit(1);
   359         }
   365         }
   360         
   366         
   361         // now set matching file if required
   367     }
   362         if (*query_type == DTN_BPQ_FILE) {
   368     // now set matching file if required
   363             strncpy(matching_file, query, PATH_MAX);
   369     if (*query_type == DTN_BPQ_FILE) {
   364             memset(query, 0, sizeof(char) * PATH_MAX);
   370         strncpy(matching_file, query, PATH_MAX);
   365 
   371         memset(query, 0, sizeof(char) * PATH_MAX);
   366             int ret = parse_matching_file(matching_file, matching_rule,
   372 
   367                                           query_type, query, bundle_expiry, *verbose);
   373         int ret = parse_matching_file(matching_file, matching_rule,
   368             if (ret != DTN_SUCCESS) {
   374                                       query_type, query, bundle_expiry, *verbose);
   369                 fprintf(stderr, "error parsing matching file, "
   375         if (ret != DTN_SUCCESS) {
   370                                 "see man page dtnmatch(1)\n");
   376             fprintf(stderr, "error parsing matching file, "
   371                 usage(1);
   377                             "see man page dtnmatch(1)\n");
   372                 exit(1);
   378             usage(1);
   373             }
   379             exit(1);
   374         }
   380         }
   375     }
   381     }
       
   382 
       
   383     // if no reply-to eid set, use the src eid
       
   384     if (reply_eid_name == NULL)
       
   385         strncpy(reply_eid_name, src_eid_name, PATH_MAX);
       
   386     
   376     return 0;
   387     return 0;
   377 }
   388 }
   378 
   389 
   379 /*******************************************************************************
   390 /*******************************************************************************
   380 * validate options:
   391 * validate options:
   383 * returns success or exits on failure
   394 * returns success or exits on failure
   384 *******************************************************************************/
   395 *******************************************************************************/
   385 int
   396 int
   386 validate_options(const char * src_eid_name,
   397 validate_options(const char * src_eid_name,
   387     const char * dest_eid_name,
   398     const char * dest_eid_name,
       
   399     const char * reply_eid_name,
   388     const char * query,
   400     const char * query,
   389     int query_type,
   401     int query_type,
   390     int matching_rule,
   402     int matching_rule,
   391     int mode,
   403     int mode,
   392     dtn_timeval_t bundle_expiry)
   404     dtn_timeval_t bundle_expiry)
   402     switch (mode)
   414     switch (mode)
   403     {
   415     {
   404     case DTN_BPQ_SEND:  //requires src, dest, query
   416     case DTN_BPQ_SEND:  //requires src, dest, query
   405         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
   417         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
   406         REQUIRE(strlen(dest_eid_name) > 0, "-d <dest eid> required\n");
   418         REQUIRE(strlen(dest_eid_name) > 0, "-d <dest eid> required\n");
       
   419         REQUIRE(strlen(reply_eid_name) > 0, "-r <reply eid> required\n");
   407         REQUIRE(strlen(query) > 0, "-q <query> required\n");
   420         REQUIRE(strlen(query) > 0, "-q <query> required\n");
   408         break;
   421         break;
   409 
   422 
   410     case DTN_BPQ_RECV:  //requires src
   423     case DTN_BPQ_RECV:  //requires src
   411         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
   424         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
       
   425         REQUIRE(strlen(reply_eid_name) > 0, "-r <reply eid> required\n");
   412         break;
   426         break;
   413 
   427 
   414     case DTN_BPQ_SEND_RECV: //requires src, dest, query
   428     case DTN_BPQ_SEND_RECV: //requires src, dest, query
   415         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
   429         REQUIRE(strlen(src_eid_name) > 0, "-s <src eid> required\n");
   416         REQUIRE(strlen(dest_eid_name) > 0, "-d <dest eid> required\n");
   430         REQUIRE(strlen(dest_eid_name) > 0, "-d <dest eid> required\n");
       
   431         REQUIRE(strlen(reply_eid_name) > 0, "-r <reply eid> required\n");
   417         REQUIRE(strlen(query) > 0, "-q <query> required\n");
   432         REQUIRE(strlen(query) > 0, "-q <query> required\n");
   418         break;
   433         break;
   419     default:  
   434     default:  
   420         REQUIRE(mode == DTN_BPQ_SEND ||
   435         REQUIRE(mode == DTN_BPQ_SEND ||
   421                 mode == DTN_BPQ_RECV ||
   436                 mode == DTN_BPQ_RECV ||
   423     }
   438     }
   424     REQUIRE(query_type == DTN_BPQ_LITERAL ||
   439     REQUIRE(query_type == DTN_BPQ_LITERAL ||
   425             query_type == DTN_BPQ_BASE64  ||
   440             query_type == DTN_BPQ_BASE64  ||
   426             query_type == DTN_BPQ_FILE, "-t <query type> invalid type\n");
   441             query_type == DTN_BPQ_FILE, "-t <query type> invalid type\n");
   427 
   442 
   428     REQUIRE(matching_rule == DTN_BPQ_EXACT, "-r <matching rule> invalid rule\n");
   443     REQUIRE(matching_rule == DTN_BPQ_EXACT, "-u <matching rule> invalid rule\n");
   429     REQUIRE(bundle_expiry > 0, "-e <expiry> must be a positive integer\n");
   444     REQUIRE(bundle_expiry > 0, "-e <expiry> must be a positive integer\n");
   430 #undef REQUIRE
   445 #undef REQUIRE
   431 //todo: check this is ok
   446 //todo: check this is ok
   432     return 0;
   447     return 0;
   433 }
   448 }
   739 int
   754 int
   740 send_bpq(dtn_handle_t handle,
   755 send_bpq(dtn_handle_t handle,
   741     dtn_reg_id_t regid,
   756     dtn_reg_id_t regid,
   742     const dtn_endpoint_id_t * src_eid,
   757     const dtn_endpoint_id_t * src_eid,
   743     const dtn_endpoint_id_t * dest_eid,
   758     const dtn_endpoint_id_t * dest_eid,
       
   759     const dtn_endpoint_id_t * reply_eid,
   744     char * query,
   760     char * query,
   745     int matching_rule,
   761     int matching_rule,
   746     int bundle_expiry,
   762     int bundle_expiry,
   747     dtn_bundle_priority_t  priority,
   763     dtn_bundle_priority_t  priority,
   748     int delivery_options,
   764     int delivery_options,
   784     dtn_set_payload(&payload, DTN_PAYLOAD_MEM, NULL, 0);    
   800     dtn_set_payload(&payload, DTN_PAYLOAD_MEM, NULL, 0);    
   785 
   801 
   786     // set the bundle spec eids
   802     // set the bundle spec eids
   787     if (verbose) fprintf(stdout, "Source: %s\n", src_eid->uri);
   803     if (verbose) fprintf(stdout, "Source: %s\n", src_eid->uri);
   788     if (verbose) fprintf(stdout, "Destination: %s\n", dest_eid->uri);
   804     if (verbose) fprintf(stdout, "Destination: %s\n", dest_eid->uri);
       
   805     if (verbose) fprintf(stdout, "Reply-To: %s\n", reply_eid->uri);
   789     bundle_spec.source = *src_eid;
   806     bundle_spec.source = *src_eid;
   790     bundle_spec.dest = *dest_eid;
   807     bundle_spec.dest = *dest_eid;
   791 
   808     bundle_spec.replyto = *reply_eid;
   792     dtn_copy_eid(&bundle_spec.replyto, &bundle_spec.source);// add support for this to be set differently
       
   793 
   809 
   794     // set the bundle spec dtn options
   810     // set the bundle spec dtn options
   795     bundle_spec.expiration = bundle_expiry;
   811     bundle_spec.expiration = bundle_expiry;
   796     bundle_spec.dopts = delivery_options;
   812     bundle_spec.dopts = delivery_options;
   797     bundle_spec.priority = priority;
   813     bundle_spec.priority = priority;
   912 main(int argc, char** argv)
   928 main(int argc, char** argv)
   913 {
   929 {
   914     dtn_endpoint_id_t reg_eid;
   930     dtn_endpoint_id_t reg_eid;
   915     dtn_endpoint_id_t src_eid;
   931     dtn_endpoint_id_t src_eid;
   916     dtn_endpoint_id_t dest_eid;
   932     dtn_endpoint_id_t dest_eid;
       
   933     dtn_endpoint_id_t reply_eid;
   917     char reg_eid_name[PATH_MAX];
   934     char reg_eid_name[PATH_MAX];
   918     char src_eid_name[PATH_MAX];
   935     char src_eid_name[PATH_MAX];
   919     char dest_eid_name[PATH_MAX];
   936     char dest_eid_name[PATH_MAX];
       
   937     char reply_eid_name[PATH_MAX];
   920     char filename[PATH_MAX];
   938     char filename[PATH_MAX];
   921     char query[PATH_MAX];
   939     char query[PATH_MAX];
   922     int query_type = DTN_BPQ_LITERAL;
   940     int query_type = DTN_BPQ_LITERAL;
   923     int matching_rule = DTN_BPQ_EXACT;
   941     int matching_rule = DTN_BPQ_EXACT;
   924     int mode = DTN_BPQ_SEND_RECV;
   942     int mode = DTN_BPQ_SEND_RECV;
   936     dtn_handle_t handle;
   954     dtn_handle_t handle;
   937 
   955 
   938     memset( reg_eid_name,   0, sizeof(char) * PATH_MAX );
   956     memset( reg_eid_name,   0, sizeof(char) * PATH_MAX );
   939     memset( src_eid_name,   0, sizeof(char) * PATH_MAX );
   957     memset( src_eid_name,   0, sizeof(char) * PATH_MAX );
   940     memset( dest_eid_name,  0, sizeof(char) * PATH_MAX );
   958     memset( dest_eid_name,  0, sizeof(char) * PATH_MAX );
       
   959     memset( reply_eid_name,  0, sizeof(char) * PATH_MAX );
   941     memset( filename,       0, sizeof(char) * PATH_MAX );
   960     memset( filename,       0, sizeof(char) * PATH_MAX );
   942     memset( query,          0, sizeof(char) * PATH_MAX );
   961     memset( query,          0, sizeof(char) * PATH_MAX );
   943 
   962 
   944     parse_options(argc, argv,
   963     parse_options(argc, argv,
   945         src_eid_name,
   964         src_eid_name,
   946         dest_eid_name,
   965         dest_eid_name,
       
   966         reply_eid_name,
   947         filename,
   967         filename,
   948         query,
   968         query,
   949         &query_type,
   969         &query_type,
   950         &matching_rule,
   970         &matching_rule,
   951         &mode,
   971         &mode,
   966         snprintf(&(reg_eid_name[0]), PATH_MAX, "%s", src_eid_name);
   986         snprintf(&(reg_eid_name[0]), PATH_MAX, "%s", src_eid_name);
   967     }
   987     }
   968 
   988 
   969     validate_options(src_eid_name,
   989     validate_options(src_eid_name,
   970         dest_eid_name,
   990         dest_eid_name,
       
   991         reply_eid_name,
   971         query,
   992         query,
   972         query_type,
   993         query_type,
   973         matching_rule,
   994         matching_rule,
   974         mode,
   995         mode,
   975         bundle_expiry);
   996         bundle_expiry);
   992 
  1013 
   993     // parse eids
  1014     // parse eids
   994     parse_eid(handle, &reg_eid, reg_eid_name, verbose);
  1015     parse_eid(handle, &reg_eid, reg_eid_name, verbose);
   995     parse_eid(handle, &src_eid, src_eid_name, verbose);
  1016     parse_eid(handle, &src_eid, src_eid_name, verbose);
   996     parse_eid(handle, &dest_eid, dest_eid_name, verbose);
  1017     parse_eid(handle, &dest_eid, dest_eid_name, verbose);
       
  1018     parse_eid(handle, &reply_eid, reply_eid_name, verbose);
   997     if (verbose) fprintf(stdout, "parsed reg_eid: %s\n", reg_eid.uri);
  1019     if (verbose) fprintf(stdout, "parsed reg_eid: %s\n", reg_eid.uri);
   998     if (verbose) fprintf(stdout, "parsed src_eid: %s\n", src_eid.uri);
  1020     if (verbose) fprintf(stdout, "parsed src_eid: %s\n", src_eid.uri);
   999     if (verbose) fprintf(stdout, "parsed dest_eid: %s\n", dest_eid.uri);
  1021     if (verbose) fprintf(stdout, "parsed dest_eid: %s\n", dest_eid.uri);
       
  1022     if (verbose) fprintf(stdout, "parsed reply_eid: %s\n", reply_eid.uri);
  1000     if (verbose) fprintf(stdout, "regid: %d\n", regid);
  1023     if (verbose) fprintf(stdout, "regid: %d\n", regid);
  1001 
  1024 
  1002     // get dtn registration
  1025     // get dtn registration
  1003     if (verbose) fprintf(stdout, "registering with dtn...\n");
  1026     if (verbose) fprintf(stdout, "registering with dtn...\n");
  1004     register_with_dtn(handle,
  1027     register_with_dtn(handle,
  1019         fprintf(stderr, err_msg); \
  1042         fprintf(stderr, err_msg); \
  1020         dtn_close(handle); \
  1043         dtn_close(handle); \
  1021         exit(1); \
  1044         exit(1); \
  1022     }
  1045     }
  1023     case DTN_BPQ_SEND:
  1046     case DTN_BPQ_SEND:
  1024         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, query,
  1047         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, &reply_eid, query,
  1025                       matching_rule, bundle_expiry, priority,
  1048                       matching_rule, bundle_expiry, priority,
  1026                       delivery_options, verbose), "error sending query\n" );
  1049                       delivery_options, verbose), "error sending query\n" );
  1027         break;
  1050         break;
  1028 
  1051 
  1029     case DTN_BPQ_RECV:
  1052     case DTN_BPQ_RECV:
  1030         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
  1053         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
  1031              "error receiving query\n" );
  1054              "error receiving query\n" );
  1032         break;
  1055         break;
  1033 
  1056 
  1034     case DTN_BPQ_SEND_RECV:
  1057     case DTN_BPQ_SEND_RECV:
  1035         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, query,
  1058         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, &reply_eid, query,
  1036                       matching_rule, bundle_expiry, priority,
  1059                       matching_rule, bundle_expiry, priority,
  1037                       delivery_options, verbose), "error sending query\n" );
  1060                       delivery_options, verbose), "error sending query\n" );
  1038 
  1061 
  1039         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
  1062         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
  1040              "error receiving query\n" );
  1063              "error receiving query\n" );