diff options
author | Leandro A. F. Pereira <leandro@hardinfo.org> | 2008-02-10 16:52:06 +0000 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2008-02-10 16:52:06 +0000 |
commit | 9cd3afcc20418ebc615de64f1bf76fc340b93f6f (patch) | |
tree | 50a19b42615d22b8ce4ec947f4fb07b34bc21879 /hardinfo2/arch/linux/common | |
parent | 1b64d687544bf0ba2f29e9c5699f1625b5b87119 (diff) |
Add wireless signal information (berlios developer patch #2317).
Diffstat (limited to 'hardinfo2/arch/linux/common')
-rw-r--r-- | hardinfo2/arch/linux/common/net.h | 255 |
1 files changed, 149 insertions, 106 deletions
diff --git a/hardinfo2/arch/linux/common/net.h b/hardinfo2/arch/linux/common/net.h index 78a9770d..f445430a 100644 --- a/hardinfo2/arch/linux/common/net.h +++ b/hardinfo2/arch/linux/common/net.h @@ -16,8 +16,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -static gchar *network_interfaces = NULL, - *network_icons = NULL; +static gchar *network_interfaces = NULL, *network_icons = NULL; #include <sys/ioctl.h> #include <net/if.h> @@ -27,7 +26,7 @@ static gchar *network_interfaces = NULL, #include <stdio.h> #include <unistd.h> -#include <string.h> /* for strncpy */ +#include <string.h> /* for strncpy */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> @@ -36,15 +35,15 @@ static gchar *network_interfaces = NULL, typedef struct _NetInfo NetInfo; struct _NetInfo { - char name[16]; - int mtu; + char name[16]; + int mtu; unsigned char mac[8]; char ip[16]; char mask[16]; char broadcast[16]; }; -void get_net_info(char *if_name, NetInfo *netinfo) +void get_net_info(char *if_name, NetInfo * netinfo) { struct ifreq ifr; int fd; @@ -58,41 +57,47 @@ void get_net_info(char *if_name, NetInfo *netinfo) /* MTU */ strcpy(ifr.ifr_name, if_name); if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) { - netinfo->mtu = 0; + netinfo->mtu = 0; } else { - netinfo->mtu = ifr.ifr_mtu; + netinfo->mtu = ifr.ifr_mtu; } /* HW Address */ strcpy(ifr.ifr_name, if_name); if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { - memset(netinfo->mac, 0, 8); + memset(netinfo->mac, 0, 8); } else { - memcpy(netinfo->mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, 8); + memcpy(netinfo->mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, 8); } - + /* IP Address */ strcpy(ifr.ifr_name, if_name); if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) { - netinfo->ip[0] = 0; + netinfo->ip[0] = 0; } else { - sprintf(netinfo->ip, "%s", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + sprintf(netinfo->ip, "%s", + inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)-> + sin_addr)); } - + /* Mask Address */ strcpy(ifr.ifr_name, if_name); if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0) { - netinfo->mask[0] = 0; + netinfo->mask[0] = 0; } else { - sprintf(netinfo->mask, "%s", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + sprintf(netinfo->mask, "%s", + inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)-> + sin_addr)); } /* Broadcast Address */ strcpy(ifr.ifr_name, if_name); if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0) { - netinfo->broadcast[0] = 0; + netinfo->broadcast[0] = 0; } else { - sprintf(netinfo->broadcast, "%s", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + sprintf(netinfo->broadcast, "%s", + inet_ntoa(((struct sockaddr_in *) &ifr.ifr_addr)-> + sin_addr)); } shutdown(fd, 0); @@ -103,38 +108,37 @@ static struct { char *label; char *icon; } netdev2type[] = { - { "eth", "Ethernet", "network" }, - { "lo", "Loopback", "network-generic" }, - { "ppp", "Point-to-Point", "modem" }, - { "ath", "Wireless", "wireless" }, - { "wlan", "Wireless", "wireless" }, - { "ra", "Wireless", "wireless" }, - { "wl", "Wireless", "wireless" }, - { "tun", "Virtual Point-to-Point (TUN)", "network-generic" }, - { "tap", "Ethernet (TAP)", "network-generic" }, - { "plip", "Parallel Line Internet Protocol", "network" }, - { "irlan", "Infrared", "network-generic" }, - { "slip", "Serial Line Internet Protocol", "network-generic" }, - { "isdn", "Integrated Services Digital Network", "modem" }, - { "sit", "IPv6-over-IPv4 Tunnel", "network-generic" }, + { "eth", "Ethernet", "network" }, + { "lo", "Loopback", "network-generic" }, + { "ppp", "Point-to-Point", "modem" }, + { "ath", "Wireless", "wireless" }, + { "wlan", "Wireless", "wireless" }, + { "ra", "Wireless", "wireless" }, + { "wl", "Wireless", "wireless" }, + { "tun", "Virtual Point-to-Point (TUN)", "network-generic" }, + { "tap", "Ethernet (TAP)", "network-generic" }, + { "plip", "Parallel Line Internet Protocol", "network" }, + { "irlan", "Infrared", "network-generic" }, + { "slip", "Serial Line Internet Protocol", "network-generic" }, + { "isdn", "Integrated Services Digital Network", "modem" }, + { "sit", "IPv6-over-IPv4 Tunnel", "network-generic" }, { "vmnet8", "VMWare Virtual Network Interface (NAT)", "computer" }, - { "vmnet", "VMWare Virtual Network Interface", "computer"}, - { "pan", "Personal Area Network (PAN)", "bluetooth"}, - { "bnep", "Bluetooth", "bluetooth"}, - { "br", "Bridge Interface", "network-generic" }, - { NULL, "Unknown", "network-generic" }, + { "vmnet", "VMWare Virtual Network Interface", "computer" }, + { "pan", "Personal Area Network (PAN)", "bluetooth" }, + { "bnep", "Bluetooth", "bluetooth" }, + { "br", "Bridge Interface", "network-generic" }, + { NULL, "Unknown", "network-generic" }, }; -static void -net_get_iface_type(gchar *name, gchar **type, gchar **icon) +static void net_get_iface_type(gchar * name, gchar ** type, gchar ** icon) { int i; - + for (i = 0; netdev2type[i].type; i++) { - if (g_str_has_prefix(name, netdev2type[i].type)) - break; + if (g_str_has_prefix(name, netdev2type[i].type)) + break; } - + *type = netdev2type[i].label; *icon = netdev2type[i].icon; } @@ -145,8 +149,7 @@ remove_net_devices(gpointer key, gpointer value, gpointer data) return g_str_has_prefix(key, "NET"); } -static void -scan_net_interfaces_24(void) +static void scan_net_interfaces_24(void) { FILE *proc_net; NetInfo ni; @@ -155,35 +158,41 @@ scan_net_interfaces_24(void) gdouble recv_bytes; gdouble recv_errors; gdouble recv_packets; - + gdouble trans_bytes; gdouble trans_errors; gdouble trans_packets; - + + /* Variables for wireless connections */ + gint wstatus; + gint wqlink, wqlevel, wqnoise; + if (!g_file_test("/proc/net/dev", G_FILE_TEST_EXISTS)) { - if (network_interfaces) { - g_free(network_interfaces); - network_interfaces = g_strdup("[Network Interfaces]\n" - "None found=\n"); - } + if (network_interfaces) { + g_free(network_interfaces); + network_interfaces = g_strdup("[Network Interfaces]\n" + "None found=\n"); + } return; } if (network_interfaces) { - g_free(network_interfaces); + g_free(network_interfaces); } - + if (network_icons) { - g_free(network_icons); + g_free(network_icons); } - + network_interfaces = g_strdup("[Network Interfaces]\n"); network_icons = g_strdup(""); proc_net = fopen("/proc/net/dev", "r"); while (fgets(buffer, 256, proc_net)) { if (strchr(buffer, ':')) { + gchar wbuf[256], *wdetailed = NULL; + FILE *wrls; gint trash; gchar ifacename[16]; gchar *buf = buffer; @@ -204,68 +213,102 @@ scan_net_interfaces_24(void) sscanf(buf, "%lf %lf %lf %d %d %d %d %d %lf %lf %lf", &recv_bytes, &recv_packets, &recv_errors, &trash, &trash, &trash, &trash, - &trash, &trans_bytes, &trans_packets, - &trans_errors); + &trash, &trans_bytes, &trans_packets, &trans_errors); - gdouble recv_mb = recv_bytes / 1048576.0; - gdouble trans_mb = trans_bytes / 1048576.0; - - get_net_info(ifacename, &ni); + gdouble recv_mb = recv_bytes / 1048576.0; + gdouble trans_mb = trans_bytes / 1048576.0; - devid = g_strdup_printf("NET%s", ifacename); - - ip = g_strdup_printf(" (%s)", ni.ip); - network_interfaces = h_strdup_cprintf("$%s$%s=Sent %.2lfMiB, received %.2lfMiB%s\n", - network_interfaces, - devid, - ifacename, - trans_mb, - recv_mb, - ni.ip[0] ? ip: ""); - g_free(ip); - - net_get_iface_type(ifacename, &iface_type, &iface_icon); + get_net_info(ifacename, &ni); + + devid = g_strdup_printf("NET%s", ifacename); + + ip = g_strdup_printf(" (%s)", ni.ip); + network_interfaces = + h_strdup_cprintf + ("$%s$%s=Sent %.2lfMiB, received %.2lfMiB%s\n", + network_interfaces, devid, ifacename, trans_mb, recv_mb, + ni.ip[0] ? ip : ""); + g_free(ip); + + net_get_iface_type(ifacename, &iface_type, &iface_icon); network_icons = h_strdup_cprintf("Icon$%s$%s=%s.png\n", - network_icons, devid, - ifacename, iface_icon); + network_icons, devid, + ifacename, iface_icon); + + if ((wrls = fopen("/proc/net/wireless", "r"))) { + while (fgets(wbuf, 256, wrls)) { + if (strchr(wbuf, ':') && strstr(wbuf, ifacename)) { + gchar *buf1 = wbuf; + + iface_type = "Wireless"; + iface_icon = "wireless"; + + buf1 = strchr(buf1, ':') + 1; + + if (strstr(buf1, ".")) { + sscanf(buf1, "%d %d. %d %d %d %d %d %d %d %d", + &wstatus, &wqlink, &wqlevel, &wqnoise, + &trash, &trash, &trash, &trash, &trash, + &trash); + } else { + sscanf(buf1, "%d %d %d %d %d %d %d %d %d %d", + &wstatus, &wqlink, &wqlevel, &wqnoise, + &trash, &trash, &trash, &trash, &trash, + &trash); + } + wdetailed = + g_strdup_printf("\n[Wireless Information]\n" + "Status=%d\n" + "Quality Level=%d%%\n" + "Signal Level=%d\n" + "Noise Level=%d\n", + wstatus, wqlink, wqlevel, + wqnoise); + } + } + fclose(wrls); + } + + detailed = g_strdup_printf("[Network Adapter Properties]\n" + "Interface Type=%s\n" + "Hardware Address (MAC)=%02x:%02x:%02x:%02x:%02x:%02x\n" + "MTU=%d\n" + "[Transfer Details]\n" + "Bytes Received=%.0lf (%.2fMiB)\n" + "Bytes Sent=%.0lf (%.2fMiB)\n", + iface_type, + ni.mac[0], ni.mac[1], + ni.mac[2], ni.mac[3], + ni.mac[4], ni.mac[5], + ni.mtu, + recv_bytes, recv_mb, + trans_bytes, trans_mb); - detailed = g_strdup_printf("[Network Adapter Properties]\n" - "Interface Type=%s\n" - "Hardware Address (MAC)=%02x:%02x:%02x:%02x:%02x:%02x\n" - "MTU=%d\n" - "[Transfer Details]\n" - "Bytes Received=%.0lf (%.2fMiB)\n" - "Bytes Sent=%.0lf (%.2fMiB)\n", - iface_type, - ni.mac[0], ni.mac[1], - ni.mac[2], ni.mac[3], - ni.mac[4], ni.mac[5], - ni.mtu, - recv_bytes, recv_mb, - trans_bytes, trans_mb); - - if (ni.ip[0] || ni.mask[0] || ni.broadcast[0]) { - detailed = h_strdup_cprintf("\n[Internet Protocol (IPv4)]\n" - "IP Address=%s\n" - "Mask=%s\n" - "Broadcast Address=%s\n", - detailed, - ni.ip[0] ? ni.ip : "Not set", - ni.mask[0] ? ni.mask : "Not set", - ni.broadcast[0] ? ni.broadcast : "Not set"); + if (wdetailed) { + detailed = h_strconcat(detailed, wdetailed, NULL); } - - g_hash_table_insert(moreinfo, devid, detailed); + + if (ni.ip[0] || ni.mask[0] || ni.broadcast[0]) { + detailed = + h_strdup_cprintf("\n[Internet Protocol (IPv4)]\n" + "IP Address=%s\n" "Mask=%s\n" + "Broadcast Address=%s\n", detailed, + ni.ip[0] ? ni.ip : "Not set", + ni.mask[0] ? ni.mask : "Not set", + ni.broadcast[0] ? ni. + broadcast : "Not set"); + } + + g_hash_table_insert(moreinfo, devid, detailed); } } fclose(proc_net); } -static void -scan_net_interfaces(void) +static void scan_net_interfaces(void) { /* FIXME: See if we're running Linux 2.6 and if /sys is mounted, then use - that instead of /proc/net/dev */ + that instead of /proc/net/dev */ /* remove old devices from global device table */ g_hash_table_foreach_remove(moreinfo, remove_net_devices, NULL); |