libbgp  0.6
A C++ BGP Library.
bgp-update-message.h
Go to the documentation of this file.
1 
11 #ifndef BGP_UPDATE_MSG_H_
12 #define BGP_UPDATE_MSG_H_
13 #include <vector>
14 #include <unistd.h>
15 #include <memory>
16 #include "prefix4.h"
17 #include "bgp-message.h"
18 #include "bgp-path-attrib.h"
19 
20 namespace libbgp {
21 
28 class BgpUpdateMessage : public BgpMessage {
29 public:
30  std::vector<Prefix4> withdrawn_routes;
31  std::vector<std::shared_ptr<BgpPathAttrib>> path_attribute;
32  std::vector<Prefix4> nlri;
33 
34  BgpUpdateMessage(BgpLogHandler *logger, bool use_4b_asn);
35 
36  // get attribute by type, if attrib of that type does not exist, exception
37  // will be thrown
38  BgpPathAttrib &getAttrib(uint8_t type);
39 
40  // get const attribute by type, if attrib of that type does not exist,
41  // exception will be thrown
42  const BgpPathAttrib &getAttrib(uint8_t type) const;
43 
44  // return true if this type of attribute is in the message
45  bool hasAttrib(uint8_t type) const;
46 
47  // copy and add an attribute, return false if attrib of same type already exists
48  bool addAttrib(const BgpPathAttrib &attrib);
49 
50  // copy and replace attribute list with attrs
51  bool setAttribs(const std::vector<std::shared_ptr<BgpPathAttrib>> &attrs);
52 
53  // utility function to remove attribute by type
54  bool dropAttrib(uint8_t type);
55 
56  // utility function to update attribute (will be append if not exist)
57  bool updateAttribute(const BgpPathAttrib &attrib);
58 
59  // utility function to drop all non-transitive attribute
60  bool dropNonTransitive();
61 
62  // utility function to set nexthop
63  bool setNextHop(uint32_t nexthop);
64 
65  // utility function to prepend an ASN to AS_PATH (for 2b mode, AS4_PATH and
66  // AS_TRANS will be used)
67  bool prepend(uint32_t asn);
68 
69  // try to recover AS_TRANS in AS_PATH with infomation in AS4_PATH, convert
70  // 2b AS_PATH to 4b AS_PATH, and remove AS4_PATH attribute.
71  bool restoreAsPath();
72 
73  // convert current 4b AS_PATH to 2b AS_PATH and add AS4_PATH attribute.
74  bool downgradeAsPath();
75 
76  // recover 4b aggregator from aggregator4, remove aggregator4
77  bool restoreAggregator();
78 
79  // convert 4b aggregator to 2b aggregator, add aggregator4
80  bool downgradeAggregator();
81 
82  // replace withdrawn with routes
83  bool setWithdrawn4(const std::vector<Prefix4> &routes);
84 
85  // utility function to add a route to withdrawn list
86  bool addWithdrawn4(uint32_t prefix, uint8_t length);
87 
88  // utility function to add a route to withdrawn list
89  bool addWithdrawn4(const Prefix4 &route);
90 
91  // replace NLRI with routes
92  bool setNlri4(const std::vector<Prefix4> &routes);
93 
94  // utility function to add a route to NLRI
95  bool addNlri4(uint32_t prefix, uint8_t length);
96 
97  // utility function to add a route to NLRI
98  bool addNlri4(const Prefix4 &route);
99 
100  // replace withdrawn with routes
101  bool setWithdrawn6(const std::vector<Prefix6> &routes);
102 
103  // replace NLRI with routes
104  bool setNlri6(const std::vector<Prefix6> &routes, const uint8_t nexthop_global[16], const uint8_t nexthop_linklocal[16]);
105 
106  ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const;
107  ssize_t parse(const uint8_t *from, size_t msg_sz);
108  ssize_t write(uint8_t *to, size_t buf_sz) const;
109 
110 private:
111  // utility function to check if attributes are valid (i.e. no dulipicated,
112  // no missing well-known)
113  bool validateAttribs();
114 
115  bool use_4b_asn;
116 };
117 
118 }
119 #endif // BGP_UPDATE_MSG_H_
bool setNextHop(uint32_t nexthop)
Set/Create nexthop attribtue.
bool restoreAsPath()
Restore the AS_PATH attribute to four octets ASN flavor.
The BGP path attributes.
bool updateAttribute(const BgpPathAttrib &attrib)
Add/Update an attribute.
virtual ssize_t length() const
Get size in bytes required to serialize the object.
The BgpPathAttrib class.
bool setWithdrawn4(const std::vector< Prefix4 > &routes)
Set withdrawn routes.
BgpPathAttrib & getAttrib(uint8_t type)
Get mutable reference to attribute by typecode.
ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const
Print implementation.
The BGP Message base.
bool setNlri4(const std::vector< Prefix4 > &routes)
Set NLRIs.
bool addWithdrawn4(uint32_t prefix, uint8_t length)
Add withdrawn route.
bool downgradeAggregator()
Downgrade aggregator to two octets.
ssize_t parse(const uint8_t *from, size_t msg_sz)
Deserialize a BGP message body.
bool restoreAggregator()
Restore aggregator attribute from as4_aggregator.
bool prepend(uint32_t asn)
Prepend ASN to AS_PATH and AS4_PATH (if in 2b-mode ans AS4_PATH exists)
The BgpMessage base class.
Definition: bgp-message.h:35
IPv4 Route/Prefix related utilities.
Definition: prefix4.h:25
Definition: bgp-afi.h:14
IPv4 Route/Prefix related utilities.
The BgpLogHandler class.
ssize_t write(uint8_t *to, size_t buf_sz) const
Serialize a BGP message body.
bool dropNonTransitive()
Drop all non-transitive attributes from the update message.
bool dropAttrib(uint8_t type)
Drop (remove) an attribute from the update message.
bool addNlri4(uint32_t prefix, uint8_t length)
Add NLRI route.
bool hasAttrib(uint8_t type) const
Test if update message has an attribute.
BgpUpdateMessage(BgpLogHandler *logger, bool use_4b_asn)
Construct a new Bgp Update Message:: Bgp Update Message object.
bool setAttribs(const std::vector< std::shared_ptr< BgpPathAttrib >> &attrs)
Replace the attributes list with another attribute list.
bool addAttrib(const BgpPathAttrib &attrib)
Add an attribute to the update message.
bool downgradeAsPath()
Downgrade the AS_PATH to two octets flavor.
The BgpUpdateMessage class.