make dist: only include files from practracker dir intentionally.
[tor.git] / src / feature / control / control_fmt.c
blobe0e77eb2d08c0fa0c49a8ead82e0ec3be7719eaa
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 /**
6 * \file control_fmt.c
7 * \brief Formatting functions for controller data.
8 */
10 #include "core/or/or.h"
12 #include "core/mainloop/connection.h"
13 #include "core/or/circuitbuild.h"
14 #include "core/or/circuitlist.h"
15 #include "core/or/connection_edge.h"
16 #include "feature/control/control_fmt.h"
17 #include "feature/control/control_proto.h"
18 #include "feature/nodelist/nodelist.h"
20 #include "core/or/cpath_build_state_st.h"
21 #include "core/or/entry_connection_st.h"
22 #include "core/or/or_connection_st.h"
23 #include "core/or/origin_circuit_st.h"
24 #include "core/or/socks_request_st.h"
25 #include "feature/control/control_connection_st.h"
27 /** Given an AP connection <b>conn</b> and a <b>len</b>-character buffer
28 * <b>buf</b>, determine the address:port combination requested on
29 * <b>conn</b>, and write it to <b>buf</b>. Return 0 on success, -1 on
30 * failure. */
31 int
32 write_stream_target_to_buf(entry_connection_t *conn, char *buf, size_t len)
34 char buf2[256];
35 if (conn->chosen_exit_name)
36 if (tor_snprintf(buf2, sizeof(buf2), ".%s.exit", conn->chosen_exit_name)<0)
37 return -1;
38 if (!conn->socks_request)
39 return -1;
40 if (tor_snprintf(buf, len, "%s%s%s:%d",
41 conn->socks_request->address,
42 conn->chosen_exit_name ? buf2 : "",
43 !conn->chosen_exit_name && connection_edge_is_rendezvous_stream(
44 ENTRY_TO_EDGE_CONN(conn)) ? ".onion" : "",
45 conn->socks_request->port)<0)
46 return -1;
47 return 0;
50 /** Figure out the best name for the target router of an OR connection
51 * <b>conn</b>, and write it into the <b>len</b>-character buffer
52 * <b>name</b>. */
53 void
54 orconn_target_get_name(char *name, size_t len, or_connection_t *conn)
56 const node_t *node = node_get_by_id(conn->identity_digest);
57 if (node) {
58 tor_assert(len > MAX_VERBOSE_NICKNAME_LEN);
59 node_get_verbose_nickname(node, name);
60 } else if (! tor_digest_is_zero(conn->identity_digest)) {
61 name[0] = '$';
62 base16_encode(name+1, len-1, conn->identity_digest,
63 DIGEST_LEN);
64 } else {
65 tor_snprintf(name, len, "%s:%d",
66 conn->base_.address, conn->base_.port);
70 /** Allocate and return a description of <b>circ</b>'s current status,
71 * including its path (if any). */
72 char *
73 circuit_describe_status_for_controller(origin_circuit_t *circ)
75 char *rv;
76 smartlist_t *descparts = smartlist_new();
79 char *vpath = circuit_list_path_for_controller(circ);
80 if (*vpath) {
81 smartlist_add(descparts, vpath);
82 } else {
83 tor_free(vpath); /* empty path; don't put an extra space in the result */
88 cpath_build_state_t *build_state = circ->build_state;
89 smartlist_t *flaglist = smartlist_new();
90 char *flaglist_joined;
92 if (build_state->onehop_tunnel)
93 smartlist_add(flaglist, (void *)"ONEHOP_TUNNEL");
94 if (build_state->is_internal)
95 smartlist_add(flaglist, (void *)"IS_INTERNAL");
96 if (build_state->need_capacity)
97 smartlist_add(flaglist, (void *)"NEED_CAPACITY");
98 if (build_state->need_uptime)
99 smartlist_add(flaglist, (void *)"NEED_UPTIME");
101 /* Only emit a BUILD_FLAGS argument if it will have a non-empty value. */
102 if (smartlist_len(flaglist)) {
103 flaglist_joined = smartlist_join_strings(flaglist, ",", 0, NULL);
105 smartlist_add_asprintf(descparts, "BUILD_FLAGS=%s", flaglist_joined);
107 tor_free(flaglist_joined);
110 smartlist_free(flaglist);
113 smartlist_add_asprintf(descparts, "PURPOSE=%s",
114 circuit_purpose_to_controller_string(circ->base_.purpose));
117 const char *hs_state =
118 circuit_purpose_to_controller_hs_state_string(circ->base_.purpose);
120 if (hs_state != NULL) {
121 smartlist_add_asprintf(descparts, "HS_STATE=%s", hs_state);
125 if (circ->rend_data != NULL || circ->hs_ident != NULL) {
126 char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
127 const char *onion_address;
128 if (circ->rend_data) {
129 onion_address = rend_data_get_address(circ->rend_data);
130 } else {
131 hs_build_address(&circ->hs_ident->identity_pk, HS_VERSION_THREE, addr);
132 onion_address = addr;
134 smartlist_add_asprintf(descparts, "REND_QUERY=%s", onion_address);
138 char tbuf[ISO_TIME_USEC_LEN+1];
139 format_iso_time_nospace_usec(tbuf, &circ->base_.timestamp_created);
141 smartlist_add_asprintf(descparts, "TIME_CREATED=%s", tbuf);
144 // Show username and/or password if available.
145 if (circ->socks_username_len > 0) {
146 char* socks_username_escaped = esc_for_log_len(circ->socks_username,
147 (size_t) circ->socks_username_len);
148 smartlist_add_asprintf(descparts, "SOCKS_USERNAME=%s",
149 socks_username_escaped);
150 tor_free(socks_username_escaped);
152 if (circ->socks_password_len > 0) {
153 char* socks_password_escaped = esc_for_log_len(circ->socks_password,
154 (size_t) circ->socks_password_len);
155 smartlist_add_asprintf(descparts, "SOCKS_PASSWORD=%s",
156 socks_password_escaped);
157 tor_free(socks_password_escaped);
160 rv = smartlist_join_strings(descparts, " ", 0, NULL);
162 SMARTLIST_FOREACH(descparts, char *, cp, tor_free(cp));
163 smartlist_free(descparts);
165 return rv;
168 /** Return a longname the node whose identity is <b>id_digest</b>. If
169 * node_get_by_id() returns NULL, base 16 encoding of <b>id_digest</b> is
170 * returned instead.
172 * This function is not thread-safe. Each call to this function invalidates
173 * previous values returned by this function.
175 MOCK_IMPL(const char *,
176 node_describe_longname_by_id,(const char *id_digest))
178 static char longname[MAX_VERBOSE_NICKNAME_LEN+1];
179 node_get_verbose_nickname_by_id(id_digest, longname);
180 return longname;