increment IP addresses - newbie Q.

This is a discussion on increment IP addresses - newbie Q. within the C Programming forums, part of the General Programming Boards category; sorry if this has been answered before- 'search' didn't return anything though... I use a gethostbyname() to get an IP ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    10

    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...

    thanks in advance,

    W.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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 hostaddr[16];
    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" : ".");
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    10
    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. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You could do that too, there are a number of approaches to this problem.
    My best code is written with the delete key.

  5. #5
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    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. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    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;
    
        u_long S_addr;
    
     } 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 
     get_subnet(unsigned long address, int which)
    {
     return ((unsigned char*)&address)[which];
    }
    
    
    unsigned long 
     set_subnet(unsigned long * address, int which, unsigned char what)
    {
     ((unsigned char*)address)[which] = what;
     return *address;
    }

    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];
     
     unsigned long address;
     
     int byte;
         
     printf("Enter a valid ip address > ");
     
     fgets(buff, MAX, stdin);
     
        if((address = inet_addr(buff)) != INADDR_NONE)
       {
        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)
             {
              set_subnet(&address, byte, i);
              
              printf("%s\n", inet_ntoa((in_addr&)address));
             }         
          }      
       }
     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:

    printf("%s", inet_ntoa(inet_addr(inet_ntoa(inet_addr("127.0.0.1 ")))));

    - prints "127.0.0.1", of course.



    Anyway, I hope that helps.

    Happy coding.



    ITSA
    Socket Library!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    10
    thanks, ppl.
    you have really helped.

    see ya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-11-2004, 08:52 AM
  2. resolving names and ip addresses
    By dicky in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-01-2004, 03:32 PM
  3. ip addresses and ports
    By jrahhali in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-18-2003, 05:06 AM
  4. Logging IP addresses
    By paladin217 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2002, 11:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21