~ubuntu-branches/ubuntu/precise/hardinfo/precise

« back to all changes in this revision

Viewing changes to network.c

  • Committer: Bazaar Package Importer
  • Author(s): Agney Lopes Roth Ferraz
  • Date: 2009-03-28 22:55:02 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090328225502-p4bnvi8q6hr95cij
Tags: 0.5c-1
New upstream version. 
(Closes: #517591, #511237, #457703, #519256, #449250, #457820, #497758) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    HardInfo - Displays System Information
 
3
 *    Copyright (C) 2003-2008 Leandro A. F. Pereira <leandro@hardinfo.org>
 
4
 *
 
5
 *    This program is free software; you can redistribute it and/or modify
 
6
 *    it under the terms of the GNU General Public License as published by
 
7
 *    the Free Software Foundation, version 2.
 
8
 *
 
9
 *    This program is distributed in the hope that it will be useful,
 
10
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *    GNU General Public License for more details.
 
13
 *
 
14
 *    You should have received a copy of the GNU General Public License
 
15
 *    along with this program; if not, write to the Free Software
 
16
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
 */
 
18
 
 
19
#include <ctype.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <gtk/gtk.h>
 
23
#include <config.h>
 
24
#include <time.h>
 
25
#include <string.h>
 
26
#include <sys/utsname.h>
 
27
#include <sys/stat.h>
 
28
 
 
29
#include <hardinfo.h>
 
30
#include <iconcache.h>
 
31
#include <shell.h>
 
32
 
 
33
#include <vendor.h>
 
34
 
 
35
static GHashTable *moreinfo = NULL;
 
36
 
 
37
/* Callbacks */
 
38
gchar *callback_network();
 
39
gchar *callback_route();
 
40
gchar *callback_dns();
 
41
gchar *callback_connections();
 
42
gchar *callback_shares();
 
43
gchar *callback_arp();
 
44
gchar *callback_statistics();
 
45
 
 
46
/* Scan callbacks */
 
47
void scan_network(gboolean reload);
 
48
void scan_route(gboolean reload);
 
49
void scan_dns(gboolean reload);
 
50
void scan_connections(gboolean reload);
 
51
void scan_shares(gboolean reload);
 
52
void scan_arp(gboolean reload);
 
53
void scan_statistics(gboolean reload);
 
54
 
 
55
static ModuleEntry entries[] = {
 
56
    {"Interfaces", "network-interface.png", callback_network, scan_network},
 
57
    {"IP Connections", "network-connections.png", callback_connections, scan_connections},
 
58
    {"Routing Table", "network.png", callback_route, scan_route},
 
59
    {"ARP Table", "module.png", callback_arp, scan_arp},
 
60
    {"DNS Servers", "dns.png", callback_dns, scan_dns},
 
61
    {"Statistics", "network-statistics.png", callback_statistics, scan_statistics},
 
62
    {"Shared Directories", "shares.png", callback_shares, scan_shares},
 
63
    {NULL},
 
64
};
 
65
 
 
66
#include <arch/this/samba.h>
 
67
#include <arch/this/nfs.h>
 
68
#include <arch/this/net.h>
 
69
 
 
70
void scan_shares(gboolean reload)
 
71
{
 
72
    SCAN_START();
 
73
    scan_samba_shared_directories();
 
74
    scan_nfs_shared_directories();
 
75
    SCAN_END();
 
76
}
 
77
 
 
78
static gchar *__statistics = NULL;
 
79
void scan_statistics(gboolean reload)
 
80
{
 
81
    FILE *netstat;
 
82
    gchar buffer[256];
 
83
    gchar *netstat_path;
 
84
    
 
85
    SCAN_START();
 
86
    
 
87
    g_free(__statistics);
 
88
    __statistics = g_strdup("");
 
89
    
 
90
    if ((netstat_path = find_program("netstat"))) {
 
91
      gchar *command_line = g_strdup_printf("%s -s", netstat_path);
 
92
      
 
93
      if ((netstat = popen(command_line, "r"))) {
 
94
        while (fgets(buffer, 256, netstat)) {
 
95
          if (!isspace(buffer[0]) && strchr(buffer, ':')) {
 
96
            gchar *tmp;
 
97
            
 
98
            tmp = g_ascii_strup(strend(buffer, ':'), -1);
 
99
            
 
100
            __statistics = h_strdup_cprintf("[%s]\n",
 
101
                                            __statistics,
 
102
                                            tmp);
 
103
            
 
104
            g_free(tmp);
 
105
          } else if (isdigit(buffer[4])) {
 
106
            gchar *tmp1 = buffer + 4,
 
107
                  *tmp2 = tmp1;
 
108
            
 
109
            while (*tmp2 && !isspace(*tmp2)) tmp2++;
 
110
            *tmp2 = 0;
 
111
            tmp2++;
 
112
            
 
113
            *tmp2 = toupper(*tmp2);
 
114
            
 
115
            __statistics = h_strdup_cprintf("%s=%s\n",
 
116
                                            __statistics,
 
117
                                            g_strstrip(tmp1),
 
118
                                            g_strstrip(tmp2));
 
119
          }
 
120
        }
 
121
 
 
122
        pclose(netstat);
 
123
      }
 
124
      
 
125
      g_free(command_line);
 
126
      g_free(netstat_path);
 
127
    }
 
128
    
 
129
    SCAN_END();
 
130
}
 
131
 
 
132
static gchar *__nameservers = NULL;
 
133
void scan_dns(gboolean reload)
 
134
{
 
135
    FILE *resolv;
 
136
    gchar buffer[256];
 
137
    
 
138
    SCAN_START();
 
139
    
 
140
    g_free(__nameservers);
 
141
    __nameservers = g_strdup("");
 
142
    
 
143
    if ((resolv = fopen("/etc/resolv.conf", "r"))) {
 
144
      while (fgets(buffer, 256, resolv)) {
 
145
        if (g_str_has_prefix(buffer, "nameserver")) {
 
146
          __nameservers = h_strdup_cprintf("%s=\n",
 
147
                                           __nameservers,
 
148
                                           g_strstrip(buffer + sizeof("nameserver")));
 
149
        } 
 
150
      }
 
151
      fclose(resolv);
 
152
    }
 
153
    
 
154
    SCAN_END();
 
155
}
 
156
 
 
157
void scan_network(gboolean reload)
 
158
{
 
159
    SCAN_START();
 
160
    scan_net_interfaces();
 
161
    SCAN_END();
 
162
}
 
163
 
 
164
static gchar *__routing_table = NULL;
 
165
void scan_route(gboolean reload)
 
166
{
 
167
    FILE *route;
 
168
    gchar buffer[256];
 
169
    gchar *route_path;
 
170
    
 
171
    SCAN_START();
 
172
 
 
173
    g_free(__routing_table);
 
174
    __routing_table = g_strdup("");
 
175
    
 
176
    if ((route_path = find_program("route"))) {
 
177
      gchar *command_line = g_strdup_printf("%s -n", route_path);
 
178
      
 
179
      if ((route = popen(command_line, "r"))) {
 
180
        /* eat first two lines */
 
181
        fgets(buffer, 256, route);
 
182
        fgets(buffer, 256, route);
 
183
 
 
184
        while (fgets(buffer, 256, route)) {
 
185
          buffer[15] = '\0';
 
186
          buffer[31] = '\0';
 
187
          buffer[47] = '\0';
 
188
          buffer[53] = '\0';
 
189
          
 
190
          __routing_table = h_strdup_cprintf("%s / %s=%s|%s|%s\n",
 
191
                                             __routing_table,
 
192
                                             g_strstrip(buffer), g_strstrip(buffer + 16),
 
193
                                             g_strstrip(buffer + 72),
 
194
                                             g_strstrip(buffer + 48),
 
195
                                             g_strstrip(buffer + 32));
 
196
        }
 
197
        
 
198
        pclose(route);
 
199
      }
 
200
      
 
201
      g_free(command_line);
 
202
      g_free(route_path);
 
203
    }
 
204
    
 
205
    SCAN_END();
 
206
}
 
207
 
 
208
static gchar *__arp_table = NULL;
 
209
void scan_arp(gboolean reload)
 
210
{
 
211
    FILE *arp;
 
212
    gchar buffer[256];
 
213
    
 
214
    SCAN_START();
 
215
 
 
216
    g_free(__arp_table);
 
217
    __arp_table = g_strdup("");
 
218
    
 
219
    if ((arp = fopen("/proc/net/arp", "r"))) {
 
220
      /* eat first line */
 
221
      fgets(buffer, 256, arp);
 
222
 
 
223
      while (fgets(buffer, 256, arp)) {
 
224
        buffer[15] = '\0';
 
225
        buffer[58] = '\0';
 
226
        
 
227
        __arp_table = h_strdup_cprintf("%s=%s|%s\n",
 
228
                                       __arp_table,
 
229
                                       g_strstrip(buffer),
 
230
                                       g_strstrip(buffer + 72),
 
231
                                       g_strstrip(buffer + 41));
 
232
      }
 
233
      
 
234
      pclose(arp);
 
235
    }
 
236
    
 
237
    SCAN_END();
 
238
}
 
239
 
 
240
static gchar *__connections = NULL;
 
241
void scan_connections(gboolean reload)
 
242
{
 
243
    FILE *netstat;
 
244
    gchar buffer[256];
 
245
    gchar *netstat_path;
 
246
    
 
247
    SCAN_START();
 
248
 
 
249
    g_free(__connections);
 
250
    __connections = g_strdup("");
 
251
    
 
252
    if ((netstat_path = find_program("netstat"))) {
 
253
      gchar *command_line = g_strdup_printf("%s -an", netstat_path);
 
254
      
 
255
      if ((netstat = popen("netstat -an", "r"))) {
 
256
        while (fgets(buffer, 256, netstat)) {
 
257
          buffer[6] = '\0';
 
258
          buffer[43] = '\0';
 
259
          buffer[67] = '\0';
 
260
 
 
261
          if (g_str_has_prefix(buffer, "tcp") || g_str_has_prefix(buffer, "udp")) {
 
262
            __connections = h_strdup_cprintf("%s=%s|%s|%s\n",
 
263
                                             __connections,
 
264
                                             g_strstrip(buffer + 20),   /* local address */
 
265
                                             g_strstrip(buffer),                /* protocol */
 
266
                                             g_strstrip(buffer + 44),   /* foreign address */
 
267
                                             g_strstrip(buffer + 68));  /* state */
 
268
          }
 
269
        }
 
270
        
 
271
        pclose(netstat);
 
272
      }
 
273
      
 
274
      g_free(command_line);
 
275
      g_free(netstat_path);
 
276
    }
 
277
    
 
278
    SCAN_END();
 
279
}
 
280
 
 
281
gchar *callback_arp()
 
282
{
 
283
    return g_strdup_printf("[ARP Table]\n"
 
284
                           "%s\n"
 
285
                           "[$ShellParam$]\n"
 
286
                           "ReloadInterval=3000\n"
 
287
                           "ColumnTitle$TextValue=IP Address\n"
 
288
                           "ColumnTitle$Value=Interface\n"
 
289
                           "ColumnTitle$Extra1=MAC Address\n"
 
290
                           "ShowColumnHeaders=true\n",
 
291
                           __arp_table);
 
292
}
 
293
 
 
294
gchar *callback_shares()
 
295
{
 
296
    return g_strdup_printf("[SAMBA]\n"
 
297
                           "%s\n"
 
298
                           "[NFS]\n"
 
299
                           "%s", smb_shares_list, nfs_shares_list);
 
300
}
 
301
 
 
302
gchar *callback_dns()
 
303
{
 
304
    return g_strdup_printf("[Name servers]\n"
 
305
                           "%s\n", __nameservers);
 
306
}
 
307
 
 
308
gchar *callback_connections()
 
309
{
 
310
    return g_strdup_printf("[Connections]\n"
 
311
                           "%s\n"
 
312
                           "[$ShellParam$]\n"
 
313
                           "ReloadInterval=3000\n"
 
314
                           "ColumnTitle$TextValue=Local Address\n"
 
315
                           "ColumnTitle$Value=Protocol\n"
 
316
                           "ColumnTitle$Extra1=Foreign Address\n"
 
317
                           "ColumnTitle$Extra2=State\n"
 
318
                           "ShowColumnHeaders=true\n",
 
319
                           __connections);
 
320
}
 
321
 
 
322
gchar *callback_network()
 
323
{
 
324
    return g_strdup_printf("%s\n"
 
325
                           "[$ShellParam$]\n"
 
326
                           "ReloadInterval=3000\n"
 
327
                           "ViewType=1\n"
 
328
                           "ColumnTitle$TextValue=Device\n"
 
329
                           "ColumnTitle$Value=IP Address\n"
 
330
                           "ColumnTitle$Extra1=Sent\n"
 
331
                           "ColumnTitle$Extra2=Received\n"
 
332
                           "ShowColumnHeaders=true\n"
 
333
                           "%s",
 
334
                           network_interfaces,
 
335
                           network_icons);
 
336
}
 
337
 
 
338
gchar *callback_route()
 
339
{
 
340
    return g_strdup_printf("[IP routing table]\n"
 
341
                           "%s\n"
 
342
                           "[$ShellParam$]\n"
 
343
                           "ViewType=0\n"
 
344
                           "ReloadInterval=3000\n"
 
345
                           "ColumnTitle$TextValue=Destination / Gateway\n"
 
346
                           "ColumnTitle$Value=Interface\n"
 
347
                           "ColumnTitle$Extra1=Flags\n"
 
348
                           "ColumnTitle$Extra2=Mask\n"
 
349
                           "ShowColumnHeaders=true\n",
 
350
                           __routing_table);
 
351
}
 
352
 
 
353
gchar *callback_statistics()
 
354
{
 
355
    return g_strdup_printf("%s\n"
 
356
                           "[$ShellParam$]\n"
 
357
                           "ReloadInterval=3000\n",
 
358
                            __statistics);
 
359
}
 
360
 
 
361
gchar *hi_more_info(gchar * entry)
 
362
{
 
363
    gchar *info = (gchar *) g_hash_table_lookup(moreinfo, entry);
 
364
 
 
365
    if (info)
 
366
        return g_strdup(info);
 
367
 
 
368
    return g_strdup_printf("[%s]", entry);
 
369
}
 
370
 
 
371
ModuleEntry *hi_module_get_entries(void)
 
372
{
 
373
    return entries;
 
374
}
 
375
 
 
376
gchar *hi_module_get_name(void)
 
377
{
 
378
    return g_strdup("Network");
 
379
}
 
380
 
 
381
guchar hi_module_get_weight(void)
 
382
{
 
383
    return 160;
 
384
}
 
385
 
 
386
void hi_module_init(void)
 
387
{
 
388
    moreinfo =
 
389
        g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
 
390
}
 
391
 
 
392
ModuleAbout *hi_module_get_about(void)
 
393
{
 
394
    static ModuleAbout ma[] = {
 
395
        {
 
396
         .author = "Leandro A. F. Pereira",
 
397
         .description = "Gathers information about this computer's network connection",
 
398
         .version = VERSION,
 
399
         .license = "GNU GPL version 2"}
 
400
    };
 
401
 
 
402
    return ma;
 
403
}