Click here to Skip to main content
13,052,641 members (64,852 online)
Rate this:
 
Please Sign up or sign in to vote.
i have the following code from some website.i am not able to understand following things:-
1>why they have used so many in_addr and in6_addr.
2>Why they have used the in6_addr twice.
3>I am also not able to understand the format specifier used in sscanf.
4>Final problem why they have written so long code in case of tcp6 and udp6.
If someone can explain all the above problems i would be very thank full to him

void net_dump()
{
	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN::net_dump");
    char buf[BUFFERSIZE*2];
 
    struct in_addr laddr, raddr;
    struct in6_addr laddr6, raddr6;
    struct in6_addr swapladdr6, swapraddr6;
    unsigned lport, rport, state, uid;//, txq, rxq, num;
    int n;
    FILE *fp,*p;
   /* char file1[200]="C:\\NEWTCP.txt";
    p=fopen(file1,"a+");
    if(p==NULL)
    {
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","cannot open %s",file1);
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","cannot open file error %s", strerror(errno));
  	  printf("cannot open %s",file1);
  	 //exit(0);
    }*/
    sprintf(buf, "/proc/%d/net/tcp", getpid());
  	fp = fopen(buf, "r");
 
    if(fp != 0)
    {
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN TCP MODE above fgets buffer is %s",buf);
        fgets(buf, BUFFERSIZE*2, fp);
        while(fgets(buf, BUFFERSIZE*2, fp))
        {
        	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN TCP MODE buffer is %s",buf);
/*Here value in buf is IN TCP MODE buffer is    0: 0100007F:13AD 00000000:0000 0A 00000000:00000000 00:00000000 00000000     0        0 300 1 c7c25900 300 0 0 2 -1  */                             
            n = sscanf(buf, " %*d: %x:%x %x:%x %x %*x:%*x %*x:%*x %*x %d",
                       &laddr.s_addr, &lport, &raddr.s_addr, &rport,
                       &state, &uid);
 
            if(n == 6)
				net_list_add("TCP", &laddr, lport, &raddr, rport, state, uid, 0);
        }
        fclose(fp);
    }
 
    snprintf(buf, BUFFERSIZE, "/proc/%d/net/tcp6", getpid());
  	fp = fopen(buf, "r");
    if(fp != 0)
    {
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN TCP6 MODE above fgets buffer is %s",buf);
        fgets(buf, BUFFERSIZE*2, fp);
        while(fgets(buf, BUFFERSIZE*2, fp))
        {
        	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN TCP6 MODE buffer is %s",buf);
            n = sscanf(buf, " %*d: %08X%08X%08X%08X:%x %08X%08X%08X%08X:%x %x %*x:%*x %*x:%*x %*x %d",
                       &laddr6.in6_u.u6_addr32[0], &laddr6.in6_u.u6_addr32[1],
                       &laddr6.in6_u.u6_addr32[2], &laddr6.in6_u.u6_addr32[3],
                       &lport,
                       &raddr6.in6_u.u6_addr32[0], &raddr6.in6_u.u6_addr32[1],
                       &raddr6.in6_u.u6_addr32[2], &raddr6.in6_u.u6_addr32[3],
                       &rport, &state, &uid);
 
   			swapladdr6.in6_u.u6_addr8[0] = laddr6.in6_u.u6_addr8[3];
   			swapladdr6.in6_u.u6_addr8[1] = laddr6.in6_u.u6_addr8[2];
   			swapladdr6.in6_u.u6_addr8[2] = laddr6.in6_u.u6_addr8[1];
   			swapladdr6.in6_u.u6_addr8[3] = laddr6.in6_u.u6_addr8[0];
 
            swapladdr6.in6_u.u6_addr8[4] = laddr6.in6_u.u6_addr8[7];
   			swapladdr6.in6_u.u6_addr8[5] = laddr6.in6_u.u6_addr8[6];
   			swapladdr6.in6_u.u6_addr8[6] = laddr6.in6_u.u6_addr8[5];
   			swapladdr6.in6_u.u6_addr8[7] = laddr6.in6_u.u6_addr8[4];
 
   			swapladdr6.in6_u.u6_addr8[8] = laddr6.in6_u.u6_addr8[11];
   			swapladdr6.in6_u.u6_addr8[9] = laddr6.in6_u.u6_addr8[10];
   			swapladdr6.in6_u.u6_addr8[10] = laddr6.in6_u.u6_addr8[9];
   			swapladdr6.in6_u.u6_addr8[11] = laddr6.in6_u.u6_addr8[8];
 
   			swapladdr6.in6_u.u6_addr8[12] = laddr6.in6_u.u6_addr8[15];
   			swapladdr6.in6_u.u6_addr8[13] = laddr6.in6_u.u6_addr8[14];
   			swapladdr6.in6_u.u6_addr8[14] = laddr6.in6_u.u6_addr8[13];
   			swapladdr6.in6_u.u6_addr8[15] = laddr6.in6_u.u6_addr8[12];
 

   			swapraddr6.in6_u.u6_addr8[0] = raddr6.in6_u.u6_addr8[3];
   			swapraddr6.in6_u.u6_addr8[1] = raddr6.in6_u.u6_addr8[2];
   			swapraddr6.in6_u.u6_addr8[2] = raddr6.in6_u.u6_addr8[1];
   			swapraddr6.in6_u.u6_addr8[3] = raddr6.in6_u.u6_addr8[0];
 
   			swapraddr6.in6_u.u6_addr8[4] = raddr6.in6_u.u6_addr8[7];
   			swapraddr6.in6_u.u6_addr8[5] = raddr6.in6_u.u6_addr8[6];
   			swapraddr6.in6_u.u6_addr8[6] = raddr6.in6_u.u6_addr8[5];
   			swapraddr6.in6_u.u6_addr8[7] = raddr6.in6_u.u6_addr8[4];
 
   			swapraddr6.in6_u.u6_addr8[8] = raddr6.in6_u.u6_addr8[11];
   			swapraddr6.in6_u.u6_addr8[9] = raddr6.in6_u.u6_addr8[10];
   			swapraddr6.in6_u.u6_addr8[10] = raddr6.in6_u.u6_addr8[9];
   			swapraddr6.in6_u.u6_addr8[11] = raddr6.in6_u.u6_addr8[8];
 
   			swapraddr6.in6_u.u6_addr8[12] = raddr6.in6_u.u6_addr8[15];
   			swapraddr6.in6_u.u6_addr8[13] = raddr6.in6_u.u6_addr8[14];
   			swapraddr6.in6_u.u6_addr8[14] = raddr6.in6_u.u6_addr8[13];
   			swapraddr6.in6_u.u6_addr8[15] = raddr6.in6_u.u6_addr8[12];
 

            if(n == 12)
				net_list_add("TCP6", &swapladdr6, lport, &swapraddr6, rport, state, uid, 1);
        }
        fclose(fp);
    }
 
    snprintf(buf, BUFFERSIZE*2, "/proc/%d/net/udp", getpid());
    fp = fopen(buf, "r");
 
    if(fp != 0) {
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN UDP MODE above fgets buffer is %s",buf);
        fgets(buf, BUFFERSIZE*2, fp);
        while(fgets(buf, BUFFERSIZE*2, fp))
        {
        	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN UDP MODE buffer is %s",buf);
            n = sscanf(buf, " %*d: %x:%x %x:%x %x %*x:%*x %*x:%*x %*x %d",
                       &laddr.s_addr, &lport, &raddr.s_addr, &rport,
                       &state, &uid);
            if(n == 6)
  				net_list_add("UDP", &laddr, lport, &raddr, rport, state, uid, 0);
        }
        fclose(fp);
    }
 
    snprintf(buf, BUFFERSIZE*2, "/proc/%d/net/udp6", getpid());
  	fp = fopen(buf, "r");
    if(fp != 0)
    {
    	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN UDP6 MODE above fgets buffer is %s",buf);
        fgets(buf, BUFFERSIZE*2, fp);
        while(fgets(buf, BUFFERSIZE*2, fp))
        {
        	__android_log_print(ANDROID_LOG_INFO,"Tarun","IN UDP6 MODE buffer is %s",buf);
            n = sscanf(buf, " %*d: %08X%08X%08X%08X:%x %08X%08X%08X%08X:%x %x %*x:%*x %*x:%*x %*x %d",
                       &laddr6.in6_u.u6_addr32[0], &laddr6.in6_u.u6_addr32[1],
                       &laddr6.in6_u.u6_addr32[2], &laddr6.in6_u.u6_addr32[3],
                       &lport,
                       &raddr6.in6_u.u6_addr32[0], &raddr6.in6_u.u6_addr32[1],
                       &raddr6.in6_u.u6_addr32[2], &raddr6.in6_u.u6_addr32[3],
                       &rport, &state, &uid);
 
   			swapladdr6.in6_u.u6_addr8[0] = laddr6.in6_u.u6_addr8[3];
   			swapladdr6.in6_u.u6_addr8[1] = laddr6.in6_u.u6_addr8[2];
   			swapladdr6.in6_u.u6_addr8[2] = laddr6.in6_u.u6_addr8[1];
   			swapladdr6.in6_u.u6_addr8[3] = laddr6.in6_u.u6_addr8[0];
 
            swapladdr6.in6_u.u6_addr8[4] = laddr6.in6_u.u6_addr8[7];
   			swapladdr6.in6_u.u6_addr8[5] = laddr6.in6_u.u6_addr8[6];
   			swapladdr6.in6_u.u6_addr8[6] = laddr6.in6_u.u6_addr8[5];
   			swapladdr6.in6_u.u6_addr8[7] = laddr6.in6_u.u6_addr8[4];
 
   			swapladdr6.in6_u.u6_addr8[8] = laddr6.in6_u.u6_addr8[11];
   			swapladdr6.in6_u.u6_addr8[9] = laddr6.in6_u.u6_addr8[10];
   			swapladdr6.in6_u.u6_addr8[10] = laddr6.in6_u.u6_addr8[9];
   			swapladdr6.in6_u.u6_addr8[11] = laddr6.in6_u.u6_addr8[8];
 
   			swapladdr6.in6_u.u6_addr8[12] = laddr6.in6_u.u6_addr8[15];
   			swapladdr6.in6_u.u6_addr8[13] = laddr6.in6_u.u6_addr8[14];
   			swapladdr6.in6_u.u6_addr8[14] = laddr6.in6_u.u6_addr8[13];
   			swapladdr6.in6_u.u6_addr8[15] = laddr6.in6_u.u6_addr8[12];
 

   			swapraddr6.in6_u.u6_addr8[0] = raddr6.in6_u.u6_addr8[3];
   			swapraddr6.in6_u.u6_addr8[1] = raddr6.in6_u.u6_addr8[2];
   			swapraddr6.in6_u.u6_addr8[2] = raddr6.in6_u.u6_addr8[1];
   			swapraddr6.in6_u.u6_addr8[3] = raddr6.in6_u.u6_addr8[0];
 
   			swapraddr6.in6_u.u6_addr8[4] = raddr6.in6_u.u6_addr8[7];
   			swapraddr6.in6_u.u6_addr8[5] = raddr6.in6_u.u6_addr8[6];
   			swapraddr6.in6_u.u6_addr8[6] = raddr6.in6_u.u6_addr8[5];
   			swapraddr6.in6_u.u6_addr8[7] = raddr6.in6_u.u6_addr8[4];
 
   			swapraddr6.in6_u.u6_addr8[8] = raddr6.in6_u.u6_addr8[11];
   			swapraddr6.in6_u.u6_addr8[9] = raddr6.in6_u.u6_addr8[10];
   			swapraddr6.in6_u.u6_addr8[10] = raddr6.in6_u.u6_addr8[9];
   			swapraddr6.in6_u.u6_addr8[11] = raddr6.in6_u.u6_addr8[8];
 
   			swapraddr6.in6_u.u6_addr8[12] = raddr6.in6_u.u6_addr8[15];
   			swapraddr6.in6_u.u6_addr8[13] = raddr6.in6_u.u6_addr8[14];
   			swapraddr6.in6_u.u6_addr8[14] = raddr6.in6_u.u6_addr8[13];
   			swapraddr6.in6_u.u6_addr8[15] = raddr6.in6_u.u6_addr8[12];
 
   			if(n == 12)
				net_list_add("UDP6", &swapladdr6, lport, &swapraddr6, rport, state, uid, 1);
        }
        fclose(fp);
    }
__android_log_print(ANDROID_LOG_INFO,"Tarun","OUT::net_dump");
    return;
}
Posted 8-Aug-12 2:01am
Tarun22557
Comments
Kenneth Haugland 8-Aug-12 8:03am
   
