• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • sensors
network.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson *
3  * karlsson.h@home.se *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "network.h"
12 
13 #include <QTextStream>
14 #include <QNetworkInterface>
15 
16 #include <KDebug>
17 
18 #ifdef __FreeBSD__
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/socket.h>
22 #include <net/route.h>
23 #endif
24 
25 NetworkSensor::NetworkSensor(const QString &dev, int interval): Sensor(interval)
26 {
27  device = dev.toLower();
28 
29 #ifdef __FreeBSD__
30  /* Determine number of interfaces */
31  u_int n = 0;
32  size_t nlen = 0;
33  nlen = sizeof(n);
34  sysctlbyname("net.link.generic.system.ifcount", &n, &nlen, NULL, 0);
35 
36  size_t if_miblen = 0;
37  if_miblen = sizeof(if_mib);
38  static int name[] = {
39  CTL_NET,
40  PF_LINK,
41  NETLINK_GENERIC,
42  IFMIB_IFDATA,
43  0,
44  IFDATA_GENERAL
45  };
46 
47  /*
48  If the device was defined by the theme, find the right devicenumber.
49  If not, use the device that holds the default route.
50  */
51 
52  if_number = -1;
53  int if_gw = -1;
54 
55  for (int i = 1; i <= n; ++i) {
56  name[4] = i;
57  /* Get data for iface-number i */
58  sysctl(name, 6, (void*)&if_mib, (size_t*)&if_miblen, (void*)NULL, (size_t)0);
59 
60  /* We found the right interface? */
61  if (QString(if_mib.ifmd_name) == device) {
62  if_number = i;
63  break;
64  }
65 
66  /* Does the interface hold the default route? */
67  if (if_mib.ifmd_flags & RTF_GATEWAY)
68  if_gw = i;
69  }
70 
71  if ((if_number == -1) && (if_gw != -1))
72  if_number = if_gw;
73 #else
74  if (device.isEmpty()) {
75  device = DEFAULT_DEVICE;
76  }
77 #endif
78  interfaceList = device.split('|', QString::SkipEmptyParts);
79  refreshDevice();
80  getIPAddress();
81 
82  getInOutBytes(receivedBytes, transmittedBytes);
83  netTimer.start();
84 
85 }
86 
87 void NetworkSensor::refreshDevice() {
88  int rank = interfaceList.count();
89  //TODO: what about BSD?
90  QFile file ("/proc/net/dev");
91 
92  if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
93  QTextStream t(&file); // use a text stream
94  t.readLine(); // reads: "Inter-| Receive..."
95  t.readLine(); // reads: " face |bytes "
96  QString line = t.readLine(); // finally reads something we care about
97 
98  QStringList::ConstIterator listEnd(interfaceList.constEnd());
99  while ((rank != 0) && (line != 0)) {
100  int i = 0;
101  for (QStringList::ConstIterator dev = interfaceList.constBegin();
102  (dev != listEnd) && (i < rank);
103  ++dev, ++i)
104  {
105  if (line.contains(*dev)) {
106  device = (*dev);
107  //deviceLine = line;
108  rank = i;
109  }
110  }
111  line = t.readLine();
112  }
113  file.close();
114  }
115 
116  if (rank >= interfaceList.count()) {
117  device = NO_DEVICE;
118  //deviceLine = "";
119  }
120 }
121 
122 NetworkSensor::~NetworkSensor()
123 {
124 }
125 
126 void NetworkSensor::getInOutBytes(unsigned long &in, unsigned long &out) const
127 {
128 #ifdef __FreeBSD__
129  if (if_number != -1) {
130  size_t if_miblen = 0;
131  if_miblen = sizeof(if_mib);
132  int name[] = { CTL_NET,
133  PF_LINK,
134  NETLINK_GENERIC,
135  IFMIB_IFDATA,
136  if_number,
137  IFDATA_GENERAL };
138 
139  sysctl(name, 6, (void*)&if_mib, (size_t*)&if_miblen, (void*)NULL, (size_t)0);
140 
141  in = if_mib.ifmd_data.ifi_ibytes;
142  out = if_mib.ifmd_data.ifi_obytes;
143  } else {
144  in = 0;
145  out = 0;
146  }
147 #else
148  QFile file("/proc/net/dev");
149  QString line;
150  if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
151  QTextStream t(&file); // use a text stream
152  line = t.readLine();
153 
154  while (line != 0 && !line.contains(device)) {
155  line = t.readLine();
156  }
157 
158  if (line.contains(device)) {
159  QRegExp rx("\\W*" + device + ":\\D*(\\d+)(?:\\D+\\d+){7}\\D+(\\d+)", Qt::CaseInsensitive);
160  rx.indexIn(line);
161  in = rx.cap(1).toULong();
162  out = rx.cap(2).toULong();
163  } else {
164  kDebug() << "Network sensor: can not find " << device ;
165  in = 0;
166  out = 0;
167  }
168  file.close();
169  }
170 #endif
171 }
172 
173 void NetworkSensor::getIPAddress()
174 {
175  const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
176 
177  foreach (const QNetworkInterface &interface, interfaces) {
178  if (device == interface.name()) {
179  QList<QNetworkAddressEntry> entries = interface.addressEntries();
180  if (entries.count() > 0) {
181  ipAddress = entries[0].ip().toString();
182  }
183  }
184  }
185 #if 0
186  struct ifaddrs *ifa = NULL, *ifp = NULL;
187 
188  if (getifaddrs (&ifp) < 0) {
189  kDebug() << "getifaddrs failed" ;
190  ipAddress = NO_IP;
191  return;
192  }
193 
194  for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
195  char ip[200];
196  socklen_t salen;
197 
198  if (ifa->ifa_addr->sa_family == AF_INET) {
199  salen = sizeof (struct sockaddr_in);
200  } else if (ifa->ifa_addr->sa_family == AF_INET6) {
201  //continue since this doesn't support IPv6 at this point
202  continue;
203  //salen = sizeof (struct sockaddr_in6);
204  } else {
205  continue;
206  }
207 
208  if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) < 0) {
209  kDebug() << "getnameinfo < 0" ;
210  continue;
211  }
212 
213  if (device == QString::fromLatin1(ifa->ifa_name)) {
214  ipAddress = QString::fromLatin1(ip);
215  freeifaddrs(ifp);
216  return;
217  }
218  }
219 
220  freeifaddrs(ifp);
221  ipAddress = NO_IP;
222 #endif
223 }
224 
225 void NetworkSensor::update()
226 {
227  SensorParams *sp;
228  Meter *meter;
229  QString format;
230  int decimals;
231 
232  unsigned long inB, outB;
233  const double delay = (double) netTimer.elapsed(); // msec elapsed since last update
234  refreshDevice();
235  getIPAddress();
236  getInOutBytes(inB, outB);
237  netTimer.restart();
238 
239  QObject *object;
240  foreach(object, *objList) {
241  sp = (SensorParams*)(object);
242  meter = sp->getMeter();
243  format = sp->getParam("FORMAT");
244  decimals = (sp->getParam("DECIMALS")).toInt();
245  if (format.length() == 0) {
246  format = "%in";
247  }
248 
249  format.replace(QRegExp("%inkb", Qt::CaseInsensitive),
250  QString::number(((inB - receivedBytes)*8) / delay, 'f', decimals));
251 
252  format.replace(QRegExp("%in", Qt::CaseInsensitive),
253  QString::number((inB - receivedBytes) / delay, 'f', decimals));
254 
255  format.replace(QRegExp("%outkb", Qt::CaseInsensitive),
256  QString::number(((outB - transmittedBytes)*8) / delay, 'f', decimals));
257 
258  format.replace(QRegExp("%out", Qt::CaseInsensitive),
259  QString::number((outB - transmittedBytes) / delay, 'f', decimals));
260 
261  format.replace(QRegExp("%dev", Qt::CaseInsensitive), device);
262 
263  format.replace(QRegExp("%ip", Qt::CaseInsensitive), ipAddress);
264 
265  meter->setValue(format);
266  }
267 
268  receivedBytes = inB;
269  transmittedBytes = outB;
270 }
271 
272 #include "network.moc"
NetworkSensor::NetworkSensor
NetworkSensor(const QString &device, int interval)
Definition: network.cpp:25
NetworkSensor::~NetworkSensor
~NetworkSensor()
Definition: network.cpp:122
SensorParams::getMeter
Meter * getMeter() const
Definition: sensorparams.cpp:31
QObject
Sensor
Definition: sensor.h:17
NO_DEVICE
#define NO_DEVICE
Definition: network.h:32
NO_IP
#define NO_IP
Definition: network.h:34
SensorParams
Hans Karlsson.
Definition: sensorparams.h:30
SensorParams::getParam
QString getParam(const QString &) const
Definition: sensorparams.cpp:26
network.h
Meter
Definition: meters/meter.h:23
Meter::setValue
virtual void setValue(int)
Definition: meters/meter.cpp:136
NetworkSensor::update
void update()
Definition: network.cpp:225
DEFAULT_DEVICE
#define DEFAULT_DEVICE
Definition: network.h:33
Sensor::objList
QList< QObject * > * objList
Definition: sensor.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal