Author Topic: [C++] Ip Class + Ip Generator Functions  (Read 911 times)

0 Members and 1 Guest are viewing this topic.

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
[C++] Ip Class + Ip Generator Functions
« on: March 01, 2014, 10:50:44 pm »
So I went a little stupid and called octets octals in the program but you get the picture  :P ...

This is a simple class with some basic functions etc for modifying the ips.
This class is in a header file:
Code: (cpp) [Select]
#ifndef IPOBJECT_H
#define IPOBJECT_H

#include <cassert>
#include <iostream>

using namespace std;
class ip{
public:
    int m_nOctal1, m_nOctal2, m_nOctal3, m_nOctal4;


    ip()
    {
        m_nOctal1 = 0;
        m_nOctal2 = 0;
        m_nOctal3 = 0;
        m_nOctal4 = 0;
    }

   
   


   
    int octalAdd(int x, short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            return    m_nOctal1 + x;
        else if(nOctalNum == 2)
            return    m_nOctal2 + x;
        else if(nOctalNum == 3)
            return    m_nOctal3 + x;
        else if(nOctalNum == 4)
            return m_nOctal4 + x;
       
    }

    int octalSub(int x, short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            return m_nOctal1 - x;
        else if(nOctalNum == 2)
            return m_nOctal2 - x;
        else if(nOctalNum == 3)
            return m_nOctal3 - x;
        else if(nOctalNum == 4)
            return m_nOctal4 - x;
       
    }

        void octalSub(short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            --m_nOctal1;
        else if(nOctalNum == 2)
            --m_nOctal2;
        else if(nOctalNum == 3)
            --m_nOctal3;
        else if(nOctalNum == 4)
            --m_nOctal4;
       
    }
        void octalAdd(short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            ++m_nOctal1;
        else if(nOctalNum == 2)
            ++m_nOctal2;
        else if(nOctalNum == 3)
            ++m_nOctal3;
        else if(nOctalNum == 4)
            ++m_nOctal4;
       
    }

    void setOctal(int x, short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            m_nOctal1 = x;
        else if(nOctalNum == 2)
            m_nOctal2 = x;
        else if(nOctalNum == 3)
            m_nOctal3 = x;
        else if(nOctalNum == 4)
            m_nOctal4 = x;
    }
   
    void setOctalMin(short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            m_nOctal1 = 0;
        else if(nOctalNum == 2)
            m_nOctal2 = 0;
        else if(nOctalNum == 3)
            m_nOctal3 = 0;
        else if(nOctalNum == 4)
            m_nOctal4 = 0;
       
    }

        void setOctalMax(short nOctalNum)
    {
        assert(nOctalNum > 0 && nOctalNum < 5);

        if(nOctalNum == 1)
            m_nOctal1 = 255;
        else if(nOctalNum == 2)
            m_nOctal2 = 255;
        else if(nOctalNum == 3)
            m_nOctal3 = 255;
        else if(nOctalNum == 4)
            m_nOctal4 = 255;
       
    }

    void setOctal()
    {
        m_nOctal1 = 0;
        m_nOctal2 = 0;
        m_nOctal3 = 0;
        m_nOctal4 = 0;
       
    }

    void printIp()
    {
    cout << m_nOctal4 << "." << m_nOctal3 << "." << m_nOctal2 << "." << m_nOctal1 << endl;
    }
};

#endif
These are the functions. There is ipScanUp, ipScanDown, and ipScanRange. ipScanUp finds all ips going up from 0.0.0.0 and ipScanDown goes down from 255.255.255.255. ipScanRange is an overloaded function. If it is given one int argument then it will scan up from int.0.0.0, 2 arguments int.int.0.0. This works for three arguments as well:
Code: (cpp) [Select]
#include "stdafx.h"
#include "ipObject.h"

void ipScanUp(ip ip1)
{
    bool isNotSet = true;
    ip1.setOctalMin(1);
    ip1.setOctalMin(3);
    ip1.setOctalMin(4);
   
    while(ip1.m_nOctal1 < 255 || ip1.m_nOctal2 < 255 || ip1.m_nOctal3 < 255 || ip1.m_nOctal4 < 255)

    {
        if(ip1.m_nOctal1 < 255)
        {
            if(isNotSet)
            {   
                isNotSet = false;
            }else{
                ip1.octalAdd(1);
            }
            if(isNotSet)
                ip1.printIp();
        }else if(ip1.m_nOctal2 < 255)
        {
            ip1.octalAdd(2);
            ip1.setOctalMin(1);
        }else if(ip1.m_nOctal3 < 255)
        {
            ip1.octalAdd(3);
            ip1.setOctalMin(2);           
        }else if(ip1.m_nOctal4 < 255)
            {
                ip1.octalAdd(4);
                ip1.setOctalMin(3);
        }
        //Action with Ip Goes here
        if(!isNotSet)
        ip1.printIp();       
}
}

