Home > Software engineering >  Raw socket accept less than the ICMP error messages
Raw socket accept less than the ICMP error messages

Time:10-07

Is willing to imitate the trace route, using the original socket, send an ICMP echo request, with the increasing TTL, obtained via routing,
However, the program can't recvfrom until icmp error messages, as usual, will receive a timeout error message types, but don't get all the time,
Program is already an administrator to run,
And the echo response can be normal in the program to recvfrom,
Also try to use the wireshark locally caught, found the ICMP error message feedback, is not remote routing problems, such as
Very headache, do not check online also similar mistakes, hope you can tell,
 
Void ConnectToHost (char * strHost)
{

M_sockRaw=WSASocket (AF_INET SOCK_RAW, IPPROTO_ICMP, NULL, 0, WSA_FLAG_OVERLAPPED);
If (m_sockRaw==INVALID_SOCKET)
{
Cerr & lt; <"WSASocket () failed" & lt; ExitProcess (1);
}

//bind
SOCKADDR_IN localaddr;
ZeroMemory (& amp; Localaddr, sizeof (SOCKADDR_IN));
Localaddr. Sin_family=AF_INET;
Localaddr. Sin_addr. S_un. S_addr=htonl (INADDR_ANY);
Bind (m_sockRaw, (sockaddr *) & amp; Localaddr, sizeof (SOCKADDR_IN));

//set the socket to receive timeout option
Int ret=setsockopt (m_sockRaw, SOL_SOCKET, SO_RCVTIMEO, (char *) & amp; M_nTimeout, sizeof (m_nTimeout));
If (ret==SOCKET_ERROR)
{
Cerr & lt; <"The setsockopt (SO_RCVTIMEO) failed" & lt; return;
}

//set the socket send timeout option
M_nTimeout=10000;
Ret=setsockopt (m_sockRaw, SOL_SOCKET, SO_SNDTIMEO, (char *) & amp; M_nTimeout, sizeof (m_nTimeout));
If (ret==SOCKET_ERROR)
{
Cerr & lt; <"The setsockopt (SO_SNDTIMEO) failed" & lt; return;
}

//fill the destination address
ZeroMemory (& amp; M_addrDest, sizeof (m_addrDest));
M_addrDest. Sin_family=AF_INET;
If ((m_addrDest sin_addr. S_un. S_addr=inet_addr (strHost))==INADDR_NONE)
{
Host hostent *=NULL;
The host=gethostbyname (strHost);
If (the host!=NULL)
{
CopyMemory (& amp; (m_addrDest sin_addr), the host - & gt; H_addr_list [0], the host - & gt; H_length);
}
The else
{
Cerr & lt; <"Gethostbyname () failed" & lt; return;
}
}

//set the routing option
Bool bopt=true;
If (setsockopt (m_sockRaw, SOL_SOCKET, SO_DONTROUTE, (char *) & amp; Bopt, sizeof (bool))==SOCKET_ERROR)
{
Cerr & lt; <"The setsockopt (SO_DONTROUTE) failed" & lt; return;
}

//to send buffer and the receive buffer memory application, ensure that here is enough,
M_nDatasize=DEF_PACKET_SIZE;
M_nDatasize +=sizeof (IcmpHeader);
M_ICMPData=https://bbs.csdn.net/topics/(char *) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, MAX_PACKET);
M_RecvBuf=(char *) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, MAX_PACKET);
if (! M_ICMPData | |! M_RecvBuf)
{
Cerr & lt; <"HeapAlloc () failed" & lt; return;
}

//to send buffer fills the ICMP data
ZeroMemory (m_ICMPData MAX_PACKET);
FillICMPData (m_ICMPData m_nDatasize);


String STR.
If (inet_addr (strHost)==INADDR_NONE)
{
STR=strHost;
STR +="["
STR +=inet_ntoa (m_addrDest sin_addr);
STR +="] ";
}
The else
STR=strHost;
Cout & lt; <"By most" & lt;
Increasing//TTL sends the ICMP echo request
For (m_nTTL=1; M_nTTL & lt;=m_nMaxhops & amp; & ! M_bDone; + + m_nTTL)
{
Int ret.
SetTTL (m_sockRaw m_nTTL);

((IcmpHeader *) m_ICMPData) - & gt; Checksum=0;
((IcmpHeader *) m_ICMPData) - & gt; Seq=m_Seqno + +;
M_dwCurrentTick=GetTickCount ();
((IcmpHeader *) m_ICMPData) - & gt; Timestamp=m_dwCurrentTick;
((IcmpHeader *) m_ICMPData) - & gt; Checksum=checksum ((unsigned short *) m_ICMPData, m_nDatasize);

Ret=sendto (m_sockRaw, m_ICMPData m_nDatasize, 0, (SOCKADDR *) & amp; M_addrDest, sizeof (SOCKADDR_IN));
If (ret==SOCKET_ERROR)
{
If (WSAGetLastError ()==WSAETIMEDOUT)
{
Cout & lt; <"Send timeout" & lt; continue;
}
The else
{
Cerr & lt; <"Sendto () failed" & lt; return;
}
}
Int fromlen=sizeof (SOCKADDR_IN);
Ret=recvfrom (m_sockRaw, m_RecvBuf MAX_PACKET, 0, (SOCKADDR *) & amp; M_addrFrom, & amp; Fromlen);
If (ret==SOCKET_ERROR)
{
If (WSAGetLastError ()==WSAETIMEDOUT)
{
Cout & lt; <"Receive timeout" & lt; continue;
}
The else
{
Cerr & lt; <"Recvfrom () failed" & lt; return;
}
}
//parse the received ICMP error message or echo reply message
M_bDone=DecodeRecv (m_RecvBuf, ret, & amp; M_addrFrom m_nTTL);
Sleep (1000);
}
}


CodePudding user response:

Is that you receive or resolve problems

CodePudding user response:

reference 1st floor shiyanzi response:
that's you receive or resolve problems

Is receiving problem,
Behind me tried to use WSASocket (AF_INET SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED) to create a raw socket,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related