forked from Atoptool/atop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifprop.c
279 lines (236 loc) · 6.12 KB
/
ifprop.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** ==========================================================================
** Author: Gerlof Langeveld
** E-mail: [email protected]
** Date: January 2007
** --------------------------------------------------------------------------
** Copyright (C) 2007-2010 Gerlof Langeveld
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/in.h>
#include <dirent.h>
typedef __u64 u64;
typedef __u32 u32;
typedef __u16 u16;
typedef __u8 u8;
#include <linux/ethtool.h>
#include <linux/wireless.h>
#ifndef SPEED_UNKNOWN
#define SPEED_UNKNOWN -1
#endif
#define MAXVIRTIF 128
#include "atop.h"
#include "ifprop.h"
#include "photosyst.h"
static struct ifprop ifprops[MAXINTF];
char *virt_if[MAXVIRTIF];
/*
** function searches for the properties of a particular interface
** the interface name should be filled in the struct ifprop before
** calling this function
**
** return value reflects true or false
*/
int
getifprop(struct ifprop *ifp)
{
register int i;
for (i=0; ifprops[i].name[0]; i++)
{
if (strcmp(ifp->name, ifprops[i].name) == 0)
{
*ifp = ifprops[i];
return 1;
}
}
ifp->type = '?';
ifp->speed = 0;
ifp->fullduplex = 0;
return 0;
}
/*
** function stores properties of all interfaces in a static
** table to be queried later on
**
** this function should be called with superuser privileges!
*/
void
initifprop(void)
{
FILE *fp;
char *cp, linebuf[2048];
int i=0, sockfd, j=0, virt_num=0;
#ifdef ETHTOOL_GLINKSETTINGS
struct ethtool_link_settings ethlink; // preferred!
#endif
struct ethtool_cmd ethcmd; // deprecated
struct ifreq ifreq;
struct iwreq iwreq;
unsigned long speed;
unsigned char duplex, ethernet;
DIR *dirp;
struct dirent *dentry;
/*
** read /sys/devices/virtual/net/xxx to obtain all
** virtual interfaces' name
*/
if ( (dirp = opendir("/sys/devices/virtual/net")) )
{
while ( (dentry = readdir(dirp)) )
{
if (dentry->d_name[0] == '.')
continue;
virt_if[j] = (char *)malloc(64);
strncpy(virt_if[j], dentry->d_name, 15);
j++;
}
virt_num = j;
}
closedir(dirp);
/*
** open /proc/net/dev to obtain all interface names and open
** a socket to determine the properties for each interface
*/
if ( (fp = fopen("/proc/net/dev", "r")) == NULL)
return;
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
fclose(fp);
return;
}
/*
** read every name and obtain properties
*/
while ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
/*
** skip lines containing a '|' symbol (headers)
*/
if ( strchr(linebuf, '|') != NULL)
continue;
if ( (cp = strchr(linebuf, ':')) != NULL)
*cp = ' '; /* subst ':' by space */
sscanf(linebuf, "%15s", ifprops[i].name);
/*
** determine properties of ethernet interface
** preferably with actual struct ethtool_link_settings,
** otherwise with deprecated struct ethtool_cmd
*/
memset(&ifreq, 0, sizeof ifreq);
strncpy((void *)&ifreq.ifr_ifrn.ifrn_name, ifprops[i].name,
sizeof ifreq.ifr_ifrn.ifrn_name-1);
ethernet = speed = duplex = 0;
#ifdef ETHTOOL_GLINKSETTINGS
ethlink.cmd = ETHTOOL_GLINKSETTINGS;
ifreq.ifr_ifru.ifru_data = (void *)ðlink;
if ( ioctl(sockfd, SIOCETHTOOL, &ifreq) == 0)
{
if (ethlink.link_mode_masks_nwords <= 0)
{
ethlink.link_mode_masks_nwords = - ethlink.link_mode_masks_nwords;
if ( ioctl(sockfd, SIOCETHTOOL, &ifreq) != 0 )
continue;
}
ethernet = 1;
speed = ethlink.speed;
duplex = ethlink.duplex;
}
else
#endif
{
memset(ðcmd, 0, sizeof ethcmd);
ethcmd.cmd = ETHTOOL_GSET;
ifreq.ifr_ifru.ifru_data = (void *)ðcmd;
if ( ioctl(sockfd, SIOCETHTOOL, &ifreq) == 0)
{
ethernet = 1;
speed = ethcmd.speed;
duplex = ethcmd.duplex;
}
}
if (ethernet)
{
ifprops[i].type = 'e'; // type ethernet
if (speed == (u32)SPEED_UNKNOWN)
ifprops[i].speed = 0;
else
ifprops[i].speed = speed;
switch (duplex)
{
case DUPLEX_FULL:
ifprops[i].fullduplex = 1;
break;
default:
ifprops[i].fullduplex = 0;
}
for (j = 0; j < virt_num; j++)
{
if ( strcmp(ifprops[i].name, virt_if[j]) == EQ ) // virtual interface?
{
ifprops[i].type = '?'; // set type unknown
ifprops[i].speed = 0;
ifprops[i].fullduplex = 0;
break;
}
}
if (++i >= MAXINTF-1)
break;
else
continue;
}
/*
** determine properties of wireless interface
*/
memset(&iwreq, 0, sizeof iwreq);
strncpy(iwreq.ifr_ifrn.ifrn_name, ifprops[i].name,
sizeof iwreq.ifr_ifrn.ifrn_name-1);
if ( ioctl(sockfd, SIOCGIWRATE, &iwreq) == 0)
{
ifprops[i].type = 'w'; // type wireless
ifprops[i].fullduplex = 0;
ifprops[i].speed =
(iwreq.u.bitrate.value + 500000) / 1000000;
if (++i >= MAXINTF-1)
break;
else
continue;
}
ifprops[i].type = '?'; // type unknown
ifprops[i].speed = 0;
ifprops[i].fullduplex = 0;
if (++i >= MAXINTF-1)
break;
}
for (j = virt_num; j > 0; j--)
free(virt_if[j - 1]);
close(sockfd);
fclose(fp);
}