Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 11th, 2003, 10:55 AM
 Matthew Doucette
matthewdoucette.com

Join Date: May 2002
Posts: 635
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 12
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
February 11th, 2003, 11:49 AM
 jonsagara
Contributing User

Join Date: Dec 2001
Location: USA
Posts: 286
Time spent in forums: 7 m 23 sec
Reputation Power: 12
__________________
Jon Sagara

"Me fail English? That's unpossible!"

#3
February 11th, 2003, 03:50 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 23 h 38 m 20 sec
Reputation Power: 2199
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.

#4
February 12th, 2003, 08:39 AM
 Matthew Doucette
matthewdoucette.com

Join Date: May 2002
Posts: 635
Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 12
Very interesting, but where does 65.106.95.13 come from?

#5
February 12th, 2003, 11:03 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 23 h 38 m 20 sec
Reputation Power: 2199
Quote:
 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;

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

cp = (unsigned char*)&ulIP;

// convert dotted-decimal to network-order address
if (ulIP == -1L)
else

return 0;
}

 Viewing: Dev Shed Forums > Programming Languages > C Programming > what is the exact specifications/format for IPs?