~ubuntu-branches/ubuntu/utopic/bubblemon/utopic

« back to all changes in this revision

Viewing changes to src/unix-interfaces.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Carlos Medeiros
  • Date: 2008-01-15 19:18:40 UTC
  • mfrom: (1.1.4 upstream) (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080115191840-up505jro46wef053
Tags: 2.0.9-1
* New upstream release. (Closes: #158667)
* debian/control:
  - Bump Standards-Version: 3.7.3.
  - Added Homepage header and removed old pseudo header.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 
 
3
 *  Bubbling Load Monitoring Applet
 
4
 *  Copyright (C) 1999-2007 Johan Walles - johan.walles@gmail.com
 
5
 *  http://www.nongnu.org/bubblemon/
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include <assert.h>
 
23
#include <stdlib.h>
 
24
#include <errno.h>
 
25
 
 
26
#include <sys/types.h>
 
27
#include <sys/socket.h>
 
28
 
 
29
#include <sys/ioctl.h>
 
30
#include <net/if.h>
 
31
 
 
32
#include "interfaces.h"
 
33
 
 
34
/**
 
35
 * The socket we use to look for interfaces.
 
36
 */
 
37
static int querySocket = 0;
 
38
 
 
39
/**
 
40
 * Our array of interfaces.
 
41
 */
 
42
static struct ifreq *interfaces = NULL;
 
43
 
 
44
/**
 
45
 * The size of our interfaces array.
 
46
 */
 
47
static int interfacesSize = 0;
 
48
 
 
49
/**
 
50
 * Return the socket to query for network interface status, or 0 if we can't come up with
 
51
 * any socket.
 
52
 */
 
53
static int getSocket(void) {
 
54
  if (querySocket == 0) {
 
55
    querySocket = socket(AF_INET, SOCK_DGRAM, 0);
 
56
  }
 
57
  if (querySocket > 0) {
 
58
    return querySocket;
 
59
  } else {
 
60
    return 0;
 
61
  }
 
62
}
 
63
 
 
64
char **interfaces_getcandidates(void) {
 
65
  int error;
 
66
  int nInterfaces;
 
67
  char **interfaceNames;
 
68
  
 
69
  if (interfacesSize == 0) {
 
70
    interfacesSize = 1;
 
71
    interfaces =
 
72
      malloc(interfacesSize * sizeof(struct ifreq));
 
73
  }
 
74
  
 
75
  error = 0;
 
76
  while (1)
 
77
  {
 
78
    struct ifconf ifc;
 
79
    // Get at most maxNInterfaces...
 
80
    ifc.ifc_len = interfacesSize * sizeof(struct ifreq);
 
81
    // ... and put them in interfaces.
 
82
    ifc.ifc_req = interfaces;
 
83
    
 
84
    if (getSocket() == 0) {
 
85
      // Unable to get any info, give up.
 
86
      nInterfaces = 0;
 
87
      break;
 
88
    }
 
89
    if (ioctl(getSocket(), SIOCGIFCONF, &ifc) < 0) {
 
90
      error = errno;
 
91
      break;
 
92
    }
 
93
    
 
94
    nInterfaces = ifc.ifc_len / sizeof(*interfaces);
 
95
    if (nInterfaces < interfacesSize) {
 
96
      // Done, our array wasn't filled
 
97
      break;
 
98
    }
 
99
    
 
100
    interfacesSize *= 2;
 
101
    interfaces =
 
102
      realloc(interfaces, interfacesSize * sizeof(struct ifreq));
 
103
    assert(interfaces != NULL);
 
104
  }
 
105
  if (error != 0) {
 
106
    // Trouble, give up
 
107
    nInterfaces = 0;
 
108
  }
 
109
  
 
110
  interfaceNames = (char**)malloc((nInterfaces + 1) * sizeof(char*));
 
111
  assert(interfaceNames != NULL);
 
112
  int i;
 
113
  for (i = 0; i < nInterfaces; i++) {
 
114
    interfaceNames[i] = interfaces[i].ifr_name;
 
115
  }
 
116
  interfaceNames[nInterfaces] = NULL;
 
117
  
 
118
  return interfaceNames;
 
119
}
 
120
 
 
121
void interfaces_freecandidates(char** candidates) {
 
122
  free(candidates);
 
123
}