Новичок
Профиль
Группа: Участник
Сообщений: 10
Регистрация: 20.1.2011
Репутация: нет Всего: нет
|
СРОЧНО ПОМОГИТЕ! ПОЧЕМУ ПРИЕМ НЕ ПРОХОДИТ, ХОТЯ ОТПРАВКА С КЛИЕНТА ТОЧНО ИДЕТ. Код | #include <cstdlib> #include <iostream> #define REQ_DATASIZE 27 #define ICMP_ECHOREPLY 0 #define ICMP_ECHOREQ 8 #define SM_EVENT WM_USER+192
#include <winsock2.h> #include <Ws2tcpip.h>
#pragma pack(1)
typedef struct tagIPHDR { u_char version; // version number u_char TOS; // Type Of Service u_short TotLen; // Total Length u_short ID; // Identification u_short FlagOff; // Flags and Fragment Offset u_char TTL; // Time To Live u_char Protocol; // Protocol u_short Checksum; // Checksum struct in_addr iaSrc; // Internet Address - Source struct in_addr iaDst; // Internet Address - Destination }IPHDR, *PIPHDR;
// ICMP Header - RFC 792 typedef struct tagICMPHDR { u_char Type; // Type u_char Code; // Code u_short Checksum; // Checksum u_short ID; // Identification u_short Seq; // Sequence char Data; // Data }ICMPHDR, *PICMPHDR;
#define REQ_DATASIZE 27 // Echo Request Data size
// ICMP Echo Reply.. typedef struct tagECHOREPLY { IPHDR ipHdr; IPHDR ipReplyHdr; ICMPHDR icmpHdr; DWORD dwTime; char cData[REQ_DATASIZE]; char cFiller[256]; }ECHOREPLY,*PECHOREPLY;
//ICMP Echo Request... typedef struct tagECHOREQUEST { ICMPHDR icmpHdr; DWORD dwTime; char cData[REQ_DATASIZE]; }ECHOREQUEST, *PECHOREQUEST;
//Here it is object client address... sockaddr_in objClientAddress; sockaddr_in objServAddress; //Here it initializes the socket.. int OnInitializeSocketENV(); //Here it creates the socket.. int OnCreateSocket(SOCKET& rawSocket); //Here it binds the socket.. int BindSocket(SOCKET& rawSocket); //Here it receives the echo message int RecvEchoMessage(SOCKET& rawSocket);
#pragma pack()
u_short in_cksum(u_short *addr, int len); using namespace std;
int main(int argc, char *argv[]) { //Here it is a instance os a socket.. SOCKET rawSocket; /* //Here it initializes the socket environment.. if(!OnInitializeSocketENV()) return 0; //Here it creates the socket.. if(!OnCreateSocket(rawSocket)) return 0 ; //Here it binds the socket.. if(!BindSocket(rawSocket)) return 0; if(!RecvEchoMessage(rawSocket)) return 0;*/ OnInitializeSocketENV(); //Here it creates the socket.. OnCreateSocket(rawSocket); //Here it binds the socket.. BindSocket(rawSocket); printf("Socket is listening..\n"); //Here it receives the echo response.. RecvEchoMessage(rawSocket); closesocket(rawSocket); WSACleanup(); getchar(); return 0; } //Here it initializes the socket environment... int OnInitializeSocketENV() { WSADATA wsaData; int errcod; errcod = WSAStartup(MAKEWORD(2, 2), &wsaData); if(errcod!=0) { cout<<"WinSock not found!"; return 0;} } //Here it is checking for version requested... /*if(wsaData.wVersion != wVersionRequested) { printf("Error winsock version not supported\n"); return false; } return true; }*/ //Here it creates the socket object... int OnCreateSocket(SOCKET& rawSocket) { rawSocket = socket(AF_INET, SOCK_RAW,IPPROTO_ICMP); if(rawSocket == INVALID_SOCKET) { cout<<"Error create socket"; } else cout<<"Create socket succesfull"; cout<<endl; }
//Here it binds the socket.. int BindSocket(SOCKET& rawSocket) { //Here preparation is done to bind the socket. objServAddress.sin_addr.s_addr = INADDR_ANY;//inet_addr("192.168.1.107"); objServAddress.sin_family = AF_INET; objServAddress.sin_port = 0; //Here it binds the socket... int nRetValue = bind(rawSocket,(struct sockaddr*)&objServAddress,sizeof(objServAddress)); if(nRetValue == SOCKET_ERROR) { printf("Socket binding is failed\n"); closesocket(rawSocket); rawSocket = 1; return false; } return(true); }
//Here it receives the echo message int RecvEchoMessage(SOCKET& rawSocket) { ECHOREPLY echoReply; int nAddrLen = sizeof(struct sockaddr_in); int nRet; //char on=1; //setsockopt(rawSocket,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on)); if((nRet = recvfrom(rawSocket,(LPSTR)&echoReply,sizeof(ECHOREPLY),0,(struct sockaddr *)&objClientAddress, &nAddrLen)) > 0) { printf("\nEcho is received from..: %s\n",inet_ntoa(objClientAddress.sin_addr)); printf("\n..............IPHEADER INFO.........................\n"); printf("size of IPHeder=%d\n",sizeof(echoReply.ipHdr)); printf("IP Header Version :%d\n",echoReply.ipHdr.version); printf("Type of service :%d\n",echoReply.ipHdr.TOS); printf("TTL :%d\n",echoReply.ipHdr.TTL);
//Here it sends the echo request.. if(echoReply.ipHdr.Protocol == IPPROTO_ICMP) printf("It is a ICMP protocol %d\n",echoReply.ipHdr.Protocol); else printf("It is not a ICMP protocol:%d\n",echoReply.ipHdr.Protocol); printf("Here is source address :%s\n",inet_ntoa(echoReply.ipHdr.iaSrc)); printf("Here is destination address:%s\n",inet_ntoa(echoReply.ipHdr.iaDst)); printf("\n\n..............ICMP HEADER INFO.........................\n"); printf("Message Type...: %d\n",echoReply.icmpHdr.Type); printf("Message Code...: %d\n",echoReply.icmpHdr.Code); printf("ID.............: %d\n",echoReply.icmpHdr.ID); printf("SEQ............: %d\n",echoReply.icmpHdr.Seq); printf("CHECKSUM.......: %d\n",echoReply.icmpHdr.Checksum); printf("DATA sent......: %s\n",echoReply.cData); //Here it constructs the echo reply and sends the same back to the client ECHOREQUEST echoReq; int nId = 1; int nSeq = 1; int nRet,nIndex; //Fill in echo request.. echoReq.icmpHdr.Type = ICMP_ECHOREPLY; echoReq.icmpHdr.Code = 0; echoReq.icmpHdr.Checksum = 0; echoReq.icmpHdr.ID = nId++; echoReq.icmpHdr.Seq = nSeq++; //Fill in some data to send.. for(nIndex = 0; nIndex < REQ_DATASIZE-1; nIndex++) { echoReq.cData[nIndex] = 'a'+nIndex; } //save tick count when sent.. echoReq.dwTime = GetTickCount(); //Put data in packet and compute checksum echoReq.icmpHdr.Checksum = in_cksum((u_short*)&echoReq,sizeof(ECHOREQUEST)); if((nRet = sendto(rawSocket,(LPSTR)&echoReq,sizeof(ECHOREQUEST),0,(struct sockaddr *)&objClientAddress, nAddrLen)) > 0) { printf("\nReply is sent to...:%s\n",inet_ntoa(objClientAddress.sin_addr)); } } if(nRet == SOCKET_ERROR) { int nError = WSAGetLastError(); printf("Failure while sending and receiving data, Error:%d\n",nError); return false; } printf("Received value is...:%d",nRet); return true; } //Check sum is done for ICMP Request.. u_short in_cksum(u_short *addr, int len) { register int nleft = len; register u_short *w = addr; register u_short answer; register int sum = 0;
/* * Our algorithm is simple, using a 32 bit accumulator (sum), * we add sequential 16 bit words to it, and at the end, fold * back all the carry bits from the top 16 bits into the lower * 16 bits. */ while( nleft > 1 ) { sum += *w++; nleft -= 2; }
/* mop up an odd byte, if necessary */ if( nleft == 1 ) { u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ; sum += u; }
/* * add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = ~sum; /* truncate to 16 bits */
return (answer); }
//Here it waits for echo reply. int WaitForEchoReply(SOCKET& objRawsocket);
//Here it waits for echo reply... int WaitForEchoReply(SOCKET& objRawsocket) { struct timeval Timeout; fd_set readfds; readfds.fd_count = 1; readfds.fd_array[0] = objRawsocket; Timeout.tv_sec = 5; Timeout.tv_usec = 0; int nSelect = select(0, &readfds, NULL, NULL, NULL); int nErrorCode = WSAGetLastError(); printf("Select Error Code:\n",nErrorCode); return(select(1, &readfds, NULL, NULL, NULL)); }
|
|