tor  0.4.2.1-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-2019, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
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/command.h"
22 #include "core/or/connection_or.h"
23 #include "core/or/reasons.h"
32 
33 #include "feature/control/control_connection_st.h"
34 #include "core/or/entry_connection_st.h"
35 #include "feature/nodelist/networkstatus_st.h"
36 #include "core/or/or_connection_st.h"
37 #include "core/or/or_circuit_st.h"
38 #include "core/or/origin_circuit_st.h"
39 
41 
42 static void flush_queued_events_cb(mainloop_event_t *event, void *arg);
43 static void control_get_bytes_rw_last_sec(uint64_t *r, uint64_t *w);
44 
47 #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN)
48 
51 static event_mask_t global_event_mask = 0;
52 
54 static int disable_log_messages = 0;
55 
58 #define EVENT_IS_INTERESTING(e) \
59  (!! (global_event_mask & EVENT_MASK_(e)))
60 
62 #define ANY_EVENT_IS_INTERESTING(e) \
63  (!! (global_event_mask & (e)))
64 
65 static void send_control_event_impl(uint16_t event,
66  const char *format, va_list ap)
67  CHECK_PRINTF(2,0);
68 static int control_event_status(int type, int severity, const char *format,
69  va_list args)
70  CHECK_PRINTF(3,0);
71 
72 static void send_control_event(uint16_t event,
73  const char *format, ...)
74  CHECK_PRINTF(2,3);
75 
78 const struct control_event_t control_event_table[] = {
79  { EVENT_CIRCUIT_STATUS, "CIRC" },
80  { EVENT_CIRCUIT_STATUS_MINOR, "CIRC_MINOR" },
81  { EVENT_STREAM_STATUS, "STREAM" },
82  { EVENT_OR_CONN_STATUS, "ORCONN" },
83  { EVENT_BANDWIDTH_USED, "BW" },
84  { EVENT_DEBUG_MSG, "DEBUG" },
85  { EVENT_INFO_MSG, "INFO" },
86  { EVENT_NOTICE_MSG, "NOTICE" },
87  { EVENT_WARN_MSG, "WARN" },
88  { EVENT_ERR_MSG, "ERR" },
89  { EVENT_NEW_DESC, "NEWDESC" },
90  { EVENT_ADDRMAP, "ADDRMAP" },
91  { EVENT_DESCCHANGED, "DESCCHANGED" },
92  { EVENT_NS, "NS" },
93  { EVENT_STATUS_GENERAL, "STATUS_GENERAL" },
94  { EVENT_STATUS_CLIENT, "STATUS_CLIENT" },
95  { EVENT_STATUS_SERVER, "STATUS_SERVER" },
96  { EVENT_GUARD, "GUARD" },
97  { EVENT_STREAM_BANDWIDTH_USED, "STREAM_BW" },
98  { EVENT_CLIENTS_SEEN, "CLIENTS_SEEN" },
99  { EVENT_NEWCONSENSUS, "NEWCONSENSUS" },
100  { EVENT_BUILDTIMEOUT_SET, "BUILDTIMEOUT_SET" },
101  { EVENT_GOT_SIGNAL, "SIGNAL" },
102  { EVENT_CONF_CHANGED, "CONF_CHANGED"},
103  { EVENT_CONN_BW, "CONN_BW" },
104  { EVENT_CELL_STATS, "CELL_STATS" },
105  { EVENT_CIRC_BANDWIDTH_USED, "CIRC_BW" },
106  { EVENT_TRANSPORT_LAUNCHED, "TRANSPORT_LAUNCHED" },
107  { EVENT_HS_DESC, "HS_DESC" },
108  { EVENT_HS_DESC_CONTENT, "HS_DESC_CONTENT" },
109  { EVENT_NETWORK_LIVENESS, "NETWORK_LIVENESS" },
110  { 0, NULL },
111 };
112 
114 static inline int
116 {
117  switch (severity) {
118  case LOG_DEBUG: return EVENT_DEBUG_MSG;
119  case LOG_INFO: return EVENT_INFO_MSG;
120  case LOG_NOTICE: return EVENT_NOTICE_MSG;
121  case LOG_WARN: return EVENT_WARN_MSG;
122  case LOG_ERR: return EVENT_ERR_MSG;
123  default: return -1;
124  }
125 }
126 
128 static void
130 {
131  origin_circuit_t *ocirc;
132  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
133  if (!CIRCUIT_IS_ORIGIN(circ))
134  continue;
135  ocirc = TO_ORIGIN_CIRCUIT(circ);
136  ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
139  }
140  SMARTLIST_FOREACH_END(circ);
141 }
142 
145 void
147 {
148  smartlist_t *conns = get_connection_array();
149  event_mask_t old_mask, new_mask;
150  old_mask = global_event_mask;
151  int any_old_per_sec_events = control_any_per_second_event_enabled();
152 
153  global_event_mask = 0;
154  SMARTLIST_FOREACH(conns, connection_t *, _conn,
155  {
156  if (_conn->type == CONN_TYPE_CONTROL &&
157  STATE_IS_OPEN(_conn->state)) {
158  control_connection_t *conn = TO_CONTROL_CONN(_conn);
159  global_event_mask |= conn->event_mask;
160  }
161  });
162 
163  new_mask = global_event_mask;
164 
165  /* Handle the aftermath. Set up the log callback to tell us only what
166  * we want to hear...*/
168 
169  /* Macro: true if ev was false before and is true now. */
170 #define NEWLY_ENABLED(ev) \
171  (! (old_mask & (ev)) && (new_mask & (ev)))
172 
173  /* ...then, if we've started logging stream or circ bw, clear the
174  * appropriate fields. */
175  if (NEWLY_ENABLED(EVENT_STREAM_BANDWIDTH_USED)) {
176  SMARTLIST_FOREACH(conns, connection_t *, conn,
177  {
178  if (conn->type == CONN_TYPE_AP) {
179  edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
180  edge_conn->n_written = edge_conn->n_read = 0;
181  }
182  });
183  }
184  if (NEWLY_ENABLED(EVENT_CIRC_BANDWIDTH_USED)) {
186  }
187  if (NEWLY_ENABLED(EVENT_BANDWIDTH_USED)) {
188  uint64_t r, w;
190  }
191  if (any_old_per_sec_events != control_any_per_second_event_enabled()) {
192  rescan_periodic_events(get_options());
193  }
194 
195 #undef NEWLY_ENABLED
196 }
197 
200 static inline int
202 {
203  switch (event) {
204  case EVENT_DEBUG_MSG: return LOG_DEBUG;
205  case EVENT_INFO_MSG: return LOG_INFO;
206  case EVENT_NOTICE_MSG: return LOG_NOTICE;
207  case EVENT_WARN_MSG: return LOG_WARN;
208  case EVENT_ERR_MSG: return LOG_ERR;
209  default: return -1;
210  }
211 }
212 
216 void
218 {
219  int i;
220  int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
221 
222  for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
223  if (EVENT_IS_INTERESTING(i)) {
224  min_log_event = i;
225  break;
226  }
227  }
228  for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
229  if (EVENT_IS_INTERESTING(i)) {
230  max_log_event = i;
231  break;
232  }
233  }
234  if (EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL)) {
235  if (min_log_event > EVENT_NOTICE_MSG)
236  min_log_event = EVENT_NOTICE_MSG;
237  if (max_log_event < EVENT_ERR_MSG)
238  max_log_event = EVENT_ERR_MSG;
239  }
240  if (min_log_event <= max_log_event)
242  event_to_log_severity(max_log_event),
244  else
247 }
248 
253 int
255 {
256  return EVENT_IS_INTERESTING(event);
257 }
258 
260 int
262 {
264  EVENT_MASK_(EVENT_BANDWIDTH_USED) |
265  EVENT_MASK_(EVENT_CELL_STATS) |
266  EVENT_MASK_(EVENT_CIRC_BANDWIDTH_USED) |
267  EVENT_MASK_(EVENT_CONN_BW) |
268  EVENT_MASK_(EVENT_STREAM_BANDWIDTH_USED)
269  );
270 }
271 
272 /* The value of 'get_bytes_read()' the previous time that
273  * control_get_bytes_rw_last_sec() as called. */
274 static uint64_t stats_prev_n_read = 0;
275 /* The value of 'get_bytes_written()' the previous time that
276  * control_get_bytes_rw_last_sec() as called. */
277 static uint64_t stats_prev_n_written = 0;
278 
285 static void
287  uint64_t *n_written)
288 {
289  const uint64_t stats_n_bytes_read = get_bytes_read();
290  const uint64_t stats_n_bytes_written = get_bytes_written();
291 
292  *n_read = stats_n_bytes_read - stats_prev_n_read;
293  *n_written = stats_n_bytes_written - stats_prev_n_written;
294  stats_prev_n_read = stats_n_bytes_read;
295  stats_prev_n_written = stats_n_bytes_written;
296 }
297 
302 void
304 {
306  return;
307 
308  uint64_t bytes_read, bytes_written;
309  control_get_bytes_rw_last_sec(&bytes_read, &bytes_written);
310  control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
311 
316 }
317 
320 typedef struct queued_event_s {
321  uint16_t event;
322  char *msg;
324 
328 
332 
335 
338 
342 
343 void
344 control_initialize_event_queue(void)
345 {
346  if (queued_control_events == NULL) {
347  queued_control_events = smartlist_new();
348  }
349 
350  if (flush_queued_events_event == NULL) {
351  struct event_base *b = tor_libevent_get_base();
352  if (b) {
356  }
357  }
358 
359  if (queued_control_events_lock == NULL) {
362  }
363 }
364 
365 static int *
366 get_block_event_queue(void)
367 {
369  if (PREDICT_UNLIKELY(val == NULL)) {
370  val = tor_malloc_zero(sizeof(int));
372  }
373  return val;
374 }
375 
387 MOCK_IMPL(STATIC void,
388 queue_control_event_string,(uint16_t event, char *msg))
389 {
390  /* This is redundant with checks done elsewhere, but it's a last-ditch
391  * attempt to avoid queueing something we shouldn't have to queue. */
392  if (PREDICT_UNLIKELY( ! EVENT_IS_INTERESTING(event) )) {
393  tor_free(msg);
394  return;
395  }
396 
397  int *block_event_queue = get_block_event_queue();
398  if (*block_event_queue) {
399  tor_free(msg);
400  return;
401  }
402 
403  queued_event_t *ev = tor_malloc(sizeof(*ev));
404  ev->event = event;
405  ev->msg = msg;
406 
407  /* No queueing an event while queueing an event */
408  ++*block_event_queue;
409 
413 
414  int activate_event = 0;
416  activate_event = 1;
418  }
419 
421 
422  --*block_event_queue;
423 
424  /* We just put an event on the queue; mark the queue to be
425  * flushed. We only do this from the main thread for now; otherwise,
426  * we'd need to incur locking overhead in Libevent or use a socket.
427  */
428  if (activate_event) {
431  }
432 }
433 
434 #define queued_event_free(ev) \
435  FREE_AND_NULL(queued_event_t, queued_event_free_, (ev))
436 
438 static void
440 {
441  if (ev == NULL)
442  return;
443 
444  tor_free(ev->msg);
445  tor_free(ev);
446 }
447 
452 static void
454 {
455  /* Make sure that we get all the pending log events, if there are any. */
457 
458  if (PREDICT_UNLIKELY(queued_control_events == NULL)) {
459  return;
460  }
461  smartlist_t *all_conns = get_connection_array();
462  smartlist_t *controllers = smartlist_new();
463  smartlist_t *queued_events;
464 
465  int *block_event_queue = get_block_event_queue();
466  ++*block_event_queue;
467 
469  /* No queueing an event while flushing events. */
471  queued_events = queued_control_events;
472  queued_control_events = smartlist_new();
474 
475  /* Gather all the controllers that will care... */
476  SMARTLIST_FOREACH_BEGIN(all_conns, connection_t *, conn) {
477  if (conn->type == CONN_TYPE_CONTROL &&
478  !conn->marked_for_close &&
479  conn->state == CONTROL_CONN_STATE_OPEN) {
480  control_connection_t *control_conn = TO_CONTROL_CONN(conn);
481 
482  smartlist_add(controllers, control_conn);
483  }
484  } SMARTLIST_FOREACH_END(conn);
485 
486  SMARTLIST_FOREACH_BEGIN(queued_events, queued_event_t *, ev) {
487  const event_mask_t bit = ((event_mask_t)1) << ev->event;
488  const size_t msg_len = strlen(ev->msg);
490  control_conn) {
491  if (control_conn->event_mask & bit) {
492  connection_buf_add(ev->msg, msg_len, TO_CONN(control_conn));
493  }
494  } SMARTLIST_FOREACH_END(control_conn);
495 
496  queued_event_free(ev);
497  } SMARTLIST_FOREACH_END(ev);
498 
499  if (force) {
501  control_conn) {
502  connection_flush(TO_CONN(control_conn));
503  } SMARTLIST_FOREACH_END(control_conn);
504  }
505 
506  smartlist_free(queued_events);
507  smartlist_free(controllers);
508 
509  --*block_event_queue;
510 }
511 
514 static void
516 {
517  (void) event;
518  (void) arg;
520 }
521 
527 MOCK_IMPL(STATIC void,
528 send_control_event_string,(uint16_t event,
529  const char *msg))
530 {
531  tor_assert(event >= EVENT_MIN_ && event <= EVENT_MAX_);
532  queue_control_event_string(event, tor_strdup(msg));
533 }
534 
539 static void
540 send_control_event_impl(uint16_t event,
541  const char *format, va_list ap)
542 {
543  char *buf = NULL;
544  int len;
545 
546  len = tor_vasprintf(&buf, format, ap);
547  if (len < 0) {
548  log_warn(LD_BUG, "Unable to format event for controller.");
549  return;
550  }
551 
552  queue_control_event_string(event, buf);
553 }
554 
558 static void
559 send_control_event(uint16_t event,
560  const char *format, ...)
561 {
562  va_list ap;
563  va_start(ap, format);
564  send_control_event_impl(event, format, ap);
565  va_end(ap);
566 }
567 
570 int
572  int reason_code)
573 {
574  const char *status;
575  char reasons[64] = "";
576 
577  if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
578  return 0;
579  tor_assert(circ);
580 
581  switch (tp)
582  {
583  case CIRC_EVENT_LAUNCHED: status = "LAUNCHED"; break;
584  case CIRC_EVENT_BUILT: status = "BUILT"; break;
585  case CIRC_EVENT_EXTENDED: status = "EXTENDED"; break;
586  case CIRC_EVENT_FAILED: status = "FAILED"; break;
587  case CIRC_EVENT_CLOSED: status = "CLOSED"; break;
588  default:
589  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
591  return 0;
592  }
593 
594  if (tp == CIRC_EVENT_FAILED || tp == CIRC_EVENT_CLOSED) {
595  const char *reason_str = circuit_end_reason_to_control_string(reason_code);
596  char unk_reason_buf[16];
597  if (!reason_str) {
598  tor_snprintf(unk_reason_buf, 16, "UNKNOWN_%d", reason_code);
599  reason_str = unk_reason_buf;
600  }
601  if (reason_code > 0 && reason_code & END_CIRC_REASON_FLAG_REMOTE) {
602  tor_snprintf(reasons, sizeof(reasons),
603  " REASON=DESTROYED REMOTE_REASON=%s", reason_str);
604  } else {
605  tor_snprintf(reasons, sizeof(reasons),
606  " REASON=%s", reason_str);
607  }
608  }
609 
610  {
611  char *circdesc = circuit_describe_status_for_controller(circ);
612  const char *sp = strlen(circdesc) ? " " : "";
613  send_control_event(EVENT_CIRCUIT_STATUS,
614  "650 CIRC %lu %s%s%s%s\r\n",
615  (unsigned long)circ->global_identifier,
616  status, sp,
617  circdesc,
618  reasons);
619  tor_free(circdesc);
620  }
621 
622  return 0;
623 }
624 
627 static int
630  int purpose, const struct timeval *tv)
631 {
632  const char *event_desc;
633  char event_tail[160] = "";
634  if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS_MINOR))
635  return 0;
636  tor_assert(circ);
637 
638  switch (e)
639  {
640  case CIRC_MINOR_EVENT_PURPOSE_CHANGED:
641  event_desc = "PURPOSE_CHANGED";
642 
643  {
644  /* event_tail can currently be up to 68 chars long */
645  const char *hs_state_str =
647  tor_snprintf(event_tail, sizeof(event_tail),
648  " OLD_PURPOSE=%s%s%s",
650  (hs_state_str != NULL) ? " OLD_HS_STATE=" : "",
651  (hs_state_str != NULL) ? hs_state_str : "");
652  }
653 
654  break;
655  case CIRC_MINOR_EVENT_CANNIBALIZED:
656  event_desc = "CANNIBALIZED";
657 
658  {
659  /* event_tail can currently be up to 130 chars long */
660  const char *hs_state_str =
662  const struct timeval *old_timestamp_began = tv;
663  char tbuf[ISO_TIME_USEC_LEN+1];
664  format_iso_time_nospace_usec(tbuf, old_timestamp_began);
665 
666  tor_snprintf(event_tail, sizeof(event_tail),
667  " OLD_PURPOSE=%s%s%s OLD_TIME_CREATED=%s",
669  (hs_state_str != NULL) ? " OLD_HS_STATE=" : "",
670  (hs_state_str != NULL) ? hs_state_str : "",
671  tbuf);
672  }
673 
674  break;
675  default:
676  log_warn(LD_BUG, "Unrecognized status code %d", (int)e);
678  return 0;
679  }
680 
681  {
682  char *circdesc = circuit_describe_status_for_controller(circ);
683  const char *sp = strlen(circdesc) ? " " : "";
684  send_control_event(EVENT_CIRCUIT_STATUS_MINOR,
685  "650 CIRC_MINOR %lu %s%s%s%s\r\n",
686  (unsigned long)circ->global_identifier,
687  event_desc, sp,
688  circdesc,
689  event_tail);
690  tor_free(circdesc);
691  }
692 
693  return 0;
694 }
695 
700 int
702  int old_purpose)
703 {
705  CIRC_MINOR_EVENT_PURPOSE_CHANGED,
706  old_purpose,
707  NULL);
708 }
709 
714 int
716  int old_purpose,
717  const struct timeval *old_tv_created)
718 {
720  CIRC_MINOR_EVENT_CANNIBALIZED,
721  old_purpose,
722  old_tv_created);
723 }
724 
727 int
729  int reason_code)
730 {
731  char reason_buf[64];
732  char addrport_buf[64];
733  const char *status;
734  circuit_t *circ;
735  origin_circuit_t *origin_circ = NULL;
736  char buf[256];
737  const char *purpose = "";
738  tor_assert(conn->socks_request);
739 
740  if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
741  return 0;
742 
743  if (tp == STREAM_EVENT_CLOSED &&
745  return 0;
746 
747  write_stream_target_to_buf(conn, buf, sizeof(buf));
748 
749  reason_buf[0] = '\0';
750  switch (tp)
751  {
752  case STREAM_EVENT_SENT_CONNECT: status = "SENTCONNECT"; break;
753  case STREAM_EVENT_SENT_RESOLVE: status = "SENTRESOLVE"; break;
754  case STREAM_EVENT_SUCCEEDED: status = "SUCCEEDED"; break;
755  case STREAM_EVENT_FAILED: status = "FAILED"; break;
756  case STREAM_EVENT_CLOSED: status = "CLOSED"; break;
757  case STREAM_EVENT_NEW: status = "NEW"; break;
758  case STREAM_EVENT_NEW_RESOLVE: status = "NEWRESOLVE"; break;
759  case STREAM_EVENT_FAILED_RETRIABLE: status = "DETACHED"; break;
760  case STREAM_EVENT_REMAP: status = "REMAP"; break;
761  default:
762  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
763  return 0;
764  }
765  if (reason_code && (tp == STREAM_EVENT_FAILED ||
766  tp == STREAM_EVENT_CLOSED ||
767  tp == STREAM_EVENT_FAILED_RETRIABLE)) {
768  const char *reason_str = stream_end_reason_to_control_string(reason_code);
769  char *r = NULL;
770  if (!reason_str) {
771  tor_asprintf(&r, " UNKNOWN_%d", reason_code);
772  reason_str = r;
773  }
774  if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
775  tor_snprintf(reason_buf, sizeof(reason_buf),
776  " REASON=END REMOTE_REASON=%s", reason_str);
777  else
778  tor_snprintf(reason_buf, sizeof(reason_buf),
779  " REASON=%s", reason_str);
780  tor_free(r);
781  } else if (reason_code && tp == STREAM_EVENT_REMAP) {
782  switch (reason_code) {
784  strlcpy(reason_buf, " SOURCE=CACHE", sizeof(reason_buf));
785  break;
787  strlcpy(reason_buf, " SOURCE=EXIT", sizeof(reason_buf));
788  break;
789  default:
790  tor_snprintf(reason_buf, sizeof(reason_buf), " REASON=UNKNOWN_%d",
791  reason_code);
792  /* XXX do we want SOURCE=UNKNOWN_%d above instead? -RD */
793  break;
794  }
795  }
796 
797  if (tp == STREAM_EVENT_NEW || tp == STREAM_EVENT_NEW_RESOLVE) {
798  /*
799  * When the control conn is an AF_UNIX socket and we have no address,
800  * it gets set to "(Tor_internal)"; see dnsserv_launch_request() in
801  * dnsserv.c.
802  */
803  if (strcmp(ENTRY_TO_CONN(conn)->address, "(Tor_internal)") != 0) {
804  tor_snprintf(addrport_buf,sizeof(addrport_buf), " SOURCE_ADDR=%s:%d",
805  ENTRY_TO_CONN(conn)->address, ENTRY_TO_CONN(conn)->port);
806  } else {
807  /*
808  * else leave it blank so control on AF_UNIX doesn't need to make
809  * something up.
810  */
811  addrport_buf[0] = '\0';
812  }
813  } else {
814  addrport_buf[0] = '\0';
815  }
816 
817  if (tp == STREAM_EVENT_NEW_RESOLVE) {
818  purpose = " PURPOSE=DNS_REQUEST";
819  } else if (tp == STREAM_EVENT_NEW) {
820  if (conn->use_begindir) {
821  connection_t *linked = ENTRY_TO_CONN(conn)->linked_conn;
822  int linked_dir_purpose = -1;
823  if (linked && linked->type == CONN_TYPE_DIR)
824  linked_dir_purpose = linked->purpose;
825  if (DIR_PURPOSE_IS_UPLOAD(linked_dir_purpose))
826  purpose = " PURPOSE=DIR_UPLOAD";
827  else
828  purpose = " PURPOSE=DIR_FETCH";
829  } else
830  purpose = " PURPOSE=USER";
831  }
832 
833  circ = circuit_get_by_edge_conn(ENTRY_TO_EDGE_CONN(conn));
834  if (circ && CIRCUIT_IS_ORIGIN(circ))
835  origin_circ = TO_ORIGIN_CIRCUIT(circ);
836  send_control_event(EVENT_STREAM_STATUS,
837  "650 STREAM %"PRIu64" %s %lu %s%s%s%s\r\n",
838  (ENTRY_TO_CONN(conn)->global_identifier),
839  status,
840  origin_circ?
841  (unsigned long)origin_circ->global_identifier : 0ul,
842  buf, reason_buf, addrport_buf, purpose);
843 
844  /* XXX need to specify its intended exit, etc? */
845 
846  return 0;
847 }
848 
854 int
856  int reason)
857 {
858  int ncircs = 0;
859  const char *status;
860  char name[128];
861  char ncircs_buf[32] = {0}; /* > 8 + log10(2^32)=10 + 2 */
862 
863  if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
864  return 0;
865 
866  switch (tp)
867  {
868  case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break;
869  case OR_CONN_EVENT_CONNECTED: status = "CONNECTED"; break;
870  case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
871  case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
872  case OR_CONN_EVENT_NEW: status = "NEW"; break;
873  default:
874  log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
875  return 0;
876  }
877  if (conn->chan) {
878  ncircs = circuit_count_pending_on_channel(TLS_CHAN_TO_BASE(conn->chan));
879  } else {
880  ncircs = 0;
881  }
882  ncircs += connection_or_get_num_circuits(conn);
883  if (ncircs && (tp == OR_CONN_EVENT_FAILED || tp == OR_CONN_EVENT_CLOSED)) {
884  tor_snprintf(ncircs_buf, sizeof(ncircs_buf), " NCIRCS=%d", ncircs);
885  }
886 
887  orconn_target_get_name(name, sizeof(name), conn);
888  send_control_event(EVENT_OR_CONN_STATUS,
889  "650 ORCONN %s %s%s%s%s ID=%"PRIu64"\r\n",
890  name, status,
891  reason ? " REASON=" : "",
893  ncircs_buf,
894  (conn->base_.global_identifier));
895 
896  return 0;
897 }
898 
902 int
904 {
905  struct timeval now;
906  char tbuf[ISO_TIME_USEC_LEN+1];
907  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
908  if (!edge_conn->n_read && !edge_conn->n_written)
909  return 0;
910 
911  tor_gettimeofday(&now);
912  format_iso_time_nospace_usec(tbuf, &now);
913  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
914  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
915  (edge_conn->base_.global_identifier),
916  (unsigned long)edge_conn->n_read,
917  (unsigned long)edge_conn->n_written,
918  tbuf);
919 
920  edge_conn->n_written = edge_conn->n_read = 0;
921  }
922 
923  return 0;
924 }
925 
928 int
930 {
931  if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
932  smartlist_t *conns = get_connection_array();
933  edge_connection_t *edge_conn;
934  struct timeval now;
935  char tbuf[ISO_TIME_USEC_LEN+1];
936 
937  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn)
938  {
939  if (conn->type != CONN_TYPE_AP)
940  continue;
941  edge_conn = TO_EDGE_CONN(conn);
942  if (!edge_conn->n_read && !edge_conn->n_written)
943  continue;
944 
945  tor_gettimeofday(&now);
946  format_iso_time_nospace_usec(tbuf, &now);
947  send_control_event(EVENT_STREAM_BANDWIDTH_USED,
948  "650 STREAM_BW %"PRIu64" %lu %lu %s\r\n",
949  (edge_conn->base_.global_identifier),
950  (unsigned long)edge_conn->n_read,
951  (unsigned long)edge_conn->n_written,
952  tbuf);
953 
954  edge_conn->n_written = edge_conn->n_read = 0;
955  }
956  SMARTLIST_FOREACH_END(conn);
957  }
958 
959  return 0;
960 }
961 
964 int
966 {
967  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
968  return 0;
969 
970  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
971  if (!CIRCUIT_IS_ORIGIN(circ))
972  continue;
973 
975  }
976  SMARTLIST_FOREACH_END(circ);
977 
978  return 0;
979 }
980 
989 int
991 {
992  struct timeval now;
993  char tbuf[ISO_TIME_USEC_LEN+1];
994 
995  tor_assert(ocirc);
996 
997  if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
998  return 0;
999 
1000  /* n_read_circ_bw and n_written_circ_bw are always updated
1001  * when there is any new cell on a circuit, and set to 0 after
1002  * the event, below.
1003  *
1004  * Therefore, checking them is sufficient to determine if there
1005  * is new data to report. */
1006  if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
1007  return 0;
1008 
1009  tor_gettimeofday(&now);
1010  format_iso_time_nospace_usec(tbuf, &now);
1011  send_control_event(EVENT_CIRC_BANDWIDTH_USED,
1012  "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu TIME=%s "
1013  "DELIVERED_READ=%lu OVERHEAD_READ=%lu "
1014  "DELIVERED_WRITTEN=%lu OVERHEAD_WRITTEN=%lu\r\n",
1015  ocirc->global_identifier,
1016  (unsigned long)ocirc->n_read_circ_bw,
1017  (unsigned long)ocirc->n_written_circ_bw,
1018  tbuf,
1019  (unsigned long)ocirc->n_delivered_read_circ_bw,
1020  (unsigned long)ocirc->n_overhead_read_circ_bw,
1021  (unsigned long)ocirc->n_delivered_written_circ_bw,
1022  (unsigned long)ocirc->n_overhead_written_circ_bw);
1023  ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
1026 
1027  return 0;
1028 }
1029 
1032 int
1034 {
1035  const char *conn_type_str;
1036  if (!get_options()->TestingEnableConnBwEvent ||
1037  !EVENT_IS_INTERESTING(EVENT_CONN_BW))
1038  return 0;
1039  if (!conn->n_read_conn_bw && !conn->n_written_conn_bw)
1040  return 0;
1041  switch (conn->type) {
1042  case CONN_TYPE_OR:
1043  conn_type_str = "OR";
1044  break;
1045  case CONN_TYPE_DIR:
1046  conn_type_str = "DIR";
1047  break;
1048  case CONN_TYPE_EXIT:
1049  conn_type_str = "EXIT";
1050  break;
1051  default:
1052  return 0;
1053  }
1054  send_control_event(EVENT_CONN_BW,
1055  "650 CONN_BW ID=%"PRIu64" TYPE=%s "
1056  "READ=%lu WRITTEN=%lu\r\n",
1057  (conn->global_identifier),
1058  conn_type_str,
1059  (unsigned long)conn->n_read_conn_bw,
1060  (unsigned long)conn->n_written_conn_bw);
1061  conn->n_written_conn_bw = conn->n_read_conn_bw = 0;
1062  return 0;
1063 }
1064 
1067 int
1069 {
1070  if (get_options()->TestingEnableConnBwEvent &&
1071  EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
1072  SMARTLIST_FOREACH(get_connection_array(), connection_t *, conn,
1074  }
1075  return 0;
1076 }
1077 
1082 void
1083 sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
1084 {
1085  memset(cell_stats, 0, sizeof(cell_stats_t));
1087  const testing_cell_stats_entry_t *, ent) {
1088  tor_assert(ent->command <= CELL_COMMAND_MAX_);
1089  if (!ent->removed && !ent->exitward) {
1090  cell_stats->added_cells_appward[ent->command] += 1;
1091  } else if (!ent->removed && ent->exitward) {
1092  cell_stats->added_cells_exitward[ent->command] += 1;
1093  } else if (!ent->exitward) {
1094  cell_stats->removed_cells_appward[ent->command] += 1;
1095  cell_stats->total_time_appward[ent->command] += ent->waiting_time * 10;
1096  } else {
1097  cell_stats->removed_cells_exitward[ent->command] += 1;
1098  cell_stats->total_time_exitward[ent->command] += ent->waiting_time * 10;
1099  }
1100  } SMARTLIST_FOREACH_END(ent);
1102 }
1103 
1113 void
1114 append_cell_stats_by_command(smartlist_t *event_parts, const char *key,
1115  const uint64_t *include_if_non_zero,
1116  const uint64_t *number_to_include)
1117 {
1118  smartlist_t *key_value_strings = smartlist_new();
1119  int i;
1120  for (i = 0; i <= CELL_COMMAND_MAX_; i++) {
1121  if (include_if_non_zero[i] > 0) {
1122  smartlist_add_asprintf(key_value_strings, "%s:%"PRIu64,
1124  (number_to_include[i]));
1125  }
1126  }
1127  if (smartlist_len(key_value_strings) > 0) {
1128  char *joined = smartlist_join_strings(key_value_strings, ",", 0, NULL);
1129  smartlist_add_asprintf(event_parts, "%s=%s", key, joined);
1130  SMARTLIST_FOREACH(key_value_strings, char *, cp, tor_free(cp));
1131  tor_free(joined);
1132  }
1133  smartlist_free(key_value_strings);
1134 }
1135 
1138 void
1139 format_cell_stats(char **event_string, circuit_t *circ,
1140  cell_stats_t *cell_stats)
1141 {
1142  smartlist_t *event_parts = smartlist_new();
1143  if (CIRCUIT_IS_ORIGIN(circ)) {
1144  origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1145  smartlist_add_asprintf(event_parts, "ID=%lu",
1146  (unsigned long)ocirc->global_identifier);
1147  } else if (TO_OR_CIRCUIT(circ)->p_chan) {
1148  or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1149  smartlist_add_asprintf(event_parts, "InboundQueue=%lu",
1150  (unsigned long)or_circ->p_circ_id);
1151  smartlist_add_asprintf(event_parts, "InboundConn=%"PRIu64,
1152  (or_circ->p_chan->global_identifier));
1153  append_cell_stats_by_command(event_parts, "InboundAdded",
1154  cell_stats->added_cells_appward,
1155  cell_stats->added_cells_appward);
1156  append_cell_stats_by_command(event_parts, "InboundRemoved",
1157  cell_stats->removed_cells_appward,
1158  cell_stats->removed_cells_appward);
1159  append_cell_stats_by_command(event_parts, "InboundTime",
1160  cell_stats->removed_cells_appward,
1161  cell_stats->total_time_appward);
1162  }
1163  if (circ->n_chan) {
1164  smartlist_add_asprintf(event_parts, "OutboundQueue=%lu",
1165  (unsigned long)circ->n_circ_id);
1166  smartlist_add_asprintf(event_parts, "OutboundConn=%"PRIu64,
1167  (circ->n_chan->global_identifier));
1168  append_cell_stats_by_command(event_parts, "OutboundAdded",
1169  cell_stats->added_cells_exitward,
1170  cell_stats->added_cells_exitward);
1171  append_cell_stats_by_command(event_parts, "OutboundRemoved",
1172  cell_stats->removed_cells_exitward,
1173  cell_stats->removed_cells_exitward);
1174  append_cell_stats_by_command(event_parts, "OutboundTime",
1175  cell_stats->removed_cells_exitward,
1176  cell_stats->total_time_exitward);
1177  }
1178  *event_string = smartlist_join_strings(event_parts, " ", 0, NULL);
1179  SMARTLIST_FOREACH(event_parts, char *, cp, tor_free(cp));
1180  smartlist_free(event_parts);
1181 }
1182 
1185 int
1187 {
1188  cell_stats_t *cell_stats;
1189  char *event_string;
1190  if (!get_options()->TestingEnableCellStatsEvent ||
1191  !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
1192  return 0;
1193  cell_stats = tor_malloc(sizeof(cell_stats_t));
1194  SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, circ) {
1195  if (!circ->testing_cell_stats)
1196  continue;
1197  sum_up_cell_stats_by_command(circ, cell_stats);
1198  format_cell_stats(&event_string, circ, cell_stats);
1199  send_control_event(EVENT_CELL_STATS,
1200  "650 CELL_STATS %s\r\n", event_string);
1201  tor_free(event_string);
1202  }
1203  SMARTLIST_FOREACH_END(circ);
1204  tor_free(cell_stats);
1205  return 0;
1206 }
1207 
1208 /* about 5 minutes worth. */
1209 #define N_BW_EVENTS_TO_CACHE 300
1210 /* Index into cached_bw_events to next write. */
1211 static int next_measurement_idx = 0;
1212 /* number of entries set in n_measurements */
1213 static int n_measurements = 0;
1214 static struct cached_bw_event_s {
1215  uint32_t n_read;
1216  uint32_t n_written;
1217 } cached_bw_events[N_BW_EVENTS_TO_CACHE];
1218 
1221 int
1222 control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
1223 {
1224  cached_bw_events[next_measurement_idx].n_read = n_read;
1225  cached_bw_events[next_measurement_idx].n_written = n_written;
1226  if (++next_measurement_idx == N_BW_EVENTS_TO_CACHE)
1227  next_measurement_idx = 0;
1228  if (n_measurements < N_BW_EVENTS_TO_CACHE)
1229  ++n_measurements;
1230 
1231  if (EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED)) {
1232  send_control_event(EVENT_BANDWIDTH_USED,
1233  "650 BW %lu %lu\r\n",
1234  (unsigned long)n_read,
1235  (unsigned long)n_written);
1236  }
1237 
1238  return 0;
1239 }
1240 
1241 char *
1242 get_bw_samples(void)
1243 {
1244  int i;
1245  int idx = (next_measurement_idx + N_BW_EVENTS_TO_CACHE - n_measurements)
1246  % N_BW_EVENTS_TO_CACHE;
1247  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1248 
1249  smartlist_t *elements = smartlist_new();
1250 
1251  for (i = 0; i < n_measurements; ++i) {
1252  tor_assert(0 <= idx && idx < N_BW_EVENTS_TO_CACHE);
1253  const struct cached_bw_event_s *bwe = &cached_bw_events[idx];
1254 
1255  smartlist_add_asprintf(elements, "%u,%u",
1256  (unsigned)bwe->n_read,
1257  (unsigned)bwe->n_written);
1258 
1259  idx = (idx + 1) % N_BW_EVENTS_TO_CACHE;
1260  }
1261 
1262  char *result = smartlist_join_strings(elements, " ", 0, NULL);
1263 
1264  SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
1265  smartlist_free(elements);
1266 
1267  return result;
1268 }
1269 
1273 void
1275 {
1277 }
1278 
1281 void
1283 {
1284  if (--disable_log_messages < 0)
1285  tor_assert(0);
1286 }
1287 
1289 void
1290 control_event_logmsg(int severity, log_domain_mask_t domain, const char *msg)
1291 {
1292  int event;
1293 
1294  /* Don't even think of trying to add stuff to a buffer from a cpuworker
1295  * thread. (See #25987 for plan to fix.) */
1296  if (! in_main_thread())
1297  return;
1298 
1300  return;
1301 
1302  if (domain == LD_BUG && EVENT_IS_INTERESTING(EVENT_STATUS_GENERAL) &&
1303  severity <= LOG_NOTICE) {
1304  char *esc = esc_for_log(msg);
1306  control_event_general_status(severity, "BUG REASON=%s", esc);
1308  tor_free(esc);
1309  }
1310 
1311  event = log_severity_to_event(severity);
1312  if (event >= 0 && EVENT_IS_INTERESTING(event)) {
1313  char *b = NULL;
1314  const char *s;
1315  if (strchr(msg, '\n')) {
1316  char *cp;
1317  b = tor_strdup(msg);
1318  for (cp = b; *cp; ++cp)
1319  if (*cp == '\r' || *cp == '\n')
1320  *cp = ' ';
1321  }
1322  switch (severity) {
1323  case LOG_DEBUG: s = "DEBUG"; break;
1324  case LOG_INFO: s = "INFO"; break;
1325  case LOG_NOTICE: s = "NOTICE"; break;
1326  case LOG_WARN: s = "WARN"; break;
1327  case LOG_ERR: s = "ERR"; break;
1328  default: s = "UnknownLogSeverity"; break;
1329  }
1331  send_control_event(event, "650 %s %s\r\n", s, b?b:msg);
1332  if (severity == LOG_ERR) {
1333  /* Force a flush, since we may be about to die horribly */
1335  }
1337  tor_free(b);
1338  }
1339 }
1340 
1344 void
1346 {
1347  if (! in_main_thread()) {
1348  /* We can't handle this case yet, since we're using a
1349  * mainloop_event_t to invoke queued_events_flush_all. We ought to
1350  * use a different mechanism instead: see #25987.
1351  **/
1352  return;
1353  }
1356 }
1357 
1362 int
1364 {
1365  char *msg;
1366 
1367  if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
1368  return 0;
1369 
1370  {
1371  smartlist_t *names = smartlist_new();
1372  char *ids;
1373  SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
1374  char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
1376  smartlist_add(names, b);
1377  });
1378  ids = smartlist_join_strings(names, " ", 0, NULL);
1379  tor_asprintf(&msg, "650 NEWDESC %s\r\n", ids);
1380  send_control_event_string(EVENT_NEW_DESC, msg);
1381  tor_free(ids);
1382  tor_free(msg);
1383  SMARTLIST_FOREACH(names, char *, cp, tor_free(cp));
1384  smartlist_free(names);
1385  }
1386  return 0;
1387 }
1388 
1394 int
1395 control_event_address_mapped(const char *from, const char *to, time_t expires,
1396  const char *error, const int cached)
1397 {
1398  if (!EVENT_IS_INTERESTING(EVENT_ADDRMAP))
1399  return 0;
1400 
1401  if (expires < 3 || expires == TIME_MAX)
1402  send_control_event(EVENT_ADDRMAP,
1403  "650 ADDRMAP %s %s NEVER %s%s"
1404  "CACHED=\"%s\"\r\n",
1405  from, to, error?error:"", error?" ":"",
1406  cached?"YES":"NO");
1407  else {
1408  char buf[ISO_TIME_LEN+1];
1409  char buf2[ISO_TIME_LEN+1];
1410  format_local_iso_time(buf,expires);
1411  format_iso_time(buf2,expires);
1412  send_control_event(EVENT_ADDRMAP,
1413  "650 ADDRMAP %s %s \"%s\""
1414  " %s%sEXPIRES=\"%s\" CACHED=\"%s\"\r\n",
1415  from, to, buf,
1416  error?error:"", error?" ":"",
1417  buf2, cached?"YES":"NO");
1418  }
1419 
1420  return 0;
1421 }
1425 int
1427 {
1428  if (liveness > 0) {
1429  if (get_cached_network_liveness() <= 0) {
1430  /* Update cached liveness */
1431  set_cached_network_liveness(1);
1432  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS UP");
1433  send_control_event_string(EVENT_NETWORK_LIVENESS,
1434  "650 NETWORK_LIVENESS UP\r\n");
1435  }
1436  /* else was already live, no-op */
1437  } else {
1438  if (get_cached_network_liveness() > 0) {
1439  /* Update cached liveness */
1440  set_cached_network_liveness(0);
1441  log_debug(LD_CONTROL, "Sending NETWORK_LIVENESS DOWN");
1442  send_control_event_string(EVENT_NETWORK_LIVENESS,
1443  "650 NETWORK_LIVENESS DOWN\r\n");
1444  }
1445  /* else was already dead, no-op */
1446  }
1447 
1448  return 0;
1449 }
1450 
1455 static int
1457  uint16_t event,
1458  const char *event_string)
1459 {
1460  smartlist_t *strs;
1461  char *s, *esc = NULL;
1462  if (!EVENT_IS_INTERESTING(event) || !smartlist_len(statuses))
1463  return 0;
1464 
1465  strs = smartlist_new();
1466  smartlist_add_strdup(strs, "650+");
1467  smartlist_add_strdup(strs, event_string);
1468  smartlist_add_strdup(strs, "\r\n");
1469  SMARTLIST_FOREACH(statuses, const routerstatus_t *, rs,
1470  {
1472  if (!s) continue;
1473  smartlist_add(strs, s);
1474  });
1475 
1476  s = smartlist_join_strings(strs, "", 0, NULL);
1477  write_escaped_data(s, strlen(s), &esc);
1478  SMARTLIST_FOREACH(strs, char *, cp, tor_free(cp));
1479  smartlist_free(strs);
1480  tor_free(s);
1481  send_control_event_string(event, esc);
1482  send_control_event_string(event,
1483  "650 OK\r\n");
1484 
1485  tor_free(esc);
1486  return 0;
1487 }
1488 
1491 int
1493 {
1494  return control_event_networkstatus_changed_helper(statuses, EVENT_NS, "NS");
1495 }
1496 
1499 int
1501 {
1502  if (!control_event_is_interesting(EVENT_NEWCONSENSUS))
1503  return 0;
1505  consensus->routerstatus_list, EVENT_NEWCONSENSUS, "NEWCONSENSUS");
1506 }
1507 
1509 int
1511  const char *args)
1512 {
1513  const char *type_string = NULL;
1514 
1515  if (!control_event_is_interesting(EVENT_BUILDTIMEOUT_SET))
1516  return 0;
1517 
1518  switch (type) {
1519  case BUILDTIMEOUT_SET_EVENT_COMPUTED:
1520  type_string = "COMPUTED";
1521  break;
1522  case BUILDTIMEOUT_SET_EVENT_RESET:
1523  type_string = "RESET";
1524  break;
1525  case BUILDTIMEOUT_SET_EVENT_SUSPENDED:
1526  type_string = "SUSPENDED";
1527  break;
1528  case BUILDTIMEOUT_SET_EVENT_DISCARD:
1529  type_string = "DISCARD";
1530  break;
1531  case BUILDTIMEOUT_SET_EVENT_RESUME:
1532  type_string = "RESUME";
1533  break;
1534  default:
1535  type_string = "UNKNOWN";
1536  break;
1537  }
1538 
1539  send_control_event(EVENT_BUILDTIMEOUT_SET,
1540  "650 BUILDTIMEOUT_SET %s %s\r\n",
1541  type_string, args);
1542 
1543  return 0;
1544 }
1545 
1547 int
1548 control_event_signal(uintptr_t signal_num)
1549 {
1550  const char *signal_string = NULL;
1551 
1552  if (!control_event_is_interesting(EVENT_GOT_SIGNAL))
1553  return 0;
1554 
1555  switch (signal_num) {
1556  case SIGHUP:
1557  signal_string = "RELOAD";
1558  break;
1559  case SIGUSR1:
1560  signal_string = "DUMP";
1561  break;
1562  case SIGUSR2:
1563  signal_string = "DEBUG";
1564  break;
1565  case SIGNEWNYM:
1566  signal_string = "NEWNYM";
1567  break;
1568  case SIGCLEARDNSCACHE:
1569  signal_string = "CLEARDNSCACHE";
1570  break;
1571  case SIGHEARTBEAT:
1572  signal_string = "HEARTBEAT";
1573  break;
1574  default:
1575  log_warn(LD_BUG, "Unrecognized signal %lu in control_event_signal",
1576  (unsigned long)signal_num);
1577  return -1;
1578  }
1579 
1580  send_control_event(EVENT_GOT_SIGNAL, "650 SIGNAL %s\r\n",
1581  signal_string);
1582  return 0;
1583 }
1584 
1587 int
1589 {
1590  smartlist_t *statuses;
1591  int r;
1592 
1593  if (!EVENT_IS_INTERESTING(EVENT_NS))
1594  return 0;
1595 
1596  statuses = smartlist_new();
1597  smartlist_add(statuses, (void*)rs);
1599  smartlist_free(statuses);
1600  return r;
1601 }
1602 
1605 int
1607 {
1608  send_control_event(EVENT_DESCCHANGED, "650 DESCCHANGED\r\n");
1609  return 0;
1610 }
1611 
1616 static int
1617 control_event_status(int type, int severity, const char *format, va_list args)
1618 {
1619  char *user_buf = NULL;
1620  char format_buf[160];
1621  const char *status, *sev;
1622 
1623  switch (type) {
1624  case EVENT_STATUS_GENERAL:
1625  status = "STATUS_GENERAL";
1626  break;
1627  case EVENT_STATUS_CLIENT:
1628  status = "STATUS_CLIENT";
1629  break;
1630  case EVENT_STATUS_SERVER:
1631  status = "STATUS_SERVER";
1632  break;
1633  default:
1634  log_warn(LD_BUG, "Unrecognized status type %d", type);
1635  return -1;
1636  }
1637  switch (severity) {
1638  case LOG_NOTICE:
1639  sev = "NOTICE";
1640  break;
1641  case LOG_WARN:
1642  sev = "WARN";
1643  break;
1644  case LOG_ERR:
1645  sev = "ERR";
1646  break;
1647  default:
1648  log_warn(LD_BUG, "Unrecognized status severity %d", severity);
1649  return -1;
1650  }
1651  if (tor_snprintf(format_buf, sizeof(format_buf), "650 %s %s",
1652  status, sev)<0) {
1653  log_warn(LD_BUG, "Format string too long.");
1654  return -1;
1655  }
1656  tor_vasprintf(&user_buf, format, args);
1657 
1658  send_control_event(type, "%s %s\r\n", format_buf, user_buf);
1659  tor_free(user_buf);
1660  return 0;
1661 }
1662 
1663 #define CONTROL_EVENT_STATUS_BODY(event, sev) \
1664  int r; \
1665  do { \
1666  va_list ap; \
1667  if (!EVENT_IS_INTERESTING(event)) \
1668  return 0; \
1669  \
1670  va_start(ap, format); \
1671  r = control_event_status((event), (sev), format, ap); \
1672  va_end(ap); \
1673  } while (0)
1674 
1677 int
1678 control_event_general_status(int severity, const char *format, ...)
1679 {
1680  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, severity);
1681  return r;
1682 }
1683 
1686 int
1687 control_event_general_error(const char *format, ...)
1688 {
1689  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_GENERAL, LOG_ERR);
1690  /* Force a flush, since we may be about to die horribly */
1692  return r;
1693 }
1694 
1697 int
1698 control_event_client_status(int severity, const char *format, ...)
1699 {
1700  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, severity);
1701  return r;
1702 }
1703 
1706 int
1707 control_event_client_error(const char *format, ...)
1708 {
1709  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_CLIENT, LOG_ERR);
1710  /* Force a flush, since we may be about to die horribly */
1712  return r;
1713 }
1714 
1717 int
1718 control_event_server_status(int severity, const char *format, ...)
1719 {
1720  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, severity);
1721  return r;
1722 }
1723 
1726 int
1727 control_event_server_error(const char *format, ...)
1728 {
1729  CONTROL_EVENT_STATUS_BODY(EVENT_STATUS_SERVER, LOG_ERR);
1730  /* Force a flush, since we may be about to die horribly */
1732  return r;
1733 }
1734 
1738 int
1739 control_event_guard(const char *nickname, const char *digest,
1740  const char *status)
1741 {
1742  char hbuf[HEX_DIGEST_LEN+1];
1743  base16_encode(hbuf, sizeof(hbuf), digest, DIGEST_LEN);
1744  if (!EVENT_IS_INTERESTING(EVENT_GUARD))
1745  return 0;
1746 
1747  {
1748  char buf[MAX_VERBOSE_NICKNAME_LEN+1];
1749  const node_t *node = node_get_by_id(digest);
1750  if (node) {
1751  node_get_verbose_nickname(node, buf);
1752  } else {
1753  tor_snprintf(buf, sizeof(buf), "$%s~%s", hbuf, nickname);
1754  }
1755  send_control_event(EVENT_GUARD,
1756  "650 GUARD ENTRY %s %s\r\n", buf, status);
1757  }
1758  return 0;
1759 }
1760 
1765 int
1767 {
1768  int i;
1769  char *result;
1770  smartlist_t *lines;
1771  if (!EVENT_IS_INTERESTING(EVENT_CONF_CHANGED) ||
1772  smartlist_len(elements) == 0) {
1773  return 0;
1774  }
1775  lines = smartlist_new();
1776  for (i = 0; i < smartlist_len(elements); i += 2) {
1777  char *k = smartlist_get(elements, i);
1778  char *v = smartlist_get(elements, i+1);
1779  if (v == NULL) {
1780  smartlist_add_asprintf(lines, "650-%s", k);
1781  } else {
1782  smartlist_add_asprintf(lines, "650-%s=%s", k, v);
1783  }
1784  }
1785  result = smartlist_join_strings(lines, "\r\n", 0, NULL);
1786  send_control_event(EVENT_CONF_CHANGED,
1787  "650-CONF_CHANGED\r\n%s\r\n650 OK\r\n", result);
1788  tor_free(result);
1789  SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
1790  smartlist_free(lines);
1791  return 0;
1792 }
1793 
1797 void
1798 control_event_clients_seen(const char *controller_str)
1799 {
1800  send_control_event(EVENT_CLIENTS_SEEN,
1801  "650 CLIENTS_SEEN %s\r\n", controller_str);
1802 }
1803 
1810 void
1811 control_event_transport_launched(const char *mode, const char *transport_name,
1812  tor_addr_t *addr, uint16_t port)
1813 {
1814  send_control_event(EVENT_TRANSPORT_LAUNCHED,
1815  "650 TRANSPORT_LAUNCHED %s %s %s %u\r\n",
1816  mode, transport_name, fmt_addr(addr), port);
1817 }
1818 
1821 void
1822 control_event_pt_log(const char *log)
1823 {
1824  send_control_event(EVENT_PT_LOG,
1825  "650 PT_LOG %s\r\n",
1826  log);
1827 }
1828 
1831 void
1832 control_event_pt_status(const char *status)
1833 {
1834  send_control_event(EVENT_PT_STATUS,
1835  "650 PT_STATUS %s\r\n",
1836  status);
1837 }
1838 
1841 const char *
1843 {
1844  const char *str;
1845 
1846  switch (auth_type) {
1847  case REND_NO_AUTH:
1848  str = "NO_AUTH";
1849  break;
1850  case REND_BASIC_AUTH:
1851  str = "BASIC_AUTH";
1852  break;
1853  case REND_STEALTH_AUTH:
1854  str = "STEALTH_AUTH";
1855  break;
1856  default:
1857  str = "UNKNOWN";
1858  }
1859 
1860  return str;
1861 }
1862 
1865 static const char *
1866 rend_hsaddress_str_or_unknown(const char *onion_address)
1867 {
1868  static const char *str_unknown = "UNKNOWN";
1869  const char *str_ret = str_unknown;
1870 
1871  /* No valid pointer, unknown it is. */
1872  if (!onion_address) {
1873  goto end;
1874  }
1875  /* Empty onion address thus we don't know, unknown it is. */
1876  if (onion_address[0] == '\0') {
1877  goto end;
1878  }
1879  /* All checks are good so return the given onion address. */
1880  str_ret = onion_address;
1881 
1882  end:
1883  return str_ret;
1884 }
1885 
1893 void
1894 control_event_hs_descriptor_requested(const char *onion_address,
1895  rend_auth_type_t auth_type,
1896  const char *id_digest,
1897  const char *desc_id,
1898  const char *hsdir_index)
1899 {
1900  char *hsdir_index_field = NULL;
1901 
1902  if (BUG(!id_digest || !desc_id)) {
1903  return;
1904  }
1905 
1906  if (hsdir_index) {
1907  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
1908  }
1909 
1910  send_control_event(EVENT_HS_DESC,
1911  "650 HS_DESC REQUESTED %s %s %s %s%s\r\n",
1912  rend_hsaddress_str_or_unknown(onion_address),
1913  rend_auth_type_to_string(auth_type),
1914  node_describe_longname_by_id(id_digest),
1915  desc_id,
1916  hsdir_index_field ? hsdir_index_field : "");
1917  tor_free(hsdir_index_field);
1918 }
1919 
1927 void
1928 control_event_hs_descriptor_created(const char *onion_address,
1929  const char *desc_id,
1930  int replica)
1931 {
1932  char *replica_field = NULL;
1933 
1934  if (BUG(!onion_address || !desc_id)) {
1935  return;
1936  }
1937 
1938  if (replica >= 0) {
1939  tor_asprintf(&replica_field, " REPLICA=%d", replica);
1940  }
1941 
1942  send_control_event(EVENT_HS_DESC,
1943  "650 HS_DESC CREATED %s UNKNOWN UNKNOWN %s%s\r\n",
1944  onion_address, desc_id,
1945  replica_field ? replica_field : "");
1946  tor_free(replica_field);
1947 }
1948 
1955 void
1956 control_event_hs_descriptor_upload(const char *onion_address,
1957  const char *id_digest,
1958  const char *desc_id,
1959  const char *hsdir_index)
1960 {
1961  char *hsdir_index_field = NULL;
1962 
1963  if (BUG(!onion_address || !id_digest || !desc_id)) {
1964  return;
1965  }
1966 
1967  if (hsdir_index) {
1968  tor_asprintf(&hsdir_index_field, " HSDIR_INDEX=%s", hsdir_index);
1969  }
1970 
1971  send_control_event(EVENT_HS_DESC,
1972  "650 HS_DESC UPLOAD %s UNKNOWN %s %s%s\r\n",
1973  onion_address,
1974  node_describe_longname_by_id(id_digest),
1975  desc_id,
1976  hsdir_index_field ? hsdir_index_field : "");
1977  tor_free(hsdir_index_field);
1978 }
1979 
1989 static void
1991  const char *onion_address,
1992  const char *desc_id,
1993  rend_auth_type_t auth_type,
1994  const char *hsdir_id_digest,
1995  const char *reason)
1996 {
1997  char *reason_field = NULL;
1998 
1999  if (BUG(!action || !onion_address)) {
2000  return;
2001  }
2002 
2003  if (reason) {
2004  tor_asprintf(&reason_field, " REASON=%s", reason);
2005  }
2006 
2007  send_control_event(EVENT_HS_DESC,
2008  "650 HS_DESC %s %s %s %s%s%s\r\n",
2009  action,
2010  rend_hsaddress_str_or_unknown(onion_address),
2011  rend_auth_type_to_string(auth_type),
2012  hsdir_id_digest ?
2013  node_describe_longname_by_id(hsdir_id_digest) :
2014  "UNKNOWN",
2015  desc_id ? desc_id : "",
2016  reason_field ? reason_field : "");
2017 
2018  tor_free(reason_field);
2019 }
2020 
2029 void
2031  const char *onion_address,
2032  const char *id_digest,
2033  const char *reason)
2034 {
2035  char *reason_field = NULL;
2036 
2037  if (BUG(!action || !id_digest)) {
2038  return;
2039  }
2040 
2041  if (reason) {
2042  tor_asprintf(&reason_field, " REASON=%s", reason);
2043  }
2044 
2045  send_control_event(EVENT_HS_DESC,
2046  "650 HS_DESC %s %s UNKNOWN %s%s\r\n",
2047  action,
2048  rend_hsaddress_str_or_unknown(onion_address),
2049  node_describe_longname_by_id(id_digest),
2050  reason_field ? reason_field : "");
2051 
2052  tor_free(reason_field);
2053 }
2054 
2061 static const char *
2062 get_desc_id_from_query(const rend_data_t *rend_data, const char *hsdir_fp)
2063 {
2064  int replica;
2065  const char *desc_id = NULL;
2066  const rend_data_v2_t *rend_data_v2 = TO_REND_DATA_V2(rend_data);
2067 
2068  /* Possible if the fetch was done using a descriptor ID. This means that
2069  * the HSFETCH command was used. */
2070  if (!tor_digest_is_zero(rend_data_v2->desc_id_fetch)) {
2071  desc_id = rend_data_v2->desc_id_fetch;
2072  goto end;
2073  }
2074 
2075  /* Without a directory fingerprint at this stage, we can't do much. */
2076  if (hsdir_fp == NULL) {
2077  goto end;
2078  }
2079 
2080  /* OK, we have an onion address so now let's find which descriptor ID
2081  * is the one associated with the HSDir fingerprint. */
2082  for (replica = 0; replica < REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS;
2083  replica++) {
2084  const char *digest = rend_data_get_desc_id(rend_data, replica, NULL);
2085 
2086  SMARTLIST_FOREACH_BEGIN(rend_data->hsdirs_fp, char *, fingerprint) {
2087  if (tor_memcmp(fingerprint, hsdir_fp, DIGEST_LEN) == 0) {
2088  /* Found it! This descriptor ID is the right one. */
2089  desc_id = digest;
2090  goto end;
2091  }
2092  } SMARTLIST_FOREACH_END(fingerprint);
2093  }
2094 
2095  end:
2096  return desc_id;
2097 }
2098 
2103 void
2104 control_event_hsv2_descriptor_received(const char *onion_address,
2105  const rend_data_t *rend_data,
2106  const char *hsdir_id_digest)
2107 {
2108  char *desc_id_field = NULL;
2109  const char *desc_id;
2110 
2111  if (BUG(!rend_data || !hsdir_id_digest || !onion_address)) {
2112  return;
2113  }
2114 
2115  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2116  if (desc_id != NULL) {
2117  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2118  /* Set the descriptor ID digest to base32 so we can send it. */
2119  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2120  DIGEST_LEN);
2121  /* Extra whitespace is needed before the value. */
2122  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2123  }
2124 
2125  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2126  TO_REND_DATA_V2(rend_data)->auth_type,
2127  hsdir_id_digest, NULL);
2128  tor_free(desc_id_field);
2129 }
2130 
2131 /* Send HS_DESC RECEIVED event
2132  *
2133  * Called when we successfully received a hidden service descriptor. */
2134 void
2135 control_event_hsv3_descriptor_received(const char *onion_address,
2136  const char *desc_id,
2137  const char *hsdir_id_digest)
2138 {
2139  char *desc_id_field = NULL;
2140 
2141  if (BUG(!onion_address || !desc_id || !hsdir_id_digest)) {
2142  return;
2143  }
2144 
2145  /* Because DescriptorID is an optional positional value, we need to add a
2146  * whitespace before in order to not be next to the HsDir value. */
2147  tor_asprintf(&desc_id_field, " %s", desc_id);
2148 
2149  event_hs_descriptor_receive_end("RECEIVED", onion_address, desc_id_field,
2150  REND_NO_AUTH, hsdir_id_digest, NULL);
2151  tor_free(desc_id_field);
2152 }
2153 
2158 void
2160  const char *onion_address)
2161 {
2162  if (BUG(!id_digest)) {
2163  return;
2164  }
2165 
2166  control_event_hs_descriptor_upload_end("UPLOADED", onion_address,
2167  id_digest, NULL);
2168 }
2169 
2175 void
2177  const char *hsdir_id_digest,
2178  const char *reason)
2179 {
2180  char *desc_id_field = NULL;
2181  const char *desc_id;
2182 
2183  if (BUG(!rend_data)) {
2184  return;
2185  }
2186 
2187  desc_id = get_desc_id_from_query(rend_data, hsdir_id_digest);
2188  if (desc_id != NULL) {
2189  char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
2190  /* Set the descriptor ID digest to base32 so we can send it. */
2191  base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_id,
2192  DIGEST_LEN);
2193  /* Extra whitespace is needed before the value. */
2194  tor_asprintf(&desc_id_field, " %s", desc_id_base32);
2195  }
2196 
2197  event_hs_descriptor_receive_end("FAILED", rend_data_get_address(rend_data),
2198  desc_id_field,
2199  TO_REND_DATA_V2(rend_data)->auth_type,
2200  hsdir_id_digest, reason);
2201  tor_free(desc_id_field);
2202 }
2203 
2209 void
2210 control_event_hsv3_descriptor_failed(const char *onion_address,
2211  const char *desc_id,
2212  const char *hsdir_id_digest,
2213  const char *reason)
2214 {
2215  char *desc_id_field = NULL;
2216 
2217  if (BUG(!onion_address || !desc_id || !reason)) {
2218  return;
2219  }
2220 
2221  /* Because DescriptorID is an optional positional value, we need to add a
2222  * whitespace before in order to not be next to the HsDir value. */
2223  tor_asprintf(&desc_id_field, " %s", desc_id);
2224 
2225  event_hs_descriptor_receive_end("FAILED", onion_address, desc_id_field,
2226  REND_NO_AUTH, hsdir_id_digest, reason);
2227  tor_free(desc_id_field);
2228 }
2229 
2235 void
2236 control_event_hs_descriptor_content(const char *onion_address,
2237  const char *desc_id,
2238  const char *hsdir_id_digest,
2239  const char *content)
2240 {
2241  static const char *event_name = "HS_DESC_CONTENT";
2242  char *esc_content = NULL;
2243 
2244  if (!onion_address || !desc_id) {
2245  log_warn(LD_BUG, "Called with onion_address==%p, desc_id==%p, ",
2246  onion_address, desc_id);
2247  return;
2248  }
2249 
2250  if (content == NULL) {
2251  /* Point it to empty content so it can still be escaped. */
2252  content = "";
2253  }
2254  write_escaped_data(content, strlen(content), &esc_content);
2255 
2256  send_control_event(EVENT_HS_DESC_CONTENT,
2257  "650+%s %s %s %s\r\n%s650 OK\r\n",
2258  event_name,
2259  rend_hsaddress_str_or_unknown(onion_address),
2260  desc_id,
2261  hsdir_id_digest ?
2262  node_describe_longname_by_id(hsdir_id_digest) :
2263  "UNKNOWN",
2264  esc_content);
2265  tor_free(esc_content);
2266 }
2267 
2272 void
2274  const char *onion_address,
2275  const char *reason)
2276 {
2277  if (BUG(!id_digest)) {
2278  return;
2279  }
2280  control_event_hs_descriptor_upload_end("FAILED", onion_address,
2281  id_digest, reason);
2282 }
2283 
2284 void
2285 control_events_free_all(void)
2286 {
2287  smartlist_t *queued_events = NULL;
2288 
2289  stats_prev_n_read = stats_prev_n_written = 0;
2290 
2294  queued_events = queued_control_events;
2295  queued_control_events = NULL;
2297  }
2298  if (queued_events) {
2299  SMARTLIST_FOREACH(queued_events, queued_event_t *, ev,
2300  queued_event_free(ev));
2301  smartlist_free(queued_events);
2302  }
2304  mainloop_event_free(flush_queued_events_event);
2306  }
2307  global_event_mask = 0;
2309 }
2310 
2311 #ifdef TOR_UNIT_TESTS
2312 /* For testing: change the value of global_event_mask */
2313 void
2314 control_testing_set_global_event_mask(uint64_t mask)
2315 {
2316  global_event_mask = mask;
2317 }
2318 #endif /* defined(TOR_UNIT_TESTS) */
int control_event_circ_bandwidth_used(void)
void control_event_clients_seen(const char *controller_str)
int control_event_conn_bandwidth_used(void)
static tor_mutex_t * queued_control_events_lock
Header file for channeltls.c.
static void clear_circ_bw_fields(void)
uint64_t global_identifier
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)
void tor_mutex_release(tor_mutex_t *m)
int write_stream_target_to_buf(entry_connection_t *conn, char *buf, size_t len)
Definition: control_fmt.c:32
uint32_t n_delivered_written_circ_bw
Definition: node_st.h:28
void control_event_hs_descriptor_created(const char *onion_address, const char *desc_id, int replica)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define TO_CONN(c)
Definition: or.h:735
int connection_flush(connection_t *conn)
Definition: connection.c:4252
Header file for command.c.
void format_local_iso_time(char *buf, time_t t)
Definition: time_fmt.c:285
Header file for connection.c.
static void static int static void const struct control_event_t control_event_table[]
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
uint32_t n_written_conn_bw
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:145
int control_event_circuit_cell_stats(void)
#define STATE_IS_OPEN(s)
void control_event_hs_descriptor_upload_end(const char *action, const char *onion_address, const char *id_digest, const char *reason)
#define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED
Definition: or.h:279
static uint64_t stats_n_bytes_written
Definition: mainloop.c:140
static void control_get_bytes_rw_last_sec(uint64_t *r, uint64_t *w)
#define LOG_INFO
Definition: log.h:43
Header file for describe.c.
Header file for nodelist.c.
Header file for directory.c.
void format_cell_stats(char **event_string, circuit_t *circ, cell_stats_t *cell_stats)
int tor_vasprintf(char **strp, const char *fmt, va_list args)
Definition: printf.c:96
void smartlist_add(smartlist_t *sl, void *element)
Header file for control_fmt.c.
const char * circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
Definition: circuitlist.c:838
const char * cell_command_to_string(uint8_t command)
Definition: command.c:88
#define CONTROL_CONN_STATE_OPEN
Definition: control.h:19
void circuit_clear_testing_cell_stats(circuit_t *circ)
Definition: circuitlist.c:1106
#define REND_DESC_ID_V2_LEN_BASE32
Definition: or.h:354
int circuit_count_pending_on_channel(channel_t *chan)
Definition: circuitlist.c:604
static tor_threadlocal_t block_event_queue_flag
Header file for config.c.
control_connection_t * TO_CONTROL_CONN(connection_t *c)
Definition: control.c:67
#define CONN_TYPE_OR
Definition: connection.h:24
char * networkstatus_getinfo_helper_single(const routerstatus_t *rs)
static event_mask_t global_event_mask
int control_event_stream_bandwidth(edge_connection_t *edge_conn)
void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:60
void format_iso_time_nospace_usec(char *buf, const struct timeval *tv)
Definition: time_fmt.c:323
smartlist_t * hsdirs_fp
Definition: or.h:424
stream_status_event_t
int control_event_signal(uintptr_t signal_num)
static void static int static void send_control_event(uint16_t event, const char *format,...) CHECK_PRINTF(2
void control_event_pt_status(const char *status)
void control_event_transport_launched(const char *mode, const char *transport_name, tor_addr_t *addr, uint16_t port)
int control_event_client_error(const char *format,...)
#define tor_free(p)
Definition: malloc.h:52
void enable_control_logging(void)
static void queued_events_flush_all(int force)
#define tor_fragile_assert()
Definition: util_bug.h:241
void control_event_logmsg(int severity, log_domain_mask_t domain, const char *msg)
#define LOG_NOTICE
Definition: log.h:48
Header file for mainloop.c.
static mainloop_event_t * flush_queued_events_event
void mainloop_event_activate(mainloop_event_t *event)
int control_event_descriptors_changed(smartlist_t *routers)
int in_main_thread(void)
Definition: or.h:923
int control_event_stream_bandwidth_used(void)
unsigned int purpose
Definition: connection_st.h:46
uint32_t n_overhead_read_circ_bw
int control_event_circ_bandwidth_used_for_circ(origin_circuit_t *ocirc)
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)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
rend_auth_type_t
Definition: or.h:402
int control_event_newconsensus(const networkstatus_t *consensus)
#define END_CIRC_REASON_FLAG_REMOTE
Definition: or.h:328
#define ENTRY_TO_CONN(c)
Definition: or.h:738
static void static int control_event_status(int type, int severity, const char *format, va_list args) CHECK_PRINTF(3
int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
socks_request_t * socks_request
void flush_pending_log_callbacks(void)
Definition: log.c:1072
channel_t * p_chan
Definition: or_circuit_st.h:37
void orconn_target_get_name(char *name, size_t len, or_connection_t *conn)
Definition: control_fmt.c:54
int tor_threadlocal_init(tor_threadlocal_t *threadlocal)
buildtimeout_set_event_t
void control_event_hs_descriptor_uploaded(const char *id_digest, const char *onion_address)
int control_event_general_status(int severity, const char *format,...)
tor_assert(buffer)
void router_get_verbose_nickname(char *buf, const routerinfo_t *router)
Definition: describe.c:226
#define LD_CONTROL
Definition: log.h:78
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:119
void control_per_second_events(void)
const char * rend_auth_type_to_string(rend_auth_type_t auth_type)
const char * circuit_end_reason_to_control_string(int reason)
Definition: reasons.c:324
int tor_memcmp(const void *a, const void *b, size_t len)
Definition: di_ops.c:31
void sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
circid_t n_circ_id
Definition: circuit_st.h:67
const char * stream_end_reason_to_control_string(int reason)
Definition: reasons.c:28
MOCK_IMPL(STATIC void, queue_control_event_string,(uint16_t event, char *msg))
void control_update_global_event_mask(void)
#define DIGEST_LEN
Definition: digest_sizes.h:20
static void send_control_event_impl(uint16_t event, const char *format, va_list ap) CHECK_PRINTF(2
size_t write_escaped_data(const char *data, size_t len, char **out)
Definition: control_proto.c:69
uint32_t n_delivered_read_circ_bw
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
static int disable_log_messages
int control_event_general_error(const char *format,...)
int control_event_circuit_purpose_changed(origin_circuit_t *circ, int old_purpose)
Master header file for Tor-specific functionality.
void change_callback_log_severity(int loglevelMin, int loglevelMax, log_callback cb)
Definition: log.c:1053
void control_event_hs_descriptor_content(const char *onion_address, const char *desc_id, const char *hsdir_id_digest, const char *content)
#define ANY_EVENT_IS_INTERESTING(e)
static int event_to_log_severity(int event)
int control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp, int reason)
int control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp, int reason_code)
void node_get_verbose_nickname(const node_t *node, char *verbose_name_out)
Definition: nodelist.c:1384
#define END_STREAM_REASON_FLAG_REMOTE
Definition: or.h:276
static const char * rend_hsaddress_str_or_unknown(const char *onion_address)
#define LOG_WARN
Definition: log.h:51
unsigned int type
Definition: connection_st.h:45
static int flush_queued_event_pending
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
smartlist_t * testing_cell_stats
Definition: circuit_st.h:201
#define CONN_TYPE_CONTROL
Definition: connection.h:40
uint32_t n_written_circ_bw
#define DIR_PURPOSE_IS_UPLOAD(p)
Definition: directory.h:82
char * circuit_describe_status_for_controller(origin_circuit_t *circ)
Definition: control_fmt.c:73
circid_t p_circ_id
Definition: or_circuit_st.h:33
static int log_severity_to_event(int severity)
static void flush_queued_events_cb(mainloop_event_t *event, void *arg)
Header file for circuitlist.c.
void tor_threadlocal_set(tor_threadlocal_t *threadlocal, void *value)
int control_event_client_status(int severity, const char *format,...)
#define REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS
Definition: or.h:348
void control_event_hsv2_descriptor_received(const char *onion_address, const rend_data_t *rend_data, const char *hsdir_id_digest)
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
#define LOG_ERR
Definition: log.h:54
smartlist_t * routerstatus_list
int control_event_network_liveness_update(int liveness)
static const char * get_desc_id_from_query(const rend_data_t *rend_data, const char *hsdir_fp)
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
void control_adjust_event_log_severity(void)
int control_event_networkstatus_changed_single(const routerstatus_t *rs)
Header file for connection_edge.c.
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:163
#define REMAP_STREAM_SOURCE_CACHE
static int control_event_circuit_status_minor(origin_circuit_t *circ, circuit_status_minor_event_t e, int purpose, const struct timeval *tv)
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
#define SMARTLIST_FOREACH(sl, type, var, cmd)
static uint64_t stats_n_bytes_read
Definition: mainloop.c:138
char desc_id_fetch[DIGEST_LEN]
Definition: or.h:454
uint64_t log_domain_mask_t
Definition: logging_types.h:21
int control_event_conf_changed(const smartlist_t *elements)
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
void disable_control_logging(void)
#define fmt_addr(a)
Definition: address.h:211
int control_event_is_interesting(int event)
channel_tls_t * chan
#define CONN_TYPE_EXIT
Definition: connection.h:26
void control_event_pt_log(const char *log)
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
Definition: circuitlist.c:151
void control_event_logmsg_pending(void)
Header file for control.c.
void tor_mutex_acquire(tor_mutex_t *m)
void control_event_hs_descriptor_upload_failed(const char *id_digest, const char *onion_address, const char *reason)
static int control_event_networkstatus_changed_helper(smartlist_t *statuses, uint16_t event, const char *event_string)
tor_mutex_t * tor_mutex_new(void)
Definition: compat_mutex.c:17
void control_event_hsv3_descriptor_failed(const char *onion_address, const char *desc_id, const char *hsdir_id_digest, const char *reason)
void rescan_periodic_events(const or_options_t *options)
Definition: mainloop.c:1568
#define LOG_DEBUG
Definition: log.h:40
const char * circuit_purpose_to_controller_string(uint8_t purpose)
Definition: circuitlist.c:777
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)
#define REMAP_STREAM_SOURCE_EXIT
int control_any_per_second_event_enabled(void)
static void queued_event_free_(queued_event_t *ev)
Header file for control_proto.c.
uint32_t n_read_conn_bw
int control_event_server_error(const char *format,...)
Header file for reasons.c.
void * tor_threadlocal_get(tor_threadlocal_t *threadlocal)
#define CONN_TYPE_DIR
Definition: connection.h:35
circuit_t * circuit_get_by_edge_conn(edge_connection_t *conn)
Definition: circuitlist.c:1572
Header file for connection_or.c.
struct queued_event_s queued_event_t
int control_event_server_status(int severity, const char *format,...)
char * esc_for_log(const char *s)
Definition: escape.c:30
uint32_t n_overhead_written_circ_bw
int control_event_circuit_status(origin_circuit_t *circ, circuit_status_event_t tp, int reason_code)
Header for compat_libevent.c.
#define CONN_TYPE_AP
Definition: connection.h:31
or_conn_status_event_t
Definition: orconn_event.h:59
Header file for control_events.c.
void control_event_hs_descriptor_upload(const char *onion_address, const char *id_digest, const char *desc_id, const char *hsdir_index)
const char * orconn_end_reason_to_control_string(int r)
Definition: reasons.c:225
int control_event_guard(const char *nickname, const char *digest, const char *status)
#define EVENT_IS_INTERESTING(e)
int control_event_address_mapped(const char *from, const char *to, time_t expires, const char *error, const int cached)
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:84
int control_event_my_descriptor_changed(void)
circuit_status_minor_event_t
int control_event_networkstatus_changed(smartlist_t *statuses)
static smartlist_t * queued_control_events
channel_t * n_chan
Definition: circuit_st.h:58
int control_event_circuit_cannibalized(origin_circuit_t *circ, int old_purpose, const struct timeval *old_tv_created)
int control_event_buildtimeout_set(buildtimeout_set_event_t type, const char *args)
circuit_status_event_t
Definition: ocirc_event.h:19
void control_event_hsv2_descriptor_failed(const rend_data_t *rend_data, const char *hsdir_id_digest, const char *reason)
uint64_t global_identifier
Definition: channel.h:197
int control_event_conn_bandwidth(connection_t *conn)