Hello everybody!

I'm very interested in networking. I'm learning socket programming on Linux. I know that exists RAW sockets. I have searched for tutorials, but didn't find anything very useful. Then I searched in google for sock_raw filetype:c and found so much useful information. I'm new to C, so I didn't understand much things, but I have basic understand of socket programming. I hope you guys will help me a little.

I'm trying to make my code to send the first packet of the three-way hand shake. Here is the code that I got so far, unfortunetly it send's a socket, but it dosn't work!

#include <stdio.h>
#include <linux/ip.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(){

int sock, optval;
char *packet;

struct sockaddr_in server;
struct iphdr *ip;
struct tcphdr *tcp;

server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(80);

ip = (struct iphdr *) malloc(sizeof(struct iphdr));
tcp = (struct tcphdr *) malloc(sizeof(struct tcphdr));
packet = (char *) malloc(sizeof(struct iphdr) + sizeof(struct tcphdr));

ip = (struct iphdr *) packet;
tcp = (struct tcphdr *) (packet + sizeof(struct iphdr));

ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
ip->id = htons(getuid());
ip->ttl = 255;
ip->protocol = IPPROTO_TCP;
ip->saddr = inet_addr("127.0.0.1");
ip->daddr = inet_addr("127.0.0.1");
ip->check = 0x87e1;

tcp->source=htons(80);
tcp->dest=htons(80);
tcp->seq=htonl(rand()%time(NULL));
tcp->ack_seq=htonl(0);
tcp->doff=5;
tcp->fin=0;
tcp->syn =1;
tcp->rst=0;
tcp->psh=0;
tcp->ack=0;
tcp->urg=0;
tcp->window=htons(4000);
tcp->urg_ptr=htons(0);
tcp->check=0x4fe2;

sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
setsockopt(sock,IPPROTO_IP,IP_HDRINCL,&optval,sizeof(int));
sendto(sock,packet,ip->tot_len,0,(struct sockaddr *)&server,sizeof(struct sockaddr));
close(sock);
return 0;
}
First of all these lines allocate memory, can these lines be writted more simply? I don't realy understand them! Why can't I just create pointer to structure?
ip = (struct iphdr *) malloc(sizeof(struct iphdr));
tcp = (struct tcphdr *) malloc(sizeof(struct tcphdr));
packet = (char *) malloc(sizeof(struct iphdr) + sizeof(struct tcphdr));
And here are the next two lines:

ip = (struct iphdr *) packet;
tcp = (struct tcphdr *) (packet + sizeof(struct iphdr));
I think it means that ip will be pointer to struct iphdr, but what does packet mean?

ip-&gt;check = 0x87e1;
tcp-&gt;check=0x4fe2;
I know that I need to write some cind of function to calcolate the checksum, but I realy have no idea how to do it! Does anyone know how any tutorial how to do this? I know that I can just use other people functions, but I want to create my own! Allso I use ethereal to monitor my packet, but the strangest thing is that even if I write a checksum in IP header, ethereal shows me different checksum for IP header, but it shows that this checksum is correct!

I started Apache, and send my packet to Apache, but it didn't reply! I think its because the TCP wrong checksum!

I know I'm asking much, but I really want to understand how it works, so I want to get this code as simple as posible! Thank you!