~ubuntu-branches/ubuntu/vivid/xfce4-netload-plugin/vivid

« back to all changes in this revision

Viewing changes to panel-plugin/net.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez
  • Date: 2006-07-26 17:48:23 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060726174823-f986iy7j48iuqvrr
Tags: 0.4-2
* Updated build-deps to 4.3.90.2 (Xfce 4.4 Beta2).
* Updated standards version to 3.7.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  XFce 4 - Netload Plugin
2
 
 *    Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
3
 
 *  
4
 
 *  Id: $Id: net.c,v 1.6 2003/09/13 12:30:49 bwalle Exp $
5
 
 *  
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
1
/*
 
2
 * Id: $Id: net.c 756 2005-07-31 22:54:22Z bwalle $
 
3
 * -------------------------------------------------------------------------------------------------
 
4
 * 
 
5
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 
6
 * GNU General Public License as published by the Free Software Foundation; You may only use 
 
7
 * version 2 of the License, you have no option to use any other version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 
10
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See 
 
11
 * the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along with this program; if 
 
14
 * not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 *
 
16
 * ------------------------------------------------------------------------------------------------- 
19
17
 */
20
18
 
21
 
 
22
 
 
23
19
/*
24
20
 * This is just a wrapper between the netload-plugin and the wormulon source.
25
21
 * Wormulon is a small command-line util which displays the netload. You can find it
29
25
 * number of operating systems quickly without a library! Without him only 
30
26
 * Linux and FreeBSD (with foreign code from IceWM) would be supported.
31
27
 */
32
 
 
33
 
 
34
28
#ifdef HAVE_CONFIG_H
35
29
#include <config.h>
36
30
#endif
41
35
#include "wormulon.h"
42
36
#include "slurm.h"      /* slurm structs */
43
37
 
 
38
#include <sys/types.h>
 
39
#include <errno.h>
 
40
 
 
41
#include "global.h"
 
42
 
44
43
#ifdef __HPUX__
45
 
#include "wormulon/hpux.h"
46
 
#include "wormulon/hpux.c"
 
44
# include "wormulon/hpux.h"
 
45
# include "wormulon/hpux.c"
47
46
#elif __APPLE__
48
 
#include "src/macos.h"
49
 
#include "src/macos.c"
 
47
# include "src/macos.h"
 
48
# include "src/macos.c"
50
49
#elif __FreeBSD__
51
 
#include "wormulon/freebsd.h"
52
 
#include "wormulon/freebsd.c"
 
50
# include "wormulon/freebsd.h"
 
51
# include "wormulon/freebsd.c"
53
52
#elif __linux__
54
 
#include "wormulon/linux.h"
55
 
#include "wormulon/linux.c"
 
53
# include "wormulon/linux.h"
 
54
# include "wormulon/linux.c"
56
55
#elif __OpenBSD__ || __MicroBSD__
57
 
#include "wormulon/openbsd.h"
58
 
#include "wormulon/openbsd.c"
 
56
# include "wormulon/openbsd.h"
 
57
# include "wormulon/openbsd.c"
59
58
#elif __NetBSD__
60
 
#include "wormulon/netbsd.h"
61
 
#include "wormulon/netbsd.c"
 
59
# include "wormulon/netbsd.h"
 
60
# include "wormulon/netbsd.c"
62
61
#elif __Solaris__
63
 
#include "wormulon/solaris.h"
64
 
#include "wormulon/solaris.c"
 
62
# include "wormulon/solaris.h"
 
63
# include "wormulon/solaris.c"
65
64
#else
66
65
/* should not get here */
67
 
#error "OS not supported"
 
66
# error "OS not supported"
68
67
#endif
69
68
 
70
69
 
 
70
/* ---------------------------------------------------------------------------------------------- */
71
71
int init_netload(netdata* data, const char* device)
72
72
{
73
73
    memset( data, 0, sizeof(netdata) );
77
77
        return TRUE;
78
78
    }
79
79
    
80
 
    strncpy( data->ifdata.if_name, device, 9 );
81
 
    data->ifdata.if_name[9] = '\0';
 
80
    strncpy( data->ifdata.if_name, device, INTERFACE_NAME_LENGTH);
 
