Source: networkmonitor.h


Annotated List
Files
Globals
Hierarchy
Index
// ##########################################################################
// ####                                                                  ####
// ####                      RTP Audio Server Project                    ####
// ####                    ============================                  ####
// ####                                                                  ####
// #### Network Monitor                                                  ####
// ####                                                                  ####
// #### Version 1.00  --  February 23, 2001                              ####
// ####                                                                  ####
// #### Copyright (C) 1999  Thomas Dreibholz                             ####
// ####               2000  Universität Bonn, Abt. IV                    ####
// ####               2001  EMail: Dreibholz@bigfoot.com                 ####
// ####                     WWW:   http://www.bigfoot.com/~dreibholz     ####
// ####                                                                  ####
// ##########################################################################


#ifndef NETWORKMONITOR_H
#define NETWORKMONITOR_H


#include "system.h"
#include "networkmonitorinterface.h"
#include "thread.h"
#include "internetaddress.h"
#include "streamsrcdest.h"
#include "streammonitorreport.h"


#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/icmp6.h>

extern "C" {
#include <pcap.h>
#include <pcap-int.h>
}


namespace Coral {


/**
  * This class implements a network monitor, which sums bytes and packets
  * transmitted over a given network interface (eth0, lo, ...). The interface
  * will be set to promiscuous mode. Note: root permissions are required
  * for monitoring a network interface!
  *
  * @short   Network Monitor
  * @author  Thomas Dreibholz (Dreibholz@bigfoot.com)
  * @version 1.0
  */
class NetworkMonitor : virtual public NetworkMonitorInterface,
                       public Thread
{
   // ====== Constructor/Destructor =========================================
   public:
   /**
     * Constructor for new NetworkMonitor. The new monitors's thread has to
     * be started by calling start()!
     *
     * @param device Device name (eth0, lo, ...). Default: NULL (try automatic detection).
     *
     * @see Thread#start
     */
   NetworkMonitor(StreamMonitorReport* smr = NULL, char* device = NULL);

   /**
     * Destructor.
     */
   ~NetworkMonitor();


   // ====== Status functions ===============================================
   /**
     * ready() Implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#ready
     */
   bool ready() const;

   /**
     * Get device name, NetworkMonitor is using. This can be used to get the
     * name of the automatically chosen device name (name = NULL in
     * contructor).
     *
     * @return Device name.
     */
   inline const char* getDevice() const;


   // ====== Lock functions =================================================
   /**
     * lock() implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#lock
     */ 
   void lock();

   /**
     * unlock() implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#unlock
     */ 
   void unlock();


   // ====== Report functions ===============================================
   /**
     * resetReport() Implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#resetReport
     */
   void resetReport();

   /**
     * getReport() Implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#getReport
     */
   void getReport(NetworkMonitorReport* report);

   /**
     * moveReport() Implementation of NetworkMonitorInterface.
     *
     * @see NetworkMonitorInterface#moveReport
     */
   void moveReport(NetworkMonitorReport* report);


   // ====== Private data ===================================================
   private:
   void run();


   void handleTCP(StreamSrcDest* stream,
                  const tcphdr*  tcp,
                  const cardinal tcpLength,
                  const cardinal rawLength);
   void handleUDP(StreamSrcDest* stream,
                  const udphdr*  udp,
                  const cardinal udpLength,
                  const cardinal rawLength);
   void handleICMP4(StreamSrcDest* stream,
                    const icmphdr* icmp,
                    const cardinal icmpLength,
                    const cardinal rawLength);
   void handleICMP6(StreamSrcDest*   stream,
                    const icmp6_hdr* icmp,
                    const cardinal   icmpLength,
                    const cardinal   rawLength);

   void ip4PacketHandler(const iphdr* ip, const cardinal length);
   void ip6PacketHandler(const ip6_hdr* ip, const cardinal length);


   static void loopbackInterfaceHandler(u_char*            userData,
                                        const pcap_pkthdr* pcapheader,
                                        const u_char*      packet);

   static void ethernetInterfaceHandler(u_char*            userData,
                                        const pcap_pkthdr* pcapheader,
                                        const u_char*      packet);


   static const cardinal DefaultSnaplen = 78; // Length of saved portion of packet


   card64                TimeStamp;
   NetworkMonitorReport  Report;

   pcap_t*               PCapDescriptor;
   char*                 PCapDevice;
   pcap_handler          PCapHandler;
   cardinal              PCapInterfaceType;
   StreamMonitorReport*  SMReport;
};


}


#include "networkmonitor.icc"


#endif

Generated by: viper@odin on Fri Feb 23 12:41:26 2001, using kdoc 2.0a36.