main.c 68.2 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2008, The Tor Project, Inc. */
5
6
/* See LICENSE for licensing information */
/* $Id$ */
Nick Mathewson's avatar
Nick Mathewson committed
7
8
const char main_c_id[] =
  "$Id$";
Roger Dingledine's avatar
Roger Dingledine committed
9

Roger Dingledine's avatar
Roger Dingledine committed
10
11
/**
 * \file main.c
12
13
 * \brief Toplevel module. Handles signals, multiplexes between
 * connections, implements main loop, and drives scheduled events.
Roger Dingledine's avatar
Roger Dingledine committed
14
 **/
15

Nick Mathewson's avatar
Nick Mathewson committed
16
#define MAIN_PRIVATE
Roger Dingledine's avatar
Roger Dingledine committed
17
#include "or.h"
18
19
20
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
21

22
void evdns_shutdown(int);
Roger Dingledine's avatar
Roger Dingledine committed
23

24
/********* PROTOTYPES **********/
Roger Dingledine's avatar
Roger Dingledine committed
25

26
static void dumpmemusage(int severity);
27
static void dumpstats(int severity); /* log stats */
28
29
30
31
32
static void conn_read_callback(int fd, short event, void *_conn);
static void conn_write_callback(int fd, short event, void *_conn);
static void signal_callback(int fd, short events, void *arg);
static void second_elapsed_callback(int fd, short event, void *args);
static int conn_close_if_marked(int i);
33
static void connection_start_reading_from_linked_conn(connection_t *conn);
34
static int connection_should_read_from_linked_conn(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
35

Roger Dingledine's avatar
Roger Dingledine committed
36
37
/********* START VARIABLES **********/

38
int global_read_bucket; /**< Max number of bytes I can read this second. */
39
int global_write_bucket; /**< Max number of bytes I can write this second. */
Roger Dingledine's avatar
Roger Dingledine committed
40

41
42
43
44
45
/** Max number of relayed (bandwidth class 1) bytes I can read this second. */
int global_relayed_read_bucket;
/** Max number of relayed (bandwidth class 1) bytes I can write this second. */
int global_relayed_write_bucket;

Roger Dingledine's avatar
Roger Dingledine committed
46
/** What was the read bucket before the last call to prepare_for_pool?
47
 * (used to determine how many bytes we've read). */
48
static int stats_prev_global_read_bucket;
49
50
51
/** What was the write bucket before the last call to prepare_for_pool?
 * (used to determine how many bytes we've written). */
static int stats_prev_global_write_bucket;
52
/* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
53
/** How many bytes have we read/written since we started the process? */
54
static uint64_t stats_n_bytes_read = 0;
55
static uint64_t stats_n_bytes_written = 0;
56
/** What time did this process start up? */
57
time_t time_of_process_start = 0;
Roger Dingledine's avatar
Roger Dingledine committed
58
/** How many seconds have we been running? */
59
long stats_n_seconds_working = 0;
60
61
/** When do we next launch DNS wildcarding checks? */
static time_t time_to_check_for_correct_dns = 0;
62
63
64
65
66
67
68

/** How often will we honor SIGNEWNYM requests? */
#define MAX_SIGNEWNYM_RATE 10
/** When did we last process a SIGNEWNYM request? */
static time_t time_of_last_signewnym = 0;
/** Is there a signewnym request we're currently waiting to handle? */
static int signewnym_is_pending = 0;
69

70
71
/** Smartlist of all open connections. */
static smartlist_t *connection_array = NULL;
72
73
/** List of connections that have been marked for close and need to be freed
 * and removed from connection_array. */
74
static smartlist_t *closeable_connection_lst = NULL;
75
76
/** List of linked connections that are currently reading data into their
 * inbuf from their partner's outbuf. */
77
static smartlist_t *active_linked_connection_lst = NULL;
78
79
80
/** Flag: Set to true iff we entered the current libevent main loop via
 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
 * to handle linked connections. */
81
static int called_loop_once = 0;
Roger Dingledine's avatar
Roger Dingledine committed
82

Roger Dingledine's avatar
Roger Dingledine committed
83
/** We set this to 1 when we've opened a circuit, so we can print a log
84
 * entry to inform the user that Tor is working. */
85
int has_completed_circuit=0;
86

87
88
89
90
91
92
/** How often do we check for router descriptors that we should download
 * when we have too little directory info? */
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
/** How often do we check for router descriptors that we should download
 * when we have enough directory info? */
#define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
93
94
95
96
97
98
99
100
101
/** How often do we 'forgive' undownloadable router descriptors and attempt
 * to download them again? */
#define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
/** How long do we let a directory connection stall before expiring it? */
#define DIR_CONN_MAX_STALL (5*60)

/** How old do we let a connection to an OR get before deciding it's
 * obsolete? */
#define TIME_BEFORE_OR_CONN_IS_OBSOLETE (60*60*24*7)
Roger Dingledine's avatar
Roger Dingledine committed
102
103
/** How long do we let OR connections handshake before we decide that
 * they are obsolete? */
104
#define TLS_HANDSHAKE_TIMEOUT (60)
105

Roger Dingledine's avatar
Roger Dingledine committed
106
107
/********* END VARIABLES ************/

Roger Dingledine's avatar
Roger Dingledine committed
108
109
110
/****************************************************************************
*
* This section contains accessors and other methods on the connection_array
111
* variables (which are global within this file and unavailable outside it).
Roger Dingledine's avatar
Roger Dingledine committed
112
113
114
*
****************************************************************************/

Roger Dingledine's avatar
Roger Dingledine committed
115
/** Add <b>conn</b> to the array of connections that we can poll on.  The
116
117
118
 * connection's socket must be set; the connection starts out
 * non-reading and non-writing.
 */
119
120
121
int
connection_add(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
122
  tor_assert(conn);
123
124
125
  tor_assert(conn->s >= 0 ||
             conn->linked ||
             (conn->type == CONN_TYPE_AP &&
126
              TO_EDGE_CONN(conn)->is_dns_request));
Roger Dingledine's avatar
Roger Dingledine committed
127

128
  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
129
130
  conn->conn_array_index = smartlist_len(connection_array);
  smartlist_add(connection_array, conn);
Roger Dingledine's avatar
Roger Dingledine committed
131

132
  if (conn->s >= 0 || conn->linked) {
133
134
135
136
137
138
139
    conn->read_event = tor_malloc_zero(sizeof(struct event));
    conn->write_event = tor_malloc_zero(sizeof(struct event));
    event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
              conn_read_callback, conn);
    event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
              conn_write_callback, conn);
  }
