So I went a little stupid and called octets octals in the program but you get the picture
...
This is a simple class with some basic functions etc for modifying the ips.
This class is in a header file:
#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:
#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 .