1
/* XFce 4 - Netload Plugin
2
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
4
* Id: $Id: net.c,v 1.6 2003/09/13 12:30:49 bwalle Exp $
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.
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.
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.
2
* Id: $Id: net.c 756 2005-07-31 22:54:22Z bwalle $
3
* -------------------------------------------------------------------------------------------------
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.
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.
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.
16
* -------------------------------------------------------------------------------------------------
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
41
35
#include "wormulon.h"
42
36
#include "slurm.h" /* slurm structs */
38
#include <sys/types.h>
45
#include "wormulon/hpux.h"
46
#include "wormulon/hpux.c"
44
# include "wormulon/hpux.h"
45
# include "wormulon/hpux.c"
48
#include "src/macos.h"
49
#include "src/macos.c"
47
# include "src/macos.h"
48
# include "src/macos.c"
51
#include "wormulon/freebsd.h"
52
#include "wormulon/freebsd.c"
50
# include "wormulon/freebsd.h"
51
# include "wormulon/freebsd.c"
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"
60
#include "wormulon/netbsd.h"
61
#include "wormulon/netbsd.c"
59
# include "wormulon/netbsd.h"
60
# include "wormulon/netbsd.c"
63
#include "wormulon/solaris.h"
64
#include "wormulon/solaris.c"
62
# include "wormulon/solaris.h"
63
# include "wormulon/solaris.c"
66
65
/* should not get here */
67
#error "OS not supported"
66
# error "OS not supported"
70
/* ---------------------------------------------------------------------------------------------- */
71
71
int init_netload(netdata* data, const char* device)
73
73
memset( data, 0, sizeof(netdata) );
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';
83
83
init_osspecific( data );
85
data->ip_address[0] = 0;
86
data->ip_update_count = 0;
85
88
if (checkinterface(data) != TRUE)
87
90
data->correct_interface = FALSE;
96
99
data->correct_interface = TRUE;
99
fprintf( stderr, "The netload plugin was initialized for '%s'.\n", device );
101
PRINT_DBG("The netload plugin was initialized for '%s'.", device);
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.
107
/* ---------------------------------------------------------------------------------------------- */
112
108
void get_current_netload(netdata* data, unsigned long *in, unsigned long *out, unsigned long *tot)
114
110
struct timeval curr_time;
117
if( ! data->correct_interface )
113
if (! data->correct_interface)
119
if( in != NULL && out != NULL && tot != NULL )
115
if (in != NULL && out != NULL && tot != NULL)
121
117
*in = *out = *tot = 0;
163
159
data->prev_time.tv_usec = curr_time.tv_usec;
163
/* ---------------------------------------------------------------------------------------------- */
164
char* get_name(netdata* data)
166
return data->ifdata.if_name;
170
/* ---------------------------------------------------------------------------------------------- */
171
char* get_ip_address(netdata* data)
175
struct sockaddr_in *p_sa;
177
/* use cached value if possible and if the update count is non-zero */
178
if (data->ip_address && data->ip_update_count > 0)
180
data->ip_update_count--;
181
return data->ip_address;
184
/* get the value from the operating system */
185
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
187
PRINT_DBG("Error in socket: %s", strerror(errno));
191
snprintf(ifr.ifr_name, IF_NAMESIZE, data->ifdata.if_name);
192
if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0)
195
PRINT_DBG("Error in ictl(sockfd): %s", strerror(errno));
200
p_sa = (struct sockaddr_in*) &ifr.ifr_addr;
202
if (!inet_ntop(AF_INET, &p_sa->sin_addr, data->ip_address, IP_ADDRESS_LENGTH))
204
PRINT_DBG("Error in inet_ntop: %s", strerror(errno));
209
data->ip_update_count = IP_UPDATE_INTERVAL;
211
return data->ip_address;
215
/* ---------------------------------------------------------------------------------------------- */
166
216
void close_netload(netdata* data)
168
218
/* We need not code here */