Have some problems in receiving packets. I can receive and read incoming packets, but I think i do not get a handshake with any host. I only want to send a packet to a remot
ip = (struct iphdr*) buffer;
tcp = (struct tcphdr*) (buffer + sizeof(struct tcphdr)); //This is wrong
Here to get array index of tcp header in buffer
, you need to add sizeof(struct iphdr)
to buffer
like mentioned below.
ip = (struct iphdr*) buffer;
tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr)); //This is correct
buffer
, but you're printing data from ip
and tcp
without parsing that buffer. You should parse the packet from buffer
after receiving it, and before printing.IPPROTO_TCP
is misleading when creating a RAW socket. Stick to IPPROTO_IP
, and add the necessary conditions to your code for each protocol you care about (TCP, UDP, etc). This happens to be working because the Linux Kernel validates the protocol number, and fallbacks to IPPROTO_IP
. However, this might not work in other systems.tcp->seq
might have an invalid value, because TCP only accepts values up to 65535, while random()
returns values from 0 to RAND_MAX
(0x7fffffff). Try tcp->seq = htonl(random() % 65535);
sizeof(struct iphdr)
rather than sizeof(struct tcphdr)
.