Example of adding new routes to RIB while BGP FSM is running. Notify BGP FSM to send updates to the peer with RouteEventBus. This example also shows how you can implement your own BgpOutHandler and BgpLogHandler.
#include <libbgp/bgp-fsm.h>
#include <libbgp/route-event-bus.h>
#include <arpa/inet.h>
#include <sys/types.h>
public:
MyEventHandler(const char *name) {
this->name = name;
}
protected:
if (ev.
type == libbgp::ADD4) {
printRoutes("add", add_ev.routes);
}
if (ev.
type == libbgp::WITHDRAW4) {
printRoutes(
"withdraw", wd_ev.
routes);
}
return false;
}
private:
void printRoutes(const char *type, const std::vector<libbgp::Prefix4> &routes) {
char ip_str[INET_ADDRSTRLEN];
uint32_t prefix = r.getPrefix();
inet_ntop(AF_INET, &prefix, ip_str, INET_ADDRSTRLEN);
printf("[%s] %s: %s/%d\n", name, type, ip_str, r.getLength());
}
}
const char *name;
};
public:
PipedOutHandler() {
other = NULL;
}
this->other = other;
}
bool handleOut(const uint8_t *buffer, size_t length) {
return other->
run(buffer, length) >= 0;
};
private:
};
public:
MyLoghandler(const char *name) {
this->name = name;
}
protected:
void logImpl(const char* str) {
printf("[%s] %s", name, str);
}
private:
const char *name;
};
int main(void) {
PipedOutHandler pipe_local;
MyLoghandler local_logger("local");
local_logger.setLogLevel(libbgp::DEBUG);
MyEventHandler local_handler("local");
local_bgp_config.
asn = 65000;
local_bgp_config.
rib4 = &local_rib;
local_bgp_config.
rev_bus = &local_bus;
local_bgp_config.
clock = NULL;
inet_pton(AF_INET,
"10.0.0.1", &local_bgp_config.
router_id);
PipedOutHandler pipe_remote;
MyLoghandler remote_logger("remote");
remote_logger.setLogLevel(libbgp::DEBUG);
MyEventHandler remote_handler("remote");
remote_bgp_config.
asn = 65001;
remote_bgp_config.
rib4 = NULL;
remote_bgp_config.
rev_bus = &remote_bus;
remote_bgp_config.
clock = NULL;
inet_pton(AF_INET,
"10.0.0.2", &remote_bgp_config.
router_id);
pipe_local.setPeer(&remote);
pipe_remote.setPeer(&local);
add_event.routes.push_back(inserted->
route);
add_event.attribs = inserted->
attribs;
local_bus.
publish(&local_handler, add_event);
withdraw_event.
routes.push_back(r_172_30_24);
local_bus.
publish(&local_handler, withdraw_event);
local_rib.
withdraw(0, r_172_30_24, &local_bus);
std::vector<libbgp::Prefix4> routes;
local_rib.
withdraw(0, routes, &local_bus);
return 0;
}