1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2018, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Header file for mainloop.c.
12 #ifndef TOR_MAINLOOP_H
13 #define TOR_MAINLOOP_H
15 int have_completed_a_circuit(void);
16 void note_that_we_completed_a_circuit(void);
17 void note_that_we_maybe_cant_complete_circuits(void);
19 int connection_add_impl(connection_t
*conn
, int is_connecting
);
20 #define connection_add(conn) connection_add_impl((conn), 0)
21 #define connection_add_connecting(conn) connection_add_impl((conn), 1)
22 int connection_remove(connection_t
*conn
);
23 void connection_unregister_events(connection_t
*conn
);
24 int connection_in_array(connection_t
*conn
);
25 void add_connection_to_closeable_list(connection_t
*conn
);
26 int connection_is_on_closeable_list(connection_t
*conn
);
28 MOCK_DECL(smartlist_t
*, get_connection_array
, (void));
29 MOCK_DECL(uint64_t,get_bytes_read
,(void));
30 MOCK_DECL(uint64_t,get_bytes_written
,(void));
31 void stats_increment_bytes_read_and_written(uint64_t r
, uint64_t w
);
33 /** Bitmask for events that we can turn on and off with
34 * connection_watch_events. */
35 typedef enum watchable_events
{
36 /* Yes, it is intentional that these match Libevent's EV_READ and EV_WRITE */
37 READ_EVENT
=0x02, /**< We want to know when a connection is readable */
38 WRITE_EVENT
=0x04 /**< We want to know when a connection is writable */
40 void connection_watch_events(connection_t
*conn
, watchable_events_t events
);
41 int connection_is_reading(connection_t
*conn
);
42 MOCK_DECL(void,connection_stop_reading
,(connection_t
*conn
));
43 MOCK_DECL(void,connection_start_reading
,(connection_t
*conn
));
45 int connection_is_writing(connection_t
*conn
);
46 MOCK_DECL(void,connection_stop_writing
,(connection_t
*conn
));
47 MOCK_DECL(void,connection_start_writing
,(connection_t
*conn
));
49 void tor_shutdown_event_loop_and_exit(int exitcode
);
50 int tor_event_loop_shutdown_is_pending(void);
52 void connection_stop_reading_from_linked_conn(connection_t
*conn
);
54 MOCK_DECL(int, connection_count_moribund
, (void));
56 void directory_all_unreachable(time_t now
);
57 void directory_info_has_arrived(time_t now
, int from_cache
, int suppress_logs
);
59 void ip_address_changed(int at_interface
);
60 void dns_servers_relaunch_checks(void);
61 void reset_all_main_loop_timers(void);
62 void reschedule_descriptor_update_check(void);
63 void reschedule_directory_downloads(void);
64 void reschedule_or_state_save(void);
65 void reschedule_dirvote(const or_options_t
*options
);
66 void mainloop_schedule_postloop_cleanup(void);
67 void rescan_periodic_events(const or_options_t
*options
);
68 MOCK_DECL(void, schedule_rescan_periodic_events
,(void));
70 void update_current_time(time_t now
);
72 MOCK_DECL(long,get_uptime
,(void));
73 MOCK_DECL(void,reset_uptime
,(void));
75 unsigned get_signewnym_epoch(void);
77 int do_main_loop(void);
79 void reset_main_loop_counters(void);
80 uint64_t get_main_loop_success_count(void);
81 uint64_t get_main_loop_error_count(void);
82 uint64_t get_main_loop_idle_count(void);
84 void periodic_events_on_new_options(const or_options_t
*options
);
86 void do_signewnym(time_t);
87 time_t get_last_signewnym_time(void);
89 void mainloop_schedule_shutdown(int delay_sec
);
91 void tor_init_connection_lists(void);
92 void initialize_mainloop_events(void);
93 void tor_mainloop_free_all(void);
95 struct token_bucket_rw_t
;
97 extern time_t time_of_process_start
;
98 extern int quiet_level
;
99 extern struct token_bucket_rw_t global_bucket
;
100 extern struct token_bucket_rw_t global_relayed_bucket
;
102 #ifdef MAINLOOP_PRIVATE
103 STATIC
int run_main_loop_until_done(void);
104 STATIC
void close_closeable_connections(void);
105 STATIC
void initialize_periodic_events(void);
106 STATIC
void teardown_periodic_events(void);
107 STATIC
int get_my_roles(const or_options_t
*);
108 STATIC
int check_network_participation_callback(time_t now
,
109 const or_options_t
*options
);
111 #ifdef TOR_UNIT_TESTS
112 extern smartlist_t
*connection_array
;
114 /* We need the periodic_event_item_t definition. */
115 #include "core/mainloop/periodic.h"
116 extern periodic_event_item_t periodic_events
[];
118 #endif /* defined(MAIN_PRIVATE) */