Method to loop through network ips

Hi i really need help with problem. This problem relates to a program i am developing to ping all hosts on a network.

So far I've managed to access a PostgreSQL database to retrieve a CIDR network address e.g. 192.168.52.14/24.

I have then managed to mask the bits to get the network address.
So for e.g. above its 192.168.52.0

i now need some kind of algorithm to loop through available hosts so from.

192.168.52.0 - 192.168.52.255

I have got as far as deriving the network and separating it into an int array
so i have : ip_start[]= {192,168,52,0}

Any ideas or previous experience anyone would like to share?

Thank you very much
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdint>
#include <strstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    const std::string prefix = "192.168.52." ;

    // loop through ips 192.168.52.0 to 192.168.52.15
    for( int i = 0 ; i < 16 ; ++i )
    {
         char buffer[1024] ;
         {
             std::ostrstream stm( buffer, sizeof(buffer) ) ;
             stm << prefix << i << std::ends ;
         }

         in_addr_t addr = inet_addr(buffer) ;
         std::cout << addr << '\n' ;
    }
}

http://ideone.com/cC8Dbf
Topic archived. No new replies allowed.