Not to be rude to you, but there are not many of us that want to understand that much code :) Were are you stuck, and perhaps you'll get an answer quicker? ;)
Tarun22 8-Aug-12 8:05am
   
Hey i have written my four ques on the top
1>why they have used so many in_addr and in6_addr.
2>Why they have used the in6_addr twice.
3>I am also not able to understand the format specifier used in sscanf.
4>Final problem why they have written so long code in case of tcp6 and udp6.
Philip Stuyck 8-Aug-12 8:20am
   
do you know anything about ipv6 ?
The sscanf format specifier is clearly to get some ipv6 ip addresses.
They don't have 2 in6_addr but 4 variables of that type. l identifies a local address and r a remote address. The swap address is used to change the order of the local and remote ip adresses for whatever reason. Might be that the client computer is little endian and then swapping might be in order if an integer is put on top of this. Dependes on the behaviour of netlistadd.
Tarun22 8-Aug-12 8:23am
   
no i have no idea regarding ipv6 will google it or if u have any good link do pass me....or any info regarding it
Philip Stuyck 8-Aug-12 8:26am
   
If you don't know anything about ipv6 then i can imagine that this code is hard to understand.
Tarun22 8-Aug-12 8:32am
   
sir the whole code which starts with the following line
swapladdr6.in6_u.u6_addr8[0] = laddr6.in6_u.u6_addr8[3];
is used for swapping?
Philip Stuyck 8-Aug-12 8:48am
   
