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

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

Roger Dingledine's avatar
Roger Dingledine committed
15
#include "or.h"
16
17
18
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
Roger Dingledine's avatar
Roger Dingledine committed
19

20
/********* PROTOTYPES **********/
Roger Dingledine's avatar
Roger Dingledine committed
21

22
static void dumpmemusage(int severity);
23
static void dumpstats(int severity); /* log stats */
24
25
26
27
28
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);
Roger Dingledine's avatar
Roger Dingledine committed
29

Roger Dingledine's avatar
Roger Dingledine committed
30
31
/********* START VARIABLES **********/

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

Roger Dingledine's avatar
Roger Dingledine committed
35
/** What was the read bucket before the last call to prepare_for_pool?
36
 * (used to determine how many bytes we've read). */
37
static int stats_prev_global_read_bucket;
38
39
40
41
/** 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;
/** How many bytes have we read/written since we started the process? */
42
static uint64_t stats_n_bytes_read = 0;
43
static uint64_t stats_n_bytes_written = 0;
44
45
/** What time did this process start up? */
long time_of_process_start = 0;
Roger Dingledine's avatar
Roger Dingledine committed
46
/** How many seconds have we been running? */
47
long stats_n_seconds_working = 0;
48
49
/** When do we next download a directory? */
static time_t time_to_fetch_directory = 0;
50
51
/** When do we next download a running-routers summary? */
static time_t time_to_fetch_running_routers = 0;
52

53
/** Array of all open connections.  The first n_conns elements are valid. */
54
static connection_t *connection_array[MAXCONNECTIONS+1] =
Roger Dingledine's avatar
Roger Dingledine committed
55
        { NULL };
56
static smartlist_t *closeable_connection_lst = NULL;
Roger Dingledine's avatar
Roger Dingledine committed
57

58
static int n_conns=0; /**< Number of connections currently active. */
Roger Dingledine's avatar
Roger Dingledine committed
59

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

64
65
66
67
#ifdef MS_WINDOWS
#define MS_WINDOWS_SERVICE
#endif

68
#ifdef MS_WINDOWS_SERVICE
69
#include <tchar.h>
70
71
#define GENSRV_SERVICENAME  TEXT("tor")
#define GENSRV_DISPLAYNAME  TEXT("Tor Win32 Service")
72
73
#define GENSRV_DESCRIPTION  \
  TEXT("Provides an anonymous Internet communication system")
74
75
76
77
78
79
80

// Cheating: using the pre-defined error codes, tricks Windows into displaying
//           a semi-related human-readable error message if startup fails as
//           opposed to simply scaring people with Error: 0xffffffff
#define NT_SERVICE_ERROR_NO_TORRC ERROR_FILE_NOT_FOUND
#define NT_SERVICE_ERROR_TORINIT_FAILED ERROR_EXCEPTION_IN_SERVICE

81
82
SERVICE_STATUS service_status;
SERVICE_STATUS_HANDLE hStatus;
83
84
static char **backup_argv;
static int backup_argc;
85
static int nt_service_is_stopped(void);
86
static char* nt_strerror(uint32_t errnum);
87
88
#else
#define nt_service_is_stopped() (0)
89
90
#endif

91
92
93
94
95
96
97
/** If our router descriptor ever goes this long without being regenerated
 * because something changed, we force an immediate regenerate-and-upload. */
#define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
/** How often do we check whether part of our router info has changed in a way
 * that would require an upload? */
#define CHECK_DESCRIPTOR_INTERVAL (60)
/** How often do we (as a router) check whether our IP address has changed? */
98
#define CHECK_IPADDRESS_INTERVAL (15*60)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/** How often do we check buffers for empty space that can be deallocated? */
#define BUF_SHRINK_INTERVAL (60)
/** How often do we check for router descriptors that we should download? */
#define DESCRIPTOR_RETRY_INTERVAL (10)
/** How often do we 'forgive' undownloadable router descriptors and attempt
 * to download them again? */
