Go to the documentation of this file.00001
00002
00003 #include "ace/Monitor_Control/BSD_Network_Interface_Monitor.h"
00004
00005 #if defined (__NetBSD__) || defined (__OpenBSD__)
00006
00007 #include "ace/Log_Msg.h"
00008 #include "ace/OS_NS_stdio.h"
00009 #include <sys/types.h>
00010 #include <sys/socket.h>
00011 #include <sys/sockio.h>
00012 #include <sys/ioctl.h>
00013 #include <net/if.h>
00014 #include <ifaddrs.h>
00015
00016 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00017
00018 namespace ACE
00019 {
00020 namespace Monitor_Control
00021 {
00022 BSD_Network_Interface_Monitor::BSD_Network_Interface_Monitor (
00023 const ACE_TCHAR *lookup_str)
00024 : value_ (0UL),
00025 start_ (0UL),
00026 lookup_str_ (lookup_str)
00027 {
00028 this->init();
00029 }
00030
00031 void
00032 BSD_Network_Interface_Monitor::update_i (void)
00033 {
00034 this->fetch(this->value_);
00035 this->value_ -= this->start_;
00036 }
00037
00038 void
00039 BSD_Network_Interface_Monitor::clear_impl (void)
00040 {
00041 this->init();
00042 }
00043
00044 void
00045 BSD_Network_Interface_Monitor::init (void)
00046 {
00047 this->fetch(this->start_);
00048 this->value_ = 0UL;
00049 }
00050
00051 void
00052 BSD_Network_Interface_Monitor::fetch (ACE_UINT64& value) const
00053 {
00054 ACE_UINT64 count = 0;
00055 int fd = socket (AF_INET, SOCK_DGRAM, 0);
00056
00057 if (fd == -1)
00058 {
00059 ACE_ERROR ((LM_ERROR, ACE_TEXT ("socket failed\n")));
00060 return;
00061 }
00062
00063 struct ifaddrs *ifa, *ifap;
00064
00065 if (getifaddrs (&ifap) < 0)
00066 {
00067 ACE_ERROR ((LM_ERROR, ACE_TEXT ("getifaddrs failed\n")));
00068 close (fd);
00069 return;
00070 }
00071
00072 char *p = 0;
00073
00074 for (ifa = ifap; ifa != 0; ifa = ifa->ifa_next)
00075 {
00076 if (p && strcmp (p, ifa->ifa_name) == 0)
00077 {
00078 continue;
00079 }
00080
00081 p = ifa->ifa_name;
00082
00083 #if defined (__OpenBSD__)
00084 struct ifreq ifdr;
00085 #else
00086 struct ifdatareq ifdr;
00087 #endif
00088 memset (&ifdr, 0, sizeof (ifdr));
00089
00090 #if defined (__OpenBSD__)
00091 struct if_data if_data;
00092 ifdr.ifr_data = reinterpret_cast<caddr_t> (&if_data);
00093 strncpy (ifdr.ifr_name, ifa->ifa_name, IFNAMSIZ-1);
00094 #else
00095 strncpy (ifdr.ifdr_name, ifa->ifa_name, sizeof (ifdr));
00096 #endif
00097 if (ioctl (fd, SIOCGIFDATA, &ifdr) == -1)
00098 {
00099 ACE_ERROR ((LM_ERROR, ACE_TEXT ("SIOCGIFDATA failed\n")));
00100 }
00101
00102 #if defined (__OpenBSD__)
00103 struct if_data * const ifi = &if_data;
00104 #else
00105 struct if_data * const ifi = &ifdr.ifdr_data;
00106 #endif
00107
00108 if (this->lookup_str_ == "ibytes")
00109 {
00110 count += ifi->ifi_ibytes;
00111 }
00112 else if (this->lookup_str_ == "ipackets")
00113 {
00114 count += ifi->ifi_ipackets;
00115 }
00116 else if (this->lookup_str_ == "obytes")
00117 {
00118 count += ifi->ifi_obytes;
00119 }
00120 else if (this->lookup_str_ == "opackets")
00121 {
00122 count += ifi->ifi_opackets;
00123 }
00124 }
00125
00126 freeifaddrs (ifap);
00127 close (fd);
00128
00129 value = count;
00130 }
00131 }
00132 }
00133
00134 ACE_END_VERSIONED_NAMESPACE_DECL
00135
00136 #endif