summaryrefslogtreecommitdiff
path: root/net.c
blob: 6529c4207cc2cd1a341eb35be1426a0503050cec (plain)
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
/*
 * Hardware Information, version 0.3
 * Copyright (C) 2003 Leandro Pereira <leandro@linuxmag.com.br>
 *
 * May be modified and/or distributed under the terms of GNU GPL version 2.
 */

#include <stdio.h>
#include "config.h"
#include "hardinfo.h"
#include "net.h"

gboolean net_update(gpointer data);

GtkWidget *net_get_widget(MainWindow * mainwindow)
{
    return gtk_label_new("Not implemented yet.");
}

gboolean net_update(gpointer data)
{
    MainWindow *mainwindow = (MainWindow *) data;
    NetDevice *net;
    static NetDevice *netold = NULL;
    
    if (!mainwindow)
	return FALSE;

    net = hi_scan_net();
    
    if (!net)
    	return FALSE;
    
    if (!netold) {
    	netold = net;
    } else {
	NetDevice *nd1, *nd2;
	
	for (nd1 = netold, nd2 = net; nd1 && nd2;
	     nd1 = nd1->next, nd2 = nd2->next) {
	     
	  	   g_print ("iface %s: %ld bytes/s up, %ld bytes/s down\n",
	  	   	   nd1->iface,
	  	   	   nd2->trans_bytes - nd1->trans_bytes,
	  	   	   nd2->recv_bytes - nd1->recv_bytes);
	} 		    	
	
	netold = net;
    }

    g_print("\n\n");
 
    return TRUE;
}

NetDevice *hi_scan_net(void)
{
    FILE *proc_net;
    gchar buffer[256];
    NetDevice *net_dev, *net;
    struct stat st;

    net = NULL;

    if (stat("/proc/net/dev", &st))
	return NULL;

    proc_net = fopen("/proc/net/dev", "r");
    while (fgets(buffer, 256, proc_net)) {
	if (strchr(buffer, ':')) {
	    gint trash;
	    gchar ifacename[16];
	    gchar *buf = buffer;
	    gint i;

	    net_dev = g_new0(NetDevice, 1);
	    net_dev->next = net;
	    net = net_dev;

	    buf = g_strstrip(buf);

	    memset(ifacename, 0, 16);

	    for (i = 0; buffer[i] != ':' && i < 16; i++) {
		ifacename[i] = buffer[i];
	    }

	    walk_until_inclusive(':');

	    /* iface: bytes packets errs drop fifo frame compressed multicast */
	    sscanf(buf, "%ld %ld %ld %d %d %d %d %d %ld %ld %ld",
		   &net_dev->recv_bytes, &net_dev->recv_packets,
		   &net_dev->recv_errors, &trash, &trash, &trash, &trash,
		   &trash, &net_dev->trans_bytes, &net_dev->trans_packets,
		   &net_dev->trans_errors);

	    g_print("%ld\n", net_dev->recv_bytes);

	    net_dev->iface = g_strdup(ifacename);
	}
    }
    fclose(proc_net);

    return net;
}