Code not able to understand please help

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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);
            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;
}
Topic archived. No new replies allowed.