1 /*
2    This file is part of LibGTop 2.0.
3 
4    LibGTop is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License,
7    or (at your option) any later version.
8 
9    LibGTop is distributed in the hope that it will be useful, but WITHOUT
10    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12    for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with LibGTop; see the file COPYING. If not, write to the
16    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #include <config.h>
21 #include <unistd.h>
22 
23 #include <glibtop.h>
24 #include <glibtop/error.h>
25 #include <glibtop/netload.h>
26 
27 #include <glibtop_suid.h>
28 
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #include <net/if_dl.h>
34 #include <net/route.h>
35 
36 static const unsigned long _glibtop_sysdeps_netload =
37 (1L << GLIBTOP_NETLOAD_IF_FLAGS) +
38 (1L << GLIBTOP_NETLOAD_MTU) +
39 /* (1L << GLIBTOP_NETLOAD_SUBNET) + */
40 /* (1L << GLIBTOP_NETLOAD_ADDRESS) + */
41 (1L << GLIBTOP_NETLOAD_PACKETS_IN) +
42 (1L << GLIBTOP_NETLOAD_PACKETS_OUT) +
43 (1L << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
44 (1L << GLIBTOP_NETLOAD_BYTES_IN) +
45 (1L << GLIBTOP_NETLOAD_BYTES_OUT) +
46 (1L << GLIBTOP_NETLOAD_BYTES_TOTAL) +
47 (1L << GLIBTOP_NETLOAD_ERRORS_IN) +
48 (1L << GLIBTOP_NETLOAD_ERRORS_OUT) +
49 (1L << GLIBTOP_NETLOAD_ERRORS_TOTAL) +
50 (1L << GLIBTOP_NETLOAD_COLLISIONS);
51 
52 /* Init function. */
53 
54 void
_glibtop_init_netload_p(glibtop * server)55 _glibtop_init_netload_p (glibtop *server)
56 {
57 	server->sysdeps.netload = _glibtop_sysdeps_netload;
58 }
59 
60 /* Provides Network statistics. */
61 
62 void
glibtop_get_netload_p(glibtop * server,glibtop_netload * buf,const char * interface)63 glibtop_get_netload_p (glibtop *server, glibtop_netload *buf,
64 		       const char *interface)
65 {
66 	int mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
67 	size_t bufsize;
68 	char *rtbuf, *ptr, *eob;
69 	struct if_msghdr *ifm;
70 
71 	glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_NETLOAD), 0);
72 
73 	memset (buf, 0, sizeof (glibtop_netload));
74 
75 	if (sysctl (mib, 6, NULL, &bufsize, NULL, 0) < 0)
76 		return;
77 
78 	rtbuf = (char*)g_malloc (bufsize);
79 	if (rtbuf == NULL)
80 		return;
81 
82 	if (sysctl (mib, 6, rtbuf, &bufsize, NULL, 0) < 0) {
83 		g_free (rtbuf);
84 		return;
85 	}
86 
87 	eob = rtbuf + bufsize;
88 	ptr = rtbuf;
89 	while (ptr < eob) {
90 		struct sockaddr_dl *sdl;
91 
92 		ifm = (struct if_msghdr*) ptr;
93 
94 		if (ifm->ifm_type != RTM_IFINFO)
95 			break;
96 		ptr += ifm->ifm_msglen;
97 
98 		while (ptr < eob) {
99 			struct if_msghdr *nextifm = (struct if_msghdr*) ptr;
100 
101 			if (nextifm->ifm_type != RTM_NEWADDR)
102 				break;
103 			ptr += nextifm->ifm_msglen;
104 		}
105 
106 		sdl = (struct sockaddr_dl*) (ifm + 1);
107 		if (sdl->sdl_family != AF_LINK)
108 			continue;
109 		if (strlen (interface) != sdl->sdl_nlen)
110 			continue;
111 		if (strcmp (interface, sdl->sdl_data) == 0)
112 			goto FOUND;
113 	}
114 	g_free (rtbuf);
115 	return;
116 
117 FOUND:
118 	if (ifm->ifm_flags & IFF_UP)
119 		buf->if_flags |= GLIBTOP_IF_FLAGS_UP;
120 	if (ifm->ifm_flags & IFF_BROADCAST)
121 		buf->if_flags |= GLIBTOP_IF_FLAGS_BROADCAST;
122 	if (ifm->ifm_flags & IFF_DEBUG)
123 		buf->if_flags |= GLIBTOP_IF_FLAGS_DEBUG;
124 	if (ifm->ifm_flags & IFF_LOOPBACK)
125 		buf->if_flags |= GLIBTOP_IF_FLAGS_LOOPBACK;
126 	if (ifm->ifm_flags & IFF_POINTOPOINT)
127 		buf->if_flags |= GLIBTOP_IF_FLAGS_POINTOPOINT;
128 	if (ifm->ifm_flags & IFF_RUNNING)
129 		buf->if_flags |= GLIBTOP_IF_FLAGS_RUNNING;
130 	if (ifm->ifm_flags & IFF_NOARP)
131 		buf->if_flags |= GLIBTOP_IF_FLAGS_NOARP;
132 	if (ifm->ifm_flags & IFF_NOARP)
133 		buf->if_flags |= GLIBTOP_IF_FLAGS_PROMISC;
134 	if (ifm->ifm_flags & IFF_ALLMULTI)
135 		buf->if_flags |= GLIBTOP_IF_FLAGS_ALLMULTI;
136 	if (ifm->ifm_flags & IFF_OACTIVE)
137 		buf->if_flags |= GLIBTOP_IF_FLAGS_OACTIVE;
138 	if (ifm->ifm_flags & IFF_SIMPLEX)
139 		buf->if_flags |= GLIBTOP_IF_FLAGS_SIMPLEX;
140 	if (ifm->ifm_flags & IFF_LINK0)
141 		buf->if_flags |= GLIBTOP_IF_FLAGS_LINK0;
142 	if (ifm->ifm_flags & IFF_LINK1)
143 		buf->if_flags |= GLIBTOP_IF_FLAGS_LINK1;
144 	if (ifm->ifm_flags & IFF_LINK2)
145 		buf->if_flags |= GLIBTOP_IF_FLAGS_LINK2;
146 	if (ifm->ifm_flags & IFF_ALTPHYS)
147 		buf->if_flags |= GLIBTOP_IF_FLAGS_ALTPHYS;
148 	if (ifm->ifm_flags & IFF_MULTICAST)
149 		buf->if_flags |= GLIBTOP_IF_FLAGS_MULTICAST;
150 	buf->mtu		= ifm->ifm_data.ifi_mtu;
151 	buf->subnet		= 0; /* FIXME */
152 	buf->address		= 0; /* FIXME */
153 	buf->packets_in		= ifm->ifm_data.ifi_ipackets;
154 	buf->packets_out	= ifm->ifm_data.ifi_opackets;
155 	buf->packets_total	= buf->packets_in + buf->packets_out;
156 	buf->bytes_in		= ifm->ifm_data.ifi_ibytes;
157 	buf->bytes_out		= ifm->ifm_data.ifi_obytes;
158 	buf->bytes_total	= buf->bytes_in + buf->bytes_out;
159 	buf->errors_in		= ifm->ifm_data.ifi_ierrors;
160 	buf->errors_out		= ifm->ifm_data.ifi_oerrors;
161 	buf->errors_total	= buf->errors_in + buf->errors_out;
162 	buf->collisions		= ifm->ifm_data.ifi_collisions;
163 	buf->flags		= _glibtop_sysdeps_netload;
164 	g_free (buf);
165 }
166