#define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
/** How often do we add more entropy to OpenSSL's RNG pool? */
#define ENTROPY_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
114
115
/** How long do we let OR connections handshake before we decide that
 * they are obsolete? */
116
#define TLS_HANDSHAKE_TIMEOUT           (60)
117

Roger Dingledine's avatar
Roger Dingledine committed
118
119
/********* END VARIABLES ************/

Roger Dingledine's avatar
Roger Dingledine committed
120
121
122
/****************************************************************************
*
* This section contains accessors and other methods on the connection_array
123
* variables (which are global within this file and unavailable outside it).
Roger Dingledine's avatar
Roger Dingledine committed
124
125
126
*
****************************************************************************/

Roger Dingledine's avatar
Roger Dingledine committed
127
/** Add <b>conn</b> to the array of connections that we can poll on.  The
128
129
130
 * connection's socket must be set; the connection starts out
 * non-reading and non-writing.
 */
131
132
133
int
connection_add(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
134
  tor_assert(conn);
135
  tor_assert(conn->s >= 0);
Roger Dingledine's avatar
Roger Dingledine committed
136

137
  if (n_conns >= get_options()->_ConnLimit-1) {
138
    log_warn(LD_NET,"Failing because we have %d connections already. Please "
139
             "raise your ulimit -n.", n_conns);
Roger Dingledine's avatar
Roger Dingledine committed
140
141
    return -1;
  }
Roger Dingledine's avatar
Roger Dingledine committed
142

143
144
145
  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
  conn->conn_array_index = n_conns;
  connection_array[n_conns] = conn;
Roger Dingledine's avatar
Roger Dingledine committed
146

147
148
149
150
151
152
  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
153

154
  n_conns++;
Roger Dingledine's avatar
Roger Dingledine committed
155

156
157
  log_debug(LD_NET,"new conn type %s, socket %d, n_conns %d.",
            conn_type_to_string(conn->type), conn->s, n_conns);
Roger Dingledine's avatar
Roger Dingledine committed
158
159
160
161

  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
162
/** Remove the connection from the global list, and remove the
Nick Mathewson's avatar
Nick Mathewson committed
163
164
165
 * corresponding poll entry.  Calling this function will shift the last
 * connection (if any) into the position occupied by conn.
 */
166
167
168
int
connection_remove(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
169
170
  int current_index;

Roger Dingledine's avatar
Roger Dingledine committed
171
  tor_assert(conn);
172
  tor_assert(n_conns>0);
Roger Dingledine's avatar
Roger Dingledine committed
173

174
175
  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
            conn->s, conn_type_to_string(conn->type), n_conns-1);
Roger Dingledine's avatar
Roger Dingledine committed
176

177
178
179
180
  tor_assert(conn->conn_array_index >= 0);
  current_index = conn->conn_array_index;
  if (current_index == n_conns-1) { /* this is the end */
    n_conns--;
Roger Dingledine's avatar
Roger Dingledine committed
181
    return 0;
Roger Dingledine's avatar
Roger Dingledine committed
182
  }
Roger Dingledine's avatar
Roger Dingledine committed
183

184
  connection_unregister(conn);
185

186
  /* replace this one with the one at the end */
187
188
189
  n_conns--;
  connection_array[current_index] = connection_array[n_conns];
  connection_array[current_index]->conn_array_index = current_index;
Roger Dingledine's avatar
Roger Dingledine committed
190

Roger Dingledine's avatar
Roger Dingledine committed
191
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
192
193
}

194
195
196
197
198
199
200
201
202
/** If it's an edge conn, remove it from the list
 * of conn's on this circuit. If it's not on an edge,
 * flush and send destroys for all circuits on this conn.
 *
 * If <b>remove</b> is non-zero, then remove it from the
 * connection_array and closeable_connection_lst.
 *
 * Then free it.
 */
203
204
205
static void
connection_unlink(connection_t *conn, int remove)
{
206
207
208
209
210
  circuit_about_to_close_connection(conn);
  connection_about_to_close_connection(conn);
  if (remove) {
    connection_remove(conn);
  }
211
  smartlist_remove(closeable_connection_lst, conn);
212
  if (conn->type == CONN_TYPE_EXIT) {
213
    assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
214
  }
215
216
217
  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));
218
  }
219
220
221
  connection_free(conn);
}

222
/** Schedule <b>conn</b> to be closed. **/
223
224
225
226
227
void
add_connection_to_closeable_list(connection_t *conn)
{
  tor_assert(!smartlist_isin(closeable_connection_lst, conn));
  tor_assert(conn->marked_for_close);
228
  assert_connection_ok(conn, time(NULL));
229
230
231
  smartlist_add(closeable_connection_lst, conn);
}

232
/** Return 1 if conn is on the closeable list, else return 0. */
233
234
235
int
connection_is_on_closeable_list(connection_t *conn)
{
236
237
238
  return smartlist_isin(closeable_connection_lst, conn);
}

239
/** Return true iff conn is in the current poll array. */
240
241
242
int
connection_in_array(connection_t *conn)
{
243
  int i;
244
  for (i=0; i<n_conns; ++i) {
245
246
247
248
249
250
    if (conn==connection_array[i])
      return 1;
  }
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
251
252
253
/** 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.
254
 */
255
256
257
void
get_connection_array(connection_t ***array, int *n)
{
258
  *array = connection_array;
259
  *n = n_conns;
260
261
}

262
/** Set the event mask on <b>conn</b> to <b>events</b>.  (The event
263
 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
264
 */
265
266
267
void
connection_watch_events(connection_t *conn, short events)
{
268
269
  int r;

270
  tor_assert(conn);
271
272
  tor_assert(conn->read_event);
  tor_assert(conn->write_event);
Roger Dingledine's avatar
Roger Dingledine committed
273

274
  if (events & EV_READ) {
275
    r = event_add(conn->read_event, NULL);
276
  } else {
277
    r = event_del(conn->read_event);
278
279
  }

280
  if (r<0)
281
282
283
284
285
    log_warn(LD_NET,
             "Error from libevent setting read event state for %d to "
             "%swatched: %s",
             conn->s, (events & EV_READ)?"":"un",
             tor_socket_strerror(tor_socket_errno(conn->s)));
286

287
  if (events & EV_WRITE) {
288
    r = event_add(conn->write_event, NULL);
289
  } else {
290
    r = event_del(conn->write_event);
291
  }
292
293

  if (r<0)
294
295
296
297
298
    log_warn(LD_NET,
             "Error from libevent setting read event state for %d to "
             "%swatched: %s",
             conn->s, (events & EV_WRITE)?"":"un",
             tor_socket_strerror(tor_socket_errno(conn->s)));
Roger Dingledine's avatar
Roger Dingledine committed
299
300
}

Roger Dingledine's avatar
Roger Dingledine committed
301
/** Return true iff <b>conn</b> is listening for read events. */
302
303
304
int
connection_is_reading(connection_t *conn)
{
305
  tor_assert(conn);
306

307
  return conn->read_event && event_pending(conn->read_event, EV_READ, NULL);
Roger Dingledine's avatar
Roger Dingledine committed
308
309
}

Roger Dingledine's avatar
Roger Dingledine committed
310
/** Tell the main loop to stop notifying <b>conn</b> of any read events. */
311
312
313
void
connection_stop_reading(connection_t *conn)
{
314
  tor_assert(conn);
315
  tor_assert(conn->read_event);
316

317
  log_debug(LD_NET,"connection_stop_reading() called.");
318
  if (event_del(conn->read_event))
319
    log_warn(LD_NET, "Error from libevent setting read event state for %d "
320
321
322
             "to unwatched: %s",
             conn->s,
             tor_socket_strerror(tor_socket_errno(conn->s)));
323
324
}

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

332
  if (event_add(conn->read_event, NULL))
333
    log_warn(LD_NET, "Error from libevent setting read event state for %d "
334
335
336
             "to watched: %s",
             conn->s,
             tor_socket_strerror(tor_socket_errno(conn->s)));
337
338
}

