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 <unistd.h>
00023 #include <errno.h>
00024 #include <strings.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <time.h>
00028 #include <sys/time.h>
00029
00030 #include "dtn_api.h"
00031 #include "dtnping.h"
00032
00033 const char *progname;
00034
00035 void
00036 usage()
00037 {
00038 fprintf(stderr, "usage: %s eid\n",
00039 progname);
00040 exit(1);
00041 }
00042
00043 void doOptions(int argc, const char **argv);
00044
00045 int expiration = 30;
00046 int wait_after_done = 0;
00047 char register_eid_str[DTN_MAX_ENDPOINT_ID] = "";
00048 char replyto_eid_str[DTN_MAX_ENDPOINT_ID] = "";
00049
00050
00051 int
00052 main(int argc, const char** argv)
00053 {
00054 int ret;
00055 dtn_handle_t handle;
00056 dtn_endpoint_id_t register_eid;
00057 dtn_reg_info_t reginfo;
00058 dtn_reg_id_t regid;
00059 dtn_bundle_spec_t ping_spec;
00060 dtn_bundle_spec_t reply_spec;
00061
00062
00063
00064 dtn_bundle_payload_t reply_payload;
00065 dtn_bundle_status_report_t* sr_data;
00066
00067 int debug = 1;
00068 char demux[64];
00069
00070
00071 struct timeval now;
00072 u_int32_t nonce;
00073 int done;
00074 time_t clock;
00075 struct tm* tm_buf;
00076
00077
00078
00079 setvbuf(stdout, (char *)NULL, _IOLBF, 0);
00080
00081 doOptions(argc, argv);
00082
00083 memset(&ping_spec, 0, sizeof(ping_spec));
00084
00085 gettimeofday(&now, 0);
00086 srand(now.tv_sec);
00087 nonce = rand();
00088
00089
00090 int err = dtn_open(&handle);
00091 if (err != DTN_SUCCESS) {
00092 fprintf(stderr, "fatal error opening dtn handle: %s\n",
00093 dtn_strerror(err));
00094 exit(1);
00095 }
00096
00097
00098
00099
00100 snprintf(demux, sizeof(demux), "/dtnReportLogger");
00101 if (register_eid_str[0] != '\0') {
00102 if (dtn_parse_eid_string(®ister_eid, register_eid_str)) {
00103 fprintf(stderr, "invalid eid string '%s'\n",
00104 register_eid_str);
00105 exit(1);
00106 }
00107 } else {
00108 dtn_build_local_eid(handle, ®ister_eid, demux);
00109 }
00110
00111
00112 memset(®info, 0, sizeof(reginfo));
00113 dtn_copy_eid(®info.endpoint, ®ister_eid);
00114 reginfo.flags = DTN_REG_DROP;
00115 reginfo.regid = DTN_REGID_NONE;
00116 reginfo.expiration = 0;
00117 if ((ret = dtn_register(handle, ®info, ®id)) != 0) {
00118 fprintf(stderr, "error creating registration: %d (%s)\n",
00119 ret, dtn_strerror(dtn_errno(handle)));
00120 exit(1);
00121 }
00122 if (debug) printf("dtn_register %s succeeded, regid %d\n",
00123 register_eid_str, regid);
00124
00125 memset(&reply_spec, 0, sizeof(reply_spec));
00126 memset(&reply_payload, 0, sizeof(reply_payload));
00127
00128
00129 done = 0;
00130 while (1) {
00131
00132 int timeout = 5000;
00133 if ((ret = dtn_recv(handle, &reply_spec,
00134 DTN_PAYLOAD_MEM, &reply_payload, timeout)) < 0)
00135 {
00136 if ( dtn_errno(handle) == DTN_ETIMEOUT) {
00137 printf("dtn_recv timed out, going again.\n");
00138 continue;
00139 }
00140 fprintf(stderr, "error getting ping reply: %d (%s)\n",
00141 ret, dtn_strerror(dtn_errno(handle)));
00142 exit(1);
00143 }
00144
00145 gettimeofday(&now, 0);
00146
00147 if (reply_payload.status_report != NULL)
00148 {
00149 sr_data = reply_payload.status_report;
00150
00151
00152 if (sr_data->flags & STATUS_RECEIVED)
00153 {
00154 clock = sr_data->receipt_ts.secs + DTNTIME_OFFSET;
00155 tm_buf = gmtime(&clock);
00156 printf("%s: received bundle %s at %.*s UTC\n",
00157 reply_spec.source.uri,
00158 "FOO",
00159 24, asctime(tm_buf));
00160 }
00161 if (sr_data->flags & STATUS_FORWARDED)
00162 {
00163 clock = sr_data->forwarding_ts.secs + DTNTIME_OFFSET;
00164 tm_buf = gmtime(&clock);
00165 printf("%s: forwarded at %.*s UTC\n",
00166 reply_spec.source.uri, 24, asctime(tm_buf));
00167 }
00168 if (sr_data->flags & STATUS_DELIVERED)
00169 {
00170 dtn_endpoint_id_t *theSource = &(sr_data->bundle_id.source);
00171 dtn_timestamp_t *theCreationTimestamp = &(sr_data->bundle_id.creation_ts);
00172
00173 clock = sr_data->delivery_ts.secs + DTNTIME_OFFSET;
00174 tm_buf = gmtime(&clock);
00175 printf("%s: delivered bundle (%s %d:%d) at %.*s UTC\n",
00176 reply_spec.source.uri,
00177 theSource->uri, theCreationTimestamp->secs, theCreationTimestamp->seqno,
00178 24, asctime(tm_buf));
00179 }
00180 if (sr_data->flags & STATUS_DELETED)
00181 {
00182 clock = sr_data->deletion_ts.secs + DTNTIME_OFFSET;
00183 tm_buf = gmtime(&clock);
00184 printf("%s: deleted at %.*s UTC\n",
00185 reply_spec.source.uri, 24, asctime(tm_buf));
00186 break;
00187 }
00188 }
00189 else {
00190
00191 continue;
00192 }
00193
00194 dtn_free_payload(&reply_payload);
00195 }
00196
00197 dtn_close(handle);
00198
00199 return 0;
00200 }
00201
00202 void
00203 doOptions(int argc, const char **argv)
00204 {
00205 int c;
00206
00207 progname = argv[0];
00208
00209
00210
00211 while ( (c=getopt(argc, (char **) argv, "he:d:s:r:w:")) !=EOF ) {
00212 switch (c) {
00213 case 'e':
00214 expiration = atoi(optarg);
00215 break;
00216 case 'd':
00217 strcpy(register_eid_str, optarg);
00218 break;
00219 case 'h':
00220 usage();
00221 break;
00222 case 'w':
00223 wait_after_done = atoi(optarg);
00224 break;
00225 default:
00226 break;
00227 }
00228 }
00229
00230 if ((optind < argc) && (strlen(register_eid_str) == 0)) {
00231 strcpy(register_eid_str, argv[optind++]);
00232 }
00233
00234 if (optind < argc) {
00235 fprintf(stderr, "unsupported argument '%s'\n", argv[optind]);
00236 exit(1);
00237 }
00238
00239 if ( 0 && register_eid_str[0] == '\0') {
00240 fprintf(stderr, "must supply a destination eid (or 'localhost')\n");
00241 exit(1);
00242 }
00243 }
00244