how to make a workable TCP SYN packet with raw socket



I have a simple TCP server program, just create two processes, one process is listening and the other process is for connecting. If I run a simple client program on one machine and this simple server on another machine, it works fine.

Now I want to write a simpe tcp client program using raw socket, I filled the IP header and TCP header in the SYN packet with SYN=1, the TCP hearder is 20 bytes without options. then I ran the raw socket-based tcp client and server on two different local machines. I used tcpdump to capture packets, and I see that the server machine received the SYN packet. But the server doesn't reply with a SYN/ACK, what are potential problems? thanks!

the main function of the client program is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void *run(void *arg)
{
        struct ip ip;
        struct tcphdr tcp;
        int sd;
        const int on = 1;
        struct sockaddr_in sin;


        u_char *packet;
        packet = (u_char *)malloc(60);

        ip.ip_hl = 0x5;
        ip.ip_v = 0x4;
        ip.ip_tos = 0x0;
        ip.ip_len = sizeof(struct ip) + sizeof(struct tcphdr);
        ip.ip_id = htons(12830);
        ip.ip_off = 0x0;
        ip.ip_ttl = 64;
        ip.ip_p = IPPROTO_TCP;
        ip.ip_sum = 0x0;
        ip.ip_src.s_addr = inet_addr("172.17.14.169");
        ip.ip_dst.s_addr = inet_addr("137.96.201.72");
        ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip));
        memcpy(packet, &ip, sizeof(ip));

        tcp.source = htons(3333);
        tcp.dest = htons(32000);
        tcp.seq = htonl(0x131123);
        tcp.doff = sizeof(struct tcphdr) / 4;
        tcp.syn = 1;
        tcp.window = htons(2048);
        tcp.check = 0;
        tcp.check = in_cksum_tcp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&tcp, sizeof(tcp));
        memcpy((packet + sizeof(ip)), &tcp, sizeof(tcp));

        if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
                perror("raw socket");
                exit(1);
        }

        if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
                perror("setsockopt");
                exit(1);
        }
    }

}
Last edited on
Topic archived. No new replies allowed.