ifstat.c (158160) | ifstat.c (158161) |
---|---|
1/* 2 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * | 1/* 2 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/usr.bin/systat/ifstat.c 158160 2006-04-30 04:26:46Z bde $ | 28 * $FreeBSD: head/usr.bin/systat/ifstat.c 158161 2006-04-30 04:47:23Z bde $ |
29 */ 30 31#include <sys/types.h> 32#include <sys/socket.h> 33#include <sys/sysctl.h> 34#include <sys/time.h> 35#include <sys/queue.h> 36#include <net/if.h> --- 10 unchanged lines hidden (view full) --- 47#include "systat.h" 48#include "extern.h" 49#include "mode.h" 50#include "convtbl.h" 51 52 /* Column numbers */ 53 54#define C1 0 /* 0-19 */ | 29 */ 30 31#include <sys/types.h> 32#include <sys/socket.h> 33#include <sys/sysctl.h> 34#include <sys/time.h> 35#include <sys/queue.h> 36#include <net/if.h> --- 10 unchanged lines hidden (view full) --- 47#include "systat.h" 48#include "extern.h" 49#include "mode.h" 50#include "convtbl.h" 51 52 /* Column numbers */ 53 54#define C1 0 /* 0-19 */ |
55#define C2 20 /* 20-39 */ | 55#define C2 20 /* 20-39 */ |
56#define C3 40 /* 40-59 */ 57#define C4 60 /* 60-80 */ 58#define C5 80 /* Used for label positioning. */ 59 60static const int col0 = 0; 61static const int col1 = C1; 62static const int col2 = C2; 63static const int col3 = C3; 64static const int col4 = C4; 65static const int col5 = C5; 66 67 | 56#define C3 40 /* 40-59 */ 57#define C4 60 /* 60-80 */ 58#define C5 80 /* Used for label positioning. */ 59 60static const int col0 = 0; 61static const int col1 = C1; 62static const int col2 = C2; 63static const int col3 = C3; 64static const int col4 = C4; 65static const int col5 = C5; 66 67 |
68SLIST_HEAD(, if_stat) curlist; | 68SLIST_HEAD(, if_stat) curlist; |
69SLIST_HEAD(, if_stat_disp) displist; 70 71struct if_stat { 72 SLIST_ENTRY(if_stat) link; 73 char if_name[IF_NAMESIZE]; 74 struct ifmibdata if_mib; 75 struct timeval tv; 76 struct timeval tv_lastchanged; --- 122 unchanged lines hidden (view full) --- 199 PUTRATE(col2, ifp->if_ypos); 200 PUTRATE(col3, ifp->if_ypos); 201 PUTTOTAL(col4, ifp->if_ypos); 202 } 203 204 return; 205} 206 | 69SLIST_HEAD(, if_stat_disp) displist; 70 71struct if_stat { 72 SLIST_ENTRY(if_stat) link; 73 char if_name[IF_NAMESIZE]; 74 struct ifmibdata if_mib; 75 struct timeval tv; 76 struct timeval tv_lastchanged; --- 122 unchanged lines hidden (view full) --- 199 PUTRATE(col2, ifp->if_ypos); 200 PUTRATE(col3, ifp->if_ypos); 201 PUTTOTAL(col4, ifp->if_ypos); 202 } 203 204 return; 205} 206 |
207int | 207int |
208initifstat(void) 209{ 210 struct if_stat *p = NULL; 211 u_int n = 0, i = 0; 212 213 n = getifnum(); 214 if (n <= 0) 215 return -1; --- 5 unchanged lines hidden (view full) --- 221 if (p == NULL) 222 IFSTAT_ERR(1, "out of memory"); 223 memset((void *)p, 0, sizeof(struct if_stat)); 224 SLIST_INSERT_HEAD(&curlist, p, link); 225 p->if_row = i+1; 226 getifmibdata(p->if_row, &p->if_mib); 227 right_align_string(p); 228 | 208initifstat(void) 209{ 210 struct if_stat *p = NULL; 211 u_int n = 0, i = 0; 212 213 n = getifnum(); 214 if (n <= 0) 215 return -1; --- 5 unchanged lines hidden (view full) --- 221 if (p == NULL) 222 IFSTAT_ERR(1, "out of memory"); 223 memset((void *)p, 0, sizeof(struct if_stat)); 224 SLIST_INSERT_HEAD(&curlist, p, link); 225 p->if_row = i+1; 226 getifmibdata(p->if_row, &p->if_mib); 227 right_align_string(p); 228 |
229 /* | 229 /* |
230 * Initially, we only display interfaces that have 231 * received some traffic. 232 */ 233 if (p->if_mib.ifmd_data.ifi_ibytes != 0) 234 p->display = 1; 235 } 236 237 sort_interface_list(); --- 6 unchanged lines hidden (view full) --- 244{ 245 struct if_stat *ifp = NULL; 246 struct timeval tv, new_tv, old_tv; 247 double elapsed = 0.0; 248 u_int new_inb, new_outb, old_inb, old_outb = 0; 249 u_int we_need_to_sort_interface_list = 0; 250 251 SLIST_FOREACH(ifp, &curlist, link) { | 230 * Initially, we only display interfaces that have 231 * received some traffic. 232 */ 233 if (p->if_mib.ifmd_data.ifi_ibytes != 0) 234 p->display = 1; 235 } 236 237 sort_interface_list(); --- 6 unchanged lines hidden (view full) --- 244{ 245 struct if_stat *ifp = NULL; 246 struct timeval tv, new_tv, old_tv; 247 double elapsed = 0.0; 248 u_int new_inb, new_outb, old_inb, old_outb = 0; 249 u_int we_need_to_sort_interface_list = 0; 250 251 SLIST_FOREACH(ifp, &curlist, link) { |
252 /* | 252 /* |
253 * Grab a copy of the old input/output values before we 254 * call getifmibdata(). 255 */ | 253 * Grab a copy of the old input/output values before we 254 * call getifmibdata(). 255 */ |
256 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes; | 256 old_inb = ifp->if_mib.ifmd_data.ifi_ibytes; |
257 old_outb = ifp->if_mib.ifmd_data.ifi_obytes; 258 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange; 259 260 if (gettimeofday(&new_tv, (struct timezone *)0) != 0) 261 IFSTAT_ERR(2, "error getting time of day"); 262 (void)getifmibdata(ifp->if_row, &ifp->if_mib); 263 264 265 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes; 266 new_outb = ifp->if_mib.ifmd_data.ifi_obytes; 267 268 /* Display interface if it's received some traffic. */ 269 if (new_inb > 0 && old_inb == 0) { 270 ifp->display = 1; 271 we_need_to_sort_interface_list++; | 257 old_outb = ifp->if_mib.ifmd_data.ifi_obytes; 258 ifp->tv_lastchanged = ifp->if_mib.ifmd_data.ifi_lastchange; 259 260 if (gettimeofday(&new_tv, (struct timezone *)0) != 0) 261 IFSTAT_ERR(2, "error getting time of day"); 262 (void)getifmibdata(ifp->if_row, &ifp->if_mib); 263 264 265 new_inb = ifp->if_mib.ifmd_data.ifi_ibytes; 266 new_outb = ifp->if_mib.ifmd_data.ifi_obytes; 267 268 /* Display interface if it's received some traffic. */ 269 if (new_inb > 0 && old_inb == 0) { 270 ifp->display = 1; 271 we_need_to_sort_interface_list++; |
272 } | 272 } |
273 274 /* 275 * The rest is pretty trivial. Calculate the new values 276 * for our current traffic rates, and while we're there, 277 * see if we have new peak rates. 278 */ 279 old_tv = ifp->tv; 280 timersub(&new_tv, &old_tv, &tv); --- 22 unchanged lines hidden (view full) --- 303 } 304 305 if (we_need_to_sort_interface_list) 306 sort_interface_list(); 307 308 return; 309} 310 | 273 274 /* 275 * The rest is pretty trivial. Calculate the new values 276 * for our current traffic rates, and while we're there, 277 * see if we have new peak rates. 278 */ 279 old_tv = ifp->tv; 280 timersub(&new_tv, &old_tv, &tv); --- 22 unchanged lines hidden (view full) --- 303 } 304 305 if (we_need_to_sort_interface_list) 306 sort_interface_list(); 307 308 return; 309} 310 |
311/* | 311/* |
312 * We want to right justify our interface names against the first column 313 * (first sixteen or so characters), so we need to do some alignment. 314 */ 315static void 316right_align_string(const struct if_stat *ifp) 317{ 318 int str_len = 0, pad_len = 0; 319 char *newstr = NULL, *ptr = NULL; --- 23 unchanged lines hidden (view full) --- 343 * 344 * This function is called any time a change is made to an interface's 345 * ``display'' state. 346 */ 347void 348sort_interface_list(void) 349{ 350 struct if_stat *ifp = NULL; | 312 * We want to right justify our interface names against the first column 313 * (first sixteen or so characters), so we need to do some alignment. 314 */ 315static void 316right_align_string(const struct if_stat *ifp) 317{ 318 int str_len = 0, pad_len = 0; 319 char *newstr = NULL, *ptr = NULL; --- 23 unchanged lines hidden (view full) --- 343 * 344 * This function is called any time a change is made to an interface's 345 * ``display'' state. 346 */ 347void 348sort_interface_list(void) 349{ 350 struct if_stat *ifp = NULL; |
351 u_int y = 0; | 351 u_int y = 0; |
352 353 y = STARTING_ROW; 354 SLIST_FOREACH(ifp, &curlist, link) { 355 if (ifp->display) { 356 ifp->if_ypos = y; 357 y += ROW_SPACING; 358 } 359 } --- 13 unchanged lines hidden (view full) --- 373 374 datalen = sizeof(data); 375 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL, 376 (size_t)0) != 0) 377 IFSTAT_ERR(1, "sysctl error"); 378 return data; 379} 380 | 352 353 y = STARTING_ROW; 354 SLIST_FOREACH(ifp, &curlist, link) { 355 if (ifp->display) { 356 ifp->if_ypos = y; 357 y += ROW_SPACING; 358 } 359 } --- 13 unchanged lines hidden (view full) --- 373 374 datalen = sizeof(data); 375 if (sysctl(name, 5, (void *)&data, (size_t *)&datalen, (void *)NULL, 376 (size_t)0) != 0) 377 IFSTAT_ERR(1, "sysctl error"); 378 return data; 379} 380 |
381static void | 381static void |
382getifmibdata(int row, struct ifmibdata *data) 383{ 384 size_t datalen = 0; 385 static int name[] = { CTL_NET, 386 PF_LINK, 387 NETLINK_GENERIC, 388 IFMIB_IFDATA, 389 0, --- 23 unchanged lines hidden --- | 382getifmibdata(int row, struct ifmibdata *data) 383{ 384 size_t datalen = 0; 385 static int name[] = { CTL_NET, 386 PF_LINK, 387 NETLINK_GENERIC, 388 IFMIB_IFDATA, 389 0, --- 23 unchanged lines hidden --- |