81
    data->ifdata.if_name[INTERFACE_NAME_LENGTH] = '\0';
82
82
    
83
83
    init_osspecific( data );
84
84
    
 
85
    data->ip_address[0] = 0;
 
86
    data->ip_update_count = 0;
 
87
    
85
88
    if (checkinterface(data) != TRUE)
86
89
        {
87
90
        data->correct_interface = FALSE;
95
98
    
96
99
    data->correct_interface = TRUE;
97
100
    
98
 
#ifdef DEBUG
99
 
    fprintf( stderr, "The netload plugin was initialized for '%s'.\n", device );
100
 
#endif /* DEBUG */
 
101
    PRINT_DBG("The netload plugin was initialized for '%s'.", device);
101
102
    
102
103
    return TRUE;
103
104
}
104
105
 
105
106
 
106
 
/**
107
 
 * Gets the current netload.
108
 
 * @param   in          Will be filled with the "in"-load.
109
 
 * @param   out         Will be filled with the "out"-load.
110
 
 * @param   tot         Will be filled with the "total"-load.
111
 
 */
 
107
/* ---------------------------------------------------------------------------------------------- */
112
108
void get_current_netload(netdata* data, unsigned long *in, unsigned long *out, unsigned long *tot) 
113
109
{
114
110
    struct timeval curr_time;
115
111
    double delta_t;
116
112
    
117
 
    if( ! data->correct_interface )
 
113
    if (! data->correct_interface)
118
114
    {
119
 
        if( in != NULL && out != NULL && tot != NULL )
 
115
        if (in != NULL && out != NULL && tot != NULL)
120
116
        {
121
117
            *in = *out = *tot = 0;
122
118
        }
147
143
        data->cur_out = (int)( (data->stats.tx_bytes - data->backup_out) / delta_t + 0.5);
148
144
    }
149
145
 
150
 
    if( in != NULL && out != NULL && tot != NULL )
 
146
    if (in != NULL && out != NULL && tot != NULL)
151
147
    {
152
148
        *in = data->cur_in;
153
149
        *out = data->cur_out;
163
159
    data->prev_time.tv_usec = curr_time.tv_usec;
164
160
}
165
161
 
 
162
 
 
163
/* ---------------------------------------------------------------------------------------------- */
 
164
char* get_name(netdata* data)
 
165
{
 
166
    return data->ifdata.if_name;
 
167
}
 
168
 
 
169
 
 
170
/* ---------------------------------------------------------------------------------------------- */
 
171
char* get_ip_address(netdata* data)
 
172
{
 
173
    int sockfd;
 
174
    struct ifreq ifr;
 
175
    struct sockaddr_in *p_sa;
 
176
        
 
177
    /* use cached value if possible and if the update count is non-zero */ 
 
178
    if (data->ip_address && data->ip_update_count > 0)
 
179
    {
 
180
        data->ip_update_count--;
 
181
        return data->ip_address;
 
182
    }
 
183
    
 
184
    /* get the value from the operating system */
 
185
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
 
186
    {
 
187
        PRINT_DBG("Error in socket: %s", strerror(errno));
 
188
        return NULL;
 
189
    }
 
190
    
 
191
    snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
 
192
    if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0)
 
193
    {
 
194
        close(sockfd);
 
195
        PRINT_DBG("Error in ictl(sockfd): %s", strerror(errno));
 
196
        return NULL;
 
197
    }
 
198
    close(sockfd);
 
199
    
 
200
    p_sa = (struct sockaddr_in*) &ifr.ifr_addr;
 
201
    
 
202
    if (!inet_ntop(AF_INET, &p_sa->sin_addr, data->ip_address, IP_ADDRESS_LENGTH))
 
203
    {
 
204
        PRINT_DBG("Error in inet_ntop: %s", strerror(errno));
 
205
        return NULL;
 
206
    }
 
207
    
 
208
    /* now updated */
 
209
    data->ip_update_count = IP_UPDATE_INTERVAL;
 
210
    
 
211
    return data->ip_address;
 
212
}
 
213
 
 
214
 
 
215
/* ---------------------------------------------------------------------------------------------- */
166
216
void close_netload(netdata* data)
167
217
{
168
218
    /* We need not code here */