Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing traceroute using icmp in C
    primarykey
    data
    text
    <p>I am trying to implement traceroute using icmp raw sockets by constructing the appropriate ip header and icmp header.The port number i am using is 7 and i have calculated the checksums.The hop limit is incremented each time and a packet is sent till the reply message contains the echo reply of type 0.</p> <pre><code>#include "libsock" #include&lt;netinet/ip.h&gt; #include&lt;netinet/ip_icmp.h&gt; unsigned short csum (unsigned short *buf, int nwords) { unsigned long sum; for (sum = 0; nwords &gt; 0; nwords--) sum += *buf++; sum = (sum &gt;&gt; 16) + (sum &amp; 0xffff); sum += (sum &gt;&gt; 16); return ~sum; } int main (int argc, char *argv[]) { if (argc != 2) { printf ("need destination for tracert\n"); exit (0); } int sfd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); char buf[4096] = { 0 }; struct ip *ip_hdr = (struct ip *) buf; int hop = 0; int one = 1; const int *val = &amp;one; if (setsockopt (sfd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) &lt; 0) printf ("Cannot set HDRINCL!\n"); struct sockaddr_in addr; addr.sin_port = htons (7); addr.sin_family = AF_INET; inet_pton (AF_INET, argv[1], &amp;(addr.sin_addr)); while (1) { ip_hdr-&gt;ip_hl = 5; ip_hdr-&gt;ip_v = 4; ip_hdr-&gt;ip_tos = 0; ip_hdr-&gt;ip_len = 20 + 8; ip_hdr-&gt;ip_id = 10000; ip_hdr-&gt;ip_off = 0; ip_hdr-&gt;ip_ttl = hop; ip_hdr-&gt;ip_p = IPPROTO_ICMP; inet_pton (AF_INET, "172.30.104.59", &amp;(ip_hdr-&gt;ip_src)); inet_pton (AF_INET, argv[1], &amp;(ip_hdr-&gt;ip_dst)); ip_hdr-&gt;ip_sum = csum ((unsigned short *) buf, 9); struct icmphdr *icmphd = (struct icmphdr *) (buf + 20); icmphd-&gt;type = ICMP_ECHO; icmphd-&gt;code = 0; icmphd-&gt;checksum = 0; icmphd-&gt;un.echo.id = 0; icmphd-&gt;un.echo.sequence = hop + 1; icmphd-&gt;checksum = csum ((unsigned short *) (buf + 20), 4); sendto (sfd, buf, 28, 0, SA &amp; addr, sizeof addr); char buff[4096] = { 0 }; struct sockaddr_in addr2; socklen_t len = sizeof (struct sockaddr_in); recvfrom (sfd, buff, 28, 0, SA &amp; addr2, &amp;len); struct icmphdr *icmphd2 = (struct icmphdr *) (buff + 20); if (icmphd2-&gt;type != 0) printf ("hop limit:%d Address:%s\n", hop, inet_ntoa (addr2.sin_addr)); else { printf ("Reached destination:%s with hop limit:%d\n", inet_ntoa (addr2.sin_addr), hop); exit (0); } hop++; } return 0; } </code></pre> <p>When the input ie <code>argv[1]</code> is <code>"127.0.0.1"</code> the o/p is</p> <pre><code>hop limit:0 Address:127.0.0.1 Reached destination:127.0.0.1 with hop limit:1 </code></pre> <p>but for other addresses present in my lan for whom tracepath works my program blocks at <code>recvfrom</code>.</p> <p>Can you please point out the reasons?</p> <p>Thank you.</p> <p>Here's libsock:-</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #include&lt;arpa/inet.h&gt; #include&lt;sys/types.h&gt; #include&lt;netinet/in.h&gt; #include&lt;sys/socket.h&gt; #include&lt;unistd.h&gt; #include&lt;pthread.h&gt; #include&lt;poll.h&gt; #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;unistd.h&gt; #include&lt;sys/types.h&gt; #include&lt;sys/ipc.h&gt; #include&lt;fcntl.h&gt; #include&lt;sys/stat.h&gt; #include&lt;signal.h&gt; #include&lt;sys/sem.h&gt; #include&lt;poll.h&gt; #include&lt;pthread.h&gt; #include&lt;sys/select.h&gt; #include&lt;sys/un.h&gt; #define SA (struct sockaddr*) </code></pre>
    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.
    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