TCP_NSC.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Sam Jansen, Andras Varga,
00003 //               2009 Zoltan Bojthe
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00020 
00021 #ifndef __TCP_NSC_H
00022 #define __TCP_NSC_H
00023 
00024 #include <map>
00025 #include <list>
00026 #include <omnetpp.h>
00027 
00028 #include "INETDefs.h"
00029 #include "IPvXAddress.h"
00030 
00031 #include <sim_interface.h> // NSC. We need this here to derive from classes
00032 #include "TCP_NSC_Connection.h"
00033 
00034 // forward declarations:
00035 class TCPCommand;
00036 class TCPSegment;
00037 
00041 class INET_API TCP_NSC : public cSimpleModule, ISendCallback, IInterruptCallback
00042 {
00043   protected:
00044     enum {MAX_SEND_BYTES = 500000};
00045 
00046   public:
00047     TCP_NSC();
00048     virtual ~TCP_NSC();
00049 
00050     // Implement NSC callbacks:
00051 
00052     //   Implement ISendCallback:
00053     virtual void send_callback(const void *, int);
00054 
00055     //   Implement IInterruptCallback:
00056     virtual void wakeup();
00057     virtual void gettime(unsigned int *, unsigned int *);
00058 
00059   protected:
00060     // called by the OMNeT++ simulation kernel:
00061 
00062     virtual void initialize();
00063     virtual void handleMessage(cMessage *msgP);
00064     virtual void finish();
00065 
00066     // internal utility functions:
00067 
00068     void changeAddresses(TCP_NSC_Connection &connP,
00069         const TCP_NSC_Connection::SockPair &inetSockPairP,
00070         const TCP_NSC_Connection::SockPair &nscSockPairP);
00071 
00072     // find a TCP_NSC_Connection by connection ID
00073     TCP_NSC_Connection *findAppConn(int connIdP);
00074 
00075     // find a TCP_NSC_Connection by inet sockpair
00076     TCP_NSC_Connection *findConnByInetSockPair(TCP_NSC_Connection::SockPair const & sockPairP);
00077 
00078     // find a TCP_NSC_Connection by nsc sockpair
00079     TCP_NSC_Connection *findConnByNscSockPair(TCP_NSC_Connection::SockPair const & sockPairP);
00080 
00081     virtual void updateDisplayString();
00082     void removeConnection(int connIdP);
00083     void printConnBrief(TCP_NSC_Connection& connP);
00084     void loadStack(const char* stacknameP, int bufferSizeP);
00085 
00086     void handleAppMessage(cMessage *msgP);
00087     void handleIpInputMessage(TCPSegment* tcpsegP);
00088 
00089     // function to be called back from the NSC stack:
00090 
00091     void sendToIP(const void *dataP, int lenP);
00092 
00093     // to be refined...
00094 
00095     void processAppCommand(TCP_NSC_Connection& connP, cMessage *msgP);
00096 
00097     // to be refined and filled in with calls into the NSC stack
00098 
00099     void process_OPEN_ACTIVE(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cMessage *msgP);
00100     void process_OPEN_PASSIVE(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cMessage *msgP);
00101     void process_SEND(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cPacket *msgP);
00102     void process_CLOSE(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cMessage *msgP);
00103     void process_ABORT(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cMessage *msgP);
00104     void process_STATUS(TCP_NSC_Connection& connP, TCPCommand *tcpCommandP, cMessage *msgP);
00105 
00106     void do_SEND(TCP_NSC_Connection& connP);
00107     void do_SEND_all();
00108 
00109     // return mapped remote IP in host byte order
00110     // if addrP not exists in map, it's create a new nsc addr, and insert it to map
00111     u_int32_t mapRemote2Nsc(IPvXAddress const& addrP);
00112 
00113     // return original remote ip from mapped ip
00114     // assert if not exists in map
00115     // nscAddrP has IP in host byte order
00116     IPvXAddress const & mapNsc2Remote(u_int32_t nscAddrP);
00117     // x == mapNsc2Remote(mapRemote2Nsc(x))
00118 
00119     // send a connection established msg to application layer
00120     void sendEstablishedMsg(TCP_NSC_Connection &connP);
00121 
00122   protected:
00123     typedef std::map<int,TCP_NSC_Connection> TcpAppConnMap; // connId-to-TCP_NSC_Connection
00124     typedef std::map<u_int32_t, IPvXAddress> Nsc2RemoteMap;
00125     typedef std::map<IPvXAddress, u_int32_t> Remote2NscMap;
00126     typedef std::map<TCP_NSC_Connection::SockPair, int> SockPair2ConnIdMap;
00127 
00128     // Maps:
00129     TcpAppConnMap tcpAppConnMapM;
00130     SockPair2ConnIdMap inetSockPair2ConnIdMapM;
00131     SockPair2ConnIdMap nscSockPair2ConnIdMapM;
00132 
00133     Nsc2RemoteMap nsc2RemoteMapM;
00134     Remote2NscMap remote2NscMapM;
00135 
00136     INetStack *pStackM;
00137 
00138     cMessage *pNsiTimerM;
00139 
00140     void decode_tcpip(const void *, int);
00141     void decode_tcp(const void *, int);
00142 
00143   public:
00144     static bool testingS;    // switches between tcpEV and testingEV
00145     static bool logverboseS; // if !testingS, turns on more verbose logging
00146 
00147   protected:
00148     bool isAliveM;   // true when I between initialize() and finish()
00149 
00150     int curAddrCounterM; // incr, when set curLocalAddr, decr when "felhasznaltam"
00151     TCP_NSC_Connection *curConnM; // store current connection in connect/listen command
00152 
00153     static const IPvXAddress localInnerIpS; // local NSC IP addr
00154     static const IPvXAddress localInnerGwS; // local NSC gateway IP addr
00155     static const IPvXAddress localInnerMaskS; // local NSC Network Mask
00156     static const IPvXAddress remoteFirstInnerIpS; // first remote NSC IP addr
00157 
00158     static const char * stackNameParamNameS; // name of stackname parameter
00159     static const char * bufferSizeParamNameS; // name of buffersize parameter
00160 
00161     // statistics
00162     cOutVector *sndWndVector;   // snd_wnd
00163     cOutVector *rcvWndVector;   // rcv_wnd
00164     cOutVector *rcvAdvVector;   // current advertised window (=rcv_avd)
00165     cOutVector *sndNxtVector;   // sent seqNo
00166     cOutVector *sndAckVector;   // sent ackNo
00167     cOutVector *rcvSeqVector;   // received seqNo
00168     cOutVector *rcvAckVector;   // received ackNo (= snd_una)
00169     cOutVector *unackedVector;  // number of bytes unacknowledged
00170 
00171     cOutVector *dupAcksVector;   // current number of received dupAcks
00172     cOutVector *pipeVector;      // current sender's estimate of bytes outstanding in the network
00173     cOutVector *sndSacksVector;  // number of sent Sacks
00174     cOutVector *rcvSacksVector;  // number of received Sacks
00175     cOutVector *rcvOooSegVector; // number of received out-of-order segments
00176 
00177     cOutVector *sackedBytesVector;                // current number of received sacked bytes
00178     cOutVector *tcpRcvQueueBytesVector;   // current amount of used bytes in tcp receive queue
00179     cOutVector *tcpRcvQueueDropsVector;   // number of drops in tcp receive queue
00180 };
00181 
00182 #endif