From 6c59586f2eb86d1c0df9283410e9a13f304db6f8 Mon Sep 17 00:00:00 2001 From: Bernhard Walle Date: Sat, 13 Sep 2003 12:30:49 +0000 Subject: [PATCH] Added support for MacOS X (Old svn revision: 313) --- panel-plugin/net.c | 5 +- panel-plugin/net.h | 4 +- panel-plugin/os.h | 26 +++++ panel-plugin/wormulon/if_media.c | 8 +- panel-plugin/wormulon/macos.c | 174 +++++++++++++++++++++++++++++++ panel-plugin/wormulon/macos.h | 10 ++ 6 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 panel-plugin/wormulon/macos.c create mode 100644 panel-plugin/wormulon/macos.h diff --git a/panel-plugin/net.c b/panel-plugin/net.c index 36a1a85..df7a960 100644 --- a/panel-plugin/net.c +++ b/panel-plugin/net.c @@ -1,7 +1,7 @@ /* XFce 4 - Netload Plugin * Copyright (c) 2003 Bernhard Walle * - * Id: $Id: net.c,v 1.5 2003/09/06 12:37:20 bwalle Exp $ + * Id: $Id: net.c,v 1.6 2003/09/13 12:30:49 bwalle Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -44,6 +44,9 @@ #ifdef __HPUX__ #include "wormulon/hpux.h" #include "wormulon/hpux.c" +#elif __APPLE__ +#include "src/macos.h" +#include "src/macos.c" #elif __FreeBSD__ #include "wormulon/freebsd.h" #include "wormulon/freebsd.c" diff --git a/panel-plugin/net.h b/panel-plugin/net.h index d6c1588..cb310ee 100644 --- a/panel-plugin/net.h +++ b/panel-plugin/net.h @@ -1,7 +1,7 @@ /* XFce 4 - Netload Plugin * Copyright (c) 2003 Bernhard Walle * - * Id: $Id: net.h,v 1.6 2003/09/06 12:37:20 bwalle Exp $ + * Id: $Id: net.h,v 1.7 2003/09/13 12:30:49 bwalle Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -77,7 +77,7 @@ typedef struct char* buf2; int alloc1; int alloc2; -#elif __OpenBSD__ || __MicroBSD__ +#elif __OpenBSD__ || __MicroBSD__ || __APPLE__ int mib_name1[6]; int mib_name2[6]; char* buf1; diff --git a/panel-plugin/os.h b/panel-plugin/os.h index b070ce1..89e98a4 100644 --- a/panel-plugin/os.h +++ b/panel-plugin/os.h @@ -39,6 +39,32 @@ #include #include #include ++#elif __APPLE__ /* Mac OS X */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include #elif __FreeBSD__ /* F R E E B S D */ #include #include diff --git a/panel-plugin/wormulon/if_media.c b/panel-plugin/wormulon/if_media.c index a1c1527..b569949 100644 --- a/panel-plugin/wormulon/if_media.c +++ b/panel-plugin/wormulon/if_media.c @@ -6,10 +6,11 @@ * operating systems. * ***************************************************************************** - * $Id: if_media.c,v 1.2 2003/08/25 21:08:58 bwalle Exp $ + * $Id: if_media.c,v 1.3 2003/09/13 12:30:10 bwalle Exp $ *****************************************************************************/ -#if defined (__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) || (__MicroBSD__) +#if defined (__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) || (__MicroBSD__) || (__APPLE__) + #define MEDIA_H_SUPPORTED #endif @@ -27,6 +28,7 @@ * - OpenBSD * - NetBSD * - MicroBSD (99% OpenBSD) + * - Mac OS X * *****************************************************************************/ @@ -127,7 +129,7 @@ int get_if_speed (char *ifstring) case IFM_10_STP: speed = 10 * 1000; break; -#if defined(__OpenBSD__) || (__MicroBSD__) || (__NetBSD__) +#if defined(__OpenBSD__) || (__MicroBSD__) || (__NetBSD__) || (__APPLE__) case IFM_HPNA_1: #else #if __FreeBSD__ <= 4 diff --git a/panel-plugin/wormulon/macos.c b/panel-plugin/wormulon/macos.c new file mode 100644 index 0000000..a7a043c --- /dev/null +++ b/panel-plugin/wormulon/macos.c @@ -0,0 +1,174 @@ +/* $Id: macos.c,v 1.1 2003/09/13 12:30:10 bwalle Exp $ */ + + +/***************************************************************************** + * + * init_osspecific() + * + * Init function + * + ****************************************************************************/ + +void init_osspecific(netdata* data) +{ + data->mib_name1[0] = CTL_NET; + data->mib_name1[1] = PF_ROUTE; + data->mib_name1[2] = 0; + data->mib_name1[3] = 0; + data->mib_name1[4] = NET_RT_IFLIST; + data->mib_name1[5] = 0; + + data->mib_name2[0] = CTL_NET; + data->mib_name2[1] = PF_ROUTE; + data->mib_name2[2] = 0; + data->mib_name2[3] = 0; + data->mib_name2[4] = NET_RT_IFLIST; + data->mib_name2[5] = 0; + +#ifdef DEBUG + fprintf( stderr, "The netload plugin was initialized for NetBSD.\n" ); +#endif +} + + +/***************************************************************************** + * + * checkinterface() + * + * check if a given interface exists, return TRUE if it does and FALSE if not + * + ****************************************************************************/ +int checkinterface(netdata* data) +{ + int validinterface = FALSE; + char *lim, *next; + struct if_msghdr *ifm, *nextifm; + struct sockaddr_dl *sdl; + size_t needed; + char s[32]; + + if (sysctl(data->mib_name1, 6, NULL, &needed, NULL, 0) < 0) + return FALSE; + if (data->alloc1 < (signed long) needed) + { + if (data->buf1 != NULL) + free (data->buf1); + data->buf1 = malloc(needed); + if (data->buf1 == NULL) + return FALSE; + data->alloc1 = needed; + } + + if (sysctl(data->mib_name1, 6, data->buf1, &needed, NULL, 0) < 0) + return FALSE; + lim = data->buf1 + needed; + next = data->buf1; + while ((next < lim) && (validinterface == 0)) + { + ifm = (struct if_msghdr *)next; + if (ifm->ifm_type != RTM_IFINFO) + return FALSE; + next += ifm->ifm_msglen; + + while (next < lim) + { + nextifm = (struct if_msghdr *)next; + if (nextifm->ifm_type != RTM_NEWADDR) + break; + next += nextifm->ifm_msglen; + } + + if (ifm->ifm_flags & IFF_UP) + { + sdl = (struct sockaddr_dl *)(ifm + 1); + strncpy(s, sdl->sdl_data, sdl->sdl_nlen); + s[sdl->sdl_nlen] = '\0'; + /* search for the right network interface */ + if (sdl->sdl_family != AF_LINK) + continue; + if (strcmp(s, data->ifdata.if_name) != 0) + continue; + else + { + validinterface = TRUE; + break; /* stop searching */ + } + } + } + return validinterface; +} +/****************************************************************************** + * + * get_stat() + * + * this code is based on gkrellm code (thanks guys!) + * + ****************************************************************************/ + +int get_stat(netdata* data) +{ + char *lim, *next; + struct if_msghdr *ifm, *nextifm; + struct sockaddr_dl *sdl; + char s[32]; + size_t needed; + unsigned long rx_o, tx_o; + + if (sysctl(data->mib_name2, 6, NULL, &needed, NULL, 0) < 0) + return 1; + if (data->alloc2 < (signed long) needed) + { + if (data->buf2 != NULL) + free (data->buf2); + data->buf2 = malloc(needed); + if (data->buf2 == NULL) + return 1; + data->alloc2 = needed; + } + + if (sysctl(data->mib_name2, 6, data->buf2, &needed, NULL, 0) < 0) + return 1; + lim = data->buf2 + needed; + next = data->buf2; + while (next < lim) + { + ifm = (struct if_msghdr *)next; + if (ifm->ifm_type != RTM_IFINFO) + return 1; + next += ifm->ifm_msglen; + + while (next < lim) + { + nextifm = (struct if_msghdr *)next; + if (nextifm->ifm_type != RTM_NEWADDR) + break; + next += nextifm->ifm_msglen; + } + + if (ifm->ifm_flags & IFF_UP) + { + sdl = (struct sockaddr_dl *)(ifm + 1); + strncpy(s, sdl->sdl_data, sdl->sdl_nlen); + s[sdl->sdl_nlen] = '\0'; + + /* search for the right network interface */ + if (strcmp(s, data->ifdata.if_name) != 0) + continue; + + rx_o = data->stats.rx_bytes; tx_o = data->stats.tx_bytes; + /* write stats */ + data->stats.tx_packets = ifm->ifm_data.ifi_opackets; + data->stats.rx_packets = ifm->ifm_data.ifi_ipackets; + data->stats.rx_bytes = ifm->ifm_data.ifi_ibytes; + data->stats.tx_bytes = ifm->ifm_data.ifi_obytes; + data->stats.rx_errors = ifm->ifm_data.ifi_ierrors; + data->stats.tx_errors = ifm->ifm_data.ifi_oerrors; + + if (rx_o > data->stats.rx_bytes) + data->stats.rx_over++; + if (tx_o > data->stats.tx_bytes) + data->stats.tx_over++; + } + } + return 0; +} diff --git a/panel-plugin/wormulon/macos.h b/panel-plugin/wormulon/macos.h new file mode 100644 index 0000000..8e8e323 --- /dev/null +++ b/panel-plugin/wormulon/macos.h @@ -0,0 +1,10 @@ +#ifndef MACOS_H +#define MACOS_H + +#include "../net.h" + +void init_osspecific(netdata* data); +int checkinterface(netdata* data); +int get_stat(netdata* data); + +#endif /* MACOS_H */