1 //===========================================================================
2 //  $Name: arts++-1-1-a13 $
3 //  $Id: ArtsAsMatrixData.hh,v 1.2 2004/04/21 23:51:25 kkeys Exp $
4 //===========================================================================
5 //  Copyright Notice
6 //
7 //  By accessing this software, arts++, you are duly informed
8 //  of and agree to be bound by the conditions described below in this
9 //  notice:
10 //
11 //  This software product, arts++, is developed by Daniel W. McRobb, and
12 //  copyrighted(C) 1998 by the University of California, San Diego
13 //  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
14 //  NCR-9711092, under which part of this code was developed.
15 //
16 //  There is no charge for arts++ software.  You can redistribute it
17 //  and/or modify it under the terms of the GNU Lesser General Public
18 //  License, Version 2.1, February 1999, which is incorporated by
19 //  reference herein.
20 //
21 //  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
22 //  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
23 //  of it will not infringe on any third party's intellectual
24 //  property rights.
25 //
26 //  You should have received a copy of the GNU Lesser General Public
27 //  License along with arts++.  Copies can also be obtained from:
28 //
29 //    http://www.gnu.org/copyleft/lesser.html
30 //
31 //  or by writing to:
32 //
33 //  Free Software Foundation, Inc.
34 //  59 Temple Place, Suite 330
35 //  Boston, MA 02111-1307
36 //  USA
37 //
38 //  Or contact:
39 //
40 //    info@caida.org
41 //===========================================================================
42 
43 #ifndef ARTSASMATRIXDATA_HH_
44 #define ARTSASMATRIXDATA_HH_
45 
46 extern "C" {
47 #include "artslocal.h"
48 #include "caida_t.h"
49 }
50 
51 #include <vector>
52 
53 #ifdef HAVE_IOSTREAM
54   #include <iostream>
55 #else
56   #include <iostream.h>
57 #endif
58 // #include <istream.h>
59 // #include <ostream.h>
60 #include <algorithm>
61 
62 #include "ArtsAsMatrixEntry.hh"
63 
64 //---------------------------------------------------------------------------
65 //  class ArtsAsMatrixData
66 //---------------------------------------------------------------------------
67 //!  This class encapsulates the data portion of an ARTS AS matrix object.
68 //!  This class is not normally instantiated directly, but instead
69 //!  instantiated from the containing object (an Arts or ArtsAsMatrix
70 //!  object).
71 //---------------------------------------------------------------------------
72 class ArtsAsMatrixData
73 {
74 public:
75 
76   //-------------------------------------------------------------------------
77   //                         ArtsAsMatrixData()
78   //.........................................................................
79   //!  constructor
80   //-------------------------------------------------------------------------
81   ArtsAsMatrixData();
82 
83   //--------------------------------------------------------------------------
84   //                        ~ArtsAsMatrixData()
85   //..........................................................................
86   //!  destructor
87   //--------------------------------------------------------------------------
88   ~ArtsAsMatrixData();
89 
90   //-------------------------------------------------------------------------
91   //                  inline uint16_t SampleInterval() const
92   //.........................................................................
93   //!  Returns the sampling ratio if the AS matrix was built using packet
94   //!  sampling techniques.  Normally this value is 1, meaning we're working
95   //!  with complete data sets (for example, from Cisco flow-export).
96   //-------------------------------------------------------------------------
SampleInterval() const97   inline uint16_t SampleInterval() const
98   {
99     return(this->_sampleInterval);
100   }
101 
102   //-------------------------------------------------------------------------
103   //         inline uint16_t SampleInterval(uint16_t sampleInterval)
104   //.........................................................................
105   //!  Sets and returns the sampling ratio if the AS matrix was built using
106   //!  packet sampling techniques.  Normally this value is 1, meaning we're
107   //!  working with complete data sets (for example, from Cisco flow-export).
108   //-------------------------------------------------------------------------
SampleInterval(uint16_t sampleInterval)109   inline uint16_t SampleInterval(uint16_t sampleInterval)
110   {
111     this->_sampleInterval = sampleInterval;
112     return(this->_sampleInterval);
113   }
114 
115   //-------------------------------------------------------------------------
116   //                      inline uint32_t Count() const
117   //.........................................................................
118   //!  Returns the number of AS matrix entries (cells) in an
119   //!  ArtsAsMatrixData object.
120   //-------------------------------------------------------------------------
Count() const121   inline uint32_t Count() const
122   {
123     return(this->_count);
124   }
125 
126   //-------------------------------------------------------------------------
127   //                  inline uint32_t Count(uint32_t count)
128   //.........................................................................
129   //!  Sets and Returns the number of AS matrix entries (cells) in an
130   //!  ArtsAsMatrixData object.
131   //-------------------------------------------------------------------------
Count(uint32_t count)132   inline uint32_t Count(uint32_t count)
133   {
134     this->_count = count;
135     return(this->_count);
136   }
137 
138   //-------------------------------------------------------------------------
139   //                    inline uint64_t TotalPkts() const
140   //.........................................................................
141   //!  Returns the total packets of traffic in the ArtsAsMatrixData object.
142   //!  The total is the sum of all traffic from each source to each
143   //!  destination.
144   //-------------------------------------------------------------------------
TotalPkts() const145   inline uint64_t TotalPkts() const
146   {
147     return(this->_totpkts);
148   }
149 
150   //-------------------------------------------------------------------------
151   //              inline uint64_t TotalPkts(uint64_t totalPkts)
152   //.........................................................................
153   //!  Sets and returns the total packets of traffic in the ArtsAsMatrixData
154   //!  object.  The total is the sum of all traffic from each source to each
155   //!  destination.
156   //-------------------------------------------------------------------------
TotalPkts(uint64_t totalPkts)157   inline uint64_t TotalPkts(uint64_t totalPkts)
158   {
159     this->_totpkts = totalPkts;
160     return(this->_totpkts);
161   }
162 
163   //-------------------------------------------------------------------------
164   //                    inline uint64_t TotalBytes() const
165   //.........................................................................
166   //!  Returns the total bytes of traffic in the ArtsAsMatrixData object.
167   //!  The total is the sum of all traffic from each source to each
168   //!  destination.
169   //-------------------------------------------------------------------------
TotalBytes() const170   inline uint64_t TotalBytes() const
171   {
172     return(this->_totbytes);
173   }
174 
175   //-------------------------------------------------------------------------
176   //             inline uint64_t TotalBytes(uint64_t totalBytes)
177   //.........................................................................
178   //!  Sets and returns the total bytes of traffic in the ArtsAsMatrixData
179   //!  object.  The total is the sum of all traffic from each source to each
180   //!  destination.
181   //-------------------------------------------------------------------------
TotalBytes(uint64_t totalBytes)182   inline uint64_t TotalBytes(uint64_t totalBytes)
183   {
184     this->_totbytes = totalBytes;
185     return(this->_totbytes);
186   }
187 
188   //-------------------------------------------------------------------------
189   //                     inline uint64_t Orphans() const
190   //.........................................................................
191   //
192   //-------------------------------------------------------------------------
Orphans() const193   inline uint64_t Orphans() const
194   {
195     return(this->_orphans);
196   }
197 
198   //-------------------------------------------------------------------------
199   //                inline uint64_t Orphans(uint64_t orphans)
200   //.........................................................................
201   //
202   //-------------------------------------------------------------------------
Orphans(uint64_t orphans)203   inline uint64_t Orphans(uint64_t orphans)
204   {
205     this->_orphans = orphans;
206     return(this->_orphans);
207   }
208 
209   //-------------------------------------------------------------------------
210   //          inline std::vector<ArtsAsMatrixEntry> & AsEntries() const
211   //.........................................................................
212   //!  Return a reference to the vector of ArtsAsMatrixEntry objects.  Note
213   //!  that even though this member may be called from a const object, the
214   //!  returned reference is not const; the vector is mutable.
215   //-------------------------------------------------------------------------
AsEntries() const216   inline std::vector<ArtsAsMatrixEntry> & AsEntries() const
217   {
218     return(this->_asEntries);
219   }
220 
221   //-------------------------------------------------------------------------
222   //              uint32_t Length(uint8_t version = 0) const
223   //.........................................................................
224   //!  Returns the length required to store the ArtsAsMatrixData object
225   //!  on disk.
226   //-------------------------------------------------------------------------
227   uint32_t Length(uint8_t version = 0) const;
228 
229   //-------------------------------------------------------------------------
230   //            std::istream& read(std::istream& is, uint8_t version = 0)
231   //.........................................................................
232   //!  Reads an ArtsAsMatrixData object from an istream.  Returns the
233   //!  istream.
234   //-------------------------------------------------------------------------
235   std::istream& read(std::istream& is, uint8_t version = 0);
236 
237   //-------------------------------------------------------------------------
238   //                  int read(int fd, uint8_t version = 0)
239   //.........................................................................
240   //!  Reads an ArtsAsMatrixData object from a file descriptor.  Returns
241   //!  the number of bytes read if successful, -1 on failure.
242   //-------------------------------------------------------------------------
243   int read(int fd, uint8_t version = 0);
244 
245   //-------------------------------------------------------------------------
246   //             std::ostream& write(std::ostream& os, uint8_t version = 0)
247   //.........................................................................
248   //!  Writes an ArtsAsMatrixData object to an ostream.  Returns the
249   //!  ostream.
250   //-------------------------------------------------------------------------
251   std::ostream& write(std::ostream& os, uint8_t version = 0);
252 
253   //-------------------------------------------------------------------------
254   //                  int write(int fd, uint8_t version = 0)
255   //.........................................................................
256   //!  Writes an ArtsAsMatrixData object to a file descriptor.  Returns
257   //!  the number of bytes written on success, -1 on failure.
258   //-------------------------------------------------------------------------
259   int write(int fd, uint8_t version = 0);
260 
261   //-------------------------------------------------------------------------
262   //    std::ostream& operator << (std::ostream& os,
263   //                          const ArtsAsMatrixData & artsAsMatrixData)
264   //.........................................................................
265   //!  Overloaded '<<' operator.  Dumps the contents of an ArtsAsMatrixData
266   //!  object to an ostream in a human-readable form.  Returns the ostream.
267   //-------------------------------------------------------------------------
268   friend std::ostream& operator << (std::ostream& os,
269                                const ArtsAsMatrixData & artsAsMatrixData);
270 
271   void SortEntriesByBytes();
272 
273   void SortEntriesByPkts();
274 
275   #ifndef NDEBUG
276     //------------------------------------------------------------------------
277     //                  static uint32_t NumObjects()
278     //........................................................................
279     //
280     //------------------------------------------------------------------------
NumObjects()281     static uint32_t NumObjects()
282     {
283       return(_numObjects);
284     }
285   #endif
286 
287 private:
288   uint16_t                           _sampleInterval;
289   uint32_t                           _count;
290   uint64_t                           _totpkts;
291   uint64_t                           _totbytes;
292   uint64_t                           _orphans;
293   mutable std::vector<ArtsAsMatrixEntry>  _asEntries;
294 
295   static uint32_t  _numObjects;
296 };
297 
298 #endif  /*  ARTSASMATRIXDATA_HH_  */
299