1 //===========================================================================
2 //  $Name: arts++-1-1-a13 $
3 //  $Id: ArtsBgp4AsPathSegment.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 _ARTSBGP4ASPATHSEGMENT_HH_
44 #define _ARTSBGP4ASPATHSEGMENT_HH_
45 
46 extern "C" {
47 #include "caida_t.h"
48 }
49 
50 #include <vector>
51 
52 //---------------------------------------------------------------------------
53 //  class ArtsBgp4AsPathSegment
54 //---------------------------------------------------------------------------
55 //!  This class abstracts an AS path segement in a BGP AS path attribute.
56 //!  An AS path segment may be of type SET or SEQUENCE; a SET segment is
57 //!  unordered while a SEQUENCE segment is ordered.  See RFC 1771 for
58 //!  more details.
59 //---------------------------------------------------------------------------
60 class ArtsBgp4AsPathSegment
61 {
62 public:
63   //-------------------------------------------------------------------------
64   //!  Define some static constants for AS path segment types.
65   //-------------------------------------------------------------------------
66   static const uint8_t k_segmentTypeSet      = 1;
67   static const uint8_t k_segmentTypeSequence = 2;
68 
69   //--------------------------------------------------------------------------
70   //                         ArtsBgp4AsPathSegment()
71   //..........................................................................
72   //!  constructor
73   //--------------------------------------------------------------------------
74   ArtsBgp4AsPathSegment();
75 
76   //--------------------------------------------------------------------------
77   //   ArtsBgp4AsPathSegment(const ArtsBgp4AsPathSegment & asPathSegment)
78   //..........................................................................
79   //!  copy constructor
80   //--------------------------------------------------------------------------
81   ArtsBgp4AsPathSegment(const ArtsBgp4AsPathSegment & asPathSegment);
82 
83   //--------------------------------------------------------------------------
84   //                        ~ArtsBgp4AsPathSegment()
85   //..........................................................................
86   //!  destructor
87   //--------------------------------------------------------------------------
88   ~ArtsBgp4AsPathSegment();
89 
90   //-------------------------------------------------------------------------
91   //                          uint8_t Type() const
92   //.........................................................................
93   //!  Returns the type of the segment (1 == SET, 2 == SEQUENCE).
94   //-------------------------------------------------------------------------
95   uint8_t Type() const;
96 
97   //-------------------------------------------------------------------------
98   //                       uint8_t Type(uint8_t type)
99   //.........................................................................
100   //!  Sets and returns the type of the segment (1 == SET, 2 == SEQUENCE).
101   //-------------------------------------------------------------------------
102   uint8_t Type(uint8_t type);
103 
104   //-------------------------------------------------------------------------
105   //                      std::vector<uint16_t> & AS() const
106   //.........................................................................
107   //!  Returns a reference to the vector of AS numbers in the segment.
108   //!  Note that this member can be called on a const object though we
109   //!  don't return a const reference; the vector of AS numbers is
110   //!  mutable.
111   //-------------------------------------------------------------------------
112   std::vector<uint16_t> & AS() const;
113 
114   //--------------------------------------------------------------------------
115   //            std::istream & read(std::istream & is, uint8_t version = 0)
116   //..........................................................................
117   //!  Reads the ArtsBgp4AsPathSegment from an istream.  Returns a reference
118   //!  to the istream.
119   //--------------------------------------------------------------------------
120   std::istream & read(std::istream & is, uint8_t version = 0);
121 
122   //--------------------------------------------------------------------------
123   //                  int read(int fd, uint8_t version = 0)
124   //..........................................................................
125   //!  Reads the ArtsBgp4AsPathSegment from a file descriptor.  Returns the
126   //!  number of bytes read on success, -1 on failure.
127   //--------------------------------------------------------------------------
128   int read(int fd, uint8_t version = 0);
129 
130   //--------------------------------------------------------------------------
131   //        std::ostream & write(std::ostream & os, uint8_t version = 0) const
132   //..........................................................................
133   //!  Writes the ArtsBgp4AsPathSegment to an ostream.  Returns a reference
134   //!  to the ostream.
135   //--------------------------------------------------------------------------
136   std::ostream & write(std::ostream & os, uint8_t version = 0) const;
137 
138   //--------------------------------------------------------------------------
139   //              int write(int fd, uint8_t version = 0) const
140   //..........................................................................
141   //!  Writes the ArtsBgp4AsPathSegment to a file descriptor.  Returns the
142   //!  number of bytes written on success, -1 on failure.
143   //--------------------------------------------------------------------------
144   int write(int fd, uint8_t version = 0) const;
145 
146   //--------------------------------------------------------------------------
147   //                uint32_t Length(uint8_t version = 0) const
148   //..........................................................................
149   //!  Returns the number of bytes required to write the
150   //!  ArtsBgp4AsPathSegment to disk.
151   //--------------------------------------------------------------------------
152   uint32_t Length(uint8_t version = 0) const;
153 
154   //--------------------------------------------------------------------------
155   //                         void AddAs(uint16_t as)
156   //..........................................................................
157   //!  Appends an AS number to the segment.
158   //--------------------------------------------------------------------------
159   void AddAs(uint16_t as);
160 
161   //--------------------------------------------------------------------------
162   //                              void Unique()
163   //..........................................................................
164   //!  If the segment is a sequence segment (not an AS_SET), removes
165   //!  adjacent duplicate AS numbers from the segment.
166   //--------------------------------------------------------------------------
167   void Unique();
168 
169   //--------------------------------------------------------------------------
170   //      ArtsBgp4AsPathSegment &
171   //      operator = (const ArtsBgp4AsPathSegment & bgp4AsPathSegment)
172   //..........................................................................
173   //!  Overloaded = operator, deep-copies an ArtsBgp4AsPathSegment.
174   //--------------------------------------------------------------------------
175   ArtsBgp4AsPathSegment &
176   operator = (const ArtsBgp4AsPathSegment & asPathSegment);
177 
178   //--------------------------------------------------------------------------
179   //       friend std::ostream &
180   //       operator << (std::ostream& os,
181   //                    const ArtsBgp4AsPathSegment & bgpAsPathSegment)
182   //..........................................................................
183   //!  Writes the contents of an ArtsBgp4AsPathSegment to an ostream in
184   //!  a human-friendly form.
185   //--------------------------------------------------------------------------
186   friend std::ostream &
187   operator << (std::ostream& os,
188                const ArtsBgp4AsPathSegment & bgpAsPathSegment);
189 
190   #ifndef NDEBUG
191     //------------------------------------------------------------------------
192     //                       static uint32_t NumObjects()
193     //........................................................................
194     //
195     //------------------------------------------------------------------------
NumObjects()196     static uint32_t NumObjects()
197     {
198       return(_numObjects);
199     }
200   #endif  // NDEBUG
201 
202 private:
203   uint8_t                   _type;   // the segment type
204   mutable std::vector<uint16_t>  _AS;     // the segment AS numbers
205 
206   static uint32_t           _numObjects;
207 };
208 
209 #endif  // _ARTSBGP4ASPATHSEGMENT_HH_
210