summaryrefslogtreecommitdiff
path: root/src/get_eth_info.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2009-07-21 19:07:30 +0200
committerMichael Stapelberg <michael@stapelberg.de>2009-07-21 19:07:30 +0200
commit6fda988f360b3145d5772b6964f336dd652357ea (patch)
tree31647729cafbadf3d9e8213f148e8510d4e9dfed /src/get_eth_info.c
parent045c88dfe39c488340e0296e36ea785e9aa77e84 (diff)
Use own files for each function, add get_ipv6_addr.c
Diffstat (limited to 'src/get_eth_info.c')
-rw-r--r--src/get_eth_info.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/get_eth_info.c b/src/get_eth_info.c
new file mode 100644
index 0000000..7561421
--- /dev/null
+++ b/src/get_eth_info.c
@@ -0,0 +1,49 @@
+#include <string.h>
+#include <limits.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <net/if.h>
+
+#ifdef LINUX
+#include <linux/ethtool.h>
+#include <linux/sockios.h>
+#endif
+
+#include "i3status.h"
+
+/*
+ * Combines ethernet IP addresses and speed (if requested) for displaying
+ *
+ */
+const char *get_eth_info() {
+ static char part[512];
+ const char *ip_address = get_ip_addr(eth_interface);
+ int ethspeed = 0;
+
+ if (get_ethspeed) {
+#ifdef LINUX
+ /* This code path requires root privileges */
+ struct ifreq ifr;
+ struct ethtool_cmd ecmd;
+
+ ecmd.cmd = ETHTOOL_GSET;
+ (void)memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_data = (caddr_t)&ecmd;
+ (void)strcpy(ifr.ifr_name, eth_interface);
+ if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
+ ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
+ else get_ethspeed = false;
+#endif
+ }
+
+ if (ip_address == NULL)
+ (void)snprintf(part, sizeof(part), "E: down");
+ else {
+ if (get_ethspeed)
+ (void)snprintf(part, sizeof(part), "E: %s (%d Mbit/s)", ip_address, ethspeed);
+ else (void)snprintf(part, sizeof(part), "E: %s", ip_address);
+ }
+
+ return part;
+}