Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "config.h"
00009 #include <ibrdtn/api/Client.h>
00010 #include <ibrdtn/api/FileBundle.h>
00011 #include <ibrdtn/api/BLOBBundle.h>
00012 #include <ibrcommon/net/tcpclient.h>
00013 #include <ibrcommon/thread/Mutex.h>
00014 #include <ibrcommon/thread/MutexLock.h>
00015 #include <ibrcommon/data/BLOB.h>
00016 #include <ibrcommon/Logger.h>
00017
00018 #include <iostream>
00019
00020 void print_help()
00021 {
00022 cout << "-- dtnsend (IBR-DTN) --" << endl;
00023 cout << "Syntax: dtnsend [options] <dst> <filename>" << endl;
00024 cout << " <dst> set the destination eid (e.g. dtn://node/filetransfer)" << endl;
00025 cout << " <filename> the file to transfer" << endl;
00026 cout << "* optional parameters *" << endl;
00027 cout << " -h|--help display this text" << endl;
00028 cout << " --src <name> set the source application name (e.g. filetransfer)" << endl;
00029 cout << " -p <0..2> set the bundle priority (0 = low, 1 = normal, 2 = high)" << endl;
00030 cout << " --lifetime <seconds>" << endl;
00031 cout << " set the lifetime of outgoing bundles; default: 3600" << endl;
00032 cout << " -U <socket> use UNIX domain sockets" << endl;
00033 cout << " -n <copies> create <copies> bundle copies" << endl;
00034 cout << " --encrypt request encryption on the bundle layer" << endl;
00035 cout << " --sign request signature on the bundle layer" << endl;
00036 cout << " --custody request custody transfer of the bundle" << endl;
00037
00038 }
00039
00040 int main(int argc, char *argv[])
00041 {
00042 bool error = false;
00043 string file_destination = "dtn://local/filetransfer";
00044 string file_source = "filetransfer";
00045 unsigned int lifetime = 3600;
00046 bool use_stdin = false;
00047 std::string filename;
00048 ibrcommon::File unixdomain;
00049 int priority = 1;
00050 int copies = 1;
00051 bool bundle_encryption = false;
00052 bool bundle_signed = false;
00053 bool bundle_custody = false;
00054
00055
00056
00057
00058 std::list<std::string> arglist;
00059
00060 for (int i = 0; i < argc; i++)
00061 {
00062 if (argv[i][0] == '-')
00063 {
00064 std::string arg = argv[i];
00065
00066
00067 if (arg == "-h" || arg == "--help")
00068 {
00069 print_help();
00070 return 0;
00071 }
00072 else if (arg == "--encrypt")
00073 {
00074 bundle_encryption = true;
00075 }
00076 else if (arg == "--sign")
00077 {
00078 bundle_signed = true;
00079 }
00080 else if (arg == "--custody")
00081 {
00082 bundle_custody = true;
00083 }
00084 else if (arg == "--src" && argc > i)
00085 {
00086 if (++i > argc)
00087 {
00088 std::cout << "argument missing!" << std::endl;
00089 return -1;
00090 }
00091
00092 file_source = argv[i];
00093 }
00094 else if (arg == "--lifetime" && argc > i)
00095 {
00096 if (++i > argc)
00097 {
00098 std::cout << "argument missing!" << std::endl;
00099 return -1;
00100 }
00101
00102 stringstream data; data << argv[i];
00103 data >> lifetime;
00104 }
00105 else if (arg == "-p" && argc > i)
00106 {
00107 if (++i > argc)
00108 {
00109 std::cout << "argument missing!" << std::endl;
00110 return -1;
00111 }
00112 stringstream data; data << argv[i];
00113 data >> priority;
00114 }
00115 else if (arg == "-U" && argc > i)
00116 {
00117 if (++i > argc)
00118 {
00119 std::cout << "argument missing!" << std::endl;
00120 return -1;
00121 }
00122
00123 unixdomain = ibrcommon::File(argv[i]);
00124 }
00125 else if (arg == "-n" && argc > i)
00126 {
00127 if (++i > argc)
00128 {
00129 std::cout << "argument missing!" << std::endl;
00130 return -1;
00131 }
00132
00133 stringstream data; data << argv[i];
00134 data >> copies;
00135
00136 if( copies < 1 ) {
00137 std::cout << "invalid number of bundle copies!" << std::endl;
00138 return -1;
00139 }
00140 }
00141 else
00142 {
00143 std::cout << "invalid argument " << arg << std::endl;
00144 return -1;
00145 }
00146 }
00147 else
00148 {
00149 arglist.push_back(argv[i]);
00150 }
00151 }
00152
00153 if (arglist.size() <= 1)
00154 {
00155 print_help();
00156 return -1;
00157 } else if (arglist.size() == 2)
00158 {
00159 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00160
00161
00162 file_destination = (*iter);
00163
00164 use_stdin = true;
00165 }
00166 else if (arglist.size() > 2)
00167 {
00168 std::list<std::string>::iterator iter = arglist.begin(); iter++;
00169
00170
00171 file_destination = (*iter); iter++;
00172
00173
00174 filename = (*iter);
00175 }
00176
00177 try {
00178
00179 ibrcommon::tcpclient conn;
00180
00181
00182 if (unixdomain.exists())
00183 {
00184
00185 conn.open(unixdomain);
00186 }
00187 else
00188 {
00189
00190 conn.open("127.0.0.1", 4550);
00191
00192
00193 conn.enableNoDelay();
00194 }
00195
00196 try {
00197
00198 dtn::api::Client client(file_source, conn, dtn::api::Client::MODE_SENDONLY);
00199
00200
00201
00202 client.connect();
00203
00204
00205 EID addr = EID(file_destination);
00206
00207 try {
00208 if (use_stdin)
00209 {
00210 cout << "Transfer stdin to " << addr.getNodeEID() << endl;
00211
00212
00213 ibrcommon::BLOB::Reference ref = ibrcommon::TmpFileBLOB::create();
00214
00215
00216 (*ref.iostream()) << cin.rdbuf();
00217
00218 for(int u=0; u<copies; u++){
00219 dtn::api::BLOBBundle b(file_destination, ref);
00220
00221
00222 if (bundle_encryption) b.requestEncryption();
00223
00224
00225 if (bundle_signed) b.requestSigned();
00226
00227
00228 if (bundle_custody) b.requestCustodyTransfer();
00229
00230
00231 b.setLifetime(lifetime);
00232
00233
00234 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00235
00236
00237 client << b;
00238
00239 if (copies > 1)
00240 {
00241 std::cout << "sent copy #" << (u+1) << std::endl;
00242 }
00243 }
00244 }
00245 else
00246 {
00247 cout << "Transfer file \"" << filename << "\" to " << addr.getNodeEID() << endl;
00248
00249 for(int u=0; u<copies; u++){
00250
00251 dtn::api::FileBundle b(file_destination, filename);
00252
00253
00254 if (bundle_encryption) b.requestEncryption();
00255
00256
00257 if (bundle_signed) b.requestSigned();
00258
00259
00260 if (bundle_custody) b.requestCustodyTransfer();
00261
00262
00263 b.setLifetime(lifetime);
00264
00265
00266 b.setPriority(dtn::api::Bundle::BUNDLE_PRIORITY(priority));
00267
00268
00269 client << b;
00270
00271 if (copies > 1)
00272 {
00273 std::cout << "sent copy #" << (u+1) << std::endl;
00274 }
00275 }
00276 }
00277
00278
00279 client.flush();
00280 } catch (const ibrcommon::IOException &ex) {
00281 std::cerr << "Error while sending bundle." << std::endl;
00282 std::cerr << "\t" << ex.what() << std::endl;
00283 error = true;
00284 }
00285
00286
00287 client.close();
00288
00289
00290 std::cout << client.lastack << " bytes sent" << std::endl;
00291
00292 } catch (const ibrcommon::IOException &ex) {
00293 cout << "Error: " << ex.what() << endl;
00294 error = true;
00295 } catch (const dtn::api::ConnectionException&) {
00296
00297 }
00298
00299
00300 conn.close();
00301 } catch (const std::exception &ex) {
00302 cout << "Error: " << ex.what() << endl;
00303 error = true;
00304 }
00305
00306 if (error) return -1;
00307
00308 return 0;
00309 }