-4

I'm trying to convert IPv4 into IPv6 using some conversion to uint8_t. I know that IPv4 has 4 bytes, and IPv6 16 unsigned ints of 2 bytes, but I can't find the way to do they conversion.

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
     uint8_t ipv6[16],direccionIP[] = {193, 110, 128, 200};
     ipv6 = &direccionIP; //this doesn't work, and I don't come up with any method
     printf("%u", ipv6);
     return 0;
}
Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
  • You cannot fit a pint in a quart pot – Ed Heal Sep 27 '16 at 21:01
  • 3
    What, specifically do you mean by convert an IPv4 into IPv6? The addressing is not compatible, and your idea of what an IPv6 address is seems to be flawed. An IPv4 address is a 32-bit integer, and an IPv6 address is a 128-bit integer. – Ron Maupin Sep 27 '16 at 21:01

2 Answers2

1

As others have said, there is no direct mapping to/from ipv4/ipv6. However, there are numerous ways of mapping to/from ipv4/ipv6 , such as 6 to 4, teredo, and others.

For most, the converted IPv4 address is placed in the lower 4 bytes of the 16 byte IPv6 address, and the remainder of the IPv6 address has a series of bytes indicating the mapping.

Sean F
  • 4,344
  • 16
  • 30
0

IPv4 and IPv6 are different protocols with different addresses. In general you cannot convert between IPv4 and IPv6 addresses.

Sander Steffann
  • 9,509
  • 35
  • 40
  • There is a range reserved for IPv4 addresses inside the IPv6 space. – allo Sep 28 '16 at 19:54
  • That's why I specified "in general". What you are talking about is for applications that use an IPv6 socket to handle IPv4 connection. That "trick" makes it possible in some cases to develop an application with only one server socket instead of separate ones for IPv4 and IPv6. For that there is a way to represent an IPv4 address in IPv6 format. But that's just software trickery, the network packets are IPv4 in that case. When communicating with IPv6 packets there is no such mapping. (I'm intentionally leaving other tricks like NAT64 out to avoid further confusion with special cases) – Sander Steffann Sep 28 '16 at 21:51