This probably isn't very sane but it was fun to think about.
Since the IP address space is 32-bits you could write a function to convert IP addresses into unsigned 32-bit integers. Then you can add or subtract 1 or as much as you want, and convert back into an IP address. You wouldn't have to worry about range checking.
In pseduo-code for 192.123.34.134 you'd do:
int i = (192 << 24) + (123 << 16) + (34 << 8) + 134
More generally, for a.b.c.d:
int i = (a << 24) + (b << 16) + (c << 8) + d
Now change i
as much as you want (i++
, i+=10000
) and convert back:
String ip = (i >> 24) + "." +
((i >> 16) mod 256) + "." +
((i >> 8) mod 256) + "." +
(i mod 256);
Excuse the syntax - I couldn't write C++ to save myself.
MK