0

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)

3
  • Is your app running with admin privileges? RAW sockets are restricted to administrators. Commented Nov 19, 2013 at 6:35
  • I try by right clicking and "run as Admin" same effect. Commented Nov 19, 2013 at 7:07
  • Are you fully populating tcph? There are some funky duplicate iph fields being set. Commented Nov 19, 2013 at 7:55

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.