Jump to content
Tuts 4 You
LCF-AT

How to convert ip6 to ip4 or name?

Rate this topic

Recommended Posts

LCF-AT

Hi  guys,

does anyone know whether its possible to convert a ip6 to ip4?Or is it possible to get a ip4 address anyhow if I have only a ip6 address?I also tried to use netstat command with diffrent paramters and also without to get name addresses but aso in that case I only get ip6 addresses for specific sites.Now the question is how to get a ip4 or name address with that ip6 address etc.The nslookup command I can only use with names or ip4 addresses.Thats the problem.Maybe you know any solution.

greetz

Share this post


Link to post
Share on other sites
Techlord

There isn't a 1-1 correspondence between IPv4 and IPv6 addresses all the time. So what you are tring to accomplish is not always possible.

There is a particular range of IPv6 addresses that actually represent the IPv4 address space, but general IPv6 addresses will not be from this range.

For IPv6 that CAN indeed be mapped, you can do so using either this JAVA library here or by using Javascript as seen below :

<script>
function parseIp6(str)
{
  //init
  var ar=new Array;
  for(var i=0;i<8;i++)ar[i]=0;
  //check for trivial IPs
  if(str=="::")return ar;
  //parse
  var sar=str.split(':');
  var slen=sar.length;
  if(slen>8)slen=8;
  var j=0;
  for(var i=0;i<slen;i++){
    //this is a "::", switch to end-run mode
    if(i && sar[i]==""){j=9-slen+i;continue;}
    ar[j]=parseInt("0x0"+sar[i]);
    j++;
  }

  return ar;
}
function ipcnvfrom6(ip6)
{
  var ip6=parseIp6(ip6);
  var ip4=(ip6[6]>>8)+"."+(ip6[6]&0xff)+"."+(ip6[7]>>8)+"."+(ip6[7]&0xff);
  return ip4;
}
alert(ipcnvfrom6("::C0A8:4A07"));
</script>

Sorry Guys, but if I use the

 tags, it loses all its formatting and so I pasted it directly..


Of course, the above library and code ar euseful only in cases where the IPv6 address can be mapped to an IPv4 one !

You can also read the article here.

Greetz :D

 

 

Edited by Techlord (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites
LCF-AT

Hi again,

so I tried to play with my network card settings and there I have 2 settings for ip4 & ip6 which I can enable / disable.Normaly I have both enabled.Now I enter any site I wanna check in browser and start CMD tool and enter netstat command with paramters what shows me a overview / status and ip / port addresses but in this case I only get for the most sites ip6 remote addresses to see + ports.Now I have ip6 X of the site I wanted to check.So far so clear right.But the problem is I cant use ip6 addresses and need to know / get a ip4 address for that site to work go on with that.If I now disable ip6 in my network card settings and do check the same site again then I get a ip4 address and this I also wanna have but its only  working if I just disable ip6 settings in my network card you know.So I dont wanna each time disable ip6 in my network card settings only to get the ip4 address listet.Thats the reason why I did ask whether its possible to convert or build a ip4 version address from a ip6 version address I have.Or is there any CMD command / paramter I could use what does show me the desired ipX version of addresses?Or are there any WinSock APIs I could use to convert ip6 to ip4 or anything like that?Just wanna prevent to disable ip6 settings in my network card if I need to know the ip4 address.So I think somehow there should be a way to handle that too or?

greetz

  • Like 1

Share this post


Link to post
Share on other sites
roocoon

How about an online conversion?

http://ipv6.ztsoftware.net/ipv4-to-ipv6/

Enter you IPv6 on the IP field and click the IPv4 button above it. It's a flip-flop button so you can try any combination.

 

  • Like 1

Share this post


Link to post
Share on other sites
LCF-AT

Hi,

so I tried this site with that calc tool and did some tests.Some are working and some not so.So if I enter the ip6 from the site I wanted to check  then I get the right ip4 back but if I do it with a other ip6 from goog*l then I get something else as ip4 back.

So do you know how to calc this ip6 to ip4 manually?Or do you know any CMD command I can use with ip6 to get ip4 back?

greetz

Share this post


Link to post
Share on other sites
roocoon

Even if you do it manually, it still will not work for all cases.

Check Techlord's link above. It contains another link to a C version of a converter (this one: http://developerweb.net/viewtopic.php?id=3434 ) as well as some comments that explain the problem. Basically, you can't do a 1-to-1 mapping of the much larger IPv6 range into the smaller IPv4 one.

I'm glad I never had to be a network admin. This topic is one of the few that gives me a headache :)

 

  • Like 2

Share this post


Link to post
Share on other sites
LCF-AT

Hi again,

hmmm,so its already strange.So the question is how does the CMD command netstat does it to get one time ip6 and one time ip4 just if I only disable ip6 in my network card.My main goal was it not to disable ip6 options anymore to get the ip4 version address.So anyhow it sucks. :)

