Note that there are some explanatory texts on larger screens.

plurals
  1. POReading ICMP reply with select in Linux using C++
    primarykey
    data
    text
    <p>I am sending an ICMP request using C++ and raw sockets to a router and after that I want to read the ICMP reply. My problem is, select() is not receiving the replay and times-out all the time. I am not getting any error (errno is returning success). The router is sending the ICMP reply, because i can see the response using Wireshark. </p> <p><a href="http://i.imgur.com/0Wra1.png" rel="nofollow">http://i.imgur.com/0Wra1.png</a> <em>wireshark screenshot</em></p> <p>For testing my program I am using Ubuntu 12.10 running on VirtualBox 4.2.6 and GN3 for a virtual network.</p> <p>My source code:</p> <pre><code>char buffer[IP_MAXPACKET]; // for the received ICMP reply struct iphdr *ipRec; // ICMP header timeval tv; // timeout fd_set mySet; // descriptor set ... tv.tv_sec = 3; // default time-out 3s tv.tv_usec = 0; int retval; // select ... do { FD_ZERO(&amp;mySet); FD_SET(mysocket, &amp;mySet); retval = select(mysocket+1, &amp;mySet, NULL, NULL, &amp;tv); cout &lt;&lt; "Errno after select:" &lt;&lt; strerror(errno) &lt;&lt; endl; if(retval == -1) { cerr &lt;&lt; "select error" &lt;&lt; endl; break; } else if (retval) { if((length = recvfrom(mysocket, buffer, MAX, 0, result-&gt;ai_addr, &amp;(result-&gt;ai_addrlen))) == -1) { cerr &lt;&lt; "Error: while receiving data." &lt;&lt; endl; } else { cout &lt;&lt; "good" &lt;&lt; endl; ipRec = (struct iphdr*) buffer; icmpRec = (struct icmphdr*) (buffer + ipRec-&gt;ihl * 4); cout &lt;&lt; "the packet." &lt;&lt; " PID: " &lt;&lt; ntohs(icmpRec-&gt;un.echo.id) &lt;&lt; " Seq: " &lt;&lt; ntohs(icmpRec-&gt;un.echo.sequence) &lt;&lt; endl; if ((icmpRec-&gt;type == ICMP_ECHOREPLY) &amp;&amp; (ntohs(icmpRec-&gt;un.echo.id) == pid) &amp;&amp; (ntohs(icmpRec-&gt;un.echo.sequence) == (seq - 1))) { minBuff = lengthBuff; } } } else { // getting here all the time = select times-out and reads no data cout &lt;&lt; "mysocket:" &lt;&lt; mysocket &lt;&lt; endl; cout &lt;&lt; "retval:" &lt;&lt; retval &lt;&lt; endl; maxBuff = lengthBuff; break; } } while (!((icmpRec-&gt;type == ICMP_ECHOREPLY) &amp;&amp; (ntohs(icmpRec-&gt;un.echo.id) == pid) &amp;&amp; (ntohs(icmpRec-&gt;un.echo.sequence) == (seq - 1)))); if (packet) delete(packet); ... </code></pre> <p>Thank you for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload