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  case STREAM_EVENT_CONTROLLER_WAIT: status = "CONTROLLER_WAIT"; break;
822  default:
823  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
824  return 0;
825  }
826  if (reason_code && (tp == STREAM_EVENT_FAILED ||
827  tp == STREAM_EVENT_CLOSED ||
828  tp == STREAM_EVENT_FAILED_RETRIABLE)) {
829  const char *reason_str = stream_end_reason_to_control_string(reason_code);
830  char *r = NULL;
831  if (!reason_str) {
832  tor_asprintf(&r, " UNKNOWN_%d", reason_code);
833  reason_str = r;
834  }
835  if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
836  tor_snprintf(reason_buf, sizeof(reason_buf),
837  " REASON=END REMOTE_REASON=%s", reason_str);
838  else
839  tor_snprintf(reason_buf, sizeof(reason_buf),
840  " REASON=%s", reason_str);
841  tor_free(r);
842  } else if (reason_code && tp == STREAM_EVENT_REMAP) {
843  switch (reason_code) {
845  strlcpy(reason_buf, " SOURCE=CACHE", sizeof(reason_buf));
846  break;
848  strlcpy(reason_buf, " SOURCE=EXIT", sizeof(reason_buf));
849  break;
850  default:
851  tor_snprintf(reason_buf, sizeof(reason_buf), " REASON=UNKNOWN_%d",
852  reason_code);
853  /* XXX do we want SOURCE=UNKNOWN_%d above instead? -RD */
854  break;
855  }
856  }
857 
858  if (tp == STREAM_EVENT_NEW || tp == STREAM_EVENT_NEW_RESOLVE) {
859  /*
860  * When the control conn is an AF_UNIX socket and we have no address,
861  * it gets set to "(Tor_internal)"; see dnsserv_launch_request() in
862  * dnsserv.c.
863  */
864  if (strcmp(ENTRY_TO_CONN(conn)->address, "(Tor_internal)") != 0) {
865  tor_snprintf(addrport_buf,sizeof(addrport_buf), " SOURCE_ADDR=%s:%d",
866  ENTRY_TO_CONN(conn)->address, ENTRY_TO_CONN(conn)->port);
867  } else {
868  /*
869  * else leave it blank so control on AF_UNIX doesn't need to make
870  * something up.
871  */
872  addrport_buf[0] = '\0';
873  }
874  } else {
875  addrport_buf[0] = '\0';
876  }
877 
878  if (tp == STREAM_EVENT_NEW_RESOLVE) {
879  purpose = " PURPOSE=DNS_REQUEST";
880  } else if (tp == STREAM_EVENT_NEW) {
881  if (conn->use_begindir) {
882  connection_t *linked = ENTRY_TO_CONN(conn)->linked_conn;
883  int linked_dir_purpose = -1;
884  if (linked && linked->type == CONN_TYPE_DIR)
885  linked_dir_purpose = linked->purpose;
886  if (DIR_PURPOSE_IS_UPLOAD(linked_dir_purpose))
887  purpose = " PURPOSE=DIR_UPLOAD";
888  else
889  purpose = " PURPOSE=DIR_FETCH";
890  } else
891  purpose = " PURPOSE=USER";
892  }
893 
895  if (circ && CIRCUIT_IS_ORIGIN(circ))
896  origin_circ = TO_ORIGIN_CIRCUIT(circ);
897 
898  {
900  const char *sp = strlen(conndesc) ? " " : "";
901  send_control_event(EVENT_STREAM_STATUS,
902  "650 STREAM %"PRIu64" %s %lu %s%s%s%s%s%s\r\n",
903  (ENTRY_TO_CONN(conn)->global_identifier),
904  status,
905  origin_circ?
906  (unsigned long)origin_circ->global_identifier : 0ul,
907  buf, reason_buf, addrport_buf, purpose, sp, conndesc);
908  tor_free(conndesc);
909  }
910 
911  /* XXX need to specify its intended exit, etc? */
912 
913  return 0;
914 }
915 
916 /** Called when the status of an OR connection <b>conn</b> changes: tell any
917  * interested control connections. <b>tp</b> is the new status for the
918  * connection. If <b>conn</b> has just closed or failed, then <b>reason</b>
919  * may be the reason why.
920  */
921 int
923  int reason)
924 {
925  int ncircs = 0;
926  const char *status;
927  char name[128];
928  char ncircs_buf[32] = {0}; /* > 8 + log10(2^32)=10 + 2 */
929 
930  if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
931  return 0;
932 
933  switch (tp)
934  {
935  case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break;
936  case OR_CONN_EVENT_CONNECTED: status = "CONNECTED"; break;
937  case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
938  case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
939  case OR_CONN_EVENT_NEW: status = "NEW"; break;
940  default:
941  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
942  return 0;
943  }
944  if (conn->chan) {
945  ncircs = circuit_count_pending_on_channel(TLS_CHAN_TO_BASE(conn->chan));
946  } else {
947  ncircs = 0;
948  }
949  ncircs += connection_or_get_num_circuits(conn);
950  if (ncircs && (tp == OR_CONN_EVENT_FAILED || tp == OR_CONN_EVENT_CLOSED)) {
951  tor_snprintf(ncircs_buf, sizeof(ncircs_buf), " NCIRCS=%d", ncircs);
952  }
953 
954  orconn_target_get_name(name, sizeof(name), conn);
955  send_control_event(EVENT_OR_CONN_STATUS,
956  "650 ORCONN %s %s%s%s%s ID=%"PRIu64"\r\n",
957  name, status,
958  reason ? " REASON=" : "",
960  ncircs_buf,
961  (conn->base_.global_identifier));
962 
963  return 0;
964 }
965 
966 /**
967  * Print out STREAM_BW event for a single conn
968  */
969 int
971 {
972  struct timeval now;
973  char tbuf[ISO_TIME_USEC_LEN+1];
974  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
975  if (!edge_conn->n_read && !edge_conn->n_written)
976  return 0;
977 
978  tor_gettimeofday(&now);
979  format_iso_time_nospace_usec(tbuf, &now);
980  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
981  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
982  (edge_conn->base_.global_identifier),
983  (unsigned long)edge_conn->n_read,
984  (unsigned long)edge_conn->n_written,
985  tbuf);
986 
987  edge_conn->n_written = edge_conn->n_read = 0;
988  }
989 
990  return 0;
991 }
992 
993 /** A second or more has elapsed: tell any interested control
994  * connections how much bandwidth streams have used. */
995 int
997 {
998  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
1000  edge_connection_t *edge_conn;
1001  struct timeval now;
1002  char tbuf[ISO_TIME_USEC_LEN+1];
1003 
1004  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn)
1005  {
1006  if (conn->type != CONN_TYPE_AP)
1007  continue;
1008  edge_conn = TO_EDGE_CONN(conn);
1009  if (!edge_conn->n_read && !edge_conn->n_written)
1010  continue;
1011 
1012  tor_gettimeofday(&now);
1013  format_iso_time_nospace_usec(tbuf, &now);
1014  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
1015  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
1016  (edge_conn->base_.global_identifier),
1017  (unsigned long)edge_conn->n_read,
1018  (unsigned long)edge_conn->n_written,
1019  tbuf);
1020 
1021  edge_conn->n_written = edge_conn->n_read = 0;
1022  }
1023  SMARTLIST_FOREACH_END(conn);
1024  }
1025 
1026  return 0;
1027 }
1028 
1029 /** A second or more has elapsed: tell any interested control connections
1030  * how much bandwidth origin circuits have used. */
1031 int
1033 {
1034  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
1035  return 0;
1036 
1038  if (!CIRCUIT_IS_ORIGIN(circ))
1039  continue;
1040 
1042  }
1043  SMARTLIST_FOREACH_END(circ);
1044 
1045  return 0;
1046 }
1047 
1048 /**
1049  * Emit a CIRC_BW event line for a specific circuit.
1050  *
1051  * This function sets the values it emits to 0, and does not emit
1052  * an event if there is no new data to report since the last call.
1053  *
1054  * Therefore, it may be called at any frequency.
1055  */
1056 int
1058 {
1059  struct timeval now;
1060  char tbuf[ISO_TIME_USEC_LEN+1];
1061 
1062  tor_assert(ocirc);
1063 
1064  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
1065  return 0;
1066 
1067  /* n_read_circ_bw and n_written_circ_bw are always updated
1068  * when there is any new cell on a circuit, and set to 0 after
1069  * the event, below.
1070  *
1071  * Therefore, checking them is sufficient to determine if there
1072  * is new data to report. */
1073  if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
1074  return 0;
1075 
1076  tor_gettimeofday(&now);
1077  format_iso_time_nospace_usec(tbuf, &now);
1078  send_control_event(EVENT_CIRC_BANDWIDTH_USED,
1079  "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu TIME=%s "
1080  "DELIVERED_READ=%lu OVERHEAD_READ=%lu "
1081  "DELIVERED_WRITTEN=%lu OVERHEAD_WRITTEN=%lu\r\n",
1082  ocirc->global_identifier,
1083  (unsigned long)ocirc->n_read_circ_bw,
1084  (unsigned long)ocirc->n_written_circ_bw,
1085  tbuf,
1086  (unsigned long)ocirc->n_delivered_read_circ_bw,
1087  (unsigned long)ocirc->n_overhead_read_circ_bw,
1088  (unsigned long)ocirc->n_delivered_written_circ_bw,
1089  (unsigned long)ocirc->n_overhead_written_circ_bw);
1090  ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
1093 
1094  return 0;
1095 }
1096 
1097 /** Print out CONN_BW event for a single OR/DIR/EXIT <b>conn</b> and reset
1098  * bandwidth counters. */
1099 int
1101 {
1102  const char *conn_type_str;
1103  if (!get_options()->TestingEnableConnBwEvent ||
1104  !EVENT_IS_INTERESTING(EVENT_CONN_BW))
1105  return 0;
1106  if (!conn->n_read_conn_bw && !conn->n_written_conn_bw)
1107  return 0;
1108  switch (conn->type) {
1109  case CONN_TYPE_OR:
1110  conn_type_str = "OR";
1111  break;
1112  case CONN_TYPE_DIR:
1113  conn_type_str = "DIR";
1114  break;
1115  case CONN_TYPE_EXIT:
1116  conn_type_str = "EXIT";
1117  break;
1118  default:
1119  return 0;
1120  }
1121  send_control_event(EVENT_CONN_BW,
1122  "650 CONN_BW ID=%"PRIu64" TYPE=%s "
1123  "READ=%lu WRITTEN=%lu\r\n",
1124  (conn->global_identifier),
1125  conn_type_str,
1126  (unsigned long)conn->n_read_conn_bw,
1127  (unsigned long)conn->n_written_conn_bw);
1128  conn->n_written_conn_bw = conn->n_read_conn_bw = 0;
1129  return 0;
1130 }
1131 
1132 /** A second or more has elapsed: tell any interested control
1133  * connections how much bandwidth connections have used. */
1134 int
1136 {
1137  if (get_options()->TestingEnableConnBwEvent &&
1138  EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
1141  }
1142  return 0;
1143 }
1144 
1145 /** Helper: iterate over cell statistics of <b>circ</b> and sum up added
1146  * cells, removed cells, and waiting times by cell command and direction.
1147  * Store results in <b>cell_stats</b>. Free cell statistics of the
1148  * circuit afterwards. */
1149 void
1150 sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
1151 {
1152  memset(cell_stats, 0, sizeof(cell_stats_t));
1154  const testing_cell_stats_entry_t *, ent) {
1155  tor_assert(ent->command <= CELL_COMMAND_MAX_);
1156  if (!ent->removed && !ent->exitward) {
1157  cell_stats->added_cells_appward[ent->command] += 1;
1158  } else if (!ent->removed && ent->exitward) {
1159  cell_stats->added_cells_exitward[ent->command] += 1;
1160  } else if (!ent->exitward) {
1161  cell_stats->removed_cells_appward[ent->command] += 1;
1162  cell_stats->total_time_appward[ent->command] += ent->waiting_time * 10;
1163  } else {
1164  cell_stats->removed_cells_exitward[ent->command] += 1;
1165  cell_stats->total_time_exitward[ent->command] += ent->waiting_time * 10;
1166  }
1167  } SMARTLIST_FOREACH_END(ent);
1169 }
1170 
1171 /** Helper: append a cell statistics string to <code>event_parts</code>,
1172  * prefixed with <code>key</code>=. Statistics consist of comma-separated
1173  * key:value pairs with lower-case command strings as keys and cell
1174  * numbers or total waiting times as values. A key:value pair is included
1175  * if the entry in <code>include_if_non_zero</code> is not zero, but with
1176  * the (possibly zero) entry from <code>number_to_include</code>. Both
1177  * arrays are expected to have a length of CELL_COMMAND_MAX_ + 1. If no
1178  * entry in <code>include_if_non_zero</code> is positive, no string will
1179  * be added to <code>event_parts</code>. */
1180 void
1181 append_cell_stats_by_command(smartlist_t *event_parts, const char *key,
1182  const uint64_t *include_if_non_zero,
1183  const uint64_t *number_to_include)
1184 {
1185  smartlist_t *key_value_strings = smartlist_new();
1186  int i;
1187  for (i = 0; i <= CELL_COMMAND_MAX_; i++) {
1188  if (include_if_non_zero[i] > 0) {
1189  smartlist_add_asprintf(key_value_strings, "%s:%"PRIu64,
1191  (number_to_include[i]));
1192  }
1193  }
1194  if (smartlist_len(key_value_strings) > 0) {
1195  char *joined = smartlist_join_strings(key_value_strings, ",", 0, NULL);
1196  smartlist_add_asprintf(event_parts, "%s=%s", key, joined);
1197  SMARTLIST_FOREACH(key_value_strings, char *, cp, tor_free(cp));
1198  tor_free(joined);
1199  }
1200  smartlist_free(key_value_strings);
1201 }
1202 
1203 /** Helper: format <b>cell_stats</b> for <b>circ</b> for inclusion in a
1204  * CELL_STATS event and write result string to <b>event_string</b>. */
1205 void
1206 format_cell_stats(char **event_string, circuit_t *circ,
1207  cell_stats_t *cell_stats)
1208 {
1209  smartlist_t *event_parts = smartlist_new();
1210  if (CIRCUIT_IS_ORIGIN(circ)) {
1211  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1212  smartlist_add_asprintf(event_parts, "ID=%lu",
1213  (unsigned long)ocirc->global_identifier);
1214  } else if (TO_OR_CIRCUIT(circ)->p_chan) {
1215  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1216  smartlist_add_asprintf(event_parts, "InboundQueue=%lu",
1217  (unsigned long)or_circ->p_circ_id);
1218  smartlist_add_asprintf(event_parts, "InboundConn=%"PRIu64,
1219  (or_circ->p_chan->global_identifier));
1220  append_cell_stats_by_command(event_parts, "InboundAdded",
1221  cell_stats->added_cells_appward,
1222  cell_stats->added_cells_appward);
1223  append_cell_stats_by_command(event_parts, "InboundRemoved",
1224  cell_stats->removed_cells_appward,
1225  cell_stats->removed_cells_appward);
1226  append_cell_stats_by_command(event_parts, "InboundTime",
1227  cell_stats->removed_cells_appward,
1228  cell_stats->total_time_appward);
1229  }
1230  if (circ->n_chan) {
1231  smartlist_add_asprintf(event_parts, "OutboundQueue=%lu",
1232  (unsigned long)circ->n_circ_id);
1233  smartlist_add_asprintf(event_parts, "OutboundConn=%"PRIu64,
1234  (circ->n_chan->global_identifier));
1235  append_cell_stats_by_command(event_parts, "OutboundAdded",
1236  cell_stats->added_cells_exitward,
1237  cell_stats->added_cells_exitward);
1238  append_cell_stats_by_command(event_parts, "OutboundRemoved",
1239  cell_stats->removed_cells_exitward,
1240  cell_stats->removed_cells_exitward);
1241  append_cell_stats_by_command(event_parts, "OutboundTime",
1242  cell_stats->removed_cells_exitward,
1243  cell_stats->total_time_exitward);
1244  }
1245  *event_string = smartlist_join_strings(event_parts, " ", 0, NULL);
1246  SMARTLIST_FOREACH(event_parts, char *, cp, tor_free(cp));
1247  smartlist_free(event_parts);
1248 }
1249 
1250 /** A second or more has elapsed: tell any interested control connection
1251  * how many cells have been processed for a given circuit. */
1252 int
1254 {
1255  cell_stats_t *cell_stats;
1256  char *event_string;
1257  if (!get_options()->TestingEnableCellStatsEvent ||
1258  !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
1259  return 0;
1260  cell_stats = tor_malloc(sizeof(cell_stats_t));
1262  if (!circ->testing_cell_stats)
1263  continue;
1264  sum_up_cell_stats_by_command(circ, cell_stats);
1265  format_cell_stats(&event_string, circ, cell_stats);
1266  send_control_event(EVENT_CELL_STATS,
1267  "650 CELL_STATS %s\r\n", event_string);
1268  tor_free(event_string);
1269  }
1270  SMARTLIST_FOREACH_END(circ);
1271  tor_free(cell_stats);
1272  return 0;
1273 }
1274 
1275 /* about 5 minutes worth. */
1276 #define N_BW_EVENTS_TO_CACHE 300
1277 /* Index into cached_bw_events to next write. */
1278 static int next_measurement_idx = 0;
1279 /* number of entries set in n_measurements */
1280 static int n_measurements = 0;
1281 static struct cached_bw_event_t {
1282  uint32_t n_read;
1283  uint32_t n_written;
1284 } cached_bw_events[N_BW_EVENTS_TO_CACHE];
1285 
1286 /** A second or more has elapsed: tell any interested control
1287  * connections how much bandwidth we used. */
1288 int
1289 control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
1290 {
1291  cached_bw_events[next_measurement_idx].n_read = n_read;
1292  cached_bw_events[next_measurement_idx].n_written = n_written;
1293  if (++next_measurement_idx == N_BW_EVENTS_TO_CACHE)
1294  next_measurement_idx = 0;
1295  if (n_measurements < N_BW_EVENTS_TO_CACHE)
1296  ++n_measurements;
1297 
1298  if (EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED)) {
1299  send_control_event(EVENT_BANDWIDTH_USED,
1300  "650 BW %lu %lu\r\n",
1301  (unsigned long)n_read,
1302  (unsigned long)n_written);
1303  }
1304 
1305  return 0;
1306 }
1307 
1308 char *
1309 get_bw_samples(void)
1310 {
1311  int i;
1312  int idx = (next_measurement_idx + N_BW_EVENTS_TO_CACHE - n_measurements)
1313  % N_BW_EVENTS_TO_CACHE;
1314  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1315 
1316  smartlist_t *elements = smartlist_new();
1317 
1318  for (i = 0; i < n_measurements; ++i) {
1319  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1320  const struct cached_bw_event_t *bwe = &cached_bw_events[idx];
1321 
1322  smartlist_add_asprintf(elements, "%u,%u",
1323  (unsigned)bwe->n_read,
1324  (unsigned)bwe->n_written);
1325 
1326  idx = (idx + 1) % N_BW_EVENTS_TO_CACHE;
1327  }
1328 
1329  char *result = smartlist_join_strings(elements, " ", 0, NULL);
1330 
1331  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
1332  smartlist_free(elements);
1333 
1334  return result;
1335 }
1336 
1337 /** Called when we are sending a log message to the controllers: suspend
1338  * sending further log messages to the controllers until we're done. Used by
1339  * CONN_LOG_PROTECT. */
1340 void
1342 {
1344 }
1345 
1346 /** We're done sending a log message to the controllers: re-enable controller
1347  * logging. Used by CONN_LOG_PROTECT. */
1348 void
1350 {
1351  if (--disable_log_messages < 0)
1352  tor_assert(0);
1353 }
1354 
1355 /** We got a log message: tell any interested control connections. */
1356 void
1357 control_event_logmsg(int severity, log_domain_mask_t domain, const char *msg)
1358 {
1359  int event;
1360 
1361  /* Don't even think of trying to add stuff to a buffer from a cpuworker
1362  * thread. (See #25987 for plan to fix.) */
1363  if (! in_main_thread())
1364  return;
1365 
1367  return;
1368 
1369  if (domain == LD_BUG && EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL) &&
1370  severity <= LOG_NOTICE) {
1371  char *esc = esc_for_log(msg);
1373  control_event_general_status(severity, "BUG REASON=%s", esc);
1375  tor_free(esc);
1376  }
1377 
1378  event = log_severity_to_event(severity);
1379  if (event >= 0 && EVENT_IS_INTERESTING(event)) {
1380  char *b = NULL;
1381  const char *s;
1382  if (strchr(msg, '\n')) {
1383  char *cp;
1384  b = tor_strdup(msg);
1385  for (cp = b; *cp; ++cp)
1386  if (*cp == '\r' || *cp == '\n')
1387  *cp = ' ';
1388  }
1389  switch (severity) {
1390  case LOG_DEBUG: s = "DEBUG"; break;
1391  case LOG_INFO: s = "INFO"; break;
1392  case LOG_NOTICE: s = "NOTICE"; break;
1393  case LOG_WARN: s = "WARN"; break;
1394  case LOG_ERR: s = "ERR"; break;
1395  default: s = "UnknownLogSeverity"; break;
1396  }
1398  send_control_event(event, "650 %s %s\r\n", s, b?b:msg);
1399  if (severity == LOG_ERR) {
1400  /* Force a flush, since we may be about to die horribly */
1402  }
1404  tor_free(b);
1405  }
1406 }
1407 
1408 /**
1409  * Logging callback: called when there is a queued pending log callback.
1410  */
1411 void
1413 {
1414  if (! in_main_thread()) {
1415  /* We can't handle this case yet, since we're using a
1416  * mainloop_event_t to invoke queued_events_flush_all. We ought to
1417  * use a different mechanism instead: see #25987.
1418  **/
1419  return;
1420  }
1423 }
1424 
1425 /** Called whenever we receive new router descriptors: tell any
1426  * interested control connections. <b>routers</b> is a list of
1427  * routerinfo_t's.
1428  */
1429 int
1431 {
1432  char *msg;
1433 
1434  if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
1435  return 0;
1436 
1437  {
1438  smartlist_t *names = smartlist_new();
1439  char *ids;
1440  SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
1441  char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
1443  smartlist_add(names, b);
1444  });
1445  ids = smartlist_join_strings(names, " ", 0, NULL);
1446  tor_asprintf(&msg, "650 NEWDESC %s\r\n", ids);
1447  send_control_event_string(EVENT_NEW_DESC, msg);
1448  tor_free(ids);
1449  tor_free(msg);
1450  SMARTLIST_FOREACH(names, char *, cp, tor_free(cp));
1451  smartlist_free(names);
1452  }
1453  return 0;
1454 }
1455 
1456 /** Called when an address mapping on <b>from</b> from changes to <b>to</b>.
1457  * <b>expires</b> values less than 3 are special; see connection_edge.c. If
1458  * <b>error</b> is non-NULL, it is an error code describing the failure
1459  * mode of the mapping.
1460  */
1461 int
1462 control_event_address_mapped(const char *from, const char *to, time_t expires,
1463  const char *error, const int cached)
1464 {
1465  if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP))
1466  return 0;
1467 
1468  if (expires < 3 || expires == TIME_MAX)
1469  send_control_event(EVENT_ADDRMAP,
1470  "650 ADDRMAP %s %s NEVER %s%s"
1471  "CACHED=\"%s\"\r\n",
1472  from, to, error?error:"", error?" ":"",
1473  cached?"YES":"NO");
1474  else {
1475  char buf[ISO_TIME_LEN+1];
1476  char buf2[ISO_TIME_LEN+1];
1477  format_local_iso_time(buf,expires);
1478  format_iso_time(buf2,expires);
1479  send_control_event(EVENT_ADDRMAP,
1480  "650 ADDRMAP %s %s \"%s\""
1481  " %s%sEXPIRES=\"%s\" CACHED=\"%s\"\r\n",
1482  from, to, buf,
1483  error?error:"", error?" ":"",
1484  buf2, cached?"YES":"NO");
1485  }
1486 
1487  return 0;
1488 }
1489 /** The network liveness has changed; this is called from circuitstats.c
1490  * whenever we receive a cell, or when timeout expires and we assume the
1491  * network is down. */
1492 int
1494 {
1495  if (liveness > 0) {
1496  if (get_cached_network_liveness() <= 0) {
1497  /* Update cached liveness */
1498  set_cached_network_liveness(1);
1499  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS UP");
1500  send_control_event_string(EVENT_NETWORK_LIVENESS,
1501  "650 NETWORK_LIVENESS UP\r\n");
1502  }
1503  /* else was already live, no-op */
1504  } else {
1505  if (get_cached_network_liveness() > 0) {
1506  /* Update cached liveness */
1507  set_cached_network_liveness(0);
1508  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS DOWN");
1509  send_control_event_string(EVENT_NETWORK_LIVENESS,
1510  "650 NETWORK_LIVENESS DOWN\r\n");
1511  }
1512  /* else was already dead, no-op */
1513  }
1514 
1515  return 0;
1516 }
1517 
1518 /** Helper function for NS-style events. Constructs and sends an event
1519  * of type <b>event</b> with string <b>event_string</b> out of the set of
1520  * networkstatuses <b>statuses</b>. Currently it is used for NS events
1521  * and NEWCONSENSUS events. */
1522 static int
1524  uint16_t event,
1525  const char *event_string)
1526 {
1527  smartlist_t *strs;
1528  char *s, *esc = NULL;
1529  if (!EVENT_IS_INTERESTING(event) || !smartlist_len(statuses))
1530  return 0;
1531 
1532  strs = smartlist_new();
1533  smartlist_add_strdup(strs, "650+");
1534  smartlist_add_strdup(strs, event_string);
1535  smartlist_add_strdup(strs, "\r\n");
1536  SMARTLIST_FOREACH(statuses, const routerstatus_t *, rs,
1537  {
1539  if (!s) continue;
1540  smartlist_add(strs, s);
1541  });
1542 
1543  s = smartlist_join_strings(strs, "", 0, NULL);
1544  write_escaped_data(s, strlen(s), &esc);
1545  SMARTLIST_FOREACH(strs, char *, cp, tor_free(cp));
1546  smartlist_free(strs);
1547  tor_free(s);
1548  send_control_event_string(event, esc);
1550  "650 OK\r\n");
1551 
1552  tor_free(esc);
1553  return 0;
1554 }
1555 
1556 /** Called when the routerstatus_ts <b>statuses</b> have changed: sends
1557  * an NS event to any controller that cares. */
1558 int
1560 {
1561  return control_event_networkstatus_changed_helper(statuses, EVENT_NS, "NS");
1562 }
1563 
1564 /** Called when we get a new consensus networkstatus. Sends a NEWCONSENSUS
1565  * event consisting of an NS-style line for each relay in the consensus. */
1566 int
1568 {
1569  if (!control_event_is_interesting(EVENT_NEWCONSENSUS))
1570  return 0;
1572  consensus->routerstatus_list, EVENT_NEWCONSENSUS, "NEWCONSENSUS");
1573 }
1574 
1575 /** Called when we compute a new circuitbuildtimeout */
1576 int
1578  const char *args)
1579 {
1580  const char *type_string = NULL;
1581 
1582  if (!control_event_is_interesting(EVENT_BUILDTIMEOUT_SET))
1583  return 0;
1584 
1585  switch (type) {
1586  case BUILDTIMEOUT_SET_EVENT_COMPUTED:
1587  type_string = "COMPUTED";
1588  break;
1589  case BUILDTIMEOUT_SET_EVENT_RESET:
1590  type_string = "RESET";
1591  break;
1592  case BUILDTIMEOUT_SET_EVENT_SUSPENDED:
1593  type_string = "SUSPENDED";
1594  break;
1595  case BUILDTIMEOUT_SET_EVENT_DISCARD:
1596  type_string = "DISCARD";
1597  break;
1598  case BUILDTIMEOUT_SET_EVENT_RESUME:
1599  type_string = "RESUME";
1600  break;
1601  default:
1602  type_string = "UNKNOWN";
1603  break;
1604  }
1605 
1606  send_control_event(EVENT_BUILDTIMEOUT_SET,
1607  "650 BUILDTIMEOUT_SET %s %s\r\n",
1608  type_string, args);
1609 
1610  return 0;
1611 }
1612 
1613 /** Called when a signal has been processed from signal_callback */
1614 int
1615 control_event_signal(uintptr_t signal_num)
1616 {
1617  const char *signal_string = NULL;
1618 
1619  if (!control_event_is_interesting(EVENT_GOT_SIGNAL))
1620  return 0;
1621 
1622  for (unsigned i = 0; signal_table[i].signal_name != NULL; ++i) {
1623  if ((int)signal_num == signal_table[i].sig) {
1624  signal_string = signal_table[i].signal_name;
1625  break;
1626  }
1627  }
1628 
1629  if (signal_string == NULL) {
1630  log_warn(LD_BUG, "Unrecognized signal %lu in control_event_signal",
1631  (unsigned long)signal_num);
1632  return -1;
1633  }
1634 
1635  send_control_event(EVENT_GOT_SIGNAL, "650 SIGNAL %s\r\n",
1636  signal_string);
1637  return 0;
1638 }
1639 
1640 /** Called when a single local_routerstatus_t has changed: Sends an NS event
1641  * to any controller that cares. */
1642 int
1644 {
1645  smartlist_t *statuses;
1646  int r;
1647 
1648  if (!EVENT_IS_INTERESTING(EVENT_NS))
1649  return 0;
1650 
1651  statuses = smartlist_new();
1652  smartlist_add(statuses, (void*)rs);
1654  smartlist_free(statuses);
1655  return r;
1656 }
1657 
1658 /** Our own router descriptor has changed; tell any controllers that care.
1659  */
1660 int
1662 {
1663  send_control_event(EVENT_DESCCHANGED, "650 DESCCHANGED\r\n");
1664  return 0;
1665 }
1666 
1667 /** Helper: sends a status event where <b>type</b> is one of
1668  * EVENT_STATUS_{GENERAL,CLIENT,SERVER}, where <b>severity</b> is one of
1669  * LOG_{NOTICE,WARN,ERR}, and where <b>format</b> is a printf-style format
1670  * string corresponding to <b>args</b>. */
1671 static int
1672 control_event_status(int type, int severity, const char *format, va_list args)
1673 {
1674  char *user_buf = NULL;
1675  char format_buf[160];
1676  const char *status, *sev;
1677 
1678  switch (type) {
1679  case EVENT_STATUS_GENERAL:
1680  status = "STATUS_GENERAL";
1681  break;
1682  case EVENT_STATUS_CLIENT:
1683  status = "STATUS_CLIENT";
1684  break;
1685  case EVENT_STATUS_SERVER:
1686  status = "STATUS_SERVER";
1687  break;
1688  default:
1689  log_warn(LD_BUG, "Unrecognized status type %d", type);
1690  return -1;
1691  }
1692  switch (severity) {
1693  case LOG_NOTICE:
1694  sev = "NOTICE";
1695  break;
1696  case LOG_WARN:
1697  sev = "WARN";
1698  break;
1699  case LOG_ERR:
1700  sev = "ERR";
1701  break;
1702  default:
1703  log_warn(LD_BUG, "Unrecognized status severity %d", severity);
1704  return -1;
1705  }
1706  if (tor_snprintf(format_buf, sizeof(format_buf), "650 %s %s",
1707  status, sev)<0) {
1708  log_warn(LD_BUG, "Format string too long.");
1709  return -1;
1710  }
1711  if (tor_vasprintf(&user_buf, format, args)<0) {
1712  log_warn(LD_BUG, "Failed to create user buffer.");
1713  return -1;
1714  }
1715 
1716  send_control_event(type, "%s %s\r\n", format_buf, user_buf);
1717  tor_free(user_buf);
1718  return 0;
1719 }
1720 
1721 #ifndef COCCI
1722 #define CONTROL_EVENT_STATUS_BODY(event, sev) \
1723  int r; \
1724  do { \
1725  va_list ap; \
1726  if (!EVENT_IS_INTERESTING(event)) \
1727  return 0; \
1728  \
1729  va_start(ap, format); \
1730  r = control_event_status((event), (sev), format, ap); \
1731  va_end(ap); \
1732  } while (0)
1733 #endif /* !defined(COCCI) */
1734 
1735 /** Format and send an EVENT_STATUS_GENERAL event whose main text is obtained
1736  * by formatting the arguments using the printf-style <b>format</b>. */
1737 int
1738 control_event_general_status(int severity, const char *format, ...)
1739 {
1740  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, severity);
1741  return r;
1742 }
1743 
1744 /** Format and send an EVENT_STATUS_GENERAL LOG_ERR event, and flush it to the
1745  * controller(s) immediately. */
1746 int
1747 control_event_general_error(const char *format, ...)
1748 {
1749  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, LOG_ERR);
1750  /* Force a flush, since we may be about to die horribly */
1752  return r;
1753 }
1754 
1755 /** Format and send an EVENT_STATUS_CLIENT event whose main text is obtained
1756  * by formatting the arguments using the printf-style <b>format</b>. */
1757 int
1758 control_event_client_status(int severity, const char *format, ...)
1759 {
1760  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, severity);
1761  return r;
1762 }
1763 
1764 /** Format and send an EVENT_STATUS_CLIENT LOG_ERR event, and flush it to the
1765  * controller(s) immediately. */
1766 int
1767 control_event_client_error(const char *format, ...)
1768 {
1769  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, LOG_ERR);
1770  /* Force a flush, since we may be about to die horribly */
1772  return r;
1773 }
1774 
1775 /** Format and send an EVENT_STATUS_SERVER event whose main text is obtained
1776  * by formatting the arguments using the printf-style <b>format</b>. */
1777 int
1778 control_event_server_status(int severity, const char *format, ...)
1779 {
1780  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, severity);
1781  return r;
1782 }
1783 
1784 /** Format and send an EVENT_STATUS_SERVER LOG_ERR event, and flush it to the
1785  * controller(s) immediately. */
1786 int
1787 control_event_server_error(const char *format, ...)
1788 {
1789  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, LOG_ERR);
1790  /* Force a flush, since we may be about to die horribly */
1792  return r;
1793 }
1794 
1795 /** Called when the status of an entry guard with the given <b>nickname</b>
1796  * and identity <b>digest</b> has changed to <b>status</b>: tells any
1797  * controllers that care. */
1798 int
1799 control_event_guard(const char *nickname, const char *digest,
1800  const char *status)
1801 {
1802  char hbuf[HEX_DIGEST_LEN+1];
1803  base16_encode(hbuf, sizeof(hbuf), digest, DIGEST_LEN);
1804  if (!EVENT_IS_INTERESTING(EVENT_GUARD))
1805  return 0;
1806 
1807  {
1808  char buf[MAX_VERBOSE_NICKNAME_LEN+1];
1809  const node_t *node = node_get_by_id(digest);
1810  if (node) {
1811  node_get_verbose_nickname(node, buf);
1812  } else {
1813  tor_snprintf(buf, sizeof(buf), "$%s~%s", hbuf, nickname);
1814  }
1815  send_control_event(EVENT_GUARD,
1816  "650 GUARD ENTRY %s %s\r\n", buf, status);
1817  }
1818  return 0;
1819 }
1820 
1821 /** Called when a configuration option changes. This is generally triggered
1822  * by SETCONF requests and RELOAD/SIGHUP signals. The <b>changes</b> are
1823  * a linked list of configuration key-values.
1824  * <b>changes</b> can be NULL, meaning "no changes".
1825  */
1826 void
1828 {
1829  char *result;
1830  smartlist_t *lines;
1831  if (!EVENT_IS_INTERESTING(EVENT_CONF_CHANGED) || !changes) {
1832  return;
1833  }
1834  lines = smartlist_new();
1835  for (const config_line_t *line = changes; line; line = line->next) {
1836  if (line->value == NULL) {
1837  smartlist_add_asprintf(lines, "650-%s", line->key);
1838  } else {
1839  smartlist_add_asprintf(lines, "650-%s=%s", line->key, line->value);
1840  }
1841  }
1842  result = smartlist_join_strings(lines, "\r\n", 0, NULL);
1843  send_control_event(EVENT_CONF_CHANGED,
1844  "650-CONF_CHANGED\r\n%s\r\n650 OK\r\n", result);
1845  tor_free(result);
1846  SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1847  smartlist_free(lines);
1848 }
1849 
1850 /** We just generated a new summary of which countries we've seen clients
1851  * from recently. Send a copy to the controller in case it wants to
1852  * display it for the user. */
1853 void
1854 control_event_clients_seen(const char *controller_str)
1855 {
1856  send_control_event(EVENT_CLIENTS_SEEN,
1857  "650 CLIENTS_SEEN %s\r\n", controller_str);
1858 }
1859 
1860 /** A new pluggable transport called <b>transport_name</b> was
1861  * launched on <b>addr</b>:<b>port</b>. <b>mode</b> is either
1862  * "server" or "client" depending on the mode of the pluggable
1863  * transport.
1864  * "650" SP "TRANSPORT_LAUNCHED" SP Mode SP Name SP Address SP Port
1865  */
1866 void
1867 control_event_transport_launched(const char *mode, const char *transport_name,
1868  tor_addr_t *addr, uint16_t port)
1869 {
1870  send_control_event(EVENT_TRANSPORT_LAUNCHED,
1871  "650 TRANSPORT_LAUNCHED %s %s %s %u\r\n",
1872  mode, transport_name, fmt_addr(addr), port);
1873 }
1874 
1875 /** A pluggable transport called <b>pt_name</b> has emitted a log message
1876  * found in <b>message</b> at <b>severity</b> log level. */
1877 void
1878 control_event_pt_log(const char *log)
1879 {
1880  send_control_event(EVENT_PT_LOG,
1881  "650 PT_LOG %s\r\n",
1882  log);
1883 }
1884 
1885 /** A pluggable transport has emitted a STATUS message found in
1886  * <b>status</b>. */
1887 void
1888 control_event_pt_status(const char *status)
1889 {
1890  send_control_event(EVENT_PT_STATUS,
1891  "650 PT_STATUS %s\r\n",
1892  status);
1893 }
1894 
1895 /** Convert rendezvous auth type to string for HS_DESC control events
1896  */
1897 const char *
1899 {
1900  const char *str;
1901 
1902  switch (auth_type) {
1903  case REND_NO_AUTH:
1904  str = "NO_AUTH";
1905  break;
1906  case REND_BASIC_AUTH:
1907  str = "BASIC_AUTH";
1908  break;
1909  case REND_STEALTH_AUTH:
1910  str = "STEALTH_AUTH";
1911  break;
1912  default:
1913  str = "UNKNOWN";
1914  }
1915 
1916  return str;
1917 }
1918 
1919 /** Return either the onion address if the given pointer is a non empty
1920  * string else the unknown string. */
1921 static const char *
1922 rend_hsaddress_str_or_unknown(const char *onion_address)
1923 {
1924  static const char *str_unknown = "UNKNOWN";
1925  const char *str_ret = str_unknown;
1926 
1927  /* No valid pointer, unknown it is. */
1928  if (!onion_address) {
1929  goto end;
1930  }
1931  /* Empty onion address thus we don't know, unknown it is. */
1932  if (onion_address[0] == '\0') {
1933  goto end;
1934  }
1935  /* All checks are good so return the given onion address. */
1936  str_ret = onion_address;
1937 
1938  end:
1939  return str_ret;
1940 }
1941 
1942 /** send HS_DESC requested event.
1943  *
1944  * <b>rend_query</b> is used to fetch requested onion address and auth type.
1945  * <b>hs_dir</b> is the description of contacting hs directory.
1946  * <b>desc_id_base32</b> is the ID of requested hs descriptor.
1947  * <b>hsdir_index</b> is the HSDir fetch index value for v3, an hex string.
1948  */
1949 void
1950 control_event_hs_descriptor_requested(const char *onion_address,
1951  rend_auth_type_t auth_type,
1952  const char *id_digest,
1953  const char *desc_id,
1954  const char *hsdir_index)
1955 {
1956  char *hsdir_index_field = NULL;
1957 
1958  if (BUG(!id_digest || !desc_id)) {
1959  return;
1960  }
1961 
1962  if (hsdir_index) {
1963  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
1964  }
1965 
1966  send_control_event(EVENT_HS_DESC,
1967  "650 HS_DESC REQUESTED %s %s %s %s%s\r\n",
1968  rend_hsaddress_str_or_unknown(onion_address),
1969  rend_auth_type_to_string(auth_type),
1970  node_describe_longname_by_id(id_digest),
1971  desc_id,
1972  hsdir_index_field ? hsdir_index_field : "");
1973  tor_free(hsdir_index_field);
1974 }
1975 
1976 /** send HS_DESC CREATED event when a local service generates a descriptor.
1977  *
1978  * <b>onion_address</b> is service address.
1979  * <b>desc_id</b> is the descriptor ID.
1980  * <b>replica</b> is the the descriptor replica number. If it is negative, it
1981  * is ignored.
1982  */
1983 void
1984 control_event_hs_descriptor_created(const char *onion_address,
1985  const char *desc_id,
1986  int replica)
1987 {
1988  char *replica_field = NULL;
1989 
1990  if (BUG(!onion_address || !desc_id)) {
1991  return;
1992  }
1993 
1994  if (replica >= 0) {
1995  tor_asprintf(&replica_field, " REPLICA=%d", replica);
1996  }
1997 
1998  send_control_event(EVENT_HS_DESC,
1999  "650 HS_DESC CREATED %s UNKNOWN UNKNOWN %s%s\r\n",
2000  onion_address, desc_id,
2001  replica_field ? replica_field : "");
2002  tor_free(replica_field);
2003 }
2004 
2005 /** send HS_DESC upload event.
2006  *
2007  * <b>onion_address</b> is service address.
2008  * <b>hs_dir</b> is the description of contacting hs directory.
2009  * <b>desc_id</b> is the ID of requested hs descriptor.
2010  */
2011 void
2012 control_event_hs_descriptor_upload(const char *onion_address,
2013  const char *id_digest,
2014  const char *desc_id,
2015  const char *hsdir_index)
2016 {
2017  char *hsdir_index_field = NULL;
2018 
2019  if (BUG(!onion_address || !id_digest || !desc_id)) {
2020  return;
2021  }
2022 
2023  if (hsdir_index) {
2024  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
2025  }
2026 
2027  send_control_event(EVENT_HS_DESC,
2028  "650 HS_DESC UPLOAD %s UNKNOWN %s %s%s\r\n",
2029  onion_address,
2030  node_describe_longname_by_id(id_digest),
2031  desc_id,
2032  hsdir_index_field ? hsdir_index_field : "");
2033  tor_free(hsdir_index_field);
2034 }
2035 
2036 /** send HS_DESC event after got response from hs directory.
2037  *
2038  * NOTE: this is an internal function used by following functions:
2039  * control_event_hsv2_descriptor_received
2040  * control_event_hsv2_descriptor_failed
2041  * control_event_hsv3_descriptor_failed
2042  *
2043  * So do not call this function directly.
2044  */
2045 static void
2047  const char *onion_address,
2048  const char *desc_id,
2049  rend_auth_type_t auth_type,
2050  const char *hsdir_id_digest,
2051  const char *reason)
2052 {
2053  char *reason_field = NULL;
2054 
2055  if (BUG(!action || !onion_address)) {
2056  return;
2057  }
2058 
2059  if (reason) {
2060  tor_asprintf(&reason_field, " REASON=%s", reason);
2061  }
2062 
2063  send_control_event(EVENT_HS_DESC,
2064  "650 HS_DESC %s %s %s %s%s%s\r\n",
2065  action,
2066  rend_hsaddress_str_or_unknown(onion_address),
2067  rend_auth_type_to_string(auth_type),
2068  hsdir_id_digest ?
2069  node_describe_longname_by_id(hsdir_id_digest) :
2070  "UNKNOWN",
2071  desc_id ? desc_id : "",
2072  reason_field ? reason_field : "");
2073 
2074  tor_free(reason_field);
2075 }
2076 
2077 /** send HS_DESC event after got response from hs directory.
2078  *
2079  * NOTE: this is an internal function used by following functions:
2080  * control_event_hs_descriptor_uploaded
2081  * control_event_hs_descriptor_upload_failed
2082  *
2083  * So do not call this function directly.
2084  */
2085 void
2087  const char *onion_address,
2088  const char *id_digest,
2089  const char *reason)
2090 {
2091  char *reason_field = NULL;
2092 
2093  if (BUG(!action || !id_digest)) {
2094  return;
2095  }
2096 
2097  if (reason) {
2098  tor_asprintf(&reason_field, " REASON=%s", reason);
2099  }
2100 
2101  send_control_event(EVENT_HS_DESC,
2102  "650 HS_DESC %s %s UNKNOWN %s%s\r\n",
2103  action,
2104  rend_hsaddress_str_or_unknown(onion_address),
2105  node_describe_longname_by_id(id_digest),
2106  reason_field ? reason_field : "");
2107 
2108  tor_free(reason_field);
2109 }
2110 
2111 /** For an HS descriptor query <b>rend_data</b>, using the
2112  * <b>onion_address</b> and HSDir fingerprint <b>hsdir_fp</b>, find out
2113  * which descriptor ID in the query is the right one.
2114  *
2115  * Return a pointer of the binary descriptor ID found in the query's object
2116  * or NULL if not found. */
2117 static const char *
2118 get_desc_id_from_query(const rend_data_t *rend_data, const char *hsdir_fp)
2119 {
2120  int replica;
2121  const char *desc_id = NULL;
2122  const rend_data_v2_t *rend_data_v2 = TO_REND_DATA_V2(rend_data);
2123 
2124  /* Possible if the fetch was done using a descriptor ID. This means that
2125  * the HSFETCH command was used. */
2126  if (!tor_digest_is_zero(rend_data_v2->desc_id_fetch)) {
2127  desc_id = rend_data_v2->desc_id_fetch;
2128  goto end;
2129  }
2130 
2131  /* Without a directory fingerprint at this stage, we can't do much. */
2132  if (hsdir_fp == NULL) {
2133  goto end;
2134  }
2135 
2136  /* OK, we have an onion address so now let's find which descriptor ID
2137  * is the one associated with the HSDir fingerprint. */
2138  for (replica = 0; replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
2139  replica++) {
2140  const char *digest = rend_data_get_desc_id(rend_data, replica, NULL);
2141 
2142  SMARTLIST_FOREACH_BEGIN(rend_data->hsdirs_fp, char *, fingerprint) {
2143  if (tor_memcmp(fingerprint, hsdir_fp, DIGEST_LEN) == 0) {
2144  /* Found it! This descriptor ID is the right one. */
2145  desc_id = digest;
2146  goto end;
2147  }
2148  } SMARTLIST_FOREACH_END(fingerprint);
2149  }
2150 
2151  end:
2152  return desc_id;
2153 }
2154 
2155 /** send HS_DESC RECEIVED event
2156  *
2157  * called when we successfully received a hidden service descriptor.
2158  */
2159 void
2160 control_event_hsv2_descriptor_received(const char *onion_address,
2161  const rend_data_t *rend_data,
2162  const char *hsdir_id_digest)
2163 {
2164  char *desc_id_field = NULL;
2165  const char *desc_id;
2166 
2167  if (BUG(!rend_data || !hsdir_id_digest || !onion_address)) {
2168  return;
2169  }
2170 
2171  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2172  if (desc_id != NULL) {
2173  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2174  /* Set the descriptor ID digest to base32 so we can send it. */
2175  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2176  DIGEST_LEN);
2177  /* Extra whitespace is needed before the value. */
2178  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2179  }
2180 
2181  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2182  TO_REND_DATA_V2(rend_data)->auth_type,
2183  hsdir_id_digest, NULL);
2184  tor_free(desc_id_field);
2185 }
2186 
2187 /* Send HS_DESC RECEIVED event
2188  *
2189  * Called when we successfully received a hidden service descriptor. */
2190 void
2191 control_event_hsv3_descriptor_received(const char *onion_address,
2192  const char *desc_id,
2193  const char *hsdir_id_digest)
2194 {
2195  char *desc_id_field = NULL;
2196 
2197  if (BUG(!onion_address || !desc_id || !hsdir_id_digest)) {
2198  return;
2199  }
2200 
2201  /* Because DescriptorID is an optional positional value, we need to add a
2202  * whitespace before in order to not be next to the HsDir value. */
2203  tor_asprintf(&desc_id_field, " %s", desc_id);
2204 
2205  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2206  REND_NO_AUTH, hsdir_id_digest, NULL);
2207  tor_free(desc_id_field);
2208 }
2209 
2210 /** send HS_DESC UPLOADED event
2211  *
2212  * called when we successfully uploaded a hidden service descriptor.
2213  */
2214 void
2216  const char *onion_address)
2217 {
2218  if (BUG(!id_digest)) {
2219  return;
2220  }
2221 
2222  control_event_hs_descriptor_upload_end("UPLOADED", onion_address,
2223  id_digest, NULL);
2224 }
2225 
2226 /** Send HS_DESC event to inform controller that query <b>rend_data</b>
2227  * failed to retrieve hidden service descriptor from directory identified by
2228  * <b>id_digest</b>. If NULL, "UNKNOWN" is used. If <b>reason</b> is not NULL,
2229  * add it to REASON= field.
2230  */
2231 void
2233  const char *hsdir_id_digest,
2234  const char *reason)
2235 {
2236  char *desc_id_field = NULL;
2237  const char *desc_id;
2238 
2239  if (BUG(!rend_data)) {
2240  return;
2241  }
2242 
2243  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2244  if (desc_id != NULL) {
2245  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2246  /* Set the descriptor ID digest to base32 so we can send it. */
2247  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2248  DIGEST_LEN);
2249  /* Extra whitespace is needed before the value. */
2250  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2251  }
2252 
2254  desc_id_field,
2255  TO_REND_DATA_V2(rend_data)->auth_type,
2256  hsdir_id_digest, reason);
2257  tor_free(desc_id_field);
2258 }
2259 
2260 /** Send HS_DESC event to inform controller that the query to
2261  * <b>onion_address</b> failed to retrieve hidden service descriptor
2262  * <b>desc_id</b> from directory identified by <b>hsdir_id_digest</b>. If
2263  * NULL, "UNKNOWN" is used. If <b>reason</b> is not NULL, add it to REASON=
2264  * field. */
2265 void
2266 control_event_hsv3_descriptor_failed(const char *onion_address,
2267  const char *desc_id,
2268  const char *hsdir_id_digest,
2269  const char *reason)
2270 {
2271  char *desc_id_field = NULL;
2272 
2273  if (BUG(!onion_address || !desc_id || !reason)) {
2274  return;
2275  }
2276 
2277  /* Because DescriptorID is an optional positional value, we need to add a
2278  * whitespace before in order to not be next to the HsDir value. */
2279  tor_asprintf(&desc_id_field, " %s", desc_id);
2280 
2281  event_hs_descriptor_receive_end("FAILED", onion_address, desc_id_field,
2282  REND_NO_AUTH, hsdir_id_digest, reason);
2283  tor_free(desc_id_field);
2284 }
2285 
2286 /** Send HS_DESC_CONTENT event after completion of a successful fetch
2287  * from hs directory. If <b>hsdir_id_digest</b> is NULL, it is replaced
2288  * by "UNKNOWN". If <b>content</b> is NULL, it is replaced by an empty
2289  * string. The <b>onion_address</b> or <b>desc_id</b> set to NULL will
2290  * not trigger the control event. */
2291 void
2292 control_event_hs_descriptor_content(const char *onion_address,
2293  const char *desc_id,
2294  const char *hsdir_id_digest,
2295  const char *content)
2296 {
2297  static const char *event_name = "HS_DESC_CONTENT";
2298  char *esc_content = NULL;
2299 
2300  if (!onion_address || !desc_id) {
2301  log_warn(LD_BUG, "Called with onion_address==%p, desc_id==%p, ",
2302  onion_address, desc_id);
2303  return;
2304  }
2305 
2306  if (content == NULL) {
2307  /* Point it to empty content so it can still be escaped. */
2308  content = "";
2309  }
2310  write_escaped_data(content, strlen(content), &esc_content);
2311 
2312  send_control_event(EVENT_HS_DESC_CONTENT,
2313  "650+%s %s %s %s\r\n%s650 OK\r\n",
2314  event_name,
2315  rend_hsaddress_str_or_unknown(onion_address),
2316  desc_id,
2317  hsdir_id_digest ?
2318  node_describe_longname_by_id(hsdir_id_digest) :
2319  "UNKNOWN",
2320  esc_content);
2321  tor_free(esc_content);
2322 }
2323 
2324 /** Send HS_DESC event to inform controller upload of hidden service
2325  * descriptor identified by <b>id_digest</b> failed. If <b>reason</b>
2326  * is not NULL, add it to REASON= field.
2327  */
2328 void
2330  const char *onion_address,
2331  const char *reason)
2332 {
2333  if (BUG(!id_digest)) {
2334  return;
2335  }
2336  control_event_hs_descriptor_upload_end("FAILED", onion_address,
2337  id_digest, reason);
2338 }
2339 
2340 void
2341 control_events_free_all(void)
2342 {
2343  smartlist_t *queued_events = NULL;
2344 
2345  stats_prev_n_read = stats_prev_n_written = 0;
2346 
2350  queued_events = queued_control_events;
2351  queued_control_events = NULL;
2353  }
2354  if (queued_events) {
2355  SMARTLIST_FOREACH(queued_events, queued_event_t *, ev,
2356  queued_event_free(ev));
2357  smartlist_free(queued_events);
2358  }
2360  mainloop_event_free(flush_queued_events_event);
2362  }
2363  global_event_mask = 0;
2365 }
2366 
2367 #ifdef TOR_UNIT_TESTS
2368 /* For testing: change the value of global_event_mask */
2369 void
2370 control_testing_set_global_event_mask(uint64_t mask)
2371 {
2372  global_event_mask = mask;
2373 }
2374 #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:1661
control_event_stream_bandwidth
int control_event_stream_bandwidth(edge_connection_t *edge_conn)
Definition: control_events.c:970
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:1181
stats_n_bytes_read
static uint64_t stats_n_bytes_read
Definition: mainloop.c:140
name
const char * name
Definition: config.c:2443
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:2086
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:69
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:1206
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:696
control_event_stream_bandwidth_used
int control_event_stream_bandwidth_used(void)
Definition: control_events.c:996
get_bytes_read
uint64_t get_bytes_read(void)
Definition: mainloop.c:465
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:55
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:922
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:2046
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:1357
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:1462
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:60
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:1878
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:71
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:2160
rend_auth_type_to_string
const char * rend_auth_type_to_string(rend_auth_type_t auth_type)
Definition: control_events.c:1898
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:2292
router_get_verbose_nickname
void router_get_verbose_nickname(char *buf, const routerinfo_t *router)
Definition: describe.c:229
control_event_clients_seen
void control_event_clients_seen(const char *controller_str)
Definition: control_events.c:1854
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:20
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:175
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:1412
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:2266
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:475
rend_hsaddress_str_or_unknown
static const char * rend_hsaddress_str_or_unknown(const char *onion_address)
Definition: control_events.c:1922
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:51
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:1950
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:1057
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:1867
control_event_client_status
int control_event_client_status(int severity, const char *format,...)
Definition: control_events.c:1758
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:1135
control_event_server_error
int control_event_server_error(const char *format,...)
Definition: control_events.c:1787
control_event_server_status
int control_event_server_status(int severity, const char *format,...)
Definition: control_events.c:1778
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:2215
control_event_general_status
int control_event_general_status(int severity, const char *format,...)
Definition: control_events.c:1738
directory.h
Header file for directory.c.
CONN_TYPE_OR
#define CONN_TYPE_OR
Definition: connection.h:44
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:83
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:1615
REMAP_STREAM_SOURCE_EXIT
#define REMAP_STREAM_SOURCE_EXIT
Definition: control_events.h:101
control_event_conn_bandwidth
int control_event_conn_bandwidth(connection_t *conn)
Definition: control_events.c:1100
get_connection_array
smartlist_t * get_connection_array(void)
Definition: mainloop.c:453
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:1767
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:1799
circuit_purpose_to_controller_string
const char * circuit_purpose_to_controller_string(uint8_t purpose)
Definition: circuitlist.c:784
node_get_verbose_nickname
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1452
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:845
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:1984
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:1567
control_event_status
static int control_event_status(int type, int severity, const char *format, va_list args)
Definition: control_events.c:1672
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:1289
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:1493
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:928
circuit_count_pending_on_channel
int circuit_count_pending_on_channel(channel_t *chan)
Definition: circuitlist.c:611
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:176
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:2232
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:168
stats_n_bytes_written
static uint64_t stats_n_bytes_written
Definition: mainloop.c:142
disable_control_logging
void disable_control_logging(void)
Definition: control_events.c:1341
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:2329
confline.h
Header for confline.c.
CONN_TYPE_EXIT
#define CONN_TYPE_EXIT
Definition: connection.h:46
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:2012
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:1281
control_event_buildtimeout_set
int control_event_buildtimeout_set(buildtimeout_set_event_t type, const char *args)
Definition: control_events.c:1577
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:179
control_event_circ_bandwidth_used
int control_event_circ_bandwidth_used(void)
Definition: control_events.c:1032
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:1888
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:1747
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:1559
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:1523
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:1150
connection_flush
int connection_flush(connection_t *conn)
Definition: connection.c:4544
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:1430
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:1643
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:44
TO_OR_CIRCUIT
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:156
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:1827
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:1586
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:98
circuit_get_by_edge_conn
circuit_t * circuit_get_by_edge_conn(edge_connection_t *conn)
Definition: circuitlist.c:1588
smartlist_t
Definition: smartlist_core.h:26
circuit_clear_testing_cell_stats
void circuit_clear_testing_cell_stats(circuit_t *circ)
Definition: circuitlist.c:1115
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:1253
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:1349
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:2118
connection_t::global_identifier
uint64_t global_identifier
Definition: connection_st.h:171
connection_or_get_num_circuits
int connection_or_get_num_circuits(or_connection_t *conn)
Definition: connection_or.c:414