greetz

  • Like 1

Share this post


Link to post
Share on other sites
kao

You can spend days writing long complaining posts how everything sucks and how the world doesn't work the way you think it should. Or..

Or you can learn the basics of networking.

 

---------------------------

TL;DR version.
IF you know the domain name you're looking for, just use proper Windows APIs and you can resolve it to IPv4 address. Or IPv6 for that matter.
IF you only know IPv6 address and want to get IPv4 address of the same server, there's only a very hackish way. Resolve IPv6 address to a server name via PTR record, then get the A record for that address. It will work in most cases but it is a hack. DnsQuery is the Windows API function you're looking for.

 

No, I will not write a sample code for you.

 

---------------------------

Crash course in DNS stuff. It's not 100% accurate, but enough to get you started.

 

About IP addresses.

IPv4 and IPv6 are 2 totally different protocols. It's like apples and oranges.
Normally you can't convert IPv6 address to IPv4 address, just like you can't make apple marmelade from oranges.
There are some compatibility mechanisms, created to make migration from IPv4 to IPv6 easier. One of them is 6to4, this is what tools suggested above use. It is a decision of IPv4 address owner, if he will implement those mechanisms or not. Most of them don't.
Also, since those 2 protocols are totally different, there is absolutely no guarantee that IPv6 server gives the exact same content as IPv4 server. 

 

About domain name resolution.

The process of converting domain name into IP address is called domain name resolution.
Domain name owner publishes DNS records for each public server in his/her domain. These records tell you everything about the domain - what is servers IPv4 address, IPv6 address, where the mailserver  is located and many many more.
The most important records for you are A and AAAA. A records tell you the IPv4 address of the server, AAAA records tell you IPv6 address of the server (if there is any).

If you want to try it online, here is one of such checkers:
https://mxtoolbox.com/SuperTool.aspx?action=a%3atuts4you.com&run=toolpage#   - shows A record of tuts4you.com It tells you that IPv4 address of tuts4you server is 198.57.187.53.
https://mxtoolbox.com/SuperTool.aspx?action=aaaa%3atuts4you.com&run=toolpage# - shows that tuts4you.com doesn't have IPv6 address.
https://mxtoolbox.com/SuperTool.aspx?action=a%3alifeinhex.com&run=toolpage#   - shows A record of my server lifeinhex.com. IPv4 address is 31.15.10.21.
https://mxtoolbox.com/SuperTool.aspx?action=aaaa%3alifeinhex.com&run=toolpage# - shows AAAA record of my server lifeinhex.com. IPv6 address is 2a02:4a8:ac24:110::10:21

 

About reverse DNS lookup.

The process of converting IP address back to domain name is called reverse DNS lookup. DNS uses PTR records for that.
The results are usualy crap and not the ones you were looking for.

When there is only one domain hosted on a server, it can work nicely. But one physical server can host thousands of domains, so it's normal lots of that different domains will resolve to the same IP address.
As an example, there are ~900 sites hosted on the same server as my blog. When you take the IP address and try to resolve it back to the name, which of the 900 domain names you will get? Correct answer is - neither one of those, you will get some server name assigned by my provider.

If you want to try, here are PTR records you're looking for:
https://mxtoolbox.com/SuperTool.aspx?action=ptr:198.57.187.53&run=toolpage#   - tuts4you.com IP address resolves to "tut.tuts4you.com"
https://mxtoolbox.com/SuperTool.aspx?action=ptr%3a31.15.10.21&run=toolpage#  - my blog's IP address doesn't resolve nicely. It is "uvirt58.active24.cz"

 

