TCP.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #ifndef __INET_TCPMAIN_H
00019 #define __INET_TCPMAIN_H
00020 
00021 #include <map>
00022 #include <set>
00023 #include <omnetpp.h>
00024 #include "IPvXAddress.h"
00025 
00026 
00027 class TCPConnection;
00028 class TCPSegment;
00029 
00030 // macro for normal ev<< logging (Note: deliberately no parens in macro def)
00031 #define tcpEV (ev.disable_tracing||TCP::testing)?ev:ev
00032 
00033 // macro for more verbose ev<< logging (Note: deliberately no parens in macro def)
00034 #define tcpEV2 (ev.disable_tracing||TCP::testing||!TCP::logverbose)?ev:ev
00035 
00036 // testingEV writes log that automated test cases can check (*.test files)
00037 #define testingEV (ev.disable_tracing||!TCP::testing)?ev:ev
00038 
00039 
00040 
00041 
00042 
00096 class INET_API TCP : public cSimpleModule
00097 {
00098   public:
00099     struct AppConnKey  // XXX this class is redundant since connId is already globally unique
00100     {
00101         int appGateIndex;
00102         int connId;
00103 
00104         inline bool operator<(const AppConnKey& b) const
00105         {
00106             if (appGateIndex!=b.appGateIndex)
00107                 return appGateIndex<b.appGateIndex;
00108             else
00109                 return connId<b.connId;
00110         }
00111 
00112     };
00113     struct SockPair
00114     {
00115         IPvXAddress localAddr;
00116         IPvXAddress remoteAddr;
00117         int localPort;   // -1: unspec
00118         int remotePort;  // -1: unspec
00119 
00120         inline bool operator<(const SockPair& b) const
00121         {
00122             if (remoteAddr!=b.remoteAddr)
00123                 return remoteAddr<b.remoteAddr;
00124             else if (localAddr!=b.localAddr)
00125                 return localAddr<b.localAddr;
00126             else if (remotePort!=b.remotePort)
00127                 return remotePort<b.remotePort;
00128             else
00129                 return localPort<b.localPort;
00130         }
00131     };
00132 
00133   protected:
00134     typedef std::map<AppConnKey,TCPConnection*> TcpAppConnMap;
00135     typedef std::map<SockPair,TCPConnection*> TcpConnMap;
00136 
00137     TcpAppConnMap tcpAppConnMap;
00138     TcpConnMap tcpConnMap;
00139 
00140     ushort lastEphemeralPort;
00141     std::multiset<ushort> usedEphemeralPorts;
00142 
00143   protected:
00145     virtual TCPConnection *createConnection(int appGateIndex, int connId);
00146 
00147     // utility methods
00148     virtual TCPConnection *findConnForSegment(TCPSegment *tcpseg, IPvXAddress srcAddr, IPvXAddress destAddr);
00149     virtual TCPConnection *findConnForApp(int appGateIndex, int connId);
00150     virtual void segmentArrivalWhileClosed(TCPSegment *tcpseg, IPvXAddress src, IPvXAddress dest);
00151     virtual void removeConnection(TCPConnection *conn);
00152     virtual void updateDisplayString();
00153 
00154   public:
00155     static bool testing;    // switches between tcpEV and testingEV
00156     static bool logverbose; // if !testing, turns on more verbose logging
00157 
00158     bool recordStatistics;  // output vectors on/off
00159 
00160   public:
00161     TCP() {}
00162     virtual ~TCP();
00163 
00164   protected:
00165     virtual void initialize();
00166     virtual void handleMessage(cMessage *msg);
00167     virtual void finish();
00168 
00169   public:
00174     virtual void addSockPair(TCPConnection *conn, IPvXAddress localAddr, IPvXAddress remoteAddr, int localPort, int remotePort);
00175 
00180     virtual void updateSockPair(TCPConnection *conn, IPvXAddress localAddr, IPvXAddress remoteAddr, int localPort, int remotePort);
00181 
00186     virtual void addForkedConnection(TCPConnection *conn, TCPConnection *newConn, IPvXAddress localAddr, IPvXAddress remoteAddr, int localPort, int remotePort);
00187 
00191     virtual ushort getEphemeralPort();
00192 };
00193 
00194 #endif
00195 
00196