Tor  0.4.3.0-alpha-dev
routerinfo.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file routerinfo.c
9  * @brief Manipulate full router descriptors.
10  **/
11 
12 #include "core/or/or.h"
13 
16 
19 
20 /** Copy the primary (IPv4) OR port (IP address and TCP port) for
21  * <b>router</b> into *<b>ap_out</b>. */
22 void
24 {
25  tor_assert(ap_out != NULL);
26  tor_addr_from_ipv4h(&ap_out->addr, router->addr);
27  ap_out->port = router->or_port;
28 }
29 
30 int
31 router_has_orport(const routerinfo_t *router, const tor_addr_port_t *orport)
32 {
33  return
34  (tor_addr_eq_ipv4h(&orport->addr, router->addr) &&
35  orport->port == router->or_port) ||
36  (tor_addr_eq(&orport->addr, &router->ipv6_addr) &&
37  orport->port == router->ipv6_orport);
38 }
39 
40 /** Return a smartlist of tor_addr_port_t's with all the OR ports of
41  <b>ri</b>. Note that freeing of the items in the list as well as
42  the smartlist itself is the callers responsibility. */
45 {
46  tor_assert(ri);
47  node_t fake_node;
48  memset(&fake_node, 0, sizeof(fake_node));
49  /* we don't modify ri, fake_node is passed as a const node_t *
50  */
51  fake_node.ri = (routerinfo_t *)ri;
52  return node_get_all_orports(&fake_node);
53 }
54 
55 /** Given a router purpose, convert it to a string. Don't call this on
56  * ROUTER_PURPOSE_UNKNOWN: The whole point of that value is that we don't
57  * know its string representation. */
58 const char *
60 {
61  switch (p)
62  {
63  case ROUTER_PURPOSE_GENERAL: return "general";
64  case ROUTER_PURPOSE_BRIDGE: return "bridge";
65  case ROUTER_PURPOSE_CONTROLLER: return "controller";
66  default:
67  tor_assert(0);
68  }
69  return NULL;
70 }
71 
72 /** Given a string, convert it to a router purpose. */
73 uint8_t
75 {
76  if (!strcmp(s, "general"))
78  else if (!strcmp(s, "bridge"))
79  return ROUTER_PURPOSE_BRIDGE;
80  else if (!strcmp(s, "controller"))
82  else
84 }
Router descriptor structure.
Definition: node_st.h:33
#define ROUTER_PURPOSE_UNKNOWN
void router_get_prim_orport(const routerinfo_t *router, tor_addr_port_t *ap_out)
Definition: routerinfo.c:23
static int tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
Definition: address.h:193
Header file for nodelist.c.
Node information structure.
#define tor_assert(expr)
Definition: util_bug.h:102
uint8_t router_purpose_from_string(const char *s)
Definition: routerinfo.c:74
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
Master header file for Tor-specific functionality.
#define ROUTER_PURPOSE_BRIDGE
#define ROUTER_PURPOSE_GENERAL
smartlist_t * router_get_all_orports(const routerinfo_t *ri)
Definition: routerinfo.c:44
const char * router_purpose_to_string(uint8_t p)
Definition: routerinfo.c:59
smartlist_t * node_get_all_orports(const node_t *node)
Definition: nodelist.c:1504
#define tor_addr_eq(a, b)
Definition: address.h:244
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:32
uint16_t or_port
Definition: routerinfo_st.h:25
uint32_t addr
Definition: routerinfo_st.h:24
#define ROUTER_PURPOSE_CONTROLLER
Header file for routerinfo.c.