summaryrefslogtreecommitdiff
path: root/net/gnome-nettool/files/patch-src_info.c
blob: d5602ae5be80e8e311b31762f07da20b59118bed (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
--- src/info.c.orig	Fri Jun 11 07:14:17 2004
+++ src/info.c	Tue Jul  6 01:19:20 2004
@@ -37,6 +37,9 @@
 #include <sys/ioctl.h>
 #include <stdlib.h>
 #include <net/if.h>
+#ifdef __FreeBSD__
+#include <net/if_media.h>
+#endif
 
 #include "info.h"
 #include "utils.h"
@@ -57,10 +60,11 @@
 	{ N_("Ethernet Interface"),      INFO_INTERFACE_ETH,     "16_ethernet.xpm", "eth",        NULL },
 	{ N_("Wireless Interface"),      INFO_INTERFACE_WLAN,    "wavelan-16.png",  "wlan",       NULL },
 	{ N_("Modem Interface"),         INFO_INTERFACE_PPP,     "16_ppp.xpm",      "ppp",        NULL },
+	{ N_("Modem Interface"),         INFO_INTERFACE_PPP,     "16_ppp.xpm",      "tun",        NULL },
 	{ N_("Parallel Line Interface"), INFO_INTERFACE_PLIP,    "16_plip.xpm",     "plip",       NULL },
 	{ N_("Infrared Interface"),      INFO_INTERFACE_IRLAN,   "irda-16.png",     "irlan",      NULL },
 	{ N_("Loopback Interface"),      INFO_INTERFACE_LO,      "16_loopback.xpm", "lo",         NULL },
-	{ N_("Unknown Interface"),       INFO_INTERFACE_UNKNOWN, "network.png",     NULL,         NULL },
+	{ N_("Unknown Interface"),       INFO_INTERFACE_UNKNOWN, "network.png",     "",         NULL },
 	{ NULL,                          INFO_INTERFACE_UNKNOWN,  NULL,             NULL,         NULL }
 };
 
