Added support for MacOS X

(Old svn revision: 313)
master
Bernhard Walle 2003-09-13 12:30:49 +00:00
parent 76d9d03724
commit 6c59586f2e
6 changed files with 221 additions and 6 deletions

View File

@ -1,7 +1,7 @@
/* XFce 4 - Netload Plugin
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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"

View File

@ -1,7 +1,7 @@
/* XFce 4 - Netload Plugin
* Copyright (c) 2003 Bernhard Walle <bernhard.walle@gmx.de>
*
* 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;

View File

@ -39,6 +39,32 @@
#include <sys/mib.h>
#include <arpa/inet.h>
#include <net/if.h>
+#elif __APPLE__ /* Mac OS X */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <ctype.h>
+#include <signal.h>
+#include <curses.h>
+#include <ifaddrs.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <net/route.h>
+#include <net/if_dl.h>
+#include <net/if.h>
+#include <net/if_media.h>
+#include <net/if_mib.h>
+#include <arpa/inet.h>
#elif __FreeBSD__ /* F R E E B S D */
#include <stdio.h>
#include <stdlib.h>

View File

@ -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

View File

@ -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;
}

View File

@ -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 */