Roger Dingledine's avatar
Roger Dingledine committed
339
/** Return true iff <b>conn</b> is listening for write events. */
340
341
342
int
connection_is_writing(connection_t *conn)
{
343
344
  tor_assert(conn);

345
  return conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL);
346
347
}

Roger Dingledine's avatar
Roger Dingledine committed
348
/** Tell the main loop to stop notifying <b>conn</b> of any write events. */
349
350
351
void
connection_stop_writing(connection_t *conn)
{
352
  tor_assert(conn);
353
354
  tor_assert(conn->write_event);

355
  if (event_del(conn->write_event))
356
    log_warn(LD_NET, "Error from libevent setting write event state for %d "
357
358
359
             "to unwatched: %s",
             conn->s,
             tor_socket_strerror(tor_socket_errno(conn->s)));
360
361
}

Roger Dingledine's avatar
Roger Dingledine committed
362
/** Tell the main loop to start notifying <b>conn</b> of any write events. */
363
364
365
void
connection_start_writing(connection_t *conn)
{
366
  tor_assert(conn);
367
368
  tor_assert(conn->write_event);

369
  if (event_add(conn->write_event, NULL))
370
    log_warn(LD_NET, "Error from libevent setting write event state for %d "
371
372
373
             "to watched: %s",
             conn->s,
             tor_socket_strerror(tor_socket_errno(conn->s)));
374
375
}

Roger Dingledine's avatar
Roger Dingledine committed
376
/** Close all connections that have been scheduled to get closed */
377
378
379
380
381
382
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);
383
    if (conn->conn_array_index < 0) {
384
385
      connection_unlink(conn, 0); /* blow it away right now */
    } else {
386
      if (!conn_close_if_marked(conn->conn_array_index))
387
388
        ++i;
    }
389
390
391
  }
}