void ipScanDown(ip ip1)
{
    bool isFirstRun;
    ip1.setOctalMax(1);
    ip1.setOctalMax(2);
    ip1.setOctalMax(3);
    ip1.setOctalMax(4);

    while(ip1.m_nOctal1  > 0 || ip1.m_nOctal2 > 0 || ip1.m_nOctal3 > 0 || ip1.m_nOctal4 > 0)

    {
        if(ip1.m_nOctal4 >= 0)
        {
            if(ip1.m_nOctal1 == 255)
                isFirstRun = true;
            else
                isFirstRun = false;
            if(isFirstRun)
                ip1.printIp();
            ip1.octalSub(4);
        }else if(ip1.m_nOctal3 > 0)
        {
            ip1.octalSub(3);
            ip1.setOctalMax(4);
        }else if(ip1.m_nOctal2 > 0)
        {
            ip1.octalSub(2);
            ip1.setOctalMax(3);           
        }else if(ip1.m_nOctal1 > 0)
            {
                ip1.octalSub(1);
                ip1.setOctalMax(2);
        }
        //Action with Ip Goes here
        if(!isFirstRun)
        ip1.printIp();   
    }
}


void ipScanRange(int octal4, int octal3, int octal2, ip ip1)
{
    bool isNotSet = true;
    ip1.m_nOctal4 = octal4;
    ip1.m_nOctal3 = octal3;
    ip1.m_nOctal2 = octal2;
    ip1.setOctalMin(1);

    while(ip1.m_nOctal1 < 255)
    {
        if(ip1.m_nOctal1 < 255)
        {
            if(isNotSet)
            {   
                isNotSet = false;
            }else{
                ip1.octalAdd(1);
            }
            if(isNotSet)
                ip1.printIp();

        //Action with Ip Goes here
        if(!isNotSet)
        ip1.printIp();
    }
}
}

void ipScanRange(int octal4, int octal3, ip ip1)
{
    bool isNotSet = true;
    ip1.m_nOctal4 = octal4;
    ip1.m_nOctal3 = octal3;
    ip1.setOctalMin(2);
    ip1.setOctalMin(1);

    while(ip1.m_nOctal1 < 255 || ip1.m_nOctal2 <255)
    {
        if(ip1.m_nOctal1 < 255)
        {
            if(isNotSet)
            {   
                isNotSet = false;
            }else{
                ip1.octalAdd(1);
            }
            if(isNotSet)
                ip1.printIp();
        }else if(ip1.m_nOctal2 < 255)
        {
            ip1.octalAdd(2);
            ip1.setOctalMin(1);
        }
        //Action with Ip Goes here
        if(!isNotSet)
        ip1.printIp();
   
}
}

void ipScanRange(int octal4, ip ip1)
{
    bool isNotSet = true;
    ip1.m_nOctal4 = octal4;
    ip1.setOctalMin(3);
    ip1.setOctalMin(2);
    ip1.setOctalMin(1);

    while(ip1.m_nOctal1 < 255 || ip1.m_nOctal2 <255 || ip1.m_nOctal3 < 255)
    {
        if(ip1.m_nOctal1 < 255)
        {
            if(isNotSet)
            {   
                isNotSet = false;
            }else{
                ip1.octalAdd(1);
            }
            if(isNotSet)
                ip1.printIp();
        }else if(ip1.m_nOctal2 < 255)
        {
            ip1.octalAdd(2);
            ip1.setOctalMin(1);
        }else if(ip1.m_nOctal3 < 255)
        {
            ip1.octalAdd(3);
            ip1.setOctalMin(2);
        }
        //Action with Ip Goes here
        if(!isNotSet)
        ip1.printIp();
   
}
}
This was used in a larger project so I thougt I would share, by the way there was an off by one bug I am pretty sure it is fixed but there could potentially be errors towards the end of ipScanUp and ipScanDown because I did not watch them print all of the values .
« Last Edit: March 01, 2014, 11:57:23 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C++] Ip Class + Ip Generator Functions
« Reply #1 on: March 01, 2014, 11:30:55 pm »
I don't understand the code very well, but all those gruesome if/else could be easily replaced with switch, since you just check values from 1 to 4 with if's anyway.
What project was this used in?

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [C++] Ip Class + Ip Generator Functions
« Reply #2 on: March 03, 2014, 07:57:49 am »
Code: (c++) [Select]
using namespace std; Someone once told me never to call "using namespace std" in a header file.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: [C++] Ip Class + Ip Generator Functions
« Reply #3 on: March 03, 2014, 03:11:19 pm »
Code: (c++) [Select]
using namespace std; Someone once told me never to call "using namespace std" in a header file.
Yeah my bad should not have done that...
EDIT:
In the case of classes in header files it is fine
« Last Edit: March 03, 2014, 04:22:23 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline I_Learning_I

  • Knight
  • **
  • Posts: 267
  • Cookies: 26
  • Nor black or white, not even grey. What hat am I?
    • View Profile
    • Hacking F0r Fr33
Re: [C++] Ip Class + Ip Generator Functions
« Reply #4 on: March 03, 2014, 09:48:43 pm »
A huge part of this could easily be replaced with 4 for's + 1 evaluation function.
Adding the input and making some ugly global variables.

Code: [Select]
    bool bFor1=true;
    int iFor1=23;
int iValue(){ //Here would take parameteres boolean and initial value

if(bFor1==true)
    return iFor1;
else
    return 0;
}
int main()
{
  for(int i = iValue();i<255;i++){
       cout<<i<<" ";
   }
   return 0;
}
« Last Edit: March 03, 2014, 09:50:56 pm by I_Learning_I »
Thanks for reading,
I_Learning_I