Note that there are some explanatory texts on larger screens.

plurals
  1. POraw sockets in c - bogus header length
    text
    copied!<pre><code>#include&lt;stdlib.h&gt; #include&lt;netinet/ip.h&gt; #include&lt;netinet/tcp.h&gt; #include&lt;sys/socket.h&gt; #include&lt;arpa/inet.h&gt; #include&lt;netinet/in.h&gt; #include&lt;stdio.h&gt; #include&lt;unistd.h&gt; #include&lt;string.h&gt; #include&lt;sys/types.h&gt; #define P 5000 /* lets flood the port */ unsigned short /* this function generates header checksums */ 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 (void) { char ipp[10] = "1.2.3.4"; int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP); /* open raw socket */ char datagram[4096]; /* this buffer will contain ip header, tcp header, and payload. we'll point an ip header structure at its beginning, and a tcp header structure after that to write the header values into it */ struct ip *iph = (struct ip *) datagram; struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof (struct ip); struct sockaddr_in sin; /* the sockaddr_in containing the dest. address is used in sendto() to determine the datagrams path */ sin.sin_family = AF_INET; sin.sin_port = htons (P);/* you byte-order &gt;1byte header values to network byte order (not needed on big endian machines) */ sin.sin_addr.s_addr = inet_addr ("192.168.1.12"); memset (datagram, 0, 4096); /* zero out the buffer */ /* we'll now fill in the ip/tcp header values, see above for explanations */ iph-&gt;ip_hl = 5; iph-&gt;ip_v = 4; iph-&gt;ip_tos = 0; iph-&gt;ip_len = sizeof (struct ip) + sizeof (struct tcphdr); /* no payload */ iph-&gt;ip_id = htonl (54321); /* the value doesn't matter here */ iph-&gt;ip_off = 0; iph-&gt;ip_ttl = 255; iph-&gt;ip_p = 6; iph-&gt;ip_sum = 0; /* set it to 0 before computing the actual checksum later */ iph-&gt;ip_src.s_addr = inet_addr (ipp);/* SYN's can be blindly spoofed */ iph-&gt;ip_dst.s_addr = sin.sin_addr.s_addr; tcph-&gt;source = htons (1234); /* arbitrary port */ tcph-&gt;dest = htons (P); tcph-&gt;seq = random ();/* in a SYN packet, the sequence is a random */ tcph-&gt;ack_seq = 0;/* number, and the ack sequence is 0 in the 1st packet */ tcph-&gt;doff = 0; /* first and only tcp segment */ tcph-&gt;syn = 1; tcph-&gt;window = htonl (65535); /* maximum allowed window size */ tcph-&gt;check = 0;/* if you set a checksum to zero, your kernel's IP stack should fill in the correct checksum during transmission */ tcph-&gt;urg_ptr = 0; iph-&gt;ip_sum = csum ((unsigned short *) datagram, iph-&gt;ip_len &gt;&gt; 1); /* finally, it is very advisable to do a IP_HDRINCL call, to make sure that the kernel knows the header is included in the data, and doesn't insert its own header into the packet before our data */ { /* lets do it the ugly way.. */ int one = 1; const int *val = &amp;one; if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) &lt; 0) printf ("Warning: Cannot set HDRINCL!\n"); } int i1=49; char i2 = 49; int cc = 0; int r1; while (cc&lt;99) { ipp[6] = i1; ipp[7] = i2; if(i1 == 57) { break; } if(i2 == 57 ) { i2 = 48; i1++; } i2++; cc++; iph-&gt;ip_src.s_addr = inet_addr (ipp); if (sendto (s, /* our socket */ datagram, /* the buffer containing headers and data */ iph-&gt;ip_len, /* total length of our datagram */ 0, /* routing flags, normally always 0 */ (struct sockaddr *) &amp;sin, /* socket addr, just like in */ sizeof (sin)) &lt; 0) /* a normal send() */ printf ("error\n"); else printf ("sent \n"); } return 0; } </code></pre> <p>the code creates a raw socket with proper ip address and the source addrewss is 192.168.1.12...port 5000 ... the problem is that when i try to capture the packets sent using wireshark, it says something like bogus tcp headee length...should be atleast 20 ... how can i correct this error ... P.S. you can refer <a href="http://mixter.void.ru/rawip.html" rel="nofollow">link</a> for the code explanation if you find the code lengthy ... </p>
 

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