简体   繁体   English

原始套接字在C ++中发送TCP SYN-FIN- ..

[英]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). 我的老师希望我们在Windows上的C ++中的原始套接字上进行练习(用于学习tcp通信)。

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(). 我的错误发生在sendto()处。 it return 10022 error = WSAEINVAL 它返回10022错误= WSAEINVAL

I saw that can be a new windows protection? 我看到这可以成为新的Windows保护吗?

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. 您无需在代码中设置iph-> tot_len。

My recommendation for networking code using c++ would be to use std::string or std::vector: 我对使用c ++的网络代码的建议是使用std :: string或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. 然后使用地址(&packet [0])进行指针操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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