00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <strings.h>
00025 #include <unistd.h>
00026 #include <errno.h>
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029 #include <sys/time.h>
00030 #include <time.h>
00031
00032 #include "dtn_api.h"
00033
00034 char *progname;
00035 int verbose = 1;
00036
00037 char data_source[1024];
00038
00039
00040 char * arg_dest = NULL;
00041 char * arg_target = NULL;
00042
00043 int expiration_time = 60 * 60;
00044 int delivery_receipts = 0;
00045
00046 void parse_options(int, char**);
00047 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle, dtn_endpoint_id_t * eid,
00048 char * str);
00049 void print_usage();
00050 void print_eid(char * label, dtn_endpoint_id_t * eid);
00051
00052 int
00053 main(int argc, char** argv)
00054 {
00055 int ret;
00056 dtn_handle_t handle;
00057 dtn_reg_info_t reginfo;
00058 dtn_reg_id_t regid = DTN_REGID_NONE;
00059 dtn_bundle_spec_t bundle_spec;
00060 dtn_bundle_spec_t reply_spec;
00061 dtn_bundle_payload_t send_payload;
00062 dtn_bundle_payload_t reply_payload;
00063 dtn_bundle_id_t bundle_id;
00064 char demux[4096];
00065 struct timeval start, end;
00066
00067
00068
00069
00070
00071
00072
00073
00074 setvbuf(stdout, (char *)NULL, _IOLBF, 0);
00075
00076 parse_options(argc, argv);
00077
00078
00079 if (verbose) fprintf(stdout, "Opening connection to local DTN daemon\n");
00080
00081 int err = dtn_open(&handle);
00082 if (err != DTN_SUCCESS) {
00083 fprintf(stderr, "fatal error opening dtn handle: %s\n",
00084 dtn_strerror(err));
00085 exit(1);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 memset(&bundle_spec, 0, sizeof(bundle_spec));
00095
00096
00097 sprintf(demux, "%s/dtncp/recv?file=%s", arg_dest, arg_target);
00098 parse_eid(handle, &bundle_spec.dest, demux);
00099
00100
00101 sprintf(demux, "/dtncp/send?source=%s", data_source);
00102 parse_eid(handle, &bundle_spec.source, demux);
00103
00104 if (verbose)
00105 {
00106 print_eid("source_eid", &bundle_spec.source);
00107 print_eid("dest_eid", &bundle_spec.dest);
00108 }
00109
00110
00111 bundle_spec.expiration = expiration_time;
00112
00113 if (delivery_receipts)
00114 {
00115
00116 bundle_spec.dopts |= DOPTS_DELIVERY_RCPT;
00117 }
00118
00119
00120 memset(&send_payload, 0, sizeof(send_payload));
00121
00122 dtn_set_payload(&send_payload, DTN_PAYLOAD_FILE,
00123 data_source, strlen(data_source));
00124
00125
00126
00127
00128 memset(®info, 0, sizeof(reginfo));
00129 dtn_copy_eid(®info.endpoint, &bundle_spec.source);
00130 reginfo.flags = DTN_REG_DEFER;
00131 reginfo.regid = regid;
00132 reginfo.expiration = 0;
00133 if ((ret = dtn_register(handle, ®info, ®id)) != 0) {
00134 fprintf(stderr, "error creating registration (id=%d): %d (%s)\n",
00135 regid, ret, dtn_strerror(dtn_errno(handle)));
00136 exit(1);
00137 }
00138
00139 if (verbose) printf("dtn_register succeeded, regid 0x%x\n", regid);
00140
00141 gettimeofday(&start, NULL);
00142
00143 memset(&bundle_id, 0, sizeof(bundle_id));
00144
00145 if ((ret = dtn_send(handle, regid, &bundle_spec, &send_payload,
00146 &bundle_id)) != 0) {
00147 fprintf(stderr, "error sending file bundle: %d (%s)\n",
00148 ret, dtn_strerror(dtn_errno(handle)));
00149 exit(1);
00150 }
00151
00152 if (delivery_receipts)
00153 {
00154 memset(&reply_spec, 0, sizeof(reply_spec));
00155 memset(&reply_payload, 0, sizeof(reply_payload));
00156
00157
00158 if ((ret = dtn_recv(handle, &reply_spec,
00159 DTN_PAYLOAD_MEM, &reply_payload, -1)) < 0)
00160 {
00161 fprintf(stderr, "error getting reply: %d (%s)\n",
00162 ret, dtn_strerror(dtn_errno(handle)));
00163 exit(1);
00164 }
00165 gettimeofday(&end, NULL);
00166
00167
00168 printf("file sent successfully to [%s]: time=%.1f ms\n",
00169 reply_spec.source.uri,
00170 ((double)(end.tv_sec - start.tv_sec) * 1000.0 +
00171 (double)(end.tv_usec - start.tv_usec)/1000.0));
00172
00173 dtn_free_payload(&reply_payload);
00174 }
00175 else
00176 {
00177 printf("file sent to [%s]\n",
00178 bundle_spec.dest.uri);
00179 }
00180
00181 dtn_close(handle);
00182
00183 return 0;
00184 }
00185
00186 void print_usage()
00187 {
00188 fprintf(stderr,
00189 "usage: %s [-D] [--expiration sec] <filename> <destination_eid> <remote-name>\n",
00190 progname);
00191 fprintf(stderr,
00192 " Remote filename is optional; defaults to the "
00193 "local filename.\n\n"
00194 "-D disables acknowledgements\n"
00195 "Bundle expiration time is in seconds.\n");
00196
00197 exit(1);
00198 }
00199
00200 void parse_options(int argc, char**argv)
00201 {
00202 progname = argv[0];
00203
00204
00205 if (argc < 2)
00206 goto bail;
00207
00208 if (strcmp(argv[1], "--expiration") == 0)
00209 {
00210 argv++;
00211 argc--;
00212
00213 if (argc < 2)
00214 goto bail;
00215
00216 expiration_time = atoi(argv[1]);
00217 if (expiration_time == 0)
00218 {
00219 fprintf(stderr,
00220 "Expiration time must be > 0\n");
00221 exit(1);
00222 }
00223
00224 argv++;
00225 argc--;
00226 }
00227
00228
00229 if (argc < 2)
00230 goto bail;
00231
00232 if (strcmp(argv[1], "-D") == 0)
00233 {
00234 delivery_receipts = 1;
00235
00236 argv++;
00237 argc--;
00238 }
00239
00240
00241 if (argc < 3)
00242 goto bail;
00243
00244 if (argv[1][0] == '/') sprintf(data_source, "%s", argv[1]);
00245 else sprintf(data_source, "%s/%s", getenv("PWD"), argv[1]);
00246
00247 arg_dest = argv[2];
00248 if (argc > 3)
00249 {
00250 arg_target = argv[3];
00251 }
00252 else
00253 {
00254 arg_target = strrchr(data_source, '/');
00255 if (arg_target == 0) arg_target = data_source;
00256 }
00257
00258 return;
00259
00260 bail:
00261 print_usage();
00262 exit(1);
00263 }
00264
00265 dtn_endpoint_id_t * parse_eid(dtn_handle_t handle,
00266 dtn_endpoint_id_t * eid, char * str)
00267 {
00268
00269
00270 if (!dtn_parse_eid_string(eid, str))
00271 {
00272 return eid;
00273 }
00274
00275
00276 else if (!dtn_build_local_eid(handle, eid, str))
00277 {
00278 return eid;
00279 }
00280 else
00281 {
00282 fprintf(stderr, "invalid endpoint id string '%s'\n", str);
00283 exit(1);
00284 }
00285 }
00286
00287 void print_eid(char * label, dtn_endpoint_id_t * eid)
00288 {
00289 printf("%s [%s]\n", label, eid->uri);
00290 }
00291
00292
00293
00294