Skip to content

Instantly share code, notes, and snippets.

@run
Last active October 29, 2015 16:41
Show Gist options
  • Save run/bc7192849b9a255c0633 to your computer and use it in GitHub Desktop.
Save run/bc7192849b9a255c0633 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <arpa/inet.h>
typedef uint32_t ipv4_addr_t;
typedef struct ipv6_addr_s ipv6_addr_t;
typedef union ipx_addr_s ipx_addr_t;
struct ipv6_addr_s {
union {
uint64_t u64[2];
uint32_t u32[4];
uint16_t u16[8];
uint8_t u8[16];
} u;
} __attribute__ ((__packed__));
union ipx_addr_s {
struct {
uint64_t unused0;
uint32_t unused1;
ipv4_addr_t ip;
};
ipv6_addr_t ip6;
} __attribute__ ((__packed__));
int main(int argc, char **argv)
{
ipv4_addr_t v4;
ipv6_addr_t v6;
char buf[INET6_ADDRSTRLEN];
ipx_addr_t *addr;
addr = (ipx_addr_t *)malloc(sizeof(ipx_addr_t));
if (inet_pton(AF_INET6, argv[1], &addr->ip6) != 1) {
printf("ERROR: Invalid IP: %s\n", argv[1]);
}
if (inet_ntop(AF_INET6, &addr->ip6, buf, INET6_ADDRSTRLEN) == NULL) {
printf("ERROR: %s\n", strerror(errno));
}
printf("IP address : %s\n", buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment