main.c 89.5 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-2012, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6

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

Nick Mathewson's avatar
Nick Mathewson committed
13
#define MAIN_PRIVATE
Roger Dingledine's avatar
Roger Dingledine committed
14
#include "or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
15
#include "buffers.h"
16
17
#include "channel.h"
#include "channeltls.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
18
#include "circuitbuild.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
19
#include "circuitlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
20
#include "circuituse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
21
#include "command.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
22
#include "config.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
23
#include "connection.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
24
#include "connection_edge.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
25
#include "connection_or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
26
#include "control.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
27
#include "cpuworker.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
28
#include "directory.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
29
#include "dirserv.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
30
#include "dirvote.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
31
#include "dns.h"
32
#include "dnsserv.h"
33
#include "entrynodes.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
34
#include "geoip.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
35
#include "hibernate.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
36
#include "main.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
37
#include "microdesc.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
38
#include "networkstatus.h"
39
#include "nodelist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
40
#include "ntmain.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
41
#include "onion.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
42
#include "policies.h"
43
#include "transports.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
44
#include "relay.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
45
#include "rendclient.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
46
#include "rendcommon.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
47
#include "rendservice.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
48
#include "rephist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
49
#include "router.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
50
#include "routerlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
51
#include "routerparse.h"
52
#include "statefile.h"
53
#include "status.h"
54
55
#ifdef USE_DMALLOC
#include <dmalloc.h>
56
#include <openssl/crypto.h>
57
#endif
58
#include "memarea.h"
59

60
61
62
63
64
65
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#endif

66
67
68
69
#ifdef USE_BUFFEREVENTS
#include <event2/bufferevent.h>
#endif

70
void evdns_shutdown(int);
Roger Dingledine's avatar
Roger Dingledine committed
71

72
/********* PROTOTYPES **********/
Roger Dingledine's avatar
Roger Dingledine committed
73

74
static void dumpmemusage(int severity);
75
static void dumpstats(int severity); /* log stats */
76
77
static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
78
static void second_elapsed_callback(periodic_timer_t *timer, void *args);
79
static int conn_close_if_marked(int i);
80
static void connection_start_reading_from_linked_conn(connection_t *conn);
81
static int connection_should_read_from_linked_conn(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
82

Roger Dingledine's avatar
Roger Dingledine committed
83
84
/********* START VARIABLES **********/

85
#ifndef USE_BUFFEREVENTS
86
int global_read_bucket; /**< Max number of bytes I can read this second. */
87
int global_write_bucket; /**< Max number of bytes I can write this second. */
Roger Dingledine's avatar
Roger Dingledine committed
88

89
90
91
92
/** 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;
93
/** What was the read bucket before the last second_elapsed_callback() call?
94
 * (used to determine how many bytes we've read). */
95
static int stats_prev_global_read_bucket;
96
/** What was the write bucket before the last second_elapsed_callback() call?
97
98
 * (used to determine how many bytes we've written). */
static int stats_prev_global_write_bucket;
99
100
#endif

101
/* DOCDOC stats_prev_n_read */
102
static uint64_t stats_prev_n_read = 0;
103
/* DOCDOC stats_prev_n_written */
104
105
static uint64_t stats_prev_n_written = 0;

106
/* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
107
/** How many bytes have we read since we started the process? */
108
static uint64_t stats_n_bytes_read = 0;
109
/** How many bytes have we written since we started the process? */
110
static uint64_t stats_n_bytes_written = 0;
111
/** What time did this process start up? */
112
time_t time_of_process_start = 0;
Roger Dingledine's avatar
Roger Dingledine committed
113
/** How many seconds have we been running? */
114
long stats_n_seconds_working = 0;
115
116
/** When do we next launch DNS wildcarding checks? */
static time_t time_to_check_for_correct_dns = 0;
117
118
119
120
121
122
123

/** 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;
124
125
/** How many times have we called newnym? */
static unsigned newnym_epoch = 0;
126

127
128
/** Smartlist of all open connections. */
static smartlist_t *connection_array = NULL;
129
130
/** List of connections that have been marked for close and need to be freed
 * and removed from connection_array. */
131
static smartlist_t *closeable_connection_lst = NULL;
132
133
/** List of linked connections that are currently reading data into their
 * inbuf from their partner's outbuf. */
134
static smartlist_t *active_linked_connection_lst = NULL;
135
136
137
/** 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. */
138
static int called_loop_once = 0;
Roger Dingledine's avatar
Roger Dingledine committed
139

Roger Dingledine's avatar
Roger Dingledine committed
140
/** We set this to 1 when we've opened a circuit, so we can print a log
141
142
143
144
145
146
 * entry to inform the user that Tor is working.  We set it to 0 when
 * we think the fact that we once opened a circuit doesn't mean we can do so
 * any longer (a big time jump happened, when we notice our directory is
 * heinously out-of-date, etc.
 */
int can_complete_circuit=0;
147

148
149
150
151
152
153
/** 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)
154
155
156
157
158
159
/** 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)

Roger Dingledine's avatar
Roger Dingledine committed
160
161
/** How long do we let OR connections handshake before we decide that
 * they are obsolete? */
162
#define TLS_HANDSHAKE_TIMEOUT (60)
163

164
165
166
167
168
169
/** Decides our behavior when no logs are configured/before any
 * logs have been configured.  For 0, we log notice to stdout as normal.
 * For 1, we log warnings only.  For 2, we log nothing.
 */
int quiet_level = 0;

Roger Dingledine's avatar
Roger Dingledine committed
170
171
/********* END VARIABLES ************/

Roger Dingledine's avatar
Roger Dingledine committed
172
173
174
/****************************************************************************
*
* This section contains accessors and other methods on the connection_array
175
* variables (which are global within this file and unavailable outside it).
Roger Dingledine's avatar
Roger Dingledine committed
176
177
178
*
****************************************************************************/

179
#if 0 && defined(USE_BUFFEREVENTS)
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
static void
free_old_inbuf(connection_t *conn)
{
  if (! conn->inbuf)
    return;

  tor_assert(conn->outbuf);
  tor_assert(buf_datalen(conn->inbuf) == 0);
  tor_assert(buf_datalen(conn->outbuf) == 0);
  buf_free(conn->inbuf);
  buf_free(conn->outbuf);
  conn->inbuf = conn->outbuf = NULL;

  if (conn->read_event) {
    event_del(conn->read_event);
    tor_event_free(conn->read_event);
  }
  if (conn->write_event) {
    event_del(conn->read_event);
    tor_event_free(conn->write_event);
  }
  conn->read_event = conn->write_event = NULL;
}
#endif

205
#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/** Remove the kernel-space send and receive buffers for <b>s</b>. For use
 * with IOCP only. */
static int
set_buffer_lengths_to_zero(tor_socket_t s)
{
  int zero = 0;
  int r = 0;
  if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&zero, sizeof(zero))) {
    log_warn(LD_NET, "Unable to clear SO_SNDBUF");
    r = -1;
  }
  if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&zero, sizeof(zero))) {
    log_warn(LD_NET, "Unable to clear SO_RCVBUF");
    r = -1;
  }
  return r;
}
#endif

Roger Dingledine's avatar
Roger Dingledine committed
225
/** Add <b>conn</b> to the array of connections that we can poll on.  The
226
227
228
 * connection's socket must be set; the connection starts out
 * non-reading and non-writing.
 */
229
int
230
connection_add_impl(connection_t *conn, int is_connecting)
231
{
Roger Dingledine's avatar
Roger Dingledine committed
232
  tor_assert(conn);
233
  tor_assert(SOCKET_OK(conn->s) ||
234
235
             conn->linked ||
             (conn->type == CONN_TYPE_AP &&
236
              TO_EDGE_CONN(conn)->is_dns_request));
Roger Dingledine's avatar
Roger Dingledine committed
237

238
  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
239
240
  conn->conn_array_index = smartlist_len(connection_array);
  smartlist_add(connection_array, conn);
Roger Dingledine's avatar
Roger Dingledine committed
241

242
#ifdef USE_BUFFEREVENTS
243
  if (connection_type_uses_bufferevent(conn)) {
244
    if (SOCKET_OK(conn->s) && !conn->linked) {
245

246
#ifdef _WIN32
247
248
249
250
251
252
      if (tor_libevent_using_iocp_bufferevents() &&
          get_options()->UserspaceIOCPBuffers) {
        set_buffer_lengths_to_zero(conn->s);
      }
#endif

253
      conn->bufev = bufferevent_socket_new(
254
255
256
                         tor_libevent_get_base(),
                         conn->s,
                         BEV_OPT_DEFER_CALLBACKS);
257
258
259
260
261
262
      if (!conn->bufev) {
        log_warn(LD_BUG, "Unable to create socket bufferevent");
        smartlist_del(connection_array, conn->conn_array_index);
        conn->conn_array_index = -1;
        return -1;
      }
263
264
265
266
267
268
269
270
      if (is_connecting) {
        /* Put the bufferevent into a "connecting" state so that we'll get
         * a "connected" event callback on successful write. */
        bufferevent_socket_connect(conn->bufev, NULL, 0);
      }
      connection_configure_bufferevent_callbacks(conn);
    } else if (conn->linked && conn->linked_conn &&
               connection_type_uses_bufferevent(conn->linked_conn)) {
Robert Ransom's avatar
Robert Ransom committed
271
      tor_assert(!(SOCKET_OK(conn->s)));
272
273
      if (!conn->bufev) {
        struct bufferevent *pair[2] = { NULL, NULL };
274
275
276
277
278
279
280
281
        if (bufferevent_pair_new(tor_libevent_get_base(),
                                 BEV_OPT_DEFER_CALLBACKS,
                                 pair) < 0) {
          log_warn(LD_BUG, "Unable to create bufferevent pair");
          smartlist_del(connection_array, conn->conn_array_index);
          conn->conn_array_index = -1;
          return -1;
        }
282
283
284
285
286
        tor_assert(pair[0]);
        conn->bufev = pair[0];
        conn->linked_conn->bufev = pair[1];
      } /* else the other side already was added, and got a bufferevent_pair */
      connection_configure_bufferevent_callbacks(conn);
287
288
    } else {
      tor_assert(!conn->linked);
289
290
    }

291
292
    if (conn->bufev)
      tor_assert(conn->inbuf == NULL);
293

294
295
    if (conn->linked_conn && conn->linked_conn->bufev)
      tor_assert(conn->linked_conn->inbuf == NULL);
296
  }
297
298
#else
  (void) is_connecting;
299
300
#endif

301
  if (!HAS_BUFFEREVENT(conn) && (SOCKET_OK(conn->s) || conn->linked)) {
302
303
304
305
    conn->read_event = tor_event_new(tor_libevent_get_base(),
         conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
    conn->write_event = tor_event_new(tor_libevent_get_base(),
         conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
306
    /* XXXX CHECK FOR NULL RETURN! */
307
  }
Roger Dingledine's avatar
Roger Dingledine committed
308

Roger Dingledine's avatar
Roger Dingledine committed
309
  log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
310
            conn_type_to_string(conn->type), (int)conn->s, conn->address,
311
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
312
313
314
315

  return 0;
}

316
317
318
319
320
321
/** Tell libevent that we don't care about <b>conn</b> any more. */
void
connection_unregister_events(connection_t *conn)
{
  if (conn->read_event) {
    if (event_del(conn->read_event))
322
      log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
323
324
325
326
    tor_free(conn->read_event);
  }
  if (conn->write_event) {
    if (event_del(conn->write_event))
327
      log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
328
329
    tor_free(conn->write_event);
  }
330
331
332
333
334
335
#ifdef USE_BUFFEREVENTS
  if (conn->bufev) {
    bufferevent_free(conn->bufev);
    conn->bufev = NULL;
  }
#endif
336
  if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
337
338
339
340
    dnsserv_close_listener(conn);
  }
}

Roger Dingledine's avatar
Roger Dingledine committed
341
/** Remove the connection from the global list, and remove the
Nick Mathewson's avatar
Nick Mathewson committed
342
343
344
 * corresponding poll entry.  Calling this function will shift the last
 * connection (if any) into the position occupied by conn.
 */
345
346
347
int
connection_remove(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
348
  int current_index;
349
  connection_t *tmp;
Roger Dingledine's avatar
Roger Dingledine committed
350

Roger Dingledine's avatar
Roger Dingledine committed
351
  tor_assert(conn);
Roger Dingledine's avatar
Roger Dingledine committed
352

353
  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
354
            (int)conn->s, conn_type_to_string(conn->type),
355
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
356

357
358
  tor_assert(conn->conn_array_index >= 0);
  current_index = conn->conn_array_index;
359
  connection_unregister_events(conn); /* This is redundant, but cheap. */
360
361
  if (current_index == smartlist_len(connection_array)-1) { /* at the end */
    smartlist_del(connection_array, current_index);
Roger Dingledine's avatar
Roger Dingledine committed
362
    return 0;
Roger Dingledine's avatar
Roger Dingledine committed
363
  }
Roger Dingledine's avatar
Roger Dingledine committed
364

365
  /* replace this one with the one at the end */
366
367
368
  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
369

Roger Dingledine's avatar
Roger Dingledine committed
370
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
371
372
}

373
/** If <b>conn</b> is an edge conn, remove it from the list
374
375
376
 * of conn's on this circuit. If it's not on an edge,
 * flush and send destroys for all circuits on this conn.
 *
377
378
 * Remove it from connection_array (if applicable) and
 * from closeable_connection_list.
379
380
381
 *
 * Then free it.
 */
382
static void
383
connection_unlink(connection_t *conn)
384
{
385
  connection_about_to_close_connection(conn);
386
  if (conn->conn_array_index >= 0) {
387
388
    connection_remove(conn);
  }
389
390
391
392
393
394
395
  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;
  }
396
  smartlist_remove(closeable_connection_lst, conn);
397
  smartlist_remove(active_linked_connection_lst, conn);
398
  if (conn->type == CONN_TYPE_EXIT) {
399
    assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
400
  }
401
402
403
  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));
