簡體   English   中英

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

[英]Raw Socket send TCP SYN-FIN-.. in c++

我的老師希望我們在Windows上的C ++中的原始套接字上進行練習(用於學習tcp通信)。

我有一個問題。 我看到了很多文檔,但我不知道如何解決。

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);
    }
}

我的錯誤發生在sendto()處。 它返回10022錯誤= WSAEINVAL

我看到這可以成為新的Windows保護嗎?

您是否有解決我的問題或繞過保護措施的想法(深入了解驅動程序等)

您無需在代碼中設置iph-> tot_len。

我對使用c ++的網絡代碼的建議是使用std :: string或std :: vector:

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

然后使用地址(&packet [0])進行指針操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM