Skip to content

Commit 1651a02

Browse files
authored
Merge pull request #7 from smlng/pr_update_to_new_rtrlib_api
Pr update to new rtrlib api
2 parents aa8a9b0 + 8e13275 commit 1651a02

12 files changed

+450
-314
lines changed

CMakeLists.txt

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
project(bird-rtrlib-cli)
22
cmake_minimum_required(VERSION 2.6)
3-
add_executable(bird-rtrlib-cli bird-rtrlib-cli.c bird.c rtr.c cli.c config.c)
43

5-
include_directories(${RTRLIB_INCLUDE} ${INCLUDE_DIRECTORIES})
4+
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
5+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=gnu99")
6+
7+
include_directories(.)
8+
if(APPLE)
9+
find_package(Argp)
10+
if(ARGP_FOUND)
11+
message(STATUS "argp found.")
12+
else()
13+
message(FATAL_ERROR "argp not found!")
14+
endif(ARGP_FOUND)
15+
else()
16+
set(ARGP_INCLUDE_DIRS "")
17+
set(ARGP_LIBRARIES "")
18+
endif(APPLE)
619

7-
target_link_libraries(bird-rtrlib-cli rtr)
20+
if(NOT RTRLIB_INCLUDE AND NOT RTRLIB_LIBRARY)
21+
find_package(Rtr)
22+
if(RTR_FOUND)
23+
message(STATUS "rtrlib found.")
24+
else()
25+
message(FATAL_ERROR "rtrlib not found!")
26+
endif(RTR_FOUND)
27+
else()
28+
set(RTR_INCLUDE_DIRS ${RTRLIB_INCLUDE})
29+
set(RTR_LIBRARIES ${RTRLIB_LIBRARY})
30+
endif(NOT RTRLIB_INCLUDE AND NOT RTRLIB_LIBRARY)
31+
32+
include_directories(${ARGP_INCLUDE_DIRS} ${RTR_INCLUDE_DIRS} ${INCLUDE_DIRECTORIES})
33+
add_executable(bird-rtrlib-cli bird-rtrlib-cli.c bird.c rtr.c cli.c config.c)
34+
target_link_libraries(bird-rtrlib-cli ${ARGP_LIBRARIES} ${RTR_LIBRARIES})

README

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Compilation
3131

3232
RTRlib was installed in a non-default directory <rtrlib>
3333

34-
cmake -D RTRLIB_INCLUDE=<rtrlib> .
34+
cmake -DRTRLIB_INCLUDE=<rtrlib> -DRTRLIB_LIBRARY=</path/to/rtrlib.[a|so|dylib]> .
3535

3636

3737
* Build the command line interface
@@ -58,4 +58,3 @@ Contact
5858
-------
5959

6060
Mailing List: [email protected]
61-

bird-rtrlib-cli.c

+44-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* You should have received a copy of the GNU Lesser General Public License
1515
* along with BIRD-RTRlib-CLI; see the file COPYING.
1616
*
17-
* written by Mehmet Ceyran, in cooperation with:
17+
* written by smlng and Mehmet Ceyran, in cooperation with:
1818
* CST group, Freie Universitaet Berlin
1919
* Website: https://github.com/rtrlib/bird-rtrlib-cli
2020
*/
@@ -26,6 +26,7 @@
2626
#include <syslog.h>
2727
#include <netinet/in.h>
2828

29+
#include "bird.h"
2930
#include "cli.h"
3031
#include "config.h"
3132
#include "rtr.h"
@@ -45,9 +46,6 @@ static size_t bird_command_length = -1;
4546
// "table " + config->bird_roa_table if provided.
4647
static char *bird_add_roa_table_arg = "";
4748

