source: titan/titan/inetwork.h @ 39959

Last change on this file since 39959 was 23633, checked in by nit, 11 years ago

[titan] fix lang

File size: 9.9 KB
Line 
1#ifndef INETWORK_H
2#define INETWORK_H
3
4void delinetworkfound()
5{
6        struct inetwork *node = inetwork;
7
8        while(node != NULL)
9        {
10                node->found = 0;
11                node = node->next;
12        }
13}
14
15struct inetwork* getinetworkfirstwlan()
16{
17        struct inetwork *node = inetwork;
18
19        while(node != NULL)
20        {
21                if(ostrcmp(node->device, "ra0") == 0)
22                        return node;
23                if(ostrcmp(node->device, "wlan0") == 0)
24                        return node;
25
26                node = node->next;
27        }
28        return NULL;
29}
30
31struct inetwork* getinetworkbydevice(char* device)
32{
33        struct inetwork *node = inetwork;
34
35        while(node != NULL)
36        {
37                if(ostrcmp(node->device, device) == 0)
38                        return node;
39
40                node = node->next;
41        }
42        debug(100, "inetwork not found (name=%s)", device);
43        return NULL;
44}
45
46void delinetwork(char* device)
47{
48        struct inetwork *node = inetwork, *prev = inetwork;
49       
50        while(node != NULL)
51        {
52                if(ostrcmp(node->device, device) == 0)
53                {
54                        if(node == inetwork)
55                        {
56                                inetwork = node->next;
57                                if(inetwork != NULL)
58                                        inetwork->prev = NULL;
59                        }
60                        else
61                        {
62                                prev->next = node->next;
63                                if(node->next != NULL)
64                                        node->next->prev = prev;
65                        }
66
67                        free(node->device); node->device = NULL;
68                        free(node->ip); node->ip = NULL;
69                        free(node->netmask); node->netmask = NULL;
70                        free(node->mac); node->mac = NULL;
71                        free(node->broadcast); node->broadcast = NULL;
72
73                        free(node);
74                        node = NULL;
75                        break;
76                }
77
78                prev = node;
79                node = node->next;
80        }
81}
82
83void delinetworknotfound()
84{
85        struct inetwork *node = inetwork, *prev = inetwork;
86
87        while(node != NULL)
88        {
89                prev = node;
90                node = node->next;
91                if(prev != NULL && prev->found == 0)
92                        delinetwork(prev->device);
93        }
94}
95
96//flag 0: change all
97//flag 1: check only values from getifaddrs (called from timer)
98struct inetwork* changeinetwork(char* device, char* ip, char* netmask, char* mac, char* broadcast, int type, struct inetwork* node, int flag)
99{
100        if(node == NULL) return NULL;
101
102        if(ostrcmp(node->device, device) != 0)
103        {
104                free(node->device);
105                node->device = device;
106        }
107        else
108                free(device);
109
110        if(ostrcmp(node->ip, ip) != 0)
111        {
112                free(node->ip);
113                node->ip = ip;
114        }
115        else
116                free(ip);
117
118        if(ostrcmp(node->netmask, netmask) != 0)
119        {
120                free(node->netmask);
121                node->netmask = netmask;
122        }
123        else
124                free(netmask);
125
126        if(ostrcmp(node->broadcast, broadcast) != 0)
127        {
128                free(node->broadcast);
129                node->broadcast = broadcast;
130        }
131        else
132                free(broadcast);
133
134        if(flag == 0)
135        {
136                if(ostrcmp(node->mac, mac) != 0)
137                {
138                        free(node->mac);
139                        node->mac = mac;
140                }
141                else
142                        free(mac);
143
144                node->type = type;
145        }
146        else
147                free(mac);
148       
149        node->found = 1;
150
151        return node;
152}
153
154struct inetwork* addinetwork(char* device, char* ip, char* netmask, char* mac, char* broadcast, int type, struct inetwork* last)
155{
156        struct inetwork *newnode = NULL, *prev = NULL, *node = inetwork;
157
158        newnode = (struct inetwork*)malloc(sizeof(struct inetwork));   
159        if(newnode == NULL)
160        {
161                err("no memory");
162                return NULL;
163        }
164
165        memset(newnode, 0, sizeof(struct inetwork));
166
167        newnode->device = device;
168        newnode->ip = ip;
169        newnode->netmask = netmask;
170        newnode->mac = mac;
171        newnode->broadcast = broadcast;
172        newnode->type = type;
173        newnode->found = 1;
174       
175        if(ostrcmp(device, "eth0") == 0)
176                newnode->flag = 1;
177        if(ostrcmp(device, "ra0") == 0)
178                newnode->flag = 1;
179        if(ostrcmp(device, "wlan0") == 0)
180                newnode->flag = 1;
181       
182        debug(50,"[NETWORK] device: %s", device);
183        debug(50,"[NETWORK] ip: %s", ip);
184        debug(50,"[NETWORK] netmask: %s", netmask);
185        debug(50,"[NETWORK] mac: %s", mac);
186        debug(50,"[NETWORK] broadcast: %s", broadcast);
187        debug(50,"[NETWORK] type: %d\n", type);
188
189        if(last == NULL)
190        {
191                while(node != NULL && strcoll(newnode->device, node->device) > 0)
192                {
193                        prev = node;
194                        node = node->next;
195                }
196        }
197        else
198        {
199                prev = last;
200                node = last->next;
201        }
202
203        if(prev == NULL)
204                inetwork = newnode;
205        else
206        {
207                prev->next = newnode;
208                newnode->prev = prev;
209        }
210        newnode->next = node;
211
212        return newnode;
213}
214
215//self == NULL: check all
216//self != NULL: check only values from getifaddrs (called from timer)
217int addinetworkall(struct stimerthread* self)
218{
219        int ret = 0;
220        struct ifaddrs *ifa = NULL, *tmpifa = NULL;
221        void* tmpaddr = NULL;
222        char* buf = NULL, *tmpstr = NULL;
223        char* tmp_gateway = NULL;
224        char* tmp_dnsserver1 = NULL;
225        char* tmp_dnsserver2 = NULL;
226        char* cmd = NULL;
227
228        m_lock(&status.inetworkmutex, 21);
229        struct inetwork* node = inetwork;
230
231        ret = getifaddrs(&ifa);
232        if(ret != 0)
233        {
234                err("get network interfaces");
235                m_unlock(&status.inetworkmutex, 21);
236                return 1;
237        }
238
239        buf = malloc(MINMALLOC);
240        if(buf == NULL)
241        {
242                err("no mem");
243                m_unlock(&status.inetworkmutex, 21);
244                return 1;
245        }
246
247        delinetworkfound();
248
249        for(tmpifa = ifa; tmpifa != NULL; tmpifa = tmpifa->ifa_next)
250        {
251                char* tmp_ipaddress = NULL;
252                char* tmp_netmask = NULL;
253                char* tmp_mac = NULL;
254                char* tmp_broadcast = NULL;
255                char* tmp_device = NULL;
256                int tmp_type = 0;
257
258                if(tmpifa->ifa_addr == NULL)
259                        continue;
260
261                //Don't add IPV6 ot other
262                if(tmpifa->ifa_addr->sa_family != AF_INET && tmpifa->ifa_addr->sa_family != AF_PACKET)
263                        continue;
264
265                //Device Name
266                tmp_device = string_newline(ostrcat(tmp_device, tmpifa->ifa_name, 1, 0));
267
268                //IP ADDR
269                memset(buf, 0, MINMALLOC);
270                if(tmpifa->ifa_addr->sa_family == AF_INET)
271                        tmpaddr = &((struct sockaddr_in*)tmpifa->ifa_addr)->sin_addr;
272                else
273                        tmpaddr = &((struct sockaddr_in6*)tmpifa->ifa_addr)->sin6_addr;
274                inet_ntop(tmpifa->ifa_addr->sa_family, tmpaddr, buf, MINMALLOC);
275                if(strlen(buf) != 0)
276                {
277                        tmpstr = fixip(buf, 0);
278                        tmp_ipaddress = string_newline(ostrcat(tmpstr, NULL, 1, 0));
279                }
280                else
281                        tmp_ipaddress = ostrcat(tmp_ipaddress, "000.000.000.000", 1, 0);
282
283                //NETMASK
284                memset(buf, 0, MINMALLOC);
285                if(tmpifa->ifa_netmask != NULL)
286                {
287                        if(tmpifa->ifa_netmask->sa_family == AF_INET)
288                                tmpaddr = &((struct sockaddr_in*)tmpifa->ifa_netmask)->sin_addr;
289                        else
290                                tmpaddr = &((struct sockaddr_in6*)tmpifa->ifa_netmask)->sin6_addr;
291                        inet_ntop(tmpifa->ifa_netmask->sa_family, tmpaddr, buf, MINMALLOC);
292
293                }
294                if(strlen(buf) != 0)
295                {
296                        tmpstr = fixip(buf, 0);
297                        tmp_netmask = string_newline(ostrcat(tmpstr, NULL, 1, 0));
298                }
299                else
300                        tmp_netmask = ostrcat(tmp_netmask, "000.000.000.000", 1, 0);
301
302                //BROADCAST / DSTADDR
303                memset(buf, 0, MINMALLOC);
304                if(tmpifa->ifa_ifu.ifu_broadaddr != NULL)
305                {
306                        if(tmpifa->ifa_flags & IFF_POINTOPOINT)
307                        {
308                                if(tmpifa->ifa_ifu.ifu_dstaddr->sa_family == AF_INET)
309                                        tmpaddr = &((struct sockaddr_in*)tmpifa->ifa_ifu.ifu_dstaddr)->sin_addr;
310                                else
311                                        tmpaddr = &((struct sockaddr_in6*)tmpifa->ifa_ifu.ifu_dstaddr)->sin6_addr;
312                                inet_ntop(tmpifa->ifa_ifu.ifu_dstaddr->sa_family, tmpaddr, buf, MINMALLOC);
313                        }
314                        else
315                        {
316                                if(tmpifa->ifa_ifu.ifu_broadaddr->sa_family == AF_INET)
317                                        tmpaddr = &((struct sockaddr_in*)tmpifa->ifa_ifu.ifu_broadaddr)->sin_addr;
318                                else
319                                        tmpaddr = &((struct sockaddr_in6*)tmpifa->ifa_ifu.ifu_broadaddr)->sin6_addr;
320                                inet_ntop(tmpifa->ifa_ifu.ifu_broadaddr->sa_family, tmpaddr, buf, MINMALLOC);
321                        }
322                               
323                }
324                if(strlen(buf) != 0)
325                {
326                        tmpstr = fixip(tmp_broadcast, 0);
327                        tmp_broadcast = string_newline(ostrcat(tmpstr, NULL,  1, 0));
328                }
329                else
330                        tmp_broadcast = ostrcat(tmp_broadcast, "000.000.000.000", 1, 0);
331
332                if(self == NULL)
333                {
334                        // MAC
335                        tmp_mac = getmac(tmp_device);
336                        if(tmp_mac == NULL)
337                                tmp_mac = ostrcat(tmp_mac, "00:00:00:00:00:00", 1, 0);
338
339                        // DHCP
340                        if(tmp_type == 0)
341                        {
342                                cmd = ostrcat(cmd, "cat /mnt/network/interfaces | grep ", 1, 0);
343                                cmd = ostrcat(cmd, tmp_device, 1, 0);
344                                cmd = ostrcat(cmd, " | grep dhcp | wc -l", 1, 0);
345                                tmpstr = command(cmd);
346                                if(tmpstr != NULL && atoi(tmpstr) == 1)
347                                        tmp_type = 1;
348                                free(tmpstr); tmpstr = NULL;
349                        }
350                        free(cmd); cmd = NULL;
351
352                        // OFF
353                        if(tmp_type == 0)
354                        {
355                                cmd = ostrcat(cmd, "cat /mnt/network/interfaces | grep ", 1, 0);
356                                cmd = ostrcat(cmd, tmp_device, 1, 0);
357                                cmd = ostrcat(cmd, " | grep off | wc -l", 1, 0);
358                                tmpstr = command(cmd);
359                                if(tmpstr != NULL && atoi(tmpstr) == 1)
360                                        tmp_type = 2;
361                                free(tmpstr); tmpstr = NULL;
362                        }
363                        free(cmd); cmd = NULL;
364                }
365
366                struct inetwork* tmpinetwork = getinetworkbydevice(tmp_device);
367                if(tmpinetwork != NULL)
368                {
369                        if(self == NULL)
370                                node = changeinetwork(tmp_device, tmp_ipaddress, tmp_netmask, tmp_mac, tmp_broadcast, tmp_type, tmpinetwork, 0);
371                        else
372                                node = changeinetwork(tmp_device, tmp_ipaddress, tmp_netmask, tmp_mac, tmp_broadcast, tmp_type, tmpinetwork, 1);
373                }
374                else
375                        node = addinetwork(tmp_device, tmp_ipaddress, tmp_netmask, tmp_mac, tmp_broadcast, tmp_type, node);
376        }
377
378        freeifaddrs(ifa); ifa = NULL;
379
380        // GATEWAY
381        if(self == NULL)
382        {
383                free(status.gateway); status.gateway = NULL;
384                tmp_gateway = getdefaultgw();
385                if(tmp_gateway != NULL)
386                {
387                        tmpstr = fixip(tmp_gateway, 0);
388                        status.gateway = string_newline(ostrcat(tmpstr, NULL, 1, 0));
389                }
390                else
391                        status.gateway = ostrcat(tmp_gateway, "000.000.000.000", 1, 0);
392
393                // DNSSERVER1
394                free(status.dnsserver1); status.dnsserver1 = NULL;
395                cmd = ostrcat(cmd, "cat /mnt/network/resolv.conf | grep nameserver | awk '{ print $2 }' | sed -n 1p", 1, 0);
396                tmp_dnsserver1 = ostrcat(tmp_dnsserver1 , command(cmd), 1, 1);
397                if(tmp_dnsserver1 != NULL)
398                {
399                        tmpstr = fixip(tmp_dnsserver1, 0);
400                        free(tmp_dnsserver1); tmp_dnsserver1 = NULL;
401                        status.dnsserver1 = string_newline(ostrcat(tmpstr, NULL, 1, 0));
402                }
403                else
404                        status.dnsserver1 = ostrcat(tmp_dnsserver1, "000.000.000.000", 1, 0);
405                free(cmd); cmd = NULL;
406
407                // DNSSERVER2
408                free(status.dnsserver2); status.dnsserver2 = NULL;
409                cmd = ostrcat(cmd, "cat /mnt/network/resolv.conf | grep nameserver | awk '{ print $2 }' | sed -n 2p", 1, 0);
410                tmp_dnsserver2 = ostrcat(tmp_dnsserver2 , command(cmd), 1, 1);
411                if(tmp_dnsserver2 != NULL)
412                {
413                        tmpstr = fixip(tmp_dnsserver2, 0);
414                        free(tmp_dnsserver2); tmp_dnsserver2 = NULL;
415                        status.dnsserver2 = string_newline(ostrcat(tmpstr, NULL, 1, 0));
416                }
417                else
418                        status.dnsserver2 = ostrcat(tmp_dnsserver2, "000.000.000.000", 1, 0);
419        }
420        free(cmd); cmd = NULL;
421        free(buf);
422
423        delinetworknotfound();
424        m_unlock(&status.inetworkmutex, 21);
425        return 0;
426}
427
428void freeinetwork()
429{
430        struct inetwork *node = inetwork, *prev = inetwork;
431
432        while(node != NULL)
433        {
434                prev = node;
435                node = node->next;
436                if(prev != NULL)
437                        delinetwork(prev->device);
438        }
439}
440
441#endif
Note: See TracBrowser for help on using the repository browser.