hi..i am new to network prog in c++...this thing is really eatn my mind...ive created code for creating a tcp syn packet.....sending part is fine...but cannot recieve.....also when i use IPPROTO_TCP during socket description sending also fails.......the source code is till recvfrom..since it returns 0 and not the incoming packet size....plz help..
the source code:
struct ip_header
{
unsigned char ver_ihl; // Version (4 bits) + Internet header length (4 bits)
unsigned char tos; // Type of service
unsigned short tlen; // Total length
unsigned short identification; // Identification
unsigned short flags_fo; // Flags (3 bits) + Fragment offset (13 bits)
unsigned char ttl; // Time to live
unsigned char proto; // Protocol
unsigned short crc; // Header checksum
unsigned int saddr; // Source address
unsigned int daddr; // Destination address
// u_int op_pad; // Option + Padding
};
struct tcp_header //20 bytes
{
unsigned short sport; //Source port
unsigned short dport; //Destination port
unsigned long seqno; //Sequence no
unsigned long ackno; //Ack no
unsigned char offset; //Higher level 4 bit indicates data offset
unsigned char flag; //Message flag
//FIN - 0x01
//SYN - 0x02
//RST - 0x04
//PUSH- 0x08
//ACK- 0x10
//URG- 0x20
//ACE- 0x40
//CWR- 0x80
unsigned short win;
unsigned short checksum;
unsigned short uptr;
};
struct pseudo_header //12 bytes
{
unsigned int saddr; // Source address
unsigned int daddr; // Destination address
unsigned char zero;
unsigned char proto; // Protocol
unsigned short tcp_len;
tcp_header tcp;
};
unsigned short ComputeChecksum(u_short *data,int size);
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(iResult != NO_ERROR)
printf("Error at WSAStartup().\n");
else
printf("WSAStartup() is OK.\n");
char *ip1="10.1.77.163";
SOCKET sock;
sock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW ); //Create a raw socket which will use ICMP
SOCKADDR_IN dest; //Dest address to send the ICMP request
dest.sin_addr.S_un.S_addr = inet_addr (ip1);
dest.sin_family = AF_INET;
dest.sin_port =htons (80) ;
SOCKADDR_IN dest1;
int len=sizeof(dest1);
fd_set fdRead;
int nResult;
ip_header ih,*rih;
tcp_header th,*rth;
pseudo_header psh;
ih.flags_fo=htons(0x4000); //For TCP Flag fixed
// ih.flags_fo=htons(0x0000); //For ICMP Flag fixed
ih.identification=htons(0x0150); //Any number
ih.tlen=htons(IP_LENGTH+TCP_LENGTH); //Depends upon data and tcp protocol
ih.tos=0;
ih.ttl=128;
ih.ver_ihl=0x45; //Version (v4) and header length(5 nibbles)
ih.crc=0;
ih.crc=ComputeChecksum((unsigned short *)&ih,IP_LENGTH);
memset(&th,0,sizeof(th));
th.sport=rand(); // source port
th.dport=htons(21); // dest port changes dynamically
th.ackno=htonl(0);
th.seqno=htonl(5555l);
th.flag=TCP_SYN; // SYN packet
th.offset=0x50; // data offset
th.uptr=0;
th.win=htons(512) ;
th.checksum=0;
//set up the pseudo header
memset(&psh,0,sizeof(psh));
psh.saddr=ih.saddr;
psh.daddr=ih.daddr;
psh.proto=ih.proto;
psh.zero=0;
psh.tcp_len=htons(TCP_LENGTH); //Length of Tcp header + Data in OCTATES
memcpy(&psh.tcp,&th,TCP_LENGTH);
if (nResult == SOCKET_ERROR)
{
printf( "An error occured in sendto operation: " );
}
else
printf("\npacket %d sent and size is:%d",j,nResult);
}
char frame1[5000] ;
int nResult1;
if ( nResult1 = recvfrom (sock, frame1, 5000, 0, (SOCKADDR *)&dest1, &len) == SOCKET_ERROR)
{
printf( "An error occured in recvfrom operation: ");
//nResult1 is 0 and not the incoming packet size.
}
getch();
}
unsigned short ComputeChecksum(unsigned short *data,int size)
{
register int nleft=size;
register u_short *w=data;
register int sum=0;
unsigned short answer =0;
Bookmarks