00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _IP_DISCOVERY_H_
00018 #define _IP_DISCOVERY_H_
00019
00020 #include <oasys/thread/Thread.h>
00021 #include <oasys/thread/Notifier.h>
00022 #include <oasys/io/NetUtils.h>
00023 #include <oasys/io/UDPClient.h>
00024
00025 #include "discovery/Discovery.h"
00026
00027 namespace dtn {
00028
00035 class IPDiscovery : public Discovery,
00036 public oasys::Thread
00037 {
00038 public:
00039
00044 struct DiscoveryHeader
00045 {
00046 u_int8_t cl_type;
00047 u_int8_t interval;
00048 u_int16_t length;
00049 u_int32_t inet_addr;
00050 u_int16_t inet_port;
00051 u_int16_t name_len;
00052 char sender_name[0];
00053 }
00054 __attribute__((packed));
00055
00059 typedef enum
00060 {
00061 UNDEFINED = 0,
00062 TCPCL = 1,
00063 UDPCL = 2,
00064 }
00065 cl_type_t;
00066
00067 static const char* type_to_str(cl_type_t t)
00068 {
00069 switch(t) {
00070 case TCPCL: return "tcp";
00071 case UDPCL: return "udp";
00072 case UNDEFINED:
00073 default: PANIC("Undefined cl_type_t %d",t);
00074 }
00075 NOTREACHED;
00076 }
00077
00078 static cl_type_t str_to_type(const char* cltype)
00079 {
00080 if (strncmp(cltype,"tcp",3) == 0)
00081 return TCPCL;
00082 else
00083 if (strncmp(cltype,"udp",3) == 0)
00084 return UDPCL;
00085 else
00086 PANIC("Unsupported CL type %s",cltype);
00087 NOTREACHED;
00088 }
00089
00093 void shutdown() { shutdown_ = true; socket_.get_notifier()->notify(); }
00094
00095 virtual ~IPDiscovery() {}
00096
00097 protected:
00098 friend class Discovery;
00099
00100 IPDiscovery(const std::string& name);
00101
00106 bool configure(int argc, const char* argv[]);
00107
00111 void run();
00112
00117 bool parse_advertisement(u_char* buf, size_t len,
00118 in_addr_t remote_addr,
00119 u_int8_t& cl_type,
00120 std::string& nexthop,
00121 EndpointID& remote_eid);
00122
00126 void handle_announce()
00127 {
00128 socket_.get_notifier()->notify();
00129 }
00130
00131 volatile bool shutdown_;
00132 in_addr_t local_addr_;
00133 u_int16_t port_;
00134 in_addr_t remote_addr_;
00135 u_int mcast_ttl_;
00136 oasys::UDPClient socket_;
00137 };
00138
00139 }
00140
00141 #endif