apps/dtnquery/dtnquery.c
changeset 16 dee8e37f24c2
parent 13 841ab1482c9c
child 18 3d1ea31e2a19
equal deleted inserted replaced
15:65e62bd13efd 16:dee8e37f24c2
    78     fprintf(stderr, " -f  < filename > response filename\n");
    78     fprintf(stderr, " -f  < filename > response filename\n");
    79     fprintf(stderr, " -q  < query > query or matching file\n");
    79     fprintf(stderr, " -q  < query > query or matching file\n");
    80     fprintf(stderr, " -t  < literal | base64 | file > query type\n");
    80     fprintf(stderr, " -t  < literal | base64 | file > query type\n");
    81     fprintf(stderr, " -r  < exact > matching rule\n");
    81     fprintf(stderr, " -r  < exact > matching rule\n");
    82     fprintf(stderr, " -m  < send | receive | both > mode\n");
    82     fprintf(stderr, " -m  < send | receive | both > mode\n");
       
    83     fprintf(stderr, " -n  < count > number of bundles to recv\n");
    83     fprintf(stderr, " -o  < seconds > timeout\n");
    84     fprintf(stderr, " -o  < seconds > timeout\n");
    84     fprintf(stderr, " -e  < seconds > bundle expiry time\n");
    85     fprintf(stderr, " -e  < seconds > bundle expiry time\n");
    85     fprintf(stderr, " -i  < regid > existing registration id\n");
    86     fprintf(stderr, " -i  < regid > existing registration id\n");
    86     fprintf(stderr, " -E  < seconds > registration expiry time\n");
    87     fprintf(stderr, " -E  < seconds > registration expiry time\n");
    87     fprintf(stderr, " -A  < defer | drop | exec > failure action\n");
    88     fprintf(stderr, " -A  < defer | drop | exec > failure action\n");
   182     char * filename,                    // f
   183     char * filename,                    // f
   183     char * query,                       // q
   184     char * query,                       // q
   184     int * query_type,                   // t
   185     int * query_type,                   // t
   185     int * matching_rule,                // r
   186     int * matching_rule,                // r
   186     int * mode,                         // m
   187     int * mode,                         // m
       
   188     int * count,                        // n
   187     dtn_timeval_t * timeout,            // o
   189     dtn_timeval_t * timeout,            // o
   188     dtn_timeval_t * bundle_expiry,      // e
   190     dtn_timeval_t * bundle_expiry,      // e
   189     dtn_reg_id_t  * regid,              // i
   191     dtn_reg_id_t  * regid,              // i
   190     dtn_timeval_t * reg_expiry,         // E
   192     dtn_timeval_t * reg_expiry,         // E
   191     int  * reg_fail_action,             // A
   193     int  * reg_fail_action,             // A
   207     memset(matching_file,   0, sizeof(char) * PATH_MAX);
   209     memset(matching_file,   0, sizeof(char) * PATH_MAX);
   208     memset(reg_fail_script, 0, sizeof(char) * PATH_MAX);
   210     memset(reg_fail_script, 0, sizeof(char) * PATH_MAX);
   209 
   211 
   210     while( !done )
   212     while( !done )
   211     {
   213     {
   212         c = getopt(argc, argv, "s:d:f:q:t:r:m:o:e:i:E:A:S:P:DXFcC1NWvhH");
   214         c = getopt(argc, argv, "s:d:f:q:t:r:m:n:o:e:i:E:A:S:P:DXFcC1NWvhH");
   213         switch(c)
   215         switch(c)
   214         {
   216         {
   215         case 's':            
   217         case 's':            
   216             strncpy(src_eid_name, optarg, PATH_MAX);
   218             strncpy(src_eid_name, optarg, PATH_MAX);
   217             break;
   219             break;
   261             } else {
   263             } else {
   262                 fprintf(stderr, "invalid mode '%s'\n", optarg);
   264                 fprintf(stderr, "invalid mode '%s'\n", optarg);
   263                 usage();
   265                 usage();
   264                 exit(1);
   266                 exit(1);
   265             }
   267             }
       
   268         case 'n':
       
   269             *count = atoi(optarg);
       
   270             break;
   266         case 'o':
   271         case 'o':
   267             *timeout = atoi(optarg);
   272             *timeout = atoi(optarg);
   268             break;
   273             break;
   269         case 'e':
   274         case 'e':
   270             *bundle_expiry = atoi(optarg);  
   275             *bundle_expiry = atoi(optarg);  
   816 *******************************************************************************/
   821 *******************************************************************************/
   817 int
   822 int
   818 recv_bpq(dtn_handle_t handle,
   823 recv_bpq(dtn_handle_t handle,
   819     dtn_timeval_t timeout,
   824     dtn_timeval_t timeout,
   820     char * filename,
   825     char * filename,
       
   826     int count,
   821     int verbose)
   827     int verbose)
   822 {
   828 {
   823     int ret = 0, num_blocks, i;
   829     int ret = 0, err = 0, num_blocks, i, j;
   824     dtn_bundle_spec_t               bundle_spec;
   830     dtn_bundle_spec_t               bundle_spec;
   825     dtn_extension_block_t*          bpq_blocks;
   831     dtn_extension_block_t*          bpq_blocks;
   826     dtn_bpq_extension_block_data_t  bpq_block_data;
   832     dtn_bpq_extension_block_data_t  bpq_block_data;
   827     dtn_bundle_payload_t            payload;
   833     dtn_bundle_payload_t            payload;
   828 
   834 
   829     memset(&bundle_spec, 0, sizeof(bundle_spec));
   835     for(j = 0; (count == 0) || (j < count); ++j) {
   830     memset(&bpq_block_data, 0, sizeof(dtn_bpq_extension_block_data_t));
   836         memset(&bundle_spec, 0, sizeof(bundle_spec));
   831     memset(&payload, 0, sizeof(payload));
   837         memset(&bpq_block_data, 0, sizeof(dtn_bpq_extension_block_data_t));
   832 
   838         memset(&payload, 0, sizeof(payload));
   833     // recv the bpq bundle
   839         err = 0;
   834     if (verbose) fprintf(stdout, "blocking waiting for dtn_recv\n");
   840 
   835     ret = dtn_recv(handle, &bundle_spec, DTN_PAYLOAD_FILE, &payload, timeout);
   841         // recv the bpq bundle
   836     if (ret != DTN_SUCCESS) {
   842         if (verbose) fprintf(stdout, "blocking waiting for dtn_recv\n");
   837         fprintf(stderr, "error receiving bundle: %d (%s)\n",
   843         ret = dtn_recv(handle, &bundle_spec, DTN_PAYLOAD_FILE, &payload, timeout);
   838                     ret, dtn_strerror(dtn_errno(handle)));
   844         if (ret != DTN_SUCCESS) {
   839         return ret;
   845             fprintf(stderr, "error receiving bundle: %d (%s)\n",
   840     } else if (verbose) {
   846                              ret, dtn_strerror(dtn_errno(handle)));
   841             fprintf(stdout, "bundle received successfully: id %s,%llu.%llu\n",
   847             err = 1;
       
   848             continue;
       
   849         } else if (verbose) {
       
   850             fprintf(stdout, "bundle num %d received successfully: id %s,%llu.%llu\n",
       
   851                              j+1,
   842                              bundle_spec.source.uri,
   852                              bundle_spec.source.uri,
   843                              bundle_spec.creation_ts.secs,
   853                              bundle_spec.creation_ts.secs,
   844                              bundle_spec.creation_ts.seqno);
   854                              bundle_spec.creation_ts.seqno);
   845     }
   855         }
   846 
   856 
   847     // extract the bpq
   857         // extract the bpq
   848     num_blocks = bundle_spec.blocks.blocks_len;
   858         num_blocks = bundle_spec.blocks.blocks_len;
   849     bpq_blocks = bundle_spec.blocks.blocks_val;
   859         bpq_blocks = bundle_spec.blocks.blocks_val;
   850 
   860 
   851     for (i = 0; i < num_blocks; ++i) {
   861         for (i = 0; i < num_blocks; ++i) {
   852         if (bpq_blocks[i].type == DTN_BPQ_BLOCK_TYPE) {
   862             if (bpq_blocks[i].type == DTN_BPQ_BLOCK_TYPE) {
   853 
   863 
   854             if (verbose) fprintf(stdout, "bundle contains a "
   864                 if (verbose) fprintf(stdout, "bundle contains a "
   855                                          "BPQ extension block\n");
   865                                              "BPQ extension block\n");
   856             
   866 
   857             if (verbose) fprintf(stdout, "BPQ query_len: %d)\n", (int)  bpq_blocks[i].data.data_len);
   867                 if ( bpq_blocks[i].data.data_len <= 0 || 
   858             if (verbose) fprintf(stdout, "BPQ query_val: %s)\n", (char*)bpq_blocks[i].data.data_val);
   868                      bpq_blocks[i].data.data_val == NULL) {
   859 
   869                     fprintf(stderr, "error decoding query bundle: %d\n", ret);
   860             if ( bpq_blocks[i].data.data_len <= 0 || bpq_blocks[i].data.data_val == NULL) {
   870                     err = 1;
   861                 fprintf(stderr, "error decoding query bundle: %d\n", ret);
   871                     break;
   862                 return -1;
   872                 }
       
   873                 
       
   874                 ret = char_array_to_bpq(bpq_blocks[i].data.data_val,
       
   875                                         bpq_blocks[i].data.data_len,
       
   876                                         &bpq_block_data,
       
   877                                         verbose);
       
   878                 if (ret != DTN_SUCCESS) {
       
   879                     fprintf(stderr, "error decoding query bundle: %d\n", ret);
       
   880                     err = 1;
       
   881                     break;
       
   882                 }
       
   883 
       
   884                 if (verbose) fprintf(stdout, "BPQ query(%s)\n", bpq_block_data.query.query_val);
       
   885                 if (*filename == NULL)
       
   886                     strncpy(filename, bpq_block_data.query.query_val, PATH_MAX);
       
   887     
       
   888                 break;
   863             }
   889             }
   864                 
       
   865             ret = char_array_to_bpq(bpq_blocks[i].data.data_val,
       
   866                                     bpq_blocks[i].data.data_len,
       
   867                                     &bpq_block_data,
       
   868                                     verbose);
       
   869             if (ret != DTN_SUCCESS) {
       
   870                 fprintf(stderr, "error decoding query bundle: %d\n", ret);
       
   871                 return ret;
       
   872             }
       
   873 
       
   874             if (verbose) fprintf(stdout, "BPQ query(%s)\n", bpq_block_data.query.query_val);
       
   875             if (*filename == NULL)
       
   876                 strncpy(filename, bpq_block_data.query.query_val, PATH_MAX);
       
   877 
       
   878             break;
       
   879         }
   890         }
   880     }
   891 
   881 
   892         if(err)
   882 
   893             continue;
   883     // handle the payload file
   894 
   884     ret = handle_file_transfer(bundle_spec, payload, filename, verbose);
   895         // handle the payload file
   885     if (ret != DTN_SUCCESS) {
   896         ret = handle_file_transfer(bundle_spec, payload, filename, verbose);
   886         fprintf(stderr, "error handling file transfer: %d\n", ret);
   897         if (ret != DTN_SUCCESS) {
   887     } else if (verbose) {
   898             fprintf(stderr, "error handling file transfer: %d\n", ret);
   888         fprintf(stdout, "sucessfully handled file transfer\n");        
   899         } else if (verbose) {
   889     }
   900             fprintf(stdout, "sucessfully handled file transfer\n");        
   890 
   901         }
   891     dtn_free_payload(&payload);  
   902     
       
   903         dtn_free_payload(&payload);  
       
   904     }
   892     return ret;
   905     return ret;
   893 }
   906 }
   894 
   907 
   895 /*******************************************************************************
   908 /*******************************************************************************
   896 * main:
   909 * main:
   897 *******************************************************************************/
   910 *******************************************************************************/
   898 int
   911 int
   899 main(int argc, char** argv)
   912 main(int argc, char** argv)
   900 {
   913 {
       
   914     dtn_endpoint_id_t reg_eid;
   901     dtn_endpoint_id_t src_eid;
   915     dtn_endpoint_id_t src_eid;
   902     dtn_endpoint_id_t dest_eid;
   916     dtn_endpoint_id_t dest_eid;
       
   917     char reg_eid_name[PATH_MAX];
   903     char src_eid_name[PATH_MAX];
   918     char src_eid_name[PATH_MAX];
   904     char dest_eid_name[PATH_MAX];
   919     char dest_eid_name[PATH_MAX];
   905     char filename[PATH_MAX];
   920     char filename[PATH_MAX];
   906     char query[PATH_MAX];
   921     char query[PATH_MAX];
   907     int query_type = DTN_BPQ_LITERAL;
   922     int query_type = DTN_BPQ_LITERAL;
   908     int matching_rule = DTN_BPQ_EXACT;
   923     int matching_rule = DTN_BPQ_EXACT;
   909     int mode = DTN_BPQ_SEND_RECV;
   924     int mode = DTN_BPQ_SEND_RECV;
       
   925     int count = 1;
   910     dtn_timeval_t timeout = DTN_TIMEOUT_INF;    //forever
   926     dtn_timeval_t timeout = DTN_TIMEOUT_INF;    //forever
   911     dtn_timeval_t bundle_expiry = 3600;         //one hour
   927     dtn_timeval_t bundle_expiry = 3600;         //one hour
   912     dtn_reg_id_t regid = DTN_REGID_NONE;
   928     dtn_reg_id_t regid = DTN_REGID_NONE;
   913     dtn_timeval_t reg_expiry = 30;
   929     dtn_timeval_t reg_expiry = 30;
   914     int reg_fail_action = DTN_REG_DEFER;
   930     int reg_fail_action = DTN_REG_DEFER;
   917     int delivery_options = 0;               
   933     int delivery_options = 0;               
   918     int verbose = 0;
   934     int verbose = 0;
   919     int err = 0;
   935     int err = 0;
   920     dtn_handle_t handle;
   936     dtn_handle_t handle;
   921 
   937 
       
   938     memset( reg_eid_name,   0, sizeof(char) * PATH_MAX );
   922     memset( src_eid_name,   0, sizeof(char) * PATH_MAX );
   939     memset( src_eid_name,   0, sizeof(char) * PATH_MAX );
   923     memset( dest_eid_name,  0, sizeof(char) * PATH_MAX );
   940     memset( dest_eid_name,  0, sizeof(char) * PATH_MAX );
   924     memset( filename,       0, sizeof(char) * PATH_MAX );
   941     memset( filename,       0, sizeof(char) * PATH_MAX );
   925     memset( query,          0, sizeof(char) * PATH_MAX );
   942     memset( query,          0, sizeof(char) * PATH_MAX );
   926 
   943 
   930         filename,
   947         filename,
   931         query,
   948         query,
   932         &query_type,
   949         &query_type,
   933         &matching_rule,
   950         &matching_rule,
   934         &mode,
   951         &mode,
       
   952         &count,
   935         &timeout,
   953         &timeout,
   936         &bundle_expiry,
   954         &bundle_expiry,
   937         &regid,
   955         &regid,
   938         &reg_expiry,
   956         &reg_expiry,
   939         &reg_fail_action,
   957         &reg_fail_action,
   940         reg_fail_script,
   958         reg_fail_script,
   941         &priority,
   959         &priority,
   942         &delivery_options,
   960         &delivery_options,
   943         &verbose);
   961         &verbose);
   944 
   962 
       
   963     if (mode == DTN_BPQ_SEND) {
       
   964         snprintf(&(reg_eid_name[0]), PATH_MAX, "%s-send", src_eid_name);
       
   965     } else {
       
   966         snprintf(&(reg_eid_name[0]), PATH_MAX, "%s", src_eid_name);
       
   967     }
       
   968 
   945     validate_options(src_eid_name,
   969     validate_options(src_eid_name,
   946         dest_eid_name,
   970         dest_eid_name,
   947         query,
   971         query,
   948         query_type,
   972         query_type,
   949         matching_rule,
   973         matching_rule,
   957                 dtn_strerror(err));
   981                 dtn_strerror(err));
   958         exit(1);
   982         exit(1);
   959     }
   983     }
   960     if (verbose) fprintf(stdout, "opened connection to dtn router...\n");
   984     if (verbose) fprintf(stdout, "opened connection to dtn router...\n");
   961 
   985 
       
   986     if (mode != DTN_BPQ_SEND) {
       
   987         if (count == 0)
       
   988             fprintf(stdout, "dtnquery will loop forever receiving bundles\n");
       
   989         else 
       
   990             fprintf(stdout, "dtnquery will exit after receiving %d bundle(s)\n", count);
       
   991     }
       
   992 
   962     // parse eids
   993     // parse eids
       
   994     parse_eid(handle, &reg_eid, reg_eid_name, verbose);
   963     parse_eid(handle, &src_eid, src_eid_name, verbose);
   995     parse_eid(handle, &src_eid, src_eid_name, verbose);
   964     parse_eid(handle, &dest_eid, dest_eid_name, verbose);
   996     parse_eid(handle, &dest_eid, dest_eid_name, verbose);
       
   997     if (verbose) fprintf(stdout, "parsed reg_eid: %s\n", reg_eid.uri);
   965     if (verbose) fprintf(stdout, "parsed src_eid: %s\n", src_eid.uri);
   998     if (verbose) fprintf(stdout, "parsed src_eid: %s\n", src_eid.uri);
   966     if (verbose) fprintf(stdout, "parsed dest_eid: %s\n", dest_eid.uri);
   999     if (verbose) fprintf(stdout, "parsed dest_eid: %s\n", dest_eid.uri);
   967     if (verbose) fprintf(stdout, "regid: %d\n", regid);
  1000     if (verbose) fprintf(stdout, "regid: %d\n", regid);
   968 
  1001 
   969     // get dtn registration
  1002     // get dtn registration
   970     if (verbose) fprintf(stdout, "registering with dtn...\n");
  1003     if (verbose) fprintf(stdout, "registering with dtn...\n");
   971     register_with_dtn(handle,
  1004     register_with_dtn(handle,
   972         &src_eid,
  1005         &reg_eid,
   973         &regid,
  1006         &regid,
   974         reg_expiry,
  1007         reg_expiry,
   975         reg_fail_action,
  1008         reg_fail_action,
   976         reg_fail_script);
  1009         reg_fail_script);
   977     if (verbose) fprintf(stdout, "registered with dtn, "
  1010     if (verbose) fprintf(stdout, "registered with dtn, "
   978                                  "regid: %d local eid: %s\n",
  1011                                  "regid: %d local eid: %s\n",
   979                                  regid, src_eid.uri);
  1012                                  regid, reg_eid.uri);
   980     
  1013     
   981     //get to work
  1014     //get to work
   982     switch (mode)
  1015     switch (mode)
   983     {
  1016     {
   984 #define TRY(fn, err_msg) \
  1017 #define TRY(fn, err_msg) \
   992                       matching_rule, bundle_expiry, priority,
  1025                       matching_rule, bundle_expiry, priority,
   993                       delivery_options, verbose), "error sending query\n" );
  1026                       delivery_options, verbose), "error sending query\n" );
   994         break;
  1027         break;
   995 
  1028 
   996     case DTN_BPQ_RECV:
  1029     case DTN_BPQ_RECV:
   997         TRY( recv_bpq(handle, timeout, filename, verbose), 
  1030         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
   998              "error receiving query\n" );
  1031              "error receiving query\n" );
   999         break;
  1032         break;
  1000 
  1033 
  1001     case DTN_BPQ_SEND_RECV:
  1034     case DTN_BPQ_SEND_RECV:
  1002         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, query,
  1035         TRY( send_bpq(handle, regid, &src_eid, &dest_eid, query,
  1003                       matching_rule, bundle_expiry, priority,
  1036                       matching_rule, bundle_expiry, priority,
  1004                       delivery_options, verbose), "error sending query\n" );
  1037                       delivery_options, verbose), "error sending query\n" );
  1005 
  1038 
  1006         TRY( recv_bpq(handle, timeout, filename, verbose), 
  1039         TRY( recv_bpq(handle, timeout, filename, count, verbose), 
  1007              "error receiving query\n" );
  1040              "error receiving query\n" );
  1008         break;
  1041         break;
  1009 
  1042 
  1010     default:
  1043     default:
  1011         fprintf(stderr, "invalid mode '%d'\n", mode);
  1044         fprintf(stderr, "invalid mode '%d'\n", mode);