blob: b704c932ff23e9f6eb77c6f081270b553107d36f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <string.h>
#include <grpc/grpc.h>
#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include "gobgp_api_client.grpc.pb.h"
extern "C" {
// Gobgp library
#include "libgobgp.h"
}
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using gobgpapi::GobgpApi;
class GrpcClient {
public:
GrpcClient(std::shared_ptr<Channel> channel) : stub_(GobgpApi::NewStub(channel)) {}
std::string GetNeighbor() {
gobgpapi::GetNeighborRequest request;
ClientContext context;
gobgpapi::GetNeighborResponse response;
grpc::Status status = stub_->GetNeighbor(&context, request, &response);
if (status.ok()) {
std::stringstream buffer;
for (int i=0; i < response.peers_size(); i++) {
gobgpapi::PeerConf peer_conf = response.peers(i).conf();
gobgpapi::PeerState peer_info = response.peers(i).info();
gobgpapi::Timers peer_timers = response.peers(i).timers();
buffer
<< "BGP neighbor is: " << peer_conf.neighbor_address()
<< ", remote AS: " << peer_conf.peer_as() << "\n"
<< "\tBGP version: 4, remote route ID " << peer_conf.id() << "\n"
<< "\tBGP state = " << peer_info.bgp_state()
<< ", up for " << peer_timers.state().uptime() << "\n"
<< "\tBGP OutQ = " << peer_info.out_q()
<< ", Flops = " << peer_info.flops() << "\n"
<< "\tHold time is " << peer_timers.state().hold_time()
<< ", keepalive interval is " << peer_timers.state().keepalive_interval() << "seconds\n"
<< "\tConfigured hold time is " << peer_timers.config().hold_time() << "\n";
}
return buffer.str();
} else {
std::stringstream buffer;
buffer
<< status.error_code() << "\n"
<< status.error_message() << "\n"
<< status.error_details() << "\n";
return buffer.str();
}
}
private:
std::unique_ptr<GobgpApi::Stub> stub_;
};
int main(int argc, char** argv) {
if(argc < 2) {
std::cout << "Usage: ./gobgp_api_client [gobgp address]\n";
return 1;
}
std::string addr = argv[1];
GrpcClient gobgp_client(grpc::CreateChannel(addr + ":50051", grpc::InsecureChannelCredentials()));
std::string reply = gobgp_client.GetNeighbor();
std::cout << reply;
return 0;
}
|