@@ -13,7 +13,255 @@ extern "C" {
1313#include < grpc++/create_channel.h>
1414#include < grpc++/security/credentials.h>
1515
16+ #include " gobgp_api_client.grpc.pb.h"
17+
18+ using grpc::Channel;
19+ using grpc::ClientContext;
20+ using grpc::Status;
21+
22+ using gobgpapi::GobgpApi;
23+
24+ class GrpcClient {
25+ public:
26+ GrpcClient (std::shared_ptr<Channel> channel) : stub_(GobgpApi::NewStub(channel)) {}
27+
28+ void GetAllActiveAnnounces (unsigned int route_family) {
29+ ClientContext context;
30+ gobgpapi::Arguments arguments;
31+
32+ arguments.set_rf (route_family);
33+ // We could specify certain neighbor here
34+ arguments.set_name (" " );
35+ arguments.set_resource (gobgpapi::Resource::GLOBAL );
36+
37+ auto destinations_list = stub_->GetRib (&context, arguments);
38+
39+ gobgpapi::Destination current_destination;
40+
41+ std::cout << " List of announced prefixes for route family: " << route_family << std::endl << std::endl;
42+ while (destinations_list->Read (¤t_destination)) {
43+ std::cout << " Prefix: " << current_destination.prefix () << std::endl;
44+
45+ // std::cout << "Paths size: " << current_destination.paths_size() << std::endl;
46+
47+ gobgpapi::Path my_path = current_destination.paths (0 );
48+
49+ // std::cout << "Pattrs size: " << my_path.pattrs_size() << std::endl;
50+
51+ buf my_nlri;
52+ my_nlri.value = (char *)my_path.nlri ().c_str ();
53+ my_nlri.len = my_path.nlri ().size ();
54+
55+ path_t gobgp_lib_path;
56+ gobgp_lib_path.nlri = my_nlri;
57+ // Not used in library code!
58+ gobgp_lib_path.path_attributes_cap = 0 ;
59+ gobgp_lib_path.path_attributes_len = my_path.pattrs_size ();
60+
61+ buf* my_path_attributes[ my_path.pattrs_size () ];
62+ for (int i = 0 ; i < my_path.pattrs_size (); i++) {
63+ my_path_attributes[i] = (buf*)malloc (sizeof (buf));
64+ my_path_attributes[i]->len = my_path.pattrs (i).size ();
65+ my_path_attributes[i]->value = (char *)my_path.pattrs (i).c_str ();
66+ }
67+
68+ gobgp_lib_path.path_attributes = my_path_attributes;
69+
70+ std::cout << " NLRI: " << decode_path (&gobgp_lib_path) << std::endl;
71+ }
72+
73+ Status status = destinations_list->Finish ();
74+ if (!status.ok ()) {
75+ // error_message
76+ std::cout << " Problem with RPC: " << status.error_code () << " message " << status.error_message () << std::endl;
77+ } else {
78+ // std::cout << "RPC working well" << std::endl;
79+ }
80+ }
81+
82+ void AnnounceFlowSpecPrefix () {
83+ const gobgpapi::ModPathArguments current_mod_path_arguments;
84+
85+ unsigned int AFI_IP = 1 ;
86+ unsigned int SAFI_FLOW_SPEC_UNICAST = 133 ;
87+ unsigned int ipv4_flow_spec_route_family = AFI_IP <<16 | SAFI_FLOW_SPEC_UNICAST ;
88+
89+ gobgpapi::Path* current_path = new gobgpapi::Path;
90+ // If you want withdraw, please use it
91+ // current_path->set_is_withdraw(true);
92+
93+ /*
94+ buf:
95+ char *value;
96+ int len;
97+ path:
98+ buf nlri;
99+ buf** path_attributes;
100+ int path_attributes_len;
101+ int path_attributes_cap;
102+ */
103+
104+ path* path_c_struct = serialize_path (ipv4_flow_spec_route_family, (char *)" match destination 10.0.0.0/24 protocol tcp source 20.0.0.0/24 then redirect 10:10" );
105+
106+ // printf("Decoded NLRI output: %s, length %d raw string length: %d\n", decode_path(path_c_struct), path_c_struct->nlri.len, strlen(path_c_struct->nlri.value));
107+
108+ for (int path_attribute_number = 0 ; path_attribute_number < path_c_struct->path_attributes_len ; path_attribute_number++) {
109+ current_path->add_pattrs (path_c_struct->path_attributes [path_attribute_number]->value ,
110+ path_c_struct->path_attributes [path_attribute_number]->len );
111+ }
112+
113+ current_path->set_nlri (path_c_struct->nlri .value , path_c_struct->nlri .len );
114+
115+ gobgpapi::ModPathArguments request;
116+ request.set_resource (gobgpapi::Resource::GLOBAL );
117+
118+ google::protobuf::RepeatedPtrField< ::gobgpapi::Path >* current_path_list = request.mutable_paths ();
119+ current_path_list->AddAllocated (current_path);
120+ request.set_name (" " );
121+
122+ ClientContext context;
123+
124+ gobgpapi::Error return_error;
125+
126+ // result is a std::unique_ptr<grpc::ClientWriter<gobgpapi::ModPathArguments> >
127+ auto send_stream = stub_->ModPath (&context, &return_error);
128+
129+ bool write_result = send_stream->Write (request);
130+
131+ if (!write_result) {
132+ std::cout << " Write to API failed\n " ;
133+ }
134+
135+ // Finish all writes
136+ send_stream->WritesDone ();
137+
138+ auto status = send_stream->Finish ();
139+
140+ if (status.ok ()) {
141+ // std::cout << "modpath executed correctly" << std::cout;
142+ } else {
143+ std::cout << " modpath failed with code: " << status.error_code ()
144+ << " message " << status.error_message () << std::endl;
145+ }
146+ }
147+
148+ void AnnounceUnicastPrefix (std::string announced_prefix, std::string announced_prefix_nexthop) {
149+ const gobgpapi::ModPathArguments current_mod_path_arguments;
150+
151+ unsigned int AFI_IP = 1 ;
152+ unsigned int SAFI_UNICAST = 1 ;
153+ unsigned int ipv4_unicast_route_family = AFI_IP <<16 | SAFI_UNICAST ;
154+
155+ gobgpapi::Path* current_path = new gobgpapi::Path;
156+ // If you want withdraw, please use it
157+ // current_path->set_is_withdraw(true);
158+
159+ /*
160+ buf:
161+ char *value;
162+ int len;
163+ path:
164+ buf nlri;
165+ buf** path_attributes;
166+ int path_attributes_len;
167+ int path_attributes_cap;
168+ */
169+
170+ std::string announce_line = announced_prefix + " nexthop " + announced_prefix_nexthop;
171+
172+ path* path_c_struct = serialize_path (ipv4_unicast_route_family, (char *)announce_line.c_str ());
173+
174+ if (path_c_struct == NULL ) {
175+ std::cerr << " Could not generate path\n " ;
176+ exit (-1 );
177+ }
178+
179+ // printf("Decoded NLRI output: %s, length %d raw string length: %d\n", decode_path(path_c_struct), path_c_struct->nlri.len, strlen(path_c_struct->nlri.value));
180+
181+ for (int path_attribute_number = 0 ; path_attribute_number < path_c_struct->path_attributes_len ; path_attribute_number++) {
182+ current_path->add_pattrs (path_c_struct->path_attributes [path_attribute_number]->value ,
183+ path_c_struct->path_attributes [path_attribute_number]->len );
184+ }
185+
186+ current_path->set_nlri (path_c_struct->nlri .value , path_c_struct->nlri .len );
187+
188+ gobgpapi::ModPathArguments request;
189+ request.set_resource (gobgpapi::Resource::GLOBAL );
190+ google::protobuf::RepeatedPtrField< ::gobgpapi::Path >* current_path_list = request.mutable_paths ();
191+ current_path_list->AddAllocated (current_path);
192+ request.set_name (" " );
193+
194+ ClientContext context;
195+
196+ gobgpapi::Error return_error;
197+
198+ // result is a std::unique_ptr<grpc::ClientWriter<api::ModPathArguments> >
199+ auto send_stream = stub_->ModPath (&context, &return_error);
200+
201+ bool write_result = send_stream->Write (request);
202+
203+ if (!write_result) {
204+ std::cout << " Write to API failed\n " ;
205+ }
206+
207+ // Finish all writes
208+ send_stream->WritesDone ();
209+
210+ auto status = send_stream->Finish ();
211+
212+ if (status.ok ()) {
213+ // std::cout << "modpath executed correctly" << std::cout;
214+ } else {
215+ std::cout << " modpath failed with code: " << status.error_code ()
216+ << " message " << status.error_message () << std::endl;
217+ }
218+ }
219+
220+ std::string GetAllNeighbor (std::string neighbor_ip) {
221+ gobgpapi::Arguments request;
222+ request.set_rf (4 );
223+ request.set_name (neighbor_ip);
224+
225+ ClientContext context;
226+
227+ gobgpapi::Peer peer;
228+ grpc::Status status = stub_->GetNeighbor (&context, request, &peer);
229+
230+ if (status.ok ()) {
231+ gobgpapi::PeerConf peer_conf = peer.conf ();
232+ gobgpapi::PeerInfo peer_info = peer.info ();
233+
234+ std::stringstream buffer;
235+
236+ buffer
237+ << " Peer AS: " << peer_conf.remote_as () << " \n "
238+ << " Peer router id: " << peer_conf.id () << " \n "
239+ << " Peer flops: " << peer_info.flops () << " \n "
240+ << " BGP state: " << peer_info.bgp_state ();
241+
242+ return buffer.str ();
243+ } else {
244+ return " Something wrong" ;
245+ }
246+ }
247+
248+ private:
249+ std::unique_ptr<GobgpApi::Stub> stub_;
250+ };
251+
252+ GrpcClient* gobgp_client = NULL ;
253+
254+ void gobgp_action_init () {
255+ logger << log4cpp::Priority::INFO << " GoBGP action module loaded" ;
256+ gobgp_client = new GrpcClient (grpc::CreateChannel (" localhost:8080" , grpc::InsecureCredentials ()));
257+ }
258+
259+ void gobgp_action_shutdown () {
260+ delete gobgp_client;
261+ }
262+
16263void gobgp_ban_manage (std::string action, std::string ip_as_string, attack_details current_attack) {
264+ gobgp_client->AnnounceUnicastPrefix (" 10.10.20.33/32" , " 10.10.1.99" );
17265 // std::string subnet_as_string_with_mask = convert_subnet_to_string(current_attack.customer_network);
18266 // std::string ip_as_string_with_mask = ip_as_string + "/32";
19267}
0 commit comments