Roger Dingledine's avatar
Roger Dingledine committed
140

141
  log_debug(LD_NET,"new conn type %s, socket %d, n_conns %d.",
142
143
            conn_type_to_string(conn->type), conn->s,
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
144
145
146
147

  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
148
/** Remove the connection from the global list, and remove the
Nick Mathewson's avatar
Nick Mathewson committed
149
150
151
 * corresponding poll entry.  Calling this function will shift the last
 * connection (if any) into the position occupied by conn.
 */
152
153
154
int
connection_remove(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
155
  int current_index;
156
  connection_t *tmp;
Roger Dingledine's avatar
Roger Dingledine committed
157

Roger Dingledine's avatar
Roger Dingledine committed
158
  tor_assert(conn);
Roger Dingledine's avatar
Roger Dingledine committed
159

160
  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
161
162
            conn->s, conn_type_to_string(conn->type),
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
163

164
165
  tor_assert(conn->conn_array_index >= 0);
  current_index = conn->conn_array_index;
166
  connection_unregister_events(conn); /* This is redundant, but cheap. */
167
168
  if (current_index == smartlist_len(connection_array)-1) { /* at the end */
    smartlist_del(connection_array, current_index);
Roger Dingledine's avatar
Roger Dingledine committed
169
    return 0;
Roger Dingledine's avatar
Roger Dingledine committed
170
  }
Roger Dingledine's avatar
Roger Dingledine committed
171

172
  /* replace this one with the one at the end */
173
174
175
  smartlist_del(connection_array, current_index);
  tmp = smartlist_get(connection_array, current_index);
  tmp->conn_array_index = current_index;
Roger Dingledine's avatar
Roger Dingledine committed
176

Roger Dingledine's avatar
Roger Dingledine committed
177
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
178
179
}

180
/** If <b>conn</b> is an edge conn, remove it from the list
181
182
183
 * of conn's on this circuit. If it's not on an edge,
 * flush and send destroys for all circuits on this conn.
 *
184
185
 * Remove it from connection_array (if applicable) and
 * from closeable_connection_list.
186
187
188
 *
 * Then free it.
 */
189
static void
190
connection_unlink(connection_t *conn)
191
{
192
  connection_about_to_close_connection(conn);
193
  if (conn->conn_array_index >= 0) {
194
195
    connection_remove(conn);
  }
196
197
198
199
200
201
202
  if (conn->linked_conn) {
    conn->linked_conn->linked_conn = NULL;
    if (! conn->linked_conn->marked_for_close &&
        conn->linked_conn->reading_from_linked_conn)
      connection_start_reading(conn->linked_conn);
    conn->linked_conn = NULL;
  }
203
  smartlist_remove(closeable_connection_lst, conn);
204
  smartlist_remove(active_linked_connection_lst, conn);
205
  if (conn->type == CONN_TYPE_EXIT) {
206
    assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
207
  }
208
209
210
  if (conn->type == CONN_TYPE_OR) {
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
      connection_or_remove_from_identity_map(TO_OR_CONN(conn));
211
  }
212
213
214
  connection_free(conn);
}

215
/** Schedule <b>conn</b> to be closed. **/
216
217
218
219
220
void
add_connection_to_closeable_list(connection_t *conn)
{
  tor_assert(!smartlist_isin(closeable_connection_lst, conn));
  tor_assert(conn->marked_for_close);
221
  assert_connection_ok(conn, time(NULL));
222
223
224
  smartlist_add(closeable_connection_lst, conn);
}

225
/** Return 1 if conn is on the closeable list, else return 0. */
226
227
228
int
connection_is_on_closeable_list(connection_t *conn)
{
229
230
231
  return smartlist_isin(closeable_connection_lst, conn);
}

232
/** Return true iff conn is in the current poll array. */
233
234
235
int
connection_in_array(connection_t *conn)
{
236
  return smartlist_isin(connection_array, conn);
237
238
}

Roger Dingledine's avatar
Roger Dingledine committed
239
240
241
/** Set <b>*array</b> to an array of all connections, and <b>*n</b>
 * to the length of the array. <b>*array</b> and <b>*n</b> must not
 * be modified.
242
 */
243
244
smartlist_t *
get_connection_array(void)
245
{
246
  return connection_array;
247
248
}

249
/** Set the event mask on <b>conn</b> to <b>events</b>.  (The event
250
 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
251
 */
252
253
254
void
connection_watch_events(connection_t *conn, short events)
{
255
256
257
258
259
260
261
262
263
  if (events & EV_READ)
    connection_start_reading(conn);
  else
    connection_stop_reading(conn);

  if (events & EV_WRITE)
    connection_start_writing(conn);
  else
    connection_stop_writing(conn);
Roger Dingledine's avatar
Roger Dingledine committed
264
265
}

Roger Dingledine's avatar
Roger Dingledine committed
266
/** Return true iff <b>conn</b> is listening for read events. */
267
268
269
int
connection_is_reading(connection_t *conn)
{
270
  tor_assert(conn);
271

272
273
  return conn->reading_from_linked_conn ||
    (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
Roger Dingledine's avatar
Roger Dingledine committed
274
275
}

Roger Dingledine's avatar
Roger Dingledine committed
276
/** Tell the main loop to stop notifying <b>conn</b> of any read events. */
277
278
279
void
connection_stop_reading(connection_t *conn)
{
280
  tor_assert(conn);
281
  tor_assert(conn->read_event);
282

283
284
285
286
287
288
289
290
291
292
  if (conn->linked) {
    conn->reading_from_linked_conn = 0;
    connection_stop_reading_from_linked_conn(conn);
  } else {
    if (event_del(conn->read_event))
      log_warn(LD_NET, "Error from libevent setting read event state for %d "
               "to unwatched: %s",
               conn->s,
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
293
294
}

Roger Dingledine's avatar
Roger Dingledine committed
295
/** Tell the main loop to start notifying <b>conn</b> of any read events. */
296
297
298
void
connection_start_reading(connection_t *conn)
{
299
  tor_assert(conn);
300
301
  tor_assert(conn->read_event);

302
303
304
305
306
307
308
309
310
311
312
  if (conn->linked) {
    conn->reading_from_linked_conn = 1;
    if (connection_should_read_from_linked_conn(conn))
      connection_start_reading_from_linked_conn(conn);
  } else {
    if (event_add(conn->read_event, NULL))
      log_warn(LD_NET, "Error from libevent setting read event state for %d "
               "to watched: %s",
               conn->s,
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
313
314
}

Roger Dingledine's avatar
Roger Dingledine committed
315
/** Return true iff <b>conn</b> is listening for write events. */
316
317
318
int
connection_is_writing(connection_t *conn)
{
319
320
  tor_assert(conn);

321
322
  return conn->writing_to_linked_conn ||
    (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
323
324
}

Roger Dingledine's avatar
Roger Dingledine committed
325
/** Tell the main loop to stop notifying <b>conn</b> of any write events. */
326
327
328
void
connection_stop_writing(connection_t *conn)
{
329
  tor_assert(conn);
330
331
  tor_assert(conn->write_event);

332
333
334
335
336
337
338
339
340
341
342
  if (conn->linked) {
    conn->writing_to_linked_conn = 0;
    if (conn->linked_conn)
      connection_stop_reading_from_linked_conn(conn->linked_conn);
  } else {
    if (event_del(conn->write_event))
      log_warn(LD_NET, "Error from libevent setting write event state for %d "
               "to unwatched: %s",
               conn->s,
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
343
344
}

Roger Dingledine's avatar
Roger Dingledine committed
345
/** Tell the main loop to start notifying <b>conn</b> of any write events. */
346
347
348
void
connection_start_writing(connection_t *conn)
{
349
  tor_assert(conn);
350
351
  tor_assert(conn->write_event);

352
353
354
355
356
357
358
359
360
361
362
363
364
365
  if (conn->linked) {
    conn->writing_to_linked_conn = 1;
    if (conn->linked_conn &&
        connection_should_read_from_linked_conn(conn->linked_conn))
      connection_start_reading_from_linked_conn(conn->linked_conn);
  } else {
    if (event_add(conn->write_event, NULL))
      log_warn(LD_NET, "Error from libevent setting write event state for %d "
               "to watched: %s",
               conn->s,
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
}

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/** Return true iff <b>conn</b> is linked conn, and reading from the conn
 * linked to it would be good and feasible.  (Reading is "feasible" if the
 * other conn exists and has data in its outbuf, and is "good" if we have our
 * reading_from_linked_conn flag set and the other conn has its
 * writing_to_linked_conn flag set.)*/
static int
connection_should_read_from_linked_conn(connection_t *conn)
{
  if (conn->linked && conn->reading_from_linked_conn) {
    if (! conn->linked_conn ||
        (conn->linked_conn->writing_to_linked_conn &&
         buf_datalen(conn->linked_conn->outbuf)))
      return 1;
  }
  return 0;
}

383
384
385
386
/** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
 * its linked connection, if it is not doing so already.  Called by
 * connection_start_reading and connection_start_writing as appropriate. */
static void
387
388
389
390
391
392
393
394
395
396
397
398
connection_start_reading_from_linked_conn(connection_t *conn)
{
  tor_assert(conn);
  tor_assert(conn->linked == 1);

  if (!conn->active_on_link) {
    conn->active_on_link = 1;
    smartlist_add(active_linked_connection_lst, conn);
    if (!called_loop_once) {
      /* This is the first event on the list; we won't be in LOOP_ONCE mode,
       * so we need to make sure that the event_loop() actually exits at the
       * end of its run through the current connections and
399
       * lets us activate read events for linked connections. */
400
401
402
403
404
405
406
407
      struct timeval tv = { 0, 0 };
      event_loopexit(&tv);
    }
  } else {
    tor_assert(smartlist_isin(active_linked_connection_lst, conn));
  }
}

408
409
410
/** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
 * connection, if is currently doing so.  Called by connection_stop_reading,
 * connection_stop_writing, and connection_read. */
411
412
413
414
415
416
417
418
void
connection_stop_reading_from_linked_conn(connection_t *conn)
{
  tor_assert(conn);
  tor_assert(conn->linked == 1);

  if (conn->active_on_link) {
    conn->active_on_link = 0;
419
420
421
    /* FFFF We could keep an index here so we can smartlist_del
     * cleanly.  On the other hand, this doesn't show up on profiles,
     * so let's leave it alone for now. */
422
423
424
425
    smartlist_remove(active_linked_connection_lst, conn);
  } else {
    tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
  }
426
427
}

428
/** Close all connections that have been scheduled to get closed. */
429
430
431
432
433
434
static void
close_closeable_connections(void)
{
  int i;
  for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
    connection_t *conn = smartlist_get(closeable_connection_lst, i);
435
    if (conn->conn_array_index < 0) {
436
      connection_unlink(conn); /* blow it away right now */
437
    } else {
438
      if (!conn_close_if_marked(conn->conn_array_index))
439
440
        ++i;
    }
441
442
443
  }
}

444
445
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to read. */
446
447
448
449
static void
conn_read_callback(int fd, short event, void *_conn)
{
  connection_t *conn = _conn;
450
451
  (void)fd;
  (void)event;
452

453
  log_debug(LD_NET,"socket %d wants to read.",conn->s);
454
455
456
457
458
459

  assert_connection_ok(conn, time(NULL));

  if (connection_handle_read(conn) < 0) {
    if (!conn->marked_for_close) {
#ifndef MS_WINDOWS
460
      log_warn(LD_BUG,"Unhandled error on read for %s connection "
461
462
               "(fd %d); removing",
               conn_type_to_string(conn->type), conn->s);
463
      tor_fragile_assert();
464
#endif
465
      if (CONN_IS_EDGE(conn))
466
        connection_edge_end_errno(TO_EDGE_CONN(conn));
467
468
469
470
471
472
473
474
475
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

  if (smartlist_len(closeable_connection_lst))
    close_closeable_connections();
}

476
477
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to write. */
478
479
static void
conn_write_callback(int fd, short events, void *_conn)
480
481
{
  connection_t *conn = _conn;
482
483
  (void)fd;
  (void)events;
484

485
  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
486
487
488

  assert_connection_ok(conn, time(NULL));

489
  if (connection_handle_write(conn, 0) < 0) {
490
491
    if (!conn->marked_for_close) {
      /* this connection is broken. remove it. */
492
      log_fn(LOG_WARN,LD_BUG,
Roger Dingledine's avatar
Roger Dingledine committed
493
494
             "unhandled error on write for %s connection (fd %d); removing",
             conn_type_to_string(conn->type), conn->s);
495
      tor_fragile_assert();
496
497
      if (CONN_IS_EDGE(conn)) {
        /* otherwise we cry wolf about duplicate close */
498
499
500
        edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
        if (!edge_conn->end_reason)
          edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
501
502
        conn->edge_has_sent_end = 1;
      }
503
      connection_close_immediate(conn); /* So we don't try to flush. */
504
505
506
507
508
509
510
511
512
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

  if (smartlist_len(closeable_connection_lst))
    close_closeable_connections();
}

Roger Dingledine's avatar
Roger Dingledine committed
513
/** If the connection at connection_array[i] is marked for close, then:
514
515
516
517
518
 *    - If it has data that it wants to flush, try to flush it.
 *    - If it _still_ has data to flush, and conn->hold_open_until_flushed is
 *      true, then leave the connection open and return.
 *    - Otherwise, remove the connection from connection_array and from
 *      all other lists, close it, and free it.
519
 * Returns 1 if the connection was closed, 0 otherwise.
520
 */
521
522
523
static int
conn_close_if_marked(int i)
{
Roger Dingledine's avatar
Roger Dingledine committed
524
  connection_t *conn;
525
  int retval;
526
  time_t now;
Roger Dingledine's avatar
Roger Dingledine committed
527

528
  conn = smartlist_get(connection_array, i);
529
  if (!conn->marked_for_close)
530
    return 0; /* nothing to see here, move along */
531
532
  now = time(NULL);
  assert_connection_ok(conn, now);
533
  assert_all_pending_dns_resolves_ok();
534

535
  log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
536
  if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
537
    /* s == -1 means it's an incomplete edge connection, or that the socket
538
     * has already been closed as unflushable. */
539
    int sz = connection_bucket_write_limit(conn, now);
540
    if (!conn->hold_open_until_flushed)
541
542
543
      log_info(LD_NET,
               "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
               "to flush %d bytes. (Marked at %s:%d)",
544
545
               escaped_safe_str(conn->address),
               conn->s, conn_type_to_string(conn->type), conn->state,
546
547
               (int)conn->outbuf_flushlen,
                conn->marked_for_close_file, conn->marked_for_close);
548
549
550
551
552
553
554
555
    if (conn->linked_conn) {
      retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
                               &conn->outbuf_flushlen);
      if (retval >= 0) {
        /* The linked conn will notice that it has data when it notices that
         * we're gone. */
        connection_start_reading_from_linked_conn(conn->linked_conn);
      }
556
      log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
557
558
559
               "%d left; flushlen %d; wants-to-flush==%d", retval,
               (int)buf_datalen(conn->outbuf),
               (int)conn->outbuf_flushlen,
560
                connection_wants_to_flush(conn));
561
    } else if (connection_speaks_cells(conn)) {
562
      if (conn->state == OR_CONN_STATE_OPEN) {
563
        retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
564
                               &conn->outbuf_flushlen);
565
      } else
566
        retval = -1; /* never flush non-open broken tls connections */
567
    } else {
568
      retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
569
    }
570
571
572
    if (retval >= 0 && /* Technically, we could survive things like
                          TLS_WANT_WRITE here. But don't bother for now. */
        conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
573
      if (retval > 0) {
574
575
576
        LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
                           "Holding conn (fd %d) open for more flushing.",
                           conn->s));
577
578
        conn->timestamp_lastwritten = now; /* reset so we can flush more */
      }
579
      return 0;
580
    }
581
    if (connection_wants_to_flush(conn)) {
582
583
      int severity;
      if (conn->type == CONN_TYPE_EXIT ||
584
          (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
585
586
587
588
          (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
        severity = LOG_INFO;
      else
        severity = LOG_NOTICE;
589
590
591
      /* XXXX Maybe allow this to happen a certain amount per hour; it usually
       * is meaningless. */
      log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
592
             "bytes to address %s.  If this happens a lot, either "
593
594
595
             "something is wrong with your network connection, or "
             "something is wrong with theirs. "
             "(fd %d, type %s, state %d, marked at %s:%d).",
596
             (int)buf_datalen(conn->outbuf),
597
598
             escaped_safe_str(conn->address), conn->s,
             conn_type_to_string(conn->type), conn->state,
599
             conn->marked_for_close_file,
600
             conn->marked_for_close);
Roger Dingledine's avatar
Roger Dingledine committed
601
602
    }
  }
603
  connection_unlink(conn); /* unlink, remove, free */
604
  return 1;
Roger Dingledine's avatar
Roger Dingledine committed
605
606
}

607
608
609
610
611
612
/** We've just tried every dirserver we know about, and none of
 * them were reachable. Assume the network is down. Change state
 * so next time an application connection arrives we'll delay it
 * and try another directory fetch. Kill off all the circuit_wait
 * streams that are waiting now, since they will all timeout anyway.
 */
613
614
615
void
directory_all_unreachable(time_t now)
{
616
  connection_t *conn;
617
  (void)now;
618

619
  stats_n_seconds_working=0; /* reset it */
620
621
622

  while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
                                              AP_CONN_STATE_CIRCUIT_WAIT))) {
623
    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
624
625
626
    log_notice(LD_NET,
               "Is your network connection down? "
               "Failing connection to '%s:%d'.",
627
628
               safe_str(edge_conn->socks_request->address),
               edge_conn->socks_request->port);
629
630
    connection_mark_unattached_ap(edge_conn,
                                  END_STREAM_REASON_NET_UNREACHABLE);
631
  }
632
  control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
633
634
}

635
636
/** This function is called whenever we successfully pull down some new
 * network statuses or server descriptors. */
637
void
638
directory_info_has_arrived(time_t now, int from_cache)
639
{
640
  or_options_t *options = get_options();
641

642
  if (!router_have_minimum_dir_info()) {
643
644
    log(LOG_NOTICE, LD_DIR,
        "I learned some more directory information, but not enough to "
645
        "build a circuit: %s", get_dir_info_status_string());
646
    update_router_descriptor_downloads(now);
647
    return;
648
  } else {
649
650
651
    /* if we have enough dir info, then update our guard status with
     * whatever we just learned. */
    entry_guards_compute_status();
652
653
654
655
    /* Don't even bother trying to get extrainfo until the rest of our
     * directory info is up-to-date */
    if (options->DownloadExtraInfo)
      update_extrainfo_downloads(now);
656
  }
657

658
  if (server_mode(options) && !we_are_hibernating() && !from_cache &&
Roger Dingledine's avatar
Roger Dingledine committed
659
      (has_completed_circuit || !any_predicted_circuits(now)))
660
    consider_testing_reachability(1, 1);
661
662
}

Roger Dingledine's avatar
Roger Dingledine committed
663
/** Perform regular maintenance tasks for a single connection.  This
664
 * function gets run once per second per connection by run_scheduled_events.
Nick Mathewson's avatar
Nick Mathewson committed
665
 */
666
667
668
static void
run_connection_housekeeping(int i, time_t now)
{
Nick Mathewson's avatar
Nick Mathewson committed
669
  cell_t cell;
670
  connection_t *conn = smartlist_get(connection_array, i);
671
  or_options_t *options = get_options();
672
  or_connection_t *or_conn;
Roger Dingledine's avatar
Roger Dingledine committed
673

674
675
  if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
    TO_OR_CONN(conn)->timestamp_lastempty = now;
676

677
678
679
680
681
  if (conn->marked_for_close) {
    /* nothing to do here */
    return;
  }

682
683
  /* Expire any directory connections that haven't been active (sent
   * if a server or received if a client) for 5 min */
684
685
686
687
688
  if (conn->type == CONN_TYPE_DIR &&
      ((DIR_CONN_IS_SERVER(conn) &&
        conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
       (!DIR_CONN_IS_SERVER(conn) &&
        conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
689
690
    log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
             conn->s, conn->purpose);
691
692
    /* This check is temporary; it's to let us know whether we should consider
     * parsing partial serverdesc responses. */
693
    if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
694
        buf_datalen(conn->inbuf)>=1024) {
695
696
      log_info(LD_DIR,"Trying to extract information from wedged server desc "
               "download.");
697
      connection_dir_reached_eof(TO_DIR_CONN(conn));
698
699
    } else {
      connection_mark_for_close(conn);
700
    }
701
702
703
    return;
  }

704
705
706
  if (!connection_speaks_cells(conn))
    return; /* we're all done here, the rest is just for OR conns */

707
708
709
  or_conn = TO_OR_CONN(conn);

  if (!conn->or_is_obsolete) {
710
    if (conn->timestamp_created + TIME_BEFORE_OR_CONN_IS_OBSOLETE < now) {
711
712
713
714
      log_info(LD_OR,
               "Marking OR conn to %s:%d obsolete (fd %d, %d secs old).",
               conn->address, conn->port, conn->s,
               (int)(now - conn->timestamp_created));
715
      conn->or_is_obsolete = 1;
716
    } else {
717
718
719
      or_connection_t *best =
        connection_or_get_by_identity_digest(or_conn->identity_digest);
      if (best && best != or_conn &&
720
          (conn->state == OR_CONN_STATE_OPEN ||
721
           now > conn->timestamp_created + TLS_HANDSHAKE_TIMEOUT)) {
722
723
          /* We only mark as obsolete connections that already are in
           * OR_CONN_STATE_OPEN, i.e. that have finished their TLS handshaking.
724
           * This is necessary because authorities judge whether a router is
725
726
727
728
           * reachable based on whether they were able to TLS handshake with it
           * recently.  Without this check we would expire connections too
           * early for router->last_reachable to be updated.
           */
729
730
731
732
733
        log_info(LD_OR,
                 "Marking duplicate conn to %s:%d obsolete "
                 "(fd %d, %d secs old).",
                 conn->address, conn->port, conn->s,
                 (int)(now - conn->timestamp_created));
734
        conn->or_is_obsolete = 1;
735
      }
736
    }
737
738
  }

739
  if (conn->or_is_obsolete && !or_conn->n_circuits) {
740
    /* no unmarked circs -- mark it now */
741
742
    log_info(LD_OR,
             "Expiring non-used OR connection to fd %d (%s:%d) [Obsolete].",
743
             conn->s, conn->address, conn->port);
744
745
746
747
748
    connection_mark_for_close(conn);
    conn->hold_open_until_flushed = 1;
    return;
  }

749
750
  /* If we haven't written to an OR connection for a while, then either nuke
     the connection or send a keepalive, depending. */
751
  if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
752
    routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
753
    int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
754
    if (!connection_state_is_open(conn)) {
755
      /* We never managed to actually get this connection open and happy. */
756
757
      log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
               conn->s,conn->address, conn->port);
758
      connection_mark_for_close(conn);
759
      conn->hold_open_until_flushed = 1;
760
    } else if (we_are_hibernating() && !or_conn->n_circuits &&
761
               !buf_datalen(conn->outbuf)) {
762
      /* We're hibernating, there's no circuits, and nothing to flush.*/
763
764
765
      log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
               "[Hibernating or exiting].",
               conn->s,conn->address, conn->port);
766
767
      connection_mark_for_close(conn);
      conn->hold_open_until_flushed = 1;
768
    } else if (!clique_mode(options) && !or_conn->n_circuits &&
769
770
               now >= or_conn->timestamp_last_added_nonpadding +
                                           maxCircuitlessPeriod &&
771
772
               (!router || !server_mode(options) ||
                !router_is_clique_mode(router))) {
773
774
775
      log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
               "[Not in clique mode].",
               conn->s,conn->address, conn->port);
776
777
      connection_mark_for_close(conn);
      conn->hold_open_until_flushed = 1;
778
    } else if (
779
         now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
780
         now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
781
782
783
      log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
             "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
             "flush; %d seconds since last write)",
784
             conn->s, conn->address, conn->port,
785
             (int)buf_datalen(conn->outbuf),
786
             (int)(now-conn->timestamp_lastwritten));
787
      connection_mark_for_close(conn);
788
    } else if (!buf_datalen(conn->outbuf)) {
789
      /* either in clique mode, or we've got a circuit. send a padding cell. */
790
      log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
Nick Mathewson's avatar
Nick Mathewson committed
791
792
793
             conn->address, conn->port);
      memset(&cell,0,sizeof(cell_t));
      cell.command = CELL_PADDING;
794
      connection_or_write_cell_to_buf(&cell, or_conn);
Nick Mathewson's avatar
Nick Mathewson committed
795
796
797
798
    }
  }
}

799
800
801
802
803
804
805
806
807
808
809
/** Honor a NEWNYM request: make future requests unlinkability to past
 * requests. */
static void
signewnym_impl(time_t now)
{
  circuit_expire_all_dirty_circs();
  addressmap_clear_transient();
  time_of_last_signewnym = now;
  signewnym_is_pending = 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
810
/** Perform regular maintenance tasks.  This function gets run once per
Nick Mathewson's avatar
Nick Mathewson committed
811
812
 * second by prepare_for_poll.
 */
813
814
815
static void
run_scheduled_events(time_t now)
{
816
817
  static time_t time_to_fetch_directory = 0;
  static time_t time_to_fetch_running_routers = 0;
818
819
  static time_t last_rotated_x509_certificate = 0;
  static time_t time_to_check_v3_certificate = 0;
820
  static time_t time_to_check_listeners = 0;
821
  static time_t time_to_check_descriptor = 0;
822
  static time_t time_to_check_ipaddress = 0;
823
  static time_t time_to_shrink_memory = 0;
824
825
  static time_t time_to_try_getting_descriptors = 0;
  static time_t time_to_reset_descriptor_failures = 0;
826
  static time_t time_to_add_entropy = 0;
827
  static time_t time_to_write_hs_statistics = 0;
828
  static time_t time_to_write_bridge_status_file = 0;
829
  static time_t time_to_downrate_stability = 0;
830
  static time_t time_to_save_stability = 0;
Roger Dingledine's avatar
Roger Dingledine committed
831
  static time_t time_to_clean_caches = 0;
832
  static time_t time_to_recheck_bandwidth = 0;
833
  static time_t time_to_check_for_expired_networkstatus = 0;
834
  or_options_t *options = get_options();
Nick Mathewson's avatar
Nick Mathewson committed
835
  int i;
836
  int have_dir_info;
837

838
  /** 0. See if we've been asked to shut down and our timeout has
839
840
   * expired; or if our bandwidth limits are exhausted and we
   * should hibernate; or if it's time to wake up from hibernation.
841
   */
842
  consider_hibernation(now);
843

844
  /* 0b. If we've deferred a signewnym, make sure it gets handled
845
   * eventually. */
846
  if (signewnym_is_pending &&
847
848
      time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
    log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
849
    signewnym_impl(now);
850
851
  }

Roger Dingledine's avatar
Roger Dingledine committed
852
  /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
853
854
855
   *  shut down and restart all cpuworkers, and update the directory if
   *  necessary.
   */
856
857
  if (server_mode(options) &&
      get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
858
    log_info(LD_GENERAL,"Rotating onion key.");
859
860
    rotate_onion_key();
    cpuworkers_rotate();
861
    if (router_rebuild_descriptor(1)<0) {
862
      log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
863
    }
864
    if (advertised_server_mode())
865
      router_upload_dir_desc_to_dirservers(0);
866
867
  }

868
869
  if (time_to_try_getting_descriptors < now) {
    update_router_descriptor_downloads(now);
870
    update_extrainfo_downloads(now);
871
872
    if (options->UseBridges)
      fetch_bridge_descriptors(now);
873
874
875
876
    if (router_have_minimum_dir_info())
      time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
    else
      time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
877
878
879
880
  }

  if (time_to_reset_descriptor_failures < now) {
    router_reset_descriptor_download_failures();
881
882
    time_to_reset_descriptor_failures =
      now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
883
884
  }

Roger Dingledine's avatar
Roger Dingledine committed
885
  /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
886
887
888
  if (!last_rotated_x509_certificate)
    last_rotated_x509_certificate = now;
  if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
889
    log_info(LD_GENERAL,"Rotating tls context.");
890
    if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
891
      log_warn(LD_BUG, "Error reinitializing TLS context");
892
      /* XXX is it a bug here, that we just keep going? -RD */
893
    }
894
    last_rotated_x509_certificate = now;
895
896
897
    /* We also make sure to rotate the TLS connections themselves if they've
     * been up for too long -- but that's done via or_is_obsolete in
     * connection_run_housekeeping() above. */
898
899
  }

900
  if (time_to_add_entropy < now) {
901
902
903
904
905
906
    if (time_to_add_entropy) {
      /* We already seeded once, so don't die on failure. */
      crypto_seed_rng();
    }
/** How often do we add more entropy to OpenSSL's RNG pool? */
#define ENTROPY_INTERVAL (60*60)
907
908
909
    time_to_add_entropy = now + ENTROPY_INTERVAL;
  }

910
911
  /** 1c. If we have to change the accounting interval or record
   * bandwidth used in this accounting interval, do so. */
912
  if (accounting_is_enabled(options))
913
    accounting_run_housekeeping(now);
914

915
  if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
916
      !we_are_hibernating()) {
917
    /* try to determine reachability of the other Tor relays */
918
    dirserv_test_reachability(now, 0);