About networking in Windows.

If both IPv4 and IPv6 are active in Windows, it will try to use the more modern IPv6. That's by design, to allow the world to switch slowly to IPv6 . 

 

Nslookup does exactly what I described above - takes a domain name and gets you IPv4/IPv6 address of that site. 

C:\>nslookup www.lifeinhex.com
Server:  resolver1.opendns.com
Address:  208.67.222.222

Non-authoritative answer:
Name:    www.lifeinhex.com
Addresses:  2a02:4a8:ac24:110::10:21
          31.15.10.21

As you can see, there are both IPv6 and IPv4 addresses here.

 

Netstat command shows the active connections. If Windows used IPv6 for connection, it shows IPv6 addresses. If IPv4 was used, it shows IPv4 addresses.

For example, "netstat -n" will print:

  TCP    192.168.0.5:63805      31.15.10.21:443        ESTABLISHED
  TCP    192.168.0.5:63940      198.57.187.53:443      TIME_WAIT

showing both IPv4 addresses for lifeinhex.com and forum.tuts4you.com. Obviously, I don't have IPv6 here, you'd see IPv6 addresses instead.

 

"netstat -f" will try to resolve addresses back to the names, so it will print:

  TCP    192.168.0.5:63805      uvirt58.active24.cz:https  ESTABLISHED  
  TCP    192.168.0.5:63940      tut.tuts4you.com:https     TIME_WAIT

when in fact I was connecting to lifeinhex.com and forum.tuts4you.com. Then you can take those server names and use nslookup to (hopefully) get IPv4 addresses of those servers.

 

About programming Winsock.

According to MSDN, gethostbyname will always return IPv4 address from the domain name.
You can use getaddrinfo and pick the proper addrinfo structure (the one where ai_family is AF_INET / AF_INET6) to get either IPv4 or IPv6 address.
And finally DnsQuery can get you any DNS record you need, that includes PTR records.

  • Like 4

Share this post


Link to post
Share on other sites
LCF-AT

Hi kao,

thanks for your answer so far. :)

So as I said before I cant get the name of the site also not if I use netstat -f.Also in this case I only get the ip6.Ok,so now I tried to check out what you told in your last step about WinSock and using getaddrinfo API.I tried it but I just get WSANO_RECOVERY error back.Maybe I did something wrong to filling the struct correctly etc.

addrinfo                    STRUCT
    ai_flags                DWORD ?
    ai_family               DWORD ?
    ai_socktype             DWORD ?
    ai_protocol             DWORD ?
    ai_addrlen              DWORD ?
    ai_canonname            DWORD ?
    ai_addr                 DWORD ?
    ai_next                 DWORD ?
addrinfo                    ENDS

local   AI:addrinfo
Local   ppaddrinfo:addrinfo
local   AIBUFFER [100]:BYTE    

    mov AI.ai_flags,0100h ;AI_ALL
    mov AI.ai_family,AF_INET6
    mov AI.ai_socktype,SOCK_STREAM
    mov AI.ai_protocol,IPPROTO_TCP
    mov AI.ai_addrlen,sizeof AIBUFFER
    lea eax,AIBUFFER
    mov AI.ai_addr,eax
    mov AI.ai_next,0
    invoke getaddrinfo,esi,0,addr AI,addr ppaddrinfo

In esi is the ip6 string pointer.I also wanted to try AI_NUMERICHOST but I dont know the value for this and it seems I dont have all files with all structs and name declarations here.

greetz

Share this post


Link to post
Share on other sites
Downpour
7 hours ago, LCF-AT said:

Hi kao,

thanks for your answer so far. :)

So as I said before I cant get the name of the site also not if I use netstat -f.Also in this case I only get the ip6.Ok,so now I tried to check out what you told in your last step about WinSock and using getaddrinfo API.I tried it but I just get WSANO_RECOVERY error back.Maybe I did something wrong to filling the struct correctly etc.


