Accepting as input, IP addresses.

Pages: 12
Instead of this (your current code, above):
 
    vector<int> octets = getOctets(ip);


try this instead:
1
2
    vector<int> octets;
    int result = getOctets(ip, octets);

Last edited on
So when I try to AND the octetsIPBits and octetsMaskBits why am I getting the wrong number netID? - I might be push_back functioning wrong on the original octetsIPBits and octetsMaskBits vectors (in the code below this post)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	vector<int> netID;
	for (int j=0; j < octetsIP.size(); j++)
	{
		if (j>0)
			cout << ".";
		for (int i=0; i < 8; i++){
			netID.push_back(octetsIPBits[i] & octetsMaskBits[i]);
			cout << netID[j];
		}
	}
	cout << "  : Network ID" << endl;
Please enter four octets in dot notation.

Enter IPv4 Address to be subnetted -> 192.168.0.1

IP in Range: YES
IP Class: Private block, Class 'C'

Please enter four octets in dot notation.


Enter subnet mask for IP address 192.168.0.1 -> 255.255.255.0

Mask in Range: YES


11000000.10101000.00000000.00000001  : IP Address
11111111.11111111.11111111.00000000  : Subnet Address
11111111.11111111.00000000.00000000  : Network ID // THIS IS WRONG


Also once this is done. How do I convert these back to decimals??? - I'm in over my head with this program but I'm determined to finish it and learn from trial.
Last edited on
Entire Function::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int getNHBits(vector<int> &octetsIP, vector<int> &octetsMask){
	// Determine Binary /--

	// Get IP binary rep. // 
	vector<int> octetsIPBits;
	for (int j=0; j < octetsIP.size(); j++)
    {
		if (j>0)
			cout << ".";

        int mask = 128;
        while (mask)
        {
            octetsIPBits.push_back((octetsIP[j] & mask) != 0);
			cout << ((octetsIP[j] & mask) != 0);
            mask >>= 1;
        }
    }
	cout << "  : IP Address" << endl;

	// Get SUBNET binary rep. // 
	vector<int> octetsMaskBits;
	for (int j=0; j < octetsMask.size(); j++)
    {
		if (j>0)
			cout << ".";
        int mask = 128;
        while (mask)
        {
            octetsMaskBits.push_back((octetsMask[j] & mask) != 0);
			cout << ((octetsMask[j] & mask) != 0);
            mask >>= 1;
        }
    }
	cout << "  : Subnet Address" << endl;
	cout << "-----------------------------------------" << endl;

	// Perform ANDing of IP and Subnet Mask to generate Network ID //
	vector<int> netID;
	for (int j=0; j < octetsIP.size(); j++)
    {
		if (j>0)
			cout << ".";
		for (int i=0; i < 8; i++){
			netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
			cout << netID[j];
		}
    }
	cout << "  : Network ID" << endl;
	return 0;
}
Last edited on
Does it need to be in binary? http://ideone.com/jIzcnz
@naraku9333 That looks very good to me.

The way i see the problem, conversion to binary is purely for human-legibility, it helps us to see and understand the data. But the computer is already working in binary, so it can handle that data just fine as it is.

My, rather long code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>

    using namespace std;

int getOctets(string ip, vector<int> &octets);
vector<int> maskAddress(vector<int> &ip, vector<int> &mask);
string toBinary(vector<int>);
string toDecimal(vector<int>);


int main()
{
    string ip_address = "192.168.0.1";
    string subnetmask = "255.255.255.0";

    vector<int> octetsIp;
    vector<int> octetsMask;
    vector<int> octetsResult;

    getOctets(ip_address, octetsIp);
    getOctets(subnetmask, octetsMask);
    octetsResult = maskAddress(octetsIp, octetsMask);

    cout << "ip_address: " << toBinary(octetsIp) << endl;
    cout << "subnetmask: " << toBinary(octetsMask) << endl;
    cout << "result:     " << toBinary(octetsResult) << endl;

    cout << "\n---------------------------\n\n";

    cout << "ip_address: " << toDecimal(octetsIp) << endl;
    cout << "subnetmask: " << toDecimal(octetsMask) << endl;
    cout << "result:     " << toDecimal(octetsResult) << endl;

    return 0;
}

// convert String to integer vector
int getOctets(string ip, vector<int> &octets)
{
    stringstream ss(ip);
    string temp;

    while (getline(ss,temp,'.'))
        octets.push_back(atoi(temp.c_str()));

    return 0;
}

// combine ip with mask
vector<int> maskAddress(vector<int> &ip, vector<int> &mask)
{
    vector<int> output;

    for (unsigned i=0; i<ip.size(); i++)
        output.push_back(ip[i] & mask[i]);

    return output;
}

// Convert to binary string "bbbbbbbb.bbbbbbbb.bbbbbbbb.bbbbbbbb"
string toBinary(vector<int> octets)
{
    string result;

    for (unsigned j=0; j < octets.size(); j++)
    {
        if (j>0)
            result += '.';

        int mask = 256;
        while (mask>>=1)
            result += '0' + ((octets[j] & mask ) != 0);
    }

    return result;
}

// Convert to decimal string "nnn.nnn.nnn.nnn"
string toDecimal(vector<int> octets)
{
    ostringstream outs;

    for (unsigned j=0; j < octets.size(); j++)
    {
        if (j>0)
            outs << '.';

        outs << octets[j];
    }

    return outs.str();
}

Output:
ip_address: 11000000.10101000.00000000.00000001
subnetmask: 11111111.11111111.11111111.00000000
result:     11000000.10101000.00000000.00000000

---------------------------

ip_address: 192.168.0.1
subnetmask: 255.255.255.0
result:     192.168.0.0
Last edited on
I'll study this after class. Thank you both.
@rcast

Replace this:
1
2
3
4
5
6
7
8
9
10
11
    // Perform ANDing of IP and Subnet Mask to generate Network ID //
    vector<int> netID;
    for (int j=0; j < octetsIP.size(); j++)
    {
        if (j>0)
            cout << ".";
        for (int i=0; i < 8; i++){
            netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
            cout << netID[j];
        }
    }


with this:
1
2
3
4
5
6
7
8
9
// Perform ANDing of IP and Subnet Mask to generate Network ID //
    vector<int> netID;
    for (int j=0; j < octetsIPBits.size(); j++)
    {
        if ((j > 0) && (j%8 == 0))
            cout << ".";
        netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
        cout << netID[j];
    }


The first version is accessing bit number [j], where j is a value from 0 to 3. That is, it uses the first four bits only, (and repeats the same identical value 8 times).

Whereas of course there are 32 bits to deal with.
Last edited on
That's the ticket. Maybe I will finish this sooner then I think.... I wont post back unless I'm stuck on a certain function for more then 3 hours.

And yes, the conversion to binary is purely cosmetic. (maybe my networking class could benefit from this)
Last edited on
Wrong place.
Last edited on
The results for the conversion of the dotted notation of the given IP can be verified at http://www.ip-details.com/ip-search/ . Where it provides the various dotted Binary, Octal, Decimal, Hexadecimal notations for the given IP. Adding to that IP location, ISP, Country, Latitude & Longitude also can be viewed.
Last edited on
Topic archived. No new replies allowed.
Pages: 12