404
405
406
407
408
409
410
411
412
413
414
415
    /* connection_unlink() can only get called if the connection
     * was already on the closeable list, and it got there by
     * connection_mark_for_close(), which was called from
     * connection_or_close_normally() or
     * connection_or_close_for_error(), so the channel should
     * already be in CHANNEL_STATE_CLOSING, and then the
     * connection_about_to_close_connection() goes to
     * connection_or_about_to_close(), which calls channel_closed()
     * to notify the channel_t layer, and closed the channel, so
     * nothing more to do here to deal with the channel associated
     * with an orconn.
     */
416
  }
417
418
419
  connection_free(conn);
}

420
/** Schedule <b>conn</b> to be closed. **/
421
422
423
424
425
void
add_connection_to_closeable_list(connection_t *conn)
{
  tor_assert(!smartlist_isin(closeable_connection_lst, conn));
  tor_assert(conn->marked_for_close);
426
  assert_connection_ok(conn, time(NULL));
427
428
429
  smartlist_add(closeable_connection_lst, conn);
}

430
/** Return 1 if conn is on the closeable list, else return 0. */
431
432
433
int
connection_is_on_closeable_list(connection_t *conn)
{
434
435
436
  return smartlist_isin(closeable_connection_lst, conn);
}

437
/** Return true iff conn is in the current poll array. */
438
439
440
int
connection_in_array(connection_t *conn)
{
441
  return smartlist_isin(connection_array, conn);
442
443
}

Roger Dingledine's avatar
Roger Dingledine committed
444
445
446
/** 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.
447
 */
448
449
smartlist_t *
get_connection_array(void)
450
{
451
  if (!connection_array)
452
    connection_array = smartlist_new();
453
  return connection_array;
454
455
}

456
457
458
459
460
461
462
463
/** Provides the traffic read and written over the life of the process. */

uint64_t
get_bytes_read(void)
{
  return stats_n_bytes_read;
}

464
/* DOCDOC get_bytes_written */
465
466
467
468
469
470
uint64_t
get_bytes_written(void)
{
  return stats_n_bytes_written;
}

471
/** Set the event mask on <b>conn</b> to <b>events</b>.  (The event
472
 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
473
 */
474
void
475
connection_watch_events(connection_t *conn, watchable_events_t events)
476
{
477
478
479
  IF_HAS_BUFFEREVENT(conn, {
      short ev = ((short)events) & (EV_READ|EV_WRITE);
      short old_ev = bufferevent_get_enabled(conn->bufev);
480
      if ((ev & ~old_ev) != 0) {
481
        bufferevent_enable(conn->bufev, ev);
482
483
      }
      if ((old_ev & ~ev) != 0) {
484
        bufferevent_disable(conn->bufev, old_ev & ~ev);
485
      }
486
487
      return;
  });
488
  if (events & READ_EVENT)
489
490
491
492
    connection_start_reading(conn);
  else
    connection_stop_reading(conn);

493
  if (events & WRITE_EVENT)
494
495
496
    connection_start_writing(conn);
  else
    connection_stop_writing(conn);
Roger Dingledine's avatar
Roger Dingledine committed
497
498
}

