File: network.c

package info (click to toggle)
hardinfo 0.5.1-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,148 kB
  • sloc: ansic: 12,199; sh: 199; makefile: 120
file content (403 lines) | stat: -rw-r--r-- 11,153 bytes parent folder | download | duplicates (4)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2008 Leandro A. F. Pereira <leandro@hardinfo.org>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, version 2.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <config.h>
#include <time.h>
#include <string.h>
#include <sys/utsname.h>
#include <sys/stat.h>

#include <hardinfo.h>
#include <iconcache.h>
#include <shell.h>

#include <vendor.h>

static GHashTable *moreinfo = NULL;

/* Callbacks */
gchar *callback_network();
gchar *callback_route();
gchar *callback_dns();
gchar *callback_connections();
gchar *callback_shares();
gchar *callback_arp();
gchar *callback_statistics();

/* Scan callbacks */
void scan_network(gboolean reload);
void scan_route(gboolean reload);
void scan_dns(gboolean reload);
void scan_connections(gboolean reload);
void scan_shares(gboolean reload);
void scan_arp(gboolean reload);
void scan_statistics(gboolean reload);

static ModuleEntry entries[] = {
    {"Interfaces", "network-interface.png", callback_network, scan_network},
    {"IP Connections", "network-connections.png", callback_connections, scan_connections},
    {"Routing Table", "network.png", callback_route, scan_route},
    {"ARP Table", "module.png", callback_arp, scan_arp},
    {"DNS Servers", "dns.png", callback_dns, scan_dns},
    {"Statistics", "network-statistics.png", callback_statistics, scan_statistics},
    {"Shared Directories", "shares.png", callback_shares, scan_shares},
    {NULL},
};

#include <arch/this/samba.h>
#include <arch/this/nfs.h>
#include <arch/this/net.h>

void scan_shares(gboolean reload)
{
    SCAN_START();
    scan_samba_shared_directories();
    scan_nfs_shared_directories();
    SCAN_END();
}

static gchar *__statistics = NULL;
void scan_statistics(gboolean reload)
{
    FILE *netstat;
    gchar buffer[256];
    gchar *netstat_path;
    
    SCAN_START();
    
    g_free(__statistics);
    __statistics = g_strdup("");
    
    if ((netstat_path = find_program("netstat"))) {
      gchar *command_line = g_strdup_printf("%s -s", netstat_path);
      
      if ((netstat = popen(command_line, "r"))) {
        while (fgets(buffer, 256, netstat)) {
          if (!isspace(buffer[0]) && strchr(buffer, ':')) {
            gchar *tmp;
            
            tmp = g_ascii_strup(strend(buffer, ':'), -1);
            
            __statistics = h_strdup_cprintf("[%s]\n",
                                            __statistics,
                                            tmp);
            
            g_free(tmp);
          } else if (isdigit(buffer[4])) {
            gchar *tmp1 = buffer + 4,
                  *tmp2 = tmp1;
            
            while (*tmp2 && !isspace(*tmp2)) tmp2++;
            *tmp2 = 0;
            tmp2++;
            
            *tmp2 = toupper(*tmp2);
            
            __statistics = h_strdup_cprintf("%s=%s\n",
                                            __statistics,
                                            g_strstrip(tmp1),
                                            g_strstrip(tmp2));
          }
        }

        pclose(netstat);
      }
      
      g_free(command_line);
      g_free(netstat_path);
    }
    
    SCAN_END();
}

static gchar *__nameservers = NULL;
void scan_dns(gboolean reload)
{
    FILE *resolv;
    gchar buffer[256];
    
    SCAN_START();
    
    g_free(__nameservers);
    __nameservers = g_strdup("");
    
    if ((resolv = fopen("/etc/resolv.conf", "r"))) {
      while (fgets(buffer, 256, resolv)) {
        if (g_str_has_prefix(buffer, "nameserver")) {
          __nameservers = h_strdup_cprintf("%s=\n",
                                           __nameservers,
                                           g_strstrip(buffer + sizeof("nameserver")));
        } 
      }
      fclose(resolv);
    }
    
    SCAN_END();
}

void scan_network(gboolean reload)
{
    SCAN_START();
    scan_net_interfaces();
    SCAN_END();
}

static gchar *__routing_table = NULL;
void scan_route(gboolean reload)
{
    FILE *route;
    gchar buffer[256];
    gchar *route_path;
    
    SCAN_START();

    g_free(__routing_table);
    __routing_table = g_strdup("");
    
    if ((route_path = find_program("route"))) {
      gchar *command_line = g_strdup_printf("%s -n", route_path);
      
      if ((route = popen(command_line, "r"))) {
        /* eat first two lines */
        (void)fgets(buffer, 256, route);
        (void)fgets(buffer, 256, route);

        while (fgets(buffer, 256, route)) {
          buffer[15] = '\0';
          buffer[31] = '\0';
          buffer[47] = '\0';
          buffer[53] = '\0';
          
          __routing_table = h_strdup_cprintf("%s / %s=%s|%s|%s\n",
                                             __routing_table,
                                             g_strstrip(buffer), g_strstrip(buffer + 16),
                                             g_strstrip(buffer + 72),
                                             g_strstrip(buffer + 48),
                                             g_strstrip(buffer + 32));
        }
        
        pclose(route);
      }
      
      g_free(command_line);
      g_free(route_path);
    }
    
    SCAN_END();
}