392
393
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to read. */
394
395
396
397
static void
conn_read_callback(int fd, short event, void *_conn)
{
  connection_t *conn = _conn;
398
399
  (void)fd;
  (void)event;
400

401
  log_debug(LD_NET,"socket %d wants to read.",conn->s);
402
403
404
405
406
407

  assert_connection_ok(conn, time(NULL));

  if (connection_handle_read(conn) < 0) {
    if (!conn->marked_for_close) {
#ifndef MS_WINDOWS
408
409
410
      log_warn(LD_BUG,"Bug: unhandled error on read for %s connection "
               "(fd %d); removing",
               conn_type_to_string(conn->type), conn->s);
411
      tor_fragile_assert();
412
#endif
413
      if (CONN_IS_EDGE(conn))
414
415
        connection_edge_end_errno(TO_EDGE_CONN(conn),
                                  TO_EDGE_CONN(conn)->cpath_layer);
416
417
418
419
420
421
422
423
424
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

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

425
426
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to write. */
427
428
static void
conn_write_callback(int fd, short events, void *_conn)
429
430
{
  connection_t *conn = _conn;
431
432
  (void)fd;
  (void)events;
433

434
  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
435
436
437
438
439
440

  assert_connection_ok(conn, time(NULL));

  if (connection_handle_write(conn) < 0) {
    if (!conn->marked_for_close) {
      /* this connection is broken. remove it. */
441
442
443
      log_fn(LOG_WARN,LD_BUG,
           "Bug: unhandled error on write for %s connection (fd %d); removing",
           conn_type_to_string(conn->type), conn->s);
444
      tor_fragile_assert();
445
446
447
448
      if (CONN_IS_EDGE(conn)) {
        /* otherwise we cry wolf about duplicate close */
        conn->edge_has_sent_end = 1;
      }
449
450
451
452
453
454
455
456
457
458
      /* XXX do we need a close-immediate here, so we don't try to flush? */
      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
459
/** If the connection at connection_array[i] is marked for close, then:
460
461
462
463
464
 *    - 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.
465
 * Returns 1 if the connection was closed, 0 otherwise.
466
 */
467
468
469
static int
conn_close_if_marked(int i)
{
Roger Dingledine's avatar
Roger Dingledine committed
470
  connection_t *conn;
471
  int retval;
Roger Dingledine's avatar
Roger Dingledine committed
472
473

  conn = connection_array[i];
474
  if (!conn->marked_for_close)
475
    return 0; /* nothing to see here, move along */
476
477
  assert_connection_ok(conn, time(NULL));
  assert_all_pending_dns_resolves_ok();
478

479
  log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
480
  if (conn->s >= 0 && connection_wants_to_flush(conn)) {
481
    /* s == -1 means it's an incomplete edge connection, or that the socket
482
     * has already been closed as unflushable. */
483
    int sz = connection_bucket_write_limit(conn);
484
    if (!conn->hold_open_until_flushed)
485
486
487
      log_info(LD_NET,
               "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
               "to flush %d bytes. (Marked at %s:%d)",
488
489
               escaped_safe_str(conn->address),
               conn->s, conn_type_to_string(conn->type), conn->state,
490
491
               (int)conn->outbuf_flushlen,
                conn->marked_for_close_file, conn->marked_for_close);
492
493
    if (connection_speaks_cells(conn)) {
      if (conn->state == OR_CONN_STATE_OPEN) {
494
        retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
495
                               &conn->outbuf_flushlen);
496
      } else
497
        retval = -1; /* never flush non-open broken tls connections */
498
    } else {
499
      retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
500
    }
501
502
503
    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)) {
504
505
506
507
      if (retval > 0)
        LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
                           "Holding conn (fd %d) open for more flushing.",
                           conn->s));
508
      /* XXX should we reset timestamp_lastwritten here? */
509
      return 0;
510
    }
511
    if (connection_wants_to_flush(conn)) {
512
513
      int severity;
      if (conn->type == CONN_TYPE_EXIT ||
514
          (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
515
516
517
518
          (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
        severity = LOG_INFO;
      else
        severity = LOG_NOTICE;
519
      log_fn(severity, LD_NET, "Something wrong with your network connection? "
Nick Mathewson's avatar
Nick Mathewson committed
520
521
             "We tried to write %d bytes to addr %s (fd %d, type %s, state %d)"
             " but timed out. (Marked at %s:%d)",
522
             (int)buf_datalen(conn->outbuf),
523
524
             escaped_safe_str(conn->address), conn->s,
             conn_type_to_string(conn->type), conn->state,
525
             conn->marked_for_close_file,
526
             conn->marked_for_close);
Roger Dingledine's avatar
Roger Dingledine committed
527
528
    }
  }
529
  connection_unlink(conn, 1); /* unlink, remove, free */
530
  return 1;
Roger Dingledine's avatar
Roger Dingledine committed
531
532
}

533
534
535
536
537
538
/** 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.
 */
539
540
541
void
directory_all_unreachable(time_t now)
{
542
  connection_t *conn;
543
  (void)now;
544

545
  stats_n_seconds_working=0; /* reset it */
546
547
548

  while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
                                              AP_CONN_STATE_CIRCUIT_WAIT))) {
549
    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
550
551
552
    log_notice(LD_NET,
               "Is your network connection down? "
               "Failing connection to '%s:%d'.",
553
554
               safe_str(edge_conn->socks_request->address),
               edge_conn->socks_request->port);
555
556
    connection_mark_unattached_ap(edge_conn,
                                  END_STREAM_REASON_NET_UNREACHABLE);
557
558
559
  }
}

560
561
/** This function is called whenever we successfully pull down some new
 * network statuses or server descriptors. */
562
void
563
directory_info_has_arrived(time_t now, int from_cache)
564
{
565
  or_options_t *options = get_options();
566

567
  if (!router_have_minimum_dir_info()) {
568
569
570
    log(LOG_NOTICE, LD_DIR,
        "I learned some more directory information, but not enough to "
        "build a circuit.");
571
    update_router_descriptor_downloads(now);
572
573
    return;
  }
574

575
  if (server_mode(options) && !we_are_hibernating() && !from_cache &&
Roger Dingledine's avatar
Roger Dingledine committed
576
      (has_completed_circuit || !any_predicted_circuits(now)))
577
    consider_testing_reachability();
578
579
}

Roger Dingledine's avatar
Roger Dingledine committed
580
/** Perform regular maintenance tasks for a single connection.  This
581
 * function gets run once per second per connection by run_scheduled_events.
Nick Mathewson's avatar
Nick Mathewson committed
582
 */
583
584
585
static void
run_connection_housekeeping(int i, time_t now)
{
Nick Mathewson's avatar
Nick Mathewson committed
586
587
  cell_t cell;
  connection_t *conn = connection_array[i];
588
  or_options_t *options = get_options();
589
  or_connection_t *or_conn;
Roger Dingledine's avatar
Roger Dingledine committed
590

591
592
  if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
    TO_OR_CONN(conn)->timestamp_lastempty = now;
593

594
595
596
597
598
  if (conn->marked_for_close) {
    /* nothing to do here */
    return;
  }

599
  /* Expire any directory connections that haven't sent anything for 5 min */
600
  if (conn->type == CONN_TYPE_DIR &&
601
      conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) {
602
603
    log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
             conn->s, conn->purpose);
604
605
    /* This check is temporary; it's to let us know whether we should consider
     * parsing partial serverdesc responses. */
606
    if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
607
        buf_datalen(conn->inbuf)>=1024) {
608
609
      log_info(LD_DIR,"Trying to extract information from wedged server desc "
               "download.");
610
      connection_dir_reached_eof(TO_DIR_CONN(conn));
611
612
    } else {
      connection_mark_for_close(conn);
613
    }
614
615
616
    return;
  }

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

620
621
622
  or_conn = TO_OR_CONN(conn);

  if (!conn->or_is_obsolete) {
623
    if (conn->timestamp_created + TIME_BEFORE_OR_CONN_IS_OBSOLETE < now) {
624
625
626
627
      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));
628
      conn->or_is_obsolete = 1;
629
    } else {
630
631
632
      or_connection_t *best =
        connection_or_get_by_identity_digest(or_conn->identity_digest);
      if (best && best != or_conn &&
633
          (conn->state == OR_CONN_STATE_OPEN ||
634
           now > conn->timestamp_created + TLS_HANDSHAKE_TIMEOUT)) {
635
636
          /* We only mark as obsolete connections that already are in
           * OR_CONN_STATE_OPEN, i.e. that have finished their TLS handshaking.
637
           * This is necessary because authorities judge whether a router is
638
639
640
641
           * 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.
           */
642
643
644
645
646
        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));
647
        conn->or_is_obsolete = 1;
648
      }
649
    }
650
651
  }

652
  if (conn->or_is_obsolete && !or_conn->n_circuits) {
653
    /* no unmarked circs -- mark it now */
654
655
    log_info(LD_OR,
             "Expiring non-used OR connection to fd %d (%s:%d) [Obsolete].",
656
             conn->s, conn->address, conn->port);
657
658
659
660
661
    connection_mark_for_close(conn);
    conn->hold_open_until_flushed = 1;
    return;
  }

662
663
  /* If we haven't written to an OR connection for a while, then either nuke
     the connection or send a keepalive, depending. */
664
  if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
665
    routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
666
    if (!connection_state_is_open(conn)) {
667
668
      log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
               conn->s,conn->address, conn->port);
669
      connection_mark_for_close(conn);
670
      conn->hold_open_until_flushed = 1;
671
    } else if (we_are_hibernating() && !or_conn->n_circuits &&
672
               !buf_datalen(conn->outbuf)) {
673
674
675
      log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
               "[Hibernating or exiting].",
               conn->s,conn->address, conn->port);
676
677
      connection_mark_for_close(conn);
      conn->hold_open_until_flushed = 1;
678
    } else if (!clique_mode(options) && !or_conn->n_circuits &&
679
680
               (!router || !server_mode(options) ||
                !router_is_clique_mode(router))) {
681
682
683
      log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
               "[Not in clique mode].",
               conn->s,conn->address, conn->port);
684
685
      connection_mark_for_close(conn);
      conn->hold_open_until_flushed = 1;
