tags:

views:

58

answers:

2

Hi,

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)

A: 

You can use sscanf() to directly convert the string into the elements of the s6_addr array:

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);
}
caf
i got ::ffff:0:1f00:c80a decomposing the last eight hex numbers. 1f=3100=0c8=2000a=10the inverse ip. i need to do something else for inet_ntop returns '::ffff:10.200.0.31'(ip4 mask)
Freaktor
Freaktor
A: 

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)

Freaktor
still geting ::ffff:0:1f00:c80a and not ::ffff:10.200.0.31may i need to pass the last hex set via inet_ntop AF_INET (not AF_INET6) when i get a previous one with 'ffff'?
Freaktor