UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2003-12-15 08:32 AM

bzchevalier
Member
Registered: 2003-09-04
Posts: 40

Re: Conversion IPv6 - IPv4

Hello Rob,

I wrote 2 functions IPv4_2_6 and IPv6_2_4 for conversions between IPv4 and IPv6...
I did as you said me in a precedent thread of discussion (IPv4 vs IPv6).

=> Do you think that these 2 functions are correct ?

[ I didn't find any system call or library function that do this conversion. We can't use successively inet_pton() and inet_ntop() for this conversion, because of the incompatibility between struct in_addr and in6_addr ... ]

But this method doesn't work for the conversion of the loopback adress :
(in IPv4 : 127.0.0.1,           
in IPv6 ::1 or  0000:0000:0000:0000:0000:0000:0000:0001  ;-)

extern const struct in6_addr in6addr_loopback;   // ::1

Am I wrong ?

By the way, when I use the following piece of code, with an IPv4 adress, and after the conversion in IPv6 with IP4_2_6(), I obtain this display at run-time with inet_ntop() in IPv6 format :

./myprog 222.173.190.239

inet_pton (222.173.190.239) = IPv4 : inet_ntop () = 222.173.190.239 
Conversion IPv6 :
IPv6 : inet_ntop () = ::ffff:222.173.190.239

It is a "bastard format" , with the unchanged IPv4 format at the end !!

Is it the same on your system ?
(I am under Linux Mandrake, kernel 2.4.19)

Do you know if we can force inet_ntop() to write it as a "true" IPv6 addr ??

Conversion IPv6 :
IPv6 : inet_ntop () = ::ffff:Dead:beef   // that's what I hope to see :-p

Here is the used code :

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define INET_ADDRSTRLEN 	16
#define INET6_ADDRSTRLEN 	46
#define ZERO			 0


void IP4_2_6(struct in_addr *adresse_4,struct in6_addr *adresse_6)
{
memset(adresse_6,ZERO,sizeof(struct in6_addr));
adresse_6->s6_addr32[3]=adresse_4->s_addr;
adresse_6->s6_addr16[5]=(unsigned short)0xFFFF;
}

void IP6_2_4(struct in_addr *adresse_4,struct in6_addr *adresse_6)
{
memset(adresse_4,ZERO,sizeof(struct in_addr));
adresse_4->s_addr=adresse_6->s6_addr32[3];
}


