Tor  0.4.5.0-alpha-dev
control_events.c
Go to the documentation of this file.
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2  * Copyright (c) 2007-2020, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
5 /**
6  * \file control_events.c
7  * \brief Implement the event-reporting part of the controller API.
8  **/
9 
10 #define CONTROL_MODULE_PRIVATE
11 #define CONTROL_EVENTS_PRIVATE
12 #define OCIRC_EVENT_PRIVATE
13 
14 #include "core/or/or.h"
15 #include "app/config/config.h"
17 #include "core/mainloop/mainloop.h"
18 #include "core/or/channeltls.h"
19 #include "core/or/circuitlist.h"
20 #include "core/or/circuitstats.h"
21 #include "core/or/command.h"
23 #include "core/or/connection_or.h"
24 #include "core/or/reasons.h"
33 
38 #include "core/or/or_circuit_st.h"
40 
42 #include "lib/encoding/confline.h"
43 
44 static void flush_queued_events_cb(mainloop_event_t *event, void *arg);
45 static void control_get_bytes_rw_last_sec(uint64_t *r, uint64_t *w);
46 
47 /** Yield true iff <b>s</b> is the state of a control_connection_t that has
48  * finished authentication and is accepting commands. */
49 #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
50 
51 /** An event mask of all the events that any controller is interested in
52  * receiving. */
53 static event_mask_t global_event_mask = 0;
54 
55 /** True iff we have disabled log messages from being sent to the controller */
56 static int disable_log_messages = 0;
57 
58 /** Macro: true if any control connection is interested in events of type
59  * <b>e</b>. */
60 #define EVENT_IS_INTERESTING(e) \
61  (!! (global_event_mask & EVENT_MASK_(e)))
62 
63 /** Macro: true if any event from the bitfield 'e' is interesting. */
64 #define ANY_EVENT_IS_INTERESTING(e) \
65  (!! (global_event_mask & (e)))
66 
67 static void send_control_event_impl(uint16_t event,
68  const char *format, va_list ap)
69  CHECK_PRINTF(2,0);
70 static int control_event_status(int type, int severity, const char *format,
71  va_list args)
72  CHECK_PRINTF(3,0);
73 
74 static void send_control_event(uint16_t event,
75  const char *format, ...)
76  CHECK_PRINTF(2,3);
77 
78 /** Table mapping event values to their names. Used to implement SETEVENTS
79  * and GETINFO events/names, and to keep they in sync. */
80 const struct control_event_t control_event_table[] = {
81  { EVENT_CIRCUIT_STATUS, "CIRC" },
82  { EVENT_CIRCUIT_STATUS_MINOR, "CIRC_MINOR" },
83  { EVENT_STREAM_STATUS, "STREAM" },
84  { EVENT_OR_CONN_STATUS, "ORCONN" },
85  { EVENT_BANDWIDTH_USED, "BW" },
86  { EVENT_DEBUG_MSG, "DEBUG" },
87  { EVENT_INFO_MSG, "INFO" },
88  { EVENT_NOTICE_MSG, "NOTICE" },
89  { EVENT_WARN_MSG, "WARN" },
90  { EVENT_ERR_MSG, "ERR" },
91  { EVENT_NEW_DESC, "NEWDESC" },
92  { EVENT_ADDRMAP, "ADDRMAP" },
93  { EVENT_DESCCHANGED, "DESCCHANGED" },
94  { EVENT_NS, "NS" },
95  { EVENT_STATUS_GENERAL, "STATUS_GENERAL" },
96  { EVENT_STATUS_CLIENT, "STATUS_CLIENT" },
97  { EVENT_STATUS_SERVER, "STATUS_SERVER" },
98  { EVENT_GUARD, "GUARD" },
99  { EVENT_STREAM_BANDWIDTH_USED, "STREAM_BW" },
100  { EVENT_CLIENTS_SEEN, "CLIENTS_SEEN" },
101  { EVENT_NEWCONSENSUS, "NEWCONSENSUS" },
102  { EVENT_BUILDTIMEOUT_SET, "BUILDTIMEOUT_SET" },
103  { EVENT_GOT_SIGNAL, "SIGNAL" },
104  { EVENT_CONF_CHANGED, "CONF_CHANGED"},
105  { EVENT_CONN_BW, "CONN_BW" },
106  { EVENT_CELL_STATS, "CELL_STATS" },
107  { EVENT_CIRC_BANDWIDTH_USED, "CIRC_BW" },
108  { EVENT_TRANSPORT_LAUNCHED, "TRANSPORT_LAUNCHED" },
109  { EVENT_HS_DESC, "HS_DESC" },
110  { EVENT_HS_DESC_CONTENT, "HS_DESC_CONTENT" },
111  { EVENT_NETWORK_LIVENESS, "NETWORK_LIVENESS" },
112  { 0, NULL },
113 };
114 
115 /** Given a log severity, return the corresponding control event code. */
116 static inline int
118 {
119  switch (severity) {
120  case LOG_DEBUG: return EVENT_DEBUG_MSG;
121  case LOG_INFO: return EVENT_INFO_MSG;
122  case LOG_NOTICE: return EVENT_NOTICE_MSG;
123  case LOG_WARN: return EVENT_WARN_MSG;
124  case LOG_ERR: return EVENT_ERR_MSG;
125  default: return -1;
126  }
127 }
128 
129 /** Helper: clear bandwidth counters of all origin circuits. */
130 static void
132 {
133  origin_circuit_t *ocirc;
135  if (!CIRCUIT_IS_ORIGIN(circ))
136  continue;
137  ocirc = TO_ORIGIN_CIRCUIT(circ);
138  ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
141  }
142  SMARTLIST_FOREACH_END(circ);
143 }
144 
145 /* Helper to emit the BUILDTIMEOUT_SET circuit build time event */
146 void
147 cbt_control_event_buildtimeout_set(const circuit_build_times_t *cbt,
149 {
150  char *args = NULL;
151  double qnt;
152  double timeout_rate = 0.0;
153  double close_rate = 0.0;
154 
155  switch (type) {
156  case BUILDTIMEOUT_SET_EVENT_RESET:
157  case BUILDTIMEOUT_SET_EVENT_SUSPENDED:
158  case BUILDTIMEOUT_SET_EVENT_DISCARD:
159  qnt = 1.0;
160  break;
161  case BUILDTIMEOUT_SET_EVENT_COMPUTED:
162  case BUILDTIMEOUT_SET_EVENT_RESUME:
163  default:
165  break;
166  }
167 
168  /* The timeout rate is the ratio of the timeout count over
169  * the total number of circuits attempted. The total number of
170  * circuits is (timeouts+succeeded), since every circuit
171  * either succeeds, or times out. "Closed" circuits are
172  * MEASURE_TIMEOUT circuits whose measurement period expired.
173  * All MEASURE_TIMEOUT circuits are counted in the timeouts stat
174  * before transitioning to MEASURE_TIMEOUT (in
175  * circuit_build_times_mark_circ_as_measurement_only()).
176  * MEASURE_TIMEOUT circuits that succeed are *not* counted as
177  * "succeeded". See circuit_build_times_handle_completed_hop().
178  *
179  * We cast the denominator
180  * to promote it to double before the addition, to avoid int32
181  * overflow. */
182  const double total_circuits =
183  ((double)cbt->num_circ_timeouts) + cbt->num_circ_succeeded;
184  if (total_circuits >= 1.0) {
185  timeout_rate = cbt->num_circ_timeouts / total_circuits;
186  close_rate = cbt->num_circ_closed / total_circuits;
187  }
188 
189  tor_asprintf(&args, "TOTAL_TIMES=%lu "
190  "TIMEOUT_MS=%lu XM=%lu ALPHA=%f CUTOFF_QUANTILE=%f "
191  "TIMEOUT_RATE=%f CLOSE_MS=%lu CLOSE_RATE=%f",
192  (unsigned long)cbt->total_build_times,
193  (unsigned long)cbt->timeout_ms,
194  (unsigned long)cbt->Xm, cbt->alpha, qnt,
195  timeout_rate,
196  (unsigned long)cbt->close_ms,
197  close_rate);
198 
199  control_event_buildtimeout_set(type, args);
200 
201  tor_free(args);
202 }
203 /** Set <b>global_event_mask*</b> to the bitwise OR of each live control
204  * connection's event_mask field. */
205 void
207 {
209  event_mask_t old_mask, new_mask;
210  old_mask = global_event_mask;
211  int any_old_per_sec_events = control_any_per_second_event_enabled();
212 
213  global_event_mask = 0;
214  SMARTLIST_FOREACH(conns, connection_t *, _conn,
215  {
216  if (_conn->type == CONN_TYPE_CONTROL &&
217  STATE_IS_OPEN(_conn->state)) {
218  control_connection_t *conn = TO_CONTROL_CONN(_conn);
219  global_event_mask |= conn->event_mask;
220  }
221  });
222 
223  new_mask = global_event_mask;
224 
225  /* Handle the aftermath. Set up the log callback to tell us only what
226  * we want to hear...*/
228 
229  /* Macro: true if ev was false before and is true now. */
230 #define NEWLY_ENABLED(ev) \
231  (! (old_mask & (ev)) && (new_mask & (ev)))
232 
233  /* ...then, if we've started logging stream or circ bw, clear the
234  * appropriate fields. */
235  if (NEWLY_ENABLED(EVENT_STREAM_BANDWIDTH_USED)) {
236  SMARTLIST_FOREACH(conns, connection_t *, conn,
237  {
238  if (conn->type == CONN_TYPE_AP) {
239  edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
240  edge_conn->n_written = edge_conn->n_read = 0;
241  }
242  });
243  }
244  if (NEWLY_ENABLED(EVENT_CIRC_BANDWIDTH_USED)) {
246  }
247  if (NEWLY_ENABLED(EVENT_BANDWIDTH_USED)) {
248  uint64_t r, w;
250  }
251  if (any_old_per_sec_events != control_any_per_second_event_enabled()) {
253  }
254 
255 #undef NEWLY_ENABLED
256 }
257 
258 /** Given a control event code for a message event, return the corresponding
259  * log severity. */
260 static inline int
262 {
263  switch (event) {
264  case EVENT_DEBUG_MSG: return LOG_DEBUG;
265  case EVENT_INFO_MSG: return LOG_INFO;
266  case EVENT_NOTICE_MSG: return LOG_NOTICE;
267  case EVENT_WARN_MSG: return LOG_WARN;
268  case EVENT_ERR_MSG: return LOG_ERR;
269  default: return -1;
270  }
271 }
272 
273 /** Adjust the log severities that result in control_event_logmsg being called
274  * to match the severity of log messages that any controllers are interested
275  * in. */
276 void
278 {
279  int i;
280  int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
281 
282  for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
283  if (EVENT_IS_INTERESTING(i)) {
284  min_log_event = i;
285  break;
286  }
287  }
288  for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
289  if (EVENT_IS_INTERESTING(i)) {
290  max_log_event = i;
291  break;
292  }
293  }
294  if (EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL)) {
295  if (min_log_event > EVENT_NOTICE_MSG)
296  min_log_event = EVENT_NOTICE_MSG;
297  if (max_log_event < EVENT_ERR_MSG)
298  max_log_event = EVENT_ERR_MSG;
299  }
300  if (min_log_event <= max_log_event)
302  event_to_log_severity(max_log_event),
304  else
307 }
308 
309 /** Return true iff the event with code <b>c</b> is being sent to any current
310  * control connection. This is useful if the amount of work needed to prepare
311  * to call the appropriate control_event_...() function is high.
312  */
313 int
315 {
316  return EVENT_IS_INTERESTING(event);
317 }
318 
319 /** Return true if any event that needs to fire once a second is enabled. */
320 int
322 {
324  EVENT_MASK_(EVENT_BANDWIDTH_USED) |
325  EVENT_MASK_(EVENT_CELL_STATS) |
326  EVENT_MASK_(EVENT_CIRC_BANDWIDTH_USED) |
327  EVENT_MASK_(EVENT_CONN_BW) |
328  EVENT_MASK_(EVENT_STREAM_BANDWIDTH_USED)
329  );
330 }
331 
332 /* The value of 'get_bytes_read()' the previous time that
333  * control_get_bytes_rw_last_sec() as called. */
334 static uint64_t stats_prev_n_read = 0;
335 /* The value of 'get_bytes_written()' the previous time that
336  * control_get_bytes_rw_last_sec() as called. */
337 static uint64_t stats_prev_n_written = 0;
338 
339 /**
340  * Set <b>n_read</b> and <b>n_written</b> to the total number of bytes read
341  * and written by Tor since the last call to this function.
342  *
343  * Call this only from the main thread.
344  */
345 static void
347  uint64_t *n_written)
348 {
349  const uint64_t stats_n_bytes_read = get_bytes_read();
350  const uint64_t stats_n_bytes_written = get_bytes_written();
351 
352  *n_read = stats_n_bytes_read - stats_prev_n_read;
353  *n_written = stats_n_bytes_written - stats_prev_n_written;
354  stats_prev_n_read = stats_n_bytes_read;
355  stats_prev_n_written = stats_n_bytes_written;
356 }
357 
358 /**
359  * Run all the controller events (if any) that are scheduled to trigger once
360  * per second.
361  */
362 void
364 {
366  return;
367 
368  uint64_t bytes_read, bytes_written;
369  control_get_bytes_rw_last_sec(&bytes_read, &bytes_written);
370  control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
371 
376 }
377 
378 /** Represents an event that's queued to be sent to one or more
379  * controllers. */
380 typedef struct queued_event_t {
381  uint16_t event;
382  char *msg;
384 
385 /** Pointer to int. If this is greater than 0, we don't allow new events to be
386  * queued. */
388 
389 /** Holds a smartlist of queued_event_t objects that may need to be sent
390  * to one or more controllers */
392 
393 /** True if the flush_queued_events_event is pending. */
395 
396 /** Lock to protect the above fields. */
398 
399 /** An event that should fire in order to flush the contents of
400  * queued_control_events. */
402 
403 void
404 control_initialize_event_queue(void)
405 {
406  if (queued_control_events == NULL) {
408  }
409 
410  if (flush_queued_events_event == NULL) {
411  struct event_base *b = tor_libevent_get_base();
412  if (b) {
416  }
417  }
418 
419  if (queued_control_events_lock == NULL) {
422  }
423 }
424 
425 static int *
426 get_block_event_queue(void)
427 {
429  if (PREDICT_UNLIKELY(val == NULL)) {
430  val = tor_malloc_zero(sizeof(int));
432  }
433  return val;
434 }
435 
436 /** Helper: inserts an event on the list of events queued to be sent to
437  * one or more controllers, and schedules the events to be flushed if needed.
438  *
439  * This function takes ownership of <b>msg</b>, and may free it.
440  *
441  * We queue these events rather than send them immediately in order to break
442  * the dependency in our callgraph from code that generates events for the
443  * controller, and the network layer at large. Otherwise, nearly every
444  * interesting part of Tor would potentially call every other interesting part
445  * of Tor.
446  */
447 MOCK_IMPL(STATIC void,
448 queue_control_event_string,(uint16_t event, char *msg))
449 {
450  /* This is redundant with checks done elsewhere, but it's a last-ditch
451  * attempt to avoid queueing something we shouldn't have to queue. */
452  if (PREDICT_UNLIKELY( ! EVENT_IS_INTERESTING(event) )) {
453  tor_free(msg);
454  return;
455  }
456 
457  int *block_event_queue = get_block_event_queue();
458  if (*block_event_queue) {
459  tor_free(msg);
460  return;
461  }
462 
463  queued_event_t *ev = tor_malloc(sizeof(*ev));
464  ev->event = event;
465  ev->msg = msg;
466 
467  /* No queueing an event while queueing an event */
468  ++*block_event_queue;
469 
473 
474  int activate_event = 0;
476  activate_event = 1;
478  }
479 
481 
482  --*block_event_queue;
483 
484  /* We just put an event on the queue; mark the queue to be
485  * flushed. We only do this from the main thread for now; otherwise,
486  * we'd need to incur locking overhead in Libevent or use a socket.
487  */
488  if (activate_event) {
491  }
492 }
493 
494 #define queued_event_free(ev) \
495  FREE_AND_NULL(queued_event_t, queued_event_free_, (ev))
496 
497 /** Release all storage held by <b>ev</b>. */
498 static void
500 {
501  if (ev == NULL)
502  return;
503 
504  tor_free(ev->msg);
505  tor_free(ev);
506 }
507 
508 /** Send every queued event to every controller that's interested in it,
509  * and remove the events from the queue. If <b>force</b> is true,
510  * then make all controllers send their data out immediately, since we
511  * may be about to shut down. */
512 static void
514 {
515  /* Make sure that we get all the pending log events, if there are any. */
517 
518  if (PREDICT_UNLIKELY(queued_control_events == NULL)) {
519  return;
520  }
521  smartlist_t *all_conns = get_connection_array();
522  smartlist_t *controllers = smartlist_new();
523  smartlist_t *queued_events;
524 
525  int *block_event_queue = get_block_event_queue();
526  ++*block_event_queue;
527 
529  /* No queueing an event while flushing events. */
531  queued_events = queued_control_events;
534 
535  /* Gather all the controllers that will care... */
536  SMARTLIST_FOREACH_BEGIN(all_conns, connection_t *, conn) {
537  if (conn->type == CONN_TYPE_CONTROL &&
538  !conn->marked_for_close &&
539  conn->state == CONTROL_CONN_STATE_OPEN) {
540  control_connection_t *control_conn = TO_CONTROL_CONN(conn);
541 
542  smartlist_add(controllers, control_conn);
543  }
544  } SMARTLIST_FOREACH_END(conn);
545 
546  SMARTLIST_FOREACH_BEGIN(queued_events, queued_event_t *, ev) {
547  const event_mask_t bit = ((event_mask_t)1) << ev->event;
548  const size_t msg_len = strlen(ev->msg);
550  control_conn) {
551  if (control_conn->event_mask & bit) {
552  connection_buf_add(ev->msg, msg_len, TO_CONN(control_conn));
553  }
554  } SMARTLIST_FOREACH_END(control_conn);
555 
556  queued_event_free(ev);
557  } SMARTLIST_FOREACH_END(ev);
558 
559  if (force) {
561  control_conn) {
562  connection_flush(TO_CONN(control_conn));
563  } SMARTLIST_FOREACH_END(control_conn);
564  }
565 
566  smartlist_free(queued_events);
567  smartlist_free(controllers);
568 
569  --*block_event_queue;
570 }
571 
572 /** Libevent callback: Flushes pending events to controllers that are
573  * interested in them. */
574 static void
576 {
577  (void) event;
578  (void) arg;
580 }
581 
582 /** Send an event to all v1 controllers that are listening for code
583  * <b>event</b>. The event's body is given by <b>msg</b>.
584  *
585  * The EXTENDED_FORMAT and NONEXTENDED_FORMAT flags behave similarly with
586  * respect to the EXTENDED_EVENTS feature. */
587 MOCK_IMPL(STATIC void,
588 send_control_event_string,(uint16_t event,
589  const char *msg))
590 {
591  tor_assert(event >= EVENT_MIN_ && event <= EVENT_MAX_);
592  queue_control_event_string(event, tor_strdup(msg));
593 }
594 
595 /** Helper for send_control_event and control_event_status:
596  * Send an event to all v1 controllers that are listening for code
597  * <b>event</b>. The event's body is created by the printf-style format in
598  * <b>format</b>, and other arguments as provided. */
599 static void
600 send_control_event_impl(uint16_t event,
601  const char *format, va_list ap)
602 {
603  char *buf = NULL;
604  int len;
605 
606  len = tor_vasprintf(&buf, format, ap);
607  if (len < 0) {
608  log_warn(LD_BUG, "Unable to format event for controller.");
609  return;
610  }
611 
612  queue_control_event_string(event, buf);
613 }
614 
615 /** Send an event to all v1 controllers that are listening for code
616  * <b>event</b>. The event's body is created by the printf-style format in
617  * <b>format</b>, and other arguments as provided. */
618 static void
619 send_control_event(uint16_t event,
620  const char *format, ...)
621 {
622  va_list ap;
623  va_start(ap, format);
624  send_control_event_impl(event, format, ap);
625  va_end(ap);
626 }
627 
628 /** Something major has happened to circuit <b>circ</b>: tell any
629  * interested control connections. */
630 int
632  int reason_code)
633 {
634  const char *status;
635  char reasons[64] = "";
636 
637  if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
638  return 0;
639  tor_assert(circ);
640 
641  switch (tp)
642  {
643  case CIRC_EVENT_LAUNCHED: status = "LAUNCHED"; break;
644  case CIRC_EVENT_BUILT: status = "BUILT"; break;
645  case CIRC_EVENT_EXTENDED: status = "EXTENDED"; break;
646  case CIRC_EVENT_FAILED: status = "FAILED"; break;
647  case CIRC_EVENT_CLOSED: status = "CLOSED"; break;
648  default:
649  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
651  return 0;
652  }
653 
654  if (tp == CIRC_EVENT_FAILED || tp == CIRC_EVENT_CLOSED) {
655  const char *reason_str = circuit_end_reason_to_control_string(reason_code);
656  char unk_reason_buf[16];
657  if (!reason_str) {
658  tor_snprintf(unk_reason_buf, 16, "UNKNOWN_%d", reason_code);
659  reason_str = unk_reason_buf;
660  }
661  if (reason_code > 0 && reason_code & END_CIRC_REASON_FLAG_REMOTE) {
662  tor_snprintf(reasons, sizeof(reasons),
663  " REASON=DESTROYED REMOTE_REASON=%s", reason_str);
664  } else {
665  tor_snprintf(reasons, sizeof(reasons),
666  " REASON=%s", reason_str);
667  }
668  }
669 
670  {
671  char *circdesc = circuit_describe_status_for_controller(circ);
672  const char *sp = strlen(circdesc) ? " " : "";
673  send_control_event(EVENT_CIRCUIT_STATUS,
674  "650 CIRC %lu %s%s%s%s\r\n",
675  (unsigned long)circ->global_identifier,
676  status, sp,
677  circdesc,
678  reasons);
679  tor_free(circdesc);
680  }
681 
682  return 0;
683 }
684 
685 /** Something minor has happened to circuit <b>circ</b>: tell any
686  * interested control connections. */
687 static int
690  int purpose, const struct timeval *tv)
691 {
692  const char *event_desc;
693  char event_tail[160] = "";
694  if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS_MINOR))
695  return 0;
696  tor_assert(circ);
697 
698  switch (e)
699  {
700  case CIRC_MINOR_EVENT_PURPOSE_CHANGED:
701  event_desc = "PURPOSE_CHANGED";
702 
703  {
704  /* event_tail can currently be up to 68 chars long */
705  const char *hs_state_str =
707  tor_snprintf(event_tail, sizeof(event_tail),
708  " OLD_PURPOSE=%s%s%s",
710  (hs_state_str != NULL) ? " OLD_HS_STATE=" : "",
711  (hs_state_str != NULL) ? hs_state_str : "");
712  }
713 
714  break;
715  case CIRC_MINOR_EVENT_CANNIBALIZED:
716  event_desc = "CANNIBALIZED";
717 
718  {
719  /* event_tail can currently be up to 130 chars long */
720  const char *hs_state_str =
722  const struct timeval *old_timestamp_began = tv;
723  char tbuf[ISO_TIME_USEC_LEN+1];
724  format_iso_time_nospace_usec(tbuf, old_timestamp_began);
725 
726  tor_snprintf(event_tail, sizeof(event_tail),
727  " OLD_PURPOSE=%s%s%s OLD_TIME_CREATED=%s",
729  (hs_state_str != NULL) ? " OLD_HS_STATE=" : "",
730  (hs_state_str != NULL) ? hs_state_str : "",
731  tbuf);
732  }
733 
734  break;
735  default:
736  log_warn(LD_BUG, "Unrecognized status code %d", (int)e);
738  return 0;
739  }
740 
741  {
742  char *circdesc = circuit_describe_status_for_controller(circ);
743  const char *sp = strlen(circdesc) ? " " : "";
744  send_control_event(EVENT_CIRCUIT_STATUS_MINOR,
745  "650 CIRC_MINOR %lu %s%s%s%s\r\n",
746  (unsigned long)circ->global_identifier,
747  event_desc, sp,
748  circdesc,
749  event_tail);
750  tor_free(circdesc);
751  }
752 
753  return 0;
754 }
755 
756 /**
757  * <b>circ</b> has changed its purpose from <b>old_purpose</b>: tell any
758  * interested controllers.
759  */
760 int
762  int old_purpose)
763 {
765  CIRC_MINOR_EVENT_PURPOSE_CHANGED,
766  old_purpose,
767  NULL);
768 }
769 
770 /**
771  * <b>circ</b> has changed its purpose from <b>old_purpose</b>, and its
772  * created-time from <b>old_tv_created</b>: tell any interested controllers.
773  */
774 int
776  int old_purpose,
777  const struct timeval *old_tv_created)
778 {
780  CIRC_MINOR_EVENT_CANNIBALIZED,
781  old_purpose,
782  old_tv_created);
783 }
784 
785 /** Something has happened to the stream associated with AP connection
786  * <b>conn</b>: tell any interested control connections. */
787 int
789  int reason_code)
790 {
791  char reason_buf[64];
792  char addrport_buf[64];
793  const char *status;
794  circuit_t *circ;
795  origin_circuit_t *origin_circ = NULL;
796  char buf[256];
797  const char *purpose = "";
798  tor_assert(conn->socks_request);
799 
800  if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
801  return 0;
802 
803  if (tp == STREAM_EVENT_CLOSED &&
805  return 0;
806 
807  write_stream_target_to_buf(conn, buf, sizeof(buf));
808 
809  reason_buf[0] = '\0';
810  switch (tp)
811  {
812  case STREAM_EVENT_SENT_CONNECT: status = "SENTCONNECT"; break;
813  case STREAM_EVENT_SENT_RESOLVE: status = "SENTRESOLVE"; break;
814  case STREAM_EVENT_SUCCEEDED: status = "SUCCEEDED"; break;
815  case STREAM_EVENT_FAILED: status = "FAILED"; break;
816  case STREAM_EVENT_CLOSED: status = "CLOSED"; break;
817  case STREAM_EVENT_NEW: status = "NEW"; break;
818  case STREAM_EVENT_NEW_RESOLVE: status = "NEWRESOLVE"; break;
819  case STREAM_EVENT_FAILED_RETRIABLE: status = "DETACHED"; break;
820  case STREAM_EVENT_REMAP: status = "REMAP"; break;
821  default:
822  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
823  return 0;
824  }
825  if (reason_code && (tp == STREAM_EVENT_FAILED ||
826  tp == STREAM_EVENT_CLOSED ||
827  tp == STREAM_EVENT_FAILED_RETRIABLE)) {
828  const char *reason_str = stream_end_reason_to_control_string(reason_code);
829  char *r = NULL;
830  if (!reason_str) {
831  tor_asprintf(&r, " UNKNOWN_%d", reason_code);
832  reason_str = r;
833  }
834  if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
835  tor_snprintf(reason_buf, sizeof(reason_buf),
836  " REASON=END REMOTE_REASON=%s", reason_str);
837  else
838  tor_snprintf(reason_buf, sizeof(reason_buf),
839  " REASON=%s", reason_str);
840  tor_free(r);
841  } else if (reason_code && tp == STREAM_EVENT_REMAP) {
842  switch (reason_code) {
844  strlcpy(reason_buf, " SOURCE=CACHE", sizeof(reason_buf));
845  break;
847  strlcpy(reason_buf, " SOURCE=EXIT", sizeof(reason_buf));
848  break;
849  default:
850  tor_snprintf(reason_buf, sizeof(reason_buf), " REASON=UNKNOWN_%d",
851  reason_code);
852  /* XXX do we want SOURCE=UNKNOWN_%d above instead? -RD */
853  break;
854  }
855  }
856 
857  if (tp == STREAM_EVENT_NEW || tp == STREAM_EVENT_NEW_RESOLVE) {
858  /*
859  * When the control conn is an AF_UNIX socket and we have no address,
860  * it gets set to "(Tor_internal)"; see dnsserv_launch_request() in
861  * dnsserv.c.
862  */
863  if (strcmp(ENTRY_TO_CONN(conn)->address, "(Tor_internal)") != 0) {
864  tor_snprintf(addrport_buf,sizeof(addrport_buf), " SOURCE_ADDR=%s:%d",
865  ENTRY_TO_CONN(conn)->address, ENTRY_TO_CONN(conn)->port);
866  } else {
867  /*
868  * else leave it blank so control on AF_UNIX doesn't need to make
869  * something up.
870  */
871  addrport_buf[0] = '\0';
872  }
873  } else {
874  addrport_buf[0] = '\0';
875  }
876 
877  if (tp == STREAM_EVENT_NEW_RESOLVE) {
878  purpose = " PURPOSE=DNS_REQUEST";
879  } else if (tp == STREAM_EVENT_NEW) {
880  if (conn->use_begindir) {
881  connection_t *linked = ENTRY_TO_CONN(conn)->linked_conn;
882  int linked_dir_purpose = -1;
883  if (linked && linked->type == CONN_TYPE_DIR)
884  linked_dir_purpose = linked->purpose;
885  if (DIR_PURPOSE_IS_UPLOAD(linked_dir_purpose))
886  purpose = " PURPOSE=DIR_UPLOAD";
887  else
888  purpose = " PURPOSE=DIR_FETCH";
889  } else
890  purpose = " PURPOSE=USER";
891  }
892 
894  if (circ && CIRCUIT_IS_ORIGIN(circ))
895  origin_circ = TO_ORIGIN_CIRCUIT(circ);
896 
897  {
899  const char *sp = strlen(conndesc) ? " " : "";
900  send_control_event(EVENT_STREAM_STATUS,
901  "650 STREAM %"PRIu64" %s %lu %s%s%s%s%s%s\r\n",
902  (ENTRY_TO_CONN(conn)->global_identifier),
903  status,
904  origin_circ?
905  (unsigned long)origin_circ->global_identifier : 0ul,
906  buf, reason_buf, addrport_buf, purpose, sp, conndesc);
907  tor_free(conndesc);
908  }
909 
910  /* XXX need to specify its intended exit, etc? */
911 
912  return 0;
913 }
914 
915 /** Called when the status of an OR connection <b>conn</b> changes: tell any
916  * interested control connections. <b>tp</b> is the new status for the
917  * connection. If <b>conn</b> has just closed or failed, then <b>reason</b>
918  * may be the reason why.
919  */
920 int
922  int reason)
923 {
924  int ncircs = 0;
925  const char *status;
926  char name[128];
927  char ncircs_buf[32] = {0}; /* > 8 + log10(2^32)=10 + 2 */
928 
929  if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
930  return 0;
931 
932  switch (tp)
933  {
934  case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break;
935  case OR_CONN_EVENT_CONNECTED: status = "CONNECTED"; break;
936  case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
937  case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
938  case OR_CONN_EVENT_NEW: status = "NEW"; break;
939  default:
940  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
941  return 0;
942  }
943  if (conn->chan) {
944  ncircs = circuit_count_pending_on_channel(TLS_CHAN_TO_BASE(conn->chan));
945  } else {
946  ncircs = 0;
947  }
948  ncircs += connection_or_get_num_circuits(conn);
949  if (ncircs && (tp == OR_CONN_EVENT_FAILED || tp == OR_CONN_EVENT_CLOSED)) {
950  tor_snprintf(ncircs_buf, sizeof(ncircs_buf), " NCIRCS=%d", ncircs);
951  }
952 
953  orconn_target_get_name(name, sizeof(name), conn);
954  send_control_event(EVENT_OR_CONN_STATUS,
955  "650 ORCONN %s %s%s%s%s ID=%"PRIu64"\r\n",
956  name, status,
957  reason ? " REASON=" : "",
959  ncircs_buf,
960  (conn->base_.global_identifier));
961 
962  return 0;
963 }
964 
965 /**
966  * Print out STREAM_BW event for a single conn
967  */
968 int
970 {
971  struct timeval now;
972  char tbuf[ISO_TIME_USEC_LEN+1];
973  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
974  if (!edge_conn->n_read && !edge_conn->n_written)
975  return 0;
976 
977  tor_gettimeofday(&now);
978  format_iso_time_nospace_usec(tbuf, &now);
979  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
980  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
981  (edge_conn->base_.global_identifier),
982  (unsigned long)edge_conn->n_read,
983  (unsigned long)edge_conn->n_written,
984  tbuf);
985 
986  edge_conn->n_written = edge_conn->n_read = 0;
987  }
988 
989  return 0;
990 }
991 
992 /** A second or more has elapsed: tell any interested control
993  * connections how much bandwidth streams have used. */
994 int
996 {
997  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
999  edge_connection_t *edge_conn;
1000  struct timeval now;
1001  char tbuf[ISO_TIME_USEC_LEN+1];
1002 
1003  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn)
1004  {
1005  if (conn->type != CONN_TYPE_AP)
1006  continue;
1007  edge_conn = TO_EDGE_CONN(conn);
1008  if (!edge_conn->n_read && !edge_conn->n_written)
1009  continue;
1010 
1011  tor_gettimeofday(&now);
1012  format_iso_time_nospace_usec(tbuf, &now);
1013  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
1014  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
1015  (edge_conn->base_.global_identifier),
1016  (unsigned long)edge_conn->n_read,
1017  (unsigned long)edge_conn->n_written,
1018  tbuf);
1019 
1020  edge_conn->n_written = edge_conn->n_read = 0;
1021  }
1022  SMARTLIST_FOREACH_END(conn);
1023  }
1024 
1025  return 0;
1026 }
1027 
1028 /** A second or more has elapsed: tell any interested control connections
1029  * how much bandwidth origin circuits have used. */
1030 int
1032 {
1033  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
1034  return 0;
1035 
1037  if (!CIRCUIT_IS_ORIGIN(circ))
1038  continue;
1039 
1041  }
1042  SMARTLIST_FOREACH_END(circ);
1043 
1044  return 0;
1045 }
1046 
1047 /**
1048  * Emit a CIRC_BW event line for a specific circuit.
1049  *
1050  * This function sets the values it emits to 0, and does not emit
1051  * an event if there is no new data to report since the last call.
1052  *
1053  * Therefore, it may be called at any frequency.
1054  */
1055 int
1057 {
1058  struct timeval now;
1059  char tbuf[ISO_TIME_USEC_LEN+1];
1060 
1061  tor_assert(ocirc);
1062 
1063  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
1064  return 0;
1065 
1066  /* n_read_circ_bw and n_written_circ_bw are always updated
1067  * when there is any new cell on a circuit, and set to 0 after
1068  * the event, below.
1069  *
1070  * Therefore, checking them is sufficient to determine if there
1071  * is new data to report. */
1072  if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
1073  return 0;
1074 
1075  tor_gettimeofday(&now);
1076  format_iso_time_nospace_usec(tbuf, &now);
1077  send_control_event(EVENT_CIRC_BANDWIDTH_USED,
1078  "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu TIME=%s "
1079  "DELIVERED_READ=%lu OVERHEAD_READ=%lu "
1080  "DELIVERED_WRITTEN=%lu OVERHEAD_WRITTEN=%lu\r\n",
1081  ocirc->global_identifier,
1082  (unsigned long)ocirc->n_read_circ_bw,
1083  (unsigned long)ocirc->n_written_circ_bw,
1084  tbuf,
1085  (unsigned long)ocirc->n_delivered_read_circ_bw,
1086  (unsigned long)ocirc->n_overhead_read_circ_bw,
1087  (unsigned long)ocirc->n_delivered_written_circ_bw,
1088  (unsigned long)ocirc->n_overhead_written_circ_bw);
1089  ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
1092 
1093  return 0;
1094 }
1095 
1096 /** Print out CONN_BW event for a single OR/DIR/EXIT <b>conn</b> and reset
1097  * bandwidth counters. */
1098 int
1100 {
1101  const char *conn_type_str;
1102  if (!get_options()->TestingEnableConnBwEvent ||
1103  !EVENT_IS_INTERESTING(EVENT_CONN_BW))
1104  return 0;
1105  if (!conn->n_read_conn_bw && !conn->n_written_conn_bw)
1106  return 0;
1107  switch (conn->type) {
1108  case CONN_TYPE_OR:
1109  conn_type_str = "OR";
1110  break;
1111  case CONN_TYPE_DIR:
1112  conn_type_str = "DIR";
1113  break;
1114  case CONN_TYPE_EXIT:
1115  conn_type_str = "EXIT";
1116  break;
1117  default:
1118  return 0;
1119  }
1120  send_control_event(EVENT_CONN_BW,
1121  "650 CONN_BW ID=%"PRIu64" TYPE=%s "
1122  "READ=%lu WRITTEN=%lu\r\n",
1123  (conn->global_identifier),
1124  conn_type_str,
1125  (unsigned long)conn->n_read_conn_bw,
1126  (unsigned long)conn->n_written_conn_bw);
1127  conn->n_written_conn_bw = conn->n_read_conn_bw = 0;
1128  return 0;
1129 }
1130 
1131 /** A second or more has elapsed: tell any interested control
1132  * connections how much bandwidth connections have used. */
1133 int
1135 {
1136  if (get_options()->TestingEnableConnBwEvent &&
1137  EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
1140  }
1141  return 0;
1142 }
1143 
1144 /** Helper: iterate over cell statistics of <b>circ</b> and sum up added
1145  * cells, removed cells, and waiting times by cell command and direction.
1146  * Store results in <b>cell_stats</b>. Free cell statistics of the
1147  * circuit afterwards. */
1148 void
1149 sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
1150 {
1151  memset(cell_stats, 0, sizeof(cell_stats_t));
1153  const testing_cell_stats_entry_t *, ent) {
1154  tor_assert(ent->command <= CELL_COMMAND_MAX_);
1155  if (!ent->removed && !ent->exitward) {
1156  cell_stats->added_cells_appward[ent->command] += 1;
1157  } else if (!ent->removed && ent->exitward) {
1158  cell_stats->added_cells_exitward[ent->command] += 1;
1159  } else if (!ent->exitward) {
1160  cell_stats->removed_cells_appward[ent->command] += 1;
1161  cell_stats->total_time_appward[ent->command] += ent->waiting_time * 10;
1162  } else {
1163  cell_stats->removed_cells_exitward[ent->command] += 1;
1164  cell_stats->total_time_exitward[ent->command] += ent->waiting_time * 10;
1165  }
1166  } SMARTLIST_FOREACH_END(ent);
1168 }
1169 
1170 /** Helper: append a cell statistics string to <code>event_parts</code>,
1171  * prefixed with <code>key</code>=. Statistics consist of comma-separated
1172  * key:value pairs with lower-case command strings as keys and cell
1173  * numbers or total waiting times as values. A key:value pair is included
1174  * if the entry in <code>include_if_non_zero</code> is not zero, but with
1175  * the (possibly zero) entry from <code>number_to_include</code>. Both
1176  * arrays are expected to have a length of CELL_COMMAND_MAX_ + 1. If no
1177  * entry in <code>include_if_non_zero</code> is positive, no string will
1178  * be added to <code>event_parts</code>. */
1179 void
1180 append_cell_stats_by_command(smartlist_t *event_parts, const char *key,
1181  const uint64_t *include_if_non_zero,
1182  const uint64_t *number_to_include)
1183 {
1184  smartlist_t *key_value_strings = smartlist_new();
1185  int i;
1186  for (i = 0; i <= CELL_COMMAND_MAX_; i++) {
1187  if (include_if_non_zero[i] > 0) {
1188  smartlist_add_asprintf(key_value_strings, "%s:%"PRIu64,
1190  (number_to_include[i]));
1191  }
1192  }
1193  if (smartlist_len(key_value_strings) > 0) {
1194  char *joined = smartlist_join_strings(key_value_strings, ",", 0, NULL);
1195  smartlist_add_asprintf(event_parts, "%s=%s", key, joined);
1196  SMARTLIST_FOREACH(key_value_strings, char *, cp, tor_free(cp));
1197  tor_free(joined);
1198  }
1199  smartlist_free(key_value_strings);
1200 }
1201 
1202 /** Helper: format <b>cell_stats</b> for <b>circ</b> for inclusion in a
1203  * CELL_STATS event and write result string to <b>event_string</b>. */
1204 void
1205 format_cell_stats(char **event_string, circuit_t *circ,
1206  cell_stats_t *cell_stats)
1207 {
1208  smartlist_t *event_parts = smartlist_new();
1209  if (CIRCUIT_IS_ORIGIN(circ)) {
1210  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1211  smartlist_add_asprintf(event_parts, "ID=%lu",
1212  (unsigned long)ocirc->global_identifier);
1213  } else if (TO_OR_CIRCUIT(circ)->p_chan) {
1214  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1215  smartlist_add_asprintf(event_parts, "InboundQueue=%lu",
1216  (unsigned long)or_circ->p_circ_id);
1217  smartlist_add_asprintf(event_parts, "InboundConn=%"PRIu64,
1218  (or_circ->p_chan->global_identifier));
1219  append_cell_stats_by_command(event_parts, "InboundAdded",
1220  cell_stats->added_cells_appward,
1221  cell_stats->added_cells_appward);
1222  append_cell_stats_by_command(event_parts, "InboundRemoved",
1223  cell_stats->removed_cells_appward,
1224  cell_stats->removed_cells_appward);
1225  append_cell_stats_by_command(event_parts, "InboundTime",
1226  cell_stats->removed_cells_appward,
1227  cell_stats->total_time_appward);
1228  }
1229  if (circ->n_chan) {
1230  smartlist_add_asprintf(event_parts, "OutboundQueue=%lu",
1231  (unsigned long)circ->n_circ_id);
1232  smartlist_add_asprintf(event_parts, "OutboundConn=%"PRIu64,
1233  (circ->n_chan->global_identifier));
1234  append_cell_stats_by_command(event_parts, "OutboundAdded",
1235  cell_stats->added_cells_exitward,
1236  cell_stats->added_cells_exitward);
1237  append_cell_stats_by_command(event_parts, "OutboundRemoved",
1238  cell_stats->removed_cells_exitward,
1239  cell_stats->removed_cells_exitward);
1240  append_cell_stats_by_command(event_parts, "OutboundTime",
1241  cell_stats->removed_cells_exitward,
1242  cell_stats->total_time_exitward);
1243  }
1244  *event_string = smartlist_join_strings(event_parts, " ", 0, NULL);
1245  SMARTLIST_FOREACH(event_parts, char *, cp, tor_free(cp));
1246  smartlist_free(event_parts);
1247 }
1248 
1249 /** A second or more has elapsed: tell any interested control connection
1250  * how many cells have been processed for a given circuit. */
1251 int
1253 {
1254  cell_stats_t *cell_stats;
1255  char *event_string;
1256  if (!get_options()->TestingEnableCellStatsEvent ||
1257  !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
1258  return 0;
1259  cell_stats = tor_malloc(sizeof(cell_stats_t));
1261  if (!circ->testing_cell_stats)
1262  continue;
1263  sum_up_cell_stats_by_command(circ, cell_stats);
1264  format_cell_stats(&event_string, circ, cell_stats);
1265  send_control_event(EVENT_CELL_STATS,
1266  "650 CELL_STATS %s\r\n", event_string);
1267  tor_free(event_string);
1268  }
1269  SMARTLIST_FOREACH_END(circ);
1270  tor_free(cell_stats);
1271  return 0;
1272 }
1273 
1274 /* about 5 minutes worth. */
1275 #define N_BW_EVENTS_TO_CACHE 300
1276 /* Index into cached_bw_events to next write. */
1277 static int next_measurement_idx = 0;
1278 /* number of entries set in n_measurements */
1279 static int n_measurements = 0;
1280 static struct cached_bw_event_t {
1281  uint32_t n_read;
1282  uint32_t n_written;
1283 } cached_bw_events[N_BW_EVENTS_TO_CACHE];
1284 
1285 /** A second or more has elapsed: tell any interested control
1286  * connections how much bandwidth we used. */
1287 int
1288 control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
1289 {
1290  cached_bw_events[next_measurement_idx].n_read = n_read;
1291  cached_bw_events[next_measurement_idx].n_written = n_written;
1292  if (++next_measurement_idx == N_BW_EVENTS_TO_CACHE)
1293  next_measurement_idx = 0;
1294  if (n_measurements < N_BW_EVENTS_TO_CACHE)
1295  ++n_measurements;
1296 
1297  if (EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED)) {
1298  send_control_event(EVENT_BANDWIDTH_USED,
1299  "650 BW %lu %lu\r\n",
1300  (unsigned long)n_read,
1301  (unsigned long)n_written);
1302  }
1303 
1304  return 0;
1305 }
1306 
1307 char *
1308 get_bw_samples(void)
1309 {
1310  int i;
1311  int idx = (next_measurement_idx + N_BW_EVENTS_TO_CACHE - n_measurements)
1312  % N_BW_EVENTS_TO_CACHE;
1313  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1314 
1315  smartlist_t *elements = smartlist_new();
1316 
1317  for (i = 0; i < n_measurements; ++i) {
1318  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1319  const struct cached_bw_event_t *bwe = &cached_bw_events[idx];
1320 
1321  smartlist_add_asprintf(elements, "%u,%u",
1322  (unsigned)bwe->n_read,
1323  (unsigned)bwe->n_written);
1324 
1325  idx = (idx + 1) % N_BW_EVENTS_TO_CACHE;
1326  }
1327 
1328  char *result = smartlist_join_strings(elements, " ", 0, NULL);
1329 
1330  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
1331  smartlist_free(elements);
1332 
1333  return result;
1334 }
1335 
1336 /** Called when we are sending a log message to the controllers: suspend
1337  * sending further log messages to the controllers until we're done. Used by
1338  * CONN_LOG_PROTECT. */
1339 void
1341 {
1343 }
1344 
1345 /** We're done sending a log message to the controllers: re-enable controller
1346  * logging. Used by CONN_LOG_PROTECT. */
1347 void
1349 {
1350  if (--disable_log_messages < 0)
1351  tor_assert(0);
1352 }
1353 
1354 /** We got a log message: tell any interested control connections. */
1355 void
1356 control_event_logmsg(int severity, log_domain_mask_t domain, const char *msg)
1357 {
1358  int event;
1359 
1360  /* Don't even think of trying to add stuff to a buffer from a cpuworker
1361  * thread. (See #25987 for plan to fix.) */
1362  if (! in_main_thread())
1363  return;
1364 
1366  return;
1367 
1368  if (domain == LD_BUG && EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL) &&
1369  severity <= LOG_NOTICE) {
1370  char *esc = esc_for_log(msg);
1372  control_event_general_status(severity, "BUG REASON=%s", esc);
1374  tor_free(esc);
1375  }
1376 
1377  event = log_severity_to_event(severity);
1378  if (event >= 0 && EVENT_IS_INTERESTING(event)) {
1379  char *b = NULL;
1380  const char *s;
1381  if (strchr(msg, '\n')) {
1382  char *cp;
1383  b = tor_strdup(msg);
1384  for (cp = b; *cp; ++cp)
1385  if (*cp == '\r' || *cp == '\n')
1386  *cp = ' ';
1387  }
1388  switch (severity) {
1389  case LOG_DEBUG: s = "DEBUG"; break;
1390  case LOG_INFO: s = "INFO"; break;
1391  case LOG_NOTICE: s = "NOTICE"; break;
1392  case LOG_WARN: s = "WARN"; break;
1393  case LOG_ERR: s = "ERR"; break;
1394  default: s = "UnknownLogSeverity"; break;
1395  }
1397  send_control_event(event, "650 %s %s\r\n", s, b?b:msg);
1398  if (severity == LOG_ERR) {
1399  /* Force a flush, since we may be about to die horribly */
1401  }
1403  tor_free(b);
1404  }
1405 }
1406 
1407 /**
1408  * Logging callback: called when there is a queued pending log callback.
1409  */
1410 void
1412 {
1413  if (! in_main_thread()) {
1414  /* We can't handle this case yet, since we're using a
1415  * mainloop_event_t to invoke queued_events_flush_all. We ought to
1416  * use a different mechanism instead: see #25987.
1417  **/
1418  return;
1419  }
1422 }
1423 
1424 /** Called whenever we receive new router descriptors: tell any
1425  * interested control connections. <b>routers</b> is a list of
1426  * routerinfo_t's.
1427  */
1428 int
1430 {
1431  char *msg;
1432 
1433  if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
1434  return 0;
1435 
1436  {
1437  smartlist_t *names = smartlist_new();
1438  char *ids;
1439  SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
1440  char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
1442  smartlist_add(names, b);
1443  });
1444  ids = smartlist_join_strings(names, " ", 0, NULL);
1445  tor_asprintf(&msg, "650 NEWDESC %s\r\n", ids);
1446  send_control_event_string(EVENT_NEW_DESC, msg);
1447  tor_free(ids);
1448  tor_free(msg);
1449  SMARTLIST_FOREACH(names, char *, cp, tor_free(cp));
1450  smartlist_free(names);
1451  }
1452  return 0;
1453 }
1454 
1455 /** Called when an address mapping on <b>from</b> from changes to <b>to</b>.
1456  * <b>expires</b> values less than 3 are special; see connection_edge.c. If
1457  * <b>error</b> is non-NULL, it is an error code describing the failure
1458  * mode of the mapping.
1459  */
1460 int
1461 control_event_address_mapped(const char *from, const char *to, time_t expires,
1462  const char *error, const int cached)
1463 {
1464  if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP))
1465  return 0;
1466 
1467  if (expires < 3 || expires == TIME_MAX)
1468  send_control_event(EVENT_ADDRMAP,
1469  "650 ADDRMAP %s %s NEVER %s%s"
1470  "CACHED=\"%s\"\r\n",
1471  from, to, error?error:"", error?" ":"",
1472  cached?"YES":"NO");
1473  else {
1474  char buf[ISO_TIME_LEN+1];
1475  char buf2[ISO_TIME_LEN+1];
1476  format_local_iso_time(buf,expires);
1477  format_iso_time(buf2,expires);
1478  send_control_event(EVENT_ADDRMAP,
1479  "650 ADDRMAP %s %s \"%s\""
1480  " %s%sEXPIRES=\"%s\" CACHED=\"%s\"\r\n",
1481  from, to, buf,
1482  error?error:"", error?" ":"",
1483  buf2, cached?"YES":"NO");
1484  }
1485 
1486  return 0;
1487 }
1488 /** The network liveness has changed; this is called from circuitstats.c
1489  * whenever we receive a cell, or when timeout expires and we assume the
1490  * network is down. */
1491 int
1493 {
1494  if (liveness > 0) {
1495  if (get_cached_network_liveness() <= 0) {
1496  /* Update cached liveness */
1497  set_cached_network_liveness(1);
1498  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS UP");
1499  send_control_event_string(EVENT_NETWORK_LIVENESS,
1500  "650 NETWORK_LIVENESS UP\r\n");
1501  }
1502  /* else was already live, no-op */
1503  } else {
1504  if (get_cached_network_liveness() > 0) {
1505  /* Update cached liveness */
1506  set_cached_network_liveness(0);
1507  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS DOWN");
1508  send_control_event_string(EVENT_NETWORK_LIVENESS,
1509  "650 NETWORK_LIVENESS DOWN\r\n");
1510  }
1511  /* else was already dead, no-op */
1512  }
1513 
1514  return 0;
1515 }
1516 
1517 /** Helper function for NS-style events. Constructs and sends an event
1518  * of type <b>event</b> with string <b>event_string</b> out of the set of
1519  * networkstatuses <b>statuses</b>. Currently it is used for NS events
1520  * and NEWCONSENSUS events. */
1521 static int
1523  uint16_t event,
1524  const char *event_string)
1525 {
1526  smartlist_t *strs;
1527  char *s, *esc = NULL;
1528  if (!EVENT_IS_INTERESTING(event) || !smartlist_len(statuses))
1529  return 0;
1530 
1531  strs = smartlist_new();
1532  smartlist_add_strdup(strs, "650+");
1533  smartlist_add_strdup(strs, event_string);
1534  smartlist_add_strdup(strs, "\r\n");
1535  SMARTLIST_FOREACH(statuses, const routerstatus_t *, rs,
1536  {
1538  if (!s) continue;
1539  smartlist_add(strs, s);
1540  });
1541 
1542  s = smartlist_join_strings(strs, "", 0, NULL);
1543  write_escaped_data(s, strlen(s), &esc);
1544  SMARTLIST_FOREACH(strs, char *, cp, tor_free(cp));
1545  smartlist_free(strs);
1546  tor_free(s);
1547  send_control_event_string(event, esc);
1549  "650 OK\r\n");
1550 
1551  tor_free(esc);
1552  return 0;
1553 }
1554 
1555 /** Called when the routerstatus_ts <b>statuses</b> have changed: sends
1556  * an NS event to any controller that cares. */
1557 int
1559 {
1560  return control_event_networkstatus_changed_helper(statuses, EVENT_NS, "NS");
1561 }
1562 
1563 /** Called when we get a new consensus networkstatus. Sends a NEWCONSENSUS
1564  * event consisting of an NS-style line for each relay in the consensus. */
1565 int
1567 {
1568  if (!control_event_is_interesting(EVENT_NEWCONSENSUS))
1569  return 0;
1571  consensus->routerstatus_list, EVENT_NEWCONSENSUS, "NEWCONSENSUS");
1572 }
1573 
1574 /** Called when we compute a new circuitbuildtimeout */
1575 int
1577  const char *args)
1578 {
1579  const char *type_string = NULL;
1580 
1581  if (!control_event_is_interesting(EVENT_BUILDTIMEOUT_SET))
1582  return 0;
1583 
1584  switch (type) {
1585  case BUILDTIMEOUT_SET_EVENT_COMPUTED:
1586  type_string = "COMPUTED";
1587  break;
1588  case BUILDTIMEOUT_SET_EVENT_RESET:
1589  type_string = "RESET";
1590  break;
1591  case BUILDTIMEOUT_SET_EVENT_SUSPENDED:
1592  type_string = "SUSPENDED";
1593  break;
1594  case BUILDTIMEOUT_SET_EVENT_DISCARD:
1595  type_string = "DISCARD";
1596  break;
1597  case BUILDTIMEOUT_SET_EVENT_RESUME:
1598  type_string = "RESUME";
1599  break;
1600  default:
1601  type_string = "UNKNOWN";
1602  break;
1603  }
1604 
1605  send_control_event(EVENT_BUILDTIMEOUT_SET,
1606  "650 BUILDTIMEOUT_SET %s %s\r\n",
1607  type_string, args);
1608 
1609  return 0;
1610 }
1611 
1612 /** Called when a signal has been processed from signal_callback */
1613 int
1614 control_event_signal(uintptr_t signal_num)
1615 {
1616  const char *signal_string = NULL;
1617 
1618  if (!control_event_is_interesting(EVENT_GOT_SIGNAL))
1619  return 0;
1620 
1621  for (unsigned i = 0; signal_table[i].signal_name != NULL; ++i) {
1622  if ((int)signal_num == signal_table[i].sig) {
1623  signal_string = signal_table[i].signal_name;
1624  break;
1625  }
1626  }
1627 
1628  if (signal_string == NULL) {
1629  log_warn(LD_BUG, "Unrecognized signal %lu in control_event_signal",
1630  (unsigned long)signal_num);
1631  return -1;
1632  }
1633 
1634  send_control_event(EVENT_GOT_SIGNAL, "650 SIGNAL %s\r\n",
1635  signal_string);
1636  return 0;
1637 }
1638 
1639 /** Called when a single local_routerstatus_t has changed: Sends an NS event
1640  * to any controller that cares. */
1641 int
1643 {
1644  smartlist_t *statuses;
1645  int r;
1646 
1647  if (!EVENT_IS_INTERESTING(EVENT_NS))
1648  return 0;
1649 
1650  statuses = smartlist_new();
1651  smartlist_add(statuses, (void*)rs);
1653  smartlist_free(statuses);
1654  return r;
1655 }
1656 
1657 /** Our own router descriptor has changed; tell any controllers that care.
1658  */
1659 int
1661 {
1662  send_control_event(EVENT_DESCCHANGED, "650 DESCCHANGED\r\n");
1663  return 0;
1664 }
1665 
1666 /** Helper: sends a status event where <b>type</b> is one of
1667  * EVENT_STATUS_{GENERAL,CLIENT,SERVER}, where <b>severity</b> is one of
1668  * LOG_{NOTICE,WARN,ERR}, and where <b>format</b> is a printf-style format
1669  * string corresponding to <b>args</b>. */
1670 static int
1671 control_event_status(int type, int severity, const char *format, va_list args)
1672 {
1673  char *user_buf = NULL;
1674  char format_buf[160];
1675  const char *status, *sev;
1676 
1677  switch (type) {
1678  case EVENT_STATUS_GENERAL:
1679  status = "STATUS_GENERAL";
1680  break;
1681  case EVENT_STATUS_CLIENT:
1682  status = "STATUS_CLIENT";
1683  break;
1684  case EVENT_STATUS_SERVER:
1685  status = "STATUS_SERVER";
1686  break;
1687  default:
1688  log_warn(LD_BUG, "Unrecognized status type %d", type);
1689  return -1;
1690  }
1691  switch (severity) {
1692  case LOG_NOTICE:
1693  sev = "NOTICE";
1694  break;
1695  case LOG_WARN:
1696  sev = "WARN";
1697  break;
1698  case LOG_ERR:
1699  sev = "ERR";
1700  break;
1701  default:
1702  log_warn(LD_BUG, "Unrecognized status severity %d", severity);
1703  return -1;
1704  }
1705  if (tor_snprintf(format_buf, sizeof(format_buf), "650 %s %s",
1706  status, sev)<0) {
1707  log_warn(LD_BUG, "Format string too long.");
1708  return -1;
1709  }
1710  if (tor_vasprintf(&user_buf, format, args)<0) {
1711  log_warn(LD_BUG, "Failed to create user buffer.");
1712  return -1;
1713  }
1714 
1715  send_control_event(type, "%s %s\r\n", format_buf, user_buf);
1716  tor_free(user_buf);
1717  return 0;
1718 }
1719 
1720 #ifndef COCCI
1721 #define CONTROL_EVENT_STATUS_BODY(event, sev) \
1722  int r; \
1723  do { \
1724  va_list ap; \
1725  if (!EVENT_IS_INTERESTING(event)) \
1726  return 0; \
1727  \
1728  va_start(ap, format); \
1729  r = control_event_status((event), (sev), format, ap); \
1730  va_end(ap); \
1731  } while (0)
1732 #endif /* !defined(COCCI) */
1733 
1734 /** Format and send an EVENT_STATUS_GENERAL event whose main text is obtained
1735  * by formatting the arguments using the printf-style <b>format</b>. */
1736 int
1737 control_event_general_status(int severity, const char *format, ...)
1738 {
1739  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, severity);
1740  return r;
1741 }
1742 
1743 /** Format and send an EVENT_STATUS_GENERAL LOG_ERR event, and flush it to the
1744  * controller(s) immediately. */
1745 int
1746 control_event_general_error(const char *format, ...)
1747 {
1748  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, LOG_ERR);
1749  /* Force a flush, since we may be about to die horribly */
1751  return r;
1752 }
1753 
1754 /** Format and send an EVENT_STATUS_CLIENT event whose main text is obtained
1755  * by formatting the arguments using the printf-style <b>format</b>. */
1756 int
1757 control_event_client_status(int severity, const char *format, ...)
1758 {
1759  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, severity);
1760  return r;
1761 }
1762 
1763 /** Format and send an EVENT_STATUS_CLIENT LOG_ERR event, and flush it to the
1764  * controller(s) immediately. */
1765 int
1766 control_event_client_error(const char *format, ...)
1767 {
1768  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, LOG_ERR);
1769  /* Force a flush, since we may be about to die horribly */
1771  return r;
1772 }
1773 
1774 /** Format and send an EVENT_STATUS_SERVER event whose main text is obtained
1775  * by formatting the arguments using the printf-style <b>format</b>. */
1776 int
1777 control_event_server_status(int severity, const char *format, ...)
1778 {
1779  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, severity);
1780  return r;
1781 }
1782 
1783 /** Format and send an EVENT_STATUS_SERVER LOG_ERR event, and flush it to the
1784  * controller(s) immediately. */
1785 int
1786 control_event_server_error(const char *format, ...)
1787 {
1788  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, LOG_ERR);
1789  /* Force a flush, since we may be about to die horribly */
1791  return r;
1792 }
1793 
1794 /** Called when the status of an entry guard with the given <b>nickname</b>
1795  * and identity <b>digest</b> has changed to <b>status</b>: tells any
1796  * controllers that care. */
1797 int
1798 control_event_guard(const char *nickname, const char *digest,
1799  const char *status)
1800 {
1801  char hbuf[HEX_DIGEST_LEN+1];
1802  base16_encode(hbuf, sizeof(hbuf), digest, DIGEST_LEN);
1803  if (!EVENT_IS_INTERESTING(EVENT_GUARD))
1804  return 0;
1805 
1806  {
1807  char buf[MAX_VERBOSE_NICKNAME_LEN+1];
1808  const node_t *node = node_get_by_id(digest);
1809  if (node) {
1810  node_get_verbose_nickname(node, buf);
1811  } else {
1812  tor_snprintf(buf, sizeof(buf), "$%s~%s", hbuf, nickname);
1813  }
1814  send_control_event(EVENT_GUARD,
1815  "650 GUARD ENTRY %s %s\r\n", buf, status);
1816  }
1817  return 0;
1818 }
1819 
1820 /** Called when a configuration option changes. This is generally triggered
1821  * by SETCONF requests and RELOAD/SIGHUP signals. The <b>changes</b> are
1822  * a linked list of configuration key-values.
1823  * <b>changes</b> can be NULL, meaning "no changes".
1824  */
1825 void
1827 {
1828  char *result;
1829  smartlist_t *lines;
1830  if (!EVENT_IS_INTERESTING(EVENT_CONF_CHANGED) || !changes) {
1831  return;
1832  }
1833  lines = smartlist_new();
1834  for (const config_line_t *line = changes; line; line = line->next) {
1835  if (line->value == NULL) {
1836  smartlist_add_asprintf(lines, "650-%s", line->key);
1837  } else {
1838  smartlist_add_asprintf(lines, "650-%s=%s", line->key, line->value);
1839  }
1840  }
1841  result = smartlist_join_strings(lines, "\r\n", 0, NULL);
1842  send_control_event(EVENT_CONF_CHANGED,
1843  "650-CONF_CHANGED\r\n%s\r\n650 OK\r\n", result);
1844  tor_free(result);
1845  SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1846  smartlist_free(lines);
1847 }
1848 
1849 /** We just generated a new summary of which countries we've seen clients
1850  * from recently. Send a copy to the controller in case it wants to
1851  * display it for the user. */
1852 void
1853 control_event_clients_seen(const char *controller_str)
1854 {
1855  send_control_event(EVENT_CLIENTS_SEEN,
1856  "650 CLIENTS_SEEN %s\r\n", controller_str);
1857 }
1858 
1859 /** A new pluggable transport called <b>transport_name</b> was
1860  * launched on <b>addr</b>:<b>port</b>. <b>mode</b> is either
1861  * "server" or "client" depending on the mode of the pluggable
1862  * transport.
1863  * "650" SP "TRANSPORT_LAUNCHED" SP Mode SP Name SP Address SP Port
1864  */
1865 void
1866 control_event_transport_launched(const char *mode, const char *transport_name,
1867  tor_addr_t *addr, uint16_t port)
1868 {
1869  send_control_event(EVENT_TRANSPORT_LAUNCHED,
1870  "650 TRANSPORT_LAUNCHED %s %s %s %u\r\n",
1871  mode, transport_name, fmt_addr(addr), port);
1872 }
1873 
1874 /** A pluggable transport called <b>pt_name</b> has emitted a log message
1875  * found in <b>message</b> at <b>severity</b> log level. */
1876 void
1877 control_event_pt_log(const char *log)
1878 {
1879  send_control_event(EVENT_PT_LOG,
1880  "650 PT_LOG %s\r\n",
1881  log);
1882 }
1883 
1884 /** A pluggable transport has emitted a STATUS message found in
1885  * <b>status</b>. */
1886 void
1887 control_event_pt_status(const char *status)
1888 {
1889  send_control_event(EVENT_PT_STATUS,
1890  "650 PT_STATUS %s\r\n",
1891  status);
1892 }
1893 
1894 /** Convert rendezvous auth type to string for HS_DESC control events
1895  */
1896 const char *
1898 {
1899  const char *str;
1900 
1901  switch (auth_type) {
1902  case REND_NO_AUTH:
1903  str = "NO_AUTH";
1904  break;
1905  case REND_BASIC_AUTH:
1906  str = "BASIC_AUTH";
1907  break;
1908  case REND_STEALTH_AUTH:
1909  str = "STEALTH_AUTH";
1910  break;
1911  default:
1912  str = "UNKNOWN";
1913  }
1914 
1915  return str;
1916 }
1917 
1918 /** Return either the onion address if the given pointer is a non empty
1919  * string else the unknown string. */
1920 static const char *
1921 rend_hsaddress_str_or_unknown(const char *onion_address)
1922 {
1923  static const char *str_unknown = "UNKNOWN";
1924  const char *str_ret = str_unknown;
1925 
1926  /* No valid pointer, unknown it is. */
1927  if (!onion_address) {
1928  goto end;
1929  }
1930  /* Empty onion address thus we don't know, unknown it is. */
1931  if (onion_address[0] == '\0') {
1932  goto end;
1933  }
1934  /* All checks are good so return the given onion address. */
1935  str_ret = onion_address;
1936 
1937  end:
1938  return str_ret;
1939 }
1940 
1941 /** send HS_DESC requested event.
1942  *
1943  * <b>rend_query</b> is used to fetch requested onion address and auth type.
1944  * <b>hs_dir</b> is the description of contacting hs directory.
1945  * <b>desc_id_base32</b> is the ID of requested hs descriptor.
1946  * <b>hsdir_index</b> is the HSDir fetch index value for v3, an hex string.
1947  */
1948 void
1949 control_event_hs_descriptor_requested(const char *onion_address,
1950  rend_auth_type_t auth_type,
1951  const char *id_digest,
1952  const char *desc_id,
1953  const char *hsdir_index)
1954 {
1955  char *hsdir_index_field = NULL;
1956 
1957  if (BUG(!id_digest || !desc_id)) {
1958  return;
1959  }
1960 
1961  if (hsdir_index) {
1962  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
1963  }
1964 
1965  send_control_event(EVENT_HS_DESC,
1966  "650 HS_DESC REQUESTED %s %s %s %s%s\r\n",
1967  rend_hsaddress_str_or_unknown(onion_address),
1968  rend_auth_type_to_string(auth_type),
1969  node_describe_longname_by_id(id_digest),
1970  desc_id,
1971  hsdir_index_field ? hsdir_index_field : "");
1972  tor_free(hsdir_index_field);
1973 }
1974 
1975 /** send HS_DESC CREATED event when a local service generates a descriptor.
1976  *
1977  * <b>onion_address</b> is service address.
1978  * <b>desc_id</b> is the descriptor ID.
1979  * <b>replica</b> is the the descriptor replica number. If it is negative, it
1980  * is ignored.
1981  */
1982 void
1983 control_event_hs_descriptor_created(const char *onion_address,
1984  const char *desc_id,
1985  int replica)
1986 {
1987  char *replica_field = NULL;
1988 
1989  if (BUG(!onion_address || !desc_id)) {
1990  return;
1991  }
1992 
1993  if (replica >= 0) {
1994  tor_asprintf(&replica_field, " REPLICA=%d", replica);
1995  }
1996 
1997  send_control_event(EVENT_HS_DESC,
1998  "650 HS_DESC CREATED %s UNKNOWN UNKNOWN %s%s\r\n",
1999  onion_address, desc_id,
2000  replica_field ? replica_field : "");
2001  tor_free(replica_field);
2002 }
2003 
2004 /** send HS_DESC upload event.
2005  *
2006  * <b>onion_address</b> is service address.
2007  * <b>hs_dir</b> is the description of contacting hs directory.
2008  * <b>desc_id</b> is the ID of requested hs descriptor.
2009  */
2010 void
2011 control_event_hs_descriptor_upload(const char *onion_address,
2012  const char *id_digest,
2013  const char *desc_id,
2014  const char *hsdir_index)
2015 {
2016  char *hsdir_index_field = NULL;
2017 
2018  if (BUG(!onion_address || !id_digest || !desc_id)) {
2019  return;
2020  }
2021 
2022  if (hsdir_index) {
2023  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
2024  }
2025 
2026  send_control_event(EVENT_HS_DESC,
2027  "650 HS_DESC UPLOAD %s UNKNOWN %s %s%s\r\n",
2028  onion_address,
2029  node_describe_longname_by_id(id_digest),
2030  desc_id,
2031  hsdir_index_field ? hsdir_index_field : "");
2032  tor_free(hsdir_index_field);
2033 }
2034 
2035 /** send HS_DESC event after got response from hs directory.
2036  *
2037  * NOTE: this is an internal function used by following functions:
2038  * control_event_hsv2_descriptor_received
2039  * control_event_hsv2_descriptor_failed
2040  * control_event_hsv3_descriptor_failed
2041  *
2042  * So do not call this function directly.
2043  */
2044 static void
2046  const char *onion_address,
2047  const char *desc_id,
2048  rend_auth_type_t auth_type,
2049  const char *hsdir_id_digest,
2050  const char *reason)
2051 {
2052  char *reason_field = NULL;
2053 
2054  if (BUG(!action || !onion_address)) {
2055  return;
2056  }
2057 
2058  if (reason) {
2059  tor_asprintf(&reason_field, " REASON=%s", reason);
2060  }
2061 
2062  send_control_event(EVENT_HS_DESC,
2063  "650 HS_DESC %s %s %s %s%s%s\r\n",
2064  action,
2065  rend_hsaddress_str_or_unknown(onion_address),
2066  rend_auth_type_to_string(auth_type),
2067  hsdir_id_digest ?
2068  node_describe_longname_by_id(hsdir_id_digest) :
2069  "UNKNOWN",
2070  desc_id ? desc_id : "",
2071  reason_field ? reason_field : "");
2072 
2073  tor_free(reason_field);
2074 }
2075 
2076 /** send HS_DESC event after got response from hs directory.
2077  *
2078  * NOTE: this is an internal function used by following functions:
2079  * control_event_hs_descriptor_uploaded
2080  * control_event_hs_descriptor_upload_failed
2081  *
2082  * So do not call this function directly.
2083  */
2084 void
2086  const char *onion_address,
2087  const char *id_digest,
2088  const char *reason)
2089 {
2090  char *reason_field = NULL;
2091 
2092  if (BUG(!action || !id_digest)) {
2093  return;
2094  }
2095 
2096  if (reason) {
2097  tor_asprintf(&reason_field, " REASON=%s", reason);
2098  }
2099 
2100  send_control_event(EVENT_HS_DESC,
2101  "650 HS_DESC %s %s UNKNOWN %s%s\r\n",
2102  action,
2103  rend_hsaddress_str_or_unknown(onion_address),
2104  node_describe_longname_by_id(id_digest),
2105  reason_field ? reason_field : "");
2106 
2107  tor_free(reason_field);
2108 }
2109 
2110 /** For an HS descriptor query <b>rend_data</b>, using the
2111  * <b>onion_address</b> and HSDir fingerprint <b>hsdir_fp</b>, find out
2112  * which descriptor ID in the query is the right one.
2113  *
2114  * Return a pointer of the binary descriptor ID found in the query's object
2115  * or NULL if not found. */
2116 static const char *
2117 get_desc_id_from_query(const rend_data_t *rend_data, const char *hsdir_fp)
2118 {
2119  int replica;
2120  const char *desc_id = NULL;
2121  const rend_data_v2_t *rend_data_v2 = TO_REND_DATA_V2(rend_data);
2122 
2123  /* Possible if the fetch was done using a descriptor ID. This means that
2124  * the HSFETCH command was used. */
2125  if (!tor_digest_is_zero(rend_data_v2->desc_id_fetch)) {
2126  desc_id = rend_data_v2->desc_id_fetch;
2127  goto end;
2128  }
2129 
2130  /* Without a directory fingerprint at this stage, we can't do much. */
2131  if (hsdir_fp == NULL) {
2132  goto end;
2133  }
2134 
2135  /* OK, we have an onion address so now let's find which descriptor ID
2136  * is the one associated with the HSDir fingerprint. */
2137  for (replica = 0; replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
2138  replica++) {
2139  const char *digest = rend_data_get_desc_id(rend_data, replica, NULL);
2140 
2141  SMARTLIST_FOREACH_BEGIN(rend_data->hsdirs_fp, char *, fingerprint) {
2142  if (tor_memcmp(fingerprint, hsdir_fp, DIGEST_LEN) == 0) {
2143  /* Found it! This descriptor ID is the right one. */
2144  desc_id = digest;
2145  goto end;
2146  }
2147  } SMARTLIST_FOREACH_END(fingerprint);
2148  }
2149 
2150  end:
2151  return desc_id;
2152 }
2153 
2154 /** send HS_DESC RECEIVED event
2155  *
2156  * called when we successfully received a hidden service descriptor.
2157  */
2158 void
2159 control_event_hsv2_descriptor_received(const char *onion_address,
2160  const rend_data_t *rend_data,
2161  const char *hsdir_id_digest)
2162 {
2163  char *desc_id_field = NULL;
2164  const char *desc_id;
2165 
2166  if (BUG(!rend_data || !hsdir_id_digest || !onion_address)) {
2167  return;
2168  }
2169 
2170  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2171  if (desc_id != NULL) {
2172  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2173  /* Set the descriptor ID digest to base32 so we can send it. */
2174  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2175  DIGEST_LEN);
2176  /* Extra whitespace is needed before the value. */
2177  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2178  }
2179 
2180  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2181  TO_REND_DATA_V2(rend_data)->auth_type,
2182  hsdir_id_digest, NULL);
2183  tor_free(desc_id_field);
2184 }
2185 
2186 /* Send HS_DESC RECEIVED event
2187  *
2188  * Called when we successfully received a hidden service descriptor. */
2189 void
2190 control_event_hsv3_descriptor_received(const char *onion_address,
2191  const char *desc_id,
2192  const char *hsdir_id_digest)
2193 {
2194  char *desc_id_field = NULL;
2195 
2196  if (BUG(!onion_address || !desc_id || !hsdir_id_digest)) {
2197  return;
2198  }
2199 
2200  /* Because DescriptorID is an optional positional value, we need to add a
2201  * whitespace before in order to not be next to the HsDir value. */
2202  tor_asprintf(&desc_id_field, " %s", desc_id);
2203 
2204  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2205  REND_NO_AUTH, hsdir_id_digest, NULL);
2206  tor_free(desc_id_field);
2207 }
2208 
2209 /** send HS_DESC UPLOADED event
2210  *
2211  * called when we successfully uploaded a hidden service descriptor.
2212  */
2213 void
2215  const char *onion_address)
2216 {
2217  if (BUG(!id_digest)) {
2218  return;
2219  }
2220 
2221  control_event_hs_descriptor_upload_end("UPLOADED", onion_address,
2222  id_digest, NULL);
2223 }
2224 
2225 /** Send HS_DESC event to inform controller that query <b>rend_data</b>
2226  * failed to retrieve hidden service descriptor from directory identified by
2227  * <b>id_digest</b>. If NULL, "UNKNOWN" is used. If <b>reason</b> is not NULL,
2228  * add it to REASON= field.
2229  */
2230 void
2232  const char *hsdir_id_digest,
2233  const char *reason)
2234 {
2235  char *desc_id_field = NULL;
2236  const char *desc_id;
2237 
2238  if (BUG(!rend_data)) {
2239  return;
2240  }
2241 
2242  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2243  if (desc_id != NULL) {
2244  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2245  /* Set the descriptor ID digest to base32 so we can send it. */
2246  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2247  DIGEST_LEN);
2248  /* Extra whitespace is needed before the value. */
2249  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2250  }
2251 
2253  desc_id_field,
2254  TO_REND_DATA_V2(rend_data)->auth_type,
2255  hsdir_id_digest, reason);
2256  tor_free(desc_id_field);
2257 }
2258 
2259 /** Send HS_DESC event to inform controller that the query to
2260  * <b>onion_address</b> failed to retrieve hidden service descriptor
2261  * <b>desc_id</b> from directory identified by <b>hsdir_id_digest</b>. If
2262  * NULL, "UNKNOWN" is used. If <b>reason</b> is not NULL, add it to REASON=
2263  * field. */
2264 void
2265 control_event_hsv3_descriptor_failed(const char *onion_address,
2266  const char *desc_id,
2267  const char *hsdir_id_digest,
2268  const char *reason)
2269 {
2270  char *desc_id_field = NULL;
2271 
2272  if (BUG(!onion_address || !desc_id || !reason)) {
2273  return;
2274  }
2275 
2276  /* Because DescriptorID is an optional positional value, we need to add a
2277  * whitespace before in order to not be next to the HsDir value. */
2278  tor_asprintf(&desc_id_field, " %s", desc_id);
2279 
2280  event_hs_descriptor_receive_end("FAILED", onion_address, desc_id_field,
2281  REND_NO_AUTH, hsdir_id_digest, reason);
2282  tor_free(desc_id_field);
2283 }
2284 
2285 /** Send HS_DESC_CONTENT event after completion of a successful fetch
2286  * from hs directory. If <b>hsdir_id_digest</b> is NULL, it is replaced
2287  * by "UNKNOWN". If <b>content</b> is NULL, it is replaced by an empty
2288  * string. The <b>onion_address</b> or <b>desc_id</b> set to NULL will
2289  * not trigger the control event. */
2290 void
2291 control_event_hs_descriptor_content(const char *onion_address,
2292  const char *desc_id,
2293  const char *hsdir_id_digest,
2294  const char *content)
2295 {
2296  static const char *event_name = "HS_DESC_CONTENT";
2297  char *esc_content = NULL;
2298 
2299  if (!onion_address || !desc_id) {
2300  log_warn(LD_BUG, "Called with onion_address==%p, desc_id==%p, ",
2301  onion_address, desc_id);
2302  return;
2303  }
2304 
2305  if (content == NULL) {
2306  /* Point it to empty content so it can still be escaped. */
2307  content = "";
2308  }
2309  write_escaped_data(content, strlen(content), &esc_content);
2310 
2311  send_control_event(EVENT_HS_DESC_CONTENT,
2312  "650+%s %s %s %s\r\n%s650 OK\r\n",
2313  event_name,
2314  rend_hsaddress_str_or_unknown(onion_address),
2315  desc_id,
2316  hsdir_id_digest ?
2317  node_describe_longname_by_id(hsdir_id_digest) :
2318  "UNKNOWN",
2319  esc_content);
2320  tor_free(esc_content);
2321 }
2322 
2323 /** Send HS_DESC event to inform controller upload of hidden service
2324  * descriptor identified by <b>id_digest</b> failed. If <b>reason</b>
2325  * is not NULL, add it to REASON= field.
2326  */
2327 void
2329  const char *onion_address,
2330  const char *reason)
2331 {
2332  if (BUG(!id_digest)) {
2333  return;
2334  }
2335  control_event_hs_descriptor_upload_end("FAILED", onion_address,
2336  id_digest, reason);
2337 }
2338 
2339 void
2340 control_events_free_all(void)
2341 {
2342  smartlist_t *queued_events = NULL;
2343 
2344  stats_prev_n_read = stats_prev_n_written = 0;
2345 
2349  queued_events = queued_control_events;
2350  queued_control_events = NULL;
2352  }
2353  if (queued_events) {
2354  SMARTLIST_FOREACH(queued_events, queued_event_t *, ev,
2355  queued_event_free(ev));
2356  smartlist_free(queued_events);
2357  }
2359  mainloop_event_free(flush_queued_events_event);
2361  }
2362  global_event_mask = 0;
2364 }
2365 
2366 #ifdef TOR_UNIT_TESTS
2367 /* For testing: change the value of global_event_mask */
2368 void
2369 control_testing_set_global_event_mask(uint64_t mask)
2370 {
2371  global_event_mask = mask;
2372 }
2373 #endif /* defined(TOR_UNIT_TESTS) */
send_control_event_string
STATIC void send_control_event_string(uint16_t event, const char *msg)
Definition: control_events.c:589
format_iso_time_nospace_usec
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
Definition: time_fmt.c:323
control_event_my_descriptor_changed
int control_event_my_descriptor_changed(void)
Definition: control_events.c:1660
control_event_stream_bandwidth
int control_event_stream_bandwidth(edge_connection_t *edge_conn)
Definition: control_events.c:969
tor_free
#define tor_free(p)
Definition: malloc.h:52
routerinfo_t
Definition: routerinfo_st.h:20
connection_edge.h
Header file for connection_edge.c.
circuit_build_times_t::num_circ_timeouts
uint32_t num_circ_timeouts
Definition: circuitstats.h:204
send_control_event
static void send_control_event(uint16_t event, const char *format,...)
Definition: control_events.c:619
testing_cell_stats_entry_t
Definition: or.h:937
LOG_DEBUG
#define LOG_DEBUG
Definition: log.h:42
circuit_build_times_t::Xm
build_time_t Xm
Definition: circuitstats.h:189
append_cell_stats_by_command
void append_cell_stats_by_command(smartlist_t *event_parts, const char *key, const uint64_t *include_if_non_zero, const uint64_t *number_to_include)
Definition: control_events.c:1180
stats_n_bytes_read
static uint64_t stats_n_bytes_read
Definition: mainloop.c:139
name
const char * name
Definition: config.c:2441
control_event_hs_descriptor_upload_end
void control_event_hs_descriptor_upload_end(const char *action, const char *onion_address, const char *id_digest, const char *reason)
Definition: control_events.c:2085
circuit_describe_status_for_controller
char * circuit_describe_status_for_controller(origin_circuit_t *circ)
Definition: control_fmt.c:73
connection_t::purpose
unsigned int purpose
Definition: connection_st.h:51
entry_connection_st.h
Entry connection structure.
cell_command_to_string
const char * cell_command_to_string(uint8_t command)
Definition: command.c:89
stream_status_event_t
stream_status_event_t
Definition: control_events.h:30
tor_addr_t
Definition: address.h:68
origin_circuit_t::n_overhead_written_circ_bw
uint32_t n_overhead_written_circ_bw
Definition: origin_circuit_st.h:118
smartlist_add_strdup
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
Definition: smartlist_core.c:137
rend_data_v2_t::desc_id_fetch
char desc_id_fetch[DIGEST_LEN]
Definition: or.h:455
LOG_NOTICE
#define LOG_NOTICE
Definition: log.h:50
format_cell_stats
void format_cell_stats(char **event_string, circuit_t *circ, cell_stats_t *cell_stats)
Definition: control_events.c:1205
MOCK_IMPL
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
#define REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
Definition: or.h:349
circuit_get_global_list
smartlist_t * circuit_get_global_list(void)
Definition: circuitlist.c:692
control_event_stream_bandwidth_used
int control_event_stream_bandwidth_used(void)
Definition: control_events.c:995
get_bytes_read
uint64_t get_bytes_read(void)
Definition: mainloop.c:464
END_STREAM_REASON_FLAG_REMOTE
#define END_STREAM_REASON_FLAG_REMOTE
Definition: or.h:277
circuit_build_times_quantile_cutoff
double circuit_build_times_quantile_cutoff(void)
Definition: circuitstats.c:268
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
control.h
Header file for control.c.
CONN_TYPE_DIR
#define CONN_TYPE_DIR
Definition: connection.h:53
LD_BUG
#define LD_BUG
Definition: log.h:86
control_per_second_events
void control_per_second_events(void)
Definition: control_events.c:363
ANY_EVENT_IS_INTERESTING
#define ANY_EVENT_IS_INTERESTING(e)
Definition: control_events.c:64
format_iso_time
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
control_event_or_conn_status
int control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp, int reason)
Definition: control_events.c:921
edge_connection_t::n_read
uint32_t n_read
Definition: edge_connection_st.h:57
control_event_circuit_status
int control_event_circuit_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Definition: control_events.c:631
base16_encode
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
event_hs_descriptor_receive_end
static void event_hs_descriptor_receive_end(const char *action, const char *onion_address, const char *desc_id, rend_auth_type_t auth_type, const char *hsdir_id_digest, const char *reason)
Definition: control_events.c:2045
tor_fragile_assert
#define tor_fragile_assert()
Definition: util_bug.h:259
control_event_logmsg
void control_event_logmsg(int severity, log_domain_mask_t domain, const char *msg)
Definition: control_events.c:1356
rend_auth_type_t
rend_auth_type_t
Definition: or.h:403
flush_pending_log_callbacks
void flush_pending_log_callbacks(void)
Definition: log.c:1055
control_proto.h
Header file for control_proto.c.
describe.h
Header file for describe.c.
control_event_address_mapped
int control_event_address_mapped(const char *from, const char *to, time_t expires, const char *error, const int cached)
Definition: control_events.c:1461
send_control_event_impl
static void send_control_event_impl(uint16_t event, const char *format, va_list ap)
Definition: control_events.c:600
control_any_per_second_event_enabled
int control_any_per_second_event_enabled(void)
Definition: control_events.c:321
queued_control_events_lock
static tor_mutex_t * queued_control_events_lock
Definition: control_events.c:397
origin_circuit_t::n_written_circ_bw
uint32_t n_written_circ_bw
Definition: origin_circuit_st.h:98
CONN_TYPE_CONTROL
#define CONN_TYPE_CONTROL
Definition: connection.h:58
node_get_by_id
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
event_to_log_severity
static int event_to_log_severity(int event)
Definition: control_events.c:261
control_event_pt_log
void control_event_pt_log(const char *log)
Definition: control_events.c:1877
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
TO_CONTROL_CONN
control_connection_t * TO_CONTROL_CONN(connection_t *c)
Definition: control.c:67
circuit_build_times_t::close_ms
double close_ms
Definition: circuitstats.h:198
circuit_t::testing_cell_stats
smartlist_t * testing_cell_stats
Definition: circuit_st.h:212
routerstatus_t
Definition: routerstatus_st.h:19
END_CIRC_REASON_FLAG_REMOTE
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:329
networkstatus_getinfo_helper_single
char * networkstatus_getinfo_helper_single(const routerstatus_t *rs)
Definition: networkstatus.c:2356
circuit_t::n_chan
channel_t * n_chan
Definition: circuit_st.h:69
control_event_hsv2_descriptor_received
void control_event_hsv2_descriptor_received(const char *onion_address, const rend_data_t *rend_data, const char *hsdir_id_digest)
Definition: control_events.c:2159
rend_auth_type_to_string
const char * rend_auth_type_to_string(rend_auth_type_t auth_type)
Definition: control_events.c:1897
control_event_circuit_status_minor
static int control_event_circuit_status_minor(origin_circuit_t *circ, circuit_status_minor_event_t e, int purpose, const struct timeval *tv)
Definition: control_events.c:688
control_event_hs_descriptor_content
void control_event_hs_descriptor_content(const char *onion_address, const char *desc_id, const char *hsdir_id_digest, const char *content)
Definition: control_events.c:2291
router_get_verbose_nickname
void router_get_verbose_nickname(char *buf, const routerinfo_t *router)
Definition: describe.c:232
control_event_clients_seen
void control_event_clients_seen(const char *controller_str)
Definition: control_events.c:1853
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
reasons.h
Header file for reasons.c.
CONTROL_CONN_STATE_OPEN
#define CONTROL_CONN_STATE_OPEN
Definition: control.h:19
block_event_queue_flag
static tor_threadlocal_t block_event_queue_flag
Definition: control_events.c:387
or_connection_t
Definition: or_connection_st.h:22
tor_snprintf
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
CIRCUIT_IS_ORIGIN
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:146
connection_t::n_read_conn_bw
uint32_t n_read_conn_bw
Definition: connection_st.h:139
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
EVENT_IS_INTERESTING
#define EVENT_IS_INTERESTING(e)
Definition: control_events.c:60
control_event_logmsg_pending
void control_event_logmsg_pending(void)
Definition: control_events.c:1411
circuit_build_times_t::num_circ_succeeded
uint32_t num_circ_succeeded
Definition: circuitstats.h:201
networkstatus.h
Header file for networkstatus.c.
origin_circuit_t::global_identifier
uint32_t global_identifier
Definition: origin_circuit_st.h:240
circuit_build_times_t::num_circ_closed
uint32_t num_circ_closed
Definition: circuitstats.h:207
control_event_hsv3_descriptor_failed
void control_event_hsv3_descriptor_failed(const char *onion_address, const char *desc_id, const char *hsdir_id_digest, const char *reason)
Definition: control_events.c:2265
control_event_circuit_cannibalized
int control_event_circuit_cannibalized(origin_circuit_t *circ, int old_purpose, const struct timeval *old_tv_created)
Definition: control_events.c:775
or_connection_t::chan
channel_tls_t * chan
Definition: or_connection_st.h:50
base32_encode
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:60
REND_DESC_ID_V2_LEN_BASE32
#define REND_DESC_ID_V2_LEN_BASE32
Definition: or.h:355
get_bytes_written
uint64_t get_bytes_written(void)
Definition: mainloop.c:474
rend_hsaddress_str_or_unknown
static const char * rend_hsaddress_str_or_unknown(const char *onion_address)
Definition: control_events.c:1921
circuit_status_event_t
circuit_status_event_t
Definition: ocirc_event.h:19
ENTRY_TO_EDGE_CONN
#define ENTRY_TO_EDGE_CONN(c)
Definition: entry_connection_st.h:102
or_circuit_t::p_chan
channel_t * p_chan
Definition: or_circuit_st.h:37
circuitlist.h
Header file for circuitlist.c.
mainloop.h
Header file for mainloop.c.
control_event_circuit_purpose_changed
int control_event_circuit_purpose_changed(origin_circuit_t *circ, int old_purpose)
Definition: control_events.c:761
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
CONN_TYPE_AP
#define CONN_TYPE_AP
Definition: connection.h:49
control_event_hs_descriptor_requested
void control_event_hs_descriptor_requested(const char *onion_address, rend_auth_type_t auth_type, const char *id_digest, const char *desc_id, const char *hsdir_index)
Definition: control_events.c:1949
node_describe_longname_by_id
const char * node_describe_longname_by_id(const char *id_digest)
Definition: control_fmt.c:269
control_event_circ_bandwidth_used_for_circ
int control_event_circ_bandwidth_used_for_circ(origin_circuit_t *ocirc)
Definition: control_events.c:1056
circuit_end_reason_to_control_string
const char * circuit_end_reason_to_control_string(int reason)
Definition: reasons.c:328
circuit_build_times_t
Definition: circuitstats.h:177
tor_mutex_t
Definition: compat_mutex.h:40
command.h
Header file for command.c.
control_event_transport_launched
void control_event_transport_launched(const char *mode, const char *transport_name, tor_addr_t *addr, uint16_t port)
Definition: control_events.c:1866
control_event_client_status
int control_event_client_status(int severity, const char *format,...)
Definition: control_events.c:1757
MAX_VERBOSE_NICKNAME_LEN
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:118
write_escaped_data
size_t write_escaped_data(const char *data, size_t len, char **out)
Definition: control_proto.c:71
control_event_stream_status
int control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp, int reason_code)
Definition: control_events.c:788
control_event_table
const struct control_event_t control_event_table[]
Definition: control_events.c:80
node_t
Definition: node_st.h:34
origin_circuit_t
Definition: origin_circuit_st.h:79
LD_CONTROL
#define LD_CONTROL
Definition: log.h:80
control_event_conn_bandwidth_used
int control_event_conn_bandwidth_used(void)
Definition: control_events.c:1134
control_event_server_error
int control_event_server_error(const char *format,...)
Definition: control_events.c:1786
control_event_server_status
int control_event_server_status(int severity, const char *format,...)
Definition: control_events.c:1777
control_event_is_interesting
int control_event_is_interesting(int event)
Definition: control_events.c:314
stream_end_reason_to_control_string
const char * stream_end_reason_to_control_string(int reason)
Definition: reasons.c:28
control_event_hs_descriptor_uploaded
void control_event_hs_descriptor_uploaded(const char *id_digest, const char *onion_address)
Definition: control_events.c:2214
control_event_general_status
int control_event_general_status(int severity, const char *format,...)
Definition: control_events.c:1737
directory.h
Header file for directory.c.
CONN_TYPE_OR
#define CONN_TYPE_OR
Definition: connection.h:42
entry_connection_t::use_begindir
unsigned int use_begindir
Definition: entry_connection_st.h:79
tor_digest_is_zero
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
ENTRY_TO_CONN
#define ENTRY_TO_CONN(c)
Definition: or.h:739
DIR_PURPOSE_IS_UPLOAD
#define DIR_PURPOSE_IS_UPLOAD(p)
Definition: directory.h:82
queued_control_events
static smartlist_t * queued_control_events
Definition: control_events.c:391
control_event_signal
int control_event_signal(uintptr_t signal_num)
Definition: control_events.c:1614
REMAP_STREAM_SOURCE_EXIT
#define REMAP_STREAM_SOURCE_EXIT
Definition: control_events.h:100
control_event_conn_bandwidth
int control_event_conn_bandwidth(connection_t *conn)
Definition: control_events.c:1099
get_connection_array
smartlist_t * get_connection_array(void)
Definition: mainloop.c:452
log_domain_mask_t
uint64_t log_domain_mask_t
Definition: logging_types.h:21
mainloop_event_t
Definition: compat_libevent.c:320
STATE_IS_OPEN
#define STATE_IS_OPEN(s)
Definition: control_events.c:49
tor_threadlocal_t
Definition: threads.h:66
circuit_build_times_t::timeout_ms
double timeout_ms
Definition: circuitstats.h:196
circuit_t
Definition: circuit_st.h:61
nodelist.h
Header file for nodelist.c.
control_event_client_error
int control_event_client_error(const char *format,...)
Definition: control_events.c:1766
flush_queued_events_event
static mainloop_event_t * flush_queued_events_event
Definition: control_events.c:401
control_event_guard
int control_event_guard(const char *nickname, const char *digest, const char *status)
Definition: control_events.c:1798
circuit_purpose_to_controller_string
const char * circuit_purpose_to_controller_string(uint8_t purpose)
Definition: circuitlist.c:780
node_get_verbose_nickname
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1461
rend_data_get_desc_id
const char * rend_data_get_desc_id(const rend_data_t *rend_data, uint8_t replica, size_t *len_out)
Definition: hs_common.c:547
queued_event_free_
static void queued_event_free_(queued_event_t *ev)
Definition: control_events.c:499
tor_gettimeofday
void tor_gettimeofday(struct timeval *timeval)
Definition: tor_gettimeofday.c:42
circuitstats.h
Header file for circuitstats.c.
circuit_purpose_to_controller_hs_state_string
const char * circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
Definition: circuitlist.c:841
control_event_hs_descriptor_created
void control_event_hs_descriptor_created(const char *onion_address, const char *desc_id, int replica)
Definition: control_events.c:1983
in_main_thread
int in_main_thread(void)
Definition: compat_threads.c:57
networkstatus_t::routerstatus_list
smartlist_t * routerstatus_list
Definition: networkstatus_st.h:96
tor_vasprintf
int tor_vasprintf(char **strp, const char *fmt, va_list args)
Definition: printf.c:96
tor_threadlocal_set
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
Definition: compat_pthreads.c:247
control_event_newconsensus
int control_event_newconsensus(const networkstatus_t *consensus)
Definition: control_events.c:1566
control_event_status
static int control_event_status(int type, int severity, const char *format, va_list args)
Definition: control_events.c:1671
log_severity_to_event
static int log_severity_to_event(int severity)
Definition: control_events.c:117
connection_t
Definition: connection_st.h:45
connection_t::type
unsigned int type
Definition: connection_st.h:50
circuit_t::n_circ_id
circid_t n_circ_id
Definition: circuit_st.h:78
control_event_bandwidth_used
int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
Definition: control_events.c:1288
LOG_INFO
#define LOG_INFO
Definition: log.h:45
fmt_addr
#define fmt_addr(a)
Definition: address.h:239
control_event_network_liveness_update
int control_event_network_liveness_update(int liveness)
Definition: control_events.c:1492
entry_connection_t::socks_request
socks_request_t * socks_request
Definition: entry_connection_st.h:27
tor_mutex_new
tor_mutex_t * tor_mutex_new(void)
Definition: compat_mutex.c:17
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:926
circuit_count_pending_on_channel
int circuit_count_pending_on_channel(channel_t *chan)
Definition: circuitlist.c:607
tor_memcmp
int tor_memcmp(const void *a, const void *b, size_t len)
Definition: di_ops.c:31
TO_EDGE_CONN
edge_connection_t * TO_EDGE_CONN(connection_t *c)
Definition: connection_edge.c:172
control_event_hsv2_descriptor_failed
void control_event_hsv2_descriptor_failed(const rend_data_t *rend_data, const char *hsdir_id_digest, const char *reason)
Definition: control_events.c:2231
circuit_status_minor_event_t
circuit_status_minor_event_t
Definition: control_events.h:23
control_get_bytes_rw_last_sec
static void control_get_bytes_rw_last_sec(uint64_t *r, uint64_t *w)
Definition: control_events.c:346
tor_libevent_get_base
struct event_base * tor_libevent_get_base(void)
Definition: compat_libevent.c:196
TO_ORIGIN_CIRCUIT
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:166
stats_n_bytes_written
static uint64_t stats_n_bytes_written
Definition: mainloop.c:141
disable_control_logging
void disable_control_logging(void)
Definition: control_events.c:1340
connection.h
Header file for connection.c.
control_adjust_event_log_severity
void control_adjust_event_log_severity(void)
Definition: control_events.c:277
channeltls.h
Header file for channeltls.c.
control_event_hs_descriptor_upload_failed
void control_event_hs_descriptor_upload_failed(const char *id_digest, const char *onion_address, const char *reason)
Definition: control_events.c:2328
confline.h
Header for confline.c.
CONN_TYPE_EXIT
#define CONN_TYPE_EXIT
Definition: connection.h:44
timeval
Definition: compat_time.h:151
control_event_hs_descriptor_upload
void control_event_hs_descriptor_upload(const char *onion_address, const char *id_digest, const char *desc_id, const char *hsdir_index)
Definition: control_events.c:2011
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
or_connection_st.h
OR connection structure.
cached_bw_event_t
Definition: control_events.c:1280
control_event_buildtimeout_set
int control_event_buildtimeout_set(buildtimeout_set_event_t type, const char *args)
Definition: control_events.c:1576
clear_circ_bw_fields
static void clear_circ_bw_fields(void)
Definition: control_events.c:131
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
change_callback_log_severity
void change_callback_log_severity(int loglevelMin, int loglevelMax, log_callback cb)
Definition: log.c:1036
origin_circuit_t::n_delivered_read_circ_bw
uint32_t n_delivered_read_circ_bw
Definition: origin_circuit_st.h:103
connection_t::n_written_conn_bw
uint32_t n_written_conn_bw
Definition: connection_st.h:143
control_event_circ_bandwidth_used
int control_event_circ_bandwidth_used(void)
Definition: control_events.c:1031
HEX_DIGEST_LEN
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
mainloop_event_activate
void mainloop_event_activate(mainloop_event_t *event)
Definition: compat_libevent.c:425
LOG_WARN
#define LOG_WARN
Definition: log.h:53
entry_connection_describe_status_for_controller
char * entry_connection_describe_status_for_controller(const entry_connection_t *conn)
Definition: control_fmt.c:170
control_connection_t
Definition: control_connection_st.h:19
edge_connection_t
Definition: edge_connection_st.h:21
LOG_ERR
#define LOG_ERR
Definition: log.h:56
control_event_pt_status
void control_event_pt_status(const char *status)
Definition: control_events.c:1887
or_conn_status_event_t
or_conn_status_event_t
Definition: orconn_event.h:59
origin_circuit_t::n_read_circ_bw
uint32_t n_read_circ_bw
Definition: origin_circuit_st.h:93
rend_data_t
Definition: or.h:419
tor_threadlocal_init
int tor_threadlocal_init(tor_threadlocal_t *threadlocal)
Definition: compat_pthreads.c:227
format_local_iso_time
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
or_circuit_t
Definition: or_circuit_st.h:21
disable_log_messages
static int disable_log_messages
Definition: control_events.c:56
or_circuit_t::p_circ_id
circid_t p_circ_id
Definition: or_circuit_st.h:33
rend_data_get_address
const char * rend_data_get_address(const rend_data_t *rend_data)
Definition: hs_common.c:529
control_event_general_error
int control_event_general_error(const char *format,...)
Definition: control_events.c:1746
flush_queued_event_pending
static int flush_queued_event_pending
Definition: control_events.c:394
config_line_t
Definition: confline.h:29
edge_connection_t::n_written
uint32_t n_written
Definition: edge_connection_st.h:60
config.h
Header file for config.c.
origin_circuit_t::n_delivered_written_circ_bw
uint32_t n_delivered_written_circ_bw
Definition: origin_circuit_st.h:108
control_event_networkstatus_changed
int control_event_networkstatus_changed(smartlist_t *statuses)
Definition: control_events.c:1558
smartlist_add_asprintf
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
tor_mutex_acquire
void tor_mutex_acquire(tor_mutex_t *m)
Definition: compat_mutex_pthreads.c:71
queue_control_event_string
STATIC void queue_control_event_string(uint16_t event, char *msg)
Definition: control_events.c:448
channel_t::global_identifier
uint64_t global_identifier
Definition: channel.h:197
control_event_networkstatus_changed_helper
static int control_event_networkstatus_changed_helper(smartlist_t *statuses, uint16_t event, const char *event_string)
Definition: control_events.c:1522
esc_for_log
char * esc_for_log(const char *s)
Definition: escape.c:30
orconn_target_get_name
void orconn_target_get_name(char *name, size_t len, or_connection_t *conn)
Definition: control_fmt.c:54
sum_up_cell_stats_by_command
void sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
Definition: control_events.c:1149
connection_flush
int connection_flush(connection_t *conn)
Definition: connection.c:4364
origin_circuit_t::n_overhead_read_circ_bw
uint32_t n_overhead_read_circ_bw
Definition: origin_circuit_st.h:113
TO_CONN
#define TO_CONN(c)
Definition: or.h:736
queued_event_t
Definition: control_events.c:380
STATIC
#define STATIC
Definition: testsupport.h:32
orconn_end_reason_to_control_string
const char * orconn_end_reason_to_control_string(int r)
Definition: reasons.c:225
control_event_descriptors_changed
int control_event_descriptors_changed(smartlist_t *routers)
Definition: control_events.c:1429
control_update_global_event_mask
void control_update_global_event_mask(void)
Definition: control_events.c:206
write_stream_target_to_buf
int write_stream_target_to_buf(entry_connection_t *conn, char *buf, size_t len)
Definition: control_fmt.c:32
control_event_networkstatus_changed_single
int control_event_networkstatus_changed_single(const routerstatus_t *rs)
Definition: control_events.c:1642
flush_queued_events_cb
static void flush_queued_events_cb(mainloop_event_t *event, void *arg)
Definition: control_events.c:575
networkstatus_st.h
Networkstatus consensus/vote structure.
tor_threadlocal_get
void * tor_threadlocal_get(tor_threadlocal_t *threadlocal)
Definition: compat_pthreads.c:241
entry_connection_t
Definition: entry_connection_st.h:19
rend_data_v2_t
Definition: or.h:434
buildtimeout_set_event_t
buildtimeout_set_event_t
Definition: control_events.h:43
TO_OR_CIRCUIT
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:154
networkstatus_t
Definition: networkstatus_st.h:26
control_event_conf_changed
void control_event_conf_changed(const config_line_t *changes)
Definition: control_events.c:1826
rend_data_t::hsdirs_fp
smartlist_t * hsdirs_fp
Definition: or.h:425
origin_circuit_st.h
Origin circuit structure.
rescan_periodic_events
void rescan_periodic_events(const or_options_t *options)
Definition: mainloop.c:1589
global_event_mask
static event_mask_t global_event_mask
Definition: control_events.c:53
REMAP_STREAM_SOURCE_CACHE
#define REMAP_STREAM_SOURCE_CACHE
Definition: control_events.h:97
circuit_get_by_edge_conn
circuit_t * circuit_get_by_edge_conn(edge_connection_t *conn)
Definition: circuitlist.c:1578
smartlist_t
Definition: smartlist_core.h:26
circuit_clear_testing_cell_stats
void circuit_clear_testing_cell_stats(circuit_t *circ)
Definition: circuitlist.c:1109
smartlist_join_strings
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
queued_events_flush_all
static void queued_events_flush_all(int force)
Definition: control_events.c:513
compat_libevent.h
Header for compat_libevent.c.
control_event_circuit_cell_stats
int control_event_circuit_cell_stats(void)
Definition: control_events.c:1252
circuit_build_times_t::total_build_times
int total_build_times
Definition: circuitstats.h:183
control_fmt.h
Header file for control_fmt.c.
mainloop_event_new
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
Definition: compat_libevent.c:396
control_connection_st.h
Controller connection structure.
connection_or.h
Header file for connection_or.c.
END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
#define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
Definition: or.h:280
circuit_build_times_t::alpha
double alpha
Definition: circuitstats.h:191
enable_control_logging
void enable_control_logging(void)
Definition: control_events.c:1348
or.h
Master header file for Tor-specific functionality.
tor_mutex_release
void tor_mutex_release(tor_mutex_t *m)
Definition: compat_mutex_pthreads.c:84
get_desc_id_from_query
static const char * get_desc_id_from_query(const rend_data_t *rend_data, const char *hsdir_fp)
Definition: control_events.c:2117
connection_t::global_identifier
uint64_t global_identifier
Definition: connection_st.h:135
connection_or_get_num_circuits
int connection_or_get_num_circuits(or_connection_t *conn)
Definition: connection_or.c:400