Roger Dingledine's avatar
Roger Dingledine committed
499
/** Return true iff <b>conn</b> is listening for read events. */
500
501
502
int
connection_is_reading(connection_t *conn)
{
503
  tor_assert(conn);
504

505
506
507
  IF_HAS_BUFFEREVENT(conn,
    return (bufferevent_get_enabled(conn->bufev) & EV_READ) != 0;
  );
508
509
  return conn->reading_from_linked_conn ||
    (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
Roger Dingledine's avatar
Roger Dingledine committed
510
511
}

Roger Dingledine's avatar
Roger Dingledine committed
512
/** Tell the main loop to stop notifying <b>conn</b> of any read events. */
513
514
515
void
connection_stop_reading(connection_t *conn)
{
516
  tor_assert(conn);
517
518
519
520
521
522

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_disable(conn->bufev, EV_READ);
      return;
  });

523
  tor_assert(conn->read_event);
524

525
526
527
528
529
530
531
  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",
532
               (int)conn->s,
533
534
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
535
536
}

Roger Dingledine's avatar
Roger Dingledine committed
537
/** Tell the main loop to start notifying <b>conn</b> of any read events. */
538
539
540
void
connection_start_reading(connection_t *conn)
{
541
  tor_assert(conn);
542
543
544
545
546
547

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_enable(conn->bufev, EV_READ);
      return;
  });

548
549
  tor_assert(conn->read_event);

550
551
552
553
554
555
556
557
  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",
558
               (int)conn->s,
559
560
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
561
562
}

Roger Dingledine's avatar
Roger Dingledine committed
563
/** Return true iff <b>conn</b> is listening for write events. */
564
565
566
int
connection_is_writing(connection_t *conn)
{
567
568
  tor_assert(conn);

569
570
571
572
  IF_HAS_BUFFEREVENT(conn,
    return (bufferevent_get_enabled(conn->bufev) & EV_WRITE) != 0;
  );

573
574
  return conn->writing_to_linked_conn ||
    (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
575
576
}

Roger Dingledine's avatar
Roger Dingledine committed
577
/** Tell the main loop to stop notifying <b>conn</b> of any write events. */
578
579
580
void
connection_stop_writing(connection_t *conn)
{
581
  tor_assert(conn);
582
583
584
585
586
587

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_disable(conn->bufev, EV_WRITE);
      return;
  });

588
589
  tor_assert(conn->write_event);

590
591
592
593
594
595
596
597
  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",
598
               (int)conn->s,
599
600
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
601
602
}

Roger Dingledine's avatar
Roger Dingledine committed
603
/** Tell the main loop to start notifying <b>conn</b> of any write events. */
604
605
606
void
connection_start_writing(connection_t *conn)
{
607
  tor_assert(conn);
608

609
610
611
612
613
  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_enable(conn->bufev, EV_WRITE);
      return;
  });

614
615
  tor_assert(conn->write_event);

616
617
618
619
620
621
622
623
624
  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",
625
               (int)conn->s,
626
627
628
629
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
}

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/** 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;
}

647
648
649
650
/** 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
651
652
653
654
655
656
657
658
659
660
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,
661
662
663
       * so we need to make sure that the event_base_loop() actually exits at
       * the end of its run through the current connections and lets us
       * activate read events for linked connections. */
664
      struct timeval tv = { 0, 0 };
665
      tor_event_base_loopexit(tor_libevent_get_base(), &tv);
666
667
668
669
670
671
    }
  } else {
    tor_assert(smartlist_isin(active_linked_connection_lst, conn));
  }
}

672
673
674
/** 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. */
675
676
677
678
679
680
681
682
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;
683
684
685
    /* 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. */
686
687
688
689
    smartlist_remove(active_linked_connection_lst, conn);
  } else {
    tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
  }
690
691
}

692
/** Close all connections that have been scheduled to get closed. */
693
694
695
696
697
698
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);
699
    if (conn->conn_array_index < 0) {
700
      connection_unlink(conn); /* blow it away right now */
701
    } else {
702
      if (!conn_close_if_marked(conn->conn_array_index))
703
704
        ++i;
    }
705
706
707
  }
}