int main( int argc, char **argv )
{
struct in6_addr add_6;
struct in_addr  add_4;

int i;
#define BUF_SIZE 2000
char buf[BUF_SIZE];

#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46


for (i = 1; i < argc; i ++) {
	fprintf(stdout, "\ninet_pton (%s) = ", argv [i]);
	if (inet_pton (AF_INET6, argv [i], &add_6) != 0) {
		fprintf(stdout, "IPv6 : ");
		inet_ntop (AF_INET6, &add_6, buf,INET6_ADDRSTRLEN);
		fprintf(stdout, "inet_ntop () = %s \n", buf);

		fprintf(stdout,"Conversion IPv4 :\n");
		IP6_2_4(&add_4,&add_6);
		fprintf(stdout, "IPv4 : ");
		inet_ntop (AF_INET, &add_4, buf, INET_ADDRSTRLEN);
		fprintf(stdout, "inet_ntop () = %s \n", buf);
		continue;
	}

	if (inet_pton (AF_INET, argv [i], &add_4) != 0) {
		fprintf(stdout, "IPv4 : ");
		inet_ntop (AF_INET, &add_4, buf, INET_ADDRSTRLEN);
		fprintf(stdout, "inet_ntop () = %s \n", buf);

		fprintf(stdout,"Conversion IPv6 :\n");
		IP4_2_6(&add_4,&add_6);
		fprintf(stdout, "IPv6 : ");
		inet_ntop (AF_INET6, &add_6, buf, INET6_ADDRSTRLEN);
		fprintf(stdout, "inet_ntop () = %s \n", buf);
		continue;
	}
	fprintf (stdout, "invalid format \n");
}

Thanks in advance.

bzchevalier

Offline

#2 2003-12-15 01:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,833
Website

Re: Conversion IPv6 - IPv4

Why on Earth would you ever need to convert between IPv4
and IPv6 addresses, anyway??  I can't understand what possible
use you have for this...  The point of the IPv4-mapped IPv6
addresses is to hide the fact that they're IPv4 addresses from
you, so you don't have to care...  You can use them as an IPv6,
and they'll function as such...  There should never be a need,
that I can think of, to convert from one to the other...

Also, realize that there is at least one other IPv6 format which
can embed an IPv4: the "IPv4-compatible" format (as opposed
to the "IPv4-mapped" format), which uses all 0s in the upper
bits...  You'll want to read the RFCs (especially 2373, or it looks
like it's been obsoleted by 3513, now)...

And, of course your method won't work for the loopback address,
because that's NOT an embedded IPv4, of any sort!  It's a true
IPv6 address...  There's absolutely no method at all of converting
it to an IPv4...  You didn't think you could convert every possible
IPv6 into an IPv4, did you??  Obviously, that's impossible, since
IPv6 has a larger address space...  Your technique will only work
on IPv4-mapped IPv6 addresses, as you've got it now; but, it
could also be extended to work with IPv4-compatible IPv6
address, as well...  But, that's it...  Nothing else aside from these
weird IPv4 backwards-compatibility hack addresses...  So, you
will definitely want to make sure that it is one of these types
before trying to convert it back to an IPv4 (which won't work,
otherwise)...  In <netinet/in.h>, there should be a couple macros:
IN6_IS_ADDR_V4MAPPED() and IN6_IS_ADDR_V4COMPAT(),
which will do the appropriate testing...

Also, the "bastard format", as you call it, is the perfectly correct
format for an IPv4-mapped IPv6 address...  Again, read the IPv6
RFCs...  The weirdo backwards-compatible hack formats, which
embed an old IPv4 into the IPv6 are purposefully displayed with
the old-style dotted-quad format IPv4 at the end...  The only way
to get a "true" IPv6 type display is to use a "true" IPv6 address,
not one of these backwards-compatible IPv4 embedded ones...

Offline

#3 2003-12-16 08:39 AM

bzchevalier
Member
Registered: 2003-09-04
Posts: 40

Re: Conversion IPv6 - IPv4

Hello Rob,

Thank you for your answer.

I didn’t want to reinvent the wheel but only to test the behavior of inet_pton() et inet_ntop() functions, to familiarize me with IPv6 … and also with conversions between compatibles IPv4 and IPv6 addresses because the transition will be spread out over several years and it will be probably necessary for us to know how to do that (a difficulty moreover in network / socket programming).

To do that I needed IPv6 addresses and I used the explanations you gave in a precedent post.

Yes I realize that IPv6 is a little more complicated than that, and I will carefully read the RFC that I didn’t have under the hand this week-end… (I have at the moment no internet connexion at home)

bzchevalier

Offline

#4 2013-12-20 06:22 AM

mario
Guest

Re: Conversion IPv6 - IPv4

You have 1 hour, to make a IPV4 piece of software work in an IPV6 network.
All the source addresses of the mobile devices have IPV6 addresses, and they convert sensibly into an IPV4 range (that part was luck)

Customer pressure, and a cheque of 50,00USD!

and thats why you would do it!

Ultimately, we will convert the IPV4 software to support full range of IPV6, but that will happen in time.

The real world is much different to what they think, to those who commented on "Why you would do it" !!!

#5 2013-12-20 12:58 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,833
Website

Re: Conversion IPv6 - IPv4

Your scenario sounds completely and totally different from the one bzchevalier was describing, though...  His scheme would only work with IPv4-mapped/compatible IPv6 addresses...  Yours it sounds like you need to convert REAL IPv6 addresses into a temporary IPv4 (presumably in one of the RFC-1918 private subnets, or out of the unallocated range residing past the end of the multicast range) for interaction with old software that can only handle an IPv4 address...  Totally different scenario...  (And, one I've done before myself, as well!)

(And, I hope you left a zero off the amount paid, and aren't from a location that uses commas in place of decimal points...)

Offline

Board footer

Powered by FluxBB