Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / proto / rpki / transport.c @ 6b3f1a54

History | View | Annotate | Download (3.13 KB)

1
/*
2
 *        BIRD -- The Resource Public Key Infrastructure (RPKI) to Router Protocol
3
 *
4
 *        (c) 2015 CZ.NIC
5
 *        (c) 2015 Pavel Tvrdik <pawel.tvrdik@gmail.com>
6
 *
7
 *        This file was a part of RTRlib: http://rpki.realmv6.org/
8
 *
9
 *        Can be freely distributed and used under the terms of the GNU GPL.
10
 */
11

    
12
#include <sys/socket.h>
13
#include <netdb.h>
14

    
15
#include "rpki.h"
16
#include "transport.h"
17
#include "sysdep/unix/unix.h"
18

    
19
/**
20
 * rpki_hostname_autoresolv - auto-resolve an IP address from a hostname
21
 * @host: domain name of host, e.g. "rpki-validator.realmv6.org"
22
 *
23
 * This function resolves an IP address from a hostname.
24
 * Returns &ip_addr structure with IP address or |IPA_NONE|.
25
 */
26
static ip_addr
27
rpki_hostname_autoresolv(const char *host)
28
{
29
  ip_addr addr = {};
30
  struct addrinfo *res;
31
  struct addrinfo hints = {
32
      .ai_family = AF_UNSPEC,
33
      .ai_socktype = SOCK_STREAM,
34
      .ai_flags = AI_ADDRCONFIG,
35
  };
36

    
37
  if (!host)
38
    return IPA_NONE;
39

    
40
  int err_code = getaddrinfo(host, NULL, &hints, &res);
41
  if (err_code != 0)
42
  {
43
    log(L_DEBUG "getaddrinfo failed: %s", gai_strerror(err_code));
44
    return IPA_NONE;
45
  }
46

    
47
  sockaddr sa = {
48
      .sa = *res->ai_addr,
49
  };
50

    
51
  uint unused;
52
  sockaddr_read(&sa, res->ai_family, &addr, NULL, &unused);
53

    
54
  freeaddrinfo(res);
55
  return addr;
56
}
57

    
58
/**
59
 * rpki_tr_open - prepare and open a socket connection
60
 * @tr: initialized transport socket
61
 *
62
 * Prepare and open a socket connection specified by @tr that must be initialized before.
63
 * This function ends with a calling the sk_open() function.
64
 * Returns RPKI_TR_SUCCESS or RPKI_TR_ERROR.
65
 */
66
int
67
rpki_tr_open(struct rpki_tr_sock *tr)
68
{
69
  struct rpki_cache *cache = tr->cache;
70
  struct rpki_config *cf = (void *) cache->p->p.cf;
71

    
72
  ASSERT(tr->sk == NULL);
73
  tr->sk = sk_new(cache->pool);
74
  sock *sk = tr->sk;
75

    
76
  /* sk->type -1 is invalid value, a correct value MUST be set in the specific transport layer in open_fp() hook */
77
  sk->type = -1;
78

    
79
  sk->tx_hook = rpki_connected_hook;
80
  sk->err_hook = rpki_err_hook;
81
  sk->data = cache;
82
  sk->daddr = cf->ip;
83
  sk->dport = cf->port;
84
  sk->host = cf->hostname;
85
  sk->rbsize = RPKI_RX_BUFFER_SIZE;
86
  sk->tbsize = RPKI_TX_BUFFER_SIZE;
87
  sk->tos = IP_PREC_INTERNET_CONTROL;
88

    
89
  if (ipa_zero2(sk->daddr) && sk->host)
90
  {
91
    sk->daddr = rpki_hostname_autoresolv(sk->host);
92
    if (ipa_zero(sk->daddr))
93
    {
94
      CACHE_TRACE(D_EVENTS, cache, "Cannot resolve the hostname '%s'", sk->host);
95
      return RPKI_TR_ERROR;
96
    }
97
  }
98

    
99
  return tr->open_fp(tr);
100
}
101

    
102
/**
103
 * rpki_tr_close - close socket and prepare it for possible next open
104
 * @tr: successfully opened transport socket
105
 *
106
 * Close socket and free resources.
107
 */
108
void
109
rpki_tr_close(struct rpki_tr_sock *tr)
110
{
111
  if (tr->ident)
112
  {
113
    mb_free((char *) tr->ident);
114
    tr->ident = NULL;
115
  }
116

    
117
  if (tr->sk)
118
  {
119
    rfree(tr->sk);
120
    tr->sk = NULL;
121
  }
122
}
123

    
124
/**
125
 * rpki_tr_ident - Returns a string identifier for the rpki transport socket
126
 * @tr: successfully opened transport socket
127
 *
128
 * Returns a \0 terminated string identifier for the socket endpoint, e.g. "<host>:<port>".
129
 * Memory is allocated inside @tr structure.
130
 */
131
inline const char *
132
rpki_tr_ident(struct rpki_tr_sock *tr)
133
{
134
  return tr->ident_fp(tr);
135
}