addrinfo                    STRUCT
    ai_flags                DWORD ?
    ai_family               DWORD ?
    ai_socktype             DWORD ?
    ai_protocol             DWORD ?
    ai_addrlen              DWORD ?
    ai_canonname            DWORD ?
    ai_addr                 DWORD ?
    ai_next                 DWORD ?
addrinfo                    ENDS

local   AI:addrinfo
Local   ppaddrinfo:addrinfo
local   AIBUFFER [100]:BYTE    

    mov AI.ai_flags,0100h ;AI_ALL
    mov AI.ai_family,AF_INET6
    mov AI.ai_socktype,SOCK_STREAM
    mov AI.ai_protocol,IPPROTO_TCP
    mov AI.ai_addrlen,sizeof AIBUFFER
    lea eax,AIBUFFER
    mov AI.ai_addr,eax
    mov AI.ai_next,0
    invoke getaddrinfo,esi,0,addr AI,addr ppaddrinfo

In esi is the ip6 string pointer.I also wanted to try AI_NUMERICHOST but I dont know the value for this and it seems I dont have all files with all structs and name declarations here.

greetz

Probably because you've set ai_addrlen and ai_addr to a non-zero value.

Quote
pHints [in, optional]

A pointer to an addrinfostructure that provides hints about the type of socket the caller supports. 

The ai_addrlenai_canonnameai_addr, and ai_next members of the addrinfo structure pointed to by the pHints parameter must be zero or NULL. Otherwise the GetAddrInfoEx function will fail with WSANO_RECOVERY.

See the Remarks for more details.

 

Edited by Castor (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites
LCF-AT

Hi Castor,

also if I set them to zero it dosent work and I get a error back.I think I do something wrong but as always I have trouble to understand the entire MSDN descriptions.Maybe anyone else can see what I did wrong or could post a short example later.

greetz

Share this post


Link to post
Share on other sites
evlncrn8

hi @LCF-AT, tried pming you but it buggered up, nehows i remember a while ago you were looking for ways to set timeouts etc in wininet and i found it...

            INTERNET_VERSION_INFO internetversioninfo;

            memset(&internetversioninfo, NULL, (sizeof(internetversioninfo)));

            DWORD queryoptionsize = (sizeof(internetversioninfo));

            if (InternetQueryOption((HINTERNET) NULL,INTERNET_OPTION_VERSION, &internetversioninfo, &queryoptionsize) ) {

                printf("\r\n Internet Version %0lu.%0lu", internetversioninfo.dwMajorVersion, internetversioninfo.dwMinorVersion);

            }

            DWORD connectretries = 0;
            queryoptionsize = (sizeof(connectretries));

            if (InternetQueryOption((HINTERNET) NULL, INTERNET_OPTION_CONNECT_RETRIES, &connectretries, &queryoptionsize) ) {

                printf("\r\n Connect retries : %0lu", connectretries);

            }

            DWORD connecttimeout = 0;
            queryoptionsize = (sizeof(connecttimeout));

            if (InternetQueryOption((HINTERNET) NULL, INTERNET_OPTION_CONNECT_TIMEOUT, &connecttimeout, &queryoptionsize) ) {

                printf("\r\n Connect timeout : 0x%0X (%0lu) (ms)", connecttimeout, connecttimeout);

            }

            DWORD recvtimeout = 0;
            queryoptionsize = (sizeof(recvtimeout));

            if (InternetQueryOption((HINTERNET) NULL, INTERNET_OPTION_RECEIVE_TIMEOUT, &recvtimeout, &queryoptionsize) ) {

                printf("\r\n Recv timeout    : 0x%0X (%0lu) (ms)", recvtimeout, recvtimeout);

            }
 

examples there are in c, but im sure you can port them easily, to so the setting its InternetSetOption,

where you'd pass the connected handle.. its pretty straightforward

  • Like 1

Share this post


Link to post
Share on other sites
Downpour
On 5.7.2017 at 7:02 PM, LCF-AT said:

Hi Castor,

also if I set them to zero it dosent work and I get a error back.I think I do something wrong but as always I have trouble to understand the entire MSDN descriptions.Maybe anyone else can see what I did wrong or could post a short example later.

greetz

mh that's weird. I made this snipped and it works for me (Program doesn't show the error message).