708
709
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to read. */
710
static void
711
conn_read_callback(evutil_socket_t fd, short event, void *_conn)
712
713
{
  connection_t *conn = _conn;
714
715
  (void)fd;
  (void)event;
716

717
  log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
718

719
  /* assert_connection_ok(conn, time(NULL)); */
720
721
722

  if (connection_handle_read(conn) < 0) {
    if (!conn->marked_for_close) {
723
#ifndef _WIN32
724
      log_warn(LD_BUG,"Unhandled error on read for %s connection "
725
               "(fd %d); removing",
726
               conn_type_to_string(conn->type), (int)conn->s);
727
      tor_fragile_assert();
728
#endif
729
      if (CONN_IS_EDGE(conn))
730
        connection_edge_end_errno(TO_EDGE_CONN(conn));
731
732
733
734
735
736
737
738
739
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

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

740
741
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to write. */
742
static void
743
conn_write_callback(evutil_socket_t fd, short events, void *_conn)
744
745
{
  connection_t *conn = _conn;
746
747
  (void)fd;
  (void)events;
748

749
750
  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
                     (int)conn->s));
751

752
  /* assert_connection_ok(conn, time(NULL)); */
753

754
  if (connection_handle_write(conn, 0) < 0) {
755
756
    if (!conn->marked_for_close) {
      /* this connection is broken. remove it. */
757
      log_fn(LOG_WARN,LD_BUG,
Roger Dingledine's avatar
Roger Dingledine committed
758
             "unhandled error on write for %s connection (fd %d); removing",
759
             conn_type_to_string(conn->type), (int)conn->s);
760
      tor_fragile_assert();
761
762
      if (CONN_IS_EDGE(conn)) {
        /* otherwise we cry wolf about duplicate close */
763
764
765
        edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
        if (!edge_conn->end_reason)
          edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
766
        edge_conn->edge_has_sent_end = 1;
767
      }
768
      connection_close_immediate(conn); /* So we don't try to flush. */
769
770
771
772
773
774
775
776
777
      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
778
/** If the connection at connection_array[i] is marked for close, then:
779
780
781
782
783
 *    - 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.
784
 * Returns 1 if the connection was closed, 0 otherwise.
785
 */
786
787
788
static int
conn_close_if_marked(int i)
{
Roger Dingledine's avatar
Roger Dingledine committed
789
  connection_t *conn;
790
  int retval;
791
  time_t now;
Roger Dingledine's avatar
Roger Dingledine committed
792

793
  conn = smartlist_get(connection_array, i);
794
  if (!conn->marked_for_close)
795
    return 0; /* nothing to see here, move along */
796
797
  now = time(NULL);
  assert_connection_ok(conn, now);
798
  /* assert_all_pending_dns_resolves_ok(); */
799

800
#ifdef USE_BUFFEREVENTS
801
802
803
804
805
806
807
  if (conn->bufev) {
    if (conn->hold_open_until_flushed &&
        evbuffer_get_length(bufferevent_get_output(conn->bufev))) {
      /* don't close yet. */
      return 0;
    }
    if (conn->linked_conn && ! conn->linked_conn->marked_for_close) {
808
809
810
811
812
      /* We need to do this explicitly so that the linked connection
       * notices that there was an EOF. */
      bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
    }
  }
813
814
#endif

815
  log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",conn->s);
816
817
818

  /* If the connection we are about to close was trying to connect to
  a proxy server and failed, the client won't be able to use that
819
  proxy. We should warn the user about this. */
820
821
  if (conn->proxy_state == PROXY_INFANT)
    log_failed_proxy_connection(conn);
822

823
  IF_HAS_BUFFEREVENT(conn, goto unlink);
824
825
  if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
      connection_wants_to_flush(conn)) {
826
    /* s == -1 means it's an incomplete edge connection, or that the socket
827
     * has already been closed as unflushable. */
828
    ssize_t sz = connection_bucket_write_limit(conn, now);
829
    if (!conn->hold_open_until_flushed)
830
831
832
      log_info(LD_NET,
               "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
               "to flush %d bytes. (Marked at %s:%d)",
833
               escaped_safe_str_client(conn->address),
834
               (int)conn->s, conn_type_to_string(conn->type), conn->state,
835
836
               (int)conn->outbuf_flushlen,
                conn->marked_for_close_file, conn->marked_for_close);
837
838
839
840
841
842
843
844
    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);
      }
845
      log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
846
               "%d left; flushlen %d; wants-to-flush==%d", retval,
847
848
                (int)connection_get_outbuf_len(conn),
                (int)conn->outbuf_flushlen,
849
                connection_wants_to_flush(conn));
850
    } else if (connection_speaks_cells(conn)) {
851
      if (conn->state == OR_CONN_STATE_OPEN) {
852
        retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
853
                               &conn->outbuf_flushlen);
854
      } else
855
        retval = -1; /* never flush non-open broken tls connections */
856
    } else {
857
      retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
858
    }
