After reading a note in September of an
alternative way to record IP addresses , I wondered what kind of smart programs are, and forgot.
Recently I had to install mpi for local development. I tried both MS MPI and MPICH, both ended with an unclear error on the 10051 network, as MSDN says, which means that the network is not available. I tried to overcome it for a long time, I already thought on the OS and on the software packages themselves, re-created the network interfaces. But it turned out a little differently ...
Then I accidentally remembered a note about IP addresses and it dawned on me; I gave the computer a name for the unusual: 0x0A0D. The fears were confirmed, this name was immediately converted to an IP address and the mpi program could not connect to the dispatcher.
Os windows
So, are all programs so smart? First, let's look at how the system ping utility of MS Windows resolves the incoming parameter to the IP address. This is done using the POSIX-compatible function getaddrinfo of the WinSock library. It happens like this:
struct addrinfo hints;
ZeroMemory( &hints, sizeof (hints) );
hints.ai_flags = AI_NUMERICHOST;
dwRetval = getaddrinfo(argv[1], NULL, &hints, &result);
if ( dwRetval != 0 ) {
hints.ai_flags = AI_CANONNAME;
dwRetval = getaddrinfo(argv[1], NULL, &hints, &result);
if ( dwRetval != 0 ) {
printf( "getaddrinfo failed with error: %d\n" , dwRetval);
WSACleanup();
return 1;
}
}
* This source code was highlighted with Source Code Highlighter .
Here is its description:
int WSAAPI getaddrinfo(
__in const char *nodename,
__in const char *servname,
__in const struct addrinfo *hints,
__out struct addrinfo **res
);
* This source code was highlighted with Source Code Highlighter .
The most interesting parameter is hints, which serves as a hint for interpreting the nodename input string. The ai_flags field can take several values on which the logic of converting an incoming string to an address depends. In the ping utility, as in many other programs, an attempt is first made to directly convert a string to an IP address, and only then it is repeated through the DNS subsystem. It is quite logical and rational.
')
And so, what happens when we force a direct transformation? In the depths of the winsock code, there is a lot of transformations and checks, but in the end the execution flow reaches the
RtlIpv4StringToAddressW function of the
Ntdll.dll library. By the way, this function is completely independent of the network subsystem and can be used without explicit initialization of WinSock. It just interprets various options for recording IP addresses. MSDN talks about all formats, but why exactly are they?
Linux OS
With this operating system, everything is much simpler, since the source code of its and its many utilities are open. Having found the first source of the ping utility for Linux, you can see how it converts a string to an IP address:
bzero(( char *)&whereto, sizeof ( struct sockaddr) );
to->sin_family = AF_INET;
to->sin_addr.s_addr = inet_addr(av[0]);
if (to->sin_addr.s_addr != (unsigned)-1) {
strcpy(hnamebuf, av[0]);
hostname = hnamebuf;
} else {
hp = gethostbyname(av[0]);
if (hp) {
to->sin_family = hp->h_addrtype;
bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
hostname = hp->h_name;
} else {
printf( "%s: unknown host %sn" , argv[0], av[0]);
exit(1);
}
}
* This source code was highlighted with Source Code Highlighter .
As you can see, the code is different, but by defining the function assignments, it can be understood that the algorithms are identical.
After reading man inet_addr , it becomes clear that this function is similar to
RtlIpv4StringToAddressW in Windows. Also in her description mentioned the mysterious standard
POSIX.1-2001 .
POSIX.1-2001 standard
And so the standard says, the function must accept a string in one of the following formats:
- abcd - When 4 parts are indicated, each part must be interpreted as a data byte and assigned from left to right to four bytes of an IP address.
- abc - When 3 parts are indicated, the third part is interpreted as a 16-bit value and assigned to the right two bytes of the IP address. Thus, you can jot down addresses in class B networks.
- ab - When two parts are indicated, the second part is interpreted as a 24-bit value and is used to briefly write class A addresses.
- a - When one part is indicated, it must be directly converted to an IP address without permutations.
All parts of the dotted IP address notation should be numbers in decimal, octal, or hexadecimal numbering systems according to ISO C.
findings
- You need to select the network name of the computer that cannot be directly converted to an address, otherwise the computer may not be available by name
- IP4 address translation has nothing to do with writing IPv6 addresses or URLs, but IP4 addresses are a subset of IPv6 addresses
- This works only on POSIX compatible systems and only on programs that first try to directly translate the address (on some Linux, the type A address is not perceived)
- For local connections, it is better to use the name localhost, not the computer name :)
Links
Alternative way to write IP addressesThe ping pageinet_addr IEEE Std 1003.1