Chinaunix首页 | 论坛 | 博客
  • 博客访问: 417693
  • 博文数量: 73
  • 博客积分: 3326
  • 博客等级: 中校
  • 技术积分: 631
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-05 15:31
文章分类

全部博文(73)

文章存档

2014年(1)

2011年(51)

2010年(21)

分类: C/C++

2011-08-02 15:58:39

  1. void get_ipv6addr_prefix_content(char *ipv6addr,char *prefix)
  2. {
  3.     struct in6_addr ipv6;
  4.     char ipv6_str_r[INET6_ADDRSTRLEN]={0};
  5.     char *p1,*p2;
  6.     int prefix_num;
  7.     char ipaddr_tmp[sizeof(struct in6_addr)];//tmp addr
  8.     int i=0;

  9.     //find '/' and process prefix num
  10.     if(NULL == (p1 = strchr(ipv6addr,'/')))
  11.     {
  12.         printf("IPv6 Address without Prefix!\n");
  13.         exit(1);
  14.     }
  15.     else
  16.     {
  17.         printf("IPv6 Address with Prefix!\n");
  18.         p2 = p1+1;
  19.         prefix_num = atoi(p2);

  20.         char *tmp = ipv6addr;
  21.         static int k = 0;
  22.         while(tmp != p1)
  23.         {
  24.             ipaddr_tmp[k] = *tmp;
  25.             tmp++;
  26.             k++;
  27.         }
  28.         ipaddr_tmp[k-1] = '\0';
  29.     }
  30.     
  31.     u_int8_t prefix_data[prefix_num/8];
  32.     
  33.     printf("ipv6address:%s\n", ipv6addr);
  34.     //string to binary
  35.     if (inet_pton(AF_INET6,ipaddr_tmp,&ipv6) <= 0)
  36.     {
  37.         printf("inet_pton failed.");
  38.         exit(1);
  39.     }
  40.     //printf ipv6addr data
  41.     for(;i<16;i++)
  42.     {
  43.         printf("ipv6.s6_addr[%d]:%x\n",i,ipv6.s6_addr[i]);
  44.     }
  45.     printf("=======================================");

  46.     /* process prefix */    
  47.     if((prefix_num%2) == 0)//prefix num is even num
  48.     {
  49.         printf("\nThe prefix data\n");
  50.         
  51.         for(i = 0;i < ((prefix_num)/8);i ++)
  52.         {
  53.             prefix_data[i] = ipv6.s6_addr[i];
  54.             printf("%x\n",prefix_data[i]);
  55.         }    
  56.         
  57.         //tail content set to 0x0
  58.         int j = prefix_num/8;
  59.         while(j<16)
  60.         {
  61.                 prefix_data[j] = 0x0;
  62.                 j++;
  63.         }
  64.     }
  65.     else //prefix num is odd num
  66.     {
  67.         //to be fixed.
  68.         exit(1);
  69.     }
  70.     printf("=======================================\n");
  71.     
  72.     //hex to string
  73.     if ( !inet_ntop(AF_INET6, prefix_data,ipv6_str_r,sizeof(ipv6_str_r)) )
  74.     {
  75.         printf("inet_ntop failed.");
  76.         exit(1);
  77.     }
  78.     printf("Get Prefix##_%s\n", ipv6_str_r);

  79.     strcpy(prefix,ipv6_str_r);
  80.     
  81. }
阅读(1523) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~