1 //===========================================================================
2 // @(#) $Name: arts++-1-1-a13 $
3 // @(#) $Id: ArtsAttribute.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 _ARTSATTRIBUTE_HH_
44 #define _ARTSATTRIBUTE_HH_
45 
46 extern "C" {
47 #include <sys/types.h>
48 #include <assert.h>
49 
50 #include "artslocal.h"
51 #include "caida_t.h"
52 }
53 
54 #include <string>
55 #ifdef HAVE_IOSTREAM
56   #include <iostream>
57 #else
58   #include <iostream.h>
59 #endif
60 
61 //---------------------------------------------------------------------------
62 //                 Object Attribute Identifiers
63 //---------------------------------------------------------------------------
64 
65 //---------------------------------------------------------------------------
66 // pad attribute
67 //---------------------------------------------------------------------------
68 const uint32_t artsC_ATTR_PAD();
69 #define artsC_ATTR_PAD          0
70 
71 //---------------------------------------------------------------------------
72 //  ASCII comment string attribute
73 //---------------------------------------------------------------------------
74 const uint32_t artsC_ATTR_COMMENT();
75 #define artsC_ATTR_COMMENT      1
76 
77 //---------------------------------------------------------------------------
78 //  Creation time attribute
79 //---------------------------------------------------------------------------
80 const uint32_t artsC_ATTR_CREATION();
81 #define artsC_ATTR_CREATION     2
82 
83 //---------------------------------------------------------------------------
84 //  Time range attribute
85 //---------------------------------------------------------------------------
86 const uint32_t artsC_ATTR_PERIOD();
87 #define artsC_ATTR_PERIOD       3
88 
89 //---------------------------------------------------------------------------
90 //  Host attribute (an IP address)
91 //---------------------------------------------------------------------------
92 const uint32_t artsC_ATTR_HOST();
93 #define artsC_ATTR_HOST         4
94 
95 //---------------------------------------------------------------------------
96 //  Interface name attribute (ifDescr)
97 //---------------------------------------------------------------------------
98 const uint32_t artsC_ATTR_IFDESCR();
99 #define artsC_ATTR_IFDESCR      5
100 
101 //---------------------------------------------------------------------------
102 //  interface index attribute
103 //---------------------------------------------------------------------------
104 const uint32_t artsC_ATTR_IFINDEX();
105 #define artsC_ATTR_IFINDEX      6
106 
107 //---------------------------------------------------------------------------
108 //  interface IP address attribute
109 //---------------------------------------------------------------------------
110 const uint32_t artsC_ATTR_IFIPADDR();
111 #define artsC_ATTR_IFIPADDR     7
112 
113 //---------------------------------------------------------------------------
114 //  pair of IP addresses
115 //---------------------------------------------------------------------------
116 const uint32_t artsC_ATTR_HOSTPAIR();
117 #define artsC_ATTR_HOSTPAIR     8
118 
119 //---------------------------------------------------------------------------
120 //                       Object Attribute Formats
121 //---------------------------------------------------------------------------
122 
123 //---------------------------------------------------------------------------
124 //  Unsigned 8-bit value
125 //---------------------------------------------------------------------------
126 const uint8_t artsC_UBIT8();
127 #define artsC_UBIT8               2
128 
129 //---------------------------------------------------------------------------
130 //  Unsigned 16-bit value
131 //---------------------------------------------------------------------------
132 const uint8_t artsC_UBIT16();
133 #define artsC_UBIT16              3
134 
135 //---------------------------------------------------------------------------
136 //  Unsigned 32-bit value
137 //---------------------------------------------------------------------------
138 const uint8_t artsC_UBIT32();
139 #define artsC_UBIT32              4
140 
141 //---------------------------------------------------------------------------
142 //  Unsigned 64-bit value
143 //---------------------------------------------------------------------------
144 const uint8_t artsC_UBIT64();
145 #define artsC_UBIT64              5
146 
147 //---------------------------------------------------------------------------
148 //  Signed 8-bit value
149 //---------------------------------------------------------------------------
150 const uint8_t artsC_BIT8();
151 #define artsC_BIT8                6
152 
153 //---------------------------------------------------------------------------
154 //  Signed 16-bit value
155 //---------------------------------------------------------------------------
156 const uint8_t artsC_BIT16();
157 #define artsC_BIT16               7
158 
159 //---------------------------------------------------------------------------
160 //  Signed 32-bit value
161 //---------------------------------------------------------------------------
162 const uint8_t artsC_BIT32();
163 #define artsC_BIT32               8
164 
165 //---------------------------------------------------------------------------
166 //  Signed 64-bit value
167 //---------------------------------------------------------------------------
168 const uint8_t artsC_BIT64();
169 #define artsC_BIT64               9
170 
171 //---------------------------------------------------------------------------
172 //  Null-terminated 8-bit string
173 //---------------------------------------------------------------------------
174 const uint8_t artsC_STRING8();
175 #define artsC_STRING8            10
176 
177 //---------------------------------------------------------------------------
178 //  8-bit character array
179 //---------------------------------------------------------------------------
180 const uint8_t artsC_CHAR8();
181 #define artsC_CHAR8              11
182 
183 //---------------------------------------------------------------------------
184 //  IP Address
185 //---------------------------------------------------------------------------
186 const uint8_t artsC_IPV4_ADDRESS();
187 #define artsC_IPV4_ADDRESS       12
188 
189 //---------------------------------------------------------------------------
190 //  Unix GMT date value
191 //---------------------------------------------------------------------------
192 const uint8_t artsC_UNIX_DATE();
193 #define artsC_UNIX_DATE          13
194 
195 //---------------------------------------------------------------------------
196 //  Pair of unix GMT date values
197 //---------------------------------------------------------------------------
198 const uint8_t artsC_UNIX_DATE_RANGE();
199 #define artsC_UNIX_DATE_RANGE    14
200 
201 //---------------------------------------------------------------------------
202 //  32-bit floating point (XDR)
203 //---------------------------------------------------------------------------
204 const uint8_t artsC_FLOAT32();
205 #define artsC_FLOAT32            15
206 
207 //---------------------------------------------------------------------------
208 //  64-bit float (XDR double)
209 //---------------------------------------------------------------------------
210 const uint8_t artsC_DOUBLE64();
211 #define artsC_DOUBLE64           16
212 
213 //---------------------------------------------------------------------------
214 //  pair of IPv4 addresses
215 //---------------------------------------------------------------------------
216 const uint8_t artsC_IPV4_ADDRESS_PAIR();
217 #define artsC_IPV4_ADDRESS_PAIR  17
218 
219 //---------------------------------------------------------------------------
220 //  class ArtsAttribute
221 //---------------------------------------------------------------------------
222 //!  Class to encapsulate an ARTS attribute.
223 //---------------------------------------------------------------------------
224 class ArtsAttribute
225 {
226 public:
227 
228   //-------------------------------------------------------------------------
229   //                             ArtsAttribute()
230   //.........................................................................
231   //!  constructor
232   //-------------------------------------------------------------------------
233   ArtsAttribute();
234 
235   ArtsAttribute(const ArtsAttribute & attribute);
236 
237   //-------------------------------------------------------------------------
238   //                            ~ArtsAttribute()
239   //.........................................................................
240   //!  destructor
241   //-------------------------------------------------------------------------
242   ~ArtsAttribute();
243 
244   //-------------------------------------------------------------------------
245   //     ArtsAttribute & operator = (const ArtsAttribute & artsAttribute)
246   //.........................................................................
247   //!  Overloaded '=' operator for deep-copying an ArtsAttribute.
248   //-------------------------------------------------------------------------
249   ArtsAttribute & operator = (const ArtsAttribute & artsAttribute);
250 
251   //-------------------------------------------------------------------------
252   //                        uint32_t Identifier() const
253   //.........................................................................
254   //!  Returns the attribute identifier.
255   //-------------------------------------------------------------------------
256   uint32_t Identifier() const;
257 
258   //-------------------------------------------------------------------------
259   //                 uint32_t Identifier(uint32_t identifier)
260   //.........................................................................
261   //!  Sets and returns the attribute identifier.
262   //-------------------------------------------------------------------------
263   uint32_t Identifier(uint32_t identifier);
264 
265   //-------------------------------------------------------------------------
266   //                          uint8_t Format() const
267   //.........................................................................
268   //!  Returns the attribute format.
269   //-------------------------------------------------------------------------
270   uint8_t Format() const;
271 
272   //-------------------------------------------------------------------------
273   //                      uint8_t Format(uint8_t format)
274   //.........................................................................
275   //!  Sets and returns the attribute format.
276   //-------------------------------------------------------------------------
277   uint8_t Format(uint8_t format);
278 
279   //-------------------------------------------------------------------------
280   //                          uint32_t Length() const
281   //.........................................................................
282   //!  Returns the attribute length.
283   //-------------------------------------------------------------------------
284   uint32_t Length() const;
285 
286   //-------------------------------------------------------------------------
287   //                     uint32_t Length(uint32_t length)
288   //.........................................................................
289   //!  Sets and returns the attribute length.
290   //-------------------------------------------------------------------------
291   uint32_t Length(uint32_t length);
292 
293   //-------------------------------------------------------------------------
294   //                       const std::string Comment() const
295   //.........................................................................
296   //!  Returns the attribute comment if the attribute really is a comment
297   //!  attribute, otherwise asserts.
298   //-------------------------------------------------------------------------
299   const std::string Comment() const;
300 
301   //-------------------------------------------------------------------------
302   //                         uint32_t Creation() const
303   //.........................................................................
304   //!  Returns the attribute creation timestamp if the attribute identifier
305   //!  is artsC_ATTR_CREATION (i.e. the attirbute really is a creation
306   //!  attribute), else asserts.
307   //-------------------------------------------------------------------------
308   uint32_t Creation() const;
309 
310   //-------------------------------------------------------------------------
311   //                   uint32_t Creation(uint32_t creation)
312   //.........................................................................
313   //!  Sets and returns the attribute creation timestamp if the attribute
314   //!  identifier is artsC_ATTR_CREATION (i.e. the attirbute really is a
315   //!  creation attribute), else asserts.
316   //-------------------------------------------------------------------------
317   uint32_t Creation(uint32_t creation);
318 
319   //-------------------------------------------------------------------------
320   //                      const uint32_t *Period() const
321   //.........................................................................
322   //!  Returns the attribute period if the attribute is really a period
323   //!  attribute, else asserts.
324   //-------------------------------------------------------------------------
325   const uint32_t *Period() const;
326 
327   //-------------------------------------------------------------------------
328   //          uint32_t *Period(uint32_t startTime, uint32_t endTime)
329   //.........................................................................
330   //!  Sets and returns the attribute period if the attribute is really a
331   //!  period attribute, else asserts.
332   //-------------------------------------------------------------------------
333   uint32_t *Period(uint32_t startTime, uint32_t endTime);
334 
335   //-------------------------------------------------------------------------
336   //                          ipv4addr_t Host() const
337   //.........................................................................
338   //!  Returns the attribute IP address if the attribute is really a host
339   //!  attribute, else asserts.
340   //-------------------------------------------------------------------------
341   ipv4addr_t Host() const;
342 
343   //-------------------------------------------------------------------------
344   //                     ipv4addr_t Host(ipv4addr_t host)
345   //.........................................................................
346   //!  Sets and returns the attribute IP address if the attribute is really a
347   //!  host attribute, else asserts.
348   //-------------------------------------------------------------------------
349   ipv4addr_t Host(ipv4addr_t host);
350 
351   //--------------------------------------------------------------------------
352   //       const ipv4addr_t * HostPair() const
353   //..........................................................................
354   //!  Returns the host pair attribute value if attribute is really a host
355   //!  pair attribute, else asserts.
356   //--------------------------------------------------------------------------
357   const ipv4addr_t * HostPair() const;
358 
359   //--------------------------------------------------------------------------
360   //       const ipv4addr_t * HostPair(ipv4addr_t host1, ipv4addr_t host2)
361   //..........................................................................
362   //!  Sets and returns the attribute host pair value id the attribute is
363   //!  really a host pair attribute, else asserts.
364   //--------------------------------------------------------------------------
365   const ipv4addr_t * HostPair(ipv4addr_t host1, ipv4addr_t host2);
366 
367   //-------------------------------------------------------------------------
368   //                      const std::string IfDescr() const
369   //.........................................................................
370   //!  Returns the attribute interface name if the attribute is really an
371   //!  ifDescr attribute, else asserts.
372   //-------------------------------------------------------------------------
373   const std::string IfDescr() const;
374 
375   //-------------------------------------------------------------------------
376   //                std::string IfDescr(const std::string & ifDescr)
377   //.........................................................................
378   //!  Sets and returns the attribute interface name if the attribute is
379   //!  really an ifDescr attribute, else asserts.
380   //-------------------------------------------------------------------------
381   std::string IfDescr(const std::string & ifDescr);
382 
383   //-------------------------------------------------------------------------
384   //                      uint16_t IfIndex() const
385   //.........................................................................
386   //!  Returns the interface index if the attribute is really an
387   //!  interface index attribute, else asserts.
388   //-------------------------------------------------------------------------
389   uint16_t IfIndex() const;
390 
391   //-------------------------------------------------------------------------
392   //                 uint16_t IfIndex(uint16_t ifIndex)
393   //.........................................................................
394   //!  Sets and returns the ifIndex if the attribute is really an
395   //!  interface index attribute, else asserts.
396   //-------------------------------------------------------------------------
397   uint16_t IfIndex(uint16_t ifIndex);
398 
399   //--------------------------------------------------------------------------
400   //                       ipv4addr_t IfIpAddr() const
401   //..........................................................................
402   //!  Returns the interface IP address if the attribute is really an
403   //!  interface IP address attribute, else asserts.
404   //--------------------------------------------------------------------------
405   ipv4addr_t IfIpAddr() const;
406 
407   //--------------------------------------------------------------------------
408   //                 ipv4addr_t IfIpAddr(ipv4addr_t ipAddr)
409   //..........................................................................
410   //!  Sets and returns the interface IP address if the attribute is
411   //!  really an interface IP address attribute, else asserts.
412   //--------------------------------------------------------------------------
413   ipv4addr_t IfIpAddr(ipv4addr_t ipAddr);
414 
415   //-------------------------------------------------------------------------
416   //                          void *Arbitrary() const
417   //.........................................................................
418   //!  Returns the value of an arbitrary-format ARTS attribute.  Arbitrary
419   //!  attributes can be used when a pre-defined attribute type does not
420   //!  exist for the type of data stored in the attribute.  For example,
421   //!  an application-specific attribute.  The pointer returned here is
422   //!  just a pointer to a block of memory of length ArtsAttribute::Length().
423   //!  It is the responsibility of the caller to convert to an approriate
424   //!  representation.
425   //-------------------------------------------------------------------------
426   void *Arbitrary() const;
427 
428   //-------------------------------------------------------------------------
429   //                     void *Arbitrary(void *arbitrary)
430   //.........................................................................
431   //!  Sets and returns the value of an arbitrary-format ARTS attribute.
432   //!  Note that the pointer passed in should be a pointer to a block of
433   //!  memory of length ArtsAttribute::Length().  Internally the ARTS library
434   //!  will treat the data as opaque.
435   //-------------------------------------------------------------------------
436   void *Arbitrary(void *arbitrary);
437 
438   //-------------------------------------------------------------------------
439   //                    std::ostream& write(std::ostream& os) const
440   //.........................................................................
441   //!  Writes the attribute to an ostream and returns the ostream.
442   //-------------------------------------------------------------------------
443   std::ostream& write(std::ostream& os) const;
444 
445   //-------------------------------------------------------------------------
446   //                         int write(int fd) const
447   //.........................................................................
448   //!  Writes the attribute to a file descriptor.  Returns the number of
449   //!  bytes written on success, -1 on failure.
450   //-------------------------------------------------------------------------
451   int write(int fd) const;
452 
453   //-------------------------------------------------------------------------
454   //                       std::istream& read(std::istream& is)
455   //.........................................................................
456   //!  Reads an attribute from an istream and returns the istream.
457   //-------------------------------------------------------------------------
458   std::istream& read(std::istream& is);
459 
460   //-------------------------------------------------------------------------
461   //                            int read(int fd)
462   //.........................................................................
463   //!  Reads an attribute from a file descriptor and returns the number of
464   //!  bytes read.  UNTESTED
465   //-------------------------------------------------------------------------
466   int read(int fd);  // UNTESTED
467 
468   //-------------------------------------------------------------------------
469   //                        uint32_t  Bytes() const
470   //.........................................................................
471   //!  Returns the number of bytes required to store the attribute on disk.
472   //-------------------------------------------------------------------------
Bytes() const473   uint32_t  Bytes() const
474   {
475     return(sizeof(this->_identifier) + sizeof(this->_length) + this->_length);
476   }
477 
478   //-------------------------------------------------------------------------
479   //     friend std::ostream & operator << (std::ostream &os,
480   //                                   ArtsAttribute & artsAttribute)
481   //.........................................................................
482   //!  Overloaded '<<' operator to dump attribute to an ostream in a
483   //!  himan-readable form.
484   //-------------------------------------------------------------------------
485   friend std::ostream & operator << (std::ostream &os,
486                                 ArtsAttribute & artsAttribute);
487 
488   #ifndef NDEBUG
489     //------------------------------------------------------------------------
490     //                   static uint32_t NumObjects()
491     //........................................................................
492     //
493     //------------------------------------------------------------------------
NumObjects()494     static uint32_t NumObjects()
495     {
496       return(_numObjects);
497     }
498   #endif  // NDEBUG
499 
500 private:
501   uint32_t  _identifier;
502   uint8_t   _format;
503   uint32_t  _length;
504   union {
505     uint8_t               pad;
506     std::string          *comment;
507     uint32_t              creation;
508     uint32_t              period[2];
509     ipv4addr_t            host;
510     ipv4addr_t            hostPair[2];
511     uint16_t              ifIndex;
512     ipv4addr_t            ifIpAddr;
513     std::string          *ifDescr;
514     void                 *arbitrary;
515   } _value;  //  Attribute value
516 
517   static uint32_t _numObjects;
518 };
519 
520 #endif  //  _ARTSATTRIBUTE_HH_
521