-1

in my project I need to reverse IPv6 IP address for future DNS lookup.

Example:

From: 2001:db8:85a3:0:0:8a2e:370:7334

To: 7334:370:8a2e:0:0:85a3:db8:2001

How can I do this with c++? Thanks for your answers :)

PS: For IPv4 I´m using this solution:

        int a,b,c,d;
        char ip2[32];
        sscanf(pageAddress, "%d.%d.%d.%d", &a, &b, &c, &d);
        sprintf(ip2, "%d.%d.%d.%d", d, c, b, a);
        std::cout << ip2 << "\n";

Is there any better solution?

  • 3
    That is NOT what reverse DNS lookup does. Not is it standard practice to reverse an IP address for future DNS lookup. "Reverse DNS" is taking an IP address and finding out what hostname it maps to. Example: looking up 151.101.1.69 and finding that it maps to www.stackoverflow.com. **What are you really trying to do?** – selbie Nov 12 '19 at 20:33
  • i ´m trying to get some information from argument, that user passed to program (ipv4 address, ipv6 address, hostname). Teacher told me, that i can reverse ip addreess and do something like this: **5.2.0.192.in-addr.arpa**. Then use **res_query** function to get information (ptr, mx, soa, a, aaaa, ...) – Ondra Studnička Nov 12 '19 at 20:37
  • Besides @selbie important comment, note that an IPv6 address can be written in multiple ways, so before doing anything with it you will need to normalize it to some common format that you need for later operations. See RFC5952 for a start on this. – Patrick Mevzek Nov 12 '19 at 20:37
  • 1
    `in-addr.arpa` is specific to IPv4, won't work for IPv6. Look at https://tools.ietf.org/html/rfc3596#section-2.5 – Patrick Mevzek Nov 12 '19 at 20:38

1 Answers1

0

I see that this is not exactly what you need, but I just wanted to answer what you asked and not what you need!

I've written a whole classes for ipv4 and ipv6 a few months ago. Trust me, you do not wanna do ipv6 manipulation in a string format. You can put ipv4 in a ipv6; so it's not that easy; specially with all the rules about short format and stuff like that.

I think there are still some unused codes in it too, but you can clean it up easily.

There's also ipv4 version of the reversed method in the ipv4 class too. And also some ipv4 and ipv6 validation and subnet mask validations as well.

Here: https://godbolt.org/z/S9SEDh

I would've put the code here, but It's too long.

The Moisrex
  • 1,857
  • 1
  • 14
  • 16