Long story short, writing a multithreaded honey pot, and am having issues starting the honey pot servers. I'll get straight to the code, then explain the issue:

Structure defs:

typedef struct __GENERIC{
    int sock;
    char buff[MAXBUFF];
    char ip[17];
    int status;
    int port;
    int max_conns; // dMaxClients (Master = max # of HPs, HP = max # of concurrent conns)
    int curr_conns;
    int ptype;
    PService opts;
} Generic, *PGeneric;

typedef struct __MAINSERVER{
    PGeneric settings;
} Master, *PMaster;

typedef struct __HONEYPOT{
    PGeneric settings;
    HANDLE thread; // Thread handle
    PMaster ms;
} Server, *PServer;

The code used to create the honey pot thread:

static PMaster master = (PMaster)malloc(sizeof(Master));
static PService vice = (PService)malloc(sizeof(Service));
static PServer server = (PServer)malloc(sizeof(Server));
static PGeneric pgen = (PGeneric)malloc(sizeof(Generic));

int HPHandleSig(PGeneric gen, char *cmd){
    char *c = HPSigCmd(cmd);

    if(HPCmdCheck(c)){
        char *p = HPSigParam(c, cmd);
        int port = (p != NULL) ? atoi(p) : -1;

        MessageTS("HPHandleSig", "Parsed command.\ncmd = %s\nparam = %s", c, p);

        if(!strcmp(c, "run")){
            MessageTS("HPHandleSig", "Attempting to create honey pot thread");

            vice = ParseFile(p);

            MessageTS("HPHandleSig", "Parsed %s", p);

            master->settings = gen;

            server->ms = master;

            free(master);

            pgen->opts = vice;
            server->settings = pgen;

            free(vice);

            MessageTS("HHS", "dMaxClients = %d", *(int*)GetOption(server->settings->opts, "dMaxClients"));

            MessageTS("HPHandleSig", "Creating thread for honey pot");

            CreateThread(NULL, 0, HoneyPotThread, server, 0, NULL);

            free(server);
        }
    }
}

And the honey pot thread:

DWORD WINAPI HoneyPotThread(void *serv_struct){
    MessageTS("HoneyPotThread", "Thread created for honey pot");

    PServer server = (PServer)malloc(sizeof(Server));
    server = reinterpret_cast<PServer>(&serv_struct);
    PGeneric gen = (PGeneric)malloc(sizeof(Generic));
    gen = reinterpret_cast<PGeneric>(&server->settings);
    PService vice = (PService)malloc(sizeof(Service));
    vice = reinterpret_cast<PService>(&server->settings->opts);

    printf("-- passed typecasting --\n");
    PrintService(vice);
    //server->settings = HPNetInit(server->settings->opts, NULL); // server->ms->settings);
    //printf("-- passed HPNetInit() --\n");
    //MessageTS("HoneyPotThread", "Created socket %d", server->settings->sock);
    //MessageTS("HoneyPotThread", "Main socket = %d", server->ms->settings->sock);

    MessageTS("HoneyPotThread", "HPT is terminating...");

    return 0;
}

In HoneyPotThread, I've done checking to see if the variables are empty (which, they are not), I know there's probably no reason for the P* var = (P*)malloc(sizeof(*)) stuff...but, here's what happens.

For whatever reason, when HPNetInit() is called (which takes a Service pointer to structure argument), it crashes the very moment a call is made to fetch data from PService. I've also checked in HPNetInit to see if the PService var is empty/NULL, and it's not.

PService struct isn't shown as I don't see how it holds any merit to the issue, as it works just fine creating the control server (which spawns/kills/pauses the honey pots).

I've tried doing quite a few things...such as, but not limited to, the following:

1) In the CreateThread() func, I've tried doing reinterpret_cast<LPVOID>(&service) (as well as the same w/o the amprehand sign).
2) In HoneyPotThread(), automatically assigning vars their specific data, ie:
PServer server = <reinterpret_cast PServer>(&serv_struct); (also done w/o the amprehand).

Also, as you can see in the HPHandleSig() func, I try to get a variable I know exists from the PService struct...and it works just fine...so, there's some problem between then and the start of the thread where it fails.

Is it impossible to pass a structure that contains another structure to it, or am I just screwing something up?

That's a nightmare... Is it a program in C? No, of course (see reinterpret_cast). But if it's a program in C++, where is operators new instead absolutely inadequate mallocs?!

I can't understand why you allocate memory then overwrite the pointer obtained from malloc in the next statement (and you? )...
It seems the best advice: think again and redesign this code from the scratch (don't forget to select the implementation language)...

commented: whew. i'm glad someone else thought so, too :) +8
commented: touche +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.