Output all IP addresses in network in specific range

Does anyone know how you can most easily output all IP addresses of a network in a specific range set by an IP and a subnet mask.
Example input: 192.168.15.13 255.255.255.0
Example output: IPs from 192.168.15.0-192.168.15.255

I was searching for some kind of library, but I haven't found anything yet. At first the boost::asio::ip library seemed promising, but I could not figure out a way to get the job done.
Last edited on
The subnet mask is "old" and unintuitive. The prefix notation is harder to mess.
Your address with prefix notation: 192.168.15.13/24

The IPv4 address is unsigned 32-bit integer that is merely formatted as four 8-bit values.

There is program 'ipcalc' that calculates first (network) and last (broadcast) addresses for you.
See https://www.mankier.com/1/ipcalc
"free software; see the source for copying conditions"


The main question: do you really need to do this math within your code?
I don't think that this tool can output all IPs of a network, only information about it.
As a background: I am currently writing an SNMP scanner and I want to enable the user to scan a whole network. I have to send an SNMP request to each host of the network separately, that is why I need to generate a list of all IPs in a certain "range".
Scan, similar to 'nmap'? https://nmap.org/book/man.html


The 'ipcalc' is open source. The code is available. You can learn from it.


In the meantime:
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
// my first program in C++
#include <iostream>

void print( unsigned add ) {
    auto octet = (1 << 8);
    unsigned w = add % octet;
    add /= octet;
    unsigned z = add % octet;
    add /= octet;
    unsigned y = add % octet;
    add /= octet;
    unsigned x = add;
    std::cout << x << '.' << y << '.' << z << '.' << w << '\n';
}

int main()
{
    unsigned x=192, y=168, z=15, w=13;
    unsigned p=29;
    unsigned address = (1 << 24) * x + (1 << 16) * y + (1 << 8) * z + w;
    unsigned tail = 1 << (32-p);
    std::cout << address << " / " << tail << '\n';
    unsigned network   = address / tail * tail;
    unsigned broadcast = (address / tail + 1) * tail - 1;
    std::cout << network << " - " << broadcast << '\n';
    for ( unsigned add=network; add <= broadcast; ++ add ) {
        print( add );
    }
}
Last edited on
Topic archived. No new replies allowed.