IPDiscovery.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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;         // Type of CL offered
00047         u_int8_t interval;        // 100ms units
00048         u_int16_t length;         // total length of packet
00049         u_int32_t inet_addr;      // IPv4 address of CL
00050         u_int16_t inet_port;      // IPv4 port of CL
00051         u_int16_t name_len;       // length of EID
00052         char sender_name[0];      // DTN URI of beacon sender
00053     }
00054     __attribute__((packed));
00055 
00059     typedef enum
00060     {
00061         UNDEFINED = 0,
00062         TCPCL     = 1, // TCP Convergence Layer
00063         UDPCL     = 2, // UDP Convergence Layer
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 } // namespace dtn
00140 
00141 #endif /* _IP_DISCOVERY_H_ */

Generated on Sat Sep 8 08:36:17 2007 for DTN Reference Implementation by  doxygen 1.5.3