TCPSpoof.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2006 Andras Varga
00003 //
00004 // This library is free software, you can redistribute it and/or modify
00005 // it under  the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation;
00007 // either version 2 of the License, or any later version.
00008 // The library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 // See the GNU Lesser General Public License for more details.
00012 //
00013 
00014 
00015 #include "TCPSpoof.h"
00016 #include "IPControlInfo.h"
00017 #include "IPv6ControlInfo.h"
00018 
00019 
00020 Define_Module(TCPSpoof);
00021 
00022 void TCPSpoof::initialize()
00023 {
00024     simtime_t t = par("t").doubleValue();
00025     scheduleAt(t, new cMessage("timer"));
00026 }
00027 
00028 void TCPSpoof::handleMessage(cMessage *msg)
00029 {
00030     if (!msg->isSelfMessage())
00031         error("Does not process incoming messages");
00032 
00033     sendSpoofPacket();
00034     delete msg;
00035 }
00036 
00037 void TCPSpoof::sendSpoofPacket()
00038 {
00039     TCPSegment *tcpseg = new TCPSegment("spoof");
00040 
00041     IPvXAddress srcAddr = IPAddressResolver().resolve(par("srcAddress"));
00042     IPvXAddress destAddr = IPAddressResolver().resolve(par("destAddress"));
00043     int srcPort = par("srcPort");
00044     int destPort = par("destPort");
00045     bool isSYN = par("isSYN");
00046     unsigned long seq = par("seqNo").longValue()==-1 ? chooseInitialSeqNum() : par("seqNo").longValue();
00047 
00048     // one can customize the following according to concrete needs
00049     tcpseg->setSrcPort(srcPort);
00050     tcpseg->setDestPort(destPort);
00051     tcpseg->setByteLength(TCP_HEADER_OCTETS);
00052     tcpseg->setSequenceNo(seq);
00053     //tcpseg->setAckNo(...);
00054     tcpseg->setSynBit(isSYN);
00055     tcpseg->setWindow(16384);
00056 
00057     sendToIP(tcpseg, srcAddr, destAddr);
00058 }
00059 
00060 void TCPSpoof::sendToIP(TCPSegment *tcpseg, IPvXAddress src, IPvXAddress dest)
00061 {
00062     EV << "Sending: ";
00063     //printSegmentBrief(tcpseg);
00064 
00065     if (!dest.isIPv6())
00066     {
00067         // send over IPv4
00068         IPControlInfo *controlInfo = new IPControlInfo();
00069         controlInfo->setProtocol(IP_PROT_TCP);
00070         controlInfo->setSrcAddr(src.get4());
00071         controlInfo->setDestAddr(dest.get4());
00072         tcpseg->setControlInfo(controlInfo);
00073 
00074         send(tcpseg,"ipv4Out");
00075     }
00076     else
00077     {
00078         // send over IPv6
00079         IPv6ControlInfo *controlInfo = new IPv6ControlInfo();
00080         controlInfo->setProtocol(IP_PROT_TCP);
00081         controlInfo->setSrcAddr(src.get6());
00082         controlInfo->setDestAddr(dest.get6());
00083         tcpseg->setControlInfo(controlInfo);
00084 
00085         send(tcpseg,"ipv6Out");
00086     }
00087 }
00088 
00089 unsigned long TCPSpoof::chooseInitialSeqNum()
00090 {
00091     // choose an initial send sequence number in the same way as TCP does
00092     return (unsigned long)SIMTIME_DBL(fmod(simTime()*250000.0, 1.0+(double)(unsigned)0xffffffffUL)) & 0xffffffffUL;
00093 }
00094 
00095