summaryrefslogtreecommitdiffstats
path: root/sysutils/gkrellm/files/patch-src::net.c
blob: 648dc036e5ca97219c933cbc7bbb77c06ab30c9d (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Index: src/net.c
diff -u src/net.c.orig src/net.c
--- src/net.c.orig	Mon Jul  1 11:17:27 2002
+++ src/net.c	Fri Jan  3 19:11:51 2003
@@ -120,10 +120,6 @@
 
 /* -------- FreeBSD / NetBSD / OpenBSD ------------------------------------ */
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
-#include <sys/sysctl.h>
-#include <net/if.h>
-#include <net/if_dl.h>
-#include <net/route.h>
 
 #define	LOCK_DIRECTORY		"/var/spool/lock"
 
@@ -133,6 +129,107 @@
 	{"ppp0", TIMER_TYPE_PPP }
 	};
 
+#if !(defined(__FreeBSD__) && __FreeBSD_version < 410000) && \
+    !(defined(__NetBSD__) && __NetBSD_version < 105000000) && \
+    !(defined(__OpenBSD__) && OpenBSD < 200006)
+#define HAVE_GETIFADDRS	1
+#endif
+
+#if defined(HAVE_GETIFADDRS)
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <ifaddrs.h>
+
+void
+read_bsd_net_data()
+	{
+	GList			*list;
+	NetMon			*net;
+	struct ifaddrs		*ifap, *ifa;
+	struct if_data		*ifd;
+
+	if (getifaddrs(&ifap) < 0)
+		return;
+
+	for (ifa = ifap; ifa; ifa = ifa->ifa_next)
+		{
+		if (ifa->ifa_flags & IFF_UP)
+			{
+			if (ifa->ifa_addr->sa_family != AF_LINK)
+				continue;
+			for (list = net_mon_list; list; list = list->next)
+				{
+				net = (NetMon *) list->data;
+				if (strcmp(net->name, ifa->ifa_name) == 0)
+					{
+					ifd = (struct if_data *)ifa->ifa_data;
+					net->rx = ifd->ifi_ibytes;
+					net->tx = ifd->ifi_obytes;
+					net->rxtx_units = NET_UNITS_BYTES;
+					break;
+					}
+				}
+			}
+		}
+
+	freeifaddrs(ifap);
+	}
+
+static void
+sync_bsd_net_interfaces()
+	{
+	GList			*list;
+	NetMon			*net;
+	struct ifaddrs		*ifap, *ifa;
+
+	for (list = net_mon_list; list; list = list->next)
+		{
+		net = (NetMon *) list->data;
+		net->old_state = net->state;
+		net->state = NET_DOWN;
+		}
+
+	if (getifaddrs(&ifap) < 0)
+		return;
+
+	for (ifa = ifap; ifa; ifa = ifa->ifa_next)
+		{
+		if (ifa->ifa_flags & IFF_UP)
+			{
+			if (ifa->ifa_addr->sa_family != AF_LINK)
+				continue;
+			for (list = net_mon_list; list; list = list->next)
+				{
+				net = (NetMon *) list->data;
+				if (strcmp(net->name, ifa->ifa_name) == 0)
+					{
+					net->state = NET_UP;
+					break;
+					}
+				}
+			if (list == NULL)
+				{
+				net = g_new0(NetMon, 1);
+				net->name = g_strdup(ifa->ifa_name);
+				net_mon_list = g_list_append(net_mon_list, net);
+				net->state = NET_UP;
+				net->old_state = NET_DOWN;
+				}
+			}
+		}
+
+	freeifaddrs(ifap);
+	}
+
+#else /* HAVE_GETIFADDRS */
+
+#include <sys/sysctl.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+
 static int	mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
 static char	*buf;
 static int	alloc;
@@ -289,6 +386,8 @@
 			}
 		}
 	}
+
+#endif /* HAVE_GETIFADDRS */
 #endif
 
 
OpenPOWER on IntegriCloud