Sniphfer on s++ and Winsock2



  • I'm trying to create my own book, that's what I've done. The only thing I can't do to win, can tell you what's wrong. WSAGetLastError = 10038

    #include <conio.h>
    #include <stdio.h>
    #include <winsock2.h>
    void main()
    {
    
    struct sockaddr saddr;
    int sa_size,data_size;
    char Buffer[1024];
    WSADATA wsaData;
    struct sockaddr_in sa;
    
    int Result = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
    if (Result != 0){
        cout &lt;&lt; "WSA strtup failed " &lt;&lt; Result &lt;&lt; endl;
    }
    
    char hostname[100];
    int in = 0;
    WSADATA wsa;
    if (gethostname(hostname, sizeof(hostname)) == SOCKET_ERROR){
        cout &lt;&lt; "Unable to get hostname" &lt;&lt; endl;
        exit(1);
    }
    struct hostent *local = gethostbyname(hostname);
    if (local == NULL){
        cout &lt;&lt; "Got an error " &lt;&lt; endl;
        exit(1);
    }
    SOCKET raw_sock;
    cout &lt;&lt; "Host name: " &lt;&lt; hostname &lt;&lt; endl;
    raw_sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
    if (raw_sock&lt;0){
        cout &lt;&lt; "Unable to create raw socket: " &lt;&lt; endl;
        exit(1);
    }
    in_addr addr;
    cout &lt;&lt; "RAW socket has been created" &lt;&lt; endl;
    cout &lt;&lt; "Available Network Interfaces" &lt;&lt; endl;
    for (int i = 0; local-&gt;h_addr_list[i] != 0; ++i){
        memcpy(&amp;addr, local-&gt;h_addr_list[i], sizeof(struct in_addr));
        cout &lt;&lt; "Interface [ " &lt;&lt; i &lt;&lt; " ]: Adress " &lt;&lt; inet_ntoa(addr) &lt;&lt; endl;
    }
    
    memset(&amp;sa, 0, sizeof(sa));
    memcpy(&amp;sa.sin_addr.s_addr, local-&gt;h_addr_list[in], sizeof(sa.sin_addr.s_addr));
    sa.sin_family = AF_INET;
    sa.sin_port = 0;
    if (bind(raw_sock, (struct sockaddr*)&amp;sa, sizeof(sa)) == SOCKET_ERROR){
        cout &lt;&lt; "Error binding" &lt;&lt; WSAGetLastError() &lt;&lt; endl;
        closesocket(raw_sock);
        exit(1);
    }
    cout &lt;&lt; "Bind completed" &lt;&lt; endl;
    

    }



  • Let's start by checking the correctness of the sket is being done wrong. It's like this:

    if (raw_sock == INVALID_SOCKET) {
       // Обработка ошибки.
    }
    

    In order to create a RAW-Socket, the program needs to start with the rights of the administrator. Then there's a mistake. WSAEINVAL (10022, Invalid argument). It's connected to the wrong parameters in the creation of a sket. Correct:

    raw_sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
    

    Now. bind It's going well.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2