无法理解代码,请帮忙 [英] Not able to understand the code please help

查看:90
本文介绍了无法理解代码,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从某些网站上获得了以下代码.我无法理解以下内容:-
1>为什么他们使用了这么多的in_addr和in6_addr.
2>为什么他们两次使用了in6_addr.
3>我也无法理解sscanf中使用的格式说明符.
4>最后一个问题,为什么在tcp6和udp6的情况下它们写了那么长的代码.
如果有人能解释以上所有问题,我将非常感谢他

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;
}

推荐答案

sscanf格式说明符显然是要获取一些ipv6 ip地址. 它们没有2个in6_addr,但没有4个该类型的变量. l标识本地地址,r标识远程地址.出于任何原因,交换地址都用于更改本地和远程IP地址的顺序.可能是因为客户端计算机是低端字节序的,如果在此顶部放置一个整数,则交换顺序可能会正确.取决于netlistadd的行为.
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.


这篇关于无法理解代码,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