3
* Bubbling Load Monitoring Applet
4
* Copyright (C) 1999-2007 Johan Walles - johan.walles@gmail.com
5
* http://www.nongnu.org/bubblemon/
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
26
#include <sys/types.h>
27
#include <sys/socket.h>
29
#include <sys/ioctl.h>
32
#include "interfaces.h"
35
* The socket we use to look for interfaces.
37
static int querySocket = 0;
40
* Our array of interfaces.
42
static struct ifreq *interfaces = NULL;
45
* The size of our interfaces array.
47
static int interfacesSize = 0;
50
* Return the socket to query for network interface status, or 0 if we can't come up with
53
static int getSocket(void) {
54
if (querySocket == 0) {
55
querySocket = socket(AF_INET, SOCK_DGRAM, 0);
57
if (querySocket > 0) {
64
char **interfaces_getcandidates(void) {
67
char **interfaceNames;
69
if (interfacesSize == 0) {
72
malloc(interfacesSize * sizeof(struct ifreq));
79
// Get at most maxNInterfaces...
80
ifc.ifc_len = interfacesSize * sizeof(struct ifreq);
81
// ... and put them in interfaces.
82
ifc.ifc_req = interfaces;
84
if (getSocket() == 0) {
85
// Unable to get any info, give up.
89
if (ioctl(getSocket(), SIOCGIFCONF, &ifc) < 0) {
94
nInterfaces = ifc.ifc_len / sizeof(*interfaces);
95
if (nInterfaces < interfacesSize) {
96
// Done, our array wasn't filled
102
realloc(interfaces, interfacesSize * sizeof(struct ifreq));
103
assert(interfaces != NULL);
110
interfaceNames = (char**)malloc((nInterfaces + 1) * sizeof(char*));
111
assert(interfaceNames != NULL);
113
for (i = 0; i < nInterfaces; i++) {
114
interfaceNames[i] = interfaces[i].ifr_name;
116
interfaceNames[nInterfaces] = NULL;
118
return interfaceNames;
121
void interfaces_freecandidates(char** candidates) {