apps/dtncp/dtncp.c
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2004-2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <stdlib.h>
       
    23 #include <string.h>
       
    24 #include <strings.h>
       
    25 #include <unistd.h>
       
    26 #include <errno.h>
       
    27 #include <sys/types.h>
       
    28 #include <sys/stat.h>
       
    29 #include <sys/time.h>
       
    30 #include <time.h>
       
    31 
       
    32 #include "dtn_api.h"
       
    33 
       
    34 char *progname;
       
    35 int verbose             = 1;
       
    36 
       
    37 char data_source[1024]; // filename or message, depending on type
       
    38 
       
    39 // specified options
       
    40 char * arg_dest         = NULL;
       
    41 char * arg_target       = NULL;
       
    42 
       
    43 int    expiration_time  = 60 * 60; // default is 1 hour
       
    44 int delivery_receipts   = 0;    // request end to end delivery receipts
       
    45 
       
    46 void parse_options(int, char**);
       
    47 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle, dtn_endpoint_id_t * eid, 
       
    48                           char * str);
       
    49 void print_usage();
       
    50 void print_eid(char * label, dtn_endpoint_id_t * eid);
       
    51 
       
    52 int
       
    53 main(int argc, char** argv)
       
    54 {
       
    55     int ret;
       
    56     dtn_handle_t handle;
       
    57     dtn_reg_info_t reginfo;
       
    58     dtn_reg_id_t regid = DTN_REGID_NONE;
       
    59     dtn_bundle_spec_t bundle_spec;
       
    60     dtn_bundle_spec_t reply_spec;
       
    61     dtn_bundle_payload_t send_payload;
       
    62     dtn_bundle_payload_t reply_payload;
       
    63     dtn_bundle_id_t bundle_id;
       
    64     char demux[4096];
       
    65     struct timeval start, end;
       
    66 
       
    67 /*     FILE * file; */
       
    68 /*     //struct stat finfo; */
       
    69 /*     char buffer[4096]; // max filesize to send is 4096 (temp) */
       
    70 /*     int bufsize = 0; */
       
    71 
       
    72     // force stdout to always be line buffered, even if output is
       
    73     // redirected to a pipe or file
       
    74     setvbuf(stdout, (char *)NULL, _IOLBF, 0);
       
    75     
       
    76     parse_options(argc, argv);
       
    77 
       
    78     // open the ipc handle
       
    79     if (verbose) fprintf(stdout, "Opening connection to local DTN daemon\n");
       
    80 
       
    81     int err = dtn_open(&handle);
       
    82     if (err != DTN_SUCCESS) {
       
    83         fprintf(stderr, "fatal error opening dtn handle: %s\n",
       
    84                 dtn_strerror(err));
       
    85         exit(1);
       
    86     }
       
    87 
       
    88 
       
    89     // ----------------------------------------------------
       
    90     // initialize bundle spec with src/dest/replyto
       
    91     // ----------------------------------------------------
       
    92 
       
    93     // initialize bundle spec
       
    94     memset(&bundle_spec, 0, sizeof(bundle_spec));
       
    95 
       
    96     // destination host is specified at run time, demux is hardcoded
       
    97     sprintf(demux, "%s/dtncp/recv?file=%s", arg_dest, arg_target);
       
    98     parse_eid(handle, &bundle_spec.dest, demux);
       
    99 
       
   100     // source is local eid with file path as demux string
       
   101     sprintf(demux, "/dtncp/send?source=%s", data_source);
       
   102     parse_eid(handle, &bundle_spec.source, demux);
       
   103 
       
   104     if (verbose)
       
   105     {
       
   106         print_eid("source_eid", &bundle_spec.source);
       
   107         print_eid("dest_eid", &bundle_spec.dest);
       
   108     }
       
   109 
       
   110     // set the expiration time (one hour)
       
   111     bundle_spec.expiration = expiration_time;
       
   112     
       
   113     if (delivery_receipts)
       
   114     {
       
   115         // set the delivery receipt option
       
   116         bundle_spec.dopts |= DOPTS_DELIVERY_RCPT;
       
   117     }
       
   118 
       
   119     // fill in a payload
       
   120     memset(&send_payload, 0, sizeof(send_payload));
       
   121 
       
   122     dtn_set_payload(&send_payload, DTN_PAYLOAD_FILE,
       
   123         data_source, strlen(data_source));
       
   124      
       
   125     // send file and wait for reply
       
   126 
       
   127     // create a new dtn registration to receive bundle status reports
       
   128     memset(&reginfo, 0, sizeof(reginfo));
       
   129     dtn_copy_eid(&reginfo.endpoint, &bundle_spec.source);
       
   130     reginfo.flags = DTN_REG_DEFER;
       
   131     reginfo.regid = regid;
       
   132     reginfo.expiration = 0;
       
   133     if ((ret = dtn_register(handle, &reginfo, &regid)) != 0) {
       
   134         fprintf(stderr, "error creating registration (id=%d): %d (%s)\n",
       
   135                 regid, ret, dtn_strerror(dtn_errno(handle)));
       
   136         exit(1);
       
   137     }
       
   138     
       
   139     if (verbose) printf("dtn_register succeeded, regid 0x%x\n", regid);
       
   140 
       
   141     gettimeofday(&start, NULL); // timer
       
   142 
       
   143     memset(&bundle_id, 0, sizeof(bundle_id));
       
   144                 
       
   145     if ((ret = dtn_send(handle, regid, &bundle_spec, &send_payload,
       
   146                         &bundle_id)) != 0) {
       
   147         fprintf(stderr, "error sending file bundle: %d (%s)\n",
       
   148                 ret, dtn_strerror(dtn_errno(handle)));
       
   149         exit(1);
       
   150     }
       
   151 
       
   152     if (delivery_receipts)
       
   153       {
       
   154 	memset(&reply_spec, 0, sizeof(reply_spec));
       
   155 	memset(&reply_payload, 0, sizeof(reply_payload));
       
   156 	
       
   157 	// now we block waiting for the echo reply
       
   158 	if ((ret = dtn_recv(handle, &reply_spec,
       
   159 			    DTN_PAYLOAD_MEM, &reply_payload, -1)) < 0)
       
   160 	  {
       
   161 	    fprintf(stderr, "error getting reply: %d (%s)\n",
       
   162 		    ret, dtn_strerror(dtn_errno(handle)));
       
   163 	    exit(1);
       
   164 	  }
       
   165 	gettimeofday(&end, NULL);
       
   166 
       
   167 
       
   168 	printf("file sent successfully to [%s]: time=%.1f ms\n",
       
   169 	       reply_spec.source.uri,
       
   170 	       ((double)(end.tv_sec - start.tv_sec) * 1000.0 + 
       
   171 		(double)(end.tv_usec - start.tv_usec)/1000.0));
       
   172 	
       
   173 	dtn_free_payload(&reply_payload);
       
   174       } 
       
   175     else 
       
   176       {
       
   177 	printf("file sent to [%s]\n",
       
   178 	       bundle_spec.dest.uri);
       
   179       }
       
   180 
       
   181     dtn_close(handle);
       
   182     
       
   183     return 0;
       
   184 }
       
   185 
       
   186 void print_usage()
       
   187 {
       
   188     fprintf(stderr,
       
   189             "usage: %s [-D] [--expiration sec] <filename> <destination_eid> <remote-name>\n", 
       
   190             progname);
       
   191     fprintf(stderr,
       
   192             "    Remote filename is optional; defaults to the "
       
   193             "local filename.\n\n"
       
   194 	    "-D disables acknowledgements\n"
       
   195             "Bundle expiration time is in seconds.\n");
       
   196     
       
   197     exit(1);
       
   198 }
       
   199 
       
   200 void parse_options(int argc, char**argv)
       
   201 {
       
   202     progname = argv[0];
       
   203 
       
   204     // expiration time in seconds
       
   205     if (argc < 2)
       
   206         goto bail;
       
   207 
       
   208     if (strcmp(argv[1], "--expiration") == 0)
       
   209     {
       
   210         argv++;
       
   211         argc--;
       
   212         
       
   213         if (argc < 2)
       
   214             goto bail;
       
   215         
       
   216         expiration_time = atoi(argv[1]);
       
   217         if (expiration_time == 0)
       
   218         {
       
   219             fprintf(stderr, 
       
   220                     "Expiration time must be > 0\n");
       
   221 	    exit(1);
       
   222 	}
       
   223 
       
   224         argv++;
       
   225         argc--;
       
   226     }
       
   227 
       
   228     // no reply
       
   229     if (argc < 2)
       
   230         goto bail;
       
   231 
       
   232     if (strcmp(argv[1], "-D") == 0)
       
   233       {
       
   234 	delivery_receipts = 1;
       
   235 	
       
   236         argv++;
       
   237         argc--;
       
   238       }
       
   239 
       
   240     // parse the normal arguments
       
   241     if (argc < 3)
       
   242         goto bail;
       
   243 
       
   244     if (argv[1][0] == '/') sprintf(data_source, "%s", argv[1]);
       
   245     else sprintf(data_source, "%s/%s", getenv("PWD"), argv[1]);
       
   246 
       
   247     arg_dest = argv[2];
       
   248     if (argc > 3)
       
   249     {
       
   250         arg_target = argv[3];
       
   251     }
       
   252     else 
       
   253     {
       
   254         arg_target = strrchr(data_source, '/');
       
   255         if (arg_target == 0) arg_target = data_source;
       
   256     }
       
   257 
       
   258     return;
       
   259 
       
   260   bail:
       
   261         print_usage();
       
   262         exit(1);
       
   263 }
       
   264 
       
   265 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle, 
       
   266                           dtn_endpoint_id_t * eid, char * str)
       
   267 {
       
   268     
       
   269     // try the string as an actual dtn eid
       
   270     if (!dtn_parse_eid_string(eid, str)) 
       
   271     {
       
   272         return eid;
       
   273     }
       
   274     // build a local eid based on the configuration of our dtn
       
   275     // router plus the str as demux string
       
   276     else if (!dtn_build_local_eid(handle, eid, str))
       
   277     {
       
   278         return eid;
       
   279     }
       
   280     else
       
   281     {
       
   282         fprintf(stderr, "invalid endpoint id string '%s'\n", str);
       
   283         exit(1);
       
   284     }
       
   285 }
       
   286 
       
   287 void print_eid(char *  label, dtn_endpoint_id_t * eid)
       
   288 {
       
   289     printf("%s [%s]\n", label, eid->uri);
       
   290 }
       
   291     
       
   292 
       
   293 
       
   294