RTRlib
Loading...
Searching...
No Matches
rtr_mgr.c

Usage example of the RTR connection manager.

#include "rtrlib/rtrlib.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
//create a SSH transport socket
char ssh_host[] = "123.231.123.221";
char ssh_user[] = "rpki_user";
char ssh_hostkey[] = "/etc/rpki-rtr/hostkey";
char ssh_privkey[] = "/etc/rpki-rtr/client.priv";
struct tr_socket tr_ssh;
struct tr_ssh_config config = {
ssh_host, //IP
22, //Port
NULL, //Source address
ssh_user,
ssh_hostkey, //Server hostkey
ssh_privkey, //Private key
NULL, // data
NULL, // new_socket()
0, // connect timeout
NULL, // password
};
tr_ssh_init(&config, &tr_ssh);
//create a TCP transport socket
struct tr_socket tr_tcp;
char tcp_host[] = "rpki-validator.realmv6.org";
char tcp_port[] = "8282";
struct tr_tcp_config tcp_config = {
tcp_host, //IP
tcp_port, //Port
NULL, //Source address
NULL, //data
NULL, //get_socket()
0, // connect timeout
};
tr_tcp_init(&tcp_config, &tr_tcp);
//create 3 rtr_sockets and associate them with the transprort sockets
struct rtr_socket rtr_ssh, rtr_tcp;
rtr_ssh.tr_socket = &tr_ssh;
rtr_tcp.tr_socket = &tr_tcp;
//create a rtr_mgr_group array with 2 elements
struct rtr_mgr_group groups[2];
//The first group contains both TCP RTR sockets
groups[0].sockets = malloc(sizeof(struct rtr_socket *));
groups[0].sockets_len = 1;
groups[0].sockets[0] = &rtr_tcp;
groups[0].preference = 1; //Preference value of this group
//The seconds group contains only the SSH RTR socket
groups[1].sockets = malloc(1 * sizeof(struct rtr_socket *));
groups[1].sockets_len = 1;
groups[1].sockets[0] = &rtr_ssh;
groups[1].preference = 2;
//create a rtr_mgr_config struct that stores the group
//initialize all rtr_sockets in the server pool with the same settings
struct rtr_mgr_config *conf;
rtr_mgr_init(&conf, groups, 2, 30, 600, 600, NULL, NULL, NULL, NULL);
//start the connection manager
//wait till at least one rtr_mgr_group is fully synchronized with the server
while (!rtr_mgr_conf_in_sync(conf)) {
sleep(1);
}
//validate the BGP-Route 10.10.0.0/24, origin ASN: 12345
struct lrtr_ip_addr pref;
lrtr_ip_str_to_addr("10.10.0.0", &pref);
enum pfxv_state result;
const uint8_t mask = 24;
rtr_mgr_validate(conf, 12345, &pref, mask, &result);
//output the result of the prefix validation above
//to showcase the returned states.
char buffer[INET_ADDRSTRLEN];
lrtr_ip_addr_to_str(&pref, buffer, sizeof(buffer));
printf("RESULT: The prefix %s/%i ", buffer, mask);
switch (result) {
printf("is valid.\n");
break;
printf("is invalid.\n");
break;
printf("was not found.\n");
break;
default:
break;
}
rtr_mgr_stop(conf);
rtr_mgr_free(conf);
free(groups[0].sockets);
free(groups[1].sockets);
}
pfxv_state
Validation states returned from pfx_validate_origin.
Definition pfx.h:46
@ BGP_PFXV_STATE_VALID
Definition pfx.h:48
@ BGP_PFXV_STATE_INVALID
One or more records that match the input prefix exists in the pfx_table but the prefix max_len or ASN...
Definition pfx.h:56
@ BGP_PFXV_STATE_NOT_FOUND
No certificate for the route exists.
Definition pfx.h:51
void rtr_mgr_free(struct rtr_mgr_config *config)
Frees all resources that were allocated from the rtr_mgr.
bool rtr_mgr_conf_in_sync(struct rtr_mgr_config *config)
Check if rtr_mgr_group is fully synchronized with at least one group.
void rtr_mgr_stop(struct rtr_mgr_config *config)
Terminates rtr_socket connections.
int rtr_mgr_start(struct rtr_mgr_config *config)
Establishes rtr_socket connections.
int rtr_mgr_validate(struct rtr_mgr_config *config, const uint32_t asn, const struct lrtr_ip_addr *prefix, const uint8_t mask_len, enum pfxv_state *result)
Validates the origin of a BGP-Route.
int rtr_mgr_init(struct rtr_mgr_config **config_out, struct rtr_mgr_group groups[], const unsigned int groups_len, const unsigned int refresh_interval, const unsigned int expire_interval, const unsigned int retry_interval, const pfx_update_fp update_fp, const spki_update_fp spki_update_fp, const rtr_mgr_status_fp status_fp, void *status_fp_data)
Initializes a rtr_mgr_config.
int tr_ssh_init(const struct tr_ssh_config *config, struct tr_socket *socket)
Initializes the tr_socket struct for a SSH connection.
int tr_tcp_init(const struct tr_tcp_config *config, struct tr_socket *socket)
Initializes the tr_socket struct for a TCP connection.
int lrtr_ip_addr_to_str(const struct lrtr_ip_addr *ip, char *str, const unsigned int len)
int lrtr_ip_str_to_addr(const char *str, struct lrtr_ip_addr *ip)
The lrtr_ip_addr struct stores a IPv4 or IPv6 address in host byte order.
Definition ip.h:38
A set of RTR sockets.
Definition rtr_mgr.h:66
A RTR socket.
Definition rtr.h:116
A transport socket datastructure.
Definition transport.h:102
A tr_ssh_config struct holds configuration data for an tr_ssh socket.
Definition ssh_transport.h:51
A tr_tcp_config struct holds configuration for a TCP connection.
Definition tcp_transport.h:41