686
    } else if (
687
         now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
688
         now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
689
690
691
      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)",
692
             conn->s, conn->address, conn->port,
693
             (int)buf_datalen(conn->outbuf),
694
             (int)(now-conn->timestamp_lastwritten));
695
      connection_mark_for_close(conn);
696
    } else if (!buf_datalen(conn->outbuf)) {
697
      /* either in clique mode, or we've got a circuit. send a padding cell. */
698
      log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
Nick Mathewson's avatar
Nick Mathewson committed
699
700
701
             conn->address, conn->port);
      memset(&cell,0,sizeof(cell_t));
      cell.command = CELL_PADDING;
702
      connection_or_write_cell_to_buf(&cell, or_conn);
Nick Mathewson's avatar
Nick Mathewson committed
703
704
705
706
    }
  }
}

Roger Dingledine's avatar
Roger Dingledine committed
707
/** Perform regular maintenance tasks.  This function gets run once per
Nick Mathewson's avatar
Nick Mathewson committed
708
709
 * second by prepare_for_poll.
 */
710
711
712
static void
run_scheduled_events(time_t now)
{
713
  static time_t last_rotated_certificate = 0;
714
  static time_t time_to_check_listeners = 0;
715
  static time_t time_to_check_descriptor = 0;
716
  static time_t time_to_check_ipaddress = 0;
717
  static time_t time_to_shrink_buffers = 0;
718
719
  static time_t time_to_try_getting_descriptors = 0;
  static time_t time_to_reset_descriptor_failures = 0;
720
  static time_t time_to_add_entropy = 0;
721
  or_options_t *options = get_options();
Nick Mathewson's avatar
Nick Mathewson committed
722
  int i;
723
  int have_dir_info;
724

725
  /** 0. See if we've been asked to shut down and our timeout has
726
727
   * expired; or if our bandwidth limits are exhausted and we
   * should hibernate; or if it's time to wake up from hibernation.
728
   */
729
  consider_hibernation(now);
730

Roger Dingledine's avatar
Roger Dingledine committed
731
  /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
732
733
734
   *  shut down and restart all cpuworkers, and update the directory if
   *  necessary.
   */
735
736
  if (server_mode(options) &&
      get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
737
    log_info(LD_GENERAL,"Rotating onion key.");
738
739
    rotate_onion_key();
    cpuworkers_rotate();
740
    if (router_rebuild_descriptor(1)<0) {
741
      log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
742
    }
743
    if (advertised_server_mode())
744
      router_upload_dir_desc_to_dirservers(0);
745
746
  }

747
748
749
750
751
752
753
  if (time_to_try_getting_descriptors < now) {
    update_router_descriptor_downloads(now);
    time_to_try_getting_descriptors = now + DESCRIPTOR_RETRY_INTERVAL;
  }

  if (time_to_reset_descriptor_failures < now) {
    router_reset_descriptor_download_failures();
754
755
    time_to_reset_descriptor_failures =
      now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
756
757
  }

Roger Dingledine's avatar
Roger Dingledine committed
758
  /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
759
760
  if (!last_rotated_certificate)
    last_rotated_certificate = now;
761
  if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
762
    log_info(LD_GENERAL,"Rotating tls context.");
763
    if (tor_tls_context_new(get_identity_key(), options->Nickname,
764
                            MAX_SSL_KEY_LIFETIME) < 0) {
765
      log_warn(LD_BUG, "Error reinitializing TLS context");
Roger Dingledine's avatar
Roger Dingledine committed
766
      /* XXX is it a bug here, that we just keep going? */
767
768
769
    }
    last_rotated_certificate = now;
    /* XXXX We should rotate TLS connections as well; this code doesn't change
770
     *      them at all. */
771
772
  }

773
774
775
776
777
778
779
780
  if (time_to_add_entropy == 0)
    time_to_add_entropy = now + ENTROPY_INTERVAL;
  if (time_to_add_entropy < now) {
    /* We already seeded once, so don't die on failure. */
    crypto_seed_rng();
    time_to_add_entropy = now + ENTROPY_INTERVAL;
  }

