In C ++, enter the start IP and end IP. An IP increment is required to scan a fixed IP range. In general, how can I change the IP example 192.168.1.1 to 192.168.1.2 and so on in C ++? Thank you in advance.

    3 answers 3

    Usually, the usual entry of an IP address in the form of four, separated by dots of numeric values ​​in decimal or hexadecimal form, translates into the most common unsigned int32 or any other unsigned integer of 4 bytes.

    To do this, all four numbers are added bit-wise, given the shift:

     return (first << 24) | (second << 16) | (third << 8) | (fourth); 

    To translate back, do the opposite, allocate each of the numbers + shift + mask, there are examples on the Internet.

    Having a network mask and a network address, you can always get the first and last IP in a given range. To get the first address, mask the IP & SubnetMask . To get the latest IP | !SubnetMask IP | !SubnetMask ( ! - sign corresponds to negation). And do not forget that the first address in the received range is the network, the last one is the broadcast address.

      Use standard network functions inet_addr (), inet_ntoa (), ntohl (), htonl ()

      For example, this code should (did not check) increase IP in the storokovy notation. Do not forget about (depending on the CPU) a different order of bytes in the binary representation of IP on the network and host (arithmetic is naturally in the host byte order).

       #include <sys/socket.h> // не уверен, нужен ли здесь #include <netinet/in.h> #include <arpa/inet.h> ... uint32_t ip1 = (uint32_t)inet_addr("192.168.1.1"); // в двоичное в сетевом uint32_t ip2 = ntohl(ip1); // в порядок байт хоста ip2++; // арифметика // strdup() т.к. inet_ntoa() возвращает static char * char *newip = strdup(inet_ntoa(htonl(ip2))); // htonl() вычисленный IP опять в сетевой 

      Well, and classes, redefinition of operations, etc. it is up to you (if you want to confuse people, then go ahead).


      update 1

      After, I remembered.

      Be careful with htonl () and ntohl () in comparison operations . Once in RedHat (I don’t remember the version of gcc) I ran into a mistake with gcc -O3. There, instead of a function call, an assembler code was inserted and the result type conversion was thrown into if .

      Just in case when working with IP ranges, assign the result of calls to ntohl (), htonl () to variables and work with them.

      • Thanks a lot, everyone!!! - Pentium

      Something like this. I can not guarantee the accuracy of the syntax, I have not written on the pros for a long time.

       char ip[MAX_IP]; strcpy(ip, "192.168.1.1"); byte x1,x2,x3,x4; sscanf(ip, "%d.%d.%d.%d", &x1,&x2,&x3,&x4); sprintf(ip, "%d.%d.%d.%d", x1,x2,x3,x4 + 1); 

      Here the essence is stated, for good it is necessary to arrange the address in a class for which to define the operators of increment, comparison, etc.

      • Thanks, and in C # the same code is suitable? - Pentium
      • there is no scanf, you have to parse the line differently. options - many: via string.Split, regular expressions, any Reader - ganouver