Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <assert.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- int main(int argc, const char *argv[])
- {
- #define _IPV6_CUSTOM_FILE "/var/if_inet6"
- #define _IPV6_RESOURCE_FILE "/proc/net/if_inet6"
- FILE *fp = NULL;
- char address[8][5];
- unsigned int if_idx = 0, plen = 0, scope = 0, dad_status = 0;
- char iface[32] = {0};
- unsigned int tmp = 0;
- struct in6_addr ipv6addr = in6addr_any;
- char txt_ipv6_address[130];
- memset((char *)address, 0x00, sizeof(address));
- fp = fopen(_IPV6_CUSTOM_FILE, "r");
- if (NULL == fp) {
- printf("fopen %s failure.\n", _IPV6_CUSTOM_FILE);
- fp = fopen(_IPV6_RESOURCE_FILE, "r");
- if (NULL == fp) {
- printf("fopen %s failure.\n", _IPV6_RESOURCE_FILE);
- return NULL;
- }
- }
- while (EOF != fscanf(fp, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
- address[0], address[1], address[2], address[3],
- address[4], address[5], address[6], address[7],
- &if_idx, &plen, &scope, &dad_status, iface))
- {
- int i;
- for (i = 0; i < 8; ++i) {
- sscanf(address[i], "%4x", &tmp);
- ipv6addr.s6_addr16[i] = htons((unsigned short int)tmp);
- }
- if (!(IN6_IS_ADDR_LOOPBACK(ipv6addr.s6_addr32)
- || IN6_IS_ADDR_UNSPECIFIED(ipv6addr.s6_addr32)
- || IN6_IS_ADDR_LINKLOCAL(ipv6addr.s6_addr32))){
- inet_ntop(AF_INET6, &ipv6addr, txt_ipv6_address, INET6_ADDRSTRLEN);
- //break;
- }
- }
- fclose(fp);
- printf( txt_ipv6_address );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement