0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

RPKI: refactore transports

This commit is contained in:
Pavel Tvrdík 2016-01-18 08:16:55 +01:00
parent 4c1e54d4a6
commit e45dc8e01d
3 changed files with 29 additions and 47 deletions

View File

@ -23,7 +23,7 @@
static int tr_ssh_open(void *tr_ssh_sock);
static void tr_ssh_close(void *tr_ssh_sock);
static void tr_ssh_free(struct tr_socket *tr_sock);
static const char *tr_ssh_ident(void *tr_ssh_sock);
static const char *tr_ssh_ident(void *socket);
int tr_ssh_open(void *socket)
{
@ -74,6 +74,8 @@ void tr_ssh_close(void *tr_ssh_sock)
ssh_free(sk->ssh->session);
sk->ssh->session = NULL;
}
mb_free(sk->ssh);
sk->ssh = NULL;
}
}
@ -90,26 +92,25 @@ void tr_ssh_free(struct tr_socket *tr_sock)
}
}
const char *tr_ssh_ident(void *tr_ssh_sock)
const char *tr_ssh_ident(void *socket)
{
size_t len;
struct tr_ssh_socket *ssh_sock = tr_ssh_sock;
struct rpki_cache *cache = ssh_sock->cache;
ASSERT(socket != NULL);
assert(ssh_sock != NULL);
struct tr_ssh_socket *ssh = socket;
struct rpki_cache *cache = ssh->cache;
if (ssh_sock->ident != NULL)
return ssh_sock->ident;
if (ssh->ident != NULL)
return ssh->ident;
const char *username = cache->cfg->ssh->username;
const char *host = cache->cfg->hostname;
len = strlen(username) + 1 + strlen(host) + 1 + 5 + 1; /* <user> + '@' + <host> + ':' + <port> + '\0' */
ssh_sock->ident = mb_alloc(cache->p->p.pool, len);
if (ssh_sock->ident == NULL)
size_t len = strlen(username) + 1 + strlen(host) + 1 + 5 + 1; /* <user> + '@' + <host> + ':' + <port> + '\0' */
ssh->ident = mb_alloc(cache->p->p.pool, len);
if (ssh->ident == NULL)
return NULL;
snprintf(ssh_sock->ident, len, "%s@%s:%u", username, host, cache->cfg->port);
return ssh_sock->ident;
snprintf(ssh->ident, len, "%s@%s:%u", username, host, cache->cfg->port);
return ssh->ident;
}
int tr_ssh_init(struct rpki_cache *cache)
@ -124,7 +125,6 @@ int tr_ssh_init(struct rpki_cache *cache)
tr_socket->socket = mb_allocz(p->p.pool, sizeof(struct tr_ssh_socket));
struct tr_ssh_socket *ssh = tr_socket->socket;
ssh->cache = cache;
return TR_SUCCESS;

View File

@ -36,7 +36,6 @@ int tr_tcp_open(void *tr_tcp_sock)
sock *sk = cache->sk;
sk->type = SK_TCP_ACTIVE;
sk->daddr = tcp_socket->config.ip;
if (sk_open(sk) != 0)
return TR_ERROR;
@ -71,31 +70,33 @@ void tr_tcp_free(struct tr_socket *tr_sock)
const char *tr_tcp_ident(void *socket)
{
assert(socket != NULL);
ASSERT(socket != NULL);
struct tr_tcp_socket *sock = socket;
struct rpki_proto *p = sock->cache->p;
struct tr_tcp_socket *tcp = socket;
struct rpki_cache *cache = tcp->cache;
if (sock->ident != NULL)
return sock->ident;
if (tcp->ident != NULL)
return tcp->ident;
const char *host = cache->cfg->hostname;
size_t colon_and_port_len = 6; /* max ":65535" */
size_t ident_len;
if (sock->config.host)
ident_len = strlen(sock->config.host) + colon_and_port_len + 1;
if (host)
ident_len = strlen(host) + colon_and_port_len + 1;
else
ident_len = IPA_MAX_TEXT_LENGTH + colon_and_port_len + 1;
sock->ident = mb_allocz(p->p.pool, ident_len);
if (sock->ident == NULL)
tcp->ident = mb_allocz(cache->p->p.pool, ident_len);
if (tcp->ident == NULL)
return NULL;
if (sock->config.host)
bsnprintf(sock->ident, ident_len, "%s:%u", sock->config.host, sock->config.port);
if (host)
bsnprintf(tcp->ident, ident_len, "%s:%u", host, cache->cfg->port);
else
bsnprintf(sock->ident, ident_len, "%I:%u", sock->config.ip, sock->config.port);
bsnprintf(tcp->ident, ident_len, "%I:%u", cache->cfg->ip, cache->cfg->port);
return sock->ident;
return tcp->ident;
}
int tr_tcp_init(struct rpki_cache *cache)
@ -111,11 +112,7 @@ int tr_tcp_init(struct rpki_cache *cache)
tr_socket->socket = mb_allocz(p->p.pool, sizeof(struct tr_tcp_socket));
struct tr_tcp_socket *tcp = tr_socket->socket;
tcp->cache = cache;
tcp->config.host = cache_cfg->hostname;
tcp->config.ip = cache_cfg->ip;
tcp->config.port = cache_cfg->port;
return TR_SUCCESS;
}

View File

@ -23,23 +23,8 @@
#include "nest/bird.h"
#include "lib/ip.h"
/**
* @brief A tr_tcp_config struct holds configuration for a TCP connection.
* @param host Hostname or IP address to connect to.
* @param port Port to connect to.
* @param bindaddr Hostname or IP address to connect from. NULL for
* determination by OS.
* to use the source address of the system's default route to the server
*/
struct tr_tcp_config {
ip_addr ip; char *host; /* at least one of @ip or @host must be defined */
uint port;
char *bindaddr; /* TODO: NEED THIS? */
};
struct tr_tcp_socket {
struct rpki_cache *cache;
struct tr_tcp_config config;
char *ident;
};