781
782
  /** 1c. If we have to change the accounting interval or record
   * bandwidth used in this accounting interval, do so. */
783
  if (accounting_is_enabled(options))
784
    accounting_run_housekeeping(now);
785

786
787
  if (now % 10 == 0 && authdir_mode(options) && !we_are_hibernating()) {
    /* try to determine reachability */
788
    dirserv_test_reachability(0);
789
790
  }

791
792
793
  /** 2. Periodically, we consider getting a new directory, getting a
   * new running-routers list, and/or force-uploading our descriptor
   * (if we've passed our internal checks). */
794
  if (time_to_fetch_directory < now) {
795
    /* purge obsolete entries */
796
    routerlist_remove_old_routers();
797
    networkstatus_list_clean(now);
798

799
800
    /* Only caches actually need to fetch directories now. */
    if (options->DirPort && !options->V1AuthoritativeDir) {
801
802
      /* XXX actually, we should only do this if we want to advertise
       * our dirport. not simply if we configured one. -RD */
803
      if (any_trusted_dir_is_v1_authority())
804
        directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
805
    }
806

807
808
#define V1_DIR_FETCH_PERIOD (60*60)
    time_to_fetch_directory = now + V1_DIR_FETCH_PERIOD;
809

810
811
    /* Also, take this chance to remove old information from rephist
     * and the rend cache. */
812
    rep_history_clean(now - options->RephistTrackTime);
813
    rend_cache_clean();
814
815
816
    /* And while we are at it, save the state with bandwidth history
     * and more. */
    or_state_save();
Nick Mathewson's avatar
Nick Mathewson committed
817
818
  }

819
820
  /* Caches need to fetch running_routers; directory clients don't. */
  if (options->DirPort && time_to_fetch_running_routers < now) {
821
    if (!authdir_mode(options) || !options->V1AuthoritativeDir) {
822
      directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
Nick Mathewson's avatar
Nick Mathewson committed
823
    }
824
825
#define V1_RUNNINGROUTERS_FETCH_PERIOD (20*60)
    time_to_fetch_running_routers = now + V1_RUNNINGROUTERS_FETCH_PERIOD;
Nick Mathewson's avatar
Nick Mathewson committed
826
827
  }

Roger Dingledine's avatar
Roger Dingledine committed
828
829
  /* 2b. Once per minute, regenerate and upload the descriptor if the old
   * one is inaccurate. */
830
831
  if (time_to_check_descriptor < now) {
    time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
832
    check_descriptor_bandwidth_changed(now);
833
834
835
836
    if (time_to_check_ipaddress < now) {
      time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
      check_descriptor_ipaddress_changed(now);
    }
837
    mark_my_descriptor_dirty_if_older_than(
838
                                  now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
839
    consider_publishable_server(0);
840
841
    /* also, check religiously for reachability, if it's within the first
     * 20 minutes of our uptime. */
842
    if (server_mode(options) &&
Roger Dingledine's avatar
Roger Dingledine committed
843
        (has_completed_circuit || !any_predicted_circuits(now)) &&
844
845
        stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
        !we_are_hibernating())
846
      consider_testing_reachability();
847

848
849
850
851
852
    /* If any networkstatus documents are no longer recent, we need to
     * update all the descriptors' running status. */
    networkstatus_list_update_recent(now);
    routers_update_all_from_networkstatus();

853
854
855
    /* Also, once per minute, check whether we want to download any
     * networkstatus documents.
     */
856
    update_networkstatus_downloads(now);
857
858
  }

859
  /** 3a. Every second, we examine pending circuits and prune the
860
   *    ones which have been pending for more than a few seconds.
861
   *    We do this before step 4, so it can try building more if
862
863
   *    it's not comfortable with the number of available circuits.
   */
864
  circuit_expire_building(now);
865

866
  /** 3b. Also look at pending streams and prune the ones that 'began'
867
   *     a long time ago but haven't gotten a 'connected' yet.
868
   *     Do this before step 4, so we can put them back into pending
869