How to convert ip6 to ip4 or name?

Recommended Posts

LCF-AT 2,302

LCF-AT
2,302

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.

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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?

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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

roocoon 5

roocoon
5

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

2

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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.

Share this post

Link to post

kao 1,836

kao
1,836

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).

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.

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.

4

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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.

Share this post

Link to post

Downpour 18

Downpour
18

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.

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_addrlen, ai_canonname, ai_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.

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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.

Share this post

Link to post

Downpour 18

Downpour
18

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).

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).

Share this post

Link to post

LCF-AT 2,302

LCF-AT
2,302

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

secursig 19

secursig
19

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:

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.