.386
.model flat, stdcall
option casemap:none

include windows.inc

include user32.inc
include kernel32.inc
include ws2_32.inc

includelib user32.lib
includelib kernel32.lib
includelib ws2_32.lib

.const
addrinfo                    STRUCT
    ai_flags                DWORD ?
    ai_family               DWORD ?
    ai_socktype             DWORD ?
    ai_protocol             DWORD ?
    ai_addrlen              DWORD ?
    ai_canonname            DWORD ?
    ai_addr                 DWORD ?
    ai_next                 DWORD ?
addrinfo                    ENDS
ip		BYTE		"localhost",0
error	BYTE		"Error.",0

.data?
.data
ai		addrinfo 	<0>
pai		addrinfo	<0>
buf		BYTE		100 dup(0)
wsa		WSADATA		<0>

.code
main:
	invoke WSAStartup, 0202h, addr wsa
	
	mov ai.ai_family, AF_INET6
	mov ai.ai_socktype, SOCK_STREAM
	mov ai.ai_protocol, IPPROTO_TCP
	
	invoke getaddrinfo, addr ip, 0, addr ai, addr pai
	test eax, eax
	jz @END
	invoke MessageBox, 0, addr error, 0, 0
@END:
	invoke WSACleanup
	invoke ExitProcess, 0
	
end main

 

  • Like 1

Share this post


Link to post
Share on other sites
LCF-AT

Hi Castor,

you do use localhost as IP instead to use a IP6 ip address.For example you can use this 2a01:7e00:1::d447:ec1c

Below you can see site name / IPv4 & IPv6

networkfaculty.com
212.71.236.28
2a01:7e00:1::d447:ec1c

So in my case I only have the IPv6 address and now I need to get IPv4 address or Name just having the IPv6 address.Just try this so maybe you can get it work anyhow.

greetz

Share this post


Link to post
Share on other sites
Downpour

Ok so I've sat down and made a small application for this in C++.

#include <winsock2.h>
#include <WS2tcpip.h>
#include <iostream>

#pragma comment(lib, "ws2_32.lib")

const char* address = "lifeinhex.com";

int main(int argc, char* argv[]) {
    WSAData dat;
    WSAStartup(MAKEWORD(2, 2), &dat);

    char szIPv6[INET6_ADDRSTRLEN] = "Unknown";
    char szIPv4[INET_ADDRSTRLEN] = "Unknown";

    struct addrinfo ai6, ai4, *ai6_out, *ai4_out;
    memset(&ai6, 0, sizeof(addrinfo));
    memset(&ai4, 0, sizeof(addrinfo));

    ai6.ai_family = AF_INET6;
    ai6.ai_socktype = SOCK_STREAM;

    ai4.ai_family = AF_INET;
    ai4.ai_socktype = SOCK_STREAM;

    if (!getaddrinfo(address, 0, &ai6, &ai6_out)) {
        sockaddr_in6* s6 = reinterpret_cast<sockaddr_in6*>(ai6_out->ai_addr);
        inet_ntop(AF_INET6, &s6->sin6_addr, szIPv6, INET6_ADDRSTRLEN);
    }
    if (!getaddrinfo(address, 0, &ai4, &ai4_out)) {
        sockaddr_in* s4 = reinterpret_cast<sockaddr_in*>(ai4_out->ai_addr);
        memcpy(szIPv4, inet_ntoa(s4->sin_addr), INET_ADDRSTRLEN);
    }

    std::cout << "Address: " << address << "\nIPv6: " << szIPv6 << "\nIPv4: " << szIPv4 << std::endl;

    freeaddrinfo(ai6_out);
    freeaddrinfo(ai4_out);

    WSACleanup();

    std::cin.get();

    return 0;
}

I highly recommend reading kao's post and if you want to do even more things with networking to read into protocols and such, just to get a basic understanding of what you're doing so you won't face basic obstacles on your way.

I just tested that snippet for the posted hosts so I don't know how it will work for others (atleast for those posted I get the correct result). Another recommendation from me is to use a different language than Assembler for such things (Rather use C/C++ or scripting languages like Python).

  • Like 1