yes, and using a for next it can be done with a lot fewer code.
Tarun22 8-Aug-12 8:51am
   
How can using "next"?
Philip Stuyck 8-Aug-12 9:19am
   
for(int i=0;i<16;i++){
swapladdr6.in6_u.u6_addr8[i]=swapladdr6.in6_u.u6_addr8[(i/4)*4+3-i%4];
}
i/4*4 is not the same as i, since i is an integer dividing will yield an integer too and not take the remainder of the division into account.
Tarun22 8-Aug-12 9:28am
   
Nice solution sir i think u are an expert in C++,can i have your mail id so that i can contact u in case of my problems if u don't have problem.......
Tarun22 8-Aug-12 9:37am
   
one thing more sir can u explain the meaning of following specifier %*d: %x:%x %x:%x %x %*x:%*x %*x:%*x %*x %d"
Philip Stuyck 8-Aug-12 9:41am
   
d is for deximal x is for hex numbers. It identifies the kind of thing you want as input or output dpending on if it is scanf or printf. These things are called format specifiers and can be found on the web too. google is your friend. ;-)
Tarun22 8-Aug-12 9:47am
   
i know they are called format specifier sir but i am asking that in the following statement
n = sscanf(buf, " %*d: %x:%x %x:%x %x %*x:%*x %*x:%*x %*x %d",
&laddr.s_addr, &lport, &raddr.s_addr, &rport,
&state, &uid);
we have used 12 format specifier while the variables are only 6
Why so?
Philip Stuyck 8-Aug-12 10:17am
   
