routerinfo.c 3.54 KB
Newer Older
1
2
3
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
5
6
/* See LICENSE for licensing information */

7
8
9
10
11
/**
 * @file routerinfo.c
 * @brief Manipulate full router descriptors.
 **/

12
13
14
15
#include "core/or/or.h"

#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerinfo.h"
16
#include "feature/nodelist/torcert.h"
17
18
19
20

#include "feature/nodelist/node_st.h"
#include "feature/nodelist/routerinfo_st.h"

21
22
23
24
/** Copy the OR port (IP address and TCP port) for <b>router</b> and
 * <b>family</b> into *<b>ap_out</b>.
 *
 * If the requested ORPort does not exist, sets *<b>ap_out</b> to the null
25
26
 * address and port, and returns -1. Otherwise, returns 0. */
int
27
28
29
router_get_orport(const routerinfo_t *router,
                  tor_addr_port_t *ap_out,
                  int family)
30
31
{
  tor_assert(ap_out != NULL);
32
  if (family == AF_INET) {
33
34
    tor_addr_copy(&ap_out->addr, &router->ipv4_addr);
    ap_out->port = router->ipv4_orport;
35
    return 0;
36
  } else if (family == AF_INET6) {
37
38
39
40
41
42
43
44
45
    /* IPv6 addresses are optional, so check if it is valid. */
    if (tor_addr_port_is_valid(&router->ipv6_addr, router->ipv6_orport, 0)) {
      tor_addr_copy(&ap_out->addr, &router->ipv6_addr);
      ap_out->port = router->ipv6_orport;
      return 0;
    } else {
      tor_addr_port_make_null_ap(ap_out, AF_INET6);
      return -1;
    }
46
47
48
49
  } else {
    /* Unsupported address family */
    tor_assert_nonfatal_unreached();
    tor_addr_port_make_null_ap(ap_out, AF_UNSPEC);
50
    return -1;
51
  }
52
53
54
55
56
57
}

int
router_has_orport(const routerinfo_t *router, const tor_addr_port_t *orport)
{
  return
58
59
    (tor_addr_eq(&orport->addr, &router->ipv4_addr) &&
     orport->port == router->ipv4_orport) ||
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    (tor_addr_eq(&orport->addr, &router->ipv6_addr) &&
     orport->port == router->ipv6_orport);
}

/** Return a smartlist of tor_addr_port_t's with all the OR ports of
    <b>ri</b>. Note that freeing of the items in the list as well as
    the smartlist itself is the callers responsibility. */
smartlist_t *
router_get_all_orports(const routerinfo_t *ri)
{
  tor_assert(ri);
  node_t fake_node;
  memset(&fake_node, 0, sizeof(fake_node));
  /* we don't modify ri, fake_node is passed as a const node_t *
   */
  fake_node.ri = (routerinfo_t *)ri;
  return node_get_all_orports(&fake_node);
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/** Return the Ed25519 identity key for this routerinfo, or NULL if it
 * doesn't have one. */
const ed25519_public_key_t *
routerinfo_get_ed25519_id(const routerinfo_t *ri)
{
  if (BUG(! ri))
    return NULL;

  const tor_cert_t *cert = ri->cache_info.signing_key_cert;
  if (cert && ! ed25519_public_key_is_zero(&cert->signing_key))
    return &cert->signing_key;
  else
    return NULL;
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/** Given a router purpose, convert it to a string.  Don't call this on
 * ROUTER_PURPOSE_UNKNOWN: The whole point of that value is that we don't
 * know its string representation. */
const char *
router_purpose_to_string(uint8_t p)
{
  switch (p)
    {
    case ROUTER_PURPOSE_GENERAL: return "general";
    case ROUTER_PURPOSE_BRIDGE: return "bridge";
    case ROUTER_PURPOSE_CONTROLLER: return "controller";
    default:
      tor_assert(0);
    }
  return NULL;
}

/** Given a string, convert it to a router purpose. */
uint8_t
router_purpose_from_string(const char *s)
{
  if (!strcmp(s, "general"))
    return ROUTER_PURPOSE_GENERAL;
  else if (!strcmp(s, "bridge"))
    return ROUTER_PURPOSE_BRIDGE;
  else if (!strcmp(s, "controller"))
    return ROUTER_PURPOSE_CONTROLLER;
  else
    return ROUTER_PURPOSE_UNKNOWN;
}