859
860
861
    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)) {
862
      if (retval > 0) {
863
864
        LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
                           "Holding conn (fd %d) open for more flushing.",
865
                           (int)conn->s));
866
        conn->timestamp_lastwritten = now; /* reset so we can flush more */
867
868
869
870
      } else if (sz == 0) {
        /* Also, retval==0.  If we get here, we didn't want to write anything
         * (because of rate-limiting) and we didn't. */

871
872
873
874
875
876
        /* Connection must flush before closing, but it's being rate-limited.
         * Let's remove from Libevent, and mark it as blocked on bandwidth
         * so it will be re-added on next token bucket refill. Prevents
         * busy Libevent loops where we keep ending up here and returning
         * 0 until we are no longer blocked on bandwidth.
         */
877
878
879
880
        if (connection_is_writing(conn)) {
          conn->write_blocked_on_bw = 1;
          connection_stop_writing(conn);
        }
881
        if (connection_is_reading(conn)) {
882
883
884
885
886
887
888
          /* XXXX024 We should make this code unreachable; if a connection is
           * marked for close and flushing, there is no point in reading to it
           * at all. Further, checking at this point is a bit of a hack: it
           * would make much more sense to react in
           * connection_handle_read_impl, or to just stop reading in
           * mark_and_flush */
#if 0
889
890
891
892
893
894
895
896
897
898
#define MARKED_READING_RATE 180
          static ratelim_t marked_read_lim = RATELIM_INIT(MARKED_READING_RATE);
          char *m;
          if ((m = rate_limit_log(&marked_read_lim, now))) {
            log_warn(LD_BUG, "Marked connection (fd %d, type %s, state %s) "
                     "is still reading; that shouldn't happen.%s",
                     (int)conn->s, conn_type_to_string(conn->type),
                     conn_state_to_string(conn->type, conn->state), m);
            tor_free(m);
          }
899
#endif
900
901
902
          conn->read_blocked_on_bw = 1;
          connection_stop_reading(conn);
        }
903
      }
904
      return 0;
905
    }
906
    if (connection_wants_to_flush(conn)) {
907
908
      int severity;
      if (conn->type == CONN_TYPE_EXIT ||
909
          (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
910
911
912
913
          (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
        severity = LOG_INFO;
      else
        severity = LOG_NOTICE;
914
915
916
      /* 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 "
917
             "bytes to address %s.  If this happens a lot, either "
918
919
920
             "something is wrong with your network connection, or "
             "something is wrong with theirs. "
             "(fd %d, type %s, state %d, marked at %s:%d).",
921
             (int)connection_get_outbuf_len(conn),
922
             escaped_safe_str_client(conn->address),
923
             (int)conn->s, conn_type_to_string(conn->type), conn->state,
924
             conn->marked_for_close_file,
925
             conn->marked_for_close);
Roger Dingledine's avatar
Roger Dingledine committed
926
927
    }
  }
928
929
930
931

#ifdef USE_BUFFEREVENTS
 unlink:
#endif
932
  connection_unlink(conn); /* unlink, remove, free */
933
  return 1;
Roger Dingledine's avatar
Roger Dingledine committed
934
935
}

936
937
938
939
940
941
/** 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.
 */
942
943
944
void
directory_all_unreachable(time_t now)
{
945
  connection_t *conn;
946
  (void)now;
947

948
  stats_n_seconds_working=0; /* reset it */
949
950
951

  while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
                                              AP_CONN_STATE_CIRCUIT_WAIT))) {
952
    entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
953
954
955
    log_notice(LD_NET,
               "Is your network connection down? "
               "Failing connection to '%s:%d'.",
956
957
958
               safe_str_client(entry_conn->socks_request->address),
               entry_conn->socks_request->port);
    connection_mark_unattached_ap(entry_conn,
959
                                  END_STREAM_REASON_NET_UNREACHABLE);
960
  }
961
  control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
962
963
}

964
965
/** This function is called whenever we successfully pull down some new
 * network statuses or server descriptors. */
966
void
967
directory_info_has_arrived(time_t now, int from_cache)
968
{