Share this post


Link to post
Share on other sites
LCF-AT

Hi again,

const char* address = "lifeinhex.com";

so I told you before that I dont have the domain name and only the IPv6 address and I want to get the domain name or IPv4 address anyhow just having IPv6 address.So is it possible now or not?

greetz

Share this post


Link to post
Share on other sites
LCF-AT

Hi,

so it means I cant get host name or IPv4 from IPv6 address. :( So why dont you guys say that straight. :) In this case I need to disable IPv6 in  my Network card to get IPv4 but exactly this I tried to prevent.Man man man man man you!Anyway,so thanks again so far for trying to help guys.

greetz

Share this post


Link to post
Share on other sites
secursig

I think your fundamental problem is assuming there is always a given v4 address for a v6 address or vice versa. There's no guarantee there are 2 addresses for a given host (1x 32 bit v4 address and 1x 128bit v6 address). Like kao says, they're two completely separate protocols. Most sites that are out there that are live in v4 land don't even have a v6 address because their networking team doesn't understand v6, have v6 upstream connectivity ( you have to be able to have a provider that can route you v6 netblocks/subnets and provide gateway functionality), or haven't v6 enabled their network because they haven't been forced to yet since most of the world is still using v4. 

There's nothing that says kao's workaround by converting a prefix to a PTR and then resolving to an A or quad A (AAAA for 6) has to work. In fact, most times you do this, you won't even be connecting to the same actual server/host/load balancer/cdn or whatever. if I wanted to, I could register a domain name and create records for A (v4) and records for AAAA (v6) that go to 2 completely separate servers in different parts of the world, and have separate things on them depending on which way you connect to them...v4 or v6. note that in windows 7 and other operating systems, if a domain is available and resolves in both v4 and v6, the v6 address will be preferred unless you specifically override it in the route table. sometimes this is not ideal if your v6 connection sucks compared to your v4 connection. see this: 

https://superuser.com/questions/436574/ipv4-vs-ipv6-priority-in-windows-7

 

the other issue you're going to have is very seldom does a single v4 address represent a single host anymore, due to the massive shortage of v4 space on the internet ( this was one of the major drivers of the push for v6 amongst service providers worldwide). We're simply out of space and have no more room for anyone else. That combined with the nightmare of managing large RIBS / route tables in carrier routers increases the urgency to establish your online presence with v6. The most common example of this is getting an internet connection from your ISP ( cable modem, DSL, etc ) and sharing it with your whole house with wifi. This is a single v4 address assigned to you that is shared among everyone that's using your wifi. So if it's your tablet, phone, or computer, as far as the remote server you're connecting to is concerned, you're all the same. NAT is what allows this. When you switch to v6, you no longer need nat because we have enough addresses for each device to be able to connect directly without sharing. With v6, Interestingly enough, since your true identity as a device connecting never technically has to change as it did with v4, this also has some interesting privacy considerations since no matter where you go, traveling with your laptop worldwide, you could still use the same address and thus you are globally trackable.

Here in Germany with Kabel Deutschland and in many other places you actually do not even get a v4 address on your modem, you get a v6 address. Since most websites can't be reached on the v6 protocol, your provider has to do CGNAT ( or carrier grade NAT ) and convert your traffic from v6 to v4 by putting your packets on a shared v4 address that might be shared with many other users ( neighbors or other customers for the cable company ). If i'm an administrator for a server and I look at the incoming traffic source address, I will see many connections from the same address, but the reality is the users might be connecting from distinct v6 addresses that are hidden from me as the server administrator since the service provider NAT translation completely masquerades the true source address of the traffic. As the server sending response traffic back to the origin IP that made the connection ( the CGNAT appliance v4 address from the server's perspective), the appliance keeps track of every TCP conversation and knows which hidden ipv6 address to reverse translate the TCP flow traffic back to when the server replies.

Sorry this all sounds very confusing, but as kao says, this stuff requires a little bit of an in depth understanding of how networking protocols work. I guess what I am trying to say is you simply just can't expect to have the ability to convert from v4 to v6 or vice versa. You might get lucky and find some domains that have both but usually you won't.

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...