"Fossies" - the Fresh Open Source Software Archive

Member "bandwidthd-2.0.1/bandwidthd.h" (11 Jan 2005, 5313 Bytes) of package /linux/privat/old/bandwidthd-2.0.1.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "bandwidthd.h" see the Fossies "Dox" file reference documentation.

    1 #ifdef HAVE_CONFIG_H
    2 #include "config.h"
    3 #endif
    4 
    5 #include <unistd.h>
    6 #include <errno.h>
    7 #include <sys/socket.h>
    8 #include <netinet/in.h>
    9 #include <arpa/inet.h>
   10 #include <string.h>
   11 
   12 #include <sys/types.h>
   13 #include <sys/stat.h>
   14 #include <signal.h>
   15 
   16 #include <net/if.h>
   17 
   18 #include <netinet/in.h>
   19 
   20 #include <netinet/in_systm.h>
   21 #include <netinet/ip.h>
   22 #include <netinet/udp.h>
   23 #include <netinet/tcp.h>
   24 
   25 #include <stdio.h>
   26 #include <pcap.h>
   27 #include <signal.h>
   28 #include <stdlib.h>
   29 
   30 #include <sys/time.h>
   31 
   32 #include <sys/wait.h>
   33 #include <dirent.h>
   34 
   35 #if HAVE_INTTYPES_H
   36 #include <inttypes.h>
   37 #endif
   38 
   39 #ifdef TYPEDEF_UINT32
   40 typedef u_int32_t uint32_t;
   41 #endif
   42 
   43 #ifdef TYPEDEF_UINT16
   44 typedef u_int16_t uint16_t;
   45 #endif
   46 
   47 #ifdef HAVE_GD_H
   48 #include <gd.h>
   49 #else 
   50 #ifdef HAVE_GD_GD_H
   51 #include <gd/gd.h>
   52 #endif
   53 #endif
   54 
   55 #ifdef HAVE_GDFONTS_H
   56 #include <gdfonts.h>
   57 #else 
   58 #ifdef HAVE_GD_GDFONTS_H
   59 #include <gd/gdfonts.h>
   60 #endif
   61 #endif
   62 
   63 #include <netdb.h>
   64 
   65 #include <time.h>
   66 
   67 #include <syslog.h>
   68 
   69 #define IP_NUM 4000         // TODO: Do this dynamicly to save ram and/or scale bigger
   70 #define SUBNET_NUM 100
   71 
   72 #define XWIDTH 900
   73 #define YHEIGHT 256L
   74 #define XOFFSET 90L
   75 #define YOFFSET 45L
   76 
   77 #define NR_WORKER_CHILDS 3
   78 
   79 #define RANGE1 172800.0     // 2 days
   80 #define RANGE2 604800.0     // 7 days
   81 #define RANGE3 3024000.0    // 35 days
   82 #define RANGE4 35640000.0   // 412.5ish days 
   83 
   84 #define INTERVAL1 200L      // 150 -60 (213 is the perfect interval?)
   85 #define INTERVAL2 600L      // 10 minutes
   86 #define INTERVAL3 3600L     // 1 hour
   87 #define INTERVAL4 43200L    // 12 hours 
   88 #define CONFIG_GRAPHINTERVALS 1    // 2 -5 Number of Intervals to wait before redrawing the graphs
   89 
   90 #define CONFIG_GRAPHCUTOFF 1024*1024    // If total data transfered doesn't reach at least this number we don't graph the ip
   91 
   92 #define LEAD .05    // % Of time to lead the graph
   93 
   94 #define TRUE 1
   95 #define FALSE 0
   96 
   97 #define CONFIG_METAREFRESH 150
   98 
   99 #define PERMIS 1
  100 
  101 #define DB_PGSQL 1
  102 
  103 struct config
  104     {
  105     char *dev;
  106     char *filter;
  107     unsigned int skip_intervals;
  108     unsigned long long graph_cutoff;
  109     int promisc;
  110     int output_cdf;
  111     int recover_cdf;
  112     int graph;
  113     double range;
  114     unsigned long long interval;
  115     char tag;
  116     unsigned int meta_refresh;
  117     int output_database;
  118     char *db_connect_string;
  119     char *sensor_id;
  120     };
  121 
  122 struct SubnetData
  123     {
  124     uint32_t ip;
  125     uint32_t mask;
  126     } SubnetTable[SUBNET_NUM];
  127 
  128 struct Statistics
  129     {
  130     unsigned long long total;
  131 
  132     unsigned long long icmp;
  133     unsigned long long udp;
  134     unsigned long long tcp;
  135 
  136     unsigned long long ftp;
  137     unsigned long long http;
  138     unsigned long long p2p;
  139     };
  140 
  141 struct IPData
  142     {
  143     time_t timestamp;
  144     uint32_t ip;    // Host byte order
  145     struct Statistics Send;
  146     struct Statistics Receive;
  147     } IpTable[IP_NUM];
  148 
  149 struct SummaryData
  150     {
  151     uint32_t IP;
  152     int Graph;  // TRUE or FALSE, Did we write out a graph for this ip
  153     unsigned long long Total;
  154     unsigned long long TotalSent;
  155     unsigned long long TotalReceived;
  156     unsigned long long ICMP;
  157     unsigned long long UDP;
  158     unsigned long long TCP;
  159     unsigned long long FTP;
  160     unsigned long long HTTP;    
  161     unsigned long long P2P;
  162     };
  163 
  164 struct IPDataStore
  165     {
  166     uint32_t ip;    
  167     struct DataStoreBlock *FirstBlock;  // This is structure is allocated at the same time, so it always exists.
  168         
  169     struct IPDataStore *Next;
  170     };
  171 
  172 #define IPDATAALLOCCHUNKS 100
  173 struct DataStoreBlock
  174     {
  175     time_t LatestTimestamp;
  176     int NumEntries;  // Is the index of the first unused entry in IPData
  177     struct IPData *Data;  // These are allocated at creation, and thus always exist
  178 
  179     struct DataStoreBlock *Next;
  180     };
  181 
  182 // ****************************************************************************************
  183 // ** Function Prototypes
  184 // ****************************************************************************************
  185 
  186 // ************ A fork that orphans the child
  187 int             fork2();
  188 
  189 // ************ The function that gets called with each packet
  190 void            PacketCallback(u_char *user, const struct pcap_pkthdr *h, const u_char *p);
  191 
  192 // ************ Reads a CDF file from a previous run
  193 void RecoverDataFromCDF(void);
  194 
  195 // ************ This function converts and IP to a char string
  196 char inline     *HostIp2CharIp(unsigned long ipaddr, char *buffer);
  197 
  198 // ************ This function converts the numbers for each quad into an IP
  199 inline uint32_t IpAddr(unsigned char q1, unsigned char q2, unsigned char q3, unsigned char q4);
  200 
  201 // ************ This function adds the packet's size to the proper entries in the data structure
  202 inline void     Credit(struct Statistics *Stats, const struct ip *ip);
  203 
  204 // ************ Finds an IP in our IPTable
  205 inline struct IPData *FindIp(uint32_t ipaddr);
  206 
  207 // ************ Writes our IPTable to Disk or to the Ram cache
  208 void            CommitData(time_t timestamp);
  209 
  210 // ************ Creates our Graphs
  211 void            GraphIp(struct IPDataStore *DataStore, struct SummaryData *SummaryData, time_t timestamp);
  212 void            PrepareXAxis(gdImagePtr im, time_t timestamp);
  213 void            PrepareYAxis(gdImagePtr im, unsigned long long int YMax);
  214 unsigned long long GraphData(gdImagePtr im, gdImagePtr im2, struct IPDataStore *DataStore, time_t timestamp,  struct SummaryData *SummaryData);
  215 
  216 
  217 // ************ Misc
  218 inline void     DstCredit(uint32_t ipaddr, unsigned int psize);
  219 void            MakeIndexPages(int NumGraphs, struct SummaryData *SummaryData[]);