RFC 5952 converter tool

Nick Hilliard nick at foobar.org
Wed Nov 27 23:54:35 CET 2013


On 27/11/2013 22:28, Nick Hilliard wrote:
> inet_ntop(3) is the canonical function for this.  Make sure your byte order
> is correct.

should be more helpful here: see the attached code.  usage:

> pancake:/home/nick> gcc -o convert convert.c
> pancake:/home/nick> ./convert 2001:0DB8::0001
> 2001:db8::1
> pancake:/home/nick> ./convert 2001:0DB8::192.168.1.1
> 2001:db8::c0a8:101
> pancake:/home/nick>

Both inet_pton() and inet_ntop() needs return value checking, but otherwise
you should get the idea.

Nick

-------------- next part --------------
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>

int main(int argc, char **argv)
{
  char *addr;
  char buffer[INET6_ADDRSTRLEN+1];
  struct sockaddr_in6 sa;

  if (argc == 2) {
    addr = argv[1];
  } else {
    addr = "2001:7f8:0:0::1:1";
  }

  inet_pton (AF_INET6, addr, &(sa.sin6_addr));
  inet_ntop (AF_INET6, &(sa.sin6_addr), buffer, INET6_ADDRSTRLEN);

  printf("%s\n", buffer);
}


More information about the ipv6-ops mailing list