-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_mod.c
116 lines (101 loc) · 3.38 KB
/
ip_mod.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <sys/socket.h>
#include <ev.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <linux/wireless.h>
#include <fcntl.h>
#define MAXLEN (256)
// TODO custom prefix, maybe custom icons?
char output[MAXLEN] = "X ";
char cur_host[MAXLEN];
char wifi_icon[] = "\uf1eb ";
char eth_icon[] = "ETH ";
struct pack {
char *iface;
char *gateway;
uint16_t metric;
};
// https://gist.github.com/edufelipe/6108057
int check_wireless(const char* ifname) {
int sock = -1;
struct iwreq pwrq;
memset(&pwrq, 0, sizeof(pwrq));
strncpy(pwrq.ifr_name, ifname, IFNAMSIZ);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
return 0;
}
if (ioctl(sock, SIOCGIWNAME, &pwrq) != -1) {
close(sock);
return 1;
}
close(sock);
return 0;
}
// TODO I doubt i'll ever use IPv6 on local network, so it probably doesn't really need to support it
// But having a toggle might be nice anyway
int get_IP_for_iface(char *iface) {
memset(cur_host, '\0', MAXLEN);
struct ifaddrs *ifaddr, *tmp;
if (getifaddrs(&ifaddr) == -1) return 0;
for (tmp = ifaddr; tmp != NULL; tmp = tmp->ifa_next) {
if (tmp->ifa_name == NULL || strcmp(iface, tmp->ifa_name) != 0) continue;
if (tmp->ifa_addr->sa_family == AF_INET) {
getnameinfo(tmp->ifa_addr, sizeof(struct addrinfo), cur_host, sizeof(cur_host), NULL, 0, NI_NUMERICHOST);
return 1;
}
}
return 0;
}
int gateway_not_NULL(char *gateway)
{
for (int i = 0; i < (int) strnlen(gateway, MAXLEN); i++)
// not the cleanest, but it works
if (gateway[i] > '0') return 1;
return 0;
}
void get_addresses() {
memset(output, '\0', MAXLEN);
output[0] = 'X', output[1] = ' ';
FILE *f = fopen("/proc/net/route", "r");
if (!f) return;
struct pack pack_[2] = {{ "", "", UINT16_MAX }, { "", "", UINT16_MAX }}; // only saving 2, wireless and wired
char iface[MAXLEN], gateway[MAXLEN], metric[MAXLEN], tmp[MAXLEN];
fscanf(f, "%s%s%s%s%s%s%s%s%s%s%s\n", iface, tmp, gateway, tmp, tmp, tmp, metric, tmp, tmp, tmp, tmp);
while(fscanf(f, "%s%s%s%s%s%s%s%s%s%s%s\n", iface, tmp, gateway, tmp, tmp, tmp, metric, tmp, tmp, tmp, tmp) == 11) {
if (gateway_not_NULL(gateway)) {
int wifi_bool = check_wireless(iface);
if (pack_[wifi_bool].metric < atoi(metric)) continue; // saving best metric
pack_[wifi_bool].iface = strndup(iface, MAXLEN);
pack_[wifi_bool].gateway = strndup(gateway, MAXLEN);
pack_[wifi_bool].metric = atoi(metric);
}
}
for (int i = 0; i < 2; i++) {
//printf("%d: (%s,%s,%d)\tggetwifi? %d\n", i, pack_[i].gateway, pack_[i].iface, pack_[i].metric, check_wireless(pack_[i].iface));
if (pack_[i].metric == 1000) continue;
if (strnlen(output, MAXLEN) != 2) strcat(output, " | ");
strncat(output, i % 2 == 0 ? eth_icon : wifi_icon , MAXLEN);
strncat(output, get_IP_for_iface(pack_[i].iface) ? cur_host : "NULL", MAXLEN); // get_IP_for_iface saves ret value in global var cur_host
}
fclose(f);
// TODO customize what gets printed if none of the interfaces is up, defaults to wifi for now.
strnlen(output, MAXLEN) == 2 ?
printf("%s%s%s\n", "X ", wifi_icon, "NULL") : printf("%s\n", output);
fflush(stdout);
}
int main() {
get_addresses();
struct ev_loop *loop = EV_DEFAULT;
ev_stat something;
ev_stat_init (&something, get_addresses, "/proc/net/route", 0.);
ev_stat_start (loop, &something);
ev_run (loop, 0);
return 1;
}