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 <oasys/util/OptParser.h>
00022 #include "bundling/BundleDaemon.h"
00023 #include "IPAnnounce.h"
00024 #include "conv_layers/IPConvergenceLayerUtils.h"
00025 #include "conv_layers/TCPConvergenceLayer.h"
00026 #include "conv_layers/UDPConvergenceLayer.h"
00027
00028 namespace dtn {
00029
00030 IPAnnounce::IPAnnounce()
00031 : cl_addr_(INADDR_ANY), cl_port_(TCPConvergenceLayer::TCPCL_DEFAULT_PORT)
00032 {
00033 }
00034
00035 bool
00036 IPAnnounce::configure(const std::string& name, ConvergenceLayer* cl,
00037 int argc, const char* argv[])
00038 {
00039 if (cl == NULL) return false;
00040
00041 cl_ = cl;
00042 name_ = name;
00043 type_.assign(cl->name());
00044
00045
00046 if (strncmp(cl_->name(),"tcp",3) != 0 &&
00047 strncmp(cl_->name(),"udp",3) != 0)
00048 {
00049 log_err("ip announce does not support cl type %s",
00050 cl_->name());
00051 return false;
00052 }
00053
00054
00055 oasys::OptParser p;
00056 p.addopt(new oasys::InAddrOpt("cl_addr",&cl_addr_));
00057 p.addopt(new oasys::UInt16Opt("cl_port",&cl_port_));
00058 p.addopt(new oasys::UIntOpt("interval",&interval_));
00059
00060 const char* invalid;
00061 if (! p.parse(argc, argv, &invalid))
00062 {
00063 log_err("bad parameter %s",invalid);
00064 return false;
00065 }
00066
00067 if (interval_ == 0)
00068 {
00069 log_err("interval must be greater than 0");
00070 return false;
00071 }
00072
00073
00074 interval_ *= 1000;
00075
00076 oasys::StringBuffer buf("%s:%d",intoa(cl_addr_),cl_port_);
00077 local_.assign(buf.c_str());
00078 return true;
00079 }
00080
00081 size_t
00082 IPAnnounce::format_advertisement(u_char* bp, size_t len)
00083 {
00084 EndpointID local(BundleDaemon::instance()->local_eid());
00085 size_t length = FOUR_BYTE_ALIGN(local.length() + sizeof(DiscoveryHeader));
00086
00087 if (len <= length)
00088 return 0;
00089
00090 DiscoveryHeader* hdr = (DiscoveryHeader*) bp;
00091 memset(hdr,0,len);
00092
00093 hdr->cl_type = IPDiscovery::str_to_type(type().c_str());
00094 hdr->interval = interval_ / 100;
00095 hdr->length = htons(length);
00096 hdr->inet_addr = cl_addr_;
00097 hdr->inet_port = htons(cl_port_);
00098 hdr->name_len = htons(local.length());
00099
00100 memcpy(hdr->sender_name,local.c_str(),local.length());
00101 ::gettimeofday(&data_sent_,0);
00102 return length;
00103 }
00104
00105 }