Advertisement
Guest User

Untitled

a guest
Apr 16th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <string.h>
  2. #include <assert.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6.  
  7. #include <sys/ioctl.h>
  8. #include <net/if.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>
  11.  
  12. int main(int argc, const char *argv[])
  13. {
  14.     #define _IPV6_CUSTOM_FILE "/var/if_inet6"
  15.     #define _IPV6_RESOURCE_FILE "/proc/net/if_inet6"
  16.     FILE *fp = NULL;
  17.     char address[8][5];
  18.     unsigned int if_idx = 0, plen = 0, scope = 0, dad_status = 0;
  19.     char iface[32] = {0};
  20.     unsigned int tmp = 0;
  21.     struct in6_addr ipv6addr = in6addr_any;
  22.     char txt_ipv6_address[130];
  23.  
  24.     memset((char *)address, 0x00, sizeof(address));
  25.     fp = fopen(_IPV6_CUSTOM_FILE, "r");
  26.     if (NULL == fp) {
  27.         printf("fopen %s failure.\n", _IPV6_CUSTOM_FILE);
  28.             fp = fopen(_IPV6_RESOURCE_FILE, "r");
  29.             if (NULL == fp) {
  30.                     printf("fopen %s failure.\n", _IPV6_RESOURCE_FILE);
  31.                     return NULL;
  32.             }
  33.  
  34.     }
  35.     while (EOF != fscanf(fp, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
  36.         address[0], address[1], address[2], address[3],
  37.         address[4], address[5], address[6], address[7],
  38.         &if_idx, &plen, &scope, &dad_status, iface))
  39.     {
  40.         int i;
  41.         for (i = 0; i < 8; ++i) {
  42.             sscanf(address[i], "%4x", &tmp);
  43.             ipv6addr.s6_addr16[i] = htons((unsigned short int)tmp);
  44.         }
  45.         if (!(IN6_IS_ADDR_LOOPBACK(ipv6addr.s6_addr32)
  46.             || IN6_IS_ADDR_UNSPECIFIED(ipv6addr.s6_addr32)
  47.             || IN6_IS_ADDR_LINKLOCAL(ipv6addr.s6_addr32))){
  48.             inet_ntop(AF_INET6, &ipv6addr, txt_ipv6_address, INET6_ADDRSTRLEN);
  49.             //break;
  50.         }
  51.     }
  52.     fclose(fp);
  53.     printf( txt_ipv6_address );
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement