Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Local IP header structure is wrong... my suggestion is to include the IP header provided with your distro (are you using linux? don't you?).</p> <p>What i did is just include linux/ip.h, rename ipheader structure reference to iphdr, and rename the ip header fields according to the structure described in the latter file.</p> <p>I tried to sniff packets with tcpdump and it works now (i didn't try with wireshark but it must work too)</p> <p>Try this fixed code:</p> <pre><code>// Must be run by root lol! Just datagram, no payload/data #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/udp.h&gt; #include &lt;linux/ip.h&gt; // The packet length #define PCKT_LEN 35 // UDP header's structure struct udpheader { unsigned short int udph_srcport; unsigned short int udph_destport; unsigned short int udph_len; unsigned short int udph_chksum; }; // total udp header length: 8 bytes (=64 bits) // Function for checksum calculation. From the RFC, // the checksum algorithm is: // "The checksum field is the 16 bit one's complement of the one's // complement sum of all 16 bit words in the header. For purposes of // computing the checksum, the value of the checksum field is zero." 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 (unsigned short)(~sum); } // Source IP, source port, target IP, target port from the command line arguments int main(int argc, char *argv[]) { int sd; // No data/payload just datagram char buffer[PCKT_LEN]; // Our own headers' structures struct iphdr *ip = (struct iphdr *) buffer; struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct iphdr)); // Source and destination addresses: IP and port struct sockaddr_in sin, din; int one = 1; const int *val = &amp;one; memset(buffer, 0, PCKT_LEN); if(argc != 5) { printf("- Invalid parameters!!!\n"); printf("- Usage %s &lt;source hostname/IP&gt; &lt;source port&gt; &lt;target hostname/IP&gt; &lt;target port&gt;\n", argv[0]); exit(-1); } // Create a raw socket with UDP protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if(sd &lt; 0) { perror("socket() error"); // If something wrong just exit exit(-1); } else printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n"); // The source is redundant, may be used later if needed // The address family sin.sin_family = AF_INET; din.sin_family = AF_INET; // Port numbers sin.sin_port = htons(atoi(argv[2])); din.sin_port = htons(atoi(argv[4])); // IP addresses sin.sin_addr.s_addr = inet_addr(argv[1]); din.sin_addr.s_addr = inet_addr(argv[3]); // Fabricate the IP header or we can use the // standard header structures but assign our own values. ip-&gt;ihl = 5; ip-&gt;version = 4; ip-&gt;tos = 16; // Low delay ip-&gt;tot_len = sizeof(struct iphdr) + sizeof(struct udpheader); ip-&gt;id = htons(54321); ip-&gt;ttl = 64; // hops ip-&gt;protocol = 17; // UDP // Source IP address, can use spoofed address here!!! ip-&gt;saddr = inet_addr(argv[1]); // The destination IP address ip-&gt;daddr = inet_addr(argv[3]); // Fabricate the UDP header. Source port number, redundant udp-&gt;udph_srcport = htons(atoi(argv[2])); // Destination port number udp-&gt;udph_destport = htons(atoi(argv[4])); udp-&gt;udph_len = htons(sizeof(struct udpheader)); // Calculate the checksum for integrity ip-&gt;check = csum((unsigned short *)buffer, sizeof(struct iphdr) + sizeof(struct udpheader)); // Inform the kernel do not fill up the packet structure. we will build our own... if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) &lt; 0) { perror("setsockopt() error"); exit(-1); } else printf("setsockopt() is OK.\n"); // Send loop, send for every 2 second for 100 count printf("Trying...\n"); printf("Using raw socket and UDP protocol\n"); printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4])); int count; int i; for(count = 1; count &lt;=20; count++) { if((i = sendto(sd, buffer, PCKT_LEN, 0, (struct sockaddr *)&amp;sin, sizeof(sin))) &lt; 0) // Verify { perror("sendto() error"); exit(-1); } else { printf("Count #%u - sendto() is OK. Data Length# %d\n", count,i); sleep(2); } } close(sd); return 0; } </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.
    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