EthConvergenceLayer.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 #ifndef _ETH_CONVERGENCE_LAYER_H_
00039 #define _ETH_CONVERGENCE_LAYER_H_
00040 
00041 // Only works on Linux (for now)
00042 #ifdef __linux__ 
00043 
00044 #include <sys/types.h>
00045 #include <netinet/in.h>
00046 #include <linux/if.h>
00047 
00048 #include <oasys/thread/Thread.h>
00049 #include <oasys/thread/Timer.h>
00050 
00051 #include "ConvergenceLayer.h"
00052 #include "naming/EthernetScheme.h" // for eth_addr_t
00053 
00069 namespace dtn {
00070 
00071 class EthConvergenceLayer : public ConvergenceLayer {
00072 
00073 public:
00074     class BeaconTimer;
00075 
00079     static const u_int8_t  ETHCL_VERSION = 0x01;
00080     static const u_int16_t ETHERTYPE_DTN = 0xd710;
00081 
00082     static const u_int8_t  ETHCL_BEACON = 0x01;
00083     static const u_int8_t  ETHCL_BUNDLE = 0x02;
00084 
00085     static const u_int32_t ETHCL_BEACON_TIMEOUT_INTERVAL = 2500; // 2.5 seconds
00086 
00087     static const u_int16_t MAX_ETHER_PACKET = 1518;
00088 
00092     static const u_int MAX_BUNDLE_LEN = 65507;
00093     
00097     struct EthCLHeader {
00098         u_int8_t  version;              
00099         u_int8_t  type;                 
00100         u_int16_t _padding2;            
00101         u_int32_t bundle_id;            
00102     } __attribute__((packed));
00103 
00108     class EthCLInfo : public CLInfo {
00109       public:
00110         EthCLInfo(char* if_name) {
00111             memset(if_name_,0,IFNAMSIZ);
00112             strcpy(if_name_,if_name);
00113             timer    = NULL;
00114         }
00115 
00116         ~EthCLInfo() {
00117           if(timer)
00118               delete timer;
00119         }
00120         
00121         // Name of the device 
00122         char if_name_[IFNAMSIZ];
00123 
00124         BeaconTimer* timer;
00125     };
00126 
00130     EthConvergenceLayer();
00131         
00135     bool interface_up(Interface* iface, int argc, const char* argv[]);
00136 
00140     bool interface_down(Interface* iface);
00141     
00146     bool open_contact(const ContactRef& contact);
00147     
00151     bool close_contact(const ContactRef& contact);
00152 
00156     void send_bundle(const ContactRef& contact, Bundle* bundle);
00157 
00167     class Params : public CLInfo {
00168     public:
00169         u_int32_t beacon_interval_;       
00170     };
00171     
00175     static Params defaults_;
00176     
00181     class Receiver : public CLInfo,
00182                      public oasys::Logger,
00183                      public oasys::Thread
00184     {
00185     public:
00189         Receiver(const char *if_name, EthConvergenceLayer::Params* params);
00190 
00194         virtual ~Receiver() {}
00195         
00205         void run();
00206         
00207     protected:
00211         void process_data(u_char* bp, size_t len);
00212         char if_name_[IFNAMSIZ];
00213     };
00214 
00215 
00216 
00226     class Sender : public CLInfo,
00227                    public oasys::Logger
00228     {
00229     public:
00233         Sender(char* if_name, const ContactRef& contact);
00234 
00238         virtual ~Sender() {}
00239         
00240     protected:
00241         friend class EthConvergenceLayer;
00242         
00246         bool send_bundle(Bundle* bundle);
00247 
00249         ContactRef contact_;
00250         
00252         int sock_;
00253 
00255         eth_addr_t src_hw_addr_;
00256         eth_addr_t dst_hw_addr_; 
00257 
00259         char if_name_[IFNAMSIZ]; 
00260         
00261         char canary_[7];
00262 
00268         u_char buf_[EthConvergenceLayer::MAX_BUNDLE_LEN];
00269     };
00270 
00275     class Beacon : public oasys::Logger,
00276                    public oasys::Thread
00277     {
00278     public:
00279         Beacon(const char* if_name, unsigned int beacon_interval);
00280 
00281         virtual ~Beacon() {};
00282 
00283     private:
00284         virtual void run();
00285         char if_name_[IFNAMSIZ];
00286         unsigned int beacon_interval_;
00287     };
00288 
00289     class BeaconTimer : public oasys::Logger, public oasys::Timer, public CLInfo {
00290     public:
00291         char * next_hop_;
00292 
00293         BeaconTimer(char * next_hop);
00294         ~BeaconTimer();
00295 
00296         void timeout(const struct timeval& now);
00297 
00298         Timer* copy();
00299     };    
00300 
00301 protected:
00305     bool parse_params(Params* params, int argc, const char** argv,
00306                       const char** invalidp);
00307 
00308 private:
00309     Beacon *if_beacon_;
00310 };
00311 
00312 
00313 } // namespace dtn
00314 
00315 #endif // __linux
00316 
00317 #endif /* _ETH_CONVERGENCE_LAYER_H_ */

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1