1. ## increment IP addresses - newbie Q.

sorry if this has been answered before- 'search' didn't return anything though...

I use a gethostbyname() to get an IP address: 10.10.10.3

I need a procedure to fill an array with a range of IP's (10.10.10.(i++)):

10.10.10.4
10.10.10.5
10.10.10.6
...

and so on...

is there a simple way to do it, or should I convert an IP to char*, cut off the last IP part, convert it to int, increment it (check if > 255) , convert back to char*, concatenate strings....

sounds awfully complex for such a simple task...

W.

2. >sounds awfully complex for such a simple task...
IP addressing doesn't exactly make it easy though. Of course, you could just convert the entire address to an array of integers and work with that:
Code:
```int i = 0;
int j;
int ip[4];
char *snip;

/* Put an ip string in hostaddr */
for (snip = strtok(hostaddr, "."); snip != NULL; snip = strtok(NULL, "."))
ip[i++] = atoi(snip);

for (j = 0; j < 4; j++)
printf("%d%s", ip[j], (j == 3) ? "\n" : ".");```

3. thanks, Sandra.

would it be a good idea to use:

Code:
```unsigned b1, b2, b3, b4;
unsigned char c;
int abc = sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c);```
and then work with b4?

4. You could do that too, there are a number of approaches to this problem.

5. Why not do it the easy way for your computer, convert the IP to binary, and simply do binary math incrimenting by one each time. Then its a simple matter to convert 4 binary octets back to ints. This is way easier than screwing around with a confusing thingy where you manually have to keep track of a base 256 number.

6. There's an even easier way.

Ip's are stored as unsigned long integers. That's 4 bytes of data.
To manipulate these bytes 'easily', the creators of the socket specification came up with this ridiculous structure:

Code:
``` struct in_addr
{
union
{
struct
{
u_char s_b1,
s_b2,
s_b3,
s_b4;
} S_un_b;

struct
{
u_short s_w1,
s_w2;
} S_un_w;

} S_un;
};```

Basically just a container for an unsigned long of course.

So to access byte[0] we 'simply' navigate to in_addr::S_un.S_un_b.s_b1 (!)

That's a bit complicated for such a trivial task!

So here's a simpler approach:

Code:
```unsigned char
{
}

unsigned long
set_subnet(unsigned long * address, int which, unsigned char what)
{
}```

Of course, the above could be coded as a macro, if necessary.

So here's an example of putting this simple little scheme to work:

Code:
```#define MAX 256

int main()
{
char buff[MAX];

int byte;

printf("Enter a valid ip address > ");

fgets(buff, MAX, stdin);

{
printf("Which subnet to manipulate? [0-3] > ");

if((byte = atoi(fgets(buff, MAX, stdin))) < 4)
{
for(int i = get_subnet(address, byte)+1; i < MAX; ++i)
{

}
}
}
system("PAUSE");
}```

>> int abc = sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c);

That's not really necessary. The inet_addr() function takes a dotted decimal string and converts it to an unsigned long.

Then you have the inet_ntoa() function that does just opposite (though you have to cast the unsigned long to an in_addr).

So between the two, you can convert back and forth rather easily, ie:

- prints "127.0.0.1", of course.

Anyway, I hope that helps.

Happy coding.

7. thanks, ppl.
you have really helped.

see ya