libbgp  0.6
A C++ BGP Library.
bgp-path-attrib.h
Go to the documentation of this file.
1 
11 #ifndef BGP_PATH_ATTR_H
12 #define BGP_PATH_ATTR_H
13 
14 #include "serializable.h"
15 #include "prefix6.h"
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <vector>
19 
20 namespace libbgp {
21 
27  UNKNOW = -1,
28  RESERVED = 0,
29  ORIGIN = 1,
30  AS_PATH = 2,
31  NEXT_HOP = 3,
32  MULTI_EXIT_DISC = 4,
33  LOCAL_PREF = 5,
34  ATOMIC_AGGREGATE = 6,
35  AGGREATOR = 7,
36  COMMUNITY = 8,
37  MP_REACH_NLRI = 14,
38  MP_UNREACH_NLRI = 15,
39  AS4_PATH = 17,
40  AS4_AGGREGATOR = 18
41 };
42 
47 class BgpPathAttrib : public Serializable {
48 public:
49 
54  bool optional;
55 
60  bool transitive;
61 
66  bool partial;
67 
72  bool extended;
73 
78  uint8_t type_code;
79 
81  BgpPathAttrib(BgpLogHandler *logger, const uint8_t *value, uint16_t val_len);
82 
83  // get attribute type from buffer, return 0 if failed.
84  static uint8_t GetTypeFromBuffer(const uint8_t *buffer, size_t buffer_sz);
85 
86  // print the attribute
87  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
88 
102  virtual ssize_t parse(const uint8_t *from, size_t msg_sz);
103 
113  virtual ssize_t write(uint8_t *to, size_t buf_sz) const;
114 
115  virtual ssize_t length() const;
116 
125  BgpPathAttrib* clone(BgpLogHandler *new_logger) const;
126 
134  virtual BgpPathAttrib* clone() const;
135 
136  virtual ~BgpPathAttrib();
137 
138 protected:
139  // utility function to parse header (flags, typecode, length) from buffer
140  ssize_t parseHeader(const uint8_t *buffer, size_t length);
141 
142  // utility function to print flags
143  ssize_t printFlags(size_t indent, uint8_t **to, size_t *buf_sz) const;
144 
145  // utility function to write header (flags, type_code) to buffer. notice
146  // that length is not write to buffer by this function.
147  ssize_t writeHeader(uint8_t *buffer, size_t buffer_sz) const;
148 
155  uint16_t value_len;
156 
157 private:
158  uint8_t* value_ptr;
159 };
160 
166  IGP = 0,
167  EGP = 1,
168  INCOMPLETE = 2
169 };
170 
176 public:
178  uint8_t origin;
179 
180  BgpPathAttrib* clone() const;
181  ssize_t parse(const uint8_t *buffer, size_t length);
182  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
183  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
184  ssize_t length() const;
185 };
186 
192  AS_SET = 1,
193  AS_SEQUENCE = 2
194 };
195 
201 public:
202  BgpAsPathSegment(bool is_4b, uint8_t type);
203 
208  bool is_4b;
209 
214  uint8_t type;
215  size_t getCount() const;
216  bool prepend(uint32_t asn);
217 
222  std::vector<uint32_t> value;
223 };
224 
230 public:
231  // is_4b: 4b ASN in AS_PATH?
232  BgpPathAttribAsPath(BgpLogHandler *logger, bool is_4b);
233 
234  BgpPathAttrib* clone() const;
235 
240  std::vector<BgpAsPathSegment> as_paths;
241 
246  bool is_4b;
247 
248  // prepend: utility function to prepend an ASN to path
249  bool prepend(uint32_t asn);
250 
251  ssize_t parse(const uint8_t *buffer, size_t length);
252  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
253  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
254  ssize_t length() const;
255 private:
256  // addSeg: add a new AS_SEQUENCE with one ASN to AS_PATH
257  void addSeg(uint32_t asn);
258 };
259 
265 public:
267 
272  uint32_t next_hop;
273 
274  BgpPathAttrib* clone() const;
275  ssize_t parse(const uint8_t *buffer, size_t length);
276  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
277  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
278  ssize_t length() const;
279 };
280 
286 public:
288 
293  uint32_t med;
294 
295  BgpPathAttrib* clone() const;
296  ssize_t parse(const uint8_t *buffer, size_t length);
297  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
298  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
299  ssize_t length() const;
300 };
301 
307 public:
309 
314  uint32_t local_pref;
315 
316  BgpPathAttrib* clone() const;
317  ssize_t parse(const uint8_t *buffer, size_t length);
318  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
319  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
320  ssize_t length() const;
321 };
322 
328 public:
330 
331  BgpPathAttrib* clone() const;
332  ssize_t parse(const uint8_t *buffer, size_t length);
333  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
334  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
335  ssize_t length() const;
336 };
337 
343 public:
344  // is_4b: 4b asn in aggregator?
345  BgpPathAttribAggregator(BgpLogHandler *logger, bool is_4b);
346 
351  uint32_t aggregator;
352 
357  uint32_t aggregator_asn;
358 
363  bool is_4b;
364 
365  BgpPathAttrib* clone() const;
366  ssize_t parse(const uint8_t *buffer, size_t length);
367  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
368  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
369  ssize_t length() const;
370 };
371 
377 public:
379 
384  std::vector<BgpAsPathSegment> as4_paths;
385 
386  bool prepend(uint32_t asn);
387 
388  BgpPathAttrib* clone() const;
389  ssize_t parse(const uint8_t *buffer, size_t length);
390  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
391  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
392  ssize_t length() const;
393 private:
394  void addSeg(uint32_t asn);
395 };
396 
402 public:
404 
409  uint32_t aggregator;
410 
415  uint32_t aggregator_asn4;
416 
417  BgpPathAttrib* clone() const;
418  ssize_t parse(const uint8_t *buffer, size_t length);
419  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
420  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
421  ssize_t length() const;
422 };
423 
429 public:
431 
436  std::vector<uint32_t> communites;
437 
438  BgpPathAttrib* clone() const;
439  ssize_t parse(const uint8_t *buffer, size_t length);
440  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
441  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
442  ssize_t length() const;
443 };
444 
450 public:
452  static int16_t GetAfiFromBuffer(const uint8_t *buffer, size_t length);
453 
458  uint16_t afi;
459 
464  uint8_t safi;
465 
466 protected:
467  ssize_t parseHeader(const uint8_t *buffer, size_t length);
468 };
469 
475 public:
477 
478  uint8_t nexthop_global[16];
479  uint8_t nexthop_linklocal[16];
480  std::vector<Prefix6> nlri;
481 
482  BgpPathAttrib* clone() const;
483  ssize_t parse(const uint8_t *buffer, size_t length);
484  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
485  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
486  ssize_t length() const;
487 };
488 
494 public:
496  BgpPathAttribMpReachNlriUnknow(BgpLogHandler *logger, const uint8_t *nexthop, size_t nexthop_len, const uint8_t *nlri, size_t nlri_len);
498 
499  BgpPathAttrib* clone() const;
500  ssize_t parse(const uint8_t *buffer, size_t length);
501  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
502  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
503  ssize_t length() const;
504 
505  const uint8_t *getNexthop() const;
506  const uint8_t *getNlri() const;
507 
508  size_t getNexthopLength() const;
509  size_t getNlriLength() const;
510 
511 private:
512  uint8_t *nexthop;
513  size_t nexthop_len;
514  uint8_t *nlri;
515  size_t nlri_len;
516 };
517 
523 public:
525  std::vector<Prefix6> withdrawn_routes;
526 
527  BgpPathAttrib* clone() const;
528  ssize_t parse(const uint8_t *buffer, size_t length);
529  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
530  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
531  ssize_t length() const;
532 };
533 
539 public:
541  BgpPathAttribMpUnreachNlriUnknow(BgpLogHandler *logger, const uint8_t *withdrawn, size_t len);
543 
544  BgpPathAttrib* clone() const;
545  ssize_t parse(const uint8_t *buffer, size_t length);
546  ssize_t write(uint8_t *buffer, size_t buffer_sz) const;
547  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
548  ssize_t length() const;
549 
550  const uint8_t *getWithdrawnRoutes() const;
551  size_t getWithdrawnRoutesLength() const;
552 private:
553  uint8_t *withdrawn_routes;
554  size_t withdrawn_routes_len;
555 };
556 
557 }
558 #endif // BGP_PATH_ATTR_H
ssize_t printFlags(size_t indent, uint8_t **to, size_t *buf_sz) const
Utility function to print flags for attribute.
Atomic aggregate attribute.
ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const
Print implementation.
bool partial
Attribute flag: partial.
ssize_t writeHeader(uint8_t *buffer, size_t buffer_sz) const
Write attribute header to buffer. (Flag, Type)
std::vector< uint32_t > value
The segment value.
The BgpPathAttrib class.
virtual ssize_t length() const
Get size in bytes required to serialize the object.
BgpPathAttrib(BgpLogHandler *logger)
Construct a new Bgp Path Attrib:: Bgp Path Attrib object.
MP-BGP ReachNlri IPv6 NLRI class.
bool is_4b
Is ASNs in the attribute four octets?
MP-BGP UnreachNlri container for unknow AFI/SAFI.
bool optional
Attribute flag: Optional.
virtual ssize_t parse(const uint8_t *from, size_t msg_sz)
Deserialize a BGP update message path attribute.
bool is_4b
Is ASNs in the attribute four octets?
std::vector< uint32_t > communites
Raw community attribute in network byte order.
ssize_t parseHeader(const uint8_t *buffer, size_t length)
Utility function to parse attribute header. (Flag, type, length)
uint16_t afi
Address Family Identifier.
The serializable base class.
Definition: serializable.h:26
uint16_t value_len
Attribute length. Length field is only used in deserialization for parseHeader() to pass length field...
AS4_AGGREGATOR attribute.
bool transitive
Attribute flag: Transitive.
uint32_t local_pref
Local Pref.
std::vector< BgpAsPathSegment > as_paths
The AS Path segments.
std::vector< BgpAsPathSegment > as4_paths
The AS4_PATH segments.
BgpPathAttribType
BGP attribute type codes.
uint32_t aggregator_asn4
Aggregator ASN.
uint32_t aggregator_asn
Aggregator ASN.
BgpAsPathSegmentType
AS_PATH segment types.
uint8_t type
Segment type.
Local Pref attribute.
bool extended
Attribute flag: extended.
virtual BgpPathAttrib * clone() const
Clone the attribute.
Definition: bgp-afi.h:14
MP-BGP Reach/Unreach NLRI base class.
The BgpLogHandler class.
IPv6 Route/Prefix related utilities.
An AS_PATH or AS4_PATH segment.
uint32_t aggregator
Aggregator in network byte order.
Multi Exit Discriminator attribute.
BGP community attribute.
virtual ssize_t write(uint8_t *to, size_t buf_sz) const
Serialize a BGP update message path attribute.
uint32_t aggregator
Aggregator in network byte order.
The serializable base.
BgpPathAttribOrigins
BGP origin path attribute values.
uint32_t next_hop
The nexthop in network byte order.
uint8_t safi
Subsequent Address Family Identifier.
uint8_t type_code
Attribute type code.
MP-BGP ReachNlri container for unknow AFI/SAFI.
bool is_4b
Is ASNs in this segment four octets?
static uint8_t GetTypeFromBuffer(const uint8_t *buffer, size_t buffer_sz)
Get type of attribute from buffer.
MP-BGP UnreachNlri IPv6 class.
virtual ~BgpPathAttrib()
Destroy the Bgp Path Attrib:: Bgp Path Attrib object.