48-
// RTR manager config.
49-
static struct rtr_mgr_config *rtr_config = 0;
50-
5149
/**
5250
* Performs cleanup on resources allocated by `init()`.
5351
*/
@@ -130,17 +128,18 @@ void init_bird_command(void) {
130128
* @param record
131129
* @param added
132130
*/
133-
void rtr_callback(
134-
struct pfx_table *table, const struct pfx_record record, const bool added
135-
) {
131+
static void pfx_update_callback(struct pfx_table *table,
132+
const struct pfx_record record,
133+
const bool added)
134+
{
136135
// IP address buffer.
137136
static char ip_addr_str[INET6_ADDRSTRLEN];
138137

139138
// Buffer for BIRD response.
140139
static char bird_response[200];
141140

142141
// Fetch IP address as string.
143-
ip_addr_to_str(&(record.prefix), ip_addr_str, sizeof(ip_addr_str));
142+
lrtr_ip_addr_to_str(&(record.prefix), ip_addr_str, sizeof(ip_addr_str));
144143

145144
// Write BIRD command to buffer.
146145
if (
@@ -217,29 +216,52 @@ int main(int argc, char *argv[]) {
217216
return EXIT_FAILURE;
218217
}
219218

219+
struct tr_socket tr_sock;
220+
struct tr_tcp_config *tcp_config;
221+
struct tr_ssh_config *ssh_config;
222+
220223
// Try to connect to the RTR server depending on the requested connection
221224
// type.
222225
switch (config.rtr_connection_type) {
223226
case tcp:
224-
rtr_config = rtr_tcp_connect(
225-
config.rtr_host, config.rtr_port, config.rtr_bind_addr,
226-
&rtr_callback
227-
);
227+
tcp_config = rtr_create_tcp_config(
228+
config.rtr_host, config.rtr_port, config.rtr_bind_addr);
229+
tr_tcp_init(tcp_config, &tr_sock);
228230
break;
229231
case ssh:
230-
rtr_config = rtr_ssh_connect(
232+
ssh_config = rtr_create_ssh_config(
231233
config.rtr_host, config.rtr_port, config.rtr_bind_addr,
232234
config.rtr_ssh_hostkey_file, config.rtr_ssh_username,
233-
config.rtr_ssh_privkey_file, &rtr_callback
234-
);
235+
config.rtr_ssh_privkey_file);
236+
tr_ssh_init(ssh_config, &tr_sock);
235237
break;
238+
default:
239+
cleanup();
240+
return EXIT_FAILURE;
236241
}
237242

238-
// Bail out if connection cannot be established.
239-
if (!rtr_config) {
240-
cleanup();
243+
struct rtr_socket rtr;
244+
struct rtr_mgr_config *conf;
245+
struct rtr_mgr_group groups[1];
246+
247+
rtr.tr_socket = &tr_sock;
248+
groups[0].sockets_len = 1;
249+
groups[0].sockets = malloc(1 * sizeof(rtr));
250+
groups[0].sockets[0] = &rtr;
251+
groups[0].preference = 1;
252+
253+
int ret = rtr_mgr_init(&conf, groups, 1, 30, 600, 600,
254+
pfx_update_callback, NULL, NULL, NULL);
255+
256+
if (ret == RTR_ERROR)
257+
printf("Error in rtr_mgr_init!\n");
258+
else if (ret == RTR_INVALID_PARAM)
259+
printf("Invalid params passed to rtr_mgr_init\n");
260+
261+
if (!conf)
241262
return EXIT_FAILURE;
242-
};
263+
264+
rtr_mgr_start(conf);
243265

244266
// Server loop. Read commands from stdin.
245267
while (getline(&command, &command_len, stdin) != -1) {
@@ -248,7 +270,9 @@ int main(int argc, char *argv[]) {
248270
}
249271

250272
// Clean up RTRLIB memory.
251-
rtr_close(rtr_config);
273+
rtr_mgr_stop(conf);
274+
rtr_mgr_free(conf);
275+
free(groups[0].sockets);
252276

253277
// Close BIRD socket.
254278
close(bird_socket);

bird.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* You should have received a copy of the GNU Lesser General Public License
1515
* along with BIRD-RTRlib-CLI; see the file COPYING.
1616
*
17-
* written by Mehmet Ceyran, in cooperation with:
17+
* written by smlng and Mehmet Ceyran, in cooperation with:
1818
* CST group, Freie Universitaet Berlin
1919
* Website: https://github.com/rtrlib/bird-rtrlib-cli
2020
*/
@@ -23,13 +23,10 @@
2323
#include <syslog.h>
2424
#include <sys/socket.h>
2525
#include <sys/un.h>
26+
#include <unistd.h>
27+
28+
#include "bird.h"
2629

27-
/**
28-
* Connects to the BIRD daemon listening at the specified socket. Returns the
29-
* socket on success or -1 on failure.
30-
* @param socket_path
31-
* @return
32-
*/
3330
int bird_connect(const char *socket_path) {
3431
// Result value containing the socket to the BIRD.
3532
int bird_socket = -1;

bird.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of BIRD-RTRlib-CLI.
3+
*
4+
* BIRD-RTRlib-CLI is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or (at your
7+
* option) any later version.
8+
*
9+
* BIRD-RTRlib-CLI is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12+
* License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with BIRD-RTRlib-CLI; see the file COPYING.
16+
*
17+
* written by smlng, and Mehmet Ceyran, in cooperation with:
18+
* CST group, Freie Universitaet Berlin
19+
* Website: https://github.com/rtrlib/bird-rtrlib-cli
20+
*/
21+
22+
#ifndef BIRD_RTRLIB_CLI__BIRD_H
23+
#define BIRD_RTRLIB_CLI__BIRD_H
24+
25+
#include <string.h>
26+
#include <syslog.h>
27+
#include <sys/socket.h>
28+
#include <sys/un.h>
29+
#include <unistd.h>
30+
31+
/**
32+
* Connects to the BIRD daemon listening at the specified socket. Returns the
33+
* socket on success or -1 on failure.
34+
* @param socket_path
35+
* @return
36+
*/
37+
int bird_connect(const char *socket_path);
38+
39+
#endif // BIRD_RTRLIB_CLI__BIRD_H

cmake/modules/FindArgp.cmake

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# - Try to find Argp
2+
# Once done this will define
3+
#
4+
# ARGP_FOUND - system has Argp
5+
# ARGP_INCLUDE_DIRS - the Argp include directory
6+
# ARGP_LIBRARIES - Link these to use Argp
7+
# ARGP_DEFINITIONS - Compiler switches required for using Argp
8+
#
9+
# Copyright (c) 2010 Andreas Schneider <[email protected]>
10+
#
11+
# Redistribution and use is allowed according to the terms of the New
12+
# BSD license.
13+
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14+
#
15+
16+
17+
if (ARGP_LIBRARIES AND ARGP_INCLUDE_DIRS)
18+
# in cache already
19+
set(ARGP_FOUND TRUE)
20+
else (ARGP_LIBRARIES AND ARGP_INCLUDE_DIRS)
21+
22+
find_path(ARGP_INCLUDE_DIR
23+
NAMES
24+
argp.h
25+
PATHS
26+
/usr/include
27+
/usr/local/include
28+
/opt/local/include
29+
/sw/include
30+
)
31+
32+
find_library(ARGP_LIBRARY
33+
NAMES
34+
argp
35+
PATHS
36+
/usr/lib
37+
/usr/local/lib
38+
/opt/local/lib
39+
/sw/lib
40+
)
41+
42+
set(ARGP_INCLUDE_DIRS
43+
${ARGP_INCLUDE_DIR}
44+
)
45+
46+
if (ARGP_LIBRARY)
47+
set(ARGP_LIBRARIES
48+
${ARGP_LIBRARIES}
49+
${ARGP_LIBRARY}
50+
)
51+
endif (ARGP_LIBRARY)
52+
53+
include(FindPackageHandleStandardArgs)
54+
find_package_handle_standard_args(Argp DEFAULT_MSG ARGP_LIBRARIES ARGP_INCLUDE_DIRS)
55+
56+
# show the ARGP_INCLUDE_DIRS and ARGP_LIBRARIES variables only in the advanced view
57+
mark_as_advanced(ARGP_INCLUDE_DIRS ARGP_LIBRARIES)
58+
59+
endif (ARGP_LIBRARIES AND ARGP_INCLUDE_DIRS)

cmake/modules/FindLibSSH.cmake

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# - Try to find LibSSH
2+
# Once done this will define
3+
#
4+
# LIBSSH_FOUND - system has LibSSH
5+
# LIBSSH_INCLUDE_DIRS - the LibSSH include directory
6+
# LIBSSH_LIBRARIES - Link these to use LibSSH
7+
# LIBSSH_DEFINITIONS - Compiler switches required for using LibSSH
8+
#
9+
# Copyright (c) 2009 Andreas Schneider <[email protected]>
10+
#
11+
# Redistribution and use is allowed according to the terms of the New
12+
# BSD license.
13+
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14+
#
15+
if (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
16+
# in cache already
17+
set(LIBSSH_FOUND TRUE)
18+
else (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
19+
20+
find_path(LIBSSH_INCLUDE_DIR
21+
NAMES
22+
libssh/libssh.h
23+
PATHS
24+
/usr/include
25+
/usr/local/include
26+
/opt/local/include
27+
/sw/include
28+
${CMAKE_INCLUDE_PATH}
29+
${CMAKE_INSTALL_PREFIX}/include
30+
)
31+
32+
find_library(SSH_LIBRARY
33+
NAMES
34+
ssh
35+
libssh
36+
PATHS
37+
/usr/lib
38+
/usr/local/lib
39+
/opt/local/lib
40+
/sw/lib
41+
${CMAKE_LIBRARY_PATH}
42+
${CMAKE_INSTALL_PREFIX}/lib
43+
)
44+
45+
if (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
46+
set(SSH_FOUND TRUE)
47+
endif (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
48+
49+
set(LIBSSH_INCLUDE_DIRS
50+
${LIBSSH_INCLUDE_DIR}
51+
)
52+
53+
if (SSH_FOUND)
54+
set(LIBSSH_LIBRARIES
55+
${LIBSSH_LIBRARIES}
56+
${SSH_LIBRARY}
57+
)
58+
59+
if (LibSSH_FIND_VERSION)
60+
file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
61+
REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
62+
# Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
63+
if (LIBSSH_VERSION_MAJOR)
64+
string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
65+
file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
66+
REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
67+
string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
68+
file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
69+
REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
70+
string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
71+
72+
set(LibSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
73+
74+
include(FindPackageVersionCheck)
75+
find_package_version_check(LibSSH DEFAULT_MSG)
76+
else (LIBSSH_VERSION_MAJOR)
77+
message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
78+
set(LIBSSH_FOUND FALSE)
79+
endif (LIBSSH_VERSION_MAJOR)
80+
endif (LibSSH_FIND_VERSION)
81+
endif (SSH_FOUND)
82+
83+
# If the version is too old, but libs and includes are set,
84+
# find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
85+
# so we need this if() here.
86+
if (LIBSSH_FOUND)
87+
include(FindPackageHandleStandardArgs)
88+
find_package_handle_standard_args(LibSSH DEFAULT_MSG LIBSSH_LIBRARIES LIBSSH_INCLUDE_DIRS)
89+
endif (LIBSSH_FOUND)
90+
91+
# show the LIBSSH_INCLUDE_DIRS and LIBSSH_LIBRARIES variables only in the advanced view
92+
mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES)
93+
94+
endif (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)

0 commit comments

Comments
 (0)