Click here to Skip to main content
15,883,796 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
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
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.

1 solution

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Aug-12 17:49pm    
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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900