static gchar *__arp_table = NULL;
void scan_arp(gboolean reload)
{
    FILE *arp;
    gchar buffer[256];
    
    SCAN_START();

    g_free(__arp_table);
    __arp_table = g_strdup("");
    
    if ((arp = fopen("/proc/net/arp", "r"))) {
      /* eat first line */
      (void)fgets(buffer, 256, arp);

      while (fgets(buffer, 256, arp)) {
        buffer[15] = '\0';
        buffer[58] = '\0';
        
        __arp_table = h_strdup_cprintf("%s=%s|%s\n",
                                       __arp_table,
                                       g_strstrip(buffer),
                                       g_strstrip(buffer + 72),
                                       g_strstrip(buffer + 41));
      }
      
      pclose(arp);
    }
    
    SCAN_END();
}

static gchar *__connections = NULL;
void scan_connections(gboolean reload)
{
    FILE *netstat;
    gchar buffer[256];
    gchar *netstat_path;
    
    SCAN_START();

    g_free(__connections);
    __connections = g_strdup("");
    
    if ((netstat_path = find_program("netstat"))) {
      gchar *command_line = g_strdup_printf("%s -an", netstat_path);
      
      if ((netstat = popen("netstat -an", "r"))) {
        while (fgets(buffer, 256, netstat)) {
          buffer[6] = '\0';
          buffer[43] = '\0';
          buffer[67] = '\0';

          if (g_str_has_prefix(buffer, "tcp") || g_str_has_prefix(buffer, "udp")) {
            __connections = h_strdup_cprintf("%s=%s|%s|%s\n",
                                             __connections,
                                             g_strstrip(buffer + 20),	/* local address */
                                             g_strstrip(buffer),		/* protocol */
                                             g_strstrip(buffer + 44),	/* foreign address */
                                             g_strstrip(buffer + 68));	/* state */
          }
        }
        
        pclose(netstat);
      }
      
      g_free(command_line);
      g_free(netstat_path);
    }
    
    SCAN_END();
}

gchar *callback_arp()
{
    return g_strdup_printf("[ARP Table]\n"
                           "%s\n"
                           "[$ShellParam$]\n"
                           "ReloadInterval=3000\n"
                           "ColumnTitle$TextValue=IP Address\n"
                           "ColumnTitle$Value=Interface\n"
                           "ColumnTitle$Extra1=MAC Address\n"
                           "ShowColumnHeaders=true\n",
                           __arp_table);
}

gchar *callback_shares()
{
    return g_strdup_printf("[SAMBA]\n"
			   "%s\n"
			   "[NFS]\n"
			   "%s", smb_shares_list, nfs_shares_list);
}

gchar *callback_dns()
{
    return g_strdup_printf("[Name servers]\n"
                           "%s\n", __nameservers);
}

gchar *callback_connections()
{
    return g_strdup_printf("[Connections]\n"
                           "%s\n"
                           "[$ShellParam$]\n"
                           "ReloadInterval=3000\n"
                           "ColumnTitle$TextValue=Local Address\n"
                           "ColumnTitle$Value=Protocol\n"
                           "ColumnTitle$Extra1=Foreign Address\n"
                           "ColumnTitle$Extra2=State\n"
                           "ShowColumnHeaders=true\n",
                           __connections);
}

gchar *callback_network()
{
    return g_strdup_printf("%s\n"
                           "[$ShellParam$]\n"
			   "ReloadInterval=3000\n"
			   "ViewType=1\n"
			   "ColumnTitle$TextValue=Interface\n"
			   "ColumnTitle$Value=IP Address\n"
			   "ColumnTitle$Extra1=Sent\n"
			   "ColumnTitle$Extra2=Received\n"
			   "ShowColumnHeaders=true\n"
			   "%s",
			   network_interfaces,
			   network_icons);
}

gchar *callback_route()
{
    return g_strdup_printf("[IP routing table]\n"
                           "%s\n"
                           "[$ShellParam$]\n"
                           "ViewType=0\n"
                           "ReloadInterval=3000\n"
                           "ColumnTitle$TextValue=Destination / Gateway\n"
                           "ColumnTitle$Value=Interface\n"
                           "ColumnTitle$Extra1=Flags\n"
                           "ColumnTitle$Extra2=Mask\n"
                           "ShowColumnHeaders=true\n",
                           __routing_table);
}

gchar *callback_statistics()
{
    return g_strdup_printf("%s\n"
                           "[$ShellParam$]\n"
                           "ReloadInterval=3000\n",
                            __statistics);
}

gchar *hi_more_info(gchar * entry)
{
    gchar *info = (gchar *) g_hash_table_lookup(moreinfo, entry);

    if (info)
	return g_strdup(info);

    return g_strdup_printf("[%s]", entry);
}

ModuleEntry *hi_module_get_entries(void)
{
    return entries;
}

gchar *hi_module_get_name(void)
{
    return g_strdup("Network");
}

guchar hi_module_get_weight(void)
{
    return 160;
}

void hi_module_init(void)
{
    moreinfo =
	g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}

ModuleAbout *hi_module_get_about(void)
{
    static ModuleAbout ma[] = {
	{
	 .author = "Leandro A. F. Pereira",
	 .description = "Gathers information about this computer's network connection",
	 .version = VERSION,
	 .license = "GNU GPL version 2"}
    };

    return ma;
}