Search code examples
cipv6ipv4netstat

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


im reading the content of the file /proc/net/tcp6

and trying to transform that notation of ip6 into a '0::1' like

previously with ipv4 y use the next method.

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)

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. Because 1F00C80A = 10.200.0.31(passing throught ntop function)


Solution

  • 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

    i fixed it.

    you need to invert every pair of hex numbers.

    ::FFFF:10.200.0.31 ended in the array like this.

    (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

    (i was looking for this all the day D:, my head hurts) (sorry for my bad english)