@@ -97,9 +101,39 @@
 {
 	gint i;
 	gchar *path;
-	
+	gchar *dev_type = NULL;
+#if defined(__FreeBSD__)
+	int s;
+	struct ifmediareq ifmr;
+
+	if ((s = socket (AF_INET, SOCK_DGRAM, 0)) > -1) {
+
+		(void) memset (&ifmr, 0, sizeof (ifmr));
+		(void) strncpy (ifmr.ifm_name, dev_name, sizeof (ifmr.ifm_name));
+
+		if (ioctl (s, SIOCGIFMEDIA, (caddr_t) &ifmr) > -1) {
+			switch (IFM_TYPE (ifmr.ifm_active)) {
+				case IFM_ETHER:
+					dev_type = "eth";
+					break;
+				case IFM_FDDI:
+				case IFM_TOKEN:
+					dev_type = "other_type";
+					break;
+				case IFM_IEEE80211:
+					dev_type = "wlan";
+					break;
+			}
+		}
+		close (s);
+	}
+#endif /* defined(__FreeBSD__) */
+
+	if (!dev_type)
+		dev_type = (gchar *) dev_name;
+
 	for (i = 0; info_iface_desc[i].name; i++)
-		if (strstr (dev_name, info_iface_desc[i].prefix) == dev_name) {
+		if (strstr (dev_type, info_iface_desc[i].prefix) == dev_type) {
 			(*iface) = g_strdup_printf ("%s (%s)", info_iface_desc[i].name, dev_name);
 			if (info_iface_desc[i].pixbuf == NULL) {
 				path = g_build_filename (PIXMAPS_DIR, info_iface_desc[i].icon, NULL);
@@ -187,17 +221,26 @@
 	gchar tx[10], tx_error[10], tx_drop[10], tx_ovr[10]; 
 	*/
 	gchar iface[30]; /*, flags[30]; */
-	gchar rx_bytes[16], rx_pkt[10], rx_error[10], rx_drop[10], rx_fifo[10];
-	gchar frame[10], compressed[10], multicast[10]; 
-	gchar tx_bytes[16], tx_pkt[10], tx_error[10], tx_drop[10], tx_fifo[10];
+	gchar rx_bytes[16], rx_pkt[10], rx_error[10];
+	gchar tx_bytes[16], tx_pkt[10], tx_error[10];
 	gchar collissions[10];
+#if defined(__linux__)
+	gchar rx_drop[10], rx_fifo[10];
+	gchar frame[10], compressed[10], multicast[10];
+	gchar tx_drop[10], tx_fifo[10];
+#elif defined(__FreeBSD__)
+	char *p;
+	gchar **tokens, **argv;
+	int i;
+	int pipe_out;
+#endif
 
 	GIOChannel *io = NULL;
 	gchar *line;
 	gboolean title = TRUE;
 	const gchar *text;
 	gchar *text_tx_bytes, *text_rx_bytes;
-		
+
 	g_return_val_if_fail (info != NULL, FALSE);
 
 	model = gtk_combo_box_get_model (GTK_COMBO_BOX (info->combo));
@@ -206,21 +249,61 @@
 	else
 		return FALSE;
 	/*text = gtk_entry_get_text (GTK_ENTRY (info->nic));*/
-	
+
 #if defined(__linux__)
 	io = g_io_channel_new_file ("/proc/net/dev", "r", NULL);
-	
+#elif defined(__FreeBSD__)
+	if (!g_shell_parse_argv ("/usr/bin/netstat -in -b -f link", NULL, &argv, NULL)) {
+		return FALSE;
+	}
+	if (!g_spawn_async_with_pipes (NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &pipe_out, NULL, NULL)) {
+		g_strfreev (argv);
+		return FALSE;
+	}
+
+	g_strfreev (argv);
+
+	io = g_io_channel_unix_new (pipe_out);
+#endif /* defined(__linux__) */
+
 	while (g_io_channel_read_line (io, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
 		if (title) {
 			title = FALSE;
 			g_free (line);
 			continue;
 		}
+#if defined(__linux__)
 		line = g_strdelimit (line, ":", ' ');
 		sscanf (line, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", iface,
 			rx_bytes, rx_pkt, rx_error, rx_drop, rx_fifo, frame, 
 		    compressed, multicast, 
 		    tx_bytes, tx_pkt, tx_error, tx_drop, tx_fifo, collissions);
+#elif defined(__FreeBSD__)
+		tokens = (gchar **) g_malloc0 (sizeof (gchar *) * 12);
+		p = strtok (line, " \t\n");
+		/* We only care about 11 fields for now */
+		for (i = 0; i < 11 && p; i++, p = strtok (NULL, " \t\n")) {
+			tokens[i] = g_strdup (p);
+		}
+
+		if (i == 0 || i < 10) {
+			g_free (line);
+			g_strfreev (tokens);
+			continue;
+		}
+
+		g_strlcpy (iface, tokens[0], sizeof (iface));
+
+		g_strlcpy (collissions, tokens[--i], sizeof (collissions));
+		g_strlcpy (tx_bytes, tokens[--i], sizeof (tx_bytes));
+		g_strlcpy (tx_error, tokens[--i], sizeof (tx_error));
+		g_strlcpy (tx_pkt, tokens[--i], sizeof (tx_pkt));
+		g_strlcpy (rx_bytes, tokens[--i], sizeof (rx_bytes));
+		g_strlcpy (rx_error, tokens[--i], sizeof (rx_error));
+		g_strlcpy (rx_pkt, tokens[--i], sizeof (rx_pkt));
+
+		g_strfreev (tokens);
+#endif /* defined(__linux__) */
 
 		if (g_ascii_strcasecmp (iface, text) == 0) {
 			/*
@@ -248,7 +331,9 @@
 	}
 	
 	g_io_channel_unref (io);
-#endif /* defined(__linux__) */
+#if defined(__FreeBSD__)
+	close (pipe_out);
+#endif /* defined(__FreeBSD__) */
 
 	return TRUE;
 }
@@ -384,6 +469,7 @@
 	mii_data_result data;
 
 	getifaddrs (&ifa0);
+	memset (&data, 0, sizeof (data));
 
 	for (ifr6 = ifa0; ifr6; ifr6 = ifr6->ifa_next) {
 		if (strcmp (ifr6->ifa_name, nic) != 0) {
@@ -425,7 +511,9 @@
 			ifc.ifc_req = (struct ifreq *) buf;
 			ioctl (sockfd, SIOCGIFCONF, &ifc);
 
+#if defined(__linux__)
 			data = mii_get_basic (nic);
+#endif /* defined(__linux__) */
 
 			for (ptr = buf; ptr < buf + ifc.ifc_len;) {
 				ifr = (struct ifreq *) ptr;