TCPTunnel.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
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 _TCPTUNNEL_H_
00018 #define _TCPTUNNEL_H_
00019 
00020 #include <map>
00021 #include <dtn_api.h>
00022 
00023 #include <oasys/debug/Log.h>
00024 #include <oasys/io/TCPClient.h>
00025 #include <oasys/io/TCPServer.h>
00026 #include <oasys/thread/MsgQueue.h>
00027 #include <oasys/thread/Thread.h>
00028 #include <oasys/util/ExpandableBuffer.h>
00029 
00030 #include "IPTunnel.h"
00031 
00032 namespace dtntunnel {
00033 
00037 class TCPTunnel : public IPTunnel {
00038 public:
00040     TCPTunnel();
00041     
00044     void add_listener(in_addr_t listen_addr, u_int16_t listen_port,
00045                       in_addr_t remote_addr, u_int16_t remote_port);
00046     
00048     void handle_bundle(dtn::APIBundle* bundle);
00049 
00050 protected:
00052     class Listener : public oasys::TCPServerThread {
00053     public:
00054         Listener(TCPTunnel* t,
00055                  in_addr_t listen_addr, u_int16_t listen_port,
00056                  in_addr_t remote_addr, u_int16_t remote_port);
00057         
00058         void accepted(int fd, in_addr_t addr, u_int16_t port);
00059 
00060     protected:
00061         TCPTunnel* tcptun_;
00062 
00065         in_addr_t listen_addr_;
00066         u_int16_t listen_port_;
00067         in_addr_t remote_addr_;
00068         u_int16_t remote_port_;
00070     };
00071 
00073     class Connection : public oasys::Thread, public oasys::Logger {
00074     public:
00077         Connection(TCPTunnel* t, dtn_endpoint_id_t* dest_eid,
00078                    in_addr_t client_addr, u_int16_t client_port,
00079                    in_addr_t remote_addr, u_int16_t remote_port);
00080 
00082         Connection(TCPTunnel* t, dtn_endpoint_id_t* dest_eid, int fd,
00083                    in_addr_t client_addr, u_int16_t client_port,
00084                    in_addr_t remote_addr, u_int16_t remote_port);
00085 
00087         ~Connection();
00088         
00090         void handle_bundle(dtn::APIBundle* bundle);
00091         
00092     protected:
00093         friend class TCPTunnel;
00094         
00096         void run();
00097 
00099         TCPTunnel* tcptun_;
00100         
00102         oasys::TCPClient sock_;
00103 
00105         dtn::APIBundleQueue queue_;
00106 
00108         typedef std::map<u_int32_t, dtn::APIBundle*> ReorderTable;
00109         ReorderTable reorder_table_;
00110 
00112         u_int32_t next_seqno_;
00113 
00115         dtn_endpoint_id_t dest_eid_;
00116         in_addr_t         client_addr_;
00117         u_int16_t         client_port_;
00118         in_addr_t         remote_addr_;
00119         u_int16_t         remote_port_;
00120     };
00121 
00123     void new_connection(Connection* c);
00124 
00126     void kill_connection(Connection* c);
00127 
00129     struct ConnKey {
00130         ConnKey()
00131             : client_addr_(INADDR_NONE), client_port_(0),
00132               remote_addr_(INADDR_NONE), remote_port_(0) {}
00133 
00134         ConnKey(in_addr_t client_addr, u_int16_t client_port,
00135                 in_addr_t remote_addr, u_int16_t remote_port)
00136             : client_addr_(client_addr),
00137               client_port_(client_port),
00138               remote_addr_(remote_addr),
00139               remote_port_(remote_port) {}
00140 
00141         bool operator<(const ConnKey& other) const {
00142             return ((client_addr_ < other.client_addr_) ||
00143                     (client_port_ < other.client_port_) ||
00144                     (remote_addr_ < other.remote_addr_) ||
00145                     (remote_port_ < other.remote_port_));
00146         }
00147         
00148         in_addr_t client_addr_;
00149         u_int16_t client_port_;
00150         in_addr_t remote_addr_;
00151         u_int16_t remote_port_;
00152     };
00153 
00155     typedef std::map<ConnKey, Connection*> ConnTable;
00156     ConnTable connections_;
00157 
00159     oasys::SpinLock lock_;
00160 };
00161 
00162 } // namespace dtntunnel
00163 
00164 #endif /* _TCPTUNNEL_H_ */

Generated on Thu Jun 7 16:56:53 2007 for DTN Reference Implementation by  doxygen 1.5.1