the ones with a * are there to confuse you(and me). %*d is not a proper format specifier. I'd have to try this out myself to see what would happen. Wrong format specifiers can lead to crashes. Whoever wrote this code is not really a strong programmer considering not using the for next I told you about earlier.
Consider stepping through the code with a debugger you can see what happens to all variables that way.
Tarun22 8-Aug-12 11:18am
   
sir the format specifier works well in the code and the ode is not even crashing
Philip Stuyck 8-Aug-12 11:40am
   
I am not saying it is. I am saying that if you make an error in such a specifier it can lead to a crash.
Tarun22 9-Aug-12 3:19am
   
ok sir,sir do u know how to debug the native code in android using eclipse,i searched the web but was not able to undersatnd
Philip Stuyck 9-Aug-12 3:23am
   
no sorry, post a new question for this. Someone else might help.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The sscanf format specifier is clearly to get some ipv6 ip addresses.
They don't have 2 in6_addr but 4 variables of that type. l identifies a local address and r a remote address. The swap address is used to change the order of the local and remote ip adresses for whatever reason. Might be that the client computer is little endian and then swapping might be in order if an integer is put on top of this. Dependes on the behaviour of netlistadd.
  Permalink  
Comments
   
I guess it would be enough to direct OP. The OP's approach is not very fruitful, so is the question. It's much better to learn IPv6 addressing and related topics from documentation and try to solve some problems with one's own hands, instead of looking into... who knows what.
My 5.
--SA
Volynsky Alex 9-Aug-12 6:54am
   
The good answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 8 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100