C++ Structure declaration and usage to hold IP + Connections
08:39 04 Sep 2015

I am having a massive confusion declaring and using my structure to hold [IP] - [Connections] record. I am trying to insert the IP address that is connecting into the structure, and its connection number #, for example if IP 123.123.12 connects 2 (two) times, then update the [Connections] number in the struct for that IP (123.123.12).

I have the following code, which should work:

// the struct
typedef struct {
    int id; // is this useful anyway?
    char *ip;
    int connNumbers;
}test_sock;

// init struct
test_sock holder[5000];
int len = 0;

// the function
void AddtoStruct(char *ip)
{

    if (len == 0) //if empty, insert.
    {
        len++;
        holder->id = len;
        holder->ip = ip;
        holder->connNumbers = 1; //1 conexiune
    
        return;
    }

    for (int i = 0; iid != 0) //check if its the same id !?
        {
                //Exista deja in structura , doar increase connNumbers;
                if (strcmp(ip, holder->ip) == 0)
                {
                    holder++;
                    holder->connNumbers++;
                    holder->id = antiddos_len;
                    holder->ip = ip;

                    return; // should return or not ?!
                }
                else{ //new IP, insert into struct.
                    len++; // COUNT AGAIN ?
                    holder->id = len;
                    holder->ip = ip;
                    holder->connNumbers = 1; // 1 connection
                

                    return; // should return or not ?!
                }
        }
    }
}

Ok so what it should is: Check the new incoming IP if it's ALREADY in the structure, then increase the number of connections for that ip If the new incoming IP is NOT inside the structure, insert it, and if it connects again, of course increase the number of connections.

I compiled a minimal example below, which you can run without problems on a Windows machine, using Visual Studio (I used 2013).

#include "stdafx.h"
#include 
#include 
using namespace std;

// the struct
typedef struct {
    int id; // is this useful anyway?
    char *ip;
    int connNumbers;
}test_sock;

// init struct
test_sock holder[5000];
int len = 0;

// the function
void AddtoStruct(char *ip)
{

    if (len == 0) //if the struct is empty, insert.
    {
        len++;
        holder->id = len;
        holder->ip = ip;
        holder->connNumbers = 1; //1 conexiune
        cout << "ADDED NEW IP: " << holder->ip << " Connections: " << holder->connNumbers << endl;
        cout << "-----------------------------------------------------------" << endl;
        return;
    }

    for (int i = 0; i <= len; i++)
    {

        if (holder->id != 0) //verificam ca sa nu fie ACELASI ID
        {
            //Exista deja in structura , doar increase connNumbers;
            if (strcmp(ip, holder->ip) == 0)
            {
                len++;
                holder->connNumbers++;
                holder->id = len;
                holder->ip = ip;
                cout << "NEW CONNECTION FROM IP: " << holder->ip << " Connections: " << holder->connNumbers << endl;
                cout << "-----------------------------------------------------------" << endl;
            }
            else{ //new IP, insert into struct.
                len++; // COUNT AGAIN ?
                holder->id = len;
                holder->ip = ip;
                holder->connNumbers = 1; // 1 connection
                cout << "CONNECTION FROM NEW IP: " << holder->ip << " Connections: " << holder->connNumbers << endl;
                cout << "-----------------------------------------------------------" << endl;
                return; // should return or not ?!
            }
        }
    }
}

// use the function

int main() {

    char *ip = "127.0.0.1";

    char *ip2 = "127.0.0.3";

    AddtoStruct(ip);
    Sleep(5); // wait for new IP
    AddtoStruct(ip2);
    Sleep(5); // wait for SAME IP
    AddtoStruct(ip);

    system("pause");
}

As you can see, if you run the code, it does not work as it should, it counts the same number of connections for every new IP ... So please give me advice, or a fix, or anything, because I really need this, and it`s been 3 days of testing, without any progress. Many thanks!

c++