简体   繁体   中英

Raw Socket send TCP SYN-FIN-.. in c++

My teacher want us to do an exercise on raw socket in c ++ on Windows (for learning tcp communication).

I have got a problem with it. I saw a lot of documentation but I don't know how to solve it.

int raw()
{
    WSADATA WSAData;
    SOCKET sock;
    SOCKADDR_IN sin,din;
    WSAStartup(MAKEWORD(2, 2), &WSAData);

    char datagram[MAX_PACKET_SIZE];
    struct iphdr *iph = (struct iphdr *)datagram;
    struct tcphdr *tcph = (struct tcphdr *)((UCHAR *)iph +  sizeof(tcphdr));
    char new_ip[sizeof "255.255.255.255"];

    sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
        cout << "failled init socket" << endl ;
    else{
        memset(datagram, 0, MAX_PACKET_SIZE); // Clear the data
        setup_ip_header(iph);
        setup_tcp_header(tcph);

        sin.sin_family = AF_INET;
        sin.sin_port = htons(8888);
        sin.sin_addr.s_addr = inet_addr("192.168.1.10"); //source ip

        din.sin_family = AF_INET;
        din.sin_port = htons(DEST_PORT);
        din.sin_addr.s_addr = inet_addr(TARGET_SERV_IP); //ip serv to connect

        tcph->port_dest = htons(DEST_PORT);
        iph->ip_dest = din.sin_addr.s_addr;
        iph->ip_source = sin.sin_addr.s_addr;
        iph->ip_dest = inet_addr(TARGET_SERV_IP); //ip serv to connect
        iph->ip_source = inet_addr("192.168.1.10"); //source ip

        //iph->checksum = csum((unsigned short *)datagram, iph->tot_len >> 1);
        iph->checksum = csum((unsigned short *)datagram, sizeof(struct iphdr));

        int one = 1;
        const int *val = &one;

        if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)val, sizeof(one)) < 0)
            printf("failled set socket option IP_HDRINCL");
        else{
                if (sendto(sock,      /* our socket */
                    datagram,         /* the buffer containing headers and data */
                    ntohs( iph->tot_len),     /* total length of our datagram */
                    0,        /* routing flags, normally always 0 */
                    (struct sockaddr *) &sin,   /* socket addr, just like in */
                    sizeof(sin)) < 0)     /* a normal send() */
                        cout << stderr << "sendto() error!!!.\n " << WSAGetLastError() << endl;
                else
                    cout << "packet send\n"  << endl;
        }
    closesocket(sock);
    }
}

My error occurs at the sendto(). it return 10022 error = WSAEINVAL

I saw that can be a new windows protection?

Have you any idea to fix my problem or bypass the protection (go deeper, driver, etc)

You don't set iph->tot_len in your code.

My recommendation for networking code using c++ would be to use std::string or std::vector:

std::vector<uint8_t> packet(MAX_PACKET_SIZE, 0);
...
packet.resize(real_size);

then use the address (&packet[0]) for your pointer manipulations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM