简体   繁体   English

试图从十六进制(基数16)传递到dec到ip6 / proc / net / tcp6

[英]trying to pass from hex(base 16) to dec to ip6 /proc/net/tcp6

im reading the content of the file /proc/net/tcp6 我正在读取文件/ proc / net / tcp6的内容

and trying to transform that notation of ip6 into a '0::1' like 并尝试将ip6的表示法转换为“ 0 :: 1”,例如

previously with ipv4 y use the next method. 以前使用ipv4,请使用下一种方法。

struct sockaddr_in tmp_ip;
char ip_str[30];
char ipex[]='00000AF0'; /*read from the file /proc/net/tcp */
tmp_ip.sin_addr.s_addr=(int)strtoll(ipex,NULL,16);
inet_ntop(AF_INET,&tmp_ip.sin_addr,ip_str,60);
printf("ip=%s \n",ip_str);

but with ipv6 the content of /proc/net/tcp6 its bigger(33 hex chars) and maybe i need to use sockaddr_in6, but the variable sin6_addr.s6_addr is a array, not a single log unsigned int (like sin_addr.s_addr) 但是对于ipv6,/ proc / net / tcp6的内容更大(33个十六进制字符),也许我需要使用sockaddr_in6,但是变量sin6_addr.s6_addr是一个数组,而不是一个未签名的日志int(例如sin_addr.s_addr)

so in resume. 所以在简历中。 i trying to pass this 我试图通过这个

0000000000000000FFFF00001F00C80A

to something like

::ffff:10.200.0.31

edit.. 编辑..

mmm maybe if i decompose that ex into 16 ex digits and feed the array in sin6_addr.s_addr. 嗯,如果我将那个ex分解成16个ex位数,并将数组输入sin6_addr.s_addr。 Because 1F00C80A = 10.200.0.31(passing throught ntop function) 因为1F00C80A = 10.200.0.31(通过ntop功能)

You can use sscanf() to directly convert the string into the elements of the s6_addr array: 您可以使用sscanf()将字符串直接转换为s6_addr数组的元素:

struct in6_addr tmp_ip;
char ip_str[128];
char ipex[]="0000000000000000FFFF00001F00C80A";

if (sscanf(ipex,
    "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
    &tmp_ip.s6_addr[3], &tmp_ip.s6_addr[2], &tmp_ip.s6_addr[1], &tmp_ip.s6_addr[0],
    &tmp_ip.s6_addr[7], &tmp_ip.s6_addr[6], &tmp_ip.s6_addr[5], &tmp_ip.s6_addr[4],
    &tmp_ip.s6_addr[11], &tmp_ip.s6_addr[10], &tmp_ip.s6_addr[9], &tmp_ip.s6_addr[8],
    &tmp_ip.s6_addr[15], &tmp_ip.s6_addr[14], &tmp_ip.s6_addr[13], &tmp_ip.s6_addr[12]) == 16)
{
    inet_ntop(AF_INET6, &tmp_ip, ip_str, sizeof ip_str);
    printf("ip=%s \n",ip_str);
}

thanks. 谢谢。 i ended doing this. 我结束了这个。

cont_ip6=0; 
        cont=0;
        for(i=0;i<34;i++) {
                if (cont ==2) {
                        cont=0;
                        hex_section[2]='\0';
                                tmp_ip6.sin6_addr.s6_addr[cont_ip6]=strtol(hex_section,NULL,16);

                        cont_ip6++;
                }

                hex_section[cont]=ipex[i];
                cont++;

        }

then inet_ntop 然后是inet_ntop

i fixed it. 我修好了它。

you need to invert every pair of hex numbers. 您需要反转每对十六进制数字。

::FFFF:10.200.0.31 ended in the array like this. :: FFFF:10.200.0.31在这样的数组中结束。

(last elements) (最后一个元素)

FF     |FF    |   00   | 00  :  0A | C8   |   00 | 1F
255    |255   |   0    | 0   :  10 | 200  |   0  | 31

^       ^         ^      ^   :  ^     ^       ^     ^
|       |         |      |      |     |       |     |
|        ---------       |      |      -------      |
 ------------------------        -------------------

so you need to swap these (in each set of numbers) 所以您需要交换它们(在每组数字中)

so i do this 所以我这样做

tmptmp=0;
for (i=0;i<5;i++){
    tmptmp=tmp_ip6.sin6_addr.s6_addr[i*4+3];
    tmp_ip6.sin6_addr.s6_addr[i*4+3]=tmp_ip6.sin6_addr.s6_addr[i*4];
    tmp_ip6.sin6_addr.s6_addr[i*4]=tmptmp;

    tmptmp=tmp_ip6.sin6_addr.s6_addr[i*4+2];
    tmp_ip6.sin6_addr.s6_addr[i*4+2]=tmp_ip6.sin6_addr.s6_addr[i*4+1];
    tmp_ip6.sin6_addr.s6_addr[i*4+1]=tmptmp;
 }

this swap the sets and when you do a inet_ntop it shows ::FFFF:10.200.0.31 这交换了集,当您执行inet_ntop时,它显示:: FFFF:10.200.0.31

(i was looking for this all the day D:, my head hurts) (sorry for my bad english) (我整天都在找这个D :,我的头很痛)(对不起我的英语不好)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM