proj2-An example

Download Report

Transcript proj2-An example

Parts of example codes
Some useful structures
• ConnectionList<State>
“constate.h”
• TCPState
“tcpstate.h”
• ConnectionToStateMapping<State>
“constate.h”
• IPHeader
“ip.h”
• TCPHeader
“tcp.h”
• Connection
“sockint.h”
•
•
•
•
•
•
void craftPacketHeader(Packet *psend,ConnectionToStateMapping<TCPState> conState,
TCPHeader th, int type, unsigned short no_of_bytes )
{
//Packet psend;
Connection c = conState.connection;
IPHeader ih;
TCPHeader tcph;
•
•
•
•
•
•
•
•
unsigned short totalLength = no_of_bytes ;
unsigned int acknum,seqnum;
unsigned char flag=0;
unsigned short winsize,up;
TCPOptions opt;
unsigned int time = Time();
int time_offset = 4;
unsigned char hlen=5;
•
•
•
•
•
•
th.GetSeqNum(acknum);// assign value to acknum
th.GetAckNum(seqnum);
th.GetWinSize(winsize);
th.GetOptions(opt);
th.GetUrgentPtr(up);
th.GetHeaderLen(hlen);
•
•
cout<<acknum<<endl;
seqnum = conState.state.GetLastSent();
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
if ( type == SYN_HEADER )
{
totalLength = SYN_HEADER_LENGTH;
seqnum = conState.state.GetLastSent();
SET_SYN(flag);
time_offset = 8;
}
else if ( type == ACK_HEADER || type == PSH_ACK_HEADER)
{
totalLength += ACK_HEADER_LENGTH;
SET_ACK(flag);
acknum = conState.state.GetLastRecvd()+ 1;
seqnum += 1;
if(type == PSH_ACK_HEADER )
SET_PSH(flag);
}
else if ( type == FIN_ACK_HEADER )
{
totalLength = FIN_HEADER_LENGTH;
SET_ACK(flag);
SET_FIN(flag);
acknum = conState.state.GetLastRecvd() + 1;
seqnum += 1;
}
else if ( type == SYN_ACK_HEADER )
{
totalLength = SYN_HEADER_LENGTH;
seqnum = conState.state.GetLastSent();
acknum = conState.state.GetLastRecvd() + 1;
SET_ACK(flag);
SET_SYN(flag);
time_offset = 8;
}
•
•
•
•
•
•
ih.SetProtocol(IP_PROTO_TCP);
ih.SetSourceIP(c.src);
ih.SetDestIP(c.dest);
ih.SetTotalLength( totalLength );
// push it onto the packet
(*psend).PushFrontHeader(ih);
•
•
tcph.SetDestPort(c.destport,(*psend));
tcph.SetSourcePort(c.srcport,(*psend));
•
•
tcph.SetAckNum(acknum,(*psend));
tcph.SetSeqNum(seqnum,(*psend));
•
tcph.SetFlags(flag,(*psend));
•
tcph.SetWinSize(winsize-12,(*psend));
•
•
memcpy((void *)(opt.data+time_offset+4),(void *)(opt.data+time_offset),4);
*(int *)(opt.data+time_offset) = htonl(time);
•
•
tcph.SetOptions(opt);
tcph.SetUrgentPtr(up,(*psend));
•
•
tcph.SetHeaderLen(hlen,(*psend));
tcph.RecomputeChecksum((*psend));
•
•
•
•
// Now we want to have the tcp header BEHIND the IP header
(*psend).PushBackHeader(tcph);
tcph.Print(cout);
//return psend;
•
}
Notice
• Pls make your codes well commented.