#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    what is the exact specifications/format for IPs?


    What is the exact specifications/format for IPs?

    This is what I know:

    4 numbers [0..255] seperated by periods [.]

    ex. 123.45.67.89

    However, does this ever occur (leading zeros)?
    123.045.067.089

    (Sorry for posting here, but I was not sure where I should post this. Seeing that this is related to my C project, I posted here.)
    Matthew Doucette / Xona.com
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    14
    Jon Sagara

    "Me fail English? That's unpossible!"
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Try the leading zeros. I did and it didn't work.

    Consider the following:

    C:\>ping www.yahoo.com

    Pinging www.yahoo.akadns.net [66.218.71.88] with 32 bytes of data:

    Reply from 66.218.71.88: bytes=32 time=327ms TTL=50
    Reply from 66.218.71.88: bytes=32 time=286ms TTL=50
    Reply from 66.218.71.88: bytes=32 time=310ms TTL=50
    Reply from 66.218.71.88: bytes=32 time=324ms TTL=50

    Ping statistics for 66.218.71.88:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 286ms, Maximum = 327ms, Average = 311ms

    C:\>ping 066.218.071.088

    Reply from 65.106.95.13: Destination net unreachable.
    Reply from 65.106.95.13: Destination net unreachable.
    Reply from 65.106.95.13: Destination net unreachable.
    Reply from 65.106.95.13: Destination net unreachable.

    Ping statistics for 54.218.57.72:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

    C:\>

    It appears that the leading zero fools ping into interpreting the octet as being in octal (octal 66 == decimal 54 and octal 71 == decimal 57). (Not so) coincidentally, C interprets numeric constants with leading zeros as being in octal.

    Therefore, I would recommend against allowing leading zeros, or at least stripping them out of your input.
  6. #4
  7. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Very interesting, but where does 65.106.95.13 come from?
    Matthew Doucette / Xona.com
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by Doucette
    Very interesting, but where does 65.106.95.13 come from?
    nslookup says its name is ia.xo.com, but when I try to ping it DNS fails to resolve the address. I assume that it is part of the Internet infrastructure.

    Back to the original question of leading zeros, for general purposes, I wrote a quick program last night to verify that ping's treatment of IP-octet leading zeros was not atypical.

    The standard sockets function for converting a dotted-decimal character string to a usable IP address (unsigned long in network byte-order) is inet_addr(). The program, iptest, takes a dotted-decimal address as its input, converts it with inet_addr(), and prints out the contents of the resultant address. The listing is at the end.

    Under Winsock:
    C:\dcw\PROJECTS\IPtest>iptest 66.218.71.85
    66.218.71.85 --> 66.218.71.85

    C:\dcw\PROJECTS\IPtest>iptest 066.218.071.085
    066.218.071.085 --> 54.218.57.69

    On Linux:
    [dwise@pc10593 iptest]$ ./iptest 66.218.71.85
    66.218.71.85 --> 66.218.71.85
    [dwise@pc10593 iptest]$ ./iptest 066.218.071.85
    066.218.071.85 --> 54.218.57.85
    [dwise@pc10593 iptest]$ ./iptest 066.218.071.085
    066.218.071.085 is not an IP address
    [dwise@pc10593 iptest]$ ./iptest 066.218.071.045
    066.218.071.045 --> 54.218.57.37

    Interestingly, on Linux, an invalid octal digit (eg, '8') results in an error, whereas under Winsock an error is not generated even though it somehow messes up the octal conversion.


    compiled under Windows with: gcc main.c -o iptest -lwsock32
    WINSOCK must be defined
    compiled under Linux with: gcc main.c -o iptest
    WINSOCK must be undef'd

    #define WINSOCK
    #include <stdio.h>
    #ifdef WINSOCK
    #include "winsock.h"
    #else
    #include "arpa/inet.h"
    #endif

    int main(int argc, char *argv[])
    {
    unsigned long ulIP;
    unsigned char *cp;
    char address[80];

    if (argc != 2) // ie, if no argument passed
    {
    printf("Usage: iptest <dotted decimal IP address>\n");
    exit(1);
    }

    strcpy(address,argv[1]);
    cp = (unsigned char*)&ulIP;

    // convert dotted-decimal to network-order address
    ulIP = inet_addr(address);
    if (ulIP == -1L)
    printf("%s is not an IP address\n",address);
    else
    printf("%s --> %d.%d.%d.%d\n",address,*cp,*(cp+1),*(cp+2),*(cp+3));

    return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo