1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2009, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Implementation for Tor's control-socket interface.
8 * See doc/spec/control-spec.txt for full details on protocol.
11 #define CONTROL_PRIVATE
15 /** Yield true iff <b>s</b> is the state of a control_connection_t that has
16 * finished authentication and is accepting commands. */
17 #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
19 /* Recognized asynchronous event types. It's okay to expand this list
20 * because it is used both as a list of v0 event types, and as indices
21 * into the bitfield to determine which controllers want which events.
23 #define _EVENT_MIN 0x0001
24 #define EVENT_CIRCUIT_STATUS 0x0001
25 #define EVENT_STREAM_STATUS 0x0002
26 #define EVENT_OR_CONN_STATUS 0x0003
27 #define EVENT_BANDWIDTH_USED 0x0004
28 #define EVENT_LOG_OBSOLETE 0x0005 /* Can reclaim this. */
29 #define EVENT_NEW_DESC 0x0006
30 #define EVENT_DEBUG_MSG 0x0007
31 #define EVENT_INFO_MSG 0x0008
32 #define EVENT_NOTICE_MSG 0x0009
33 #define EVENT_WARN_MSG 0x000A
34 #define EVENT_ERR_MSG 0x000B
35 #define EVENT_ADDRMAP 0x000C
36 // #define EVENT_AUTHDIR_NEWDESCS 0x000D
37 #define EVENT_DESCCHANGED 0x000E
38 // #define EVENT_NS 0x000F
39 #define EVENT_STATUS_CLIENT 0x0010
40 #define EVENT_STATUS_SERVER 0x0011
41 #define EVENT_STATUS_GENERAL 0x0012
42 #define EVENT_GUARD 0x0013
43 #define EVENT_STREAM_BANDWIDTH_USED 0x0014
44 #define EVENT_CLIENTS_SEEN 0x0015
45 #define EVENT_NEWCONSENSUS 0x0016
46 #define _EVENT_MAX 0x0016
47 /* If _EVENT_MAX ever hits 0x0020, we need to make the mask wider. */
49 /** Bitfield: The bit 1<<e is set if <b>any</b> open control
50 * connection is interested in events of type <b>e</b>. We use this
51 * so that we can decide to skip generating event messages that nobody
52 * has interest in without having to walk over the global connection
55 typedef uint32_t event_mask_t
;
57 /** An event mask of all the events that controller with the LONG_NAMES option
58 * set is interested in receiving. */
59 static event_mask_t global_event_mask1long
= 0;
61 /** An event mask of all the events that controller with the SHORT_NAMES option
62 * set is interested in receiving. */
63 static event_mask_t global_event_mask1short
= 0;
65 /** True iff we have disabled log messages from being sent to the controller */
66 static int disable_log_messages
= 0;
68 /** Macro: true if any control connection is interested in events of type
70 #define EVENT_IS_INTERESTING(e) \
71 ((global_event_mask1long|global_event_mask1short) & (1<<(e)))
72 /** Macro: true if any control connection with the LONG_NAMES option is
73 * interested in events of type <b>e</b>. */
74 #define EVENT_IS_INTERESTING1L(e) (global_event_mask1long & (1<<(e)))
75 /** Macro: true if any control connection with the SHORT_NAMES option is
76 * interested in events of type <b>e</b>. */
77 #define EVENT_IS_INTERESTING1S(e) (global_event_mask1short & (1<<(e)))
79 /** If we're using cookie-type authentication, how long should our cookies be?
81 #define AUTHENTICATION_COOKIE_LEN 32
83 /** If true, we've set authentication_cookie to a secret code and
84 * stored it to disk. */
85 static int authentication_cookie_is_set
= 0;
86 /** If authentication_cookie_is_set, a secret cookie that we've stored to disk
87 * and which we're using to authenticate controllers. (If the controller can
88 * read it off disk, it has permission to connect. */
89 static char authentication_cookie
[AUTHENTICATION_COOKIE_LEN
];
91 /** A sufficiently large size to record the last bootstrap phase string. */
92 #define BOOTSTRAP_MSG_LEN 1024
94 /** What was the last bootstrap phase message we sent? We keep track
95 * of this so we can respond to getinfo status/bootstrap-phase queries. */
96 static char last_sent_bootstrap_message
[BOOTSTRAP_MSG_LEN
];
98 /** Flag for event_format_t. Indicates that we should use the old
99 * name format of nickname|hexdigest
101 #define SHORT_NAMES 1
102 /** Flag for event_format_t. Indicates that we should use the new
103 * name format of $hexdigest[=~]nickname
106 #define ALL_NAMES (SHORT_NAMES|LONG_NAMES)
107 /** Flag for event_format_t. Indicates that we should use the new event
108 * format where extra event fields are allowed using a NAME=VAL format. */
109 #define EXTENDED_FORMAT 4
110 /** Flag for event_format_t. Indicates that we are using the old event format
111 * where extra fields aren't allowed. */
112 #define NONEXTENDED_FORMAT 8
113 #define ALL_FORMATS (EXTENDED_FORMAT|NONEXTENDED_FORMAT)
115 /** Bit field of flags to select how to format a controller event. Recognized
116 * flags are SHORT_NAMES, LONG_NAMES, EXTENDED_FORMAT, NONEXTENDED_FORMAT. */
117 typedef int event_format_t
;
119 static void connection_printf_to_buf(control_connection_t
*conn
,
120 const char *format
, ...)
122 static void send_control_done(control_connection_t
*conn
);
123 static void send_control_event(uint16_t event
, event_format_t which
,
124 const char *format
, ...)
126 static void send_control_event_extended(uint16_t event
, event_format_t which
,
127 const char *format
, ...)
129 static int handle_control_setconf(control_connection_t
*conn
, uint32_t len
,
131 static int handle_control_resetconf(control_connection_t
*conn
, uint32_t len
,
133 static int handle_control_getconf(control_connection_t
*conn
, uint32_t len
,
135 static int handle_control_loadconf(control_connection_t
*conn
, uint32_t len
,
137 static int handle_control_setevents(control_connection_t
*conn
, uint32_t len
,
139 static int handle_control_authenticate(control_connection_t
*conn
,
142 static int handle_control_saveconf(control_connection_t
*conn
, uint32_t len
,
144 static int handle_control_signal(control_connection_t
*conn
, uint32_t len
,
146 static int handle_control_mapaddress(control_connection_t
*conn
, uint32_t len
,
148 static char *list_getinfo_options(void);
149 static int handle_control_getinfo(control_connection_t
*conn
, uint32_t len
,
151 static int handle_control_extendcircuit(control_connection_t
*conn
,
154 static int handle_control_setcircuitpurpose(control_connection_t
*conn
,
155 uint32_t len
, const char *body
);
156 static int handle_control_attachstream(control_connection_t
*conn
,
159 static int handle_control_postdescriptor(control_connection_t
*conn
,
162 static int handle_control_redirectstream(control_connection_t
*conn
,
165 static int handle_control_closestream(control_connection_t
*conn
, uint32_t len
,
167 static int handle_control_closecircuit(control_connection_t
*conn
,
170 static int handle_control_resolve(control_connection_t
*conn
, uint32_t len
,
172 static int handle_control_usefeature(control_connection_t
*conn
,
175 static int write_stream_target_to_buf(edge_connection_t
*conn
, char *buf
,
177 static void orconn_target_get_name(int long_names
, char *buf
, size_t len
,
178 or_connection_t
*conn
);
179 static char *get_cookie_file(void);
181 /** Given a control event code for a message event, return the corresponding
184 event_to_log_severity(int event
)
187 case EVENT_DEBUG_MSG
: return LOG_DEBUG
;
188 case EVENT_INFO_MSG
: return LOG_INFO
;
189 case EVENT_NOTICE_MSG
: return LOG_NOTICE
;
190 case EVENT_WARN_MSG
: return LOG_WARN
;
191 case EVENT_ERR_MSG
: return LOG_ERR
;
196 /** Given a log severity, return the corresponding control event code. */
198 log_severity_to_event(int severity
)
201 case LOG_DEBUG
: return EVENT_DEBUG_MSG
;
202 case LOG_INFO
: return EVENT_INFO_MSG
;
203 case LOG_NOTICE
: return EVENT_NOTICE_MSG
;
204 case LOG_WARN
: return EVENT_WARN_MSG
;
205 case LOG_ERR
: return EVENT_ERR_MSG
;
210 /** Set <b>global_event_mask*</b> to the bitwise OR of each live control
211 * connection's event_mask field. */
213 control_update_global_event_mask(void)
215 smartlist_t
*conns
= get_connection_array();
216 event_mask_t old_mask
, new_mask
;
217 old_mask
= global_event_mask1short
;
218 old_mask
|= global_event_mask1long
;
220 global_event_mask1short
= 0;
221 global_event_mask1long
= 0;
222 SMARTLIST_FOREACH(conns
, connection_t
*, _conn
,
224 if (_conn
->type
== CONN_TYPE_CONTROL
&&
225 STATE_IS_OPEN(_conn
->state
)) {
226 control_connection_t
*conn
= TO_CONTROL_CONN(_conn
);
227 if (conn
->use_long_names
)
228 global_event_mask1long
|= conn
->event_mask
;
230 global_event_mask1short
|= conn
->event_mask
;
234 new_mask
= global_event_mask1short
;
235 new_mask
|= global_event_mask1long
;
237 /* Handle the aftermath. Set up the log callback to tell us only what
238 * we want to hear...*/
239 control_adjust_event_log_severity();
241 /* ...then, if we've started logging stream bw, clear the appropriate
243 if (! (old_mask
& EVENT_STREAM_BANDWIDTH_USED
) &&
244 (new_mask
& EVENT_STREAM_BANDWIDTH_USED
)) {
245 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
247 if (conn
->type
== CONN_TYPE_AP
) {
248 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
249 edge_conn
->n_written
= edge_conn
->n_read
= 0;
255 /** Adjust the log severities that result in control_event_logmsg being called
256 * to match the severity of log messages that any controllers are interested
259 control_adjust_event_log_severity(void)
262 int min_log_event
=EVENT_ERR_MSG
, max_log_event
=EVENT_DEBUG_MSG
;
264 for (i
= EVENT_DEBUG_MSG
; i
<= EVENT_ERR_MSG
; ++i
) {
265 if (EVENT_IS_INTERESTING(i
)) {
270 for (i
= EVENT_ERR_MSG
; i
>= EVENT_DEBUG_MSG
; --i
) {
271 if (EVENT_IS_INTERESTING(i
)) {
276 if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE
) ||
277 EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL
)) {
278 if (min_log_event
> EVENT_NOTICE_MSG
)
279 min_log_event
= EVENT_NOTICE_MSG
;
280 if (max_log_event
< EVENT_ERR_MSG
)
281 max_log_event
= EVENT_ERR_MSG
;
283 if (min_log_event
<= max_log_event
)
284 change_callback_log_severity(event_to_log_severity(min_log_event
),
285 event_to_log_severity(max_log_event
),
286 control_event_logmsg
);
288 change_callback_log_severity(LOG_ERR
, LOG_ERR
,
289 control_event_logmsg
);
292 /** Return true iff the event with code <b>c</b> is being sent to any current
293 * control connection. This is useful if the amount of work needed to prepare
294 * to call the appropriate control_event_...() function is high.
297 control_event_is_interesting(int event
)
299 return EVENT_IS_INTERESTING(event
);
302 /** Append a NUL-terminated string <b>s</b> to the end of
303 * <b>conn</b>-\>outbuf.
306 connection_write_str_to_buf(const char *s
, control_connection_t
*conn
)
308 size_t len
= strlen(s
);
309 connection_write_to_buf(s
, len
, TO_CONN(conn
));
312 /** Given a <b>len</b>-character string in <b>data</b>, made of lines
313 * terminated by CRLF, allocate a new string in *<b>out</b>, and copy the
314 * contents of <b>data</b> into *<b>out</b>, adding a period before any period
315 * that that appears at the start of a line, and adding a period-CRLF line at
316 * the end. Replace all LF characters sequences with CRLF. Return the number
317 * of bytes in *<b>out</b>.
320 write_escaped_data(const char *data
, size_t len
, char **out
)
322 size_t sz_out
= len
+8;
324 const char *start
= data
, *end
;
327 for (i
=0; i
<(int)len
; ++i
) {
329 sz_out
+= 2; /* Maybe add a CR; maybe add a dot. */
331 *out
= outp
= tor_malloc(sz_out
+1);
336 if (data
> start
&& data
[-1] != '\r')
339 } else if (*data
== '.') {
349 if (outp
< *out
+2 || memcmp(outp
-2, "\r\n", 2)) {
356 *outp
= '\0'; /* NUL-terminate just in case. */
357 tor_assert((outp
- *out
) <= (int)sz_out
);
361 /** Given a <b>len</b>-character string in <b>data</b>, made of lines
362 * terminated by CRLF, allocate a new string in *<b>out</b>, and copy
363 * the contents of <b>data</b> into *<b>out</b>, removing any period
364 * that appears at the start of a line, and replacing all CRLF sequences
365 * with LF. Return the number of
366 * bytes in *<b>out</b>. */
368 read_escaped_data(const char *data
, size_t len
, char **out
)
374 *out
= outp
= tor_malloc(len
+1);
379 /* we're at the start of a line. */
382 next
= memchr(data
, '\n', end
-data
);
384 size_t n_to_copy
= next
-data
;
385 /* Don't copy a CR that precedes this LF. */
386 if (n_to_copy
&& *(next
-1) == '\r')
388 memcpy(outp
, data
, n_to_copy
);
390 data
= next
+1; /* This will point at the start of the next line,
391 * or the end of the string, or a period. */
393 memcpy(outp
, data
, end
-data
);
405 /** If the first <b>in_len_max</b> characters in <b>start</b> contain a
406 * double-quoted string with escaped characters, return the length of that
407 * string (as encoded, including quotes). Otherwise return -1. */
409 get_escaped_string_length(const char *start
, size_t in_len_max
,
412 const char *cp
, *end
;
419 end
= start
+in_len_max
;
421 /* Calculate length. */
424 return -1; /* Too long. */
425 } else if (*cp
== '\\') {
427 return -1; /* Can't escape EOS. */
430 } else if (*cp
== '\"') {
439 return (int)(cp
- start
+1);
442 /** As decode_escaped_string, but does not decode the string: copies the
443 * entire thing, including quotation marks. */
445 extract_escaped_string(const char *start
, size_t in_len_max
,
446 char **out
, size_t *out_len
)
448 int length
= get_escaped_string_length(start
, in_len_max
, NULL
);
452 *out
= tor_strndup(start
, *out_len
);
456 /** Given a pointer to a string starting at <b>start</b> containing
457 * <b>in_len_max</b> characters, decode a string beginning with one double
458 * quote, containing any number of non-quote characters or characters escaped
459 * with a backslash, and ending with a final double quote. Place the resulting
460 * string (unquoted, unescaped) into a newly allocated string in *<b>out</b>;
461 * store its length in <b>out_len</b>. On success, return a pointer to the
462 * character immediately following the escaped string. On failure, return
465 decode_escaped_string(const char *start
, size_t in_len_max
,
466 char **out
, size_t *out_len
)
468 const char *cp
, *end
;
470 int len
, n_chars
= 0;
472 len
= get_escaped_string_length(start
, in_len_max
, &n_chars
);
476 end
= start
+len
-1; /* Index of last quote. */
477 tor_assert(*end
== '\"');
478 outp
= *out
= tor_malloc(len
+1);
488 tor_assert((outp
- *out
) == (int)*out_len
);
493 /** Acts like sprintf, but writes its formatted string to the end of
494 * <b>conn</b>-\>outbuf. The message may be truncated if it is too long,
495 * but it will always end with a CRLF sequence.
497 * Currently the length of the message is limited to 1024 (including the
498 * ending CR LF NUL ("\\r\\n\\0"). */
500 connection_printf_to_buf(control_connection_t
*conn
, const char *format
, ...)
502 #define CONNECTION_PRINTF_TO_BUF_BUFFERSIZE 1024
504 char buf
[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE
];
508 r
= tor_vsnprintf(buf
, sizeof(buf
), format
, ap
);
511 log_warn(LD_BUG
, "Unable to format string for controller.");
515 if (memcmp("\r\n\0", buf
+len
-2, 3)) {
516 buf
[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE
-1] = '\0';
517 buf
[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE
-2] = '\n';
518 buf
[CONNECTION_PRINTF_TO_BUF_BUFFERSIZE
-3] = '\r';
520 connection_write_to_buf(buf
, len
, TO_CONN(conn
));
523 /** Send a "DONE" message down the control connection <b>conn</b>. */
525 send_control_done(control_connection_t
*conn
)
527 connection_write_str_to_buf("250 OK\r\n", conn
);
530 /** Send an event to all v1 controllers that are listening for code
531 * <b>event</b>. The event's body is given by <b>msg</b>.
533 * If <b>which</b> & SHORT_NAMES, the event contains short-format names: send
534 * it to controllers that haven't enabled the VERBOSE_NAMES feature. If
535 * <b>which</b> & LONG_NAMES, the event contains long-format names: send it
536 * to controllers that <em>have</em> enabled VERBOSE_NAMES.
538 * The EXTENDED_FORMAT and NONEXTENDED_FORMAT flags behave similarly with
539 * respect to the EXTENDED_EVENTS feature. */
541 send_control_event_string(uint16_t event
, event_format_t which
,
544 smartlist_t
*conns
= get_connection_array();
545 tor_assert(event
>= _EVENT_MIN
&& event
<= _EVENT_MAX
);
547 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
549 if (conn
->type
== CONN_TYPE_CONTROL
&&
550 !conn
->marked_for_close
&&
551 conn
->state
== CONTROL_CONN_STATE_OPEN
) {
552 control_connection_t
*control_conn
= TO_CONTROL_CONN(conn
);
553 if (control_conn
->use_long_names
) {
554 if (!(which
& LONG_NAMES
))
557 if (!(which
& SHORT_NAMES
))
560 if (control_conn
->use_extended_events
) {
561 if (!(which
& EXTENDED_FORMAT
))
564 if (!(which
& NONEXTENDED_FORMAT
))
567 if (control_conn
->event_mask
& (1<<event
)) {
569 connection_write_to_buf(msg
, strlen(msg
), TO_CONN(control_conn
));
570 if (event
== EVENT_ERR_MSG
)
572 else if (event
== EVENT_STATUS_GENERAL
)
573 is_err
= !strcmpstart(msg
, "STATUS_GENERAL ERR ");
574 else if (event
== EVENT_STATUS_CLIENT
)
575 is_err
= !strcmpstart(msg
, "STATUS_CLIENT ERR ");
576 else if (event
== EVENT_STATUS_SERVER
)
577 is_err
= !strcmpstart(msg
, "STATUS_SERVER ERR ");
579 connection_handle_write(TO_CONN(control_conn
), 1);
585 /** Helper for send_control1_event and send_control1_event_extended:
586 * Send an event to all v1 controllers that are listening for code
587 * <b>event</b>. The event's body is created by the printf-style format in
588 * <b>format</b>, and other arguments as provided.
590 * If <b>extended</b> is true, and the format contains a single '@' character,
591 * it will be replaced with a space and all text after that character will be
592 * sent only to controllers that have enabled extended events.
594 * Currently the length of the message is limited to 1024 (including the
595 * ending \\r\\n\\0). */
597 send_control_event_impl(uint16_t event
, event_format_t which
, int extended
,
598 const char *format
, va_list ap
)
600 /* This is just a little longer than the longest allowed log message */
601 #define SEND_CONTROL1_EVENT_BUFFERSIZE 10064
603 char buf
[SEND_CONTROL1_EVENT_BUFFERSIZE
];
607 r
= tor_vsnprintf(buf
, sizeof(buf
), format
, ap
);
609 log_warn(LD_BUG
, "Unable to format event for controller.");
614 if (memcmp("\r\n\0", buf
+len
-2, 3)) {
615 /* if it is not properly terminated, do it now */
616 buf
[SEND_CONTROL1_EVENT_BUFFERSIZE
-1] = '\0';
617 buf
[SEND_CONTROL1_EVENT_BUFFERSIZE
-2] = '\n';
618 buf
[SEND_CONTROL1_EVENT_BUFFERSIZE
-3] = '\r';
621 if (extended
&& (cp
= strchr(buf
, '@'))) {
622 which
&= ~ALL_FORMATS
;
624 send_control_event_string(event
, which
|EXTENDED_FORMAT
, buf
);
625 memcpy(cp
, "\r\n\0", 3);
626 send_control_event_string(event
, which
|NONEXTENDED_FORMAT
, buf
);
628 send_control_event_string(event
, which
|ALL_FORMATS
, buf
);
632 /** Send an event to all v1 controllers that are listening for code
633 * <b>event</b>. The event's body is created by the printf-style format in
634 * <b>format</b>, and other arguments as provided.
636 * Currently the length of the message is limited to 1024 (including the
637 * ending \\n\\r\\0. */
639 send_control_event(uint16_t event
, event_format_t which
,
640 const char *format
, ...)
643 va_start(ap
, format
);
644 send_control_event_impl(event
, which
, 0, format
, ap
);
648 /** Send an event to all v1 controllers that are listening for code
649 * <b>event</b>. The event's body is created by the printf-style format in
650 * <b>format</b>, and other arguments as provided.
652 * If the format contains a single '@' character, it will be replaced with a
653 * space and all text after that character will be sent only to controllers
654 * that have enabled extended events.
656 * Currently the length of the message is limited to 1024 (including the
657 * ending \\n\\r\\0. */
659 send_control_event_extended(uint16_t event
, event_format_t which
,
660 const char *format
, ...)
663 va_start(ap
, format
);
664 send_control_event_impl(event
, which
, 1, format
, ap
);
668 /** Given a text circuit <b>id</b>, return the corresponding circuit. */
669 static origin_circuit_t
*
670 get_circ(const char *id
)
674 n_id
= (uint32_t) tor_parse_ulong(id
, 10, 0, UINT32_MAX
, &ok
, NULL
);
677 return circuit_get_by_global_id(n_id
);
680 /** Given a text stream <b>id</b>, return the corresponding AP connection. */
681 static edge_connection_t
*
682 get_stream(const char *id
)
687 n_id
= tor_parse_uint64(id
, 10, 0, UINT64_MAX
, &ok
, NULL
);
690 conn
= connection_get_by_global_id(n_id
);
691 if (!conn
|| conn
->type
!= CONN_TYPE_AP
|| conn
->marked_for_close
)
693 return TO_EDGE_CONN(conn
);
696 /** Helper for setconf and resetconf. Acts like setconf, except
697 * it passes <b>use_defaults</b> on to options_trial_assign(). Modifies the
701 control_setconf_helper(control_connection_t
*conn
, uint32_t len
, char *body
,
704 setopt_err_t opt_err
;
705 config_line_t
*lines
=NULL
;
707 char *errstring
= NULL
;
708 const int clear_first
= 1;
711 smartlist_t
*entries
= smartlist_create();
713 /* We have a string, "body", of the format '(key(=val|="val")?)' entries
714 * separated by space. break it into a list of configuration entries. */
719 while (!TOR_ISSPACE(*eq
) && *eq
!= '=')
721 key
= tor_strndup(body
, eq
-body
);
728 char *val_start
= body
;
729 while (!TOR_ISSPACE(*body
))
731 val
= tor_strndup(val_start
, body
-val_start
);
732 val_len
= strlen(val
);
734 body
= (char*)extract_escaped_string(body
, (len
- (body
-start
)),
737 connection_write_str_to_buf("551 Couldn't parse string\r\n", conn
);
738 SMARTLIST_FOREACH(entries
, char *, cp
, tor_free(cp
));
739 smartlist_free(entries
);
744 ent_len
= strlen(key
)+val_len
+3;
745 entry
= tor_malloc(ent_len
+1);
746 tor_snprintf(entry
, ent_len
, "%s %s", key
, val
);
752 smartlist_add(entries
, entry
);
753 while (TOR_ISSPACE(*body
))
757 smartlist_add(entries
, tor_strdup(""));
758 config
= smartlist_join_strings(entries
, "\n", 0, NULL
);
759 SMARTLIST_FOREACH(entries
, char *, cp
, tor_free(cp
));
760 smartlist_free(entries
);
762 if (config_get_lines(config
, &lines
) < 0) {
763 log_warn(LD_CONTROL
,"Controller gave us config lines we can't parse.");
764 connection_write_str_to_buf("551 Couldn't parse configuration\r\n",
771 opt_err
= options_trial_assign(lines
, use_defaults
, clear_first
, &errstring
);
775 case SETOPT_ERR_MISC
:
776 msg
= "552 Unrecognized option";
778 case SETOPT_ERR_PARSE
:
779 msg
= "513 Unacceptable option value";
781 case SETOPT_ERR_TRANSITION
:
782 msg
= "553 Transition not allowed";
784 case SETOPT_ERR_SETTING
:
786 msg
= "553 Unable to set option";
789 config_free_lines(lines
);
790 send_control_done(conn
);
794 "Controller gave us config lines that didn't validate: %s",
796 connection_printf_to_buf(conn
, "%s: %s\r\n", msg
, errstring
);
797 config_free_lines(lines
);
803 /** Called when we receive a SETCONF message: parse the body and try
804 * to update our configuration. Reply with a DONE or ERROR message.
805 * Modifies the contents of body.*/
807 handle_control_setconf(control_connection_t
*conn
, uint32_t len
, char *body
)
809 return control_setconf_helper(conn
, len
, body
, 0);
812 /** Called when we receive a RESETCONF message: parse the body and try
813 * to update our configuration. Reply with a DONE or ERROR message.
814 * Modifies the contents of body. */
816 handle_control_resetconf(control_connection_t
*conn
, uint32_t len
, char *body
)
818 return control_setconf_helper(conn
, len
, body
, 1);
821 /** Called when we receive a GETCONF message. Parse the request, and
822 * reply with a CONFVALUE or an ERROR message */
824 handle_control_getconf(control_connection_t
*conn
, uint32_t body_len
,
827 smartlist_t
*questions
= smartlist_create();
828 smartlist_t
*answers
= smartlist_create();
829 smartlist_t
*unrecognized
= smartlist_create();
832 or_options_t
*options
= get_options();
835 (void) body_len
; /* body is NUL-terminated; so we can ignore len. */
836 smartlist_split_string(questions
, body
, " ",
837 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
838 SMARTLIST_FOREACH(questions
, const char *, q
,
840 if (!option_is_recognized(q
)) {
841 smartlist_add(unrecognized
, (char*) q
);
843 config_line_t
*answer
= option_get_assignment(options
,q
);
845 const char *name
= option_get_canonical_name(q
);
846 size_t alen
= strlen(name
)+8;
847 char *astr
= tor_malloc(alen
);
848 tor_snprintf(astr
, alen
, "250-%s\r\n", name
);
849 smartlist_add(answers
, astr
);
854 size_t alen
= strlen(answer
->key
)+strlen(answer
->value
)+8;
855 char *astr
= tor_malloc(alen
);
856 tor_snprintf(astr
, alen
, "250-%s=%s\r\n",
857 answer
->key
, answer
->value
);
858 smartlist_add(answers
, astr
);
861 tor_free(answer
->key
);
862 tor_free(answer
->value
);
869 if ((len
= smartlist_len(unrecognized
))) {
870 for (i
=0; i
< len
-1; ++i
)
871 connection_printf_to_buf(conn
,
872 "552-Unrecognized configuration key \"%s\"\r\n",
873 (char*)smartlist_get(unrecognized
, i
));
874 connection_printf_to_buf(conn
,
875 "552 Unrecognized configuration key \"%s\"\r\n",
876 (char*)smartlist_get(unrecognized
, len
-1));
877 } else if ((len
= smartlist_len(answers
))) {
878 char *tmp
= smartlist_get(answers
, len
-1);
879 tor_assert(strlen(tmp
)>4);
881 msg
= smartlist_join_strings(answers
, "", 0, &msg_len
);
882 connection_write_to_buf(msg
, msg_len
, TO_CONN(conn
));
884 connection_write_str_to_buf("250 OK\r\n", conn
);
887 SMARTLIST_FOREACH(answers
, char *, cp
, tor_free(cp
));
888 smartlist_free(answers
);
889 SMARTLIST_FOREACH(questions
, char *, cp
, tor_free(cp
));
890 smartlist_free(questions
);
891 smartlist_free(unrecognized
);
898 /** Called when we get a +LOADCONF message. */
900 handle_control_loadconf(control_connection_t
*conn
, uint32_t len
,
904 char *errstring
= NULL
;
905 const char *msg
= NULL
;
908 retval
= options_init_from_string(body
, CMD_RUN_TOR
, NULL
, &errstring
);
910 if (retval
!= SETOPT_OK
) {
912 "Controller gave us config file that didn't validate: %s",
915 case SETOPT_ERR_PARSE
:
916 msg
= "552 Invalid config file";
918 case SETOPT_ERR_TRANSITION
:
919 msg
= "553 Transition not allowed";
921 case SETOPT_ERR_SETTING
:
922 msg
= "553 Unable to set option";
924 case SETOPT_ERR_MISC
:
926 msg
= "550 Unable to load config";
929 tor_fragile_assert();
933 connection_printf_to_buf(conn
, "%s: %s\r\n", msg
, errstring
);
935 connection_printf_to_buf(conn
, "%s\r\n", msg
);
939 send_control_done(conn
);
943 /** Called when we get a SETEVENTS message: update conn->event_mask,
944 * and reply with DONE or ERROR. */
946 handle_control_setevents(control_connection_t
*conn
, uint32_t len
,
950 uint32_t event_mask
= 0;
951 unsigned int extended
= 0;
952 smartlist_t
*events
= smartlist_create();
956 smartlist_split_string(events
, body
, " ",
957 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
958 SMARTLIST_FOREACH_BEGIN(events
, const char *, ev
)
960 if (!strcasecmp(ev
, "EXTENDED")) {
963 } else if (!strcasecmp(ev
, "CIRC"))
964 event_code
= EVENT_CIRCUIT_STATUS
;
965 else if (!strcasecmp(ev
, "STREAM"))
966 event_code
= EVENT_STREAM_STATUS
;
967 else if (!strcasecmp(ev
, "ORCONN"))
968 event_code
= EVENT_OR_CONN_STATUS
;
969 else if (!strcasecmp(ev
, "BW"))
970 event_code
= EVENT_BANDWIDTH_USED
;
971 else if (!strcasecmp(ev
, "DEBUG"))
972 event_code
= EVENT_DEBUG_MSG
;
973 else if (!strcasecmp(ev
, "INFO"))
974 event_code
= EVENT_INFO_MSG
;
975 else if (!strcasecmp(ev
, "NOTICE"))
976 event_code
= EVENT_NOTICE_MSG
;
977 else if (!strcasecmp(ev
, "WARN"))
978 event_code
= EVENT_WARN_MSG
;
979 else if (!strcasecmp(ev
, "ERR"))
980 event_code
= EVENT_ERR_MSG
;
981 else if (!strcasecmp(ev
, "NEWDESC"))
982 event_code
= EVENT_NEW_DESC
;
983 else if (!strcasecmp(ev
, "ADDRMAP"))
984 event_code
= EVENT_ADDRMAP
;
985 else if (!strcasecmp(ev
, "AUTHDIR_NEWDESCS"))
986 event_code
= EVENT_AUTHDIR_NEWDESCS
;
987 else if (!strcasecmp(ev
, "DESCCHANGED"))
988 event_code
= EVENT_DESCCHANGED
;
989 else if (!strcasecmp(ev
, "NS"))
990 event_code
= EVENT_NS
;
991 else if (!strcasecmp(ev
, "STATUS_GENERAL"))
992 event_code
= EVENT_STATUS_GENERAL
;
993 else if (!strcasecmp(ev
, "STATUS_CLIENT"))
994 event_code
= EVENT_STATUS_CLIENT
;
995 else if (!strcasecmp(ev
, "STATUS_SERVER"))
996 event_code
= EVENT_STATUS_SERVER
;
997 else if (!strcasecmp(ev
, "GUARD"))
998 event_code
= EVENT_GUARD
;
999 else if (!strcasecmp(ev
, "STREAM_BW"))
1000 event_code
= EVENT_STREAM_BANDWIDTH_USED
;
1001 else if (!strcasecmp(ev
, "CLIENTS_SEEN"))
1002 event_code
= EVENT_CLIENTS_SEEN
;
1003 else if (!strcasecmp(ev
, "NEWCONSENSUS"))
1004 event_code
= EVENT_NEWCONSENSUS
;
1006 connection_printf_to_buf(conn
, "552 Unrecognized event \"%s\"\r\n",
1008 SMARTLIST_FOREACH(events
, char *, e
, tor_free(e
));
1009 smartlist_free(events
);
1012 event_mask
|= (1 << event_code
);
1014 SMARTLIST_FOREACH_END(ev
);
1015 SMARTLIST_FOREACH(events
, char *, e
, tor_free(e
));
1016 smartlist_free(events
);
1018 conn
->event_mask
= event_mask
;
1020 conn
->use_extended_events
= 1;
1022 control_update_global_event_mask();
1023 send_control_done(conn
);
1027 /** Decode the hashed, base64'd passwords stored in <b>passwords</b>.
1028 * Return a smartlist of acceptable passwords (unterminated strings of
1029 * length S2K_SPECIFIER_LEN+DIGEST_LEN) on success, or NULL on failure.
1032 decode_hashed_passwords(config_line_t
*passwords
)
1036 smartlist_t
*sl
= smartlist_create();
1038 tor_assert(passwords
);
1040 for (cl
= passwords
; cl
; cl
= cl
->next
) {
1041 const char *hashed
= cl
->value
;
1043 if (!strcmpstart(hashed
, "16:")) {
1044 if (base16_decode(decoded
, sizeof(decoded
), hashed
+3, strlen(hashed
+3))<0
1045 || strlen(hashed
+3) != (S2K_SPECIFIER_LEN
+DIGEST_LEN
)*2) {
1049 if (base64_decode(decoded
, sizeof(decoded
), hashed
, strlen(hashed
))
1050 != S2K_SPECIFIER_LEN
+DIGEST_LEN
) {
1054 smartlist_add(sl
, tor_memdup(decoded
, S2K_SPECIFIER_LEN
+DIGEST_LEN
));
1060 SMARTLIST_FOREACH(sl
, char*, cp
, tor_free(cp
));
1065 /** Called when we get an AUTHENTICATE message. Check whether the
1066 * authentication is valid, and if so, update the connection's state to
1067 * OPEN. Reply with DONE or ERROR.
1070 handle_control_authenticate(control_connection_t
*conn
, uint32_t len
,
1073 int used_quoted_string
= 0;
1074 or_options_t
*options
= get_options();
1075 const char *errstr
= NULL
;
1077 size_t password_len
;
1080 int bad_cookie
=0, bad_password
=0;
1081 smartlist_t
*sl
= NULL
;
1083 if (TOR_ISXDIGIT(body
[0])) {
1085 while (TOR_ISXDIGIT(*cp
))
1087 i
= (int)(cp
- body
);
1090 password
= tor_malloc(password_len
+ 1);
1091 if (base16_decode(password
, password_len
+1, body
, i
)<0) {
1092 connection_write_str_to_buf(
1093 "551 Invalid hexadecimal encoding. Maybe you tried a plain text "
1094 "password? If so, the standard requires that you put it in "
1095 "double quotes.\r\n", conn
);
1096 connection_mark_for_close(TO_CONN(conn
));
1100 } else if (TOR_ISSPACE(body
[0])) {
1101 password
= tor_strdup("");
1104 if (!decode_escaped_string(body
, len
, &password
, &password_len
)) {
1105 connection_write_str_to_buf("551 Invalid quoted string. You need "
1106 "to put the password in double quotes.\r\n", conn
);
1107 connection_mark_for_close(TO_CONN(conn
));
1110 used_quoted_string
= 1;
1113 if (!options
->CookieAuthentication
&& !options
->HashedControlPassword
&&
1114 !options
->HashedControlSessionPassword
) {
1115 /* if Tor doesn't demand any stronger authentication, then
1116 * the controller can get in with anything. */
1120 if (options
->CookieAuthentication
) {
1121 int also_password
= options
->HashedControlPassword
!= NULL
||
1122 options
->HashedControlSessionPassword
!= NULL
;
1123 if (password_len
!= AUTHENTICATION_COOKIE_LEN
) {
1124 if (!also_password
) {
1125 log_warn(LD_CONTROL
, "Got authentication cookie with wrong length "
1126 "(%d)", (int)password_len
);
1127 errstr
= "Wrong length on authentication cookie.";
1131 } else if (memcmp(authentication_cookie
, password
, password_len
)) {
1132 if (!also_password
) {
1133 log_warn(LD_CONTROL
, "Got mismatched authentication cookie");
1134 errstr
= "Authentication cookie did not match expected value.";
1143 if (options
->HashedControlPassword
||
1144 options
->HashedControlSessionPassword
) {
1146 smartlist_t
*sl_tmp
;
1147 char received
[DIGEST_LEN
];
1148 int also_cookie
= options
->CookieAuthentication
;
1149 sl
= smartlist_create();
1150 if (options
->HashedControlPassword
) {
1151 sl_tmp
= decode_hashed_passwords(options
->HashedControlPassword
);
1155 smartlist_add_all(sl
, sl_tmp
);
1156 smartlist_free(sl_tmp
);
1159 if (options
->HashedControlSessionPassword
) {
1160 sl_tmp
= decode_hashed_passwords(options
->HashedControlSessionPassword
);
1164 smartlist_add_all(sl
, sl_tmp
);
1165 smartlist_free(sl_tmp
);
1170 log_warn(LD_CONTROL
,
1171 "Couldn't decode HashedControlPassword: invalid base16");
1172 errstr
="Couldn't decode HashedControlPassword value in configuration.";
1175 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1178 SMARTLIST_FOREACH(sl
, char *, expected
,
1180 secret_to_key(received
,DIGEST_LEN
,password
,password_len
,expected
);
1181 if (!memcmp(expected
+S2K_SPECIFIER_LEN
, received
, DIGEST_LEN
))
1184 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1187 if (used_quoted_string
)
1188 errstr
= "Password did not match HashedControlPassword value from "
1191 errstr
= "Password did not match HashedControlPassword value from "
1192 "configuration. Maybe you tried a plain text password? "
1193 "If so, the standard requires that you put it in double quotes.";
1200 /** We only get here if both kinds of authentication failed. */
1201 tor_assert(bad_password
&& bad_cookie
);
1202 log_warn(LD_CONTROL
, "Bad password or authentication cookie on controller.");
1203 errstr
= "Password did not match HashedControlPassword *or* authentication "
1208 connection_printf_to_buf(conn
, "515 Authentication failed: %s\r\n",
1209 errstr
? errstr
: "Unknown reason.");
1210 connection_mark_for_close(TO_CONN(conn
));
1213 log_info(LD_CONTROL
, "Authenticated control connection (%d)", conn
->_base
.s
);
1214 send_control_done(conn
);
1215 conn
->_base
.state
= CONTROL_CONN_STATE_OPEN
;
1217 if (sl
) { /* clean up */
1218 SMARTLIST_FOREACH(sl
, char *, cp
, tor_free(cp
));
1224 /** Called when we get a SAVECONF command. Try to flush the current options to
1225 * disk, and report success or failure. */
1227 handle_control_saveconf(control_connection_t
*conn
, uint32_t len
,
1232 if (options_save_current()<0) {
1233 connection_write_str_to_buf(
1234 "551 Unable to write configuration to disk.\r\n", conn
);
1236 send_control_done(conn
);
1241 /** Called when we get a SIGNAL command. React to the provided signal, and
1242 * report success or failure. (If the signal results in a shutdown, success
1243 * may not be reported.) */
1245 handle_control_signal(control_connection_t
*conn
, uint32_t len
,
1254 while (body
[n
] && ! TOR_ISSPACE(body
[n
]))
1256 s
= tor_strndup(body
, n
);
1257 if (!strcasecmp(s
, "RELOAD") || !strcasecmp(s
, "HUP"))
1259 else if (!strcasecmp(s
, "SHUTDOWN") || !strcasecmp(s
, "INT"))
1261 else if (!strcasecmp(s
, "DUMP") || !strcasecmp(s
, "USR1"))
1263 else if (!strcasecmp(s
, "DEBUG") || !strcasecmp(s
, "USR2"))
1265 else if (!strcasecmp(s
, "HALT") || !strcasecmp(s
, "TERM"))
1267 else if (!strcasecmp(s
, "NEWNYM"))
1269 else if (!strcasecmp(s
, "CLEARDNSCACHE"))
1270 sig
= SIGCLEARDNSCACHE
;
1272 connection_printf_to_buf(conn
, "552 Unrecognized signal code \"%s\"\r\n",
1280 send_control_done(conn
);
1281 /* Flush the "done" first if the signal might make us shut down. */
1282 if (sig
== SIGTERM
|| sig
== SIGINT
)
1283 connection_handle_write(TO_CONN(conn
), 1);
1284 control_signal_act(sig
);
1288 /** Called when we get a MAPADDRESS command; try to bind all listed addresses,
1289 * and report success or failure. */
1291 handle_control_mapaddress(control_connection_t
*conn
, uint32_t len
,
1299 (void) len
; /* body is NUL-terminated, so it's safe to ignore the length. */
1301 lines
= smartlist_create();
1302 elts
= smartlist_create();
1303 reply
= smartlist_create();
1304 smartlist_split_string(lines
, body
, " ",
1305 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
1306 SMARTLIST_FOREACH(lines
, char *, line
,
1309 smartlist_split_string(elts
, line
, "=", 0, 2);
1310 if (smartlist_len(elts
) == 2) {
1311 const char *from
= smartlist_get(elts
,0);
1312 const char *to
= smartlist_get(elts
,1);
1313 size_t anslen
= strlen(line
)+512;
1314 char *ans
= tor_malloc(anslen
);
1315 if (address_is_invalid_destination(to
, 1)) {
1316 tor_snprintf(ans
, anslen
,
1317 "512-syntax error: invalid address '%s'", to
);
1318 smartlist_add(reply
, ans
);
1319 log_warn(LD_CONTROL
,
1320 "Skipping invalid argument '%s' in MapAddress msg", to
);
1321 } else if (!strcmp(from
, ".") || !strcmp(from
, "0.0.0.0")) {
1322 const char *address
= addressmap_register_virtual_address(
1323 !strcmp(from
,".") ? RESOLVED_TYPE_HOSTNAME
: RESOLVED_TYPE_IPV4
,
1326 tor_snprintf(ans
, anslen
,
1327 "451-resource exhausted: skipping '%s'", line
);
1328 smartlist_add(reply
, ans
);
1329 log_warn(LD_CONTROL
,
1330 "Unable to allocate address for '%s' in MapAddress msg",
1333 tor_snprintf(ans
, anslen
, "250-%s=%s", address
, to
);
1334 smartlist_add(reply
, ans
);
1337 addressmap_register(from
, tor_strdup(to
), 1, ADDRMAPSRC_CONTROLLER
);
1338 tor_snprintf(ans
, anslen
, "250-%s", line
);
1339 smartlist_add(reply
, ans
);
1342 size_t anslen
= strlen(line
)+256;
1343 char *ans
= tor_malloc(anslen
);
1344 tor_snprintf(ans
, anslen
, "512-syntax error: mapping '%s' is "
1345 "not of expected form 'foo=bar'.", line
);
1346 smartlist_add(reply
, ans
);
1347 log_info(LD_CONTROL
, "Skipping MapAddress '%s': wrong "
1348 "number of items.", safe_str(line
));
1350 SMARTLIST_FOREACH(elts
, char *, cp
, tor_free(cp
));
1351 smartlist_clear(elts
);
1353 SMARTLIST_FOREACH(lines
, char *, cp
, tor_free(cp
));
1354 smartlist_free(lines
);
1355 smartlist_free(elts
);
1357 if (smartlist_len(reply
)) {
1358 ((char*)smartlist_get(reply
,smartlist_len(reply
)-1))[3] = ' ';
1359 r
= smartlist_join_strings(reply
, "\r\n", 1, &sz
);
1360 connection_write_to_buf(r
, sz
, TO_CONN(conn
));
1363 const char *response
=
1364 "512 syntax error: not enough arguments to mapaddress.\r\n";
1365 connection_write_to_buf(response
, strlen(response
), TO_CONN(conn
));
1368 SMARTLIST_FOREACH(reply
, char *, cp
, tor_free(cp
));
1369 smartlist_free(reply
);
1373 /** Implementation helper for GETINFO: knows the answers for various
1374 * trivial-to-implement questions. */
1376 getinfo_helper_misc(control_connection_t
*conn
, const char *question
,
1380 if (!strcmp(question
, "version")) {
1381 *answer
= tor_strdup(get_version());
1382 } else if (!strcmp(question
, "config-file")) {
1383 *answer
= tor_strdup(get_torrc_fname());
1384 } else if (!strcmp(question
, "info/names")) {
1385 *answer
= list_getinfo_options();
1386 } else if (!strcmp(question
, "events/names")) {
1387 *answer
= tor_strdup("CIRC STREAM ORCONN BW DEBUG INFO NOTICE WARN ERR "
1388 "NEWDESC ADDRMAP AUTHDIR_NEWDESCS DESCCHANGED "
1389 "NS STATUS_GENERAL STATUS_CLIENT STATUS_SERVER "
1390 "GUARD STREAM_BW CLIENTS_SEEN NEWCONSENSUS");
1391 } else if (!strcmp(question
, "features/names")) {
1392 *answer
= tor_strdup("VERBOSE_NAMES EXTENDED_EVENTS");
1393 } else if (!strcmp(question
, "address")) {
1395 if (router_pick_published_address(get_options(), &addr
) < 0)
1397 *answer
= tor_dup_ip(addr
);
1398 } else if (!strcmp(question
, "dir-usage")) {
1399 *answer
= directory_dump_request_log();
1400 } else if (!strcmp(question
, "fingerprint")) {
1401 routerinfo_t
*me
= router_get_my_routerinfo();
1404 *answer
= tor_malloc(HEX_DIGEST_LEN
+1);
1405 base16_encode(*answer
, HEX_DIGEST_LEN
+1, me
->cache_info
.identity_digest
,
1411 /** Awful hack: return a newly allocated string based on a routerinfo and
1412 * (possibly) an extrainfo, sticking the read-history and write-history from
1413 * <b>ei</b> into the resulting string. The thing you get back won't
1414 * necessarily have a valid signature.
1416 * New code should never use this; it's for backward compatibility.
1418 * NOTE: <b>ri_body</b> is as returned by signed_descriptor_get_body: it might
1419 * not be NUL-terminated. */
1421 munge_extrainfo_into_routerinfo(const char *ri_body
, signed_descriptor_t
*ri
,
1422 signed_descriptor_t
*ei
)
1424 char *out
= NULL
, *outp
;
1426 const char *router_sig
;
1427 const char *ei_body
= signed_descriptor_get_body(ei
);
1428 size_t ri_len
= ri
->signed_descriptor_len
;
1429 size_t ei_len
= ei
->signed_descriptor_len
;
1433 outp
= out
= tor_malloc(ri_len
+ei_len
+1);
1434 if (!(router_sig
= tor_memstr(ri_body
, ri_len
, "\nrouter-signature")))
1437 memcpy(out
, ri_body
, router_sig
-ri_body
);
1438 outp
+= router_sig
-ri_body
;
1440 for (i
=0; i
< 2; ++i
) {
1441 const char *kwd
= i
?"\nwrite-history ":"\nread-history ";
1442 const char *cp
, *eol
;
1443 if (!(cp
= tor_memstr(ei_body
, ei_len
, kwd
)))
1446 eol
= memchr(cp
, '\n', ei_len
- (cp
-ei_body
));
1447 memcpy(outp
, cp
, eol
-cp
+1);
1450 memcpy(outp
, router_sig
, ri_len
- (router_sig
-ri_body
));
1452 tor_assert(outp
-out
< (int)(ri_len
+ei_len
+1));
1457 return tor_strndup(ri_body
, ri
->signed_descriptor_len
);
1460 /** Implementation helper for GETINFO: knows the answers for questions about
1461 * directory information. */
1463 getinfo_helper_dir(control_connection_t
*control_conn
,
1464 const char *question
, char **answer
)
1466 if (!strcmpstart(question
, "desc/id/")) {
1467 routerinfo_t
*ri
= router_get_by_hexdigest(question
+strlen("desc/id/"));
1469 const char *body
= signed_descriptor_get_body(&ri
->cache_info
);
1471 *answer
= tor_strndup(body
, ri
->cache_info
.signed_descriptor_len
);
1473 } else if (!strcmpstart(question
, "desc/name/")) {
1474 routerinfo_t
*ri
= router_get_by_nickname(question
+strlen("desc/name/"),1);
1476 const char *body
= signed_descriptor_get_body(&ri
->cache_info
);
1478 *answer
= tor_strndup(body
, ri
->cache_info
.signed_descriptor_len
);
1480 } else if (!strcmp(question
, "desc/all-recent")) {
1481 routerlist_t
*routerlist
= router_get_routerlist();
1482 smartlist_t
*sl
= smartlist_create();
1483 if (routerlist
&& routerlist
->routers
) {
1484 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
1486 const char *body
= signed_descriptor_get_body(&ri
->cache_info
);
1489 tor_strndup(body
, ri
->cache_info
.signed_descriptor_len
));
1492 *answer
= smartlist_join_strings(sl
, "", 0, NULL
);
1493 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
1495 } else if (!strcmp(question
, "desc/all-recent-extrainfo-hack")) {
1496 /* XXXX Remove this once Torstat asks for extrainfos. */
1497 routerlist_t
*routerlist
= router_get_routerlist();
1498 smartlist_t
*sl
= smartlist_create();
1499 if (routerlist
&& routerlist
->routers
) {
1500 SMARTLIST_FOREACH(routerlist
->routers
, routerinfo_t
*, ri
,
1502 const char *body
= signed_descriptor_get_body(&ri
->cache_info
);
1503 signed_descriptor_t
*ei
= extrainfo_get_by_descriptor_digest(
1504 ri
->cache_info
.extra_info_digest
);
1506 smartlist_add(sl
, munge_extrainfo_into_routerinfo(body
,
1507 &ri
->cache_info
, ei
));
1510 tor_strndup(body
, ri
->cache_info
.signed_descriptor_len
));
1514 *answer
= smartlist_join_strings(sl
, "", 0, NULL
);
1515 SMARTLIST_FOREACH(sl
, char *, c
, tor_free(c
));
1517 } else if (!strcmpstart(question
, "desc-annotations/id/")) {
1518 routerinfo_t
*ri
= router_get_by_hexdigest(question
+
1519 strlen("desc-annotations/id/"));
1521 const char *annotations
=
1522 signed_descriptor_get_annotations(&ri
->cache_info
);
1524 *answer
= tor_strndup(annotations
,
1525 ri
->cache_info
.annotations_len
);
1527 } else if (!strcmpstart(question
, "dir/server/")) {
1528 size_t answer_len
= 0, url_len
= strlen(question
)+2;
1529 char *url
= tor_malloc(url_len
);
1530 smartlist_t
*descs
= smartlist_create();
1534 tor_snprintf(url
, url_len
, "/tor/%s", question
+4);
1535 res
= dirserv_get_routerdescs(descs
, url
, &msg
);
1537 log_warn(LD_CONTROL
, "getinfo '%s': %s", question
, msg
);
1538 smartlist_free(descs
);
1542 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, sd
,
1543 answer_len
+= sd
->signed_descriptor_len
);
1544 cp
= *answer
= tor_malloc(answer_len
+1);
1545 SMARTLIST_FOREACH(descs
, signed_descriptor_t
*, sd
,
1547 memcpy(cp
, signed_descriptor_get_body(sd
),
1548 sd
->signed_descriptor_len
);
1549 cp
+= sd
->signed_descriptor_len
;
1553 smartlist_free(descs
);
1554 } else if (!strcmpstart(question
, "dir/status/")) {
1555 if (directory_permits_controller_requests(get_options())) {
1558 smartlist_t
*status_list
= smartlist_create();
1559 dirserv_get_networkstatus_v2(status_list
,
1560 question
+strlen("dir/status/"));
1561 SMARTLIST_FOREACH(status_list
, cached_dir_t
*, d
, len
+= d
->dir_len
);
1562 cp
= *answer
= tor_malloc(len
+1);
1563 SMARTLIST_FOREACH(status_list
, cached_dir_t
*, d
, {
1564 memcpy(cp
, d
->dir
, d
->dir_len
);
1568 smartlist_free(status_list
);
1570 smartlist_t
*fp_list
= smartlist_create();
1571 smartlist_t
*status_list
= smartlist_create();
1572 dirserv_get_networkstatus_v2_fingerprints(
1573 fp_list
, question
+strlen("dir/status/"));
1574 SMARTLIST_FOREACH(fp_list
, const char *, fp
, {
1576 char *fname
= networkstatus_get_cache_filename(fp
);
1577 s
= read_file_to_str(fname
, 0, NULL
);
1579 smartlist_add(status_list
, s
);
1582 SMARTLIST_FOREACH(fp_list
, char *, fp
, tor_free(fp
));
1583 smartlist_free(fp_list
);
1584 *answer
= smartlist_join_strings(status_list
, "", 0, NULL
);
1585 SMARTLIST_FOREACH(status_list
, char *, s
, tor_free(s
));
1586 smartlist_free(status_list
);
1588 } else if (!strcmp(question
, "dir/status-vote/current/consensus")) { /* v3 */
1589 if (directory_caches_dir_info(get_options())) {
1590 const cached_dir_t
*consensus
= dirserv_get_consensus();
1592 *answer
= tor_strdup(consensus
->dir
);
1594 if (!*answer
) { /* try loading it from disk */
1595 char *filename
= get_datadir_fname("cached-consensus");
1596 *answer
= read_file_to_str(filename
, RFTS_IGNORE_MISSING
, NULL
);
1599 } else if (!strcmp(question
, "network-status")) { /* v1 */
1600 routerlist_t
*routerlist
= router_get_routerlist();
1601 int verbose
= control_conn
->use_long_names
;
1602 if (!routerlist
|| !routerlist
->routers
||
1603 list_server_status_v1(routerlist
->routers
, answer
,
1604 verbose
? 2 : 1) < 0) {
1607 } else if (!strcmpstart(question
, "extra-info/digest/")) {
1608 question
+= strlen("extra-info/digest/");
1609 if (strlen(question
) == HEX_DIGEST_LEN
) {
1611 signed_descriptor_t
*sd
= NULL
;
1612 if (base16_decode(d
, sizeof(d
), question
, strlen(question
))==0) {
1613 /* XXXX this test should move into extrainfo_get_by_descriptor_digest,
1614 * but I don't want to risk affecting other parts of the code,
1615 * especially since the rules for using our own extrainfo (including
1616 * when it might be freed) are different from those for using one
1617 * we have downloaded. */
1618 if (router_extrainfo_digest_is_me(d
))
1619 sd
= &(router_get_my_extrainfo()->cache_info
);
1621 sd
= extrainfo_get_by_descriptor_digest(d
);
1624 const char *body
= signed_descriptor_get_body(sd
);
1626 *answer
= tor_strndup(body
, sd
->signed_descriptor_len
);
1634 /** Implementation helper for GETINFO: knows how to generate summaries of the
1635 * current states of things we send events about. */
1637 getinfo_helper_events(control_connection_t
*control_conn
,
1638 const char *question
, char **answer
)
1640 if (!strcmp(question
, "circuit-status")) {
1642 smartlist_t
*status
= smartlist_create();
1643 for (circ
= _circuit_get_global_list(); circ
; circ
= circ
->next
) {
1647 const char *purpose
;
1648 if (! CIRCUIT_IS_ORIGIN(circ
) || circ
->marked_for_close
)
1650 if (control_conn
->use_long_names
)
1651 path
= circuit_list_path_for_controller(TO_ORIGIN_CIRCUIT(circ
));
1653 path
= circuit_list_path(TO_ORIGIN_CIRCUIT(circ
),0);
1654 if (circ
->state
== CIRCUIT_STATE_OPEN
)
1656 else if (strlen(path
))
1661 purpose
= circuit_purpose_to_controller_string(circ
->purpose
);
1662 slen
= strlen(path
)+strlen(state
)+strlen(purpose
)+30;
1663 s
= tor_malloc(slen
+1);
1664 tor_snprintf(s
, slen
, "%lu %s%s%s PURPOSE=%s",
1665 (unsigned long)TO_ORIGIN_CIRCUIT(circ
)->global_identifier
,
1666 state
, *path
? " " : "", path
, purpose
);
1667 smartlist_add(status
, s
);
1670 *answer
= smartlist_join_strings(status
, "\r\n", 0, NULL
);
1671 SMARTLIST_FOREACH(status
, char *, cp
, tor_free(cp
));
1672 smartlist_free(status
);
1673 } else if (!strcmp(question
, "stream-status")) {
1674 smartlist_t
*conns
= get_connection_array();
1675 smartlist_t
*status
= smartlist_create();
1677 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, base_conn
) {
1679 edge_connection_t
*conn
;
1683 origin_circuit_t
*origin_circ
= NULL
;
1684 if (base_conn
->type
!= CONN_TYPE_AP
||
1685 base_conn
->marked_for_close
||
1686 base_conn
->state
== AP_CONN_STATE_SOCKS_WAIT
||
1687 base_conn
->state
== AP_CONN_STATE_NATD_WAIT
)
1689 conn
= TO_EDGE_CONN(base_conn
);
1690 switch (conn
->_base
.state
)
1692 case AP_CONN_STATE_CONTROLLER_WAIT
:
1693 case AP_CONN_STATE_CIRCUIT_WAIT
:
1694 if (conn
->socks_request
&&
1695 SOCKS_COMMAND_IS_RESOLVE(conn
->socks_request
->command
))
1696 state
= "NEWRESOLVE";
1700 case AP_CONN_STATE_RENDDESC_WAIT
:
1701 case AP_CONN_STATE_CONNECT_WAIT
:
1702 state
= "SENTCONNECT"; break;
1703 case AP_CONN_STATE_RESOLVE_WAIT
:
1704 state
= "SENTRESOLVE"; break;
1705 case AP_CONN_STATE_OPEN
:
1706 state
= "SUCCEEDED"; break;
1708 log_warn(LD_BUG
, "Asked for stream in unknown state %d",
1712 circ
= circuit_get_by_edge_conn(conn
);
1713 if (circ
&& CIRCUIT_IS_ORIGIN(circ
))
1714 origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
1715 write_stream_target_to_buf(conn
, buf
, sizeof(buf
));
1716 slen
= strlen(buf
)+strlen(state
)+32;
1717 s
= tor_malloc(slen
+1);
1718 tor_snprintf(s
, slen
, "%lu %s %lu %s",
1719 (unsigned long) conn
->_base
.global_identifier
,state
,
1721 (unsigned long)origin_circ
->global_identifier
: 0ul,
1723 smartlist_add(status
, s
);
1724 } SMARTLIST_FOREACH_END(base_conn
);
1725 *answer
= smartlist_join_strings(status
, "\r\n", 0, NULL
);
1726 SMARTLIST_FOREACH(status
, char *, cp
, tor_free(cp
));
1727 smartlist_free(status
);
1728 } else if (!strcmp(question
, "orconn-status")) {
1729 smartlist_t
*conns
= get_connection_array();
1730 smartlist_t
*status
= smartlist_create();
1731 SMARTLIST_FOREACH(conns
, connection_t
*, base_conn
,
1737 or_connection_t
*conn
;
1738 if (base_conn
->type
!= CONN_TYPE_OR
|| base_conn
->marked_for_close
)
1740 conn
= TO_OR_CONN(base_conn
);
1741 if (conn
->_base
.state
== OR_CONN_STATE_OPEN
)
1742 state
= "CONNECTED";
1743 else if (conn
->nickname
)
1747 orconn_target_get_name(control_conn
->use_long_names
, name
, sizeof(name
),
1749 slen
= strlen(name
)+strlen(state
)+2;
1750 s
= tor_malloc(slen
+1);
1751 tor_snprintf(s
, slen
, "%s %s", name
, state
);
1752 smartlist_add(status
, s
);
1754 *answer
= smartlist_join_strings(status
, "\r\n", 0, NULL
);
1755 SMARTLIST_FOREACH(status
, char *, cp
, tor_free(cp
));
1756 smartlist_free(status
);
1757 } else if (!strcmpstart(question
, "addr-mappings/") ||
1758 !strcmpstart(question
, "address-mappings/")) {
1759 time_t min_e
, max_e
;
1760 smartlist_t
*mappings
;
1761 int want_expiry
= !strcmpstart(question
, "address-mappings/");
1762 if (!strcmpstart(question
, "addr-mappings/")) {
1763 /* XXXX022 This has been deprecated since 0.2.0.3-alpha, and has
1764 generated a warning since 0.2.1.10-alpha; remove late in 0.2.2.x. */
1765 log_warn(LD_CONTROL
, "Controller used obsolete addr-mappings/ GETINFO "
1766 "key; use address-mappings/ instead.");
1768 question
+= strlen(want_expiry
? "address-mappings/"
1769 : "addr-mappings/");
1770 if (!strcmp(question
, "all")) {
1771 min_e
= 0; max_e
= TIME_MAX
;
1772 } else if (!strcmp(question
, "cache")) {
1773 min_e
= 2; max_e
= TIME_MAX
;
1774 } else if (!strcmp(question
, "config")) {
1775 min_e
= 0; max_e
= 0;
1776 } else if (!strcmp(question
, "control")) {
1777 min_e
= 1; max_e
= 1;
1781 mappings
= smartlist_create();
1782 addressmap_get_mappings(mappings
, min_e
, max_e
, want_expiry
);
1783 *answer
= smartlist_join_strings(mappings
, "\r\n", 0, NULL
);
1784 SMARTLIST_FOREACH(mappings
, char *, cp
, tor_free(cp
));
1785 smartlist_free(mappings
);
1786 } else if (!strcmpstart(question
, "status/")) {
1787 /* Note that status/ is not a catch-all for events; there's only supposed
1788 * to be a status GETINFO if there's a corresponding STATUS event. */
1789 if (!strcmp(question
, "status/circuit-established")) {
1790 *answer
= tor_strdup(has_completed_circuit
? "1" : "0");
1791 } else if (!strcmp(question
, "status/enough-dir-info")) {
1792 *answer
= tor_strdup(router_have_minimum_dir_info() ? "1" : "0");
1793 } else if (!strcmp(question
, "status/good-server-descriptor") ||
1794 !strcmp(question
, "status/accepted-server-descriptor")) {
1795 /* They're equivalent for now, until we can figure out how to make
1796 * good-server-descriptor be what we want. See comment in
1797 * control-spec.txt. */
1798 *answer
= tor_strdup(directories_have_accepted_server_descriptor()
1800 } else if (!strcmp(question
, "status/reachability-succeeded/or")) {
1801 *answer
= tor_strdup(check_whether_orport_reachable() ? "1" : "0");
1802 } else if (!strcmp(question
, "status/reachability-succeeded/dir")) {
1803 *answer
= tor_strdup(check_whether_dirport_reachable() ? "1" : "0");
1804 } else if (!strcmp(question
, "status/reachability-succeeded")) {
1805 *answer
= tor_malloc(16);
1806 tor_snprintf(*answer
, 16, "OR=%d DIR=%d",
1807 check_whether_orport_reachable() ? 1 : 0,
1808 check_whether_dirport_reachable() ? 1 : 0);
1809 } else if (!strcmp(question
, "status/bootstrap-phase")) {
1810 *answer
= tor_strdup(last_sent_bootstrap_message
);
1811 } else if (!strcmpstart(question
, "status/version/")) {
1812 int is_server
= server_mode(get_options());
1813 networkstatus_t
*c
= networkstatus_get_latest_consensus();
1814 version_status_t status
;
1815 const char *recommended
;
1817 recommended
= is_server
? c
->server_versions
: c
->client_versions
;
1818 status
= tor_version_is_obsolete(VERSION
, recommended
);
1821 status
= VS_UNKNOWN
;
1824 if (!strcmp(question
, "status/version/recommended")) {
1825 *answer
= tor_strdup(recommended
);
1828 if (!strcmp(question
, "status/version/current")) {
1831 case VS_RECOMMENDED
: *answer
= tor_strdup("recommended"); break;
1832 case VS_OLD
: *answer
= tor_strdup("obsolete"); break;
1833 case VS_NEW
: *answer
= tor_strdup("new"); break;
1834 case VS_NEW_IN_SERIES
: *answer
= tor_strdup("new in series"); break;
1835 case VS_UNRECOMMENDED
: *answer
= tor_strdup("unrecommended"); break;
1836 case VS_EMPTY
: *answer
= tor_strdup("none recommended"); break;
1837 case VS_UNKNOWN
: *answer
= tor_strdup("unknown"); break;
1838 default: tor_fragile_assert();
1840 } else if (!strcmp(question
, "status/version/num-versioning") ||
1841 !strcmp(question
, "status/version/num-concurring")) {
1843 tor_snprintf(s
, sizeof(s
), "%d", get_n_authorities(V3_AUTHORITY
));
1844 *answer
= tor_strdup(s
);
1845 log_warn(LD_GENERAL
, "%s is deprecated; it no longer gives useful "
1846 "information", question
);
1848 } else if (!strcmp(question
, "status/clients-seen")) {
1849 char geoip_start
[ISO_TIME_LEN
+1];
1851 char *geoip_summary
= extrainfo_get_client_geoip_summary(time(NULL
));
1856 answer_len
= strlen("TimeStarted=\"\" CountrySummary=") +
1857 ISO_TIME_LEN
+ strlen(geoip_summary
) + 1;
1858 *answer
= tor_malloc(answer_len
);
1859 format_iso_time(geoip_start
, geoip_get_history_start());
1860 tor_snprintf(*answer
, answer_len
,
1861 "TimeStarted=\"%s\" CountrySummary=%s",
1862 geoip_start
, geoip_summary
);
1863 tor_free(geoip_summary
);
1871 /** Callback function for GETINFO: on a given control connection, try to
1872 * answer the question <b>q</b> and store the newly-allocated answer in
1873 * *<b>a</b>. If there's no answer, or an error occurs, just don't set
1874 * <b>a</b>. Return 0.
1876 typedef int (*getinfo_helper_t
)(control_connection_t
*,
1877 const char *q
, char **a
);
1879 /** A single item for the GETINFO question-to-answer-function table. */
1880 typedef struct getinfo_item_t
{
1881 const char *varname
; /**< The value (or prefix) of the question. */
1882 getinfo_helper_t fn
; /**< The function that knows the answer: NULL if
1883 * this entry is documentation-only. */
1884 const char *desc
; /**< Description of the variable. */
1885 int is_prefix
; /** Must varname match exactly, or must it be a prefix? */
1888 #define ITEM(name, fn, desc) { name, getinfo_helper_##fn, desc, 0 }
1889 #define PREFIX(name, fn, desc) { name, getinfo_helper_##fn, desc, 1 }
1890 #define DOC(name, desc) { name, NULL, desc, 0 }
1892 /** Table mapping questions accepted by GETINFO to the functions that know how
1893 * to answer them. */
1894 static const getinfo_item_t getinfo_items
[] = {
1895 ITEM("version", misc
, "The current version of Tor."),
1896 ITEM("config-file", misc
, "Current location of the \"torrc\" file."),
1897 ITEM("accounting/bytes", accounting
,
1898 "Number of bytes read/written so far in the accounting interval."),
1899 ITEM("accounting/bytes-left", accounting
,
1900 "Number of bytes left to write/read so far in the accounting interval."),
1901 ITEM("accounting/enabled", accounting
, "Is accounting currently enabled?"),
1902 ITEM("accounting/hibernating", accounting
, "Are we hibernating or awake?"),
1903 ITEM("accounting/interval-start", accounting
,
1904 "Time when the accounting period starts."),
1905 ITEM("accounting/interval-end", accounting
,
1906 "Time when the accounting period ends."),
1907 ITEM("accounting/interval-wake", accounting
,
1908 "Time to wake up in this accounting period."),
1909 ITEM("helper-nodes", entry_guards
, NULL
), /* deprecated */
1910 ITEM("entry-guards", entry_guards
,
1911 "Which nodes are we using as entry guards?"),
1912 ITEM("fingerprint", misc
, NULL
),
1913 PREFIX("config/", config
, "Current configuration values."),
1915 "List of configuration options, types, and documentation."),
1916 ITEM("info/names", misc
,
1917 "List of GETINFO options, types, and documentation."),
1918 ITEM("events/names", misc
,
1919 "Events that the controller can ask for with SETEVENTS."),
1920 ITEM("features/names", misc
, "What arguments can USEFEATURE take?"),
1921 PREFIX("desc/id/", dir
, "Router descriptors by ID."),
1922 PREFIX("desc/name/", dir
, "Router descriptors by nickname."),
1923 ITEM("desc/all-recent", dir
,
1924 "All non-expired, non-superseded router descriptors."),
1925 ITEM("desc/all-recent-extrainfo-hack", dir
, NULL
), /* Hack. */
1926 PREFIX("extra-info/digest/", dir
, "Extra-info documents by digest."),
1927 ITEM("ns/all", networkstatus
,
1928 "Brief summary of router status (v2 directory format)"),
1929 PREFIX("ns/id/", networkstatus
,
1930 "Brief summary of router status by ID (v2 directory format)."),
1931 PREFIX("ns/name/", networkstatus
,
1932 "Brief summary of router status by nickname (v2 directory format)."),
1933 PREFIX("ns/purpose/", networkstatus
,
1934 "Brief summary of router status by purpose (v2 directory format)."),
1936 PREFIX("unregistered-servers-", dirserv_unregistered
, NULL
),
1937 ITEM("network-status", dir
,
1938 "Brief summary of router status (v1 directory format)"),
1939 ITEM("circuit-status", events
, "List of current circuits originating here."),
1940 ITEM("stream-status", events
,"List of current streams."),
1941 ITEM("orconn-status", events
, "A list of current OR connections."),
1942 PREFIX("address-mappings/", events
, NULL
),
1943 DOC("address-mappings/all", "Current address mappings."),
1944 DOC("address-mappings/cache", "Current cached DNS replies."),
1945 DOC("address-mappings/config",
1946 "Current address mappings from configuration."),
1947 DOC("address-mappings/control", "Current address mappings from controller."),
1948 PREFIX("addr-mappings/", events
, NULL
),
1949 DOC("addr-mappings/all", "Current address mappings without expiry times."),
1950 DOC("addr-mappings/cache",
1951 "Current cached DNS replies without expiry times."),
1952 DOC("addr-mappings/config",
1953 "Current address mappings from configuration without expiry times."),
1954 DOC("addr-mappings/control",
1955 "Current address mappings from controller without expiry times."),
1956 PREFIX("status/", events
, NULL
),
1957 DOC("status/circuit-established",
1958 "Whether we think client functionality is working."),
1959 DOC("status/enough-dir-info",
1960 "Whether we have enough up-to-date directory information to build "
1962 DOC("status/bootstrap-phase",
1963 "The last bootstrap phase status event that Tor sent."),
1964 DOC("status/clients-seen",
1965 "Breakdown of client countries seen by a bridge."),
1966 DOC("status/version/recommended", "List of currently recommended versions."),
1967 DOC("status/version/current", "Status of the current version."),
1968 DOC("status/version/num-versioning", "Number of versioning authorities."),
1969 DOC("status/version/num-concurring",
1970 "Number of versioning authorities agreeing on the status of the "
1972 ITEM("address", misc
, "IP address of this Tor host, if we can guess it."),
1973 ITEM("dir-usage", misc
, "Breakdown of bytes transferred over DirPort."),
1974 PREFIX("desc-annotations/id/", dir
, "Router annotations by hexdigest."),
1975 PREFIX("dir/server/", dir
,"Router descriptors as retrieved from a DirPort."),
1976 PREFIX("dir/status/", dir
,
1977 "v2 networkstatus docs as retrieved from a DirPort."),
1978 ITEM("dir/status-vote/current/consensus", dir
,
1979 "v3 Networkstatus consensus as retrieved from a DirPort."),
1980 PREFIX("exit-policy/default", policies
,
1981 "The default value appended to the configured exit policy."),
1982 PREFIX("ip-to-country/", geoip
, "Perform a GEOIP lookup"),
1983 { NULL
, NULL
, NULL
, 0 }
1986 /** Allocate and return a list of recognized GETINFO options. */
1988 list_getinfo_options(void)
1992 smartlist_t
*lines
= smartlist_create();
1994 for (i
= 0; getinfo_items
[i
].varname
; ++i
) {
1995 if (!getinfo_items
[i
].desc
)
1998 tor_snprintf(buf
, sizeof(buf
), "%s%s -- %s\n",
1999 getinfo_items
[i
].varname
,
2000 getinfo_items
[i
].is_prefix
? "*" : "",
2001 getinfo_items
[i
].desc
);
2002 smartlist_add(lines
, tor_strdup(buf
));
2004 smartlist_sort_strings(lines
);
2006 ans
= smartlist_join_strings(lines
, "", 0, NULL
);
2007 SMARTLIST_FOREACH(lines
, char *, cp
, tor_free(cp
));
2008 smartlist_free(lines
);
2013 /** Lookup the 'getinfo' entry <b>question</b>, and return
2014 * the answer in <b>*answer</b> (or NULL if key not recognized).
2015 * Return 0 if success or unrecognized, or -1 if recognized but
2016 * internal error. */
2018 handle_getinfo_helper(control_connection_t
*control_conn
,
2019 const char *question
, char **answer
)
2022 *answer
= NULL
; /* unrecognized key by default */
2024 for (i
= 0; getinfo_items
[i
].varname
; ++i
) {
2026 if (getinfo_items
[i
].is_prefix
)
2027 match
= !strcmpstart(question
, getinfo_items
[i
].varname
);
2029 match
= !strcmp(question
, getinfo_items
[i
].varname
);
2031 tor_assert(getinfo_items
[i
].fn
);
2032 return getinfo_items
[i
].fn(control_conn
, question
, answer
);
2036 return 0; /* unrecognized */
2039 /** Called when we receive a GETINFO command. Try to fetch all requested
2040 * information, and reply with information or error message. */
2042 handle_control_getinfo(control_connection_t
*conn
, uint32_t len
,
2045 smartlist_t
*questions
= smartlist_create();
2046 smartlist_t
*answers
= smartlist_create();
2047 smartlist_t
*unrecognized
= smartlist_create();
2048 char *msg
= NULL
, *ans
= NULL
;
2050 (void) len
; /* body is NUL-terminated, so it's safe to ignore the length. */
2052 smartlist_split_string(questions
, body
, " ",
2053 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2054 SMARTLIST_FOREACH(questions
, const char *, q
,
2056 if (handle_getinfo_helper(conn
, q
, &ans
) < 0) {
2057 connection_write_str_to_buf("551 Internal error\r\n", conn
);
2061 smartlist_add(unrecognized
, (char*)q
);
2063 smartlist_add(answers
, tor_strdup(q
));
2064 smartlist_add(answers
, ans
);
2067 if (smartlist_len(unrecognized
)) {
2068 for (i
=0; i
< smartlist_len(unrecognized
)-1; ++i
)
2069 connection_printf_to_buf(conn
,
2070 "552-Unrecognized key \"%s\"\r\n",
2071 (char*)smartlist_get(unrecognized
, i
));
2072 connection_printf_to_buf(conn
,
2073 "552 Unrecognized key \"%s\"\r\n",
2074 (char*)smartlist_get(unrecognized
, i
));
2078 for (i
= 0; i
< smartlist_len(answers
); i
+= 2) {
2079 char *k
= smartlist_get(answers
, i
);
2080 char *v
= smartlist_get(answers
, i
+1);
2081 if (!strchr(v
, '\n') && !strchr(v
, '\r')) {
2082 connection_printf_to_buf(conn
, "250-%s=", k
);
2083 connection_write_str_to_buf(v
, conn
);
2084 connection_write_str_to_buf("\r\n", conn
);
2088 esc_len
= write_escaped_data(v
, strlen(v
), &esc
);
2089 connection_printf_to_buf(conn
, "250+%s=\r\n", k
);
2090 connection_write_to_buf(esc
, esc_len
, TO_CONN(conn
));
2094 connection_write_str_to_buf("250 OK\r\n", conn
);
2097 SMARTLIST_FOREACH(answers
, char *, cp
, tor_free(cp
));
2098 smartlist_free(answers
);
2099 SMARTLIST_FOREACH(questions
, char *, cp
, tor_free(cp
));
2100 smartlist_free(questions
);
2101 smartlist_free(unrecognized
);
2107 /** Given a string, convert it to a circuit purpose. */
2109 circuit_purpose_from_string(const char *string
)
2111 if (!strcmpstart(string
, "purpose="))
2112 string
+= strlen("purpose=");
2114 if (!strcmp(string
, "general"))
2115 return CIRCUIT_PURPOSE_C_GENERAL
;
2116 else if (!strcmp(string
, "controller"))
2117 return CIRCUIT_PURPOSE_CONTROLLER
;
2119 return CIRCUIT_PURPOSE_UNKNOWN
;
2122 /** Return a newly allocated smartlist containing the arguments to the command
2123 * waiting in <b>body</b>. If there are fewer than <b>min_args</b> arguments,
2124 * or if <b>max_args</b> is nonnegative and there are more than
2125 * <b>max_args</b> arguments, send a 512 error to the controller, using
2126 * <b>command</b> as the command name in the error message. */
2127 static smartlist_t
*
2128 getargs_helper(const char *command
, control_connection_t
*conn
,
2129 const char *body
, int min_args
, int max_args
)
2131 smartlist_t
*args
= smartlist_create();
2132 smartlist_split_string(args
, body
, " ",
2133 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2134 if (smartlist_len(args
) < min_args
) {
2135 connection_printf_to_buf(conn
, "512 Missing argument to %s\r\n",command
);
2137 } else if (max_args
>= 0 && smartlist_len(args
) > max_args
) {
2138 connection_printf_to_buf(conn
, "512 Too many arguments to %s\r\n",command
);
2143 SMARTLIST_FOREACH(args
, char *, s
, tor_free(s
));
2144 smartlist_free(args
);
2148 /** Called when we get an EXTENDCIRCUIT message. Try to extend the listed
2149 * circuit, and report success or failure. */
2151 handle_control_extendcircuit(control_connection_t
*conn
, uint32_t len
,
2154 smartlist_t
*router_nicknames
=NULL
, *routers
=NULL
;
2155 origin_circuit_t
*circ
= NULL
;
2157 uint8_t intended_purpose
= CIRCUIT_PURPOSE_C_GENERAL
;
2161 router_nicknames
= smartlist_create();
2163 args
= getargs_helper("EXTENDCIRCUIT", conn
, body
, 2, -1);
2167 zero_circ
= !strcmp("0", (char*)smartlist_get(args
,0));
2168 if (!zero_circ
&& !(circ
= get_circ(smartlist_get(args
,0)))) {
2169 connection_printf_to_buf(conn
, "552 Unknown circuit \"%s\"\r\n",
2170 (char*)smartlist_get(args
, 0));
2172 smartlist_split_string(router_nicknames
, smartlist_get(args
,1), ",", 0, 0);
2174 if (zero_circ
&& smartlist_len(args
)>2) {
2175 char *purp
= smartlist_get(args
,2);
2176 intended_purpose
= circuit_purpose_from_string(purp
);
2177 if (intended_purpose
== CIRCUIT_PURPOSE_UNKNOWN
) {
2178 connection_printf_to_buf(conn
, "552 Unknown purpose \"%s\"\r\n", purp
);
2179 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2180 smartlist_free(args
);
2184 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2185 smartlist_free(args
);
2186 if (!zero_circ
&& !circ
) {
2190 routers
= smartlist_create();
2191 SMARTLIST_FOREACH(router_nicknames
, const char *, n
,
2193 routerinfo_t
*r
= router_get_by_nickname(n
, 1);
2195 connection_printf_to_buf(conn
, "552 No such router \"%s\"\r\n", n
);
2198 smartlist_add(routers
, r
);
2200 if (!smartlist_len(routers
)) {
2201 connection_write_str_to_buf("512 No router names provided\r\n", conn
);
2206 /* start a new circuit */
2207 circ
= origin_circuit_init(intended_purpose
, 0);
2210 /* now circ refers to something that is ready to be extended */
2211 SMARTLIST_FOREACH(routers
, routerinfo_t
*, r
,
2213 extend_info_t
*info
= extend_info_from_router(r
);
2214 circuit_append_new_exit(circ
, info
);
2215 extend_info_free(info
);
2218 /* now that we've populated the cpath, start extending */
2221 if ((err_reason
= circuit_handle_first_hop(circ
)) < 0) {
2222 circuit_mark_for_close(TO_CIRCUIT(circ
), -err_reason
);
2223 connection_write_str_to_buf("551 Couldn't start circuit\r\n", conn
);
2227 if (circ
->_base
.state
== CIRCUIT_STATE_OPEN
) {
2229 circuit_set_state(TO_CIRCUIT(circ
), CIRCUIT_STATE_BUILDING
);
2230 if ((err_reason
= circuit_send_next_onion_skin(circ
)) < 0) {
2231 log_info(LD_CONTROL
,
2232 "send_next_onion_skin failed; circuit marked for closing.");
2233 circuit_mark_for_close(TO_CIRCUIT(circ
), -err_reason
);
2234 connection_write_str_to_buf("551 Couldn't send onion skin\r\n", conn
);
2240 connection_printf_to_buf(conn
, "250 EXTENDED %lu\r\n",
2241 (unsigned long)circ
->global_identifier
);
2242 if (zero_circ
) /* send a 'launched' event, for completeness */
2243 control_event_circuit_status(circ
, CIRC_EVENT_LAUNCHED
, 0);
2245 SMARTLIST_FOREACH(router_nicknames
, char *, n
, tor_free(n
));
2246 smartlist_free(router_nicknames
);
2248 smartlist_free(routers
);
2252 /** Called when we get a SETCIRCUITPURPOSE message. If we can find the
2253 * circuit and it's a valid purpose, change it. */
2255 handle_control_setcircuitpurpose(control_connection_t
*conn
,
2256 uint32_t len
, const char *body
)
2258 origin_circuit_t
*circ
= NULL
;
2259 uint8_t new_purpose
;
2261 (void) len
; /* body is NUL-terminated, so it's safe to ignore the length. */
2263 args
= getargs_helper("SETCIRCUITPURPOSE", conn
, body
, 2, -1);
2267 if (!(circ
= get_circ(smartlist_get(args
,0)))) {
2268 connection_printf_to_buf(conn
, "552 Unknown circuit \"%s\"\r\n",
2269 (char*)smartlist_get(args
, 0));
2274 char *purp
= smartlist_get(args
,1);
2275 new_purpose
= circuit_purpose_from_string(purp
);
2276 if (new_purpose
== CIRCUIT_PURPOSE_UNKNOWN
) {
2277 connection_printf_to_buf(conn
, "552 Unknown purpose \"%s\"\r\n", purp
);
2282 circ
->_base
.purpose
= new_purpose
;
2283 connection_write_str_to_buf("250 OK\r\n", conn
);
2287 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2288 smartlist_free(args
);
2293 /** Called when we get an ATTACHSTREAM message. Try to attach the requested
2294 * stream, and report success or failure. */
2296 handle_control_attachstream(control_connection_t
*conn
, uint32_t len
,
2299 edge_connection_t
*ap_conn
= NULL
;
2300 origin_circuit_t
*circ
= NULL
;
2303 crypt_path_t
*cpath
=NULL
;
2304 int hop
=0, hop_line_ok
=1;
2307 args
= getargs_helper("ATTACHSTREAM", conn
, body
, 2, -1);
2311 zero_circ
= !strcmp("0", (char*)smartlist_get(args
,1));
2313 if (!(ap_conn
= get_stream(smartlist_get(args
, 0)))) {
2314 connection_printf_to_buf(conn
, "552 Unknown stream \"%s\"\r\n",
2315 (char*)smartlist_get(args
, 0));
2316 } else if (!zero_circ
&& !(circ
= get_circ(smartlist_get(args
, 1)))) {
2317 connection_printf_to_buf(conn
, "552 Unknown circuit \"%s\"\r\n",
2318 (char*)smartlist_get(args
, 1));
2319 } else if (circ
&& smartlist_len(args
) > 2) {
2320 char *hopstring
= smartlist_get(args
, 2);
2321 if (!strcasecmpstart(hopstring
, "HOP=")) {
2322 hopstring
+= strlen("HOP=");
2323 hop
= (int) tor_parse_ulong(hopstring
, 10, 0, INT_MAX
,
2324 &hop_line_ok
, NULL
);
2325 if (!hop_line_ok
) { /* broken hop line */
2326 connection_printf_to_buf(conn
, "552 Bad value hop=%s\r\n", hopstring
);
2330 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2331 smartlist_free(args
);
2332 if (!ap_conn
|| (!zero_circ
&& !circ
) || !hop_line_ok
)
2335 if (ap_conn
->_base
.state
!= AP_CONN_STATE_CONTROLLER_WAIT
&&
2336 ap_conn
->_base
.state
!= AP_CONN_STATE_CONNECT_WAIT
&&
2337 ap_conn
->_base
.state
!= AP_CONN_STATE_RESOLVE_WAIT
) {
2338 connection_write_str_to_buf(
2339 "555 Connection is not managed by controller.\r\n",
2344 /* Do we need to detach it first? */
2345 if (ap_conn
->_base
.state
!= AP_CONN_STATE_CONTROLLER_WAIT
) {
2346 circuit_t
*tmpcirc
= circuit_get_by_edge_conn(ap_conn
);
2347 connection_edge_end(ap_conn
, END_STREAM_REASON_TIMEOUT
);
2348 /* Un-mark it as ending, since we're going to reuse it. */
2349 ap_conn
->edge_has_sent_end
= 0;
2350 ap_conn
->end_reason
= 0;
2352 circuit_detach_stream(tmpcirc
,ap_conn
);
2353 ap_conn
->_base
.state
= AP_CONN_STATE_CONTROLLER_WAIT
;
2356 if (circ
&& (circ
->_base
.state
!= CIRCUIT_STATE_OPEN
)) {
2357 connection_write_str_to_buf(
2358 "551 Can't attach stream to non-open origin circuit\r\n",
2362 /* Is this a single hop circuit? */
2363 if (circ
&& (circuit_get_cpath_len(circ
)<2 || hop
==1)) {
2364 routerinfo_t
*r
= NULL
;
2366 if (circ
->build_state
&&
2367 circ
->build_state
->chosen_exit
&&
2368 circ
->build_state
->chosen_exit
->identity_digest
) {
2369 exit_digest
= circ
->build_state
->chosen_exit
->identity_digest
;
2370 r
= router_get_by_digest(exit_digest
);
2372 /* Do both the client and relay allow one-hop exit circuits? */
2373 if (!r
|| !r
->allow_single_hop_exits
||
2374 !get_options()->AllowSingleHopCircuits
) {
2375 connection_write_str_to_buf(
2376 "551 Can't attach stream to this one-hop circuit.\r\n", conn
);
2379 ap_conn
->chosen_exit_name
= tor_strdup(hex_str(exit_digest
, DIGEST_LEN
));
2382 if (circ
&& hop
>0) {
2383 /* find this hop in the circuit, and set cpath */
2384 cpath
= circuit_get_cpath_hop(circ
, hop
);
2386 connection_printf_to_buf(conn
,
2387 "551 Circuit doesn't have %d hops.\r\n", hop
);
2391 if (connection_ap_handshake_rewrite_and_attach(ap_conn
, circ
, cpath
) < 0) {
2392 connection_write_str_to_buf("551 Unable to attach stream\r\n", conn
);
2395 send_control_done(conn
);
2399 /** Called when we get a POSTDESCRIPTOR message. Try to learn the provided
2400 * descriptor, and report success or failure. */
2402 handle_control_postdescriptor(control_connection_t
*conn
, uint32_t len
,
2406 const char *msg
=NULL
;
2407 uint8_t purpose
= ROUTER_PURPOSE_GENERAL
;
2408 int cache
= 0; /* eventually, we may switch this to 1 */
2410 char *cp
= memchr(body
, '\n', len
);
2411 smartlist_t
*args
= smartlist_create();
2415 smartlist_split_string(args
, body
, " ",
2416 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2417 SMARTLIST_FOREACH(args
, char *, option
,
2419 if (!strcasecmpstart(option
, "purpose=")) {
2420 option
+= strlen("purpose=");
2421 purpose
= router_purpose_from_string(option
);
2422 if (purpose
== ROUTER_PURPOSE_UNKNOWN
) {
2423 connection_printf_to_buf(conn
, "552 Unknown purpose \"%s\"\r\n",
2427 } else if (!strcasecmpstart(option
, "cache=")) {
2428 option
+= strlen("cache=");
2429 if (!strcmp(option
, "no"))
2431 else if (!strcmp(option
, "yes"))
2434 connection_printf_to_buf(conn
, "552 Unknown cache request \"%s\"\r\n",
2438 } else { /* unrecognized argument? */
2439 connection_printf_to_buf(conn
,
2440 "512 Unexpected argument \"%s\" to postdescriptor\r\n", option
);
2445 read_escaped_data(cp
, len
-(cp
-body
), &desc
);
2447 switch (router_load_single_router(desc
, purpose
, cache
, &msg
)) {
2449 if (!msg
) msg
= "Could not parse descriptor";
2450 connection_printf_to_buf(conn
, "554 %s\r\n", msg
);
2453 if (!msg
) msg
= "Descriptor not added";
2454 connection_printf_to_buf(conn
, "251 %s\r\n",msg
);
2457 send_control_done(conn
);
2463 SMARTLIST_FOREACH(args
, char *, arg
, tor_free(arg
));
2464 smartlist_free(args
);
2468 /** Called when we receive a REDIRECTSTERAM command. Try to change the target
2469 * address of the named AP stream, and report success or failure. */
2471 handle_control_redirectstream(control_connection_t
*conn
, uint32_t len
,
2474 edge_connection_t
*ap_conn
= NULL
;
2475 char *new_addr
= NULL
;
2476 uint16_t new_port
= 0;
2480 args
= getargs_helper("REDIRECTSTREAM", conn
, body
, 2, -1);
2484 if (!(ap_conn
= get_stream(smartlist_get(args
, 0)))
2485 || !ap_conn
->socks_request
) {
2486 connection_printf_to_buf(conn
, "552 Unknown stream \"%s\"\r\n",
2487 (char*)smartlist_get(args
, 0));
2490 if (smartlist_len(args
) > 2) { /* they included a port too */
2491 new_port
= (uint16_t) tor_parse_ulong(smartlist_get(args
, 2),
2492 10, 1, 65535, &ok
, NULL
);
2495 connection_printf_to_buf(conn
, "512 Cannot parse port \"%s\"\r\n",
2496 (char*)smartlist_get(args
, 2));
2498 new_addr
= tor_strdup(smartlist_get(args
, 1));
2502 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2503 smartlist_free(args
);
2507 strlcpy(ap_conn
->socks_request
->address
, new_addr
,
2508 sizeof(ap_conn
->socks_request
->address
));
2510 ap_conn
->socks_request
->port
= new_port
;
2512 send_control_done(conn
);
2516 /** Called when we get a CLOSESTREAM command; try to close the named stream
2517 * and report success or failure. */
2519 handle_control_closestream(control_connection_t
*conn
, uint32_t len
,
2522 edge_connection_t
*ap_conn
=NULL
;
2528 args
= getargs_helper("CLOSESTREAM", conn
, body
, 2, -1);
2532 else if (!(ap_conn
= get_stream(smartlist_get(args
, 0))))
2533 connection_printf_to_buf(conn
, "552 Unknown stream \"%s\"\r\n",
2534 (char*)smartlist_get(args
, 0));
2536 reason
= (uint8_t) tor_parse_ulong(smartlist_get(args
,1), 10, 0, 255,
2539 connection_printf_to_buf(conn
, "552 Unrecognized reason \"%s\"\r\n",
2540 (char*)smartlist_get(args
, 1));
2544 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2545 smartlist_free(args
);
2549 connection_mark_unattached_ap(ap_conn
, reason
);
2550 send_control_done(conn
);
2554 /** Called when we get a CLOSECIRCUIT command; try to close the named circuit
2555 * and report success or failure. */
2557 handle_control_closecircuit(control_connection_t
*conn
, uint32_t len
,
2560 origin_circuit_t
*circ
= NULL
;
2565 args
= getargs_helper("CLOSECIRCUIT", conn
, body
, 1, -1);
2569 if (!(circ
=get_circ(smartlist_get(args
, 0))))
2570 connection_printf_to_buf(conn
, "552 Unknown circuit \"%s\"\r\n",
2571 (char*)smartlist_get(args
, 0));
2574 for (i
=1; i
< smartlist_len(args
); ++i
) {
2575 if (!strcasecmp(smartlist_get(args
, i
), "IfUnused"))
2578 log_info(LD_CONTROL
, "Skipping unknown option %s",
2579 (char*)smartlist_get(args
,i
));
2582 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2583 smartlist_free(args
);
2587 if (!safe
|| !circ
->p_streams
) {
2588 circuit_mark_for_close(TO_CIRCUIT(circ
), END_CIRC_REASON_REQUESTED
);
2591 send_control_done(conn
);
2595 /** Called when we get a RESOLVE command: start trying to resolve
2596 * the listed addresses. */
2598 handle_control_resolve(control_connection_t
*conn
, uint32_t len
,
2601 smartlist_t
*args
, *failed
;
2603 (void) len
; /* body is nul-terminated; it's safe to ignore the length */
2605 if (!(conn
->event_mask
& ((uint32_t)1L<<EVENT_ADDRMAP
))) {
2606 log_warn(LD_CONTROL
, "Controller asked us to resolve an address, but "
2607 "isn't listening for ADDRMAP events. It probably won't see "
2610 args
= smartlist_create();
2611 smartlist_split_string(args
, body
, " ",
2612 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2613 if (smartlist_len(args
) &&
2614 !strcasecmp(smartlist_get(args
, 0), "mode=reverse")) {
2615 char *cp
= smartlist_get(args
, 0);
2616 smartlist_del_keeporder(args
, 0);
2620 failed
= smartlist_create();
2621 SMARTLIST_FOREACH(args
, const char *, arg
, {
2622 if (dnsserv_launch_request(arg
, is_reverse
)<0)
2623 smartlist_add(failed
, (char*)arg
);
2626 send_control_done(conn
);
2627 SMARTLIST_FOREACH(failed
, const char *, arg
, {
2628 control_event_address_mapped(arg
, arg
, time(NULL
),
2629 "Unable to launch resolve request");
2632 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2633 smartlist_free(args
);
2634 smartlist_free(failed
);
2638 /** Called when we get a PROTOCOLINFO command: send back a reply. */
2640 handle_control_protocolinfo(control_connection_t
*conn
, uint32_t len
,
2643 const char *bad_arg
= NULL
;
2647 conn
->have_sent_protocolinfo
= 1;
2648 args
= smartlist_create();
2649 smartlist_split_string(args
, body
, " ",
2650 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2651 SMARTLIST_FOREACH(args
, const char *, arg
, {
2653 tor_parse_long(arg
, 10, 0, LONG_MAX
, &ok
, NULL
);
2660 connection_printf_to_buf(conn
, "513 No such version %s\r\n",
2662 /* Don't tolerate bad arguments when not authenticated. */
2663 if (!STATE_IS_OPEN(TO_CONN(conn
)->state
))
2664 connection_mark_for_close(TO_CONN(conn
));
2667 or_options_t
*options
= get_options();
2668 int cookies
= options
->CookieAuthentication
;
2669 char *cfile
= get_cookie_file();
2670 char *esc_cfile
= esc_for_log(cfile
);
2673 int passwd
= (options
->HashedControlPassword
!= NULL
||
2674 options
->HashedControlSessionPassword
!= NULL
);
2675 smartlist_t
*mlist
= smartlist_create();
2677 smartlist_add(mlist
, (char*)"COOKIE");
2679 smartlist_add(mlist
, (char*)"HASHEDPASSWORD");
2680 if (!cookies
&& !passwd
)
2681 smartlist_add(mlist
, (char*)"NULL");
2682 methods
= smartlist_join_strings(mlist
, ",", 0, NULL
);
2683 smartlist_free(mlist
);
2686 connection_printf_to_buf(conn
,
2687 "250-PROTOCOLINFO 1\r\n"
2688 "250-AUTH METHODS=%s%s%s\r\n"
2689 "250-VERSION Tor=%s\r\n"
2692 cookies
?" COOKIEFILE=":"",
2693 cookies
?esc_cfile
:"",
2697 tor_free(esc_cfile
);
2700 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2701 smartlist_free(args
);
2705 /** Called when we get a USEFEATURE command: parse the feature list, and
2706 * set up the control_connection's options properly. */
2708 handle_control_usefeature(control_connection_t
*conn
,
2713 int verbose_names
= 0, extended_events
= 0;
2715 (void) len
; /* body is nul-terminated; it's safe to ignore the length */
2716 args
= smartlist_create();
2717 smartlist_split_string(args
, body
, " ",
2718 SPLIT_SKIP_SPACE
|SPLIT_IGNORE_BLANK
, 0);
2719 SMARTLIST_FOREACH(args
, const char *, arg
, {
2720 if (!strcasecmp(arg
, "VERBOSE_NAMES"))
2722 else if (!strcasecmp(arg
, "EXTENDED_EVENTS"))
2723 extended_events
= 1;
2725 connection_printf_to_buf(conn
, "552 Unrecognized feature \"%s\"\r\n",
2733 if (verbose_names
) {
2734 conn
->use_long_names
= 1;
2735 control_update_global_event_mask();
2737 if (extended_events
)
2738 conn
->use_extended_events
= 1;
2739 send_control_done(conn
);
2742 SMARTLIST_FOREACH(args
, char *, cp
, tor_free(cp
));
2743 smartlist_free(args
);
2747 /** Called when <b>conn</b> has no more bytes left on its outbuf. */
2749 connection_control_finished_flushing(control_connection_t
*conn
)
2753 connection_stop_writing(TO_CONN(conn
));
2757 /** Called when <b>conn</b> has gotten its socket closed. */
2759 connection_control_reached_eof(control_connection_t
*conn
)
2763 log_info(LD_CONTROL
,"Control connection reached EOF. Closing.");
2764 connection_mark_for_close(TO_CONN(conn
));
2768 /** Return true iff <b>cmd</b> is allowable (or at least forgivable) at this
2769 * stage of the protocol. */
2771 is_valid_initial_command(control_connection_t
*conn
, const char *cmd
)
2773 if (conn
->_base
.state
== CONTROL_CONN_STATE_OPEN
)
2775 if (!strcasecmp(cmd
, "PROTOCOLINFO"))
2776 return !conn
->have_sent_protocolinfo
;
2777 if (!strcasecmp(cmd
, "AUTHENTICATE") ||
2778 !strcasecmp(cmd
, "QUIT"))
2783 /** Do not accept any control command of more than 1MB in length. Anything
2784 * that needs to be anywhere near this long probably means that one of our
2785 * interfaces is broken. */
2786 #define MAX_COMMAND_LINE_LENGTH (1024*1024)
2788 /** Called when data has arrived on a v1 control connection: Try to fetch
2789 * commands from conn->inbuf, and execute them.
2792 connection_control_process_inbuf(control_connection_t
*conn
)
2795 uint32_t cmd_data_len
;
2800 tor_assert(conn
->_base
.state
== CONTROL_CONN_STATE_OPEN
||
2801 conn
->_base
.state
== CONTROL_CONN_STATE_NEEDAUTH
);
2803 if (!conn
->incoming_cmd
) {
2804 conn
->incoming_cmd
= tor_malloc(1024);
2805 conn
->incoming_cmd_len
= 1024;
2806 conn
->incoming_cmd_cur_len
= 0;
2809 if (conn
->_base
.state
== CONTROL_CONN_STATE_NEEDAUTH
&&
2810 peek_buf_has_control0_command(conn
->_base
.inbuf
)) {
2811 /* Detect v0 commands and send a "no more v0" message. */
2814 set_uint16(buf
+2, htons(0x0000)); /* type == error */
2815 set_uint16(buf
+4, htons(0x0001)); /* code == internal error */
2816 strlcpy(buf
+6, "The v0 control protocol is not supported by Tor 0.1.2.17 "
2817 "and later; upgrade your controller.",
2819 body_len
= 2+strlen(buf
+6)+2; /* code, msg, nul. */
2820 set_uint16(buf
+0, htons(body_len
));
2821 connection_write_to_buf(buf
, 4+body_len
, TO_CONN(conn
));
2822 connection_mark_for_close(TO_CONN(conn
));
2823 conn
->_base
.hold_open_until_flushed
= 1;
2831 /* First, fetch a line. */
2833 data_len
= conn
->incoming_cmd_len
- conn
->incoming_cmd_cur_len
;
2834 r
= fetch_from_buf_line(conn
->_base
.inbuf
,
2835 conn
->incoming_cmd
+conn
->incoming_cmd_cur_len
,
2838 /* Line not all here yet. Wait. */
2841 if (data_len
+ conn
->incoming_cmd_cur_len
> MAX_COMMAND_LINE_LENGTH
) {
2842 connection_write_str_to_buf("500 Line too long.\r\n", conn
);
2843 connection_stop_reading(TO_CONN(conn
));
2844 connection_mark_for_close(TO_CONN(conn
));
2845 conn
->_base
.hold_open_until_flushed
= 1;
2847 while (conn
->incoming_cmd_len
< data_len
+conn
->incoming_cmd_cur_len
)
2848 conn
->incoming_cmd_len
*= 2;
2849 conn
->incoming_cmd
= tor_realloc(conn
->incoming_cmd
,
2850 conn
->incoming_cmd_len
);
2854 tor_assert(data_len
);
2856 last_idx
= conn
->incoming_cmd_cur_len
;
2857 conn
->incoming_cmd_cur_len
+= (int)data_len
;
2859 /* We have appended a line to incoming_cmd. Is the command done? */
2860 if (last_idx
== 0 && *conn
->incoming_cmd
!= '+')
2861 /* One line command, didn't start with '+'. */
2863 /* XXXX this code duplication is kind of dumb. */
2864 if (last_idx
+3 == conn
->incoming_cmd_cur_len
&&
2865 !memcmp(conn
->incoming_cmd
+ last_idx
, ".\r\n", 3)) {
2866 /* Just appended ".\r\n"; we're done. Remove it. */
2867 conn
->incoming_cmd
[last_idx
] = '\0';
2868 conn
->incoming_cmd_cur_len
-= 3;
2870 } else if (last_idx
+2 == conn
->incoming_cmd_cur_len
&&
2871 !memcmp(conn
->incoming_cmd
+ last_idx
, ".\n", 2)) {
2872 /* Just appended ".\n"; we're done. Remove it. */
2873 conn
->incoming_cmd
[last_idx
] = '\0';
2874 conn
->incoming_cmd_cur_len
-= 2;
2877 /* Otherwise, read another line. */
2879 data_len
= conn
->incoming_cmd_cur_len
;
2880 /* Okay, we now have a command sitting on conn->incoming_cmd. See if we
2884 while ((size_t)cmd_len
< data_len
2885 && !TOR_ISSPACE(conn
->incoming_cmd
[cmd_len
]))
2888 data_len
-= cmd_len
;
2889 conn
->incoming_cmd
[cmd_len
]='\0';
2890 args
= conn
->incoming_cmd
+cmd_len
+1;
2891 while (*args
== ' ' || *args
== '\t') {
2896 /* If the connection is already closing, ignore further commands */
2897 if (TO_CONN(conn
)->marked_for_close
) {
2901 /* Otherwise, Quit is always valid. */
2902 if (!strcasecmp(conn
->incoming_cmd
, "QUIT")) {
2903 connection_write_str_to_buf("250 closing connection\r\n", conn
);
2904 connection_mark_for_close(TO_CONN(conn
));
2905 conn
->_base
.hold_open_until_flushed
= 1;
2909 if (conn
->_base
.state
== CONTROL_CONN_STATE_NEEDAUTH
&&
2910 !is_valid_initial_command(conn
, conn
->incoming_cmd
)) {
2911 connection_write_str_to_buf("514 Authentication required.\r\n", conn
);
2912 connection_mark_for_close(TO_CONN(conn
));
2916 if (data_len
>= UINT32_MAX
) {
2917 connection_write_str_to_buf("500 A 4GB command? Nice try.\r\n", conn
);
2918 connection_mark_for_close(TO_CONN(conn
));
2922 cmd_data_len
= (uint32_t)data_len
;
2923 if (!strcasecmp(conn
->incoming_cmd
, "SETCONF")) {
2924 if (handle_control_setconf(conn
, cmd_data_len
, args
))
2926 } else if (!strcasecmp(conn
->incoming_cmd
, "RESETCONF")) {
2927 if (handle_control_resetconf(conn
, cmd_data_len
, args
))
2929 } else if (!strcasecmp(conn
->incoming_cmd
, "GETCONF")) {
2930 if (handle_control_getconf(conn
, cmd_data_len
, args
))
2932 } else if (!strcasecmp(conn
->incoming_cmd
, "+LOADCONF")) {
2933 if (handle_control_loadconf(conn
, cmd_data_len
, args
))
2935 } else if (!strcasecmp(conn
->incoming_cmd
, "SETEVENTS")) {
2936 if (handle_control_setevents(conn
, cmd_data_len
, args
))
2938 } else if (!strcasecmp(conn
->incoming_cmd
, "AUTHENTICATE")) {
2939 if (handle_control_authenticate(conn
, cmd_data_len
, args
))
2941 } else if (!strcasecmp(conn
->incoming_cmd
, "SAVECONF")) {
2942 if (handle_control_saveconf(conn
, cmd_data_len
, args
))
2944 } else if (!strcasecmp(conn
->incoming_cmd
, "SIGNAL")) {
2945 if (handle_control_signal(conn
, cmd_data_len
, args
))
2947 } else if (!strcasecmp(conn
->incoming_cmd
, "MAPADDRESS")) {
2948 if (handle_control_mapaddress(conn
, cmd_data_len
, args
))
2950 } else if (!strcasecmp(conn
->incoming_cmd
, "GETINFO")) {
2951 if (handle_control_getinfo(conn
, cmd_data_len
, args
))
2953 } else if (!strcasecmp(conn
->incoming_cmd
, "EXTENDCIRCUIT")) {
2954 if (handle_control_extendcircuit(conn
, cmd_data_len
, args
))
2956 } else if (!strcasecmp(conn
->incoming_cmd
, "SETCIRCUITPURPOSE")) {
2957 if (handle_control_setcircuitpurpose(conn
, cmd_data_len
, args
))
2959 } else if (!strcasecmp(conn
->incoming_cmd
, "SETROUTERPURPOSE")) {
2960 connection_write_str_to_buf("511 SETROUTERPURPOSE is obsolete.\r\n", conn
);
2961 } else if (!strcasecmp(conn
->incoming_cmd
, "ATTACHSTREAM")) {
2962 if (handle_control_attachstream(conn
, cmd_data_len
, args
))
2964 } else if (!strcasecmp(conn
->incoming_cmd
, "+POSTDESCRIPTOR")) {
2965 if (handle_control_postdescriptor(conn
, cmd_data_len
, args
))
2967 } else if (!strcasecmp(conn
->incoming_cmd
, "REDIRECTSTREAM")) {
2968 if (handle_control_redirectstream(conn
, cmd_data_len
, args
))
2970 } else if (!strcasecmp(conn
->incoming_cmd
, "CLOSESTREAM")) {
2971 if (handle_control_closestream(conn
, cmd_data_len
, args
))
2973 } else if (!strcasecmp(conn
->incoming_cmd
, "CLOSECIRCUIT")) {
2974 if (handle_control_closecircuit(conn
, cmd_data_len
, args
))
2976 } else if (!strcasecmp(conn
->incoming_cmd
, "USEFEATURE")) {
2977 if (handle_control_usefeature(conn
, cmd_data_len
, args
))
2979 } else if (!strcasecmp(conn
->incoming_cmd
, "RESOLVE")) {
2980 if (handle_control_resolve(conn
, cmd_data_len
, args
))
2982 } else if (!strcasecmp(conn
->incoming_cmd
, "PROTOCOLINFO")) {
2983 if (handle_control_protocolinfo(conn
, cmd_data_len
, args
))
2986 connection_printf_to_buf(conn
, "510 Unrecognized command \"%s\"\r\n",
2987 conn
->incoming_cmd
);
2990 conn
->incoming_cmd_cur_len
= 0;
2994 /** Something has happened to circuit <b>circ</b>: tell any interested
2995 * control connections. */
2997 control_event_circuit_status(origin_circuit_t
*circ
, circuit_status_event_t tp
,
3001 char extended_buf
[96];
3002 int providing_reason
=0;
3003 if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS
))
3009 case CIRC_EVENT_LAUNCHED
: status
= "LAUNCHED"; break;
3010 case CIRC_EVENT_BUILT
: status
= "BUILT"; break;
3011 case CIRC_EVENT_EXTENDED
: status
= "EXTENDED"; break;
3012 case CIRC_EVENT_FAILED
: status
= "FAILED"; break;
3013 case CIRC_EVENT_CLOSED
: status
= "CLOSED"; break;
3015 log_warn(LD_BUG
, "Unrecognized status code %d", (int)tp
);
3019 tor_snprintf(extended_buf
, sizeof(extended_buf
), "PURPOSE=%s",
3020 circuit_purpose_to_controller_string(circ
->_base
.purpose
));
3022 if (tp
== CIRC_EVENT_FAILED
|| tp
== CIRC_EVENT_CLOSED
) {
3023 const char *reason_str
= circuit_end_reason_to_control_string(reason_code
);
3024 char *reason
= NULL
;
3025 size_t n
=strlen(extended_buf
);
3028 reason
= tor_malloc(16);
3029 tor_snprintf(reason
, 16, "UNKNOWN_%d", reason_code
);
3030 reason_str
= reason
;
3032 if (reason_code
> 0 && reason_code
& END_CIRC_REASON_FLAG_REMOTE
) {
3033 tor_snprintf(extended_buf
+n
, sizeof(extended_buf
)-n
,
3034 " REASON=DESTROYED REMOTE_REASON=%s", reason_str
);
3036 tor_snprintf(extended_buf
+n
, sizeof(extended_buf
)-n
,
3037 " REASON=%s", reason_str
);
3042 if (EVENT_IS_INTERESTING1S(EVENT_CIRCUIT_STATUS
)) {
3043 char *path
= circuit_list_path(circ
,0);
3044 const char *sp
= strlen(path
) ? " " : "";
3045 send_control_event_extended(EVENT_CIRCUIT_STATUS
, SHORT_NAMES
,
3046 "650 CIRC %lu %s%s%s@%s\r\n",
3047 (unsigned long)circ
->global_identifier
,
3048 status
, sp
, path
, extended_buf
);
3051 if (EVENT_IS_INTERESTING1L(EVENT_CIRCUIT_STATUS
)) {
3052 char *vpath
= circuit_list_path_for_controller(circ
);
3053 const char *sp
= strlen(vpath
) ? " " : "";
3054 send_control_event_extended(EVENT_CIRCUIT_STATUS
, LONG_NAMES
,
3055 "650 CIRC %lu %s%s%s@%s\r\n",
3056 (unsigned long)circ
->global_identifier
,
3057 status
, sp
, vpath
, extended_buf
);
3064 /** Given an AP connection <b>conn</b> and a <b>len</b>-character buffer
3065 * <b>buf</b>, determine the address:port combination requested on
3066 * <b>conn</b>, and write it to <b>buf</b>. Return 0 on success, -1 on
3069 write_stream_target_to_buf(edge_connection_t
*conn
, char *buf
, size_t len
)
3072 if (conn
->chosen_exit_name
)
3073 if (tor_snprintf(buf2
, sizeof(buf2
), ".%s.exit", conn
->chosen_exit_name
)<0)
3075 if (!conn
->socks_request
)
3077 if (tor_snprintf(buf
, len
, "%s%s%s:%d",
3078 conn
->socks_request
->address
,
3079 conn
->chosen_exit_name
? buf2
: "",
3080 !conn
->chosen_exit_name
&&
3081 connection_edge_is_rendezvous_stream(conn
) ? ".onion" : "",
3082 conn
->socks_request
->port
)<0)
3087 /** Something has happened to the stream associated with AP connection
3088 * <b>conn</b>: tell any interested control connections. */
3090 control_event_stream_status(edge_connection_t
*conn
, stream_status_event_t tp
,
3093 char reason_buf
[64];
3094 char addrport_buf
[64];
3097 origin_circuit_t
*origin_circ
= NULL
;
3099 const char *purpose
= "";
3100 tor_assert(conn
->socks_request
);
3102 if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS
))
3105 if (tp
== STREAM_EVENT_CLOSED
&&
3106 (reason_code
& END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
))
3109 write_stream_target_to_buf(conn
, buf
, sizeof(buf
));
3111 reason_buf
[0] = '\0';
3114 case STREAM_EVENT_SENT_CONNECT
: status
= "SENTCONNECT"; break;
3115 case STREAM_EVENT_SENT_RESOLVE
: status
= "SENTRESOLVE"; break;
3116 case STREAM_EVENT_SUCCEEDED
: status
= "SUCCEEDED"; break;
3117 case STREAM_EVENT_FAILED
: status
= "FAILED"; break;
3118 case STREAM_EVENT_CLOSED
: status
= "CLOSED"; break;
3119 case STREAM_EVENT_NEW
: status
= "NEW"; break;
3120 case STREAM_EVENT_NEW_RESOLVE
: status
= "NEWRESOLVE"; break;
3121 case STREAM_EVENT_FAILED_RETRIABLE
: status
= "DETACHED"; break;
3122 case STREAM_EVENT_REMAP
: status
= "REMAP"; break;
3124 log_warn(LD_BUG
, "Unrecognized status code %d", (int)tp
);
3127 if (reason_code
&& (tp
== STREAM_EVENT_FAILED
||
3128 tp
== STREAM_EVENT_CLOSED
||
3129 tp
== STREAM_EVENT_FAILED_RETRIABLE
)) {
3130 const char *reason_str
= stream_end_reason_to_control_string(reason_code
);
3134 tor_snprintf(r
, 16, "UNKNOWN_%d", reason_code
);
3137 if (reason_code
& END_STREAM_REASON_FLAG_REMOTE
)
3138 tor_snprintf(reason_buf
, sizeof(reason_buf
),
3139 "REASON=END REMOTE_REASON=%s", reason_str
);
3141 tor_snprintf(reason_buf
, sizeof(reason_buf
),
3142 "REASON=%s", reason_str
);
3144 } else if (reason_code
&& tp
== STREAM_EVENT_REMAP
) {
3145 switch (reason_code
) {
3146 case REMAP_STREAM_SOURCE_CACHE
:
3147 strlcpy(reason_buf
, "SOURCE=CACHE", sizeof(reason_buf
));
3149 case REMAP_STREAM_SOURCE_EXIT
:
3150 strlcpy(reason_buf
, "SOURCE=EXIT", sizeof(reason_buf
));
3153 tor_snprintf(reason_buf
, sizeof(reason_buf
), "REASON=UNKNOWN_%d",
3155 /* XXX do we want SOURCE=UNKNOWN_%d above instead? -RD */
3160 if (tp
== STREAM_EVENT_NEW
) {
3161 tor_snprintf(addrport_buf
,sizeof(addrport_buf
), "%sSOURCE_ADDR=%s:%d",
3162 strlen(reason_buf
) ? " " : "",
3163 TO_CONN(conn
)->address
, TO_CONN(conn
)->port
);
3165 addrport_buf
[0] = '\0';
3168 if (tp
== STREAM_EVENT_NEW_RESOLVE
) {
3169 purpose
= " PURPOSE=DNS_REQUEST";
3170 } else if (tp
== STREAM_EVENT_NEW
) {
3171 if (conn
->is_dns_request
||
3172 (conn
->socks_request
&&
3173 SOCKS_COMMAND_IS_RESOLVE(conn
->socks_request
->command
)))
3174 purpose
= " PURPOSE=DNS_REQUEST";
3175 else if (conn
->use_begindir
) {
3176 connection_t
*linked
= TO_CONN(conn
)->linked_conn
;
3177 int linked_dir_purpose
= -1;
3178 if (linked
&& linked
->type
== CONN_TYPE_DIR
)
3179 linked_dir_purpose
= linked
->purpose
;
3180 if (DIR_PURPOSE_IS_UPLOAD(linked_dir_purpose
))
3181 purpose
= " PURPOSE=DIR_UPLOAD";
3183 purpose
= " PURPOSE=DIR_FETCH";
3185 purpose
= " PURPOSE=USER";
3188 circ
= circuit_get_by_edge_conn(conn
);
3189 if (circ
&& CIRCUIT_IS_ORIGIN(circ
))
3190 origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
3191 send_control_event_extended(EVENT_STREAM_STATUS
, ALL_NAMES
,
3192 "650 STREAM "U64_FORMAT
" %s %lu %s@%s%s%s\r\n",
3193 U64_PRINTF_ARG(conn
->_base
.global_identifier
), status
,
3195 (unsigned long)origin_circ
->global_identifier
: 0ul,
3196 buf
, reason_buf
, addrport_buf
, purpose
);
3198 /* XXX need to specify its intended exit, etc? */
3203 /** Figure out the best name for the target router of an OR connection
3204 * <b>conn</b>, and write it into the <b>len</b>-character buffer
3205 * <b>name</b>. Use verbose names if <b>long_names</b> is set. */
3207 orconn_target_get_name(int long_names
,
3208 char *name
, size_t len
, or_connection_t
*conn
)
3212 strlcpy(name
, conn
->nickname
, len
);
3214 tor_snprintf(name
, len
, "%s:%d",
3215 conn
->_base
.address
, conn
->_base
.port
);
3217 routerinfo_t
*ri
= router_get_by_digest(conn
->identity_digest
);
3219 tor_assert(len
> MAX_VERBOSE_NICKNAME_LEN
);
3220 router_get_verbose_nickname(name
, ri
);
3221 } else if (! tor_digest_is_zero(conn
->identity_digest
)) {
3223 base16_encode(name
+1, len
-1, conn
->identity_digest
,
3226 tor_snprintf(name
, len
, "%s:%d",
3227 conn
->_base
.address
, conn
->_base
.port
);
3232 /** Called when the status of an OR connection <b>conn</b> changes: tell any
3233 * interested control connections. <b>tp</b> is the new status for the
3234 * connection. If <b>conn</b> has just closed or failed, then <b>reason</b>
3235 * may be the reason why.
3238 control_event_or_conn_status(or_connection_t
*conn
, or_conn_status_event_t tp
,
3244 char ncircs_buf
[32] = {0}; /* > 8 + log10(2^32)=10 + 2 */
3246 if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS
))
3251 case OR_CONN_EVENT_LAUNCHED
: status
= "LAUNCHED"; break;
3252 case OR_CONN_EVENT_CONNECTED
: status
= "CONNECTED"; break;
3253 case OR_CONN_EVENT_FAILED
: status
= "FAILED"; break;
3254 case OR_CONN_EVENT_CLOSED
: status
= "CLOSED"; break;
3255 case OR_CONN_EVENT_NEW
: status
= "NEW"; break;
3257 log_warn(LD_BUG
, "Unrecognized status code %d", (int)tp
);
3260 ncircs
= circuit_count_pending_on_or_conn(conn
);
3261 ncircs
+= conn
->n_circuits
;
3262 if (ncircs
&& (tp
== OR_CONN_EVENT_FAILED
|| tp
== OR_CONN_EVENT_CLOSED
)) {
3263 tor_snprintf(ncircs_buf
, sizeof(ncircs_buf
), "%sNCIRCS=%d",
3264 reason
? " " : "", ncircs
);
3267 if (EVENT_IS_INTERESTING1S(EVENT_OR_CONN_STATUS
)) {
3268 orconn_target_get_name(0, name
, sizeof(name
), conn
);
3269 send_control_event_extended(EVENT_OR_CONN_STATUS
, SHORT_NAMES
,
3270 "650 ORCONN %s %s@%s%s%s\r\n",
3272 reason
? "REASON=" : "",
3273 orconn_end_reason_to_control_string(reason
),
3276 if (EVENT_IS_INTERESTING1L(EVENT_OR_CONN_STATUS
)) {
3277 orconn_target_get_name(1, name
, sizeof(name
), conn
);
3278 send_control_event_extended(EVENT_OR_CONN_STATUS
, LONG_NAMES
,
3279 "650 ORCONN %s %s@%s%s%s\r\n",
3281 reason
? "REASON=" : "",
3282 orconn_end_reason_to_control_string(reason
),
3290 * Print out STREAM_BW event for a single conn
3293 control_event_stream_bandwidth(edge_connection_t
*edge_conn
)
3295 if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED
)) {
3296 if (!edge_conn
->n_read
&& !edge_conn
->n_written
)
3299 send_control_event(EVENT_STREAM_BANDWIDTH_USED
, ALL_NAMES
,
3300 "650 STREAM_BW "U64_FORMAT
" %lu %lu\r\n",
3301 U64_PRINTF_ARG(edge_conn
->_base
.global_identifier
),
3302 (unsigned long)edge_conn
->n_read
,
3303 (unsigned long)edge_conn
->n_written
);
3305 edge_conn
->n_written
= edge_conn
->n_read
= 0;
3311 /** A second or more has elapsed: tell any interested control
3312 * connections how much bandwidth streams have used. */
3314 control_event_stream_bandwidth_used(void)
3316 if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED
)) {
3317 smartlist_t
*conns
= get_connection_array();
3318 edge_connection_t
*edge_conn
;
3320 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
)
3322 if (conn
->type
!= CONN_TYPE_AP
)
3324 edge_conn
= TO_EDGE_CONN(conn
);
3325 if (!edge_conn
->n_read
&& !edge_conn
->n_written
)
3328 send_control_event(EVENT_STREAM_BANDWIDTH_USED
, ALL_NAMES
,
3329 "650 STREAM_BW "U64_FORMAT
" %lu %lu\r\n",
3330 U64_PRINTF_ARG(edge_conn
->_base
.global_identifier
),
3331 (unsigned long)edge_conn
->n_read
,
3332 (unsigned long)edge_conn
->n_written
);
3334 edge_conn
->n_written
= edge_conn
->n_read
= 0;
3336 SMARTLIST_FOREACH_END(conn
);
3342 /** A second or more has elapsed: tell any interested control
3343 * connections how much bandwidth we used. */
3345 control_event_bandwidth_used(uint32_t n_read
, uint32_t n_written
)
3347 if (EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED
)) {
3348 send_control_event(EVENT_BANDWIDTH_USED
, ALL_NAMES
,
3349 "650 BW %lu %lu\r\n",
3350 (unsigned long)n_read
,
3351 (unsigned long)n_written
);
3357 /** Called when we are sending a log message to the controllers: suspend
3358 * sending further log messages to the controllers until we're done. Used by
3359 * CONN_LOG_PROTECT. */
3361 disable_control_logging(void)
3363 ++disable_log_messages
;
3366 /** We're done sending a log message to the controllers: re-enable controller
3367 * logging. Used by CONN_LOG_PROTECT. */
3369 enable_control_logging(void)
3371 if (--disable_log_messages
< 0)
3375 /** We got a log message: tell any interested control connections. */
3377 control_event_logmsg(int severity
, uint32_t domain
, const char *msg
)
3381 /* Don't even think of trying to add stuff to a buffer from a cpuworker
3383 if (! in_main_thread())
3386 if (disable_log_messages
)
3389 if (domain
== LD_BUG
&& EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL
) &&
3390 severity
<= LOG_NOTICE
) {
3391 char *esc
= esc_for_log(msg
);
3392 ++disable_log_messages
;
3393 control_event_general_status(severity
, "BUG REASON=\"%s\"", esc
);
3394 --disable_log_messages
;
3398 event
= log_severity_to_event(severity
);
3399 if (event
>= 0 && EVENT_IS_INTERESTING(event
)) {
3402 if (strchr(msg
, '\n')) {
3404 b
= tor_strdup(msg
);
3405 for (cp
= b
; *cp
; ++cp
)
3406 if (*cp
== '\r' || *cp
== '\n')
3410 case LOG_DEBUG
: s
= "DEBUG"; break;
3411 case LOG_INFO
: s
= "INFO"; break;
3412 case LOG_NOTICE
: s
= "NOTICE"; break;
3413 case LOG_WARN
: s
= "WARN"; break;
3414 case LOG_ERR
: s
= "ERR"; break;
3415 default: s
= "UnknownLogSeverity"; break;
3417 ++disable_log_messages
;
3418 send_control_event(event
, ALL_NAMES
, "650 %s %s\r\n", s
, b
?b
:msg
);
3419 --disable_log_messages
;
3424 /** Called whenever we receive new router descriptors: tell any
3425 * interested control connections. <b>routers</b> is a list of
3429 control_event_descriptors_changed(smartlist_t
*routers
)
3433 smartlist_t
*identities
= NULL
;
3434 char buf
[HEX_DIGEST_LEN
+1];
3436 if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC
))
3438 if (EVENT_IS_INTERESTING1S(EVENT_NEW_DESC
)) {
3439 identities
= smartlist_create();
3440 SMARTLIST_FOREACH(routers
, routerinfo_t
*, r
,
3442 base16_encode(buf
,sizeof(buf
),r
->cache_info
.identity_digest
,DIGEST_LEN
);
3443 smartlist_add(identities
, tor_strdup(buf
));
3446 if (EVENT_IS_INTERESTING1S(EVENT_NEW_DESC
)) {
3447 char *ids
= smartlist_join_strings(identities
, " ", 0, &len
);
3448 size_t ids_len
= strlen(ids
)+32;
3449 msg
= tor_malloc(ids_len
);
3450 tor_snprintf(msg
, ids_len
, "650 NEWDESC %s\r\n", ids
);
3451 send_control_event_string(EVENT_NEW_DESC
, SHORT_NAMES
|ALL_FORMATS
, msg
);
3455 if (EVENT_IS_INTERESTING1L(EVENT_NEW_DESC
)) {
3456 smartlist_t
*names
= smartlist_create();
3459 SMARTLIST_FOREACH(routers
, routerinfo_t
*, ri
, {
3460 char *b
= tor_malloc(MAX_VERBOSE_NICKNAME_LEN
+1);
3461 router_get_verbose_nickname(b
, ri
);
3462 smartlist_add(names
, b
);
3464 ids
= smartlist_join_strings(names
, " ", 0, &names_len
);
3465 names_len
= strlen(ids
)+32;
3466 msg
= tor_malloc(names_len
);
3467 tor_snprintf(msg
, names_len
, "650 NEWDESC %s\r\n", ids
);
3468 send_control_event_string(EVENT_NEW_DESC
, LONG_NAMES
|ALL_FORMATS
, msg
);
3471 SMARTLIST_FOREACH(names
, char *, cp
, tor_free(cp
));
3472 smartlist_free(names
);
3475 SMARTLIST_FOREACH(identities
, char *, cp
, tor_free(cp
));
3476 smartlist_free(identities
);
3481 /** Called when an address mapping on <b>from</b> from changes to <b>to</b>.
3482 * <b>expires</b> values less than 3 are special; see connection_edge.c. If
3483 * <b>error</b> is non-NULL, it is an error code describing the failure
3484 * mode of the mapping.
3487 control_event_address_mapped(const char *from
, const char *to
, time_t expires
,
3490 if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP
))
3493 if (expires
< 3 || expires
== TIME_MAX
)
3494 send_control_event_extended(EVENT_ADDRMAP
, ALL_NAMES
,
3495 "650 ADDRMAP %s %s NEVER@%s\r\n", from
, to
,
3498 char buf
[ISO_TIME_LEN
+1];
3499 char buf2
[ISO_TIME_LEN
+1];
3500 format_local_iso_time(buf
,expires
);
3501 format_iso_time(buf2
,expires
);
3502 send_control_event_extended(EVENT_ADDRMAP
, ALL_NAMES
,
3503 "650 ADDRMAP %s %s \"%s\""
3504 "@%s%sEXPIRES=\"%s\"\r\n",
3506 error
?error
:"", error
?" ":"",
3513 /** The authoritative dirserver has received a new descriptor that
3514 * has passed basic syntax checks and is properly self-signed.
3516 * Notify any interested party of the new descriptor and what has
3517 * been done with it, and also optionally give an explanation/reason. */
3519 control_event_or_authdir_new_descriptor(const char *action
,
3520 const char *desc
, size_t desclen
,
3523 char firstline
[1024];
3529 if (!EVENT_IS_INTERESTING(EVENT_AUTHDIR_NEWDESCS
))
3532 tor_snprintf(firstline
, sizeof(firstline
),
3533 "650+AUTHDIR_NEWDESC=\r\n%s\r\n%s\r\n",
3537 /* Escape the server descriptor properly */
3538 esclen
= write_escaped_data(desc
, desclen
, &esc
);
3540 totallen
= strlen(firstline
) + esclen
+ 1;
3541 buf
= tor_malloc(totallen
);
3542 strlcpy(buf
, firstline
, totallen
);
3543 strlcpy(buf
+strlen(firstline
), esc
, totallen
);
3544 send_control_event_string(EVENT_AUTHDIR_NEWDESCS
, ALL_NAMES
|ALL_FORMATS
,
3546 send_control_event_string(EVENT_AUTHDIR_NEWDESCS
, ALL_NAMES
|ALL_FORMATS
,
3554 /** Helper function for NS-style events. Constructs and sends an event
3555 * of type <b>event</b> with string <b>event_string</b> out of the set of
3556 * networkstatuses <b>statuses</b>. Currently it is used for NS events
3557 * and NEWCONSENSUS events. */
3559 control_event_networkstatus_changed_helper(smartlist_t
*statuses
,
3561 const char *event_string
)
3564 char *s
, *esc
= NULL
;
3565 if (!EVENT_IS_INTERESTING(event
) || !smartlist_len(statuses
))
3568 strs
= smartlist_create();
3569 smartlist_add(strs
, tor_strdup("650+"));
3570 smartlist_add(strs
, tor_strdup(event_string
));
3571 smartlist_add(strs
, tor_strdup("\r\n"));
3572 SMARTLIST_FOREACH(statuses
, routerstatus_t
*, rs
,
3574 s
= networkstatus_getinfo_helper_single(rs
);
3576 smartlist_add(strs
, s
);
3579 s
= smartlist_join_strings(strs
, "", 0, NULL
);
3580 write_escaped_data(s
, strlen(s
), &esc
);
3581 SMARTLIST_FOREACH(strs
, char *, cp
, tor_free(cp
));
3582 smartlist_free(strs
);
3584 send_control_event_string(event
, ALL_NAMES
|ALL_FORMATS
, esc
);
3585 send_control_event_string(event
, ALL_NAMES
|ALL_FORMATS
,
3592 /** Called when the routerstatus_ts <b>statuses</b> have changed: sends
3593 * an NS event to any controller that cares. */
3595 control_event_networkstatus_changed(smartlist_t
*statuses
)
3597 return control_event_networkstatus_changed_helper(statuses
, EVENT_NS
, "NS");
3600 /** Called when we get a new consensus networkstatus. Sends a NEWCONSENSUS
3601 * event consisting of an NS-style line for each relay in the consensus. */
3603 control_event_newconsensus(const networkstatus_t
*consensus
)
3605 if (!control_event_is_interesting(EVENT_NEWCONSENSUS
))
3607 return control_event_networkstatus_changed_helper(
3608 consensus
->routerstatus_list
, EVENT_NEWCONSENSUS
, "NEWCONSENSUS");
3611 /** Called when a single local_routerstatus_t has changed: Sends an NS event
3612 * to any controller that cares. */
3614 control_event_networkstatus_changed_single(routerstatus_t
*rs
)
3616 smartlist_t
*statuses
;
3619 if (!EVENT_IS_INTERESTING(EVENT_NS
))
3622 statuses
= smartlist_create();
3623 smartlist_add(statuses
, rs
);
3624 r
= control_event_networkstatus_changed(statuses
);
3625 smartlist_free(statuses
);
3629 /** Our own router descriptor has changed; tell any controllers that care.
3632 control_event_my_descriptor_changed(void)
3634 send_control_event(EVENT_DESCCHANGED
, ALL_NAMES
, "650 DESCCHANGED\r\n");
3638 /** Helper: sends a status event where <b>type</b> is one of
3639 * EVENT_STATUS_{GENERAL,CLIENT,SERVER}, where <b>severity</b> is one of
3640 * LOG_{NOTICE,WARN,ERR}, and where <b>format</b> is a printf-style format
3641 * string corresponding to <b>args</b>. */
3643 control_event_status(int type
, int severity
, const char *format
, va_list args
)
3645 char format_buf
[160];
3646 const char *status
, *sev
;
3649 case EVENT_STATUS_GENERAL
:
3650 status
= "STATUS_GENERAL";
3652 case EVENT_STATUS_CLIENT
:
3653 status
= "STATUS_CLIENT";
3655 case EVENT_STATUS_SERVER
:
3656 status
= "STATUS_SERVER";
3659 log_warn(LD_BUG
, "Unrecognized status type %d", type
);
3673 log_warn(LD_BUG
, "Unrecognized status severity %d", severity
);
3676 if (tor_snprintf(format_buf
, sizeof(format_buf
), "650 %s %s %s\r\n",
3677 status
, sev
, format
)<0) {
3678 log_warn(LD_BUG
, "Format string too long.");
3682 send_control_event_impl(type
, ALL_NAMES
|ALL_FORMATS
, 0, format_buf
, args
);
3686 /** Format and send an EVENT_STATUS_GENERAL event whose main text is obtained
3687 * by formatting the arguments using the printf-style <b>format</b>. */
3689 control_event_general_status(int severity
, const char *format
, ...)
3693 if (!EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL
))
3696 va_start(ap
, format
);
3697 r
= control_event_status(EVENT_STATUS_GENERAL
, severity
, format
, ap
);
3702 /** Format and send an EVENT_STATUS_CLIENT event whose main text is obtained
3703 * by formatting the arguments using the printf-style <b>format</b>. */
3705 control_event_client_status(int severity
, const char *format
, ...)
3709 if (!EVENT_IS_INTERESTING(EVENT_STATUS_CLIENT
))
3712 va_start(ap
, format
);
3713 r
= control_event_status(EVENT_STATUS_CLIENT
, severity
, format
, ap
);
3718 /** Format and send an EVENT_STATUS_SERVER event whose main text is obtained
3719 * by formatting the arguments using the printf-style <b>format</b>. */
3721 control_event_server_status(int severity
, const char *format
, ...)
3725 if (!EVENT_IS_INTERESTING(EVENT_STATUS_SERVER
))
3728 va_start(ap
, format
);
3729 r
= control_event_status(EVENT_STATUS_SERVER
, severity
, format
, ap
);
3734 /** Called when the status of an entry guard with the given <b>nickname</b>
3735 * and identity <b>digest</b> has changed to <b>status</b>: tells any
3736 * controllers that care. */
3738 control_event_guard(const char *nickname
, const char *digest
,
3741 char hbuf
[HEX_DIGEST_LEN
+1];
3742 base16_encode(hbuf
, sizeof(hbuf
), digest
, DIGEST_LEN
);
3743 if (!EVENT_IS_INTERESTING(EVENT_GUARD
))
3746 if (EVENT_IS_INTERESTING1L(EVENT_GUARD
)) {
3747 char buf
[MAX_VERBOSE_NICKNAME_LEN
+1];
3748 routerinfo_t
*ri
= router_get_by_digest(digest
);
3750 router_get_verbose_nickname(buf
, ri
);
3752 tor_snprintf(buf
, sizeof(buf
), "$%s~%s", hbuf
, nickname
);
3754 send_control_event(EVENT_GUARD
, LONG_NAMES
,
3755 "650 GUARD ENTRY %s %s\r\n", buf
, status
);
3757 if (EVENT_IS_INTERESTING1S(EVENT_GUARD
)) {
3758 send_control_event(EVENT_GUARD
, SHORT_NAMES
,
3759 "650 GUARD ENTRY $%s %s\r\n", hbuf
, status
);
3764 /** Helper: Return a newly allocated string containing a path to the
3765 * file where we store our authentication cookie. */
3767 get_cookie_file(void)
3769 or_options_t
*options
= get_options();
3770 if (options
->CookieAuthFile
&& strlen(options
->CookieAuthFile
)) {
3771 return tor_strdup(options
->CookieAuthFile
);
3773 return get_datadir_fname("control_auth_cookie");
3777 /** Choose a random authentication cookie and write it to disk.
3778 * Anybody who can read the cookie from disk will be considered
3779 * authorized to use the control connection. Return -1 if we can't
3780 * write the file, or 0 on success. */
3782 init_cookie_authentication(int enabled
)
3786 authentication_cookie_is_set
= 0;
3790 /* We don't want to generate a new cookie every time we call
3791 * options_act(). One should be enough. */
3792 if (authentication_cookie_is_set
)
3793 return 0; /* all set */
3795 fname
= get_cookie_file();
3796 crypto_rand(authentication_cookie
, AUTHENTICATION_COOKIE_LEN
);
3797 authentication_cookie_is_set
= 1;
3798 if (write_bytes_to_file(fname
, authentication_cookie
,
3799 AUTHENTICATION_COOKIE_LEN
, 1)) {
3800 log_warn(LD_FS
,"Error writing authentication cookie to %s.",
3806 if (get_options()->CookieAuthFileGroupReadable
) {
3807 if (chmod(fname
, 0640)) {
3808 log_warn(LD_FS
,"Unable to make %s group-readable.", escaped(fname
));
3817 /** Convert the name of a bootstrapping phase <b>s</b> into strings
3818 * <b>tag</b> and <b>summary</b> suitable for display by the controller. */
3820 bootstrap_status_to_string(bootstrap_status_t s
, const char **tag
,
3821 const char **summary
)
3824 case BOOTSTRAP_STATUS_UNDEF
:
3826 *summary
= "Undefined";
3828 case BOOTSTRAP_STATUS_STARTING
:
3830 *summary
= "Starting";
3832 case BOOTSTRAP_STATUS_CONN_DIR
:
3834 *summary
= "Connecting to directory server";
3836 case BOOTSTRAP_STATUS_HANDSHAKE
:
3837 *tag
= "status_handshake";
3838 *summary
= "Finishing handshake";
3840 case BOOTSTRAP_STATUS_HANDSHAKE_DIR
:
3841 *tag
= "handshake_dir";
3842 *summary
= "Finishing handshake with directory server";
3844 case BOOTSTRAP_STATUS_ONEHOP_CREATE
:
3845 *tag
= "onehop_create";
3846 *summary
= "Establishing an encrypted directory connection";
3848 case BOOTSTRAP_STATUS_REQUESTING_STATUS
:
3849 *tag
= "requesting_status";
3850 *summary
= "Asking for networkstatus consensus";
3852 case BOOTSTRAP_STATUS_LOADING_STATUS
:
3853 *tag
= "loading_status";
3854 *summary
= "Loading networkstatus consensus";
3856 case BOOTSTRAP_STATUS_LOADING_KEYS
:
3857 *tag
= "loading_keys";
3858 *summary
= "Loading authority key certs";
3860 case BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS
:
3861 *tag
= "requesting_descriptors";
3862 *summary
= "Asking for relay descriptors";
3864 case BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
:
3865 *tag
= "loading_descriptors";
3866 *summary
= "Loading relay descriptors";
3868 case BOOTSTRAP_STATUS_CONN_OR
:
3870 *summary
= "Connecting to the Tor network";
3872 case BOOTSTRAP_STATUS_HANDSHAKE_OR
:
3873 *tag
= "handshake_or";
3874 *summary
= "Finishing handshake with first hop";
3876 case BOOTSTRAP_STATUS_CIRCUIT_CREATE
:
3877 *tag
= "circuit_create";
3878 *summary
= "Establishing a Tor circuit";
3880 case BOOTSTRAP_STATUS_DONE
:
3885 // log_warn(LD_BUG, "Unrecognized bootstrap status code %d", s);
3886 *tag
= *summary
= "unknown";
3892 /** What percentage through the bootstrap process are we? We remember
3893 * this so we can avoid sending redundant bootstrap status events, and
3894 * so we can guess context for the bootstrap messages which are
3895 * ambiguous. It starts at 'undef', but gets set to 'starting' while
3896 * Tor initializes. */
3897 static int bootstrap_percent
= BOOTSTRAP_STATUS_UNDEF
;
3899 /** How many problems have we had getting to the next bootstrapping phase?
3900 * These include failure to establish a connection to a Tor relay,
3901 * failures to finish the TLS handshake, failures to validate the
3902 * consensus document, etc. */
3903 static int bootstrap_problems
= 0;
3905 /* We only tell the controller once we've hit a threshold of problems
3906 * for the current phase. */
3907 #define BOOTSTRAP_PROBLEM_THRESHOLD 10
3909 /** Called when Tor has made progress at bootstrapping its directory
3910 * information and initial circuits.
3912 * <b>status</b> is the new status, that is, what task we will be doing
3913 * next. <b>percent</b> is zero if we just started this task, else it
3914 * represents progress on the task. */
3916 control_event_bootstrap(bootstrap_status_t status
, int progress
)
3918 const char *tag
, *summary
;
3919 char buf
[BOOTSTRAP_MSG_LEN
];
3921 if (bootstrap_percent
== BOOTSTRAP_STATUS_DONE
)
3922 return; /* already bootstrapped; nothing to be done here. */
3924 /* special case for handshaking status, since our TLS handshaking code
3925 * can't distinguish what the connection is going to be for. */
3926 if (status
== BOOTSTRAP_STATUS_HANDSHAKE
) {
3927 if (bootstrap_percent
< BOOTSTRAP_STATUS_CONN_OR
) {
3928 status
= BOOTSTRAP_STATUS_HANDSHAKE_DIR
;
3930 status
= BOOTSTRAP_STATUS_HANDSHAKE_OR
;
3934 if (status
> bootstrap_percent
||
3935 (progress
&& progress
> bootstrap_percent
)) {
3936 bootstrap_status_to_string(status
, &tag
, &summary
);
3937 log(status
? LOG_NOTICE
: LOG_INFO
, LD_CONTROL
,
3938 "Bootstrapped %d%%: %s.", progress
? progress
: status
, summary
);
3939 tor_snprintf(buf
, sizeof(buf
),
3940 "BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\"",
3941 progress
? progress
: status
, tag
, summary
);
3942 tor_snprintf(last_sent_bootstrap_message
,
3943 sizeof(last_sent_bootstrap_message
),
3945 control_event_client_status(LOG_NOTICE
, "%s", buf
);
3946 if (status
> bootstrap_percent
) {
3947 bootstrap_percent
= status
; /* new milestone reached */
3949 if (progress
> bootstrap_percent
) {
3950 /* incremental progress within a milestone */
3951 bootstrap_percent
= progress
;
3952 bootstrap_problems
= 0; /* Progress! Reset our problem counter. */
3957 /** Called when Tor has failed to make bootstrapping progress in a way
3958 * that indicates a problem. <b>warn</b> gives a hint as to why, and
3959 * <b>reason</b> provides an "or_conn_end_reason" tag.
3962 control_event_bootstrap_problem(const char *warn
, int reason
)
3964 int status
= bootstrap_percent
;
3965 const char *tag
, *summary
;
3966 char buf
[BOOTSTRAP_MSG_LEN
];
3967 const char *recommendation
= "ignore";
3969 if (bootstrap_percent
== 100)
3970 return; /* already bootstrapped; nothing to be done here. */
3972 bootstrap_problems
++;
3974 if (bootstrap_problems
>= BOOTSTRAP_PROBLEM_THRESHOLD
)
3975 recommendation
= "warn";
3977 if (reason
== END_OR_CONN_REASON_NO_ROUTE
)
3978 recommendation
= "warn";
3980 if (get_options()->UseBridges
&&
3981 !any_bridge_descriptors_known() &&
3982 !any_pending_bridge_descriptor_fetches())
3983 recommendation
= "warn";
3985 while (status
>=0 && bootstrap_status_to_string(status
, &tag
, &summary
) < 0)
3986 status
--; /* find a recognized status string based on current progress */
3987 status
= bootstrap_percent
; /* set status back to the actual number */
3989 log_fn(!strcmp(recommendation
, "warn") ? LOG_WARN
: LOG_INFO
,
3990 LD_CONTROL
, "Problem bootstrapping. Stuck at %d%%: %s. (%s; %s; "
3991 "count %d; recommendation %s)",
3992 status
, summary
, warn
,
3993 orconn_end_reason_to_control_string(reason
),
3994 bootstrap_problems
, recommendation
);
3995 tor_snprintf(buf
, sizeof(buf
),
3996 "BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\" WARNING=\"%s\" REASON=%s "
3997 "COUNT=%d RECOMMENDATION=%s",
3998 bootstrap_percent
, tag
, summary
, warn
,
3999 orconn_end_reason_to_control_string(reason
), bootstrap_problems
,
4001 tor_snprintf(last_sent_bootstrap_message
,
4002 sizeof(last_sent_bootstrap_message
),
4004 control_event_client_status(LOG_WARN
, "%s", buf
);
4007 /** We just generated a new summary of which countries we've seen clients
4008 * from recently. Send a copy to the controller in case it wants to
4009 * display it for the user. */
4011 control_event_clients_seen(const char *timestarted
, const char *countries
)
4013 send_control_event(EVENT_CLIENTS_SEEN
, 0,
4014 "650 CLIENTS_SEEN TimeStarted=\"%s\" CountrySummary=%s\